@peerbit/keychain 1.0.32 → 1.1.0-0fddff8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ import { type Constructor } from "@dao-xyz/borsh";
2
+ import { type AnyStore } from "@peerbit/any-store";
3
+ import { type ByteKey, Ed25519Keypair, type Ed25519PublicKey, type PublicKeyEncryptionKey, type PublicSignKey, type Secp256k1Keypair, type Secp256k1PublicKey, X25519Keypair, type X25519PublicKey } from "@peerbit/crypto";
4
+ import { type CryptoKeychain, type KeyParameters, type KeypairFromPublicKey, type KeypairParameters } from "./interface.js";
5
+ export declare class DefaultCryptoKeychain implements CryptoKeychain {
6
+ readonly properties: {
7
+ store: AnyStore;
8
+ };
9
+ constructor(properties?: {
10
+ store: AnyStore;
11
+ });
12
+ import(parameters: (KeypairParameters | KeyParameters) & {
13
+ id?: Uint8Array;
14
+ }): Promise<void>;
15
+ exportByKey<T extends X25519PublicKey | Ed25519PublicKey | Secp256k1PublicKey | PublicSignKey | PublicKeyEncryptionKey, Q = KeypairFromPublicKey<T>>(publicKey: T): Promise<Q | undefined>;
16
+ exportById<T = Ed25519Keypair | Secp256k1Keypair | X25519Keypair | ByteKey>(id: Uint8Array, type: Constructor<T>): Promise<T | undefined>;
17
+ start(): Promise<void>;
18
+ stop(): Promise<void>;
19
+ }
20
+ //# sourceMappingURL=crypto.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAA0B,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EACN,KAAK,OAAO,EACZ,cAAc,EACd,KAAK,gBAAgB,EAErB,KAAK,sBAAsB,EAC3B,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,aAAa,EACb,KAAK,eAAe,EAEpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,MAAM,gBAAgB,CAAC;AAExB,qBAAa,qBAAsB,YAAW,cAAc;IAE1D,QAAQ,CAAC,UAAU,EAAE;QAAE,KAAK,EAAE,QAAQ,CAAA;KAAE;gBAA/B,UAAU,GAAE;QAAE,KAAK,EAAE,QAAQ,CAAA;KAA6B;IAG9D,MAAM,CACX,UAAU,EAAE,CAAC,iBAAiB,GAAG,aAAa,CAAC,GAAG;QAAE,EAAE,CAAC,EAAE,UAAU,CAAA;KAAE,GACnE,OAAO,CAAC,IAAI,CAAC;IAyBV,WAAW,CAChB,CAAC,SACE,eAAe,GACf,gBAAgB,GAChB,kBAAkB,GAClB,aAAa,GACb,sBAAsB,EACzB,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,EAC1B,SAAS,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAQjC,UAAU,CACf,CAAC,GAAG,cAAc,GAAG,gBAAgB,GAAG,aAAa,GAAG,OAAO,EAC9D,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAiBzD,KAAK;IAIL,IAAI;CAGV"}
@@ -0,0 +1,60 @@
1
+ import { deserialize, serialize } from "@dao-xyz/borsh";
2
+ import {} from "@peerbit/any-store";
3
+ import { createStore } from "@peerbit/any-store";
4
+ import { Ed25519Keypair, Keypair, X25519Keypair, toBase64, } from "@peerbit/crypto";
5
+ import {} from "./interface.js";
6
+ export class DefaultCryptoKeychain {
7
+ properties;
8
+ constructor(properties = { store: createStore() }) {
9
+ this.properties = properties;
10
+ }
11
+ async import(parameters) {
12
+ let bytes;
13
+ let publicKey;
14
+ if (parameters.keypair) {
15
+ const kp = parameters.keypair;
16
+ bytes = serialize(kp);
17
+ publicKey = serialize(kp.publicKey);
18
+ if (kp instanceof Ed25519Keypair) {
19
+ // also import as X25519Keypair for convenience
20
+ const xkp = await X25519Keypair.from(kp);
21
+ await this.import({ keypair: xkp });
22
+ }
23
+ }
24
+ else {
25
+ bytes = serialize(parameters.key);
26
+ }
27
+ if (parameters.id) {
28
+ await this.properties.store.put(toBase64(parameters.id), bytes);
29
+ }
30
+ if (publicKey) {
31
+ await this.properties.store.put(toBase64(publicKey), bytes);
32
+ }
33
+ }
34
+ async exportByKey(publicKey) {
35
+ const key = await this.properties.store.get(toBase64(serialize(publicKey)));
36
+ if (key) {
37
+ return deserialize(key, Keypair);
38
+ }
39
+ return undefined;
40
+ }
41
+ async exportById(id, type) {
42
+ const key = await this.properties.store.get(toBase64(id));
43
+ if (key) {
44
+ const maybeConvert = type === X25519Keypair;
45
+ const exported = deserialize(key, maybeConvert ? Keypair : type);
46
+ if (maybeConvert && exported instanceof Ed25519Keypair) {
47
+ return X25519Keypair.from(exported);
48
+ }
49
+ return exported;
50
+ }
51
+ return undefined;
52
+ }
53
+ async start() {
54
+ await this.properties.store.open();
55
+ }
56
+ async stop() {
57
+ await this.properties.store.close();
58
+ }
59
+ }
60
+ //# sourceMappingURL=crypto.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"crypto.js","sourceRoot":"","sources":["../../src/crypto.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,WAAW,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAiB,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAEN,cAAc,EAEd,OAAO,EAKP,aAAa,EAEb,QAAQ,GACR,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAKN,MAAM,gBAAgB,CAAC;AAExB,MAAM,OAAO,qBAAqB;IAEvB;IADV,YACU,aAAkC,EAAE,KAAK,EAAE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AAG,EAAE,OAAO,CAAM,CAAC;QACvC,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,UAAU,CAEd,EAAc,EAAE,IAAoB;QACrC,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;QAC1D,IAAI,GAAG,EAAE,CAAC;YACT,MAAM,YAAY,GAAI,IAAY,KAAK,aAAa,CAAC;YACrD,MAAM,QAAQ,GAAG,WAAW,CAC3B,GAAG,EACH,YAAY,CAAC,CAAC,CAAE,OAA0B,CAAC,CAAC,CAAC,IAAI,CACjD,CAAC;YAEF,IAAI,YAAY,IAAI,QAAQ,YAAY,cAAc,EAAE,CAAC;gBACxD,OAAO,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAM,CAAC;YAC1C,CAAC;YACD,OAAO,QAAa,CAAC;QACtB,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,KAAK;QACV,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,IAAI;QACT,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrC,CAAC;CACD"}
@@ -1,22 +1,4 @@
1
- import { type Constructor } from "@dao-xyz/borsh";
2
- import { type AnyStore } from "@peerbit/any-store";
3
- import { type ByteKey, Ed25519Keypair, type Ed25519PublicKey, Keypair, type PublicKeyEncryptionKey, type PublicSignKey, type Secp256k1Keypair, type Secp256k1PublicKey, X25519Keypair, type X25519PublicKey } from "@peerbit/crypto";
4
- import { type KeyParameters, type Keychain, type KeypairParameters } from "./interface.js";
5
- export type { Keychain };
6
- export type KeypairFromPublicKey<T> = T extends X25519PublicKey ? X25519Keypair : T extends Ed25519PublicKey ? Ed25519Keypair : T extends Secp256k1PublicKey ? Secp256k1Keypair : T extends PublicSignKey | PublicKeyEncryptionKey ? Keypair : never;
7
- export declare class DefaultKeychain implements Keychain {
8
- readonly properties: {
9
- store: AnyStore;
10
- };
11
- constructor(properties?: {
12
- store: AnyStore;
13
- });
14
- import(parameters: (KeypairParameters | KeyParameters) & {
15
- id?: Uint8Array;
16
- }): Promise<void>;
17
- exportByKey<T extends X25519PublicKey | Ed25519PublicKey | Secp256k1PublicKey | PublicSignKey | PublicKeyEncryptionKey, Q = KeypairFromPublicKey<T>>(publicKey: T): Promise<Q | undefined>;
18
- exportById<T = Ed25519Keypair | Secp256k1Keypair | X25519Keypair | ByteKey>(id: Uint8Array, type: Constructor<T>): Promise<T | undefined>;
19
- start(): Promise<void>;
20
- stop(): Promise<void>;
21
- }
1
+ export * from "./crypto.js";
2
+ export * from "./interface.js";
3
+ export * from "./libp2p.js";
22
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAA0B,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EACN,KAAK,OAAO,EACZ,cAAc,EACd,KAAK,gBAAgB,EACrB,OAAO,EACP,KAAK,sBAAsB,EAC3B,KAAK,aAAa,EAClB,KAAK,gBAAgB,EACrB,KAAK,kBAAkB,EACvB,aAAa,EACb,KAAK,eAAe,EAEpB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,KAAK,aAAa,EAClB,KAAK,QAAQ,EACb,KAAK,iBAAiB,EACtB,MAAM,gBAAgB,CAAC;AAExB,YAAY,EAAE,QAAQ,EAAE,CAAC;AAEzB,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI,CAAC,SAAS,eAAe,GAC5D,aAAa,GACb,CAAC,SAAS,gBAAgB,GACzB,cAAc,GACd,CAAC,SAAS,kBAAkB,GAC3B,gBAAgB,GAChB,CAAC,SAAS,aAAa,GAAG,sBAAsB,GAC/C,OAAO,GACP,KAAK,CAAC;AAEZ,qBAAa,eAAgB,YAAW,QAAQ;IAE9C,QAAQ,CAAC,UAAU,EAAE;QAAE,KAAK,EAAE,QAAQ,CAAA;KAAE;gBAA/B,UAAU,GAAE;QAAE,KAAK,EAAE,QAAQ,CAAA;KAA6B;IAE9D,MAAM,CACX,UAAU,EAAE,CAAC,iBAAiB,GAAG,aAAa,CAAC,GAAG;QAAE,EAAE,CAAC,EAAE,UAAU,CAAA;KAAE,GACnE,OAAO,CAAC,IAAI,CAAC;IAyBV,WAAW,CAChB,CAAC,SACE,eAAe,GACf,gBAAgB,GAChB,kBAAkB,GAClB,aAAa,GACb,sBAAsB,EACzB,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,EAC1B,SAAS,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAQjC,UAAU,CACf,CAAC,GAAG,cAAc,GAAG,gBAAgB,GAAG,aAAa,GAAG,OAAO,EAC9D,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,WAAW,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAiBzD,KAAK;IAIL,IAAI;CAGV"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
package/dist/src/index.js CHANGED
@@ -1,60 +1,4 @@
1
- import { deserialize, serialize } from "@dao-xyz/borsh";
2
- import {} from "@peerbit/any-store";
3
- import { createStore } from "@peerbit/any-store";
4
- import { Ed25519Keypair, Keypair, X25519Keypair, toBase64, } from "@peerbit/crypto";
5
- import {} from "./interface.js";
6
- export class DefaultKeychain {
7
- properties;
8
- constructor(properties = { store: createStore() }) {
9
- this.properties = properties;
10
- }
11
- async import(parameters) {
12
- let bytes;
13
- let publicKey;
14
- if (parameters.keypair) {
15
- const kp = parameters.keypair;
16
- bytes = serialize(kp);
17
- publicKey = serialize(kp.publicKey);
18
- if (kp instanceof Ed25519Keypair) {
19
- // also import as X25519Keypair for convenience
20
- const xkp = await X25519Keypair.from(kp);
21
- await this.import({ keypair: xkp });
22
- }
23
- }
24
- else {
25
- bytes = serialize(parameters.key);
26
- }
27
- if (parameters.id) {
28
- await this.properties.store.put(toBase64(parameters.id), bytes);
29
- }
30
- if (publicKey) {
31
- await this.properties.store.put(toBase64(publicKey), bytes);
32
- }
33
- }
34
- async exportByKey(publicKey) {
35
- const key = await this.properties.store.get(toBase64(serialize(publicKey)));
36
- if (key) {
37
- return deserialize(key, Keypair);
38
- }
39
- return undefined;
40
- }
41
- async exportById(id, type) {
42
- const key = await this.properties.store.get(toBase64(id));
43
- if (key) {
44
- const maybeConvert = type === X25519Keypair;
45
- const exported = deserialize(key, maybeConvert ? Keypair : type);
46
- if (maybeConvert && exported instanceof Ed25519Keypair) {
47
- return X25519Keypair.from(exported);
48
- }
49
- return exported;
50
- }
51
- return undefined;
52
- }
53
- async start() {
54
- await this.properties.store.open();
55
- }
56
- async stop() {
57
- await this.properties.store.close();
58
- }
59
- }
1
+ export * from "./crypto.js";
2
+ export * from "./interface.js";
3
+ export * from "./libp2p.js";
60
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,WAAW,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC1E,OAAO,EAAiB,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAEN,cAAc,EAEd,OAAO,EAKP,aAAa,EAEb,QAAQ,GACR,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAIN,MAAM,gBAAgB,CAAC;AAcxB,MAAM,OAAO,eAAe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AAG,EAAE,CAAC;YACT,OAAO,WAAW,CAAC,GAAG,EAAE,OAAO,CAAM,CAAC;QACvC,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,UAAU,CAEd,EAAc,EAAE,IAAoB;QACrC,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;QAC1D,IAAI,GAAG,EAAE,CAAC;YACT,MAAM,YAAY,GAAI,IAAY,KAAK,aAAa,CAAC;YACrD,MAAM,QAAQ,GAAG,WAAW,CAC3B,GAAG,EACH,YAAY,CAAC,CAAC,CAAE,OAA0B,CAAC,CAAC,CAAC,IAAI,CACjD,CAAC;YAEF,IAAI,YAAY,IAAI,QAAQ,YAAY,cAAc,EAAE,CAAC;gBACxD,OAAO,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAM,CAAC;YAC1C,CAAC;YACD,OAAO,QAAa,CAAC;QACtB,CAAC;QACD,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,KAAK,CAAC,KAAK;QACV,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,IAAI;QACT,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrC,CAAC;CACD"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { type AbstractType } from "@dao-xyz/borsh";
2
+ import { type Keychain as ILibp2pKeychain } from "@libp2p/keychain";
2
3
  import type { ByteKey, Ed25519Keypair, Ed25519PublicKey, Keypair, PublicKeyEncryptionKey, PublicSignKey, Secp256k1Keypair, Secp256k1PublicKey, X25519Keypair, X25519PublicKey } from "@peerbit/crypto";
