This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.ts
272 lines (242 loc) · 5.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import type { PeerId } from '@libp2p/interface-peer-id'
import type { Multiaddr } from '@multiformats/multiaddr'
/**
* A multiaddr with an optional flag that indicates if its trustworthy
*/
export interface Address {
/**
* Peer multiaddr
*/
multiaddr: Multiaddr
/**
* Obtained from a signed peer record
*/
isCertified: boolean
}
/**
* Data stored in the peer store about peers
*/
export interface Peer {
/**
* Peer's peer-id instance
*/
id: PeerId
/**
* Peer's addresses containing a list of multiaddrs and a isCertified field
* indicating if the address was loaded from a signed peer record or not
*/
addresses: Address[]
/**
* Peer's supported protocols
*/
protocols: string[]
/**
* Peer's metadata map
*/
metadata: Map<string, Uint8Array>
/**
* Tags a peer has
*/
tags: Map<string, Tag>
/**
* The last peer record envelope received
*/
peerRecordEnvelope?: Uint8Array
}
/**
* Peer data used to update the peer store
*/
export interface PeerData {
/**
* Peer's addresses containing its multiaddrs and metadata - multiaddrs
* passed here can be treated as certified if the `isCertifed` value is
* set to true.
*
* If both addresses and multiaddrs are specified they will be merged
* together with entries in addresses taking precedence.
*/
addresses?: Address[]
/**
* Peer's multiaddrs - any multiaddrs passed here will be treated as
* uncertified.
*
* If both addresses and multiaddrs are specified they will be merged
* together with entries in addresses taking precedence.
*/
multiaddrs?: Multiaddr[]
/**
* Peer's supported protocols
*/
protocols?: string[]
/**
* Peer's metadata map. When merging pass undefined as values to remove metadata.
*/
metadata?: Map<string, Uint8Array | undefined> | Record<string, Uint8Array | undefined>
/**
* Peer tags. When merging pass undefined as values to remove tags.
*/
tags?: Map<string, TagOptions | undefined> | Record<string, TagOptions | undefined>
/**
* If this Peer has an RSA key, it's public key can be set with this property
*/
publicKey?: Uint8Array
/**
* The last peer record envelope received
*/
peerRecordEnvelope?: Uint8Array
}
export interface TagOptions {
/**
* An optional tag value (1-100)
*/
value?: number
/**
* An optional duration in ms after which the tag will expire
*/
ttl?: number
}
export interface Tag {
/**
* The tag value
*/
value: number
}
/**
* A predicate by which to filter lists of peers
*/
export interface PeerQueryFilter { (peer: Peer): boolean }
/**
* A predicate by which to sort lists of peers
*/
export interface PeerQueryOrder { (a: Peer, b: Peer): -1 | 0 | 1 }
/**
* A query for getting lists of peers
*/
export interface PeerQuery {
filters?: PeerQueryFilter[]
orders?: PeerQueryOrder[]
limit?: number
offset?: number
}
export interface PeerStore {
/**
* Loop over every peer - the looping is async because we read from a
* datastore but the peer operation is sync, this is to prevent
* long-lived peer operations causing deadlocks over the datastore
* which can happen if they try to access the peer store during the
* loop
*
* @example
*
* ```js
* await peerStore.forEach(peer => {
* // ...
* })
* ```
*/
forEach: (fn: (peer: Peer) => void, query?: PeerQuery) => Promise<void>
/**
* Returns all peers in the peer store.
*
* @example
*
* ```js
* for (const peer of await peerStore.all()) {
* // ...
* }
* ```
*/
all: (query?: PeerQuery) => Promise<Peer[]>
/**
* Delete all data stored for the passed peer
*
* @example
*
* ```js
* await peerStore.addressBook.set(peerId, multiaddrs)
* await peerStore.addressBook.get(peerId)
* // multiaddrs[]
*
* await peerStore.delete(peerId)
*
* await peerStore.addressBook.get(peerId)
* // []
* ```
*/
delete: (peerId: PeerId) => Promise<void>
/**
* Returns true if the passed PeerId is in the peer store
*
* @example
*
* ```js
* await peerStore.has(peerId)
* // false
* await peerStore.addressBook.add(peerId, multiaddrs)
* await peerStore.has(peerId)
* // true
* ```
*/
has: (peerId: PeerId) => Promise<boolean>
/**
* Returns all data stored for the passed PeerId
*
* @example
*
* ```js
* const peer = await peerStore.get(peerId)
* // { .. }
* ```
*/
get: (peerId: PeerId) => Promise<Peer>
/**
* Adds a peer to the peer store, overwriting any existing data
*
* @example
*
* ```js
* await peerStore.save(peerId, {
* multiaddrs
* })
* ```
*/
save: (id: PeerId, data: PeerData) => Promise<Peer>
/**
* Adds a peer to the peer store, overwriting only the passed fields
*
* @example
*
* ```js
* await peerStore.patch(peerId, {
* multiaddrs
* })
* ```
*/
patch: (id: PeerId, data: PeerData) => Promise<Peer>
/**
* Adds a peer to the peer store, deeply merging any existing data.
*
* @example
*
* ```js
* await peerStore.merge(peerId, {
* multiaddrs
* })
* ```
*/
merge: (id: PeerId, data: PeerData) => Promise<Peer>
/**
* Unmarshal and verify a signed peer record, extract the multiaddrs and
* overwrite the stored addresses for the peer.
*
* Optionally pass an expected PeerId to verify that the peer record was
* signed by that peer.
*
* @example
*
* ```js
* await peerStore.consumePeerRecord(buf, expectedPeer)
* ```
*/
consumePeerRecord: (buf: Uint8Array, expectedPeer?: PeerId) => Promise<boolean>
}