libp2p 0.35.8 → 0.36.0-d44bd90

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 (108) hide show
  1. package/dist/index.min.js +55 -0
  2. package/dist/src/circuit/auto-relay.d.ts +8 -10
  3. package/dist/src/circuit/auto-relay.d.ts.map +1 -1
  4. package/dist/src/config.d.ts +2 -0
  5. package/dist/src/config.d.ts.map +1 -1
  6. package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
  7. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  8. package/dist/src/connection-manager/index.d.ts +1 -2
  9. package/dist/src/connection-manager/index.d.ts.map +1 -1
  10. package/dist/src/content-routing/utils.d.ts +3 -3
  11. package/dist/src/content-routing/utils.d.ts.map +1 -1
  12. package/dist/src/dialer/dial-request.d.ts.map +1 -1
  13. package/dist/src/dialer/index.d.ts +12 -7
  14. package/dist/src/dialer/index.d.ts.map +1 -1
  15. package/dist/src/errors.d.ts +2 -0
  16. package/dist/src/fetch/constants.d.ts +2 -0
  17. package/dist/src/fetch/constants.d.ts.map +1 -0
  18. package/dist/src/fetch/index.d.ts +76 -0
  19. package/dist/src/fetch/index.d.ts.map +1 -0
  20. package/dist/src/fetch/proto.d.ts +134 -0
  21. package/dist/src/identify/index.d.ts +4 -4
  22. package/dist/src/identify/index.d.ts.map +1 -1
  23. package/dist/src/index.d.ts +26 -7
  24. package/dist/src/index.d.ts.map +1 -1
  25. package/dist/src/metrics/index.d.ts +9 -4
  26. package/dist/src/metrics/index.d.ts.map +1 -1
  27. package/dist/src/metrics/tracked-map.d.ts +6 -1
  28. package/dist/src/metrics/tracked-map.d.ts.map +1 -1
  29. package/dist/src/peer-routing.d.ts +1 -1
  30. package/dist/src/peer-routing.d.ts.map +1 -1
  31. package/dist/src/peer-store/address-book.d.ts +30 -84
  32. package/dist/src/peer-store/address-book.d.ts.map +1 -1
  33. package/dist/src/peer-store/index.d.ts +28 -94
  34. package/dist/src/peer-store/index.d.ts.map +1 -1
  35. package/dist/src/peer-store/key-book.d.ts +31 -15
  36. package/dist/src/peer-store/key-book.d.ts.map +1 -1
  37. package/dist/src/peer-store/metadata-book.d.ts +33 -29
  38. package/dist/src/peer-store/metadata-book.d.ts.map +1 -1
  39. package/dist/src/peer-store/pb/peer.d.ts +222 -0
  40. package/dist/src/peer-store/proto-book.d.ts +27 -28
  41. package/dist/src/peer-store/proto-book.d.ts.map +1 -1
  42. package/dist/src/peer-store/store.d.ts +73 -0
  43. package/dist/src/peer-store/store.d.ts.map +1 -0
  44. package/dist/src/peer-store/types.d.ts +202 -0
  45. package/dist/src/peer-store/types.d.ts.map +1 -0
  46. package/dist/src/ping/index.d.ts +32 -25
  47. package/dist/src/ping/index.d.ts.map +1 -1
  48. package/dist/src/record/peer-record/index.d.ts +2 -2
  49. package/dist/src/record/peer-record/index.d.ts.map +1 -1
  50. package/dist/src/registrar.d.ts +5 -5
  51. package/dist/src/registrar.d.ts.map +1 -1
  52. package/dist/src/types.d.ts +90 -0
  53. package/dist/src/types.d.ts.map +1 -0
  54. package/dist/src/upgrader.d.ts +8 -2
  55. package/dist/src/upgrader.d.ts.map +1 -1
  56. package/package.json +23 -26
  57. package/src/circuit/auto-relay.js +52 -38
  58. package/src/config.js +2 -0
  59. package/src/connection-manager/auto-dialler.js +20 -8
  60. package/src/connection-manager/index.js +42 -22
  61. package/src/content-routing/utils.js +4 -4
  62. package/src/dialer/dial-request.js +4 -1
  63. package/src/dialer/index.js +38 -8
  64. package/src/errors.js +2 -0
  65. package/src/fetch/README.md +36 -0
  66. package/src/fetch/constants.js +6 -0
  67. package/src/fetch/index.js +159 -0
  68. package/src/fetch/proto.d.ts +134 -0
  69. package/src/fetch/proto.js +333 -0
  70. package/src/fetch/proto.proto +15 -0
  71. package/src/identify/index.js +47 -38
  72. package/src/index.js +81 -34
  73. package/src/metrics/index.js +15 -8
  74. package/src/metrics/tracked-map.js +40 -14
  75. package/src/peer-routing.js +4 -1
  76. package/src/peer-store/address-book.js +254 -226
  77. package/src/peer-store/index.js +79 -110
  78. package/src/peer-store/key-book.js +99 -54
  79. package/src/peer-store/metadata-book.js +171 -101
  80. package/src/peer-store/pb/peer.d.ts +222 -0
  81. package/src/peer-store/pb/peer.js +643 -0
  82. package/src/peer-store/pb/peer.proto +31 -0
  83. package/src/peer-store/proto-book.js +171 -105
  84. package/src/peer-store/store.js +263 -0
  85. package/src/peer-store/types.ts +245 -0
  86. package/src/ping/index.js +51 -46
  87. package/src/record/peer-record/index.js +1 -1
  88. package/src/record/utils.js +1 -1
  89. package/src/registrar.js +4 -4
  90. package/src/types.ts +98 -0
  91. package/src/upgrader.js +32 -2
  92. package/dist/src/peer-store/book.d.ts +0 -81
  93. package/dist/src/peer-store/book.d.ts.map +0 -1
  94. package/dist/src/peer-store/persistent/consts.d.ts +0 -6
  95. package/dist/src/peer-store/persistent/consts.d.ts.map +0 -1
  96. package/dist/src/peer-store/persistent/index.d.ts +0 -129
  97. package/dist/src/peer-store/persistent/index.d.ts.map +0 -1
  98. package/dist/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  99. package/dist/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  100. package/src/peer-store/book.js +0 -124
  101. package/src/peer-store/persistent/consts.js +0 -15
  102. package/src/peer-store/persistent/index.js +0 -408
  103. package/src/peer-store/persistent/pb/address-book.d.ts +0 -198
  104. package/src/peer-store/persistent/pb/address-book.js +0 -522
  105. package/src/peer-store/persistent/pb/address-book.proto +0 -27
  106. package/src/peer-store/persistent/pb/proto-book.d.ts +0 -59
  107. package/src/peer-store/persistent/pb/proto-book.js +0 -157
  108. package/src/peer-store/persistent/pb/proto-book.proto +0 -5
@@ -0,0 +1,333 @@
1
+ /*eslint-disable*/
2
+ "use strict";
3
+
4
+ var $protobuf = require("protobufjs/minimal");
5
+
6
+ // Common aliases
7
+ var $Reader = $protobuf.Reader, $Writer = $protobuf.Writer, $util = $protobuf.util;
8
+
9
+ // Exported root namespace
10
+ var $root = $protobuf.roots["libp2p-fetch"] || ($protobuf.roots["libp2p-fetch"] = {});
11
+
12
+ $root.FetchRequest = (function() {
13
+
14
+ /**
15
+ * Properties of a FetchRequest.
16
+ * @exports IFetchRequest
17
+ * @interface IFetchRequest
18
+ * @property {string|null} [identifier] FetchRequest identifier
19
+ */
20
+
21
+ /**
22
+ * Constructs a new FetchRequest.
23
+ * @exports FetchRequest
24
+ * @classdesc Represents a FetchRequest.
25
+ * @implements IFetchRequest
26
+ * @constructor
27
+ * @param {IFetchRequest=} [p] Properties to set
28
+ */
29
+ function FetchRequest(p) {
30
+ if (p)
31
+ for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
32
+ if (p[ks[i]] != null)
33
+ this[ks[i]] = p[ks[i]];
34
+ }
35
+
36
+ /**
37
+ * FetchRequest identifier.
38
+ * @member {string} identifier
39
+ * @memberof FetchRequest
40
+ * @instance
41
+ */
42
+ FetchRequest.prototype.identifier = "";
43
+
44
+ /**
45
+ * Encodes the specified FetchRequest message. Does not implicitly {@link FetchRequest.verify|verify} messages.
46
+ * @function encode
47
+ * @memberof FetchRequest
48
+ * @static
49
+ * @param {IFetchRequest} m FetchRequest message or plain object to encode
50
+ * @param {$protobuf.Writer} [w] Writer to encode to
51
+ * @returns {$protobuf.Writer} Writer
52
+ */
53
+ FetchRequest.encode = function encode(m, w) {
54
+ if (!w)
55
+ w = $Writer.create();
56
+ if (m.identifier != null && Object.hasOwnProperty.call(m, "identifier"))
57
+ w.uint32(10).string(m.identifier);
58
+ return w;
59
+ };
60
+
61
+ /**
62
+ * Decodes a FetchRequest message from the specified reader or buffer.
63
+ * @function decode
64
+ * @memberof FetchRequest
65
+ * @static
66
+ * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
67
+ * @param {number} [l] Message length if known beforehand
68
+ * @returns {FetchRequest} FetchRequest
69
+ * @throws {Error} If the payload is not a reader or valid buffer
70
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
71
+ */
72
+ FetchRequest.decode = function decode(r, l) {
73
+ if (!(r instanceof $Reader))
74
+ r = $Reader.create(r);
75
+ var c = l === undefined ? r.len : r.pos + l, m = new $root.FetchRequest();
76
+ while (r.pos < c) {
77
+ var t = r.uint32();
78
+ switch (t >>> 3) {
79
+ case 1:
80
+ m.identifier = r.string();
81
+ break;
82
+ default:
83
+ r.skipType(t & 7);
84
+ break;
85
+ }
86
+ }
87
+ return m;
88
+ };
89
+
90
+ /**
91
+ * Creates a FetchRequest message from a plain object. Also converts values to their respective internal types.
92
+ * @function fromObject
93
+ * @memberof FetchRequest
94
+ * @static
95
+ * @param {Object.<string,*>} d Plain object
96
+ * @returns {FetchRequest} FetchRequest
97
+ */
98
+ FetchRequest.fromObject = function fromObject(d) {
99
+ if (d instanceof $root.FetchRequest)
100
+ return d;
101
+ var m = new $root.FetchRequest();
102
+ if (d.identifier != null) {
103
+ m.identifier = String(d.identifier);
104
+ }
105
+ return m;
106
+ };
107
+
108
+ /**
109
+ * Creates a plain object from a FetchRequest message. Also converts values to other types if specified.
110
+ * @function toObject
111
+ * @memberof FetchRequest
112
+ * @static
113
+ * @param {FetchRequest} m FetchRequest
114
+ * @param {$protobuf.IConversionOptions} [o] Conversion options
115
+ * @returns {Object.<string,*>} Plain object
116
+ */
117
+ FetchRequest.toObject = function toObject(m, o) {
118
+ if (!o)
119
+ o = {};
120
+ var d = {};
121
+ if (o.defaults) {
122
+ d.identifier = "";
123
+ }
124
+ if (m.identifier != null && m.hasOwnProperty("identifier")) {
125
+ d.identifier = m.identifier;
126
+ }
127
+ return d;
128
+ };
129
+
130
+ /**
131
+ * Converts this FetchRequest to JSON.
132
+ * @function toJSON
133
+ * @memberof FetchRequest
134
+ * @instance
135
+ * @returns {Object.<string,*>} JSON object
136
+ */
137
+ FetchRequest.prototype.toJSON = function toJSON() {
138
+ return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
139
+ };
140
+
141
+ return FetchRequest;
142
+ })();
143
+
144
+ $root.FetchResponse = (function() {
145
+
146
+ /**
147
+ * Properties of a FetchResponse.
148
+ * @exports IFetchResponse
149
+ * @interface IFetchResponse
150
+ * @property {FetchResponse.StatusCode|null} [status] FetchResponse status
151
+ * @property {Uint8Array|null} [data] FetchResponse data
152
+ */
153
+
154
+ /**
155
+ * Constructs a new FetchResponse.
156
+ * @exports FetchResponse
157
+ * @classdesc Represents a FetchResponse.
158
+ * @implements IFetchResponse
159
+ * @constructor
160
+ * @param {IFetchResponse=} [p] Properties to set
161
+ */
162
+ function FetchResponse(p) {
163
+ if (p)
164
+ for (var ks = Object.keys(p), i = 0; i < ks.length; ++i)
165
+ if (p[ks[i]] != null)
166
+ this[ks[i]] = p[ks[i]];
167
+ }
168
+
169
+ /**
170
+ * FetchResponse status.
171
+ * @member {FetchResponse.StatusCode} status
172
+ * @memberof FetchResponse
173
+ * @instance
174
+ */
175
+ FetchResponse.prototype.status = 0;
176
+
177
+ /**
178
+ * FetchResponse data.
179
+ * @member {Uint8Array} data
180
+ * @memberof FetchResponse
181
+ * @instance
182
+ */
183
+ FetchResponse.prototype.data = $util.newBuffer([]);
184
+
185
+ /**
186
+ * Encodes the specified FetchResponse message. Does not implicitly {@link FetchResponse.verify|verify} messages.
187
+ * @function encode
188
+ * @memberof FetchResponse
189
+ * @static
190
+ * @param {IFetchResponse} m FetchResponse message or plain object to encode
191
+ * @param {$protobuf.Writer} [w] Writer to encode to
192
+ * @returns {$protobuf.Writer} Writer
193
+ */
194
+ FetchResponse.encode = function encode(m, w) {
195
+ if (!w)
196
+ w = $Writer.create();
197
+ if (m.status != null && Object.hasOwnProperty.call(m, "status"))
198
+ w.uint32(8).int32(m.status);
199
+ if (m.data != null && Object.hasOwnProperty.call(m, "data"))
200
+ w.uint32(18).bytes(m.data);
201
+ return w;
202
+ };
203
+
204
+ /**
205
+ * Decodes a FetchResponse message from the specified reader or buffer.
206
+ * @function decode
207
+ * @memberof FetchResponse
208
+ * @static
209
+ * @param {$protobuf.Reader|Uint8Array} r Reader or buffer to decode from
210
+ * @param {number} [l] Message length if known beforehand
211
+ * @returns {FetchResponse} FetchResponse
212
+ * @throws {Error} If the payload is not a reader or valid buffer
213
+ * @throws {$protobuf.util.ProtocolError} If required fields are missing
214
+ */
215
+ FetchResponse.decode = function decode(r, l) {
216
+ if (!(r instanceof $Reader))
217
+ r = $Reader.create(r);
218
+ var c = l === undefined ? r.len : r.pos + l, m = new $root.FetchResponse();
219
+ while (r.pos < c) {
220
+ var t = r.uint32();
221
+ switch (t >>> 3) {
222
+ case 1:
223
+ m.status = r.int32();
224
+ break;
225
+ case 2:
226
+ m.data = r.bytes();
227
+ break;
228
+ default:
229
+ r.skipType(t & 7);
230
+ break;
231
+ }
232
+ }
233
+ return m;
234
+ };
235
+
236
+ /**
237
+ * Creates a FetchResponse message from a plain object. Also converts values to their respective internal types.
238
+ * @function fromObject
239
+ * @memberof FetchResponse
240
+ * @static
241
+ * @param {Object.<string,*>} d Plain object
242
+ * @returns {FetchResponse} FetchResponse
243
+ */
244
+ FetchResponse.fromObject = function fromObject(d) {
245
+ if (d instanceof $root.FetchResponse)
246
+ return d;
247
+ var m = new $root.FetchResponse();
248
+ switch (d.status) {
249
+ case "OK":
250
+ case 0:
251
+ m.status = 0;
252
+ break;
253
+ case "NOT_FOUND":
254
+ case 1:
255
+ m.status = 1;
256
+ break;
257
+ case "ERROR":
258
+ case 2:
259
+ m.status = 2;
260
+ break;
261
+ }
262
+ if (d.data != null) {
263
+ if (typeof d.data === "string")
264
+ $util.base64.decode(d.data, m.data = $util.newBuffer($util.base64.length(d.data)), 0);
265
+ else if (d.data.length)
266
+ m.data = d.data;
267
+ }
268
+ return m;
269
+ };
270
+
271
+ /**
272
+ * Creates a plain object from a FetchResponse message. Also converts values to other types if specified.
273
+ * @function toObject
274
+ * @memberof FetchResponse
275
+ * @static
276
+ * @param {FetchResponse} m FetchResponse
277
+ * @param {$protobuf.IConversionOptions} [o] Conversion options
278
+ * @returns {Object.<string,*>} Plain object
279
+ */
280
+ FetchResponse.toObject = function toObject(m, o) {
281
+ if (!o)
282
+ o = {};
283
+ var d = {};
284
+ if (o.defaults) {
285
+ d.status = o.enums === String ? "OK" : 0;
286
+ if (o.bytes === String)
287
+ d.data = "";
288
+ else {
289
+ d.data = [];
290
+ if (o.bytes !== Array)
291
+ d.data = $util.newBuffer(d.data);
292
+ }
293
+ }
294
+ if (m.status != null && m.hasOwnProperty("status")) {
295
+ d.status = o.enums === String ? $root.FetchResponse.StatusCode[m.status] : m.status;
296
+ }
297
+ if (m.data != null && m.hasOwnProperty("data")) {
298
+ d.data = o.bytes === String ? $util.base64.encode(m.data, 0, m.data.length) : o.bytes === Array ? Array.prototype.slice.call(m.data) : m.data;
299
+ }
300
+ return d;
301
+ };
302
+
303
+ /**
304
+ * Converts this FetchResponse to JSON.
305
+ * @function toJSON
306
+ * @memberof FetchResponse
307
+ * @instance
308
+ * @returns {Object.<string,*>} JSON object
309
+ */
310
+ FetchResponse.prototype.toJSON = function toJSON() {
311
+ return this.constructor.toObject(this, $protobuf.util.toJSONOptions);
312
+ };
313
+
314
+ /**
315
+ * StatusCode enum.
316
+ * @name FetchResponse.StatusCode
317
+ * @enum {number}
318
+ * @property {number} OK=0 OK value
319
+ * @property {number} NOT_FOUND=1 NOT_FOUND value
320
+ * @property {number} ERROR=2 ERROR value
321
+ */
322
+ FetchResponse.StatusCode = (function() {
323
+ var valuesById = {}, values = Object.create(valuesById);
324
+ values[valuesById[0] = "OK"] = 0;
325
+ values[valuesById[1] = "NOT_FOUND"] = 1;
326
+ values[valuesById[2] = "ERROR"] = 2;
327
+ return values;
328
+ })();
329
+
330
+ return FetchResponse;
331
+ })();
332
+
333
+ module.exports = $root;
@@ -0,0 +1,15 @@
1
+ syntax = "proto3";
2
+
3
+ message FetchRequest {
4
+ string identifier = 1;
5
+ }
6
+
7
+ message FetchResponse {
8
+ StatusCode status = 1;
9
+ enum StatusCode {
10
+ OK = 0;
11
+ NOT_FOUND = 1;
12
+ ERROR = 2;
13
+ }
14
+ bytes data = 2;
15
+ }
@@ -77,8 +77,6 @@ class IdentifyService {
77
77
  ...libp2p._options.host
78
78
  }
