@libp2p/interface 0.0.1-6fdaa7dc

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 (129) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +45 -0
  3. package/dist/index.min.js +3 -0
  4. package/dist/src/connection/index.d.ts +188 -0
  5. package/dist/src/connection/index.d.ts.map +1 -0
  6. package/dist/src/connection/index.js +5 -0
  7. package/dist/src/connection/index.js.map +1 -0
  8. package/dist/src/connection/status.d.ts +4 -0
  9. package/dist/src/connection/status.d.ts.map +1 -0
  10. package/dist/src/connection/status.js +4 -0
  11. package/dist/src/connection/status.js.map +1 -0
  12. package/dist/src/connection-encrypter/index.d.ts +27 -0
  13. package/dist/src/connection-encrypter/index.d.ts.map +1 -0
  14. package/dist/src/connection-encrypter/index.js +2 -0
  15. package/dist/src/connection-encrypter/index.js.map +1 -0
  16. package/dist/src/connection-gater/index.d.ts +117 -0
  17. package/dist/src/connection-gater/index.d.ts.map +1 -0
  18. package/dist/src/connection-gater/index.js +2 -0
  19. package/dist/src/connection-gater/index.js.map +1 -0
  20. package/dist/src/content-routing/index.d.ts +79 -0
  21. package/dist/src/content-routing/index.d.ts.map +1 -0
  22. package/dist/src/content-routing/index.js +21 -0
  23. package/dist/src/content-routing/index.js.map +1 -0
  24. package/dist/src/errors.d.ts +33 -0
  25. package/dist/src/errors.d.ts.map +1 -0
  26. package/dist/src/errors.js +51 -0
  27. package/dist/src/errors.js.map +1 -0
  28. package/dist/src/events.d.ts +28 -0
  29. package/dist/src/events.d.ts.map +1 -0
  30. package/dist/src/events.js +70 -0
  31. package/dist/src/events.js.map +1 -0
  32. package/dist/src/index.d.ts +575 -0
  33. package/dist/src/index.d.ts.map +1 -0
  34. package/dist/src/index.js +17 -0
  35. package/dist/src/index.js.map +1 -0
  36. package/dist/src/keychain/index.d.ts +154 -0
  37. package/dist/src/keychain/index.d.ts.map +1 -0
  38. package/dist/src/keychain/index.js +23 -0
  39. package/dist/src/keychain/index.js.map +1 -0
  40. package/dist/src/keys/index.d.ts +35 -0
  41. package/dist/src/keys/index.d.ts.map +1 -0
  42. package/dist/src/keys/index.js +4 -0
  43. package/dist/src/keys/index.js.map +1 -0
  44. package/dist/src/metrics/index.d.ts +165 -0
  45. package/dist/src/metrics/index.d.ts.map +1 -0
  46. package/dist/src/metrics/index.js +2 -0
  47. package/dist/src/metrics/index.js.map +1 -0
  48. package/dist/src/metrics/tracked-map.d.ts +17 -0
  49. package/dist/src/metrics/tracked-map.d.ts.map +1 -0
  50. package/dist/src/metrics/tracked-map.js +38 -0
  51. package/dist/src/metrics/tracked-map.js.map +1 -0
  52. package/dist/src/peer-discovery/index.d.ts +28 -0
  53. package/dist/src/peer-discovery/index.d.ts.map +1 -0
  54. package/dist/src/peer-discovery/index.js +21 -0
  55. package/dist/src/peer-discovery/index.js.map +1 -0
  56. package/dist/src/peer-id/index.d.ts +30 -0
  57. package/dist/src/peer-id/index.d.ts.map +1 -0
  58. package/dist/src/peer-id/index.js +5 -0
  59. package/dist/src/peer-id/index.js.map +1 -0
  60. package/dist/src/peer-info/index.d.ts +8 -0
  61. package/dist/src/peer-info/index.d.ts.map +1 -0
  62. package/dist/src/peer-info/index.js +2 -0
  63. package/dist/src/peer-info/index.js.map +1 -0
  64. package/dist/src/peer-routing/index.d.ts +51 -0
  65. package/dist/src/peer-routing/index.d.ts.map +1 -0
  66. package/dist/src/peer-routing/index.js +21 -0
  67. package/dist/src/peer-routing/index.js.map +1 -0
  68. package/dist/src/peer-store/index.d.ts +247 -0
  69. package/dist/src/peer-store/index.d.ts.map +1 -0
  70. package/dist/src/peer-store/index.js +2 -0
  71. package/dist/src/peer-store/index.js.map +1 -0
  72. package/dist/src/peer-store/tags.d.ts +2 -0
  73. package/dist/src/peer-store/tags.d.ts.map +1 -0
  74. package/dist/src/peer-store/tags.js +2 -0
  75. package/dist/src/peer-store/tags.js.map +1 -0
  76. package/dist/src/record/index.d.ts +33 -0
  77. package/dist/src/record/index.d.ts.map +1 -0
  78. package/dist/src/record/index.js +2 -0
  79. package/dist/src/record/index.js.map +1 -0
  80. package/dist/src/startable.d.ts +46 -0
  81. package/dist/src/startable.d.ts.map +1 -0
  82. package/dist/src/startable.js +46 -0
  83. package/dist/src/startable.js.map +1 -0
  84. package/dist/src/stream-handler/index.d.ts +23 -0
  85. package/dist/src/stream-handler/index.d.ts.map +1 -0
  86. package/dist/src/stream-handler/index.js +2 -0
  87. package/dist/src/stream-handler/index.js.map +1 -0
  88. package/dist/src/stream-muxer/index.d.ts +51 -0
  89. package/dist/src/stream-muxer/index.d.ts.map +1 -0
  90. package/dist/src/stream-muxer/index.js +2 -0
  91. package/dist/src/stream-muxer/index.js.map +1 -0
  92. package/dist/src/stream-muxer/stream.d.ts +85 -0
  93. package/dist/src/stream-muxer/stream.d.ts.map +1 -0
  94. package/dist/src/stream-muxer/stream.js +255 -0
  95. package/dist/src/stream-muxer/stream.js.map +1 -0
  96. package/dist/src/topology/index.d.ts +9 -0
  97. package/dist/src/topology/index.d.ts.map +1 -0
  98. package/dist/src/topology/index.js +2 -0
  99. package/dist/src/topology/index.js.map +1 -0
  100. package/dist/src/transport/index.d.ts +96 -0
  101. package/dist/src/transport/index.d.ts.map +1 -0
  102. package/dist/src/transport/index.js +19 -0
  103. package/dist/src/transport/index.js.map +1 -0
  104. package/package.json +177 -0
  105. package/src/connection/index.ts +221 -0
  106. package/src/connection/status.ts +4 -0
  107. package/src/connection-encrypter/index.ts +30 -0
  108. package/src/connection-gater/index.ts +128 -0
  109. package/src/content-routing/index.ts +83 -0
  110. package/src/errors.ts +68 -0
  111. package/src/events.ts +100 -0
  112. package/src/index.ts +627 -0
  113. package/src/keychain/index.ts +167 -0
  114. package/src/keys/index.ts +38 -0
  115. package/src/metrics/index.ts +187 -0
  116. package/src/metrics/tracked-map.ts +65 -0
  117. package/src/peer-discovery/index.ts +29 -0
  118. package/src/peer-id/index.ts +39 -0
  119. package/src/peer-info/index.ts +8 -0
  120. package/src/peer-routing/index.ts +53 -0
  121. package/src/peer-store/index.ts +272 -0
  122. package/src/peer-store/tags.ts +2 -0
  123. package/src/record/index.ts +35 -0
  124. package/src/startable.ts +117 -0
  125. package/src/stream-handler/index.ts +27 -0
  126. package/src/stream-muxer/index.ts +58 -0
  127. package/src/stream-muxer/stream.ts +365 -0
  128. package/src/topology/index.ts +10 -0
  129. package/src/transport/index.ts +111 -0
