@libp2p/keychain 3.0.7 → 3.0.8-0b4a2ee79

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.
@@ -22,7 +22,7 @@
22
22
  *
23
23
  * The **key id** is the SHA-256 [multihash](https://github.com/multiformats/multihash) of its public key.
24
24
  *
25
- * The *public key* is a [protobuf encoding](https://github.com/libp2p/js-libp2p-crypto/blob/master/src/keys/keys.proto.js) containing a type and the [DER encoding](https://en.wikipedia.org/wiki/X.690) of the PKCS [SubjectPublicKeyInfo](https://www.ietf.org/rfc/rfc3279.txt).
25
+ * The *public key* is a [protobuf encoding](https://github.com/libp2p/js-libp2p/blob/main/packages/crypto/src/keys/keys.proto.js) containing a type and the [DER encoding](https://en.wikipedia.org/wiki/X.690) of the PKCS [SubjectPublicKeyInfo](https://www.ietf.org/rfc/rfc3279.txt).
26
26
  *
27
27
  * ## Private key storage
28
28
  *
@@ -49,128 +49,153 @@
49
49
  *
50
50
  * A key benefit is that now the key chain can be used in browser with the [js-datastore-level](https://github.com/ipfs/js-datastore-level) implementation.
51
51
  */
52
- import type { KeyChain, KeyInfo } from '@libp2p/interface/keychain';
52
+ import type { ComponentLogger } from '@libp2p/interface';
53
53
  import type { KeyType } from '@libp2p/interface/keys';
54
54
  import type { PeerId } from '@libp2p/interface/peer-id';
55
55
  import type { Datastore } from 'interface-datastore';
56
+ import type { Multibase } from 'multiformats/bases/interface.js';
56
57
  export interface DEKConfig {
57
58
  hash: string;
58
59
  salt: string;
59
60
  iterationCount: number;
60
61
  keyLength: number;
61
62
  }
62
- export interface KeyChainInit {
63
+ export interface KeychainInit {
63
64
  pass?: string;
64
65
  dek?: DEKConfig;
65
66
  }
66
- declare const defaultOptions: {
67
- dek: {
68
- keyLength: number;
69
- iterationCount: number;
70
- salt: string;
71
- hash: string;
72
- };
73
- };
74
- export interface KeyChainComponents {
67
+ export interface KeychainComponents {
75
68
  datastore: Datastore;
69
+ logger: ComponentLogger;
76
70
  }
77
- /**
78
- * Manages the lifecycle of a key. Keys are encrypted at rest using PKCS #8.
79
- *
80
- * A key in the store has two entries
81
- * - '/info/*key-name*', contains the KeyInfo for the key
82
- * - '/pkcs8/*key-name*', contains the PKCS #8 for the key
83
- *
84
- */
85
- export declare class DefaultKeyChain implements KeyChain {
86
- private readonly components;
87
- private readonly init;
71
+ export interface KeyInfo {
72
+ /**
73
+ * The universally unique key id
74
+ */
75
+ id: string;
88
76
  /**
89
- * Creates a new instance of a key chain
77
+ * The local key name
90
78
  */
91
- constructor(components: KeyChainComponents, init: KeyChainInit);
79
+ name: string;
80
+ }
81
+ export interface Keychain {
92
82
  /**
93
- * Generates the options for a keychain. A random salt is produced.
83
+ * Export an existing key as a PEM encrypted PKCS #8 string.
84
+ *
85
+ * @example
94
86
  *
95
- * @returns {object}
87
+ * ```js
88
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
89
+ * const pemKey = await libp2p.keychain.exportKey('keyTest', 'password123')
90
+ * ```
96
91
  */
97
- static generateOptions(): KeyChainInit;
92
+ exportKey(name: string, password: string): Promise<Multibase<'m'>>;
98
93
  /**
99
- * Gets an object that can encrypt/decrypt protected data.
100
- * The default options for a keychain.
94
+ * Import a new key from a PEM encoded PKCS #8 string.
95
+ *
96
+ * @example
101
97
  *
102
- * @returns {object}
98
+ * ```js
99
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
100
+ * const pemKey = await libp2p.keychain.exportKey('keyTest', 'password123')
101
+ * const keyInfo = await libp2p.keychain.importKey('keyTestImport', pemKey, 'password123')
102
+ * ```
103
103
  */
104
- static get options(): typeof defaultOptions;
104
+ importKey(name: string, pem: string, password: string): Promise<KeyInfo>;
105
105
  /**
106
- * Create a new key.
106
+ * Import a new key from a PeerId with a private key component
107
+ *
108
+ * @example
107
109
  *
108
- * @param {string} name - The local key name; cannot already exist.
109
- * @param {string} type - One of the key types; 'rsa'.
110
- * @param {number} [size = 2048] - The key size in bits. Used for rsa keys only
110
+ * ```js
111
+ * const keyInfo = await libp2p.keychain.importPeer('keyTestImport', peerIdFromString('12D3Foo...'))
112
+ * ```
111
113
  */
112
- createKey(name: string, type: KeyType, size?: number): Promise<KeyInfo>;
114
+ importPeer(name: string, peerId: PeerId): Promise<KeyInfo>;
113
115
  /**
114
- * List all the keys.
116
+ * Export an existing key as a PeerId
117
+ *
118
+ * @example
115
119
  *
116
- * @returns {Promise<KeyInfo[]>}
120
+ * ```js
121
+ * const peerId = await libp2p.keychain.exportPeerId('key-name')
122
+ * ```
117
123
  */
118
- listKeys(): Promise<KeyInfo[]>;
124
+ exportPeerId(name: string): Promise<PeerId>;
119
125
  /**
120
- * Find a key by it's id
126
+ * Create a key in the keychain.
127
+ *
128
+ * @example
129
+ *
130
+ * ```js
131
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
132
+ * ```
121
133
  */
122
- findKeyById(id: string): Promise<KeyInfo>;
134
+ createKey(name: string, type: KeyType, size?: number): Promise<KeyInfo>;
123
135
  /**
124
- * Find a key by it's name.
136
+ * List all the keys.
125
137
  *
126
- * @param {string} name - The local key name.
127
- * @returns {Promise<KeyInfo>}
138
+ * @example
139
+ *
140
+ * ```js
141
+ * const keyInfos = await libp2p.keychain.listKeys()
142
+ * ```
128
143
  */
129
- findKeyByName(name: string): Promise<KeyInfo>;
144
+ listKeys(): Promise<KeyInfo[]>;
130
145
  /**
131
- * Remove an existing key.
146
+ * Removes a key from the keychain.
132
147
  *
133
- * @param {string} name - The local key name; must already exist.
134
- * @returns {Promise<KeyInfo>}
148
+ * @example
149
+ *
150
+ * ```js
151
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
152
+ * const keyInfo = await libp2p.keychain.removeKey('keyTest')
153
+ * ```
135
154
  */
136
155
  removeKey(name: string): Promise<KeyInfo>;
137
156
  /**
138
- * Rename a key
157
+ * Rename a key in the keychain.
158
+ *
159
+ * @example
139
160
  *
140
- * @param {string} oldName - The old local key name; must already exist.
141
- * @param {string} newName - The new local key name; must not already exist.
142
- * @returns {Promise<KeyInfo>}
161
+ * ```js
162
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
163
+ * const keyInfo = await libp2p.keychain.renameKey('keyTest', 'keyNewNtest')
164
+ * ```
143
165
  */
144
166
  renameKey(oldName: string, newName: string): Promise<KeyInfo>;
145
167
  /**
146
- * Export an existing key as a PEM encrypted PKCS #8 string
147
- */
148
- exportKey(name: string, password: string): Promise<string>;
149
- /**
150
- * Export an existing key as a PeerId
151
- */
152
- exportPeerId(name: string): Promise<PeerId>;
153
- /**
154
- * Import a new key from a PEM encoded PKCS #8 string
168
+ * Find a key by it's id.
155
169
  *
156
- * @param {string} name - The local key name; must not already exist.
157
- * @param {string} pem - The PEM encoded PKCS #8 string
158
- * @param {string} password - The password.
159
- * @returns {Promise<KeyInfo>}
160
- */
161
- importKey(name: string, pem: string, password: string): Promise<KeyInfo>;
162
- /**
163
- * Import a peer key
170
+ * @example
171
+ *
172
+ * ```js
173
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
174
+ * const keyInfo2 = await libp2p.keychain.findKeyById(keyInfo.id)
175
+ * ```
164
176
  */
165
- importPeer(name: string, peer: PeerId): Promise<KeyInfo>;
177
+ findKeyById(id: string): Promise<KeyInfo>;
166
178
  /**
167
- * Gets the private key as PEM encoded PKCS #8 string
179
+ * Find a key by it's name.
180
+ *
181
+ * @example
182
+ *
183
+ * ```js
184
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
185
+ * const keyInfo2 = await libp2p.keychain.findKeyByName('keyTest')
186
+ * ```
168
187
  */
169
- getPrivateKey(name: string): Promise<string>;
188
+ findKeyByName(name: string): Promise<KeyInfo>;
170
189
  /**
171
190
  * Rotate keychain password and re-encrypt all associated keys
191
+ *
192
+ * @example
193
+ *
194
+ * ```js
195
+ * await libp2p.keychain.rotateKeychainPass('oldPassword', 'newPassword')
196
+ * ```
172
197
  */
173
198
  rotateKeychainPass(oldPass: string, newPass: string): Promise<void>;
174
199
  }
175
- export {};
200
+ export declare function keychain(init?: KeychainInit): (components: KeychainComponents) => Keychain;
176
201
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAeH,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AACnE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AACrD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAIpD,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,cAAc,EAAE,MAAM,CAAA;IACtB,SAAS,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,GAAG,CAAC,EAAE,SAAS,CAAA;CAChB;AAaD,QAAA,MAAM,cAAc;;;;;;;CAQnB,CAAA;AAwCD,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,SAAS,CAAA;CACrB;AAED;;;;;;;GAOG;AACH,qBAAa,eAAgB,YAAW,QAAQ;IAC9C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;IAC/C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAc;IAEnC;;OAEG;gBACU,UAAU,EAAE,kBAAkB,EAAE,IAAI,EAAE,YAAY;IA8B/D;;;;OAIG;IACH,MAAM,CAAC,eAAe,IAAK,YAAY;IAOvC;;;;;OAKG;IACH,MAAM,KAAK,OAAO,IAAK,OAAO,cAAc,CAE3C;IAED;;;;;;OAMG;IACG,SAAS,CAAE,IAA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