@libp2p/interface 1.7.0 → 2.0.0-18dd3cb26

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.
Files changed (56) hide show
  1. package/dist/index.min.js +1 -1
  2. package/dist/src/connection/index.d.ts +32 -8
  3. package/dist/src/connection/index.d.ts.map +1 -1
  4. package/dist/src/connection/index.js.map +1 -1
  5. package/dist/src/connection-encrypter/index.d.ts +11 -2
  6. package/dist/src/connection-encrypter/index.d.ts.map +1 -1
  7. package/dist/src/errors.d.ts +200 -25
  8. package/dist/src/errors.d.ts.map +1 -1
  9. package/dist/src/errors.js +280 -43
  10. package/dist/src/errors.js.map +1 -1
  11. package/dist/src/event-target.d.ts +0 -4
  12. package/dist/src/event-target.d.ts.map +1 -1
  13. package/dist/src/event-target.js +0 -1
  14. package/dist/src/event-target.js.map +1 -1
  15. package/dist/src/index.d.ts +12 -3
  16. package/dist/src/index.d.ts.map +1 -1
  17. package/dist/src/index.js.map +1 -1
  18. package/dist/src/keys/index.d.ts +85 -24
  19. package/dist/src/keys/index.d.ts.map +1 -1
  20. package/dist/src/keys/index.js +29 -3
  21. package/dist/src/keys/index.js.map +1 -1
  22. package/dist/src/metrics/index.d.ts +8 -8
  23. package/dist/src/metrics/index.d.ts.map +1 -1
  24. package/dist/src/peer-id/index.d.ts +120 -18
  25. package/dist/src/peer-id/index.d.ts.map +1 -1
  26. package/dist/src/peer-id/index.js +8 -1
  27. package/dist/src/peer-id/index.js.map +1 -1
  28. package/dist/src/peer-store/index.d.ts +5 -2
  29. package/dist/src/peer-store/index.d.ts.map +1 -1
  30. package/dist/src/pubsub/index.d.ts +2 -1
  31. package/dist/src/pubsub/index.d.ts.map +1 -1
  32. package/dist/src/pubsub/index.js.map +1 -1
  33. package/dist/src/record/index.d.ts +2 -2
  34. package/dist/src/record/index.d.ts.map +1 -1
  35. package/dist/src/stream-handler/index.d.ts +3 -3
  36. package/dist/src/stream-handler/index.d.ts.map +1 -1
  37. package/dist/src/topology/index.d.ts +4 -3
  38. package/dist/src/topology/index.d.ts.map +1 -1
  39. package/dist/src/transport/index.d.ts +3 -7
  40. package/dist/src/transport/index.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/src/connection/index.ts +34 -8
  43. package/src/connection-encrypter/index.ts +12 -2
  44. package/src/errors.ts +320 -49
  45. package/src/event-target.ts +0 -2
  46. package/src/index.ts +12 -4
  47. package/src/keys/index.ts +112 -24
  48. package/src/metrics/index.ts +8 -8
  49. package/src/peer-id/index.ts +140 -19
  50. package/src/peer-store/index.ts +5 -2
  51. package/src/pubsub/index.ts +2 -1
  52. package/src/record/index.ts +2 -2
  53. package/src/stream-handler/index.ts +3 -3
  54. package/src/topology/index.ts +4 -3
  55. package/src/transport/index.ts +3 -8
  56. package/dist/typedoc-urls.json +0 -175
package/src/errors.ts CHANGED
@@ -4,91 +4,362 @@
4
4
  * AbortSignal.
5
5
  */
6
6
  export class AbortError extends Error {
7
- public readonly code: string
8
- public readonly type: string
7
+ static name = 'AbortError'
9
8
 
10
9
  constructor (message: string = 'The operation was aborted') {
11
10
  super(message)
12
11
  this.name = 'AbortError'
13
- this.code = AbortError.code
14
- this.type = AbortError.type
15
12
  }
13
+ }
16
14
 