@@ -0,0 +1,167 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * The libp2p keychain provides an API to store keys in a datastore in
5
+ * an encrypted format.
6
+ *
7
+ * @example
8
+ *
9
+ * ```typescript
10
+ * import { createLibp2p } from 'libp2p'
11
+ * import { FsDatastore } from 'datastore-fs'
12
+ *
13
+ * const node = await createLibp2p({
14
+ * datastore: new FsDatastore('/path/to/dir')
15
+ * })
16
+ *
17
+ * const info = await node.keychain.createKey('my-new-key', 'Ed25519')
18
+ *
19
+ * console.info(info) // { id: '...', name: 'my-new-key' }
20
+ * ```
21
+ */
22
+
23
+ import type { KeyType } from '../keys/index.js'
24
+ import type { PeerId } from '../peer-id/index.js'
25
+ import type { Multibase } from 'multiformats/bases/interface'
26
+
27
+ export interface KeyInfo {
28
+ /**
29
+ * The universally unique key id
30
+ */
31
+ id: string
32
+
33
+ /**
34
+ * The local key name
35
+ */
36
+ name: string
37
+ }
38
+
39
+ export interface KeyChain {
40
+ /**
41
+ * Export an existing key as a PEM encrypted PKCS #8 string.
42
+ *
43
+ * @example
44
+ *
45
+ * ```js
46
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
47
+ * const pemKey = await libp2p.keychain.exportKey('keyTest', 'password123')
48
+ * ```
49
+ */
50
+ exportKey: (name: string, password: string) => Promise<Multibase<'m'>>
51
+
52
+ /**
53
+ * Import a new key from a PEM encoded PKCS #8 string.
54
+ *
55
+ * @example
56
+ *
57
+ * ```js
58
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
59
+ * const pemKey = await libp2p.keychain.exportKey('keyTest', 'password123')
60
+ * const keyInfo = await libp2p.keychain.importKey('keyTestImport', pemKey, 'password123')
61
+ * ```
62
+ */
63
+ importKey: (name: string, pem: string, password: string) => Promise<KeyInfo>
64
+
65
+ /**
66
+ * Import a new key from a PeerId with a private key component
67
+ *
68
+ * @example
69
+ *
70
+ * ```js
71
+ * const keyInfo = await libp2p.keychain.importPeer('keyTestImport', peerIdFromString('12D3Foo...'))
72
+ * ```
73
+ */
74
+ importPeer: (name: string, peerId: PeerId) => Promise<KeyInfo>
75
+
76
+ /**
77
+ * Export an existing key as a PeerId
78
+ *
79
+ * @example
80
+ *
81
+ * ```js
82
+ * const peerId = await libp2p.keychain.exportPeerId('key-name')
83
+ * ```
84
+ */
85
+ exportPeerId: (name: string) => Promise<PeerId>
86
+
87
+ /**
88
+ * Create a key in the keychain.
89
+ *
90
+ * @example
91
+ *
92
+ * ```js
93
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
94
+ * ```
95
+ */
96
+ createKey: (name: string, type: KeyType, size?: number) => Promise<KeyInfo>
97
+
98
+ /**
99
+ * List all the keys.
100
+ *
101
+ * @example
102
+ *
103
+ * ```js
104
+ * const keyInfos = await libp2p.keychain.listKeys()
105
+ * ```
106
+ */
107
+ listKeys: () => Promise<KeyInfo[]>
108
+
109
+ /**
110
+ * Removes a key from the keychain.
111
+ *
112
+ * @example
113
+ *
114
+ * ```js
115
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
116
+ * const keyInfo = await libp2p.keychain.removeKey('keyTest')
117
+ * ```
118
+ */
119
+ removeKey: (name: string) => Promise<KeyInfo>
120
+
121
+ /**
122
+ * Rename a key in the keychain.
123
+ *
124
+ * @example
125
+ *
126
+ * ```js
127
+ * await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
128
+ * const keyInfo = await libp2p.keychain.renameKey('keyTest', 'keyNewNtest')
129
+ * ```
130
+ */
131
+ renameKey: (oldName: string, newName: string) => Promise<KeyInfo>
132
+
133
+ /**
134
+ * Find a key by it's id.
135
+ *
136
+ * @example
137
+ *
138
+ * ```js
139
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
140
+ * const keyInfo2 = await libp2p.keychain.findKeyById(keyInfo.id)
141
+ * ```
142
+ */
143
+ findKeyById: (id: string) => Promise<KeyInfo>
144
+
145
+ /**
146
+ * Find a key by it's name.
147
+ *
148
+ * @example
149
+ *
150
+ * ```js
151
+ * const keyInfo = await libp2p.keychain.createKey('keyTest', 'RSA', 4096)
152
+ * const keyInfo2 = await libp2p.keychain.findKeyByName('keyTest')
153
+ * ```
154
+ */
155
+ findKeyByName: (name: string) => Promise<KeyInfo>
156
+
157
+ /**
158
+ * Rotate keychain password and re-encrypt all associated keys
159
+ *
160
+ * @example
161
+ *
162
+ * ```js
163
+ * await libp2p.keychain.rotateKeychainPass('oldPassword', 'newPassword')
164
+ * ```
165
+ */
166
+ rotateKeychainPass: (oldPass: string, newPass: string) => Promise<void>
167
+ }
@@ -0,0 +1,38 @@
1
+
2
+ export interface PublicKey {
3
+ readonly bytes: Uint8Array
4
+ verify: (data: Uint8Array, sig: Uint8Array) => Promise<boolean>
5
+ marshal: () => Uint8Array
6
+ equals: (key: PublicKey) => boolean
7
+ hash: () => Promise<Uint8Array>
8
+ }
9
+
10
+ /**
11
+ * Generic private key interface
12
+ */
13
+ export interface PrivateKey {
14
+ readonly public: PublicKey
15
+ readonly bytes: Uint8Array
16
+ sign: (data: Uint8Array) => Promise<Uint8Array>
17
+ marshal: () => Uint8Array
18
+ equals: (key: PrivateKey) => boolean
19
+ hash: () => Promise<Uint8Array>
20
+ /**
21
+ * Gets the ID of the key.
22
+ *
23
+ * The key id is the base58 encoding of the SHA-256 multihash of its public key.
24
+ * The public key is a protobuf encoding containing a type and the DER encoding
25
+ * of the PKCS SubjectPublicKeyInfo.
26
+ */
27
+ id: () => Promise<string>
28
+ /**
29
+ * Exports the password protected key in the format specified.
30
+ */
31
+ export: (password: string, format?: 'pkcs-8' | string) => Promise<string>
32
+ }
33
+
34
+ export const Ed25519 = 'Ed25519'
35
+ export const RSA = 'RSA'
36
+ export const secp256k1 = 'secp256k1'
37
+
38
+ export type KeyType = typeof Ed25519 | typeof RSA | typeof secp256k1
@@ -0,0 +1,187 @@
1
+ import type { MultiaddrConnection, Stream, Connection } from '../connection/index.js'
2
+
3
+ /**
4
+ * Create tracked metrics with these options. Loosely based on the
5
+ * interfaces exposed by the prom-client module
6
+ */
7
+ export interface MetricOptions {
8
+ /**
9
+ * Optional label for the metric
10
+ */
11
+ label?: string
12
+
13
+ /**
14
+ * Optional help for the metric
15
+ */
16
+ help?: string
17
+ }
18
+
19
+ /**
20
+ * A function that returns a tracked metric which may be expensive
21
+ * to calculate so it is only invoked when metrics are being scraped
22
+ */
23
+ export type CalculateMetric<T = number> = (() => T) | (() => Promise<T>)
24
+
25
+ /**
26
+ * Create tracked metrics that are expensive to calculate by passing
27
+ * a function that is only invoked when metrics are being scraped
28
+ */
29
+ export interface CalculatedMetricOptions<T = number> extends MetricOptions {
30
+ /**
31
+ * An optional function invoked to calculate the component metric instead of
32
+ * using `.update`, `.increment`, and `.decrement`
33
+ */
34
+ calculate: CalculateMetric<T>
35
+ }
36
+
37
+ /**
38
+ * Call this function to stop the timer returned from the `.timer` method
39
+ * on the metric
40
+ */
41
+ export interface StopTimer { (): void }
42
+
43
+ /**
44
+ * A tracked metric loosely based on the interfaces exposed by the
45
+ * prom-client module
46
+ */
47
+ export interface Metric {
48
+ /**
49
+ * Update the stored metric to the passed value
50
+ */
51
+ update: (value: number) => void
52
+
53
+ /**
54
+ * Increment the metric by the passed value or 1
55
+ */
56
+ increment: (value?: number) => void
57
+
58
+ /**
59
+ * Decrement the metric by the passed value or 1
60
+ */
61
+ decrement: (value?: number) => void
62
+
63
+ /**
64
+ * Reset this metric to its default value
65
+ */
66
+ reset: () => void
67
+
68
+ /**
69
+ * Start a timed metric, call the returned function to
70
+ * stop the timer
71
+ */
72
+ timer: () => StopTimer
73
+ }
74
+
75
+ /**
76
+ * A group of related metrics loosely based on the interfaces exposed by the
77
+ * prom-client module
78
+ */
79
+ export interface MetricGroup {
80
+ /**
81
+ * Update the stored metric group to the passed value
82
+ */
83
+ update: (values: Record<string, number>) => void
84
+
85
+ /**
86
+ * Increment the metric group keys by the passed number or
87
+ * any non-numeric value to increment by 1
88
+ */
89
+ increment: (values: Record<string, number | unknown>) => void
90
+
91
+ /**
92
+ * Decrement the metric group keys by the passed number or
93
+ * any non-numeric value to decrement by 1
94
+ */
95
+ decrement: (values: Record<string, number | unknown>) => void
96
+
97
+ /**
98
+ * Reset the passed key in this metric group to its default value
99
+ * or all keys if no key is passed
100
+ */
101
+ reset: () => void
102
+
103
+ /**
104
+ * Start a timed metric for the named key in the group, call
105
+ * the returned function to stop the timer
106
+ */
107
+ timer: (key: string) => StopTimer
108
+ }
109
+
110
+ /**
111
+ * A tracked counter loosely based on the Counter interface exposed
112
+ * by the prom-client module - counters are metrics that only go up
113
+ */
114
+ export interface Counter {
115
+ /**
116
+ * Increment the metric by the passed value or 1
117
+ */
118
+ increment: (value?: number) => void
119
+
120
+ /**
121
+ * Reset this metric to its default value
122
+ */
123
+ reset: () => void
124
+ }
125
+
126
+ /**
127
+ * A group of tracked counters loosely based on the Counter interface
128
+ * exposed by the prom-client module - counters are metrics that only
129
+ * go up
130
+ */
131
+ export interface CounterGroup {
132
+ /**
133
+ * Increment the metric group keys by the passed number or
134
+ * any non-numeric value to increment by 1
135
+ */
136
+ increment: (values: Record<string, number | unknown>) => void
137
+
138
+ /**
139
+ * Reset the passed key in this metric group to its default value
140
+ * or all keys if no key is passed
141
+ */
142
+ reset: () => void
143
+ }
144
+
145
+ /**
146
+ * The libp2p metrics tracking object. This interface is only concerned
147
+ * with the collection of metrics, please see the individual implementations
148
+ * for how to extract metrics for viewing.
149
+ */
150
+ export interface Metrics {
151
+ /**
152
+ * Track a newly opened multiaddr connection
153
+ */
154
+ trackMultiaddrConnection: (maConn: MultiaddrConnection) => void
155
+
156
+ /**
157
+ * Track a newly opened protocol stream
158
+ */
159
+ trackProtocolStream: (stream: Stream, connection: Connection) => void
160
+
161
+ /**
162
+ * Register an arbitrary metric. Call this to set help/labels for metrics
163
+ * and update/increment/decrement/etc them by calling methods on the returned
164
+ * metric object
165
+ */
166
+ registerMetric: ((name: string, options?: MetricOptions) => Metric) & ((name: string, options: CalculatedMetricOptions) => void)
167
+
168
+ /**
169
+ * Register a a group of related metrics. Call this to set help/labels for
170
+ * groups of related metrics that will be updated with by calling `.update`,
171
+ * `.increment` and/or `.decrement` methods on the returned metric group object
172
+ */
173
+ registerMetricGroup: ((name: string, options?: MetricOptions) => MetricGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number>>) => void)
174
+
175
+ /**
176
+ * Register an arbitrary counter. Call this to set help/labels for counters
177
+ * and increment them by calling methods on the returned counter object
178
+ */
179
+ registerCounter: ((name: string, options?: MetricOptions) => Counter) & ((name: string, options: CalculatedMetricOptions) => void)
180
+
181
+ /**
182
+ * Register a a group of related counters. Call this to set help/labels for
183
+ * groups of related counters that will be updated with by calling the `.increment`
184
+ * method on the returned counter group object
185
+ */
186
+ registerCounterGroup: ((name: string, options?: MetricOptions) => CounterGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number>>) => void)
187
+ }
@@ -0,0 +1,65 @@
1
+ import type { Metric, Metrics } from './index.js'
2
+
3
+ export interface TrackedMapInit {
4
+ name: string
5
+ metrics: Metrics
6
+ }
7
+
8
+ class TrackedMap<K, V> extends Map<K, V> {
9
+ private readonly metric: Metric
10
+
11
+ constructor (init: TrackedMapInit) {
12
+ super()
13
+
14
+ const { name, metrics } = init
15
+
16
+ this.metric = metrics.registerMetric(name)
17
+ this.updateComponentMetric()
18
+ }
19
+
20
+ set (key: K, value: V): this {
21
+ super.set(key, value)
22
+ this.updateComponentMetric()
23
+ return this
24
+ }
25
+
26
+ delete (key: K): boolean {
27
+ const deleted = super.delete(key)
28
+ this.updateComponentMetric()
29
+ return deleted
30
+ }
31
+
32
+ clear (): void {
33
+ super.clear()
34
+ this.updateComponentMetric()
35
+ }
36
+
37
+ private updateComponentMetric (): void {
38
+ this.metric.update(this.size)
39
+ }
40
+ }
41
+
42
+ export interface CreateTrackedMapInit {
43
+ /**
44
+ * The metric name to use
45
+ */
46
+ name: string
47
+
48
+ /**
49
+ * A metrics implementation
50
+ */
51
+ metrics?: Metrics
52
+ }
53
+
54
+ export function trackedMap <K, V> (config: CreateTrackedMapInit): Map<K, V> {
55
+ const { name, metrics } = config
56
+ let map: Map<K, V>
57
+
58
+ if (metrics != null) {
59
+ map = new TrackedMap<K, V>({ name, metrics })
60
+ } else {
61
+ map = new Map<K, V>()
62
+ }
63
+
64
+ return map
65
+ }
@@ -0,0 +1,29 @@
1
+ import type { EventEmitter } from '../events.js'
2
+ import type { PeerInfo } from '../peer-info/index.js'
3
+
4
+ /**
5
+ * Any object that implements this Symbol as a property should return a
6
+ * PeerDiscovery instance as the property value, similar to how
7
+ * `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
8
+ *
9
+ * @example
10
+ *
11
+ * ```js
12
+ * import { peerDiscovery, PeerDiscovery } from '@libp2p/peer-discovery'
13
+ *
14
+ * class MyPeerDiscoverer implements PeerDiscovery {
15
+ * get [peerDiscovery] () {
16
+ * return this
17
+ * }
18
+ *
19
+ * // ...other methods
20
+ * }
21
+ * ```
22
+ */
23
+ export const peerDiscovery = Symbol.for('@libp2p/peer-discovery')
24
+
25
+ export interface PeerDiscoveryEvents {
26
+ 'peer': CustomEvent<PeerInfo>
27
+ }
28
+
29
+ export interface PeerDiscovery extends EventEmitter<PeerDiscoveryEvents> {}
@@ -0,0 +1,39 @@
1
+ import type { CID } from 'multiformats/cid'
2
+ import type { MultihashDigest } from 'multiformats/hashes/interface'
3
+
4
+ export type PeerIdType = 'RSA' | 'Ed25519' | 'secp256k1'
5
+
6
+ interface BasePeerId {
7
+ readonly type: PeerIdType
8
+ readonly multihash: MultihashDigest
9
+ readonly privateKey?: Uint8Array
10
+ readonly publicKey?: Uint8Array
11
+
12
+ toString: () => string
13
+ toCID: () => CID
14
+ toBytes: () => Uint8Array
15
+ equals: (other: PeerId | Uint8Array | string) => boolean
16
+ }
17
+
18
+ export interface RSAPeerId extends BasePeerId {
19
+ readonly type: 'RSA'
20
+ readonly publicKey?: Uint8Array
21
+ }
22
+
23
+ export interface Ed25519PeerId extends BasePeerId {
24
+ readonly type: 'Ed25519'
25
+ readonly publicKey: Uint8Array
26
+ }
27
+
28
+ export interface Secp256k1PeerId extends BasePeerId {
29
+ readonly type: 'secp256k1'
30
+ readonly publicKey: Uint8Array
31
+ }
32
+
33
+ export type PeerId = RSAPeerId | Ed25519PeerId | Secp256k1PeerId
34
+
35
+ export const symbol = Symbol.for('@libp2p/peer-id')
36
+
37
+ export function isPeerId (other: any): other is PeerId {
38
+ return other != null && Boolean(other[symbol])
39
+ }
@@ -0,0 +1,8 @@
1
+ import type { PeerId } from '../peer-id/index.js'
2
+ import type { Multiaddr } from '@multiformats/multiaddr'
3
+
4
+ export interface PeerInfo {
5
+ id: PeerId
6
+ multiaddrs: Multiaddr[]
7
+ protocols: string[]
8
+ }
@@ -0,0 +1,53 @@
1
+ import type { AbortOptions } from '../index.js'
2
+ import type { PeerId } from '../peer-id/index.js'
3
+ import type { PeerInfo } from '../peer-info/index.js'
4
+
5
+ /**
6
+ * Any object that implements this Symbol as a property should return a
7
+ * PeerRouting instance as the property value, similar to how
8
+ * `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
9
+ *
10
+ * @example
11
+ *
12
+ * ```js
13
+ * import { peerRouting, PeerRouting } from '@libp2p/peer-routing'
14
+ *
15
+ * class MyPeerRouter implements PeerRouting {
16
+ * get [peerRouting] () {
17
+ * return this
18
+ * }
19
+ *
20
+ * // ...other methods
21
+ * }
22
+ * ```
23
+ */
24
+ export const peerRouting = Symbol.for('@libp2p/peer-routing')
25
+
26
+ export interface PeerRouting {
27
+ /**
28
+ * Searches the network for peer info corresponding to the passed peer id.
29
+ *
30
+ * @example
31
+ *
32
+ * ```js
33
+ * // ...
34
+ * const peer = await peerRouting.findPeer(peerId, options)
35
+ * ```
36
+ */
37
+ findPeer: (peerId: PeerId, options?: AbortOptions) => Promise<PeerInfo>
38
+
39
+ /**
40
+ * Search the network for peers that are closer to the passed key. Peer
41
+ * info should be yielded in ever-increasing closeness to the key.
42
+ *
43
+ * @example
44
+ *
45
+ * ```js
46
+ * // Iterate over the closest peers found for the given key
47
+ * for await (const peer of peerRouting.getClosestPeers(key)) {
48
+ * console.log(peer.id, peer.multiaddrs)
49
+ * }
50
+ * ```
51
+ */
52
+ getClosestPeers: (key: Uint8Array, options?: AbortOptions) => AsyncIterable<PeerInfo>
53
+ }