trac-msb 0.0.71 → 0.0.72

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.0.71",
4
+ "version": "0.0.72",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
package/src/index.js CHANGED
@@ -33,7 +33,7 @@ export class MainSettlementBus extends ReadyResource {
33
33
  #store;
34
34
  #bee;
35
35
  #swarm;
36
- #tx_swarm;
36
+ #dht_server;
37
37
  #base;
38
38
  #writingKey;
39
39
  #enable_txchannel;
@@ -65,7 +65,7 @@ export class MainSettlementBus extends ReadyResource {
65
65
  this.#store = new Corestore(this.STORES_DIRECTORY + options.store_name);
66
66
  this.#bee = null;
67
67
  this.#swarm = null;
68
- this.#tx_swarm = null;
68
+ this.#dht_server = null;
69
69
  this.#base = null;
70
70
  this.#writingKey = null;
71
71
  this.#enable_txchannel = options.enable_txchannel !== false;
@@ -339,26 +339,6 @@ export class MainSettlementBus extends ReadyResource {
339
339
  }
340
340
  }
341
341
 
342
- async dhtNode(wallet){
343
- const node = new DHT()
344
- const server = node.createServer()
345
-
346
- server.on('connection', function (socket) {
347
- // socket is E2E encrypted between you and the other peer
348
- console.log('Remote public key', socket)
349
- process.stdin.pipe(socket).pipe(process.stdout)
350
- })
351
-
352
- const keyPair = {
353
- publicKey: b4a.from(wallet.publicKey, 'hex'),
354
- secretKey: b4a.from(wallet.secretKey, 'hex')
355
- };
356
-
357
- // this makes the server accept connections on this keypair
358
- await server.listen(keyPair)
359
- console.log('DHT node is listening');
360
- }
361
-
362
342
  async _open() {
363
343
  await this.#base.ready();
364
344
  if (this.#enable_wallet) {
@@ -369,15 +349,14 @@ export class MainSettlementBus extends ReadyResource {
369
349
  console.log('MSB Key:', b4a.from(this.#base.view.core.key).toString('hex'));
370
350
 
371
351
  this.#writingKey = b4a.toString(this.#base.local.key, 'hex');
372
- console.log('Writing Key:', this.#writingKey);
352
+ console.log('Writer Key:', this.#writingKey);
373
353
 
374
354
  if (this.#replicate) {
375
355
  this.#swarm = await Network.replicate(this.#swarm, this.#enable_wallet, this.#store, this.#wallet, this.#channel, this.#isStreaming, this.#handleIncomingEvent.bind(this), this.emit.bind(this));
376
356
  }
377
357
 
378
358
  if (this.#enable_txchannel) {
379
- this.#tx_swarm = await Network.txChannel(this.#tx_swarm, this.#tx, this.#base, this.#wallet, this.#writingKey, this.#network);
380
- this.dhtNode(this.#wallet);
359
+ this.#dht_server = await Network.dhtServer(this.#base, this.#wallet, this.#writingKey, this.#network);
381
360
  }
382
361
 
383
362
  const adminEntry = await this.getSigned(EntryType.ADMIN);
@@ -406,8 +385,13 @@ export class MainSettlementBus extends ReadyResource {
406
385
  if (this.#swarm) {
407
386
  await this.#swarm.destroy();
408
387
  }
409
- if (this.#tx_swarm) {
410
- await this.#tx_swarm.destroy();
388
+ if (this.#dht_server) {
389
+ try{
390
+ await this.#dht_server.server.close();
391
+ await this.#dht_server.node.destroy();
392
+ } catch(e){
393
+ console.log(e.message);
394
+ }
411
395
  }
412
396
  await this.#base.close();
413
397
  }
package/src/network.js CHANGED
@@ -5,6 +5,7 @@ import { EventType, TRAC_NAMESPACE, MAX_PEERS, MAX_PARALLEL, MAX_SERVER_CONNECTI
5
5
  import {sleep } from './utils/functions.js';
6
6
  import MsgUtils from './utils/msgUtils.js';
7
7
  import Check from './utils/check.js';
8
+ import DHT from "hyperdht";
8
9
  const wakeup = new w();
9
10
 
10
11
  class Network {
@@ -44,73 +45,80 @@ class Network {
44
45
  }
45
46
  });
46
47
 
47
- const channelBuffer = channel;
48
- swarm.join(channelBuffer, { server: true, client: true });
48
+ swarm.join(channel, { server: true, client: true });
49
49
  await swarm.flush();
50
50
  console.log('Joined channel for peer discovery');
51
51
  }
52
52
  return swarm;
53
53
  }
54
54
 
55
- static async txChannel(tx_swarm, tx_channel, base, wallet, writingKey, networkInstance) {
56
- if (!tx_swarm) {
57
- tx_swarm = new Hyperswarm({ maxPeers: MAX_PEERS, maxParallel: MAX_PARALLEL, maxServerConnections: MAX_SERVER_CONNECTIONS });
58
- tx_swarm.on('connection', async (connection, peerInfo) => {
59
-
60
- connection.on('close', () => { });
61
- connection.on('error', (error) => { });
62
- connection.on('data', async (msg) => {
63
-
64
- if (base.isIndexer || !base.writable) return;
65
-
66
- // TODO: decide if a tx rejection should be responded with
67
- if (networkInstance.tx_pool.length >= 1000) {
68
- console.log('pool full');
69
- return
70
- }
71
-
72
- if (b4a.byteLength(msg) > 3072) return;
73
-
74
- try {
75
-
76
- const parsedPreTx = JSON.parse(msg);
55
+ static async dhtServer(base, wallet, writingKey, networkInstance){
56
+ try{
57
+ const _this = this;
58
+ const node = new DHT()
59
+ const server = node.createServer()
60
+ server.on('connection', function (connection) {
61
+ connection.on('message', async (msg) => {
62
+ const cmd = b4a.toString(msg, 'utf-8');
63
+ if(cmd === 'get_writer_key'){
64
+ await connection.send(b4a.from(JSON.stringify({op:'writer_key', key : writingKey})));
65
+ } else {
66
+ if (base.isIndexer || !base.writable) return;
67
+
68
+ // TODO: decide if a tx rejection should be responded with
69
+ if (networkInstance.tx_pool.length >= 1000) {
70
+ console.log('pool full');
71
+ return
72
+ }
77
73
 
78
- if (networkInstance.check.sanitizePreTx(parsedPreTx) &&
79
- wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
80
- parsedPreTx.w === writingKey &&
81
- null === await base.view.get(parsedPreTx.tx)
82
- ) {
83
- const nonce = MsgUtils.generateNonce();
84
- const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
85
- const append_tx = {
86
- op: OperationType.POST_TX,
87
- tx: parsedPreTx.tx,
88
- is: parsedPreTx.is,
89
- w: parsedPreTx.w,
90
- i: parsedPreTx.i,
91
- ipk: parsedPreTx.ipk,
92
- ch: parsedPreTx.ch,
93
- in: parsedPreTx.in,
94
- bs: parsedPreTx.bs,
95
- mbs: parsedPreTx.mbs,
96
- ws: signature.toString('hex'),
97
- wp: wallet.publicKey,
98
- wn: nonce
99
- };
100
- networkInstance.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
74
+ if (b4a.byteLength(msg) > 3072) return;
75
+
76
+ try {
77
+
78
+ const parsedPreTx = JSON.parse(msg);
79
+
80
+ if (networkInstance.check.sanitizePreTx(parsedPreTx) &&
81
+ wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
82
+ parsedPreTx.w === writingKey &&
83
+ parsedPreTx.wp === wallet.publicKey &&
84
+ null === await base.view.get(parsedPreTx.tx)
85
+ ) {
86
+ const nonce = MsgUtils.generateNonce();
87
+ const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
88
+ const append_tx = {
89
+ op: OperationType.POST_TX,
90
+ tx: parsedPreTx.tx,
91
+ is: parsedPreTx.is,
92
+ w: parsedPreTx.w,
93
+ i: parsedPreTx.i,
94
+ ipk: parsedPreTx.ipk,
95
+ ch: parsedPreTx.ch,
96
+ in: parsedPreTx.in,
97
+ bs: parsedPreTx.bs,
98
+ mbs: parsedPreTx.mbs,
99
+ ws: signature.toString('hex'),
100
+ wp: wallet.publicKey,
101
+ wn: nonce
102
+ };
103
+ networkInstance.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
104
+ }
105
+ } catch (e) {
106
+ //console.log(e)
101
107
  }
102
- } catch (e) {
103
- //console.log(e)
104
108
  }
109
+ await connection.destroy();
105
110
  });
106
-
107
- });
108
-
109
- tx_swarm.join(tx_channel, { server: true, client: true });
110
- await tx_swarm.flush();
111
- console.log('Joined MSB TX channel');
112
- }
113
- return tx_swarm;
111
+ connection.on('close', () => { console.log('remote closing'); });
112
+ connection.on('error', (error) => { });
113
+ })
114
+ const keyPair = {
115
+ publicKey: b4a.from(wallet.publicKey, 'hex'),
116
+ secretKey: b4a.from(wallet.secretKey, 'hex')
117
+ };
118
+ await server.listen(keyPair)
119
+ console.log('DHT node is listening on public key', wallet.publicKey);
120
+ return { server, node }
121
+ } catch(e) { }
114
122
  }
115
123
 
116
124
  async pool(base) {