79
79
 
80
- this.peerStore.metadataBook.set(this.peerId, 'AgentVersion', uint8ArrayFromString(this._host.agentVersion))
81
- this.peerStore.metadataBook.set(this.peerId, 'ProtocolVersion', uint8ArrayFromString(this._host.protocolVersion))
82
80
  // When a new connection happens, trigger identify
83
81
  this.connectionManager.on('peer:connect', (connection) => {
84
82
  this.identify(connection).catch(log.error)
@@ -87,18 +85,27 @@ class IdentifyService {
87
85
  // When self multiaddrs change, trigger identify-push
88
86
  this.peerStore.on('change:multiaddrs', ({ peerId }) => {
89
87
  if (peerId.toString() === this.peerId.toString()) {
90
- this.pushToPeerStore()
88
+ this.pushToPeerStore().catch(err => log.error(err))
91
89
  }
92
90
  })
93
91
 
94
92
  // When self protocols change, trigger identify-push
95
93
  this.peerStore.on('change:protocols', ({ peerId }) => {
96
94
  if (peerId.toString() === this.peerId.toString()) {
97
- this.pushToPeerStore()
95
+ this.pushToPeerStore().catch(err => log.error(err))
98
96
  }
99
97
  })
100
98
  }
101
99
 
100
+ async start () {
101
+ await this.peerStore.metadataBook.setValue(this.peerId, 'AgentVersion', uint8ArrayFromString(this._host.agentVersion))
102
+ await this.peerStore.metadataBook.setValue(this.peerId, 'ProtocolVersion', uint8ArrayFromString(this._host.protocolVersion))
103
+ }
104
+
105
+ async stop () {
106
+
107
+ }
108
+
102
109
  /**
103
110
  * Send an Identify Push update to the list of connections
104
111
  *
@@ -108,7 +115,7 @@ class IdentifyService {
108
115
  async push (connections) {
109
116
  const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
110
117
  const listenAddrs = this._libp2p.multiaddrs.map((ma) => ma.bytes)
111
- const protocols = this.peerStore.protoBook.get(this.peerId) || []
118
+ const protocols = await this.peerStore.protoBook.get(this.peerId)
112
119
 
113
120
  const pushes = connections.map(async connection => {
114
121
  try {
@@ -135,10 +142,8 @@ class IdentifyService {
135
142
 
136
143
  /**
137
144
  * Calls `push` for all peers in the `peerStore` that are connected
138
- *
139
- * @returns {void}
140
145
  */
141
- pushToPeerStore () {
146
+ async pushToPeerStore () {
142
147
  // Do not try to push if libp2p node is not running
143
148
  if (!this._libp2p.isStarted()) {
144
149
  return
@@ -146,13 +151,13 @@ class IdentifyService {
146
151
 
147
152
  const connections = []
148
153
  let connection
149
- for (const peer of this.peerStore.peers.values()) {
154
+ for await (const peer of this.peerStore.getPeers()) {
150
155
  if (peer.protocols.includes(this.identifyPushProtocolStr) && (connection = this.connectionManager.get(peer.id))) {
151
156
  connections.push(connection)
152
157
  }
153
158
  }
154
159
 
155
- this.push(connections)
160
+ await this.push(connections)
156
161
  }
157
162
 
158
163
  /**
@@ -205,10 +210,10 @@ class IdentifyService {
205
210
 
206
211
  try {
207
212
  const envelope = await Envelope.openAndCertify(signedPeerRecord, PeerRecord.DOMAIN)
208
- if (this.peerStore.addressBook.consumePeerRecord(envelope)) {
209
- this.peerStore.protoBook.set(id, protocols)
210
- this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
211
- this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
213
+ if (await this.peerStore.addressBook.consumePeerRecord(envelope)) {
214
+ await this.peerStore.protoBook.set(id, protocols)
215
+ await this.peerStore.metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
216
+ await this.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
212
217
  return
213
218
  }
214
219
  } catch (/** @type {any} */ err) {
@@ -217,14 +222,14 @@ class IdentifyService {
217
222
 
218
223
  // LEGACY: Update peers data in PeerStore
219
224
  try {
220
- this.peerStore.addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
225
+ await this.peerStore.addressBook.set(id, listenAddrs.map((addr) => new Multiaddr(addr)))
221
226
  } catch (/** @type {any} */ err) {
222
227
  log.error('received invalid addrs', err)
223
228
  }
224
229
 
225
- this.peerStore.protoBook.set(id, protocols)
226
- this.peerStore.metadataBook.set(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
227
- this.peerStore.metadataBook.set(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
230
+ await this.peerStore.protoBook.set(id, protocols)
231
+ await this.peerStore.metadataBook.setValue(id, 'AgentVersion', uint8ArrayFromString(message.agentVersion))
232
+ await this.peerStore.metadataBook.setValue(id, 'ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
228
233
 
229
234
  // TODO: Add and score our observed addr
230
235
  log('received observed address of %s', cleanObservedAddr)
@@ -262,25 +267,25 @@ class IdentifyService {
262
267
  * @returns {Promise<void>}
263
268
  */
264
269
  async _handleIdentify ({ connection, stream }) {
265
- let publicKey = new Uint8Array(0)
266
- if (this.peerId.pubKey) {
267
- publicKey = this.peerId.pubKey.bytes
268
- }
270
+ try {
271
+ let publicKey = new Uint8Array(0)
272
+ if (this.peerId.pubKey) {
273
+ publicKey = this.peerId.pubKey.bytes
274
+ }
269
275
 
270
- const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
271
- const protocols = this.peerStore.protoBook.get(this.peerId) || []
276
+ const signedPeerRecord = await this.peerStore.addressBook.getRawEnvelope(this.peerId)
277
+ const protocols = await this.peerStore.protoBook.get(this.peerId)
272
278
 
273
- const message = Message.Identify.encode({
274
- protocolVersion: this._host.protocolVersion,
275
- agentVersion: this._host.agentVersion,
276
- publicKey,
277
- listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
278
- signedPeerRecord,
279
- observedAddr: connection.remoteAddr.bytes,
280
- protocols
281
- }).finish()
279
+ const message = Message.Identify.encode({
280
+ protocolVersion: this._host.protocolVersion,
281
+ agentVersion: this._host.agentVersion,
282
+ publicKey,
283
+ listenAddrs: this._libp2p.multiaddrs.map((ma) => ma.bytes),
284
+ signedPeerRecord,
285
+ observedAddr: connection.remoteAddr.bytes,
286
+ protocols
287
+ }).finish()
282
288
 
283
- try {
284
289
  await pipe(
285
290
  [message],
286
291
  lp.encode(),
@@ -321,8 +326,8 @@ class IdentifyService {
321
326
 
322
327
  try {
323
328
  const envelope = await Envelope.openAndCertify(message.signedPeerRecord, PeerRecord.DOMAIN)
324
- if (this.peerStore.addressBook.consumePeerRecord(envelope)) {
325
- this.peerStore.protoBook.set(id, message.protocols)
329
+ if (await this.peerStore.addressBook.consumePeerRecord(envelope)) {
330
+ await this.peerStore.protoBook.set(id, message.protocols)
326
331
  return
327
332
  }
328
333
  } catch (/** @type {any} */ err) {
@@ -331,14 +336,18 @@ class IdentifyService {
331
336
 
332
337
  // LEGACY: Update peers data in PeerStore
333
338
  try {
334
- this.peerStore.addressBook.set(id,
339
+ await this.peerStore.addressBook.set(id,
335
340
  message.listenAddrs.map((addr) => new Multiaddr(addr)))
336
341
  } catch (/** @type {any} */ err) {
337
342
  log.error('received invalid addrs', err)
338
343
  }
339
344
 
340
345
  // Update the protocols
341
- this.peerStore.protoBook.set(id, message.protocols)
346
+ try {
347
+ await this.peerStore.protoBook.set(id, message.protocols)
348
+ } catch (/** @type {any} */ err) {
349
+ log.error('received invalid protocols', err)
350
+ }
342
351
  }
343
352
 
344
353
  /**