@libp2p/interface 1.7.0-3c8dd5bbf → 1.7.0-5214dec4a

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 (55) 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 +88 -15
  8. package/dist/src/errors.d.ts.map +1 -1
  9. package/dist/src/errors.js +165 -23
  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 +75 -24
  19. package/dist/src/keys/index.d.ts.map +1 -1
  20. package/dist/src/keys/index.js +1 -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 +185 -29
  45. package/src/event-target.ts +0 -2
  46. package/src/index.ts +12 -4
  47. package/src/keys/index.ts +80 -25
  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
@@ -1,4 +1,4 @@
1
- import type { Connection, MultiaddrConnection } from '../connection/index.js';
1
+ import type { Connection, ConnectionLimits, MultiaddrConnection } from '../connection/index.js';
2
2
  import type { TypedEventTarget } from '../event-target.js';
3
3
  import type { AbortOptions } from '../index.js';
4
4
  import type { StreamMuxerFactory } from '../stream-muxer/index.js';
@@ -85,15 +85,11 @@ export declare enum FaultTolerance {
85
85
  */
86
86
  NO_FATAL = 1
87
87
  }
88
- export interface UpgraderOptions<ConnectionUpgradeEvents extends ProgressEvent = ProgressEvent> extends ProgressOptions<ConnectionUpgradeEvents> {
88
+ export interface UpgraderOptions<ConnectionUpgradeEvents extends ProgressEvent = ProgressEvent> extends ProgressOptions<ConnectionUpgradeEvents>, AbortOptions {
89
89
  skipEncryption?: boolean;
90
90
  skipProtection?: boolean;
91
91
  muxerFactory?: StreamMuxerFactory;
92
- /**
93
- * The passed MultiaddrConnection has limits place on duration and/or data
94
- * transfer amounts so is not expected to be open for very long.
95
- */
96
- transient?: boolean;
92
+ limits?: ConnectionLimits;
97
93
  }
98
94
  export type InboundConnectionUpgradeEvents = ProgressEvent<'upgrader:encrypt-inbound-connection'> | ProgressEvent<'upgrader:multiplex-inbound-connection'>;
99
95
  export type OutboundConnectionUpgradeEvents = ProgressEvent<'upgrader:encrypt-outbound-connection'> | ProgressEvent<'upgrader:multiplex-outbound-connection'>;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/transport/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAA;AAC7E,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAA;AAClE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAA;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAErE,MAAM,WAAW,cAAc;IAC7B,YAAY,EAAE,WAAW,CAAC,UAAU,CAAC,CAAA;IACrC,WAAW,EAAE,WAAW,CAAA;IACxB,OAAO,EAAE,WAAW,CAAC,KAAK,CAAC,CAAA;IAC3B,OAAO,EAAE,WAAW,CAAA;CACrB;AAED,MAAM,WAAW,QAAS,SAAQ,gBAAgB,CAAC,cAAc,CAAC;IAChE;;OAEG;IACH,MAAM,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC3C;;OAEG;IACH,QAAQ,IAAI,SAAS,EAAE,CAAA;IACvB;;;;OAIG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACvB;AAED,eAAO,MAAM,eAAe,eAAkC,CAAA;AAE9D,MAAM,WAAW,iBAAiB;IAAG,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI,CAAA;CAAE;AAErE,MAAM,WAAW,eAAe;IAAG,CAAC,UAAU,EAAE,SAAS,EAAE,GAAG,SAAS,EAAE,CAAA;CAAE;AAE3E,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AAQ,IAAA;CACT;AAED,MAAM,WAAW,eAAe,CAAC,uBAAuB,SAAS,aAAa,GAAG,aAAa,CAAE,SAAQ,eAAe,CAAC,uBAAuB,CAAC,EAAE,YAAY;IAC5J,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,YAAY,CAAC,EAAE,kBAAkB,CAAA;IACjC,MAAM,CAAC,EAAE,gBAAgB,CAAA;CAC1B;AAED,MAAM,MAAM,8BAA8B,GAC1C,aAAa,CAAC,qCAAqC,CAAC,GACpD,aAAa,CAAC,uCAAuC,CAAC,CAAA;AAEtD,MAAM,MAAM,+BAA+B,GAC3C,aAAa,CAAC,sCAAsC,CAAC,GACrD,aAAa,CAAC,wCAAwC,CAAC,CAAA;AAEvD,MAAM,WAAW,QAAQ;IACvB;;OAEG;IACH,eAAe,CAAC,MAAM,EAAE,mBAAmB,EAAE,IAAI,CAAC,EAAE,eAAe,CAAC,+BAA+B,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;IAE1H;;OAEG;IACH,cAAc,CAAC,MAAM,EAAE,mBAAmB,EAAE,IAAI,CAAC,EAAE,eAAe,CAAC,8BAA8B,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAA;CACzH"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/interface",
3
- "version": "1.7.0-3c8dd5bbf",
3
+ "version": "1.7.0-5214dec4a",
4
4
  "description": "The interface implemented by a libp2p node",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/interface#readme",
@@ -185,12 +185,16 @@ export interface NewStreamOptions extends AbortOptions {
185
185
  maxOutboundStreams?: number
186
186
 
187
187
  /**
188
- * Opt-in to running over a transient connection - one that has time/data limits
189
- * placed on it.
188
+ * Opt-in to running over a limited connection - one that has restrictions
189
+ * on the amount of data that may be transferred or how long it may be open for.
190
+ *
191
+ * These limits are typically enforced by a relay server, if the protocol
192
+ * will be transferring a lot of data or the stream will be open for a long time
193
+ * consider upgrading to a direct connection before opening the stream.
190
194
  *
191
195
  * @default false
192
196
  */
193
- runOnTransientConnection?: boolean
197
+ runOnLimitedConnection?: boolean
194
198
 
195
199
  /**
196
200
  * By default when negotiating a protocol the dialer writes then protocol name
@@ -222,6 +226,29 @@ export interface NewStreamOptions extends AbortOptions {
222
226
 
223
227
  export type ConnectionStatus = 'open' | 'closing' | 'closed'
224
228
 
229
+ /**
230
+ * Connection limits are present on connections that are only allowed to
231
+ * transfer a certain amount of bytes or be open for a certain number
232
+ * of seconds.
233
+ *
234
+ * These limits are applied by Circuit Relay v2 servers, for example and
235
+ * the connection will normally be closed abruptly if the limits are
236
+ * exceeded.
237
+ */
238
+ export interface ConnectionLimits {
239
+ /**
240
+ * If present this is the number of bytes remaining that may be
241
+ * transferred over this connection
242
+ */
243
+ bytes?: bigint
244
+
245
+ /**
246
+ * If present this is the number of seconds that this connection will
247
+ * remain open for
248
+ */
249
+ seconds?: number
250
+ }
251
+
225
252
  /**
226
253
  * A Connection is a high-level representation of a connection
227
254
  * to a remote peer that may have been secured by encryption and
@@ -280,12 +307,11 @@ export interface Connection {
280
307
  status: ConnectionStatus
281
308
 
282
309
  /**
283
- * A transient connection is one that is not expected to be open for very long
284
- * or one that cannot transfer very much data, such as one being used as a
285
- * circuit relay connection. Protocols need to explicitly opt-in to being run
286
- * over transient connections.
310
+ * If present, this connection has limits applied to it, perhaps by an
311
+ * intermediate relay. Once the limits have been reached the connection will
312
+ * be closed by the relay.
287
313
  */
288
- transient: boolean
314
+ limits?: ConnectionLimits
289
315
 
290
316
  /**
291
317
  * The time in milliseconds it takes to make a round trip to the remote peer.
@@ -1,8 +1,18 @@
1
1
  import type { MultiaddrConnection } from '../connection/index.js'
2
+ import type { AbortOptions } from '../index.js'
2
3
  import type { PeerId } from '../peer-id/index.js'
3
4
  import type { Duplex } from 'it-stream-types'
4
5
  import type { Uint8ArrayList } from 'uint8arraylist'
5
6
 
7
+ /**
8
+ * If the remote PeerId is known and passed as an option, the securing operation
9
+ * will throw if the remote peer cannot prove it has the private key that
10
+ * corresponds to the public key the remote PeerId is derived from.
11
+ */
12
+ export interface SecureConnectionOptions extends AbortOptions {
13
+ remotePeer?: PeerId
14
+ }
15
+
6
16
  /**
7
17
  * A libp2p connection encrypter module must be compliant to this interface
8
18
  * to ensure all exchanged data between two peers is encrypted.
@@ -15,14 +25,14 @@ export interface ConnectionEncrypter<Extension = unknown> {
15
25
  * pass it for extra verification, otherwise it will be determined during
16
26
  * the handshake.
17
27
  */
18
- secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, Extension>>
28
+ secureOutbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (connection: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream, Extension>>
19
29
 
20
30
  /**
21
31
  * Decrypt incoming data. If the remote PeerId is known,
22
32
  * pass it for extra verification, otherwise it will be determined during
23
33
  * the handshake
24
34
  */
25
- secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (localPeer: PeerId, connection: Stream, remotePeer?: PeerId): Promise<SecuredConnection<Stream, Extension>>
35
+ secureInbound <Stream extends Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>> = MultiaddrConnection> (connection: Stream, options?: SecureConnectionOptions): Promise<SecuredConnection<Stream, Extension>>
26
36
  }
27
37
 
28
38
  export interface SecuredConnection<Stream = any, Extension = unknown> {
package/src/errors.ts CHANGED
@@ -4,21 +4,15 @@
4
4
  * AbortSignal.
5
5
  */
6
6
  export class AbortError extends Error {
7
- public readonly code: string
8
- public readonly type: string
9
-
10
7
  constructor (message: string = 'The operation was aborted') {
11
8
  super(message)
12
9
  this.name = 'AbortError'
13
- this.code = AbortError.code
14
- this.type = AbortError.type
15
10
  }
16
-
17
- static readonly code = 'ABORT_ERR'
18
-
19
- static readonly type = 'aborted'
20
11
  }
21
12
 
13
+ /**
14
+ * @deprecated
15
+ */
22
16
  export class CodeError<T extends Record<string, any> = Record<string, never>> extends Error {
23
17
  public readonly props: T
24
18
 
@@ -34,6 +28,9 @@ export class CodeError<T extends Record<string, any> = Record<string, never>> ex
34
28
  }
35
29
  }
36
30
 
31
+ /**
32
+ * @deprecated
33
+ */
37
34
  export class AggregateCodeError<T extends Record<string, any> = Record<string, never>> extends AggregateError {
38
35
  public readonly props: T
39
36
 
@@ -51,44 +48,203 @@ export class AggregateCodeError<T extends Record<string, any> = Record<string, n
51
48
  }
52
49
 
53
50
  export class UnexpectedPeerError extends Error {
54
- public code: string
55
-
56
51
  constructor (message = 'Unexpected Peer') {
57
52
  super(message)
58
53
  this.name = 'UnexpectedPeerError'
59
- this.code = UnexpectedPeerError.code
60
54
  }
61
-
62
- static readonly code = 'ERR_UNEXPECTED_PEER'
63
55
  }
64
56
 
65
57
  export class InvalidCryptoExchangeError extends Error {
66
- public code: string
67
-
68
58
  constructor (message = 'Invalid crypto exchange') {
69
59
  super(message)
70
60
  this.name = 'InvalidCryptoExchangeError'
71
- this.code = InvalidCryptoExchangeError.code
72
61
  }
62
+ }
63
+
64
+ export class InvalidParametersError extends Error {
65
+ constructor (message = 'Invalid parameters') {
66
+ super(message)
67
+ this.name = 'InvalidParametersError'
68
+ }
69
+ }
73
70
 
74
- static readonly code = 'ERR_INVALID_CRYPTO_EXCHANGE'
71
+ export class InvalidPublicKeyError extends Error {
72
+ constructor (message = 'Invalid public key') {
73
+ super(message)
74
+ this.name = 'InvalidPublicKeyError'
75
+ }
75
76
  }
76
77
 
77
- export class InvalidCryptoTransmissionError extends Error {
78
- public code: string
78
+ export class InvalidPrivateKeyError extends Error {
79
+ constructor (message = 'Invalid private key') {
80
+ super(message)
81
+ this.name = 'InvalidPrivateKeyError'
82
+ }
83
+ }
79
84
 
80
- constructor (message = 'Invalid crypto transmission') {
85
+ export class UnsupportedOperationError extends Error {
86
+ constructor (message = 'Unsupported operation') {
81
87
  super(message)
82
- this.name = 'InvalidCryptoTransmissionError'
83
- this.code = InvalidCryptoTransmissionError.code
88
+ this.name = 'UnsupportedOperationError'
84
89
  }
90
+ }
85
91
 
86
- static readonly code = 'ERR_INVALID_CRYPTO_TRANSMISSION'
92
+ export class ConnectionClosingError extends Error {
93
+ constructor (message = 'The connection is closing') {
94
+ super(message)
95
+ this.name = 'ConnectionClosingError'
96
+ }
87
97
  }
88
98
 
89
- // Error codes
99
+ export class ConnectionClosedError extends Error {
100
+ constructor (message = 'The connection is closed') {
101
+ super(message)
102
+ this.name = 'ConnectionClosedError'
103
+ }
104
+ }
90
105
 
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'
106
+ export class ConnectionFailedError extends Error {
107
+ constructor (message = 'Connection failed') {
108
+ super(message)
109
+ this.name = 'ConnectionFailedError'
110
+ }
111
+ }
112
+
113
+ export class StreamResetError extends Error {
114
+ constructor (message = 'The stream has been reset') {
115
+ super(message)
116
+ this.name = 'StreamResetError'
117
+ }
118
+ }
119
+
120
+ export class StreamStateError extends Error {
121
+ constructor (message = 'The stream is in an invalid state') {
122
+ super(message)
123
+ this.name = 'StreamStateError'
124
+ }
125
+ }
126
+
127
+ export class NotFoundError extends Error {
128
+ constructor (message = 'Not found') {
129
+ super(message)
130
+ this.name = 'NotFoundError'
131
+ }
132
+ }
133
+
134
+ export class InvalidPeerIdError extends Error {
135
+ constructor (message = 'Invalid PeerID') {
136
+ super(message)
137
+ this.name = 'InvalidPeerIdError'
138
+ }
139
+ }
140
+
141
+ export class InvalidMultiaddrError extends Error {
142
+ constructor (message = 'Invalid multiaddr') {
143
+ super(message)
144
+ this.name = 'InvalidMultiaddrError'
145
+ }
146
+ }
147
+
148
+ export class InvalidCIDError extends Error {
149
+ constructor (message = 'Invalid CID') {
150
+ super(message)
151
+ this.name = 'InvalidCIDError'
152
+ }
153
+ }
154
+
155
+ export class InvalidMultihashError extends Error {
156
+ constructor (message = 'Invalid Multihash') {
157
+ super(message)
158
+ this.name = 'InvalidMultihashError'
159
+ }
160
+ }
161
+
162
+ export class UnsupportedProtocolError extends Error {
163
+ constructor (message = 'Unsupported protocol error') {
164
+ super(message)
165
+ this.name = 'UnsupportedProtocolError'
166
+ }
167
+ }
168
+
169
+ /**
170
+ * An invalid or malformed message was encountered during a protocol exchange
171
+ */
172
+ export class InvalidMessageError extends Error {
173
+ constructor (message = 'Invalid message') {
174
+ super(message)
175
+ this.name = 'InvalidMessageError'
176
+ }
177
+ }
178
+
179
+ export class ProtocolError extends Error {
180
+ constructor (message = 'Protocol error') {
181
+ super(message)
182
+ this.name = 'ProtocolError'
183
+ }
184
+ }
185
+
186
+ export class TimeoutError extends Error {
187
+ constructor (message = 'Timed out') {
188
+ super(message)
189
+ this.name = 'TimeoutError'
190
+ }
191
+ }
192
+
193
+ export class NotStartedError extends Error {
194
+ constructor (message = 'Not started') {
195
+ super(message)
196
+ this.name = 'NotStartedError'
197
+ }
198
+ }
199
+
200
+ export class AlreadyStartedError extends Error {
201
+ constructor (message = 'Already started') {
202
+ super(message)
203
+ this.name = 'AlreadyStartedError'
204
+ }
205
+ }
206
+
207
+ export class DialError extends Error {
208
+ constructor (message = 'Dial error') {
209
+ super(message)
210
+ this.name = 'DialError'
211
+ }
212
+ }
213
+
214
+ export class ListenError extends Error {
215
+ constructor (message = 'Listen error') {
216
+ super(message)
217
+ this.name = 'ListenError'
218
+ }
219
+ }
220
+
221
+ export class LimitedConnectionError extends Error {
222
+ constructor (message = 'Limited connection') {
223
+ super(message)
224
+ this.name = 'LimitedConnectionError'
225
+ }
226
+ }
227
+
228
+ export class TooManyInboundProtocolStreamsError extends Error {
229
+ constructor (message = 'Too many inbound protocol streams') {
230
+ super(message)
231
+ this.name = 'TooManyInboundProtocolStreamsError'
232
+ }
233
+ }
234
+
235
+ export class TooManyOutboundProtocolStreamsError extends Error {
236
+ constructor (message = 'Too many outbound protocol streams') {
237
+ super(message)
238
+ this.name = 'TooManyOutboundProtocolStreamsError'
239
+ }
240
+ }
241
+
242
+ /**
243
+ * Thrown when and attempt to operate on an unsupported key was made
244
+ */
245
+ export class UnsupportedKeyTypeError extends Error {
246
+ constructor (message = 'Unsupported key type') {
247
+ super(message)
248
+ this.name = 'UnsupportedKeyTypeError'
249
+ }
250
+ }
@@ -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,94 @@
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
9
52
  }
10
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
+
11
59
  /**
12
60
  * Generic private key interface
13
61
  */
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>
62
+ interface PrivateKeyBase<KeyType extends string, PublicKeyType extends PublicKeyBase<KeyType>> {
21
63
  /**
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.
64
+ * The type of this key
27
65
  */
28
- id(): Promise<string>
66
+ readonly type: KeyType
67
+
29
68
  /**
30
- * Exports the password protected key in the format specified.
69
+ * The public key that corresponds to this private key
31
70
  */
32
- export(password: string, format?: 'pkcs-8' | string): Promise<string>
33
- }
71
+ readonly publicKey: PublicKeyType
34
72
 
35
- export const Ed25519 = 'Ed25519'
36
- export const RSA = 'RSA'
37
- export const secp256k1 = 'secp256k1'
73
+ /**
74
+ * The raw public key bytes (for Ed25519 and secp256k1 keys) or PKIX in ASN1
75
+ * DER format (for RSA keys)
76
+ */
77
+ readonly raw: Uint8Array
78
+
79
+ /**
80
+ * Returns `true` if the passed object matches this key
81
+ */
82
+ equals(key?: any): boolean
83
+
84
+ /**
85
+ * Sign the passed data with this private key and return the signature for
86
+ * later verification
87
+ */
88
+ sign(data: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array>
89
+ }
38
90
 
39
- export type KeyType = typeof Ed25519 | typeof RSA | typeof secp256k1
91
+ export interface RSAPrivateKey extends PrivateKeyBase<'RSA', RSAPublicKey> {}
92
+ export interface Ed25519PrivateKey extends PrivateKeyBase<'Ed25519', Ed25519PublicKey> {}
93
+ export interface Secp256k1PrivateKey extends PrivateKeyBase<'secp256k1', Secp256k1PublicKey> {}
94
+ export type PrivateKey = RSAPrivateKey | Ed25519PrivateKey | Secp256k1PrivateKey
@@ -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