Skip to content

Commit

Permalink
fix!: refactor keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-pousette committed Dec 19, 2023
1 parent 743db18 commit 2bb0734
Show file tree
Hide file tree
Showing 87 changed files with 2,612 additions and 2,279 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
Expand Up @@ -177,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
86 changes: 44 additions & 42 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 @@ -34,9 +36,8 @@ import * as pubsub from "./pubsub.js";
import * as connection from "./connection.js";
import { v4 as uuid } from "uuid";
import { TypedEventEmitter } from "@libp2p/interface";

import { X25519Keypair } from "@peerbit/crypto";
import { serialize, deserialize } from "@dao-xyz/borsh";
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 @@ -117,7 +118,7 @@ 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;
Expand Down Expand Up @@ -255,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 @@ -386,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
23 changes: 13 additions & 10 deletions packages/clients/peerbit-proxy/interface/src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AnyStore } from "@peerbit/any-store";
import { PeerId } from "@libp2p/interface/peer-id";
import { Multiaddr } from "@multiformats/multiaddr";
import { Blocks } from "@peerbit/blocks-interface";
import { Keychain, Ed25519Keypair } from "@peerbit/crypto";
import { Ed25519Keypair } from "@peerbit/crypto";
import { DataEvent, PubSub, PublishEvent } from "@peerbit/pubsub-interface";
import { ProgramClient } from "@peerbit/program";
import * as blocks from "./blocks.js";
Expand All @@ -15,7 +15,8 @@ import * as network from "./network.js";
import * as pubsub from "./pubsub.js";
import * as connection from "./connection.js";
import { CustomEvent } from "@libp2p/interface";
import { serialize, deserialize } from "@dao-xyz/borsh";
import { serialize, deserialize, AbstractType } from "@dao-xyz/borsh";
import { Keychain } from "@peerbit/keychain";

const levelKey = (level: string[]) => JSON.stringify(level);

Expand Down Expand Up @@ -75,13 +76,9 @@ export class PeerbitProxyHost implements ProgramClient {
return this.hostClient.dial(address);
}

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

get memory(): AnyStore {
return this.hostClient.memory;
}
Expand Down Expand Up @@ -145,20 +142,26 @@ export class PeerbitProxyHost implements ProgramClient {
await this.respond(
message,
new keychain.RESP_ExportKeypairById(
await this.keychain?.exportById(message.keyId, message.type)
await this.services.keychain?.exportById(
message.keyId,
message.type as AbstractType<any>
)
),
from
);
} else if (message instanceof keychain.REQ_ExportKeypairByKey) {
await this.respond(
message,
new keychain.RESP_ExportKeypairByKey(
await this.keychain?.exportByKey(message.publicKey.key)
await this.services.keychain?.exportByKey(message.publicKey.key)
),
from
);
} else if (message instanceof keychain.REQ_ImportKey) {
await this.keychain?.import(message.keypair, message.keyId);
await this.services.keychain?.import({
keypair: message.keypair,
id: message.keyId
});
await this.respond(
message,
new keychain.RESP_ImportKey(message.messageId),
Expand Down
Loading

0 comments on commit 2bb0734

Please sign in to comment.