Skip to content

Commit

Permalink
Merge pull request #226 from dao-xyz/refactor-keychain-2
Browse files Browse the repository at this point in the history
fix!: update libp2p to v1
  • Loading branch information
marcus-pousette authored Dec 20, 2023
2 parents bb94af7 + 854ced3 commit 9f2a149
Show file tree
Hide file tree
Showing 97 changed files with 3,079 additions and 2,610 deletions.
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

<br>
<p align="center">
<img width="140" src="./docs/peerbit-logo.png" alt="Peerbit icon Icon">
Expand Down Expand Up @@ -26,48 +25,67 @@ Your database schema can remain very simple but still utilize P2P networks, auto

## Optimized for performance

Peerbit is performant, so performant in fact you can use it for [streaming video](https://stream.dao.xyz) by having peers subscribing to database updates. In a low latency setting, you can achieve around 1000 replications a second and have a thoughput of 100 MB/s.
Peerbit is performant, so performant in fact you can use it for [streaming video](https://stream.dao.xyz) by having peers subscribing to database updates. In a low latency setting, you can achieve around 1000 replications a second and have a thoughput of 100 MB/s.

![Dogestream](/docs/videostream.gif)


## Other examples

### [Chat room](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/one-chat-room/)

[<img src="https://github.com/dao-xyz/peerbit-examples/blob/master/packages/one-chat-room/demo.gif" width="600" />](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/one-chat-room/)

### [Lobby + chat rooms](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/many-chat-rooms/)

[<img src="https://github.com/dao-xyz/peerbit-examples/blob/master/packages/many-chat-rooms/demo.gif" width="600" />](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/many-chat-rooms/)

### [Sync files](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/file-share/)

#### [React app](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/file-share/)

[<img src="https://github.com/dao-xyz/peerbit-examples/blob/master/packages/file-share/demo-frontend.gif" width="600" />](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/file-share/)

#### [CLI](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/file-share/)
[<img src="https://github.com/dao-xyz/peerbit-examples/blob/master/packages/file-share/demo-cli.gif" width="600" />](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/file-share/)


[<img src="https://github.com/dao-xyz/peerbit-examples/blob/master/packages/file-share/demo-cli.gif" width="600" />](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/file-share/)

### [Collaborative machine learning](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/collaborative-learning/)
[<img src="https://github.com/dao-xyz/peerbit-examples/blob/master/packages/collaborative-learning/demo.gif" width="600" />](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/collaborative-learning/)


[<img src="https://github.com/dao-xyz/peerbit-examples/blob/master/packages/collaborative-learning/demo.gif" width="600" />](https://github.com/dao-xyz/peerbit-examples/tree/master/packages/collaborative-learning/)

## Get Started

1. Install Peerbit by following the simple setup instructions in our [Installation Guide](https://peerbit.org/#/getting-started).

2. Dive into our comprehensive [Documentation](https://peerbit.org/#/modules/client/) or checkout the [Example repository](https://github.com/dao-xyz/peerbit-examples) to explore the powerful features and learn how to leverage Peerbit to its fullest potential.
2. Dive into our comprehensive [Documentation](https://peerbit.org/#/modules/client/) or checkout the [Example repository](https://github.com/dao-xyz/peerbit-examples) to explore the powerful features and learn how to leverage Peerbit to its fullest potential.

3. Join us on [Matrix](https://matrix.to/#/#peerbit:matrix.org) to connect, share ideas, and collaborate with like-minded individuals.

## Contribute

Peerbit is an open-source project, and we welcome contributions from developers like you! Feel free to contribute code, report issues, and submit feature requests. Together, let's shape the future of Peerbit.

IMPORTANT: Peerbit uses yarn.

1. Check yarn version: `yarn -v` should print 1.something
2. Install: `yarn`
3. Build: `yarn build`
4. Run tests: `yarn test`

You might possibly need to CMD + Shift + P and then enter to restart the typescript server after the build step.

To create a new package, follow the following steps:

1. Clone the time folder within /packages/utils/time to the desired destination and rename it
2. Update the package.json `name`, `description`, `version` fields
3. Possibly add other depencencies to the package.json `dependencies` field (like `@peerbit/crypto`)
4. Delete contents in CHANGELOG.md
5. Update the root package.json `workspaces.packages` field
6. Update root lerna.json `workspaces.packages` field
7. run yarn once in root

We recommend running tests with the VS Code integration though: https://marketplace.visualstudio.com/items?itemName=firsttris.vscode-jest-runner

## Let's Get Coding!

[peerbit.org](https://peerbit.org)


2 changes: 1 addition & 1 deletion docs/modules/program/rpc/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class RPCTest extends Program<Args> {
args?.role instanceof Responder
? (hello, from) => {
return new World();
}
}
: undefined // only create a response handler if we are to respond to requests
});
}
Expand Down
3 changes: 2 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"packages/utils/uint8arrays",
"packages/utils/cache",
"packages/utils/time",
"packages/utils/logger"
"packages/utils/logger",
"packages/utils/keychain"
],
"version": "independent",
"npmClient": "yarn"
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"packages/utils/uint8arrays",
"packages/utils/time",
"packages/utils/cache",
"packages/utils/logger"
"packages/utils/logger",
"packages/utils/keychain"
]
},
"engines": {
Expand Down Expand Up @@ -97,7 +98,8 @@
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"patch-package": "^8.0.0"
},
"version": "0.0.0"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Node } from "../connection.js";
import { waitForResolved } from "@peerbit/time";
import { EventEmitter } from "@libp2p/interface/events";
import { TypedEventEmitter } from "@libp2p/interface";

import { EventEmitterNode } from "./utils.js";

const testNodes = async (a: Node, b: Node, c: Node) => {
Expand Down Expand Up @@ -43,7 +44,7 @@ const testNodes = async (a: Node, b: Node, c: Node) => {

describe("index", () => {
it("event-emitter-node", async () => {
const events = new EventEmitter();
const events = new TypedEventEmitter();
const a = new EventEmitterNode(events);
const b = new EventEmitterNode(events);
const c = new EventEmitterNode(events);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Ed25519Keypair, X25519Keypair } from "@peerbit/crypto";
import { delay, waitForResolved } from "@peerbit/time";
import { GetSubscribers } from "@peerbit/pubsub-interface";
import { deserialize } from "@dao-xyz/borsh";
import { EventEmitter } from "@libp2p/interface/events";
import { TypedEventEmitter } from "@libp2p/interface";

import { DataMessage } from "@peerbit/stream-interface";
import { PeerbitProxyClient } from "../client.js";
import { EventEmitterNode } from "./utils.js";
Expand All @@ -27,7 +28,7 @@ describe("index", () => {
hostWithClients = [];
session = await TestSession.disconnected(2);

const events = new EventEmitter();
const events = new TypedEventEmitter();
for (let i = 0; i < 2; i++) {
const host = new PeerbitProxyHost(
session.peers[i],
Expand Down Expand Up @@ -176,34 +177,44 @@ describe("index", () => {
it("ed25519", async () => {
const keypair = await Ed25519Keypair.create();
const id = new Uint8Array([1, 2, 3]);
await client1.keychain.import(keypair, id);
await client1.services.keychain.import({ keypair, id });
expect(
(await client1.keychain.exportById(id, "ed25519"))?.equals(keypair)
(
await client1.services.keychain.exportById(id, Ed25519Keypair)
)?.equals(keypair)
).toBeTrue();
expect(
(await host1.keychain.exportById(id, "ed25519"))?.equals(keypair)
(await host1.services.keychain.exportById(id, Ed25519Keypair))?.equals(
keypair
)
).toBeTrue();
expect(
(await client1.keychain.exportByKey(keypair.publicKey))?.equals(keypair)
(
await client1.services.keychain.exportByKey(keypair.publicKey)
)?.equals(keypair)
).toBeTrue();
expect(
(await host1.keychain.exportByKey(keypair.publicKey))?.equals(keypair)
(await host1.services.keychain.exportByKey(keypair.publicKey))?.equals(
keypair
)
).toBeTrue();
});

it("x25519", async () => {
const keypair = await Ed25519Keypair.create();
const id = new Uint8Array([1, 2, 3]);
await client1.keychain.import(keypair, id);
await client1.services.keychain.import({ keypair, id });
const xkeypair = await X25519Keypair.from(keypair);
expect(
(await client1.keychain.exportByKey(xkeypair.publicKey))?.equals(
xkeypair
)
(
await client1.services.keychain.exportByKey(xkeypair.publicKey)
)?.equals(xkeypair)
).toBeTrue();

expect(
(await host1.keychain.exportByKey(xkeypair.publicKey))?.equals(xkeypair)
(await host1.services.keychain.exportByKey(xkeypair.publicKey))?.equals(
xkeypair
)
).toBeTrue();
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as connection from "../connection.js";
import { CustomEvent } from "@libp2p/interface/events";
import type { TypedEventTarget } from "@libp2p/interface/events";
import { CustomEvent } from "@libp2p/interface";
import type { TypedEventTarget } from "@libp2p/interface";

export class EventEmitterNode extends connection.MessageNode {
constructor(
Expand Down
89 changes: 46 additions & 43 deletions packages/clients/peerbit-proxy/interface/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { Blocks } from "@peerbit/blocks-interface";
import {
Identity,
Ed25519PublicKey,
Keychain,
Ed25519Keypair,
PublicSignKey,
X25519PublicKey,
KeypairFromPublicKey,
randomBytes,
toBase64,
ready
ready,
PublicKeyEncryptionKey,
Secp256k1PublicKey,
Ed25519Keypair,
Secp256k1Keypair,
X25519Keypair,
ByteKey
} from "@peerbit/crypto";
import { PubSub, PubSubEvents } from "@peerbit/pubsub-interface";
import {
Expand All @@ -33,9 +35,9 @@ import * as network from "./network.js";
import * as pubsub from "./pubsub.js";
import * as connection from "./connection.js";
import { v4 as uuid } from "uuid";
import { EventEmitter } from "@libp2p/interface/events";
import { X25519Keypair } from "@peerbit/crypto";
import { serialize, deserialize } from "@dao-xyz/borsh";
import { TypedEventEmitter } from "@libp2p/interface";
import { serialize, deserialize, AbstractType } from "@dao-xyz/borsh";
import { Keychain, KeypairFromPublicKey } from "@peerbit/keychain";

const messageIdString = (messageId: Uint8Array) => sha256Base64(messageId);
const levelKey = (level: string[]) => JSON.stringify(level);
Expand Down Expand Up @@ -116,13 +118,13 @@ export class PeerbitProxyClient implements ProgramClient {
identity: Identity<Ed25519PublicKey>;

private _multiaddr: Multiaddr[];
private _services: { pubsub: PubSub; blocks: Blocks };
private _services: { pubsub: PubSub; blocks: Blocks; keychain: Keychain };
private _keychain: Keychain;
private _memory: AnyStore;
private _handler: ProgramHandler;

constructor(readonly messages: connection.Node) {
const pubsubEventEmitter = new EventEmitter();
const pubsubEventEmitter = new TypedEventEmitter();
const eventListenerSubscribeCounter: Map<
string,
{ messageId: string; counter: number }
Expand Down Expand Up @@ -254,36 +256,40 @@ export class PeerbitProxyClient implements ProgramClient {
new blocks.REQ_BlockWaitFor(publicKey)
);
}
}
};
this._keychain = {
exportById: async <
T = "ed25519" | "x25519",
Q = T extends "ed25519" ? Ed25519Keypair : X25519Keypair
>(
id: Uint8Array,
type: T
) => {
const resp = await this.request<keychain.RESP_ExportKeypairById>(
new keychain.REQ_ExportKeypairById(id, type as "ed25519" | "x25519")
);
return resp.keypair as Q;
},
exportByKey: async <
T extends Ed25519PublicKey | X25519PublicKey,
Q = KeypairFromPublicKey<T>
>(
publicKey: T
) => {
const resp = await this.request<keychain.RESP_ExportKeypairByKey>(
new keychain.REQ_ExportKeypairByKey(publicKey)
);
return resp.keypair as Q;
},
import: async (keypair, id) => {
await this.request<keychain.RESP_ImportKey>(
new keychain.REQ_ImportKey(keypair, id)
);
keychain: {
exportById: async <
T extends Ed25519Keypair | Secp256k1Keypair | X25519Keypair | ByteKey
>(
id: Uint8Array,
type: AbstractType<T>
) => {
const resp = await this.request<keychain.RESP_ExportKeypairById>(
new keychain.REQ_ExportKeypairById(id, type)
);
return resp.keypair?.key as T;
},
exportByKey: async <
T extends
| Ed25519PublicKey
| X25519PublicKey
| Secp256k1PublicKey
| PublicSignKey
| PublicKeyEncryptionKey,
Q = KeypairFromPublicKey<T>
>(
publicKey: T
) => {
const resp = await this.request<keychain.RESP_ExportKeypairByKey>(
new keychain.REQ_ExportKeypairByKey(publicKey)
);
return resp.keypair?.key as Q;
},
import: async (properties: { keypair; id }) => {
await this.request<keychain.RESP_ImportKey>(
new keychain.REQ_ImportKey(properties.keypair, properties.id)
);
}
}
};
const levelMap: Map<string, AnyStore> = new Map();
Expand Down Expand Up @@ -385,12 +391,9 @@ export class PeerbitProxyClient implements ProgramClient {
return response.value;
}

get services(): { pubsub: PubSub; blocks: Blocks } {
get services(): { pubsub: PubSub; blocks: Blocks; keychain: Keychain } {
return this._services;
}
get keychain(): Keychain {
return this._keychain;
}

get memory(): AnyStore {
return this._memory;
Expand Down
Loading

0 comments on commit 9f2a149

Please sign in to comment.