@leofcoin/peernet 0.11.5 → 0.11.8

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.
@@ -92,6 +92,28 @@ export default class FormatInterface {
92
92
  else this.decode()
93
93
  }
94
94
 
95
+ toString() {
96
+ return this.encoded.toString()
97
+ }
98
+
99
+ async toArray() {
100
+ const array = []
101
+ for await (const value of this.encoded.values()) {
102
+ array.push(value)
103
+ }
104
+ return array
105
+ }
106
+
107
+ fromString(string) {
108
+ this.encoded = new Uint8Array(string.split(','))
109
+ this.decode()
110
+ }
111
+
112
+ fromArray(array) {
113
+ this.encoded = new Uint8Array([...array])
114
+ this.decode()
115
+ }
116
+
95
117
  /**
96
118
  * @param {Buffer} encoded
97
119
  */
@@ -2,7 +2,7 @@ export default {
2
2
  // just a hash
3
3
  'disco-hash': {
4
4
  codec: parseInt('30', 16),
5
- hashAlg: 'dbl-keccak-512', // ,
5
+ hashAlg: 'dbl-keccak-256', // ,
6
6
  // testnet: 'olivia'
7
7
  },
8
8
  'peernet-peer-response': {
@@ -33,7 +33,7 @@ export default {
33
33
  // message
34
34
  'peernet-message': {
35
35
  codec: parseInt('706d65', 16),
36
- hashAlg: 'keccak-512',
36
+ hashAlg: 'keccak-256',
37
37
  },
38
38
  // pubsub
39
39
  'peernet-ps': {
@@ -74,6 +74,6 @@ export default {
74
74
  // chat message
75
75
  'chat-message': {
76
76
  codec: parseInt('636d', 16),
77
- hashAlg: 'dbl-keccak-512',
77
+ hashAlg: 'dbl-keccak-256',
78
78
  },
79
79
  }
@@ -22,7 +22,7 @@ export default class MessageHandler {
22
22
  identity = JSON.parse(new TextDecoder().decode(identity))
23
23
  const wallet = new MultiWallet(this.network)
24
24
  wallet.import(identity.multiWIF)
25
- return wallet.sign(hasher.hash.slice(0, 32))
25
+ return wallet.sign(Buffer.from(hasher.hash).slice(0, 32))
26
26
  }
27
27
 
28
28
  /**
package/src/hash/hash.js CHANGED
@@ -11,7 +11,7 @@ export default class PeernetHash {
11
11
  else this.name = 'disco-hash'
12
12
  if (options.codecs) this.codecs = options.codecs
13
13
  if (buffer) {
14
- if (Buffer.isBuffer(buffer)) {
14
+ if (buffer instanceof Uint8Array) {
15
15
  this.discoCodec = new Codec(buffer, this.codecs)
16
16
  const name = this.discoCodec.name
17
17
 
@@ -35,13 +35,9 @@ export default class PeernetHash {
35
35
  get prefix() {
36
36
  const length = this.length
37
37
  const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length)
38
- for (let i = 0; i < this.discoCodec.codecBuffer.length; i++) {
39
- uint8Array[i] = this.discoCodec.codecBuffer[i]
40
- }
38
+ uint8Array.set(length)
39
+ uint8Array.set(this.discoCodec.codecBuffer, length.length)
41
40
 
42
- for (let i = uint8Array.length - 1; i < length.length; i++) {
43
- uint8Array[i] = length[i]
44
- }
45
41
  return uint8Array
46
42
  }
47
43
 
@@ -100,10 +96,11 @@ export default class PeernetHash {
100
96
 
101
97
  this.codec = this.discoCodec.encode();
102
98
  this.codec = this.discoCodec.codecBuffer
103
- this.hash = Buffer.concat([
104
- this.prefix,
105
- this.digest,
106
- ])
99
+ const uint8Array = new Uint8Array(this.digest.length + this.prefix.length)
100
+ uint8Array.set(this.prefix)
101
+ uint8Array.set(this.digest, this.prefix.length)
102
+
103
+ this.hash = uint8Array
107
104
 
108
105
  return this.hash
109
106
  }
package/src/peernet.js CHANGED
@@ -257,6 +257,7 @@ export default class Peernet {
257
257
  * @param {PeernetPeer} peer - peernet peer
258
258
  */
259
259
  async _protoHandler(message, peer, from) {
260
+
260
261
  const {id, proto} = message
261
262
  this.bw.down += proto.encoded.length
262
263
 
@@ -309,8 +310,7 @@ export default class Peernet {
309
310
 
310
311
  this.bw.up += node.encoded.length
311
312
  }
312
- } else if (proto.name === 'peernet-ps' &&
313
- this._getPeerId(peer.id) !== this.id.toString()) {
313
+ } else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
314
314
  globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString())
315
315
  }
316
316
  // }
@@ -325,12 +325,11 @@ export default class Peernet {
325
325
  if (!hash) throw new Error('hash expected, received undefined')
326
326
  const data = new DHTMessage({hash})
327
327
  const clientId = this.client.id
328
- for (const peer of this.connections) {
329
- const node = await this.prepareMessage(peer.id, data.encoded)
330
-
331
- const result = await peer.request(node.encoded)
332
-
333
- let proto = protoFor(result.data)
328
+ const walk = async peer => {
329
+ const node = await this.prepareMessage(peer.peerId, data.encoded)
330
+ let result = await peer.request(node.encoded)
331
+ result = new Uint8Array(Object.values(result))
332
+ let proto = protoFor(result)
334
333
 
335
334
  if (proto.name !== 'peernet-message') throw encapsulatedError()
336
335
  const from = proto.decoded.from
@@ -354,7 +353,13 @@ export default class Peernet {
354
353
 
355
354
  if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
356
355
  }
357
- return
356
+ let walks = []
357
+ for (const peer of this.connections) {
358
+ if (peer.peerId !== this.id) {
359
+ walks.push(walk(peer))
360
+ }
361
+ }
362
+ return Promise.all(walks)
358
363
  }
359
364
 
360
365
  /**
@@ -425,7 +430,7 @@ export default class Peernet {
425
430
  const id = closestPeer.id.toString()
426
431
  if (this.connections) {
427
432
  let closest = this.connections.filter((peer) => {
428
- if (peer.id === id) return peer
433
+ if (peer.peerId === id) return peer
429
434
  })
430
435
 
431
436
  let data = new DataMessage({hash, store: store.name ? store.name : store});
@@ -434,7 +439,7 @@ export default class Peernet {
434
439
  if (closest[0]) data = await closest[0].request(node.encoded)
435
440
  else {
436
441
  closest = this.connections.filter((peer) => {
437
- if (peer.id === id) return peer
442
+ if (peer.peerId === id) return peer
438
443
  })
439
444
  if (closest[0]) data = await closest[0].request(node.encoded)
440
445
  }
@@ -578,8 +583,8 @@ export default class Peernet {
578
583
  data = new PsMessage({data, topic})
579
584
  for (const peer of this.connections) {
580
585
  if (peer.connected) {
581
- if (peer.id !== this.peerId) {
582
- const node = await this.prepareMessage(peer.id, data.encoded)
586
+ if (peer.peerId !== this.peerId) {
587
+ const node = await this.prepareMessage(peer.peerId, data.encoded)
583
588
  peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
584
589
  }
585
590
  } else {
@@ -604,7 +609,7 @@ export default class Peernet {
604
609
  }
605
610
 
606
611
  async removePeer(peer) {
607
- delete this.client.connections[peer.id]
612
+ delete this.client.connections[peer.peerId]
608
613
  }
609
614
 
610
615
  get Buffer() {
package/test4.js ADDED
@@ -0,0 +1,7 @@
1
+ const Client = require('./dist/commonjs/peernet.js')
2
+
3
+ const client = new Client({root: '.peernet/test4'})
4
+
5
+ pubsub.subscribe('peer:connected', async peer => {
6
+ await peernet.get("ba5xcadcnub27naa7zrcvtmyruviahaiwvupatfgjkvdgcehuif7clissf")
7
+ })
package/webpack.config.js CHANGED
@@ -17,7 +17,7 @@ module.exports = [{
17
17
  })
18
18
  ],
19
19
  optimization: {
20
- minimize: true
20
+ minimize: false
21
21
  },
22
22
  resolve: {
23
23
  extensions: [ '.ts', '.js' ],
@@ -1,40 +0,0 @@
1
- /*!
2
- * Determine if an object is a Buffer
3
- *
4
- * @author Feross Aboukhadijeh <https://feross.org>
5
- * @license MIT
6
- */
7
-
8
- /*!
9
- * The buffer module from node.js, for the browser.
10
- *
11
- * @author Feross Aboukhadijeh <https://feross.org>
12
- * @license MIT
13
- */
14
-
15
- /*!
16
- * assert.js - assertions for javascript
17
- * Copyright (c) 2018, Christopher Jeffrey (MIT License).
18
- * https://github.com/chjj/bsert
19
- */
20
-
21
- /*!
22
- * base32.js - base32 for bcrypto
23
- * Copyright (c) 2014-2019, Christopher Jeffrey (MIT License).
24
- * https://github.com/bcoin-org/bcrypto
25
- *
26
- * Parts of this software are based on bitcoin/bitcoin:
27
- * Copyright (c) 2009-2019, The Bitcoin Core Developers (MIT License).
28
- * Copyright (c) 2009-2019, The Bitcoin Developers (MIT License).
29
- * https://github.com/bitcoin/bitcoin
30
- *
31
- * Resources:
32
- * https://tools.ietf.org/html/rfc4648
33
- * https://github.com/bitcoin/bitcoin/blob/11d486d/src/utilstrencodings.cpp#L230
34
- */
35
-
36
- /*! For license information please see browser.js.LICENSE.txt */
37
-
38
- /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
39
-
40
- /*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */