@libp2p/keychain 3.0.8 → 4.0.0

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,151 @@
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';
53
- import type { KeyType } from '@libp2p/interface/keys';
54
- import type { PeerId } from '@libp2p/interface/peer-id';
52
+ import type { ComponentLogger, KeyType, PeerId } from '@libp2p/interface';
55
53
  import type { Datastore } from 'interface-datastore';
54
+ import type { Multibase } from 'multiformats/bases/interface.js';
56
55
  export interface DEKConfig {
57
56
  hash: string;
58
57
  salt: string;
59
58
  iterationCount: number;
60
59
  keyLength: number;
61
60
  }
62
- export interface KeyChainInit {
61
+ export interface KeychainInit {
63
62
  pass?: string;
64
63
  dek?: DEKConfig;
65
64
  }
66
- declare const defaultOptions: {
67
- dek: {
68
- keyLength: number;
69
- iterationCount: number;
70
- salt: string;
71
- hash: string;
72
- };
73
- };
74
- export interface KeyChainComponents {
65
+ export interface KeychainComponents {
75
66
  datastore: Datastore;
67
+ logger: ComponentLogger;
76
68
  }
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;
69
+ export interface KeyInfo {
70
+ /**
71
+ * The universally unique key id
72
+ */
73
+ id: string;
88
74
  /**
89
- * Creates a new instance of a key chain
75
+ * The local key name
90
76
  */
91
- constructor(components: KeyChainComponents, init: KeyChainInit);
77
+ name: string;
78
+ }
79
+ export interface Keychain {
92
80
  /**
93
- * Generates the options for a keychain. A random salt is produced.
81
+ * Export an existing key as a PEM encrypted PKCS #8 string.
82
+ *
83
+ * @example
94
84
  *
95
- * @returns {object}
85
+ * ```js
86
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
87
+ * const pemKey = await libp2p.keychain.exportKey('keyTest', 'password123')
88
+ * ```
96
89
  */
97
- static generateOptions(): KeyChainInit;
90
+ exportKey(name: string, password: string): Promise<Multibase<'m'>>;
98
91
  /**
99
- * Gets an object that can encrypt/decrypt protected data.
100
- * The default options for a keychain.
92
+ * Import a new key from a PEM encoded PKCS #8 string.
93
+ *
94
+ * @example
101
95
  *
102
- * @returns {object}
96
+ * ```js
97
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
98
+ * const pemKey = await libp2p.keychain.exportKey('keyTest', 'password123')
99
+ * const keyInfo = await libp2p.keychain.importKey('keyTestImport', pemKey, 'password123')
100
+ * ```
103
101
  */
104
- static get options(): typeof defaultOptions;
102
+ importKey(name: string, pem: string, password: string): Promise<KeyInfo>;
105
103
  /**
106
- * Create a new key.
104
+ * Import a new key from a PeerId with a private key component
105
+ *
106
+ * @example
107
107
  *
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
108
+ * ```js
109
+ * const keyInfo = await libp2p.keychain.importPeer('keyTestImport', peerIdFromString('12D3Foo...'))
110
+ * ```
111
111
  */
112
- createKey(name: string, type: KeyType, size?: number): Promise<KeyInfo>;
112
+ importPeer(name: string, peerId: PeerId): Promise<KeyInfo>;
113
113
  /**
114
- * List all the keys.
114
+ * Export an existing key as a PeerId
115
+ *
116
+ * @example
115
117
  *
116
- * @returns {Promise<KeyInfo[]>}
118
+ * ```js
119
+ * const peerId = await libp2p.keychain.exportPeerId('key-name')
120
+ * ```
117
121
  */
118
- listKeys(): Promise<KeyInfo[]>;
122
+ exportPeerId(name: string): Promise<PeerId>;
119
123
  /**
120
- * Find a key by it's id
124
+ * Create a key in the keychain.
125
+ *
126
+ * @example
127
+ *
128
+ * ```js
129
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
130
+ * ```
121
131
  */
122
- findKeyById(id: string): Promise<KeyInfo>;
132
+ createKey(name: string, type: KeyType, size?: number): Promise<KeyInfo>;
123
133
  /**
124
- * Find a key by it's name.
134
+ * List all the keys.
125
135
  *
126
- * @param {string} name - The local key name.
127
- * @returns {Promise<KeyInfo>}
136
+ * @example
137
+ *
138
+ * ```js
139
+ * const keyInfos = await libp2p.keychain.listKeys()
140
+ * ```
128
141
  */
129
- findKeyByName(name: string): Promise<KeyInfo>;
142
+ listKeys(): Promise<KeyInfo[]>;
130
143
  /**
131
- * Remove an existing key.
144
+ * Removes a key from the keychain.
132
145
  *
133
- * @param {string} name - The local key name; must already exist.
134
- * @returns {Promise<KeyInfo>}
146
+ * @example
147
+ *
148
+ * ```js
149
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
150
+ * const keyInfo = await libp2p.keychain.removeKey('keyTest')
151
+ * ```
135
152
  */
136
153
  removeKey(name: string): Promise<KeyInfo>;
137
154
  /**
138
- * Rename a key
155
+ * Rename a key in the keychain.
156
+ *
157
+ * @example
139
158
  *
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>}
159
+ * ```js
160
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
161
+ * const keyInfo = await libp2p.keychain.renameKey('keyTest', 'keyNewNtest')
162
+ * ```
143
163
  */
144
164
  renameKey(oldName: string, newName: string): Promise<KeyInfo>;
145
165
  /**
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
166
+ * Find a key by it's id.
155
167
  *
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
168
+ * @example
169
+ *
170
+ * ```js
171
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
172
+ * const keyInfo2 = await libp2p.keychain.findKeyById(keyInfo.id)
173
+ * ```
164
174
  */
165
- importPeer(name: string, peer: PeerId): Promise<KeyInfo>;
175
+ findKeyById(id: string): Promise<KeyInfo>;
166
176
  /**
167
- * Gets the private key as PEM encoded PKCS #8 string
177
+ * Find a key by it's name.
178
+ *
179
+ * @example
180
+ *
181
+ * ```js
182
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
183
+ * const keyInfo2 = await libp2p.keychain.findKeyByName('keyTest')
184
+ * ```
168
185
  */
169
- getPrivateKey(name: string): Promise<string>;
186
+ findKeyByName(name: string): Promise<KeyInfo>;
170
187
  /**
171
188
  * Rotate keychain password and re-encrypt all associated keys
189
+ *
190
+ * @example
191
+ *
192
+ * ```js
193
+ * await libp2p.keychain.rotateKeychainPass('oldPassword', 'newPassword')
194
+ * ```
172
195
  */
173
196
  rotateKeychainPass(oldPass: string, newPass: string): Promise<void>;
174
197
  }
175
- export {};
198
+ export declare function keychain(init?: KeychainInit): (components: KeychainComponents) => Keychain;
176
199
  //# 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,IAAI,E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