17
- static readonly code = 'ABORT_ERR'
15
+ /**
16
+ * Thrown when a remote Peer ID does not match the expected one
17
+ */
18
+ export class UnexpectedPeerError extends Error {
19
+ static name = 'UnexpectedPeerError'
18
20
 
19
- static readonly type = 'aborted'
21
+ constructor (message = 'Unexpected Peer') {
22
+ super(message)
23
+ this.name = 'UnexpectedPeerError'
24
+ }
20
25
  }
21
26
 
22
- export class CodeError<T extends Record<string, any> = Record<string, never>> extends Error {
23
- public readonly props: T
27
+ /**
28
+ * Thrown when a crypto exchange fails
29
+ */
30
+ export class InvalidCryptoExchangeError extends Error {
31
+ static name = 'InvalidCryptoExchangeError'
24
32
 
25
- constructor (
26
- message: string,
27
- public readonly code: string,
28
- props?: T
29
- ) {
33
+ constructor (message = 'Invalid crypto exchange') {
30
34
  super(message)
35
+ this.name = 'InvalidCryptoExchangeError'
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Thrown when invalid parameters are passed to a function or method call
41
+ */
42
+ export class InvalidParametersError extends Error {
43
+ static name = 'InvalidParametersError'
31
44
 
32
- this.name = props?.name ?? 'CodeError'
33
- this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions
45
+ constructor (message = 'Invalid parameters') {
46
+ super(message)
47
+ this.name = 'InvalidParametersError'
34
48
  }
35
49
  }
36
50
 
37
- export class AggregateCodeError<T extends Record<string, any> = Record<string, never>> extends AggregateError {
38
- public readonly props: T
51
+ /**
52
+ * Thrown when a public key is invalid
53
+ */
54
+ export class InvalidPublicKeyError extends Error {
55
+ static name = 'InvalidPublicKeyError'
56
+
57
+ constructor (message = 'Invalid public key') {
58
+ super(message)
59
+ this.name = 'InvalidPublicKeyError'
60
+ }
61
+ }
39
62
 
40
- constructor (
41
- errors: Error[],
42
- message: string,
43
- public readonly code: string,
44
- props?: T
45
- ) {
46
- super(errors, message)
63
+ /**
64
+ * Thrown when a private key is invalid
65
+ */
66
+ export class InvalidPrivateKeyError extends Error {
67
+ static name = 'InvalidPrivateKeyError'
47
68
 
48
- this.name = props?.name ?? 'AggregateCodeError'
49
- this.props = props ?? {} as T // eslint-disable-line @typescript-eslint/consistent-type-assertions
69
+ constructor (message = 'Invalid private key') {
70
+ super(message)
71
+ this.name = 'InvalidPrivateKeyError'
50
72
  }
51
73
  }
52
74
 
53
- export class UnexpectedPeerError extends Error {
54
- public code: string
75
+ /**
76
+ * Thrown when a operation is unsupported
77
+ */
78
+ export class UnsupportedOperationError extends Error {
79
+ static name = 'UnsupportedOperationError'
55
80
 
56
- constructor (message = 'Unexpected Peer') {
81
+ constructor (message = 'Unsupported operation') {
57
82
  super(message)
58
- this.name = 'UnexpectedPeerError'
59
- this.code = UnexpectedPeerError.code
83
+ this.name = 'UnsupportedOperationError'
60
84
  }
85
+ }
61
86
 
62
- static readonly code = 'ERR_UNEXPECTED_PEER'
87
+ /**
88
+ * Thrown when a connection is closing
89
+ */
90
+ export class ConnectionClosingError extends Error {
91
+ static name = 'ConnectionClosingError'
92
+
93
+ constructor (message = 'The connection is closing') {
94
+ super(message)
95
+ this.name = 'ConnectionClosingError'
96
+ }
63
97
  }
64
98
 
65
- export class InvalidCryptoExchangeError extends Error {
66
- public code: string
99
+ /**
100
+ * Thrown when a connection is closed
101
+ */
102
+ export class ConnectionClosedError extends Error {
103
+ static name = 'ConnectionClosedError'
67
104
 
68
- constructor (message = 'Invalid crypto exchange') {
105
+ constructor (message = 'The connection is closed') {
69
106
  super(message)
70
- this.name = 'InvalidCryptoExchangeError'
71
- this.code = InvalidCryptoExchangeError.code
107
+ this.name = 'ConnectionClosedError'
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Thrown when a connection fails
113
+ */
114
+ export class ConnectionFailedError extends Error {
115
+ static name = 'ConnectionFailedError'
116
+
117
+ constructor (message = 'Connection failed') {
118
+ super(message)
119
+ this.name = 'ConnectionFailedError'
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Thrown when the muxer is closed and an attempt to open a stream occurs
125
+ */
126
+ export class MuxerClosedError extends Error {
127
+ static name = 'MuxerClosedError'
128
+
129
+ constructor (message = 'The muxer is closed') {
130
+ super(message)
131
+ this.name = 'MuxerClosedError'
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Thrown when a protocol stream is reset by the remote muxer
137
+ */
138
+ export class StreamResetError extends Error {
139
+ static name = 'StreamResetError'
140
+
141
+ constructor (message = 'The stream has been reset') {
142
+ super(message)
143
+ this.name = 'StreamResetError'
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Thrown when a stream is in an invalid state
149
+ */
150
+ export class StreamStateError extends Error {
151
+ static name = 'StreamStateError'
152
+
153
+ constructor (message = 'The stream is in an invalid state') {
154
+ super(message)
155
+ this.name = 'StreamStateError'
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Thrown when a value could not be found
161
+ */
162
+ export class NotFoundError extends Error {
163
+ static name = 'NotFoundError'
164
+
165
+ constructor (message = 'Not found') {
166
+ super(message)
167
+ this.name = 'NotFoundError'
168
+ }
169
+ }
170
+
171
+ /**
172
+ * Thrown when an invalid peer ID is encountered
173
+ */
174
+ export class InvalidPeerIdError extends Error {
175
+ static name = 'InvalidPeerIdError'
176
+
177
+ constructor (message = 'Invalid PeerID') {
178
+ super(message)
179
+ this.name = 'InvalidPeerIdError'
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Thrown when an invalid multiaddr is encountered
185
+ */
186
+ export class InvalidMultiaddrError extends Error {
187
+ static name = 'InvalidMultiaddrError'
188
+
189
+ constructor (message = 'Invalid multiaddr') {
190
+ super(message)
191
+ this.name = 'InvalidMultiaddrError'
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Thrown when an invalid CID is encountered
197
+ */
198
+ export class InvalidCIDError extends Error {
199
+ static name = 'InvalidCIDError'
200
+
201
+ constructor (message = 'Invalid CID') {
202
+ super(message)
203
+ this.name = 'InvalidCIDError'
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Thrown when an invalid multihash is encountered
209
+ */
210
+ export class InvalidMultihashError extends Error {
211
+ static name = 'InvalidMultihashError'
212
+
213
+ constructor (message = 'Invalid Multihash') {
214
+ super(message)
215
+ this.name = 'InvalidMultihashError'
216
+ }
217
+ }
218
+
219
+ /**
220
+ * Thrown when a protocol is not supported
221
+ */
222
+ export class UnsupportedProtocolError extends Error {
223
+ static name = 'UnsupportedProtocolError'
224
+
225
+ constructor (message = 'Unsupported protocol error') {
226
+ super(message)
227
+ this.name = 'UnsupportedProtocolError'
72
228
  }
229
+ }
230
+
231
+ /**
232
+ * An invalid or malformed message was encountered during a protocol exchange
233
+ */
234
+ export class InvalidMessageError extends Error {
235
+ static name = 'InvalidMessageError'
73
236
 
74
- static readonly code = 'ERR_INVALID_CRYPTO_EXCHANGE'
237
+ constructor (message = 'Invalid message') {
238
+ super(message)
239
+ this.name = 'InvalidMessageError'
240
+ }
75
241
  }
76
242
 
77
- export class InvalidCryptoTransmissionError extends Error {
78
- public code: string
243
+ /**
244
+ * Thrown when a remote peer sends a structurally valid message that does not
245
+ * comply with the protocol
246
+ */
247
+ export class ProtocolError extends Error {
248
+ static name = 'ProtocolError'
79
249
 
80
- constructor (message = 'Invalid crypto transmission') {
250
+ constructor (message = 'Protocol error') {
81
251
  super(message)
82
- this.name = 'InvalidCryptoTransmissionError'
83
- this.code = InvalidCryptoTransmissionError.code
252
+ this.name = 'ProtocolError'
84
253
  }
254
+ }
255
+
256
+ /**
257
+ * Throw when an operation times out
258
+ */
259
+ export class TimeoutError extends Error {
260
+ static name = 'TimeoutError'
85
261
 
86
- static readonly code = 'ERR_INVALID_CRYPTO_TRANSMISSION'
262
+ constructor (message = 'Timed out') {
263
+ super(message)
264
+ this.name = 'TimeoutError'
265
+ }
87
266
  }
88
267
 
89
- // Error codes
268
+ /**
269
+ * Thrown when a startable component is interacted with but it has not been
270
+ * started yet
271
+ */
272
+ export class NotStartedError extends Error {
273
+ static name = 'NotStartedError'
274
+
275
+ constructor (message = 'Not started') {
276
+ super(message)
277
+ this.name = 'NotStartedError'
278
+ }
279
+ }
280
+
281
+ /**
282
+ * Thrown when a component is started that has already been started
283
+ */
284
+ export class AlreadyStartedError extends Error {
285
+ static name = 'AlreadyStartedError'
90
286
 
91
- export const ERR_TIMEOUT = 'ERR_TIMEOUT'
92
- export const ERR_INVALID_PARAMETERS = 'ERR_INVALID_PARAMETERS'
93
- export const ERR_NOT_FOUND = 'ERR_NOT_FOUND'
94
- export const ERR_INVALID_MESSAGE = 'ERR_INVALID_MESSAGE'
287
+ constructor (message = 'Already started') {
288
+ super(message)
289
+ this.name = 'AlreadyStartedError'
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Thrown when dialing an address failed
295
+ */
296
+ export class DialError extends Error {
297
+ static name = 'DialError'
298
+
299
+ constructor (message = 'Dial error') {
300
+ super(message)
301
+ this.name = 'DialError'
302
+ }
303
+ }
304
+
305
+ /**
306
+ * Thrown when listening on an address failed
307
+ */
308
+ export class ListenError extends Error {
309
+ static name = 'ListenError'
310
+
311
+ constructor (message = 'Listen error') {
312
+ super(message)
313
+ this.name = 'ListenError'
314
+ }
315
+ }
316
+
317
+ /**
318
+ * This error is thrown when a limited connection is encountered, i.e. if the
319
+ * user tried to open a stream on a connection for a protocol that is not
320
+ * configured to run over limited connections.
321
+ */
322
+ export class LimitedConnectionError extends Error {
323
+ static name = 'LimitedConnectionError'
324
+
325
+ constructor (message = 'Limited connection') {
326
+ super(message)
327
+ this.name = 'LimitedConnectionError'
328
+ }
329
+ }
330
+
331
+ /**
332
+ * This error is thrown where there are too many inbound protocols streams open
333
+ */
334
+ export class TooManyInboundProtocolStreamsError extends Error {
335
+ static name = 'TooManyInboundProtocolStreamsError'
336
+
337
+ constructor (message = 'Too many inbound protocol streams') {
338
+ super(message)
339
+ this.name = 'TooManyInboundProtocolStreamsError'
340
+ }
341
+ }
342
+
343
+ /**
344
+ * This error is thrown where there are too many outbound protocols streams open
345
+ */
346
+ export class TooManyOutboundProtocolStreamsError extends Error {
347
+ static name = 'TooManyOutboundProtocolStreamsError'
348
+
349
+ constructor (message = 'Too many outbound protocol streams') {
350
+ super(message)
351
+ this.name = 'TooManyOutboundProtocolStreamsError'
352
+ }
353
+ }
354
+
355
+ /**
356
+ * Thrown when and attempt to operate on an unsupported key was made
357
+ */
358
+ export class UnsupportedKeyTypeError extends Error {
359
+ static name = 'UnsupportedKeyTypeError'
360
+
361
+ constructor (message = 'Unsupported key type') {
362
+ super(message)
363
+ this.name = 'UnsupportedKeyTypeError'
364
+ }
365
+ }
@@ -104,5 +104,3 @@ export class TypedEventEmitter<EventMap extends Record<string, any>> extends Eve
104
104
  return this.dispatchEvent(new CustomEvent<Detail>(type as string, detail))
105
105
  }
106
106
  }
107
-
108
- export const CustomEvent = globalThis.CustomEvent
package/src/index.ts CHANGED
@@ -17,8 +17,9 @@
17
17
  import type { Connection, NewStreamOptions, Stream } from './connection/index.js'
18
18
  import type { ContentRouting } from './content-routing/index.js'
19
19
  import type { TypedEventTarget } from './event-target.js'
20
+ import type { Ed25519PublicKey, PublicKey, RSAPublicKey, Secp256k1PublicKey } from './keys/index.js'
20
21
  import type { Metrics } from './metrics/index.js'
21
- import type { PeerId } from './peer-id/index.js'
22
+ import type { Ed25519PeerId, PeerId, RSAPeerId, Secp256k1PeerId, URLPeerId } from './peer-id/index.js'
22
23
  import type { PeerInfo } from './peer-info/index.js'
23
24
  import type { PeerRouting } from './peer-routing/index.js'
24
25
  import type { Address, Peer, PeerStore } from './peer-store/index.js'
@@ -331,7 +332,7 @@ export interface IsDialableOptions extends AbortOptions {
331
332
  * because that protocol would not be allowed to run over a data/time limited
332
333
  * connection.
333
334
  */
334
- runOnTransientConnection?: boolean
335
+ runOnLimitedConnection?: boolean
335
336
  }
336
337
 
337
338
  export type TransportManagerDialProgressEvents =
@@ -347,7 +348,10 @@ export type OpenConnectionProgressEvents =
347
348
  OutboundConnectionUpgradeEvents
348
349
 
349
350
  export interface DialOptions extends AbortOptions, ProgressOptions {
350
-
351
+ /**
352
+ * If true, open a new connection to the remote even if one already exists
353
+ */
354
+ force?: boolean
351
355
  }
352
356
 
353
357
  export interface DialProtocolOptions extends NewStreamOptions {
@@ -642,7 +646,11 @@ export interface Libp2p<T extends ServiceMap = ServiceMap> extends Startable, Ty
642
646
  * type this may mean searching the routing if the peer's key is not present
643
647
  * in the peer store.
644
648
  */
645
- getPublicKey(peer: PeerId, options?: AbortOptions): Promise<Uint8Array>
649
+ getPublicKey(peer: Ed25519PeerId, options?: AbortOptions): Promise<Ed25519PublicKey>
650
+ getPublicKey(peer: Secp256k1PeerId, options?: AbortOptions): Promise<Secp256k1PublicKey>
651
+ getPublicKey(peer: RSAPeerId, options?: AbortOptions): Promise<RSAPublicKey>
652
+ getPublicKey(peer: URLPeerId, options?: AbortOptions): Promise<never>
653
+ getPublicKey(peer: PeerId, options?: AbortOptions): Promise<PublicKey>
646
654
 
647
655
  /**
648
656
  * Given the current node configuration, returns a promise of `true` or
package/src/keys/index.ts CHANGED
@@ -1,39 +1,127 @@
1
+ import type { CID } from 'multiformats/cid'
2
+ import type { MultihashDigest } from 'multiformats/hashes/interface'
1
3
  import type { Uint8ArrayList } from 'uint8arraylist'
2
4
 
3
- export interface PublicKey<Type extends KeyType = 'Ed25519'> {
4
- readonly bytes: Uint8Array
5
+ export type KeyType = 'RSA' | 'Ed25519' | 'secp256k1'
6
+
7
+ interface PublicKeyBase<KeyType extends string, DigestCode extends number = number> {
8
+ /**
9
+ * The type of this key
10
+ */
11
+ readonly type: KeyType
12
+
13
+ /**
14
+ * The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1
15
+ * DER format (for RSA keys)
16
+ */
17
+ readonly raw: Uint8Array
18
+
19
+ /**
20
+ * Returns `true` if the passed object matches this key
21
+ */
22
+ equals(key?: any): boolean
23
+
24
+ /**
25
+ * Returns this public key as a Multihash digest.
26
+ *
27
+ * It contains either an identity hash containing the protobuf version of the
28
+ * public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the
29
+ * protobuf version of the public key (RSA keys).
30
+ */
31
+ toMultihash(): MultihashDigest<DigestCode>
32
+
33
+ /**
34
+ * Return this public key as a CID encoded with the `libp2p-key` codec
35
+ *
36
+ * The digest contains either an identity hash containing the protobuf version
37
+ * of the public key (for Ed25519 and secp256k1 keys) or a sha256 hash of the
38
+ * protobuf version of the public key (RSA keys).
39
+ */
40
+ toCID(): CID<unknown, 0x72, DigestCode, 1>
41
+
42
+ /**
43
+ * Verify the passed data was signed by the private key corresponding to this
44
+ * public key
45
+ */
5
46
  verify(data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean>
6
- marshal(): Uint8Array
7
- equals(key: PublicKey<Type>): boolean
8
- hash(): Uint8Array | Promise<Uint8Array>
47
+
48
+ /**
49
+ * Returns this key as a multihash with base58btc encoding
50
+ */
51
+ toString(): string
52
+ }
53
+
54
+ export interface RSAPublicKey extends PublicKeyBase<'RSA', 0x12> {}
55
+ export interface Ed25519PublicKey extends PublicKeyBase<'Ed25519', 0x0> {}
56
+ export interface Secp256k1PublicKey extends PublicKeyBase<'secp256k1', 0x0> {}
57
+ export type PublicKey = RSAPublicKey | Ed25519PublicKey | Secp256k1PublicKey
58
+
59
+ /**
60
+ * Returns true if the passed argument has type overlap with the `PublicKey`
61
+ * interface. Can be used to disambiguate object types.
62
+ */
63
+ export function isPublicKey (key?: any): key is PublicKey {
64
+ if (key == null) {
65
+ return false
66
+ }
67
+
68
+ return (key.type === 'RSA' || key.type === 'Ed25519' || key.type === 'secp256k1') &&
69
+ key.raw instanceof Uint8Array &&
70
+ typeof key.equals === 'function' &&
71
+ typeof key.toMultihash === 'function' &&
72
+ typeof key.toCID === 'function' &&
73
+ typeof key.verify === 'function'
9
74
  }
10
75
 
11
76
  /**
12
77
  * Generic private key interface
13
78
  */
14
- export interface PrivateKey<Type extends KeyType = 'Ed25519'> {
15
- readonly public: PublicKey<Type>
16
- readonly bytes: Uint8Array
17
- sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>
18
- marshal(): Uint8Array
19
- equals(key: PrivateKey<Type>): boolean
20
- hash(): Uint8Array | Promise<Uint8Array>
79
+ interface PrivateKeyBase<KeyType extends string, PublicKeyType extends PublicKeyBase<KeyType>> {
21
80
  /**
22
- * Gets the ID of the key.
23
- *
24
- * The key id is the base58 encoding of the SHA-256 multihash of its public key.
25
- * The public key is a protobuf encoding containing a type and the DER encoding
26
- * of the PKCS SubjectPublicKeyInfo.
81
+ * The type of this key
82
+ */
83
+ readonly type: KeyType
84
+
85
+ /**
86
+ * The public key that corresponds to this private key
27
87
  */
28
- id(): Promise<string>
88
+ readonly publicKey: PublicKeyType
89
+
29
90
  /**
30
- * Exports the password protected key in the format specified.
91
+ * The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1
92
+ * DER format (for RSA keys)
31
93
  */
32
- export(password: string, format?: 'pkcs-8' | string): Promise<string>
94
+ readonly raw: Uint8Array
95
+
96
+ /**
97
+ * Returns `true` if the passed object matches this key
98
+ */
99
+ equals(key?: any): boolean
100
+
101
+ /**
102
+ * Sign the passed data with this private key and return the signature for
103
+ * later verification
104
+ */
105
+ sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>
33
106
  }
34
107
 
35
- export const Ed25519 = 'Ed25519'
36
- export const RSA = 'RSA'
37
- export const secp256k1 = 'secp256k1'
108
+ export interface RSAPrivateKey extends PrivateKeyBase<'RSA', RSAPublicKey> {}
109
+ export interface Ed25519PrivateKey extends PrivateKeyBase<'Ed25519', Ed25519PublicKey> {}
110
+ export interface Secp256k1PrivateKey extends PrivateKeyBase<'secp256k1', Secp256k1PublicKey> {}
111
+ export type PrivateKey = RSAPrivateKey | Ed25519PrivateKey | Secp256k1PrivateKey
38
112
 
39
- export type KeyType = typeof Ed25519 | typeof RSA | typeof secp256k1
113
+ /**
114
+ * Returns true if the passed argument has type overlap with the `PrivateKey`
115
+ * interface. Can be used to disambiguate object types.
116
+ */
117
+ export function isPrivateKey (key?: any): key is PrivateKey {
118
+ if (key == null) {
119
+ return false
120
+ }
121
+
122
+ return (key.type === 'RSA' || key.type === 'Ed25519' || key.type === 'secp256k1') &&
123
+ isPublicKey(key.publicKey) &&
124
+ key.raw instanceof Uint8Array &&
125
+ typeof key.equals === 'function' &&
126
+ typeof key.sign === 'function'
127
+ }
@@ -76,23 +76,23 @@ export interface Metric {
76
76
  * A group of related metrics loosely based on the interfaces exposed by the
77
77
  * prom-client module
78
78
  */
79
- export interface MetricGroup {
79
+ export interface MetricGroup<T extends string = any> {
80
80
  /**
81
81
  * Update the stored metric group to the passed value
82
82
  */
83
- update(values: Record<string, number>): void
83
+ update(values: Partial<Record<T, number>>): void
84
84
 
85
85
  /**
86
86
  * Increment the metric group keys by the passed number or
87
- * any non-numeric value to increment by 1
87
+ * `true` to increment by 1
88
88
  */
89
- increment(values: Record<string, number | unknown>): void
89
+ increment(values: Partial<Record<T, number | true>>): void
90
90
 
91
91
  /**
92
92
  * Decrement the metric group keys by the passed number or
93
- * any non-numeric value to decrement by 1
93
+ * `true` to decrement by 1
94
94
  */
95
- decrement(values: Record<string, number | unknown>): void
95
+ decrement(values: Partial<Record<T, number | true>>): void
96
96
 
97
97
  /**
98
98
  * Reset the passed key in this metric group to its default value
@@ -128,12 +128,12 @@ export interface Counter {
128
128
  * exposed by the prom-client module - counters are metrics that only
129
129
  * go up
130
130
  */
131
- export interface CounterGroup {
131
+ export interface CounterGroup<T extends string = any> {
132
132
  /**
133
133
  * Increment the metric group keys by the passed number or
134
134
  * any non-numeric value to increment by 1
135
135
  */
136
- increment(values: Record<string, number | unknown>): void
136
+ increment(values: Partial<Record<T, number | true>>): void
137
137
 
138
138
  /**
139
139
  * Reset the passed key in this metric group to its default value