trac-msb 0.0.71 → 0.0.73
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 +1 -1
- package/src/index.js +17 -27
- package/src/network.js +61 -54
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -33,7 +33,8 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
33
33
|
#store;
|
|
34
34
|
#bee;
|
|
35
35
|
#swarm;
|
|
36
|
-
#
|
|
36
|
+
#dht_server;
|
|
37
|
+
#dht_node;
|
|
37
38
|
#base;
|
|
38
39
|
#writingKey;
|
|
39
40
|
#enable_txchannel;
|
|
@@ -65,7 +66,8 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
65
66
|
this.#store = new Corestore(this.STORES_DIRECTORY + options.store_name);
|
|
66
67
|
this.#bee = null;
|
|
67
68
|
this.#swarm = null;
|
|
68
|
-
this.#
|
|
69
|
+
this.#dht_node = new DHT();
|
|
70
|
+
this.#dht_server = this.#dht_node.createServer();
|
|
69
71
|
this.#base = null;
|
|
70
72
|
this.#writingKey = null;
|
|
71
73
|
this.#enable_txchannel = options.enable_txchannel !== false;
|
|
@@ -339,26 +341,6 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
339
341
|
}
|
|
340
342
|
}
|
|
341
343
|
|
|
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
344
|
async _open() {
|
|
363
345
|
await this.#base.ready();
|
|
364
346
|
if (this.#enable_wallet) {
|
|
@@ -369,15 +351,14 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
369
351
|
console.log('MSB Key:', b4a.from(this.#base.view.core.key).toString('hex'));
|
|
370
352
|
|
|
371
353
|
this.#writingKey = b4a.toString(this.#base.local.key, 'hex');
|
|
372
|
-
console.log('
|
|
354
|
+
console.log('Writer Key:', this.#writingKey);
|
|
373
355
|
|
|
374
356
|
if (this.#replicate) {
|
|
375
357
|
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
358
|
}
|
|
377
359
|
|
|
378
360
|
if (this.#enable_txchannel) {
|
|
379
|
-
|
|
380
|
-
this.dhtNode(this.#wallet);
|
|
361
|
+
await Network.dhtServer(this.#dht_server, this.#base, this.#wallet, this.#writingKey, this.#network);
|
|
381
362
|
}
|
|
382
363
|
|
|
383
364
|
const adminEntry = await this.getSigned(EntryType.ADMIN);
|
|
@@ -406,8 +387,17 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
406
387
|
if (this.#swarm) {
|
|
407
388
|
await this.#swarm.destroy();
|
|
408
389
|
}
|
|
409
|
-
if (this.#
|
|
410
|
-
|
|
390
|
+
if (this.#dht_server) {
|
|
391
|
+
try{
|
|
392
|
+
await this.#dht_server.close();
|
|
393
|
+
} catch(e){
|
|
394
|
+
console.log(e.message);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
try{
|
|
398
|
+
this.#dht_node.destroy();
|
|
399
|
+
} catch(e){
|
|
400
|
+
console.log(e.message);
|
|
411
401
|
}
|
|
412
402
|
await this.#base.close();
|
|
413
403
|
}
|
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,79 @@ class Network {
|
|
|
44
45
|
}
|
|
45
46
|
});
|
|
46
47
|
|
|
47
|
-
|
|
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
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
static async dhtServer(dhtServer, base, wallet, writingKey, networkInstance){
|
|
56
|
+
try{
|
|
57
|
+
server.on('connection', function (connection) {
|
|
58
|
+
connection.on('message', async (msg) => {
|
|
59
|
+
const cmd = b4a.toString(msg, 'utf-8');
|
|
60
|
+
if(cmd === 'get_writer_key'){
|
|
61
|
+
await connection.send(b4a.from(JSON.stringify({op:'writer_key', key : writingKey})));
|
|
62
|
+
} else {
|
|
59
63
|
|
|
60
|
-
|
|
61
|
-
connection.on('error', (error) => { });
|
|
62
|
-
connection.on('data', async (msg) => {
|
|
64
|
+
console.log(msg);
|
|
63
65
|
|
|
64
|
-
|
|
66
|
+
if (base.isIndexer || !base.writable) return;
|
|
65
67
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
+
}
|
|
71
73
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
connection.on('close', () => { });
|
|
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
|
+
} catch(e) { }
|
|
114
121
|
}
|
|
115
122
|
|
|
116
123
|
async pool(base) {
|