@libp2p/peer-store 0.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.
Files changed (44) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +44 -0
  3. package/dist/src/address-book.d.ts +31 -0
  4. package/dist/src/address-book.d.ts.map +1 -0
  5. package/dist/src/address-book.js +265 -0
  6. package/dist/src/address-book.js.map +1 -0
  7. package/dist/src/errors.d.ts +5 -0
  8. package/dist/src/errors.d.ts.map +1 -0
  9. package/dist/src/errors.js +5 -0
  10. package/dist/src/errors.js.map +1 -0
  11. package/dist/src/index.d.ts +43 -0
  12. package/dist/src/index.d.ts.map +1 -0
  13. package/dist/src/index.js +88 -0
  14. package/dist/src/index.js.map +1 -0
  15. package/dist/src/key-book.d.ts +21 -0
  16. package/dist/src/key-book.d.ts.map +1 -0
  17. package/dist/src/key-book.js +98 -0
  18. package/dist/src/key-book.js.map +1 -0
  19. package/dist/src/metadata-book.d.ts +28 -0
  20. package/dist/src/metadata-book.d.ts.map +1 -0
  21. package/dist/src/metadata-book.js +168 -0
  22. package/dist/src/metadata-book.js.map +1 -0
  23. package/dist/src/pb/peer.d.ts +222 -0
  24. package/dist/src/pb/peer.js +641 -0
  25. package/dist/src/proto-book.d.ts +18 -0
  26. package/dist/src/proto-book.d.ts.map +1 -0
  27. package/dist/src/proto-book.js +170 -0
  28. package/dist/src/proto-book.js.map +1 -0
  29. package/dist/src/store.d.ts +37 -0
  30. package/dist/src/store.d.ts.map +1 -0
  31. package/dist/src/store.js +169 -0
  32. package/dist/src/store.js.map +1 -0
  33. package/package.json +162 -0
  34. package/src/README.md +145 -0
  35. package/src/address-book.ts +330 -0
  36. package/src/errors.ts +5 -0
  37. package/src/index.ts +123 -0
  38. package/src/key-book.ts +117 -0
  39. package/src/metadata-book.ts +200 -0
  40. package/src/pb/peer.d.ts +222 -0
  41. package/src/pb/peer.js +641 -0
  42. package/src/pb/peer.proto +31 -0
  43. package/src/proto-book.ts +204 -0
  44. package/src/store.ts +224 -0
@@ -0,0 +1,117 @@
1
+ import { logger } from '@libp2p/logger'
2
+ import errcode from 'err-code'
3
+ import { codes } from './errors.js'
4
+ import { PeerId } from '@libp2p/peer-id'
5
+ import { equals as uint8arrayEquals } from 'uint8arrays/equals'
6
+ import type { Store } from './store.js'
7
+ import type { PeerStore, KeyBook } from '@libp2p/interfaces/src/peer-store'
8
+
9
+ /**
10
+ * @typedef {import('./types').PeerStore} PeerStore
11
+ * @typedef {import('./types').KeyBook} KeyBook
12
+ * @typedef {import('libp2p-interfaces/src/keys/types').PublicKey} PublicKey
13
+ */
14
+
15
+ const log = logger('libp2p:peer-store:key-book')
16
+
17
+ const EVENT_NAME = 'change:pubkey'
18
+
19
+ export class PeerStoreKeyBook implements KeyBook {
20
+ private emit: PeerStore["emit"]
21
+ private store: Store
22
+
23
+ /**
24
+ * The KeyBook is responsible for keeping the known public keys of a peer
25
+ */
26
+ constructor (emit: PeerStore["emit"], store: Store) {
27
+ this.emit = emit
28
+ this.store = store
29
+ }
30
+
31
+ /**
32
+ * Set the Peer public key
33
+ */
34
+ async set (peerId: PeerId, publicKey: Uint8Array) {
35
+ peerId = PeerId.fromPeerId(peerId)
36
+
37
+ if (!publicKey) {
38
+ log.error('publicKey must be an instance of PublicKey to store data')
39
+ throw errcode(new Error('publicKey must be an instance of PublicKey'), codes.ERR_INVALID_PARAMETERS)
40
+ }
41
+
42
+ log('set await write lock')
43
+ const release = await this.store.lock.writeLock()
44
+ log('set got write lock')
45
+
46
+ let updatedKey = false
47
+
48
+ try {
49
+ try {
50
+ const existing = await this.store.load(peerId)
51
+
52
+ if (existing.pubKey && uint8arrayEquals(existing.pubKey, publicKey)) {
53
+ return
54
+ }
55
+ } catch (err: any) {
56
+ if (err.code !== codes.ERR_NOT_FOUND) {
57
+ throw err
58
+ }
59
+ }
60
+
61
+ await this.store.patchOrCreate(peerId, {
62
+ pubKey: publicKey
63
+ })
64
+ updatedKey = true
65
+ } finally {
66
+ log('set release write lock')
67
+ release()
68
+ }
69
+
70
+ if (updatedKey) {
71
+ this.emit(EVENT_NAME, { peerId, pubKey: publicKey })
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Get Public key of the given PeerId, if stored
77
+ */
78
+ async get (peerId: PeerId) {
79
+ peerId = PeerId.fromPeerId(peerId)
80
+
81
+ log('get await write lock')
82
+ const release = await this.store.lock.readLock()
83
+ log('get got write lock')
84
+
85
+ try {
86
+ const peer = await this.store.load(peerId)
87
+
88
+ return peer.pubKey
89
+ } catch (err: any) {
90
+ if (err.code !== codes.ERR_NOT_FOUND) {
91
+ throw err
92
+ }
93
+ } finally {
94
+ log('get release write lock')
95
+ release()
96
+ }
97
+ }
98
+
99
+ async delete (peerId: PeerId) {
100
+ peerId = PeerId.fromPeerId(peerId)
101
+
102
+ log('delete await write lock')
103
+ const release = await this.store.lock.writeLock()
104
+ log('delete got write lock')
105
+
106
+ try {
107
+ await this.store.patchOrCreate(peerId, {
108
+ pubKey: undefined
109
+ })
110
+ } finally {
111
+ log('delete release write lock')
112
+ release()
113
+ }
114
+
115
+ this.emit(EVENT_NAME, { peerId, pubKey: undefined })
116
+ }
117
+ }
@@ -0,0 +1,200 @@
1
+ import { logger } from '@libp2p/logger'
2
+ import errcode from 'err-code'
3
+ import { codes } from './errors.js'
4
+ import { PeerId } from '@libp2p/peer-id'
5
+ import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
6
+ import type { Store } from './store.js'
7
+ import type { PeerStore, MetadataBook } from '@libp2p/interfaces/src/peer-store'
8
+
9
+ const log = logger('libp2p:peer-store:metadata-book')
10
+
11
+ const EVENT_NAME = 'change:metadata'
12
+
13
+ export class PeerStoreMetadataBook implements MetadataBook {
14
+ private emit: PeerStore["emit"]
15
+ private store: Store
16
+
17
+ /**
18
+ * The MetadataBook is responsible for keeping metadata
19
+ * about known peers
20
+ */
21
+ constructor (emit: PeerStore["emit"], store: Store) {
22
+ this.emit = emit
23
+ this.store = store
24
+ }
25
+
26
+ /**
27
+ * Get the known data of a provided peer
28
+ */
29
+ async get (peerId: PeerId) {
30
+ peerId = PeerId.fromPeerId(peerId)
31
+
32
+ log('get await read lock')
33
+ const release = await this.store.lock.readLock()
34
+ log('get got read lock')
35
+
36
+ try {
37
+ const peer = await this.store.load(peerId)
38
+
39
+ return peer.metadata
40
+ } catch (err: any) {
41
+ if (err.code !== codes.ERR_NOT_FOUND) {
42
+ throw err
43
+ }
44
+ } finally {
45
+ log('get release read lock')
46
+ release()
47
+ }
48
+
49
+ return new Map()
50
+ }
51
+
52
+ /**
53
+ * Get specific metadata value, if it exists
54
+ */
55
+ async getValue (peerId: PeerId, key: string) {
56
+ peerId = PeerId.fromPeerId(peerId)
57
+
58
+ log('getValue await read lock')
59
+ const release = await this.store.lock.readLock()
60
+ log('getValue got read lock')
61
+
62
+ try {
63
+ const peer = await this.store.load(peerId)
64
+
65
+ return peer.metadata.get(key)
66
+ } catch (err: any) {
67
+ if (err.code !== codes.ERR_NOT_FOUND) {
68
+ throw err
69
+ }
70
+ } finally {
71
+ log('getValue release write lock')
72
+ release()
73
+ }
74
+ }
75
+
76
+ async set (peerId: PeerId, metadata: Map<string, Uint8Array>) {
77
+ peerId = PeerId.fromPeerId(peerId)
78
+
79
+ if (!metadata || !(metadata instanceof Map)) {
80
+ log.error('valid metadata must be provided to store data')
81
+ throw errcode(new Error('valid metadata must be provided'), codes.ERR_INVALID_PARAMETERS)
82
+ }
83
+
84
+ log('set await write lock')
85
+ const release = await this.store.lock.writeLock()
86
+ log('set got write lock')
87
+
88
+ try {
89
+ await this.store.mergeOrCreate(peerId, {
90
+ metadata
91
+ })
92
+ } finally {
93
+ log('set release write lock')
94
+ release()
95
+ }
96
+
97
+ this.emit(EVENT_NAME, { peerId, metadata })
98
+ }
99
+
100
+ /**
101
+ * Set metadata key and value of a provided peer
102
+ */
103
+ async setValue (peerId: PeerId, key: string, value: Uint8Array) {
104
+ peerId = PeerId.fromPeerId(peerId)
105
+
106
+ if (typeof key !== 'string' || !(value instanceof Uint8Array)) {
107
+ log.error('valid key and value must be provided to store data')
108
+ throw errcode(new Error('valid key and value must be provided'), codes.ERR_INVALID_PARAMETERS)
109
+ }
110
+
111
+ log('setValue await write lock')
112
+ const release = await this.store.lock.writeLock()
113
+ log('setValue got write lock')
114
+
115
+ let updatedPeer
116
+
117
+ try {
118
+ try {
119
+ const existingPeer = await this.store.load(peerId)
120
+ const existingValue = existingPeer.metadata.get(key)
121
+
122
+ if (existingValue != null && uint8ArrayEquals(value, existingValue)) {
123
+ return
124
+ }
125
+ } catch (err: any) {
126
+ if (err.code !== codes.ERR_NOT_FOUND) {
127
+ throw err
128
+ }
129
+ }
130
+
131
+ updatedPeer = await this.store.mergeOrCreate(peerId, {
132
+ metadata: new Map([[key, value]])
133
+ })
134
+ } finally {
135
+ log('setValue release write lock')
136
+ release()
137
+ }
138
+
139
+ this.emit(EVENT_NAME, { peerId, metadata: updatedPeer.metadata })
140
+ }
141
+
142
+ async delete (peerId: PeerId) {
143
+ peerId = PeerId.fromPeerId(peerId)
144
+
145
+ log('delete await write lock')
146
+ const release = await this.store.lock.writeLock()
147
+ log('delete got write lock')
148
+
149
+ let has
150
+
151
+ try {
152
+ has = await this.store.has(peerId)
153
+
154
+ if (has) {
155
+ await this.store.patch(peerId, {
156
+ metadata: new Map()
157
+ })
158
+ }
159
+ } finally {
160
+ log('delete release write lock')
161
+ release()
162
+ }
163
+
164
+ if (has) {
165
+ this.emit(EVENT_NAME, { peerId, metadata: new Map() })
166
+ }
167
+ }
168
+
169
+ async deleteValue (peerId: PeerId, key: string) {
170
+ peerId = PeerId.fromPeerId(peerId)
171
+
172
+ log('deleteValue await write lock')
173
+ const release = await this.store.lock.writeLock()
174
+ log('deleteValue got write lock')
175
+
176
+ let metadata
177
+
178
+ try {
179
+ const peer = await this.store.load(peerId)
180
+ metadata = peer.metadata
181
+
182
+ metadata.delete(key)
183
+
184
+ await this.store.patch(peerId, {
185
+ metadata
186
+ })
187
+ } catch (err: any) {
188
+ if (err.code !== codes.ERR_NOT_FOUND) {
189
+ throw err
190
+ }
191
+ } finally {
192
+ log('deleteValue release write lock')
193
+ release()
194
+ }
195
+
196
+ if (metadata) {
197
+ this.emit(EVENT_NAME, { peerId, metadata })
198
+ }
199
+ }
200
+ }
@@ -0,0 +1,222 @@
1
+ import * as $protobuf from "protobufjs";
2
+ /** Properties of a Peer. */
3
+ export interface IPeer {
4
+
5
+ /** Peer addresses */
6
+ addresses?: (IAddress[]|null);
7
+
8
+ /** Peer protocols */
9
+ protocols?: (string[]|null);
10
+
11
+ /** Peer metadata */
12
+ metadata?: (IMetadata[]|null);
13
+
14
+ /** Peer pubKey */
15
+ pubKey?: (Uint8Array|null);
16
+
17
+ /** Peer peerRecordEnvelope */
18
+ peerRecordEnvelope?: (Uint8Array|null);
19
+ }
20
+
21
+ /** Represents a Peer. */
22
+ export class Peer implements IPeer {
23
+
24
+ /**
25
+ * Constructs a new Peer.
26
+ * @param [p] Properties to set
27
+ */
28
+ constructor(p?: IPeer);
29
+
30
+ /** Peer addresses. */
31
+ public addresses: IAddress[];
32
+
33
+ /** Peer protocols. */
34
+ public protocols: string[];
35
+
36
+ /** Peer metadata. */
37
+ public metadata: IMetadata[];
38
+
39
+ /** Peer pubKey. */
40
+ public pubKey?: (Uint8Array|null);
41
+
42
+ /** Peer peerRecordEnvelope. */
43
+ public peerRecordEnvelope?: (Uint8Array|null);
44
+
45
+ /** Peer _pubKey. */
46
+ public _pubKey?: "pubKey";
47
+
48
+ /** Peer _peerRecordEnvelope. */
49
+ public _peerRecordEnvelope?: "peerRecordEnvelope";
50
+
51
+ /**
52
+ * Encodes the specified Peer message. Does not implicitly {@link Peer.verify|verify} messages.
53
+ * @param m Peer message or plain object to encode
54
+ * @param [w] Writer to encode to
55
+ * @returns Writer
56
+ */
57
+ public static encode(m: IPeer, w?: $protobuf.Writer): $protobuf.Writer;
58
+
59
+ /**
60
+ * Decodes a Peer message from the specified reader or buffer.
61
+ * @param r Reader or buffer to decode from
62
+ * @param [l] Message length if known beforehand
63
+ * @returns Peer
64
+ * @throws {Error} If the payload is not a reader or valid buffer
65
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
66
+ */
67
+ public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Peer;
68
+
69
+ /**
70
+ * Creates a Peer message from a plain object. Also converts values to their respective internal types.
71
+ * @param d Plain object
72
+ * @returns Peer
73
+ */
74
+ public static fromObject(d: { [k: string]: any }): Peer;
75
+
76
+ /**
77
+ * Creates a plain object from a Peer message. Also converts values to other types if specified.
78
+ * @param m Peer
79
+ * @param [o] Conversion options
80
+ * @returns Plain object
81
+ */
82
+ public static toObject(m: Peer, o?: $protobuf.IConversionOptions): { [k: string]: any };
83
+
84
+ /**
85
+ * Converts this Peer to JSON.
86
+ * @returns JSON object
87
+ */
88
+ public toJSON(): { [k: string]: any };
89
+ }
90
+
91
+ /** Properties of an Address. */
92
+ export interface IAddress {
93
+
94
+ /** Address multiaddr */
95
+ multiaddr?: (Uint8Array|null);
96
+
97
+ /** Address isCertified */
98
+ isCertified?: (boolean|null);
99
+ }
100
+
101
+ /** Represents an Address. */
102
+ export class Address implements IAddress {
103
+
104
+ /**
105
+ * Constructs a new Address.
106
+ * @param [p] Properties to set
107
+ */
108
+ constructor(p?: IAddress);
109
+
110
+ /** Address multiaddr. */
111
+ public multiaddr: Uint8Array;
112
+
113
+ /** Address isCertified. */
114
+ public isCertified?: (boolean|null);
115
+
116
+ /** Address _isCertified. */
117
+ public _isCertified?: "isCertified";
118
+
119
+ /**
120
+ * Encodes the specified Address message. Does not implicitly {@link Address.verify|verify} messages.
121
+ * @param m Address message or plain object to encode
122
+ * @param [w] Writer to encode to
123
+ * @returns Writer
124
+ */
125
+ public static encode(m: IAddress, w?: $protobuf.Writer): $protobuf.Writer;
126
+
127
+ /**
128
+ * Decodes an Address message from the specified reader or buffer.
129
+ * @param r Reader or buffer to decode from
130
+ * @param [l] Message length if known beforehand
131
+ * @returns Address
132
+ * @throws {Error} If the payload is not a reader or valid buffer
133
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
134
+ */
135
+ public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Address;
136
+
137
+ /**
138
+ * Creates an Address message from a plain object. Also converts values to their respective internal types.
139
+ * @param d Plain object
140
+ * @returns Address
141
+ */
142
+ public static fromObject(d: { [k: string]: any }): Address;
143
+
144
+ /**
145
+ * Creates a plain object from an Address message. Also converts values to other types if specified.
146
+ * @param m Address
147
+ * @param [o] Conversion options
148
+ * @returns Plain object
149
+ */
150
+ public static toObject(m: Address, o?: $protobuf.IConversionOptions): { [k: string]: any };
151
+
152
+ /**
153
+ * Converts this Address to JSON.
154
+ * @returns JSON object
155
+ */
156
+ public toJSON(): { [k: string]: any };
157
+ }
158
+
159
+ /** Properties of a Metadata. */
160
+ export interface IMetadata {
161
+
162
+ /** Metadata key */
163
+ key?: (string|null);
164
+
165
+ /** Metadata value */
166
+ value?: (Uint8Array|null);
167
+ }
168
+
169
+ /** Represents a Metadata. */
170
+ export class Metadata implements IMetadata {
171
+
172
+ /**
173
+ * Constructs a new Metadata.
174
+ * @param [p] Properties to set
175
+ */
176
+ constructor(p?: IMetadata);
177
+
178
+ /** Metadata key. */
179
+ public key: string;
180
+
181
+ /** Metadata value. */
182
+ public value: Uint8Array;
183
+
184
+ /**
185
+ * Encodes the specified Metadata message. Does not implicitly {@link Metadata.verify|verify} messages.
186
+ * @param m Metadata message or plain object to encode
187
+ * @param [w] Writer to encode to
188
+ * @returns Writer
189
+ */
190
+ public static encode(m: IMetadata, w?: $protobuf.Writer): $protobuf.Writer;
191
+
192
+ /**
193
+ * Decodes a Metadata message from the specified reader or buffer.
194
+ * @param r Reader or buffer to decode from
195
+ * @param [l] Message length if known beforehand
196
+ * @returns Metadata
197
+ * @throws {Error} If the payload is not a reader or valid buffer
198
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
199
+ */
200
+ public static decode(r: ($protobuf.Reader|Uint8Array), l?: number): Metadata;
201
+
202
+ /**
203
+ * Creates a Metadata message from a plain object. Also converts values to their respective internal types.
204
+ * @param d Plain object
205
+ * @returns Metadata
206
+ */
207
+ public static fromObject(d: { [k: string]: any }): Metadata;
208
+
209
+ /**
210
+ * Creates a plain object from a Metadata message. Also converts values to other types if specified.
211
+ * @param m Metadata
212
+ * @param [o] Conversion options
213
+ * @returns Plain object
214
+ */
215
+ public static toObject(m: Metadata, o?: $protobuf.IConversionOptions): { [k: string]: any };
216
+
217
+ /**
218
+ * Converts this Metadata to JSON.
219
+ * @returns JSON object
220
+ */
221
+ public toJSON(): { [k: string]: any };
222
+ }