@leofcoin/peernet 0.11.30 → 0.12.1

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.
package/src/peernet.js CHANGED
@@ -69,6 +69,14 @@ export default class Peernet {
69
69
  return ['account', 'wallet', 'block', 'transaction', 'chain', 'data', 'message']
70
70
  }
71
71
 
72
+ get protos() {
73
+ return globalThis.peernet.protos
74
+ }
75
+
76
+ get codecs() {
77
+ return codecs
78
+ }
79
+
72
80
  addProto(name, proto) {
73
81
  if (!this.protos[name]) this.protos[name] = proto
74
82
  }
@@ -173,16 +181,11 @@ export default class Peernet {
173
181
  'chat-message': ChatMessage,
174
182
  }
175
183
 
176
- this.protos = globalThis.peernet.protos
177
- this.codecs = codecs
178
-
179
184
  this._messageHandler = new MessageHandler(this.network)
180
185
 
181
186
  const {daemon, environment} = await target()
182
187
  this.hasDaemon = daemon
183
188
 
184
- HTTP_IMPORT
185
-
186
189
  for (const store of this.defaultStores) {
187
190
  await this.addStore(store, options.storePrefix, options.root)
188
191
  }
@@ -196,12 +199,10 @@ export default class Peernet {
196
199
  // fixing account issue (string while needs to be a JSON)
197
200
  // TODO: remove when on mainnet
198
201
  try {
199
- accounts = JSON.parse(new TextDecoder().decode(accounts))
202
+ this.accounts = JSON.parse(account)
200
203
  } catch (e) {
201
- accounts = [accounts.split(',')]
204
+ this.accounts = [accounts.split(',')]
202
205
  }
203
-
204
- this.accounts = JSON.parse(new TextDecoder().decode(accounts))
205
206
  } catch (e) {
206
207
  if (e.code === 'ERR_NOT_FOUND') {
207
208
  const wallet = {}
@@ -283,7 +284,7 @@ export default class Peernet {
283
284
  if (store.private) has = false
284
285
  else has = await store.has(hash)
285
286
  }
286
- const data = new DHTMessageResponse({hash, has})
287
+ const data = await new DHTMessageResponse({hash, has})
287
288
  const node = await this.prepareMessage(from, data.encoded)
288
289
 
289
290
  this.sendMessage(peer, id, node.encoded)
@@ -299,7 +300,7 @@ export default class Peernet {
299
300
  data = await store.get(hash)
300
301
 
301
302
  if (data) {
302
- data = new DataMessageResponse({hash, data});
303
+ data = await new DataMessageResponse({hash, data});
303
304
 
304
305
  const node = await this.prepareMessage(from, data.encoded)
305
306
  this.sendMessage(peer, id, node.encoded)
@@ -328,16 +329,16 @@ export default class Peernet {
328
329
  */
329
330
  async walk(hash) {
330
331
  if (!hash) throw new Error('hash expected, received undefined')
331
- const data = new DHTMessage({hash})
332
+ const data = await new DHTMessage({hash})
332
333
  const clientId = this.client.id
333
334
  const walk = async peer => {
334
335
  const node = await this.prepareMessage(peer.peerId, data.encoded)
335
336
  let result = await peer.request(node.encoded)
336
337
  result = new Uint8Array(Object.values(result))
337
- let proto = protoFor(result)
338
+ let proto = await protoFor(result)
338
339
  if (proto.name !== 'peernet-message') throw encapsulatedError()
339
340
  const from = proto.decoded.from
340
- proto = protoFor(proto.decoded.data)
341
+ proto = await protoFor(proto.decoded.data)
341
342
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
342
343
 
343
344
  // TODO: give ip and port (just used for location)
@@ -436,7 +437,7 @@ export default class Peernet {
436
437
  if (peer.peerId === id) return peer
437
438
  })
438
439
 
439
- let data = new DataMessage({hash, store: store?.name ? store?.name : store});
440
+ let data = await new DataMessage({hash, store: store?.name ? store?.name : store});
440
441
 
441
442
  const node = await this.prepareMessage(id, data.encoded)
442
443
  if (closest[0]) data = await closest[0].request(node.encoded)
@@ -447,8 +448,8 @@ export default class Peernet {
447
448
  if (closest[0]) data = await closest[0].request(node.encoded)
448
449
  }
449
450
  data = new Uint8Array(Object.values(data))
450
- let proto = protoFor(data)
451
- proto = protoFor(proto.decoded.data)
451
+ let proto = await protoFor(data)
452
+ proto = await protoFor(proto.decoded.data)
452
453
  // TODO: store data automaticly or not
453
454
  return proto.decoded.data
454
455
 
@@ -582,7 +583,7 @@ export default class Peernet {
582
583
  if (topic instanceof Uint8Array === false) topic = new TextEncoder().encode(topic)
583
584
  if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data))
584
585
  const id = Math.random().toString(36).slice(-12)
585
- data = new PsMessage({data, topic})
586
+ data = await new PsMessage({data, topic})
586
587
  for (const peer of this.connections) {
587
588
  if (peer.peerId !== this.peerId) {
588
589
  const node = await this.prepareMessage(peer.peerId, data.encoded)