@leofcoin/peernet 0.11.29 → 0.12.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.
@@ -1,4 +1,3 @@
1
- import protons from 'protons'
2
1
  import proto from './../proto/peer.proto.js'
3
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
4
3
 
@@ -7,8 +6,12 @@ export default class PeerMessage extends FormatInterface {
7
6
  return ['id']
8
7
  }
9
8
 
9
+ get messageName() {
10
+ return 'PeernetPeerMessage'
11
+ }
12
+
10
13
  constructor(data) {
11
14
  const name = 'peernet-peer'
12
- super(data, protons(proto).PeernetPeerMessage, {name})
15
+ super(data, proto, {name})
13
16
  }
14
17
  }
@@ -1,4 +1,3 @@
1
- import protons from 'protons'
2
1
  import proto from './../proto/peernet.proto.js'
3
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
4
3
 
@@ -7,8 +6,12 @@ export default class PeernetMessage extends FormatInterface {
7
6
  return ['data', 'signature', 'from', 'to', 'id']
8
7
  }
9
8
 
9
+ get messageName() {
10
+ return 'PeernetMessage'
11
+ }
12
+
10
13
  constructor(buffer) {
11
14
  const name = 'peernet-message'
12
- super(buffer, protons(proto).PeernetMessage, {name})
15
+ super(buffer, proto, {name})
13
16
  }
14
17
  }
@@ -1,4 +1,3 @@
1
- import protons from 'protons'
2
1
  import proto from './../proto/ps.proto.js'
3
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
4
3
 
@@ -7,8 +6,12 @@ export default class PsMessage extends FormatInterface {
7
6
  return ['data', 'topic']
8
7
  }
9
8
 
9
+ get messageName() {
10
+ return 'PsMessage'
11
+ }
12
+
10
13
  constructor(buffer) {
11
14
  const name = 'peernet-ps'
12
- super(buffer, protons(proto).PsMessage, {name})
15
+ super(buffer, proto, {name})
13
16
  }
14
17
  }
@@ -1,4 +1,3 @@
1
- import protons from 'protons'
2
1
  import proto from './../proto/request.proto.js'
3
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
4
3
 
@@ -7,8 +6,12 @@ export default class RequestMessage extends FormatInterface {
7
6
  return ['request']
8
7
  }
9
8
 
9
+ get messageName() {
10
+ return 'PeernetRequestMessage'
11
+ }
12
+
10
13
  constructor(data) {
11
14
  const name = 'peernet-request'
12
- super(data, protons(proto).PeernetRequestMessage, {name})
15
+ super(data, proto, {name})
13
16
  }
14
17
  }
@@ -1,4 +1,3 @@
1
- import protons from 'protons'
2
1
  import proto from './../proto/response.proto.js'
3
2
  import { FormatInterface } from '@leofcoin/codec-format-interface'
4
3
 
@@ -7,8 +6,12 @@ export default class ResponseMessage extends FormatInterface {
7
6
  return ['response']
8
7
  }
9
8
 
9
+ get messageName() {
10
+ return 'PeernetResponseMessage'
11
+ }
12
+
10
13
  constructor(data) {
11
14
  const name = 'peernet-response'
12
- super(data, protons(proto).PeernetResponseMessage, {name})
15
+ super(data, proto, {name})
13
16
  }
14
17
  }
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
  }
@@ -190,8 +193,16 @@ export default class Peernet {
190
193
  try {
191
194
  const pub = await accountStore.get('public')
192
195
  this.id = JSON.parse(new TextDecoder().decode(pub)).walletId
193
- const accounts = await walletStore.get('accounts')
194
- this.accounts = JSON.parse(new TextDecoder().decode(accounts))
196
+ let accounts = await walletStore.get('accounts')
197
+ accounts = new TextDecoder().decode(accounts)
198
+
199
+ // fixing account issue (string while needs to be a JSON)
200
+ // TODO: remove when on mainnet
201
+ try {
202
+ this.accounts = JSON.parse(account)
203
+ } catch (e) {
204
+ this.accounts = [accounts.split(',')]
205
+ }
195
206
  } catch (e) {
196
207
  if (e.code === 'ERR_NOT_FOUND') {
197
208
  const wallet = {}
@@ -273,7 +284,7 @@ export default class Peernet {
273
284
  if (store.private) has = false
274
285
  else has = await store.has(hash)
275
286
  }
276
- const data = new DHTMessageResponse({hash, has})
287
+ const data = await new DHTMessageResponse({hash, has})
277
288
  const node = await this.prepareMessage(from, data.encoded)
278
289
 
279
290
  this.sendMessage(peer, id, node.encoded)
@@ -289,7 +300,7 @@ export default class Peernet {
289
300
  data = await store.get(hash)
290
301
 
291
302
  if (data) {
292
- data = new DataMessageResponse({hash, data});
303
+ data = await new DataMessageResponse({hash, data});
293
304
 
294
305
  const node = await this.prepareMessage(from, data.encoded)
295
306
  this.sendMessage(peer, id, node.encoded)
@@ -318,16 +329,16 @@ export default class Peernet {
318
329
  */
319
330
  async walk(hash) {
320
331
  if (!hash) throw new Error('hash expected, received undefined')
321
- const data = new DHTMessage({hash})
332
+ const data = await new DHTMessage({hash})
322
333
  const clientId = this.client.id
323
334
  const walk = async peer => {
324
335
  const node = await this.prepareMessage(peer.peerId, data.encoded)
325
336
  let result = await peer.request(node.encoded)
326
337
  result = new Uint8Array(Object.values(result))
327
- let proto = protoFor(result)
338
+ let proto = await protoFor(result)
328
339
  if (proto.name !== 'peernet-message') throw encapsulatedError()
329
340
  const from = proto.decoded.from
330
- proto = protoFor(proto.decoded.data)
341
+ proto = await protoFor(proto.decoded.data)
331
342
  if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
332
343
 
333
344
  // TODO: give ip and port (just used for location)
@@ -426,7 +437,7 @@ export default class Peernet {
426
437
  if (peer.peerId === id) return peer
427
438
  })
428
439
 
429
- let data = new DataMessage({hash, store: store?.name ? store?.name : store});
440
+ let data = await new DataMessage({hash, store: store?.name ? store?.name : store});
430
441
 
431
442
  const node = await this.prepareMessage(id, data.encoded)
432
443
  if (closest[0]) data = await closest[0].request(node.encoded)
@@ -437,8 +448,8 @@ export default class Peernet {
437
448
  if (closest[0]) data = await closest[0].request(node.encoded)
438
449
  }
439
450
  data = new Uint8Array(Object.values(data))
440
- let proto = protoFor(data)
441
- proto = protoFor(proto.decoded.data)
451
+ let proto = await protoFor(data)
452
+ proto = await protoFor(proto.decoded.data)
442
453
  // TODO: store data automaticly or not
443
454
  return proto.decoded.data
444
455
 
@@ -572,7 +583,7 @@ export default class Peernet {
572
583
  if (topic instanceof Uint8Array === false) topic = new TextEncoder().encode(topic)
573
584
  if (data instanceof Uint8Array === false) data = new TextEncoder().encode(JSON.stringify(data))
574
585
  const id = Math.random().toString(36).slice(-12)
575
- data = new PsMessage({data, topic})
586
+ data = await new PsMessage({data, topic})
576
587
  for (const peer of this.connections) {
577
588
  if (peer.peerId !== this.peerId) {
578
589
  const node = await this.prepareMessage(peer.peerId, data.encoded)