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 +1 -1
- package/src/index.js +11 -27
- package/src/network.js +65 -57
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -33,7 +33,7 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
33
33
|
#store;
|
|
34
34
|
#bee;
|
|
35
35
|
#swarm;
|
|
36
|
-
#
|
|
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.#
|
|
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('
|
|
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.#
|
|
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.#
|
|
410
|
-
|
|
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
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
connection.on('
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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 (
|
|
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', () => { 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) {
|