3
4
  export type KeypairFromPublicKey<T> = T extends X25519PublicKey ? X25519Keypair : T extends Ed25519PublicKey ? Ed25519Keypair : T extends Secp256k1PublicKey ? Secp256k1Keypair : T extends PublicSignKey | PublicKeyEncryptionKey ? Keypair : never;
4
5
  export type KeypairParameters = {
@@ -7,11 +8,14 @@ export type KeypairParameters = {
7
8
  export type KeyParameters = {
8
9
  key: ByteKey;
9
10
  };
10
- export interface Keychain {
11
+ export interface CryptoKeychain {
11
12
  import(parameters: (KeypairParameters | KeyParameters) & {
12
13
  id: Uint8Array;
13
14
  }): Promise<void>;
14
15
  exportByKey<T extends Ed25519PublicKey | X25519PublicKey | Secp256k1PublicKey | PublicSignKey | PublicKeyEncryptionKey, Q = KeypairFromPublicKey<T>>(publicKey: T): Promise<Q | undefined>;
15
16
  exportById<T extends Ed25519Keypair | Secp256k1Keypair | X25519Keypair | ByteKey>(id: Uint8Array, type: AbstractType<T>): Promise<T | undefined>;
17
+ start(): Promise<void>;
18
+ stop(): Promise<void>;
16
19
  }
20
+ export type IPeerbitKeychain = CryptoKeychain & ILibp2pKeychain;
17
21
  //# sourceMappingURL=interface.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../src/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,KAAK,EACX,OAAO,EACP,cAAc,EACd,gBAAgB,EAChB,OAAO,EACP,sBAAsB,EACtB,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,aAAa,EACb,eAAe,EACf,MAAM,iBAAiB,CAAC;AAEzB,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI,CAAC,SAAS,eAAe,GAC5D,aAAa,GACb,CAAC,SAAS,gBAAgB,GACzB,cAAc,GACd,CAAC,SAAS,kBAAkB,GAC3B,gBAAgB,GAChB,CAAC,SAAS,aAAa,GAAG,sBAAsB,GA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AAS,gBAAgB,GACzB,cAAc,GACd,CAAC,SAAS,kBAAkB,GAC3B,gBAAgB,GAChB,CAAC,SAAS,aAAa,GAAG,sBAAsB,GAC/C,OAAO,GACP,KAAK,CAAC;AAEZ,MAAM,MAAM,iBAAiB,GAAG;IAC/B,OAAO,EAAE,cAAc,GAAG,aAAa,GAAG,gBAAgB,GAAG,OAAO,CAAC;CACrE,CAAC;AACF,MAAM,MAAM,aAAa,GAAG;IAAE,GAAG,EAAE,OAAO,CAAA;CAAE,CAAC;AAE7C,MAAM,WAAW,cAAc;IAE9B,MAAM,CACL,UAAU,EAAE,CAAC,iBAAiB,GAAG,aAAa,CAAC,GAAG;QAAE,EAAE,EAAE,UAAU,CAAA;KAAE,GAClE,OAAO,CAAC,IAAI,CAAC,CAAC;IAGjB,WAAW,CACV,CAAC,SACE,gBAAgB,GAChB,eAAe,GACf,kBAAkB,GAClB,aAAa,GACb,sBAAsB,EACzB,CAAC,GAAG,oBAAoB,CAAC,CAAC,CAAC,EAE3B,SAAS,EAAE,CAAC,GACV,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAK1B,UAAU,CACT,CAAC,SAAS,cAAc,GAAG,gBAAgB,GAAG,aAAa,GAAG,OAAO,EAErE,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GACnB,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IAE1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB;AAED,MAAM,MAAM,gBAAgB,GAAG,cAAc,GAAG,eAAe,CAAC"}
@@ -1,2 +1,3 @@
1
1
  import {} from "@dao-xyz/borsh";
2
+ import {} from "@libp2p/keychain";
2
3
  //# sourceMappingURL=interface.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"interface.js","sourceRoot":"","sources":["../../src/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"interface.js","sourceRoot":"","sources":["../../src/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAoC,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { Constructor } from "@dao-xyz/borsh";
2
+ import type { PrivateKey } from "@libp2p/interface";
3
+ import { type Keychain as ILibp2pKeychain, type KeyInfo, type KeychainComponents, type KeychainInit } from "@libp2p/keychain";
4
+ import { type AnyStore } from "@peerbit/any-store";
5
+ import type { ByteKey, Ed25519Keypair, Ed25519PublicKey, PublicKeyEncryptionKey, PublicSignKey, Secp256k1Keypair, Secp256k1PublicKey, X25519Keypair, X25519PublicKey } from "@peerbit/crypto/dist/src";
6
+ import type { CryptoKeychain, KeyParameters, KeypairFromPublicKey, KeypairParameters } from "./interface.js";
7
+ export declare class PeerbitKeychain implements CryptoKeychain, ILibp2pKeychain {
8
+ private libp2p;
9
+ private crypto;
10
+ constructor(components: KeychainComponents, init: {
11
+ libp2p?: KeychainInit;
12
+ crypto?: {
13
+ store: AnyStore;
14
+ } | CryptoKeychain;
15
+ });
16
+ import(parameters: (KeypairParameters | KeyParameters) & {
17
+ id: Uint8Array;
18
+ }): Promise<void>;
19
+ exportByKey<T extends Ed25519PublicKey | X25519PublicKey | Secp256k1PublicKey | PublicSignKey | PublicKeyEncryptionKey, Q = KeypairFromPublicKey<T>>(publicKey: T): Promise<Q | undefined>;
20
+ exportById<T extends Ed25519Keypair | Secp256k1Keypair | X25519Keypair | ByteKey>(id: Uint8Array, type: Constructor<T>): Promise<T | undefined>;
21
+ findKeyByName(name: string): Promise<KeyInfo>;
22
+ findKeyById(id: string): Promise<KeyInfo>;
23
+ importKey(name: string, key: PrivateKey): Promise<KeyInfo>;
24
+ exportKey(name: string): Promise<PrivateKey>;
25
+ removeKey(name: string): Promise<KeyInfo>;
26
+ renameKey(oldName: string, newName: string): Promise<KeyInfo>;
27
+ listKeys(): Promise<KeyInfo[]>;
28
+ rotateKeychainPass(oldPass: string, newPass: string): Promise<void>;
29
+ start(): Promise<void>;
30
+ stop(): Promise<void>;
31
+ }
32
+ export declare function keychain(init?: {
33
+ libp2p: KeychainInit;
34
+ crypto?: CryptoKeychain | {
35
+ store: AnyStore;
36
+ };
37
+ }): (components: KeychainComponents) => PeerbitKeychain;
38
+ //# sourceMappingURL=libp2p.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"libp2p.d.ts","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EACN,KAAK,QAAQ,IAAI,eAAe,EAChC,KAAK,OAAO,EACZ,KAAK,kBAAkB,EACvB,KAAK,YAAY,EAEjB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,KAAK,QAAQ,EAAe,MAAM,oBAAoB,CAAC;AAChE,OAAO,KAAK,EACX,OAAO,EACP,cAAc,EACd,gBAAgB,EAChB,sBAAsB,EACtB,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,aAAa,EACb,eAAe,EACf,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EACX,cAAc,EACd,aAAa,EACb,oBAAoB,EACpB,iBAAiB,EACjB,MAAM,gBAAgB,CAAC;AAExB,qBAAa,eAAgB,YAAW,cAAc,EAAE,eAAe;IACtE,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,MAAM,CAAiB;gBAG9B,UAAU,EAAE,kBAAkB,EAC9B,IAAI,EAAE;QACL,MAAM,CAAC,EAAE,YAAY,CAAC;QACtB,MAAM,CAAC,EAAE;YAAE,KAAK,EAAE,QAAQ,CAAA;SAAE,GAAG,cAAc,CAAC;KAC9C;IAQF,MAAM,CACL,UAAU,EAAE,CAAC,iBAA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C;IACrB,MAAM,CAAC,EAAE,cAAc,GAAG;QAAE,KAAK,EAAE,QAAQ,CAAA;KAAE,CAAC;CACK,GAClD,CAAC,UAAU,EAAE,kBAAkB,KAAK,eAAe,CAIrD"}
@@ -0,0 +1,59 @@
1
+ import { keychain as libp2pKeyChain, } from "@libp2p/keychain";
2
+ import { createStore } from "@peerbit/any-store";
3
+ import { DefaultCryptoKeychain } from "./crypto.js";
4
+ export class PeerbitKeychain {
5
+ libp2p;
6
+ crypto;
7
+ constructor(components, init) {
8
+ this.libp2p = libp2pKeyChain(init.libp2p)(components);
9
+ this.crypto =
10
+ init.crypto instanceof DefaultCryptoKeychain
11
+ ? init.crypto
12
+ : new DefaultCryptoKeychain({ store: createStore() });
13
+ }
14
+ import(parameters) {
15
+ return this.crypto.import(parameters);
16
+ }
17
+ exportByKey(publicKey) {
18
+ return this.crypto.exportByKey(publicKey);
19
+ }
20
+ exportById(id, type) {
21
+ return this.crypto.exportById(id, type);
22
+ }
23
+ findKeyByName(name) {
24
+ return this.libp2p.findKeyByName(name);
25
+ }
26
+ findKeyById(id) {
27
+ return this.libp2p.findKeyById(id);
28
+ }
29
+ importKey(name, key) {
30
+ return this.libp2p.importKey(name, key);
31
+ }
32
+ exportKey(name) {
33
+ return this.libp2p.exportKey(name);
34
+ }
35
+ removeKey(name) {
36
+ return this.libp2p.removeKey(name);
37
+ }
38
+ renameKey(oldName, newName) {
39
+ return this.libp2p.renameKey(oldName, newName);
40
+ }
41
+ listKeys() {
42
+ return this.libp2p.listKeys();
43
+ }
44
+ rotateKeychainPass(oldPass, newPass) {
45
+ return this.libp2p.rotateKeychainPass(oldPass, newPass);
46
+ }
47
+ async start() {
48
+ await this.crypto.start();
49
+ }
50
+ async stop() {
51
+ await this.crypto.stop();
52
+ }
53
+ }
54
+ export function keychain(init = { libp2p: {}, crypto: { store: createStore() } }) {
55
+ return (components) => {
56
+ return new PeerbitKeychain(components, init);
57
+ };
58
+ }
59
+ //# sourceMappingURL=libp2p.js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,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACzC,CAAC;IACD,SAAS,CAAC,IAAY;QACrB,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IACD,SAAS,CAAC,IAAY;QACrB,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IACpC,CAAC;IACD,SAAS,CAAC,OAAe,EAAE,OAAe;QACzC,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAChD,CAAC;IACD,QAAQ;QACP,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC/B,CAAC;IACD,kBAAkB,CAAC,OAAe,EAAE,OAAe;QAClD,OAAO,IAAI,CAAC,MAAM,CAAC,kBAAkB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACzD,CAAC;IACD,KAAK,CAAC,KAAK;QACV,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC;IACD,KAAK,CAAC,IAAI;QACT,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC1B,CAAC;CACD;AAED,MAAM,UAAU,QAAQ,CACvB,OAGI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE,EAAE;IAEpD,OAAO,CAAC,UAA8B,EAAE,EAAE;QACzC,OAAO,IAAI,eAAe,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IAC9C,CAAC,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,63 +1,64 @@
1
1
  {
2
- "name": "@peerbit/keychain",
3
- "version": "1.0.32",
4
- "description": "Utility functions for keychain",
5
- "sideEffects": false,
6
- "type": "module",
7
- "types": "./dist/src/index.d.ts",
8
- "typesVersions": {
9
- "*": {
10
- "*": [
11
- "*",
12
- "dist/*",
13
- "dist/src/*",
14
- "dist/src/*/index"
15
- ],
16
- "src/*": [
17
- "*",
18
- "dist/*",
19
- "dist/src/*",
20
- "dist/src/*/index"
21
- ]
22
- }
23
- },
24
- "files": [
25
- "src",
26
- "dist",
27
- "!dist/test",
28
- "!**/*.tsbuildinfo"
29
- ],
30
- "exports": {
31
- ".": {
32
- "types": "./dist/src/index.d.ts",
33
- "import": "./dist/src/index.js"
34
- }
35
- },
36
- "eslintConfig": {
37
- "extends": "peerbit",
38
- "parserOptions": {
39
- "project": true,
40
- "sourceType": "module"
41
- },
42
- "ignorePatterns": [
43
- "!.aegir.js",
44
- "test/ts-use",
45
- "*.d.ts"
46
- ]
47
- },
48
- "publishConfig": {
49
- "access": "public"
50
- },
51
- "scripts": {
52
- "clean": "aegir clean",
53
- "build": "aegir build --no-bundle",
54
- "test": "aegir test",
55
- "lint": "aegir lint"
56
- },
57
- "author": "dao.xyz",
58
- "license": "MIT",
59
- "dependencies": {
60
- "@peerbit/crypto": "^2.3.11",
61
- "@peerbit/any-store": "^2.1.14"
62
- }
2
+ "name": "@peerbit/keychain",
3
+ "version": "1.1.0-0fddff8",
4
+ "description": "Utility functions for keychain",
5
+ "sideEffects": false,
6
+ "type": "module",
7
+ "types": "./dist/src/index.d.ts",
8
+ "typesVersions": {
9
+ "*": {
10
+ "*": [
11
+ "*",
12
+ "dist/*",
13
+ "dist/src/*",
14
+ "dist/src/*/index"
15
+ ],
16
+ "src/*": [
17
+ "*",
18
+ "dist/*",
19
+ "dist/src/*",
20
+ "dist/src/*/index"
21
+ ]
22
+ }
23
+ },
24
+ "files": [
25
+ "src",
26
+ "dist",
27
+ "!dist/test",
28
+ "!**/*.tsbuildinfo"
29
+ ],
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/src/index.d.ts",
33
+ "import": "./dist/src/index.js"
34
+ }
35
+ },
36
+ "eslintConfig": {
37
+ "extends": "peerbit",
38
+ "parserOptions": {
39
+ "project": true,
40
+ "sourceType": "module"
41
+ },
42
+ "ignorePatterns": [
43
+ "!.aegir.js",
44
+ "test/ts-use",
45
+ "*.d.ts"
46
+ ]
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "scripts": {
52
+ "clean": "aegir clean",
53
+ "build": "aegir build --no-bundle",
54
+ "test": "aegir test",
55
+ "lint": "aegir lint"
56
+ },
57
+ "author": "dao.xyz",
58
+ "license": "MIT",
59
+ "dependencies": {
60
+ "@peerbit/crypto": "2.3.11-0fddff8",
61
+ "@peerbit/any-store": "2.1.14-0fddff8",
62
+ "@libp2p/keychain": "^5.2.9"
63
+ }
63
64
  }
package/src/crypto.ts ADDED
@@ -0,0 +1,98 @@
1
+ import { type Constructor, deserialize, serialize } from "@dao-xyz/borsh";
2
+ import { type AnyStore } from "@peerbit/any-store";
3
+ import { createStore } from "@peerbit/any-store";
4
+ import {
5
+ type ByteKey,
6
+ Ed25519Keypair,
7
+ type Ed25519PublicKey,
8
+ Keypair,
9
+ type PublicKeyEncryptionKey,
10
+ type PublicSignKey,
11
+ type Secp256k1Keypair,
12
+ type Secp256k1PublicKey,
13
+ X25519Keypair,
14
+ type X25519PublicKey,
15
+ toBase64,
16
+ } from "@peerbit/crypto";
17
+ import {
18
+ type CryptoKeychain,
19
+ type KeyParameters,
20
+ type KeypairFromPublicKey,
21
+ type KeypairParameters,
22
+ } from "./interface.js";
23
+
24
+ export class DefaultCryptoKeychain implements CryptoKeychain {
25
+ constructor(
26
+ readonly properties: { store: AnyStore } = { store: createStore() },
27
+ ) {}
28
+
29
+ async import(
30
+ parameters: (KeypairParameters | KeyParameters) & { id?: Uint8Array },
31
+ ): Promise<void> {
32
+ let bytes: Uint8Array;
33
+ let publicKey: Uint8Array | undefined;
34
+ if ((parameters as KeypairParameters).keypair) {
35
+ const kp = (parameters as KeypairParameters).keypair;
36
+ bytes = serialize(kp);
37
+ publicKey = serialize(kp.publicKey);
38
+
39
+ if (kp instanceof Ed25519Keypair) {
40
+ // also import as X25519Keypair for convenience
41
+ const xkp = await X25519Keypair.from(kp);
42
+ await this.import({ keypair: xkp });
43
+ }
44
+ } else {
45
+ bytes = serialize((parameters as KeyParameters).key);
46
+ }
47
+
48
+ if (parameters.id) {
49
+ await this.properties.store.put(toBase64(parameters.id), bytes);
50
+ }
51
+
52
+ if (publicKey) {
53
+ await this.properties.store.put(toBase64(publicKey), bytes);
54
+ }
55
+ }
56
+ async exportByKey<
57
+ T extends
58
+ | X25519PublicKey
59
+ | Ed25519PublicKey
60
+ | Secp256k1PublicKey
61
+ | PublicSignKey
62
+ | PublicKeyEncryptionKey,
63
+ Q = KeypairFromPublicKey<T>,
64
+ >(publicKey: T): Promise<Q | undefined> {
65
+ const key = await this.properties.store.get(toBase64(serialize(publicKey)));
66
+ if (key) {
67
+ return deserialize(key, Keypair) as Q;
68
+ }
69
+ return undefined;
70
+ }
71
+
72
+ async exportById<
73
+ T = Ed25519Keypair | Secp256k1Keypair | X25519Keypair | ByteKey,
74
+ >(id: Uint8Array, type: Constructor<T>): Promise<T | undefined> {
75
+ const key = await this.properties.store.get(toBase64(id));
76
+ if (key) {
77
+ const maybeConvert = (type as any) === X25519Keypair;
78
+ const exported = deserialize(
79
+ key,
80
+ maybeConvert ? (Keypair as Constructor<T>) : type,
81
+ );
82
+
83
+ if (maybeConvert && exported instanceof Ed25519Keypair) {
84
+ return X25519Keypair.from(exported) as T;
85
+ }
86
+ return exported as T;
87
+ }
88
+ return undefined;
89
+ }
90
+
91
+ async start() {
92
+ await this.properties.store.open();
93
+ }
94
+
95
+ async stop() {
96
+ await this.properties.store.close();
97
+ }
98
+ }
package/src/index.ts CHANGED
@@ -1,108 +1,3 @@
1
- import { type Constructor, deserialize, serialize } from "@dao-xyz/borsh";
2
- import { type AnyStore } from "@peerbit/any-store";
3
- import { createStore } from "@peerbit/any-store";
4
- import {
5
- type ByteKey,
6
- Ed25519Keypair,
7
- type Ed25519PublicKey,
8
- Keypair,
9
- type PublicKeyEncryptionKey,
10
- type PublicSignKey,
11
- type Secp256k1Keypair,
12
- type Secp256k1PublicKey,
13
- X25519Keypair,
14
- type X25519PublicKey,
15
- toBase64,
16
- } from "@peerbit/crypto";
17
- import {
18
- type KeyParameters,
19
- type Keychain,
20
- type KeypairParameters,
21
- } from "./interface.js";
22
-
23
- export type { Keychain };
24
-
25
- export type KeypairFromPublicKey<T> = T extends X25519PublicKey
26
- ? X25519Keypair
27
- : T extends Ed25519PublicKey
28
- ? Ed25519Keypair
29
- : T extends Secp256k1PublicKey
30
- ? Secp256k1Keypair
31
- : T extends PublicSignKey | PublicKeyEncryptionKey
32
- ? Keypair
33
- : never;
34
-
35
- export class DefaultKeychain implements Keychain {
36
- constructor(
37
- readonly properties: { store: AnyStore } = { store: createStore() },
38
- ) {}
39
- async import(
40
- parameters: (KeypairParameters | KeyParameters) & { id?: Uint8Array },
41
- ): Promise<void> {
42
- let bytes: Uint8Array;
43
- let publicKey: Uint8Array | undefined;
44
- if ((parameters as KeypairParameters).keypair) {
45
- const kp = (parameters as KeypairParameters).keypair;
46
- bytes = serialize(kp);
47
- publicKey = serialize(kp.publicKey);
48
-
49
- if (kp instanceof Ed25519Keypair) {
50
- // also import as X25519Keypair for convenience
51
- const xkp = await X25519Keypair.from(kp);
52
- await this.import({ keypair: xkp });
53
- }
54
- } else {
55
- bytes = serialize((parameters as KeyParameters).key);
56
- }
57
-
58
- if (parameters.id) {
59
- await this.properties.store.put(toBase64(parameters.id), bytes);
60
- }
61
-
62
- if (publicKey) {
63
- await this.properties.store.put(toBase64(publicKey), bytes);
64
- }
65
- }
66
- async exportByKey<
67
- T extends
68
- | X25519PublicKey
69
- | Ed25519PublicKey
70
- | Secp256k1PublicKey
71
- | PublicSignKey
72
- | PublicKeyEncryptionKey,
73
- Q = KeypairFromPublicKey<T>,
74
- >(publicKey: T): Promise<Q | undefined> {
75
- const key = await this.properties.store.get(toBase64(serialize(publicKey)));
76
- if (key) {
77
- return deserialize(key, Keypair) as Q;
78
- }
79
- return undefined;
80
- }
81
-
82
- async exportById<
83
- T = Ed25519Keypair | Secp256k1Keypair | X25519Keypair | ByteKey,
84
- >(id: Uint8Array, type: Constructor<T>): Promise<T | undefined> {
85
- const key = await this.properties.store.get(toBase64(id));
86
- if (key) {
87
- const maybeConvert = (type as any) === X25519Keypair;
88
- const exported = deserialize(
89
- key,
90
- maybeConvert ? (Keypair as Constructor<T>) : type,
91
- );
92
-
93
- if (maybeConvert && exported instanceof Ed25519Keypair) {
94
- return X25519Keypair.from(exported) as T;
95
- }
96
- return exported as T;
97
- }
98
- return undefined;
99
- }
100
-
101
- async start() {
102
- await this.properties.store.open();
103
- }
104
-
105
- async stop() {
106
- await this.properties.store.close();
107
- }
108
- }
1
+ export * from "./crypto.js";
2
+ export * from "./interface.js";
3
+ export * from "./libp2p.js";
package/src/interface.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { type AbstractType } from "@dao-xyz/borsh";
2
+ import { type Keychain as ILibp2pKeychain } from "@libp2p/keychain";
2
3
  import type {
3
4
  ByteKey,
4
5
  Ed25519Keypair,
@@ -27,7 +28,7 @@ export type KeypairParameters = {
27
28
  };
28
29
  export type KeyParameters = { key: ByteKey };
29
30
 
30
- export interface Keychain {
31
+ export interface CryptoKeychain {
31
32
  // Add a key to the keychain.
32
33
  import(
33
34
  parameters: (KeypairParameters | KeyParameters) & { id: Uint8Array },
@@ -55,4 +56,9 @@ export interface Keychain {
55
56
  id: Uint8Array,
56
57
  type: AbstractType<T>,
57
58
  ): Promise<T | undefined>;
59
+
60
+ start(): Promise<void>;
61
+ stop(): Promise<void>;
58
62
  }
63
+
64
+ export type IPeerbitKeychain = CryptoKeychain & ILibp2pKeychain;
package/src/libp2p.ts ADDED
@@ -0,0 +1,109 @@
1
+ import type { Constructor } from "@dao-xyz/borsh";
2
+ import type { PrivateKey } from "@libp2p/interface";
3
+ import {
4
+ type Keychain as ILibp2pKeychain,
5
+ type KeyInfo,
6
+ type KeychainComponents,
7
+ type KeychainInit,
8
+ keychain as libp2pKeyChain,
9
+ } from "@libp2p/keychain";
10
+ import { type AnyStore, createStore } from "@peerbit/any-store";
11
+ import type {
12
+ ByteKey,
13
+ Ed25519Keypair,
14
+ Ed25519PublicKey,
15
+ PublicKeyEncryptionKey,
16
+ PublicSignKey,
17
+ Secp256k1Keypair,
18
+ Secp256k1PublicKey,
19
+ X25519Keypair,
20
+ X25519PublicKey,
21
+ } from "@peerbit/crypto/dist/src";
22
+ import { DefaultCryptoKeychain } from "./crypto.js";
23
+ import type {
24
+ CryptoKeychain,
25
+ KeyParameters,
26
+ KeypairFromPublicKey,
27
+ KeypairParameters,
28
+ } from "./interface.js";
29
+
30
+ export class PeerbitKeychain implements CryptoKeychain, ILibp2pKeychain {
31
+ private libp2p: ILibp2pKeychain;
32
+ private crypto: CryptoKeychain;
33
+
34
+ constructor(
35
+ components: KeychainComponents,
36
+ init: {
37
+ libp2p?: KeychainInit;
38
+ crypto?: { store: AnyStore } | CryptoKeychain;
39
+ },
40
+ ) {
41
+ this.libp2p = libp2pKeyChain(init.libp2p)(components);
42
+ this.crypto =
43
+ init.crypto instanceof DefaultCryptoKeychain
44
+ ? init.crypto
45
+ : new DefaultCryptoKeychain({ store: createStore() });
46
+ }
47
+ import(
48
+ parameters: (KeypairParameters | KeyParameters) & { id: Uint8Array },
49
+ ): Promise<void> {
50
+ return this.crypto.import(parameters);
51
+ }
52
+ exportByKey<
53
+ T extends
54
+ | Ed25519PublicKey
55
+ | X25519PublicKey
56
+ | Secp256k1PublicKey
57
+ | PublicSignKey
58
+ | PublicKeyEncryptionKey,
59
+ Q = KeypairFromPublicKey<T>,
60
+ >(publicKey: T): Promise<Q | undefined> {
61
+ return this.crypto.exportByKey(publicKey);
62
+ }
63
+ exportById<
64
+ T extends Ed25519Keypair | Secp256k1Keypair | X25519Keypair | ByteKey,
65
+ >(id: Uint8Array, type: Constructor<T>): Promise<T | undefined> {
66
+ return this.crypto.exportById(id, type);
67
+ }
68
+ findKeyByName(name: string): Promise<KeyInfo> {
69
+ return this.libp2p.findKeyByName(name);
70
+ }
71
+ findKeyById(id: string): Promise<KeyInfo> {
72
+ return this.libp2p.findKeyById(id);
73
+ }
74
+ importKey(name: string, key: PrivateKey): Promise<KeyInfo> {
75
+ return this.libp2p.importKey(name, key);
76
+ }
77
+ exportKey(name: string): Promise<PrivateKey> {
78
+ return this.libp2p.exportKey(name);
79
+ }
80
+ removeKey(name: string): Promise<KeyInfo> {
81
+ return this.libp2p.removeKey(name);
82
+ }
83
+ renameKey(oldName: string, newName: string): Promise<KeyInfo> {
84
+ return this.libp2p.renameKey(oldName, newName);
85
+ }
86
+ listKeys(): Promise<KeyInfo[]> {
87
+ return this.libp2p.listKeys();
88
+ }
89
+ rotateKeychainPass(oldPass: string, newPass: string): Promise<void> {
90
+ return this.libp2p.rotateKeychainPass(oldPass, newPass);
91
+ }
92
+ async start(): Promise<void> {
93
+ await this.crypto.start();
94
+ }
95
+ async stop(): Promise<void> {
96
+ await this.crypto.stop();
97
+ }
98
+ }
99
+
100
+ export function keychain(
101
+ init: {
102
+ libp2p: KeychainInit;
103
+ crypto?: CryptoKeychain | { store: AnyStore };
104
+ } = { libp2p: {}, crypto: { store: createStore() } },
105
+ ): (components: KeychainComponents) => PeerbitKeychain {
106
+ return (components: KeychainComponents) => {
107
+ return new PeerbitKeychain(components, init);
108
+ };
109
+ }