trac-msb 0.1.0 → 0.1.1
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/Whitelist/pubkeys.csv +1 -1
- package/package.json +1 -1
- package/src/index.js +23 -31
- package/src/network.js +81 -92
package/Whitelist/pubkeys.csv
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
f95bfb8ef84d4cc3ee074765fb1e2a69af846df4309782fc4bc5bb6e18b944f5
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -9,12 +9,22 @@ import PeerWallet from "trac-wallet"
|
|
|
9
9
|
import tty from 'tty';
|
|
10
10
|
import Corestore from 'corestore';
|
|
11
11
|
import MsgUtils from './utils/msgUtils.js';
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
LISTENER_TIMEOUT,
|
|
14
|
+
EntryType,
|
|
15
|
+
OperationType,
|
|
16
|
+
EventType,
|
|
17
|
+
ACK_INTERVAL,
|
|
18
|
+
WHITELIST_SLEEP_INTERVAL,
|
|
19
|
+
UPDATER_INTERVAL,
|
|
20
|
+
MAX_INDEXERS,
|
|
21
|
+
MIN_INDEXERS,
|
|
22
|
+
WHITELIST_PREFIX,
|
|
23
|
+
TRAC_NAMESPACE
|
|
24
|
+
} from './utils/constants.js';
|
|
13
25
|
import Network from './network.js';
|
|
14
26
|
import Check from './utils/check.js';
|
|
15
|
-
|
|
16
27
|
import DHT from 'hyperdht'
|
|
17
|
-
import process from 'process'
|
|
18
28
|
|
|
19
29
|
//TODO: CHANGE NONCE.
|
|
20
30
|
|
|
@@ -33,7 +43,6 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
33
43
|
#store;
|
|
34
44
|
#bee;
|
|
35
45
|
#swarm;
|
|
36
|
-
#dht_server;
|
|
37
46
|
#dht_node;
|
|
38
47
|
#dht_bootstrap;
|
|
39
48
|
#base;
|
|
@@ -52,9 +61,9 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
52
61
|
this.#initInternalAttributes(options);
|
|
53
62
|
this.msbListener();
|
|
54
63
|
this.#boot();
|
|
55
|
-
this.ready().catch(noop);
|
|
56
64
|
this.#setupInternalListeners();
|
|
57
65
|
this.#network = new Network(this.#base);
|
|
66
|
+
this.ready().catch(noop);
|
|
58
67
|
}
|
|
59
68
|
|
|
60
69
|
#initInternalAttributes(options) {
|
|
@@ -67,8 +76,7 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
67
76
|
this.#bee = null;
|
|
68
77
|
this.#swarm = null;
|
|
69
78
|
this.#dht_bootstrap = [/*'116.202.214.143:10001','116.202.214.149:10001', */'node1.hyperdht.org:49737', 'node2.hyperdht.org:49737', 'node3.hyperdht.org:49737'];
|
|
70
|
-
this.#dht_node =
|
|
71
|
-
this.#dht_server = null;
|
|
79
|
+
this.#dht_node = null;
|
|
72
80
|
this.#base = null;
|
|
73
81
|
this.#writingKey = null;
|
|
74
82
|
this.#enable_txchannel = options.enable_txchannel !== false;
|
|
@@ -368,12 +376,8 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
368
376
|
console.log('Writer Key:', this.#writingKey);
|
|
369
377
|
|
|
370
378
|
if (this.#replicate) {
|
|
371
|
-
this.#swarm = await Network.replicate(this.#dht_bootstrap, this.#swarm, this.#enable_wallet, this.#store, this.#wallet, this.#channel, this.#isStreaming, this.#handleIncomingEvent.bind(this), this.emit.bind(this));
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if (this.#enable_txchannel) {
|
|
375
|
-
this.#dht_server = this.#dht_node.createServer();
|
|
376
|
-
await Network.dhtServer(this, this.#dht_server, this.#base, this.#wallet, this.#writingKey, this.#network);
|
|
379
|
+
this.#swarm = await Network.replicate(this, this.#enable_txchannel, this.#base, this.#writingKey, this.#dht_bootstrap, this.#swarm, this.#enable_wallet, this.#store, this.#wallet, this.#channel, this.#isStreaming, this.#handleIncomingEvent.bind(this), this.emit.bind(this));
|
|
380
|
+
this.#dht_node = this.#swarm.dht;
|
|
377
381
|
}
|
|
378
382
|
|
|
379
383
|
const adminEntry = await this.get(EntryType.ADMIN);
|
|
@@ -403,18 +407,6 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
403
407
|
if (this.#swarm) {
|
|
404
408
|
await this.#swarm.destroy();
|
|
405
409
|
}
|
|
406
|
-
if (this.#dht_server) {
|
|
407
|
-
try{
|
|
408
|
-
await this.#dht_server.close();
|
|
409
|
-
} catch(e){
|
|
410
|
-
console.log(e.message);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
try{
|
|
414
|
-
this.#dht_node.destroy();
|
|
415
|
-
} catch(e){
|
|
416
|
-
console.log(e.message);
|
|
417
|
-
}
|
|
418
410
|
await this.#base.close();
|
|
419
411
|
}
|
|
420
412
|
|
|
@@ -432,15 +424,15 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
432
424
|
if (!adminEntry || !message) {
|
|
433
425
|
return;
|
|
434
426
|
}
|
|
435
|
-
const
|
|
436
|
-
const stream = node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
|
|
427
|
+
const stream = this.#dht_node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
|
|
437
428
|
stream.on('connect', async function () {
|
|
429
|
+
console.log('Trying to send message to admin.');
|
|
438
430
|
await stream.send(b4a.from(JSON.stringify(message)));
|
|
439
|
-
await stream.
|
|
431
|
+
await stream.end();
|
|
440
432
|
});
|
|
441
|
-
stream.on('open', function () { });
|
|
442
|
-
stream.on('close', () => { });
|
|
443
|
-
stream.on('error', (error) => { });
|
|
433
|
+
stream.on('open', function () { console.log('Message channel opened') });
|
|
434
|
+
stream.on('close', () => { console.log('Message channel closed') });
|
|
435
|
+
stream.on('error', (error) => { console.log('Message send error', error) });
|
|
444
436
|
}
|
|
445
437
|
|
|
446
438
|
async #verifyMessage(signature, publicKey, bufferMessage) {
|
package/src/network.js
CHANGED
|
@@ -14,7 +14,6 @@ import {
|
|
|
14
14
|
import {sleep } from './utils/functions.js';
|
|
15
15
|
import MsgUtils from './utils/msgUtils.js';
|
|
16
16
|
import Check from './utils/check.js';
|
|
17
|
-
import DHT from "hyperdht";
|
|
18
17
|
const wakeup = new w();
|
|
19
18
|
|
|
20
19
|
class Network {
|
|
@@ -25,8 +24,11 @@ class Network {
|
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
|
|
28
|
-
static async replicate(bootstrap, swarm, walletEnabled, store, wallet, channel, isStreaming, handleIncomingEvent, emit) {
|
|
27
|
+
static async replicate(msb, enable_txchannel, base, writingKey, bootstrap, swarm, walletEnabled, store, wallet, channel, isStreaming, handleIncomingEvent, emit) {
|
|
29
28
|
if (!swarm) {
|
|
29
|
+
|
|
30
|
+
const _this = this;
|
|
31
|
+
|
|
30
32
|
let keyPair;
|
|
31
33
|
if (!walletEnabled) {
|
|
32
34
|
keyPair = await store.createKeyPair(TRAC_NAMESPACE);
|
|
@@ -49,6 +51,83 @@ class Network {
|
|
|
49
51
|
await handleIncomingEvent(data);
|
|
50
52
|
});
|
|
51
53
|
|
|
54
|
+
if(enable_txchannel){
|
|
55
|
+
connection.on('message', async (msg) => {
|
|
56
|
+
try{
|
|
57
|
+
msg = b4a.toString(msg, 'utf-8');
|
|
58
|
+
msg = JSON.parse(msg);
|
|
59
|
+
if(null === msg) return;
|
|
60
|
+
if(msg === 'get_writer_key'){
|
|
61
|
+
await connection.send(b4a.from(JSON.stringify({op:'writer_key', key : writingKey})));
|
|
62
|
+
await connection.end();
|
|
63
|
+
} else if(msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addWriter'){
|
|
64
|
+
await connection.end();
|
|
65
|
+
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
66
|
+
if(null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
67
|
+
const nodeEntry = await msb.get(msg.value.pub);
|
|
68
|
+
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
69
|
+
const isAllowedToRequestRole = await msb._isAllowedToRequestRole(msg.key, adminEntry);
|
|
70
|
+
const canAddWriter = base.writable && !isAlreadyWriter && isAllowedToRequestRole;
|
|
71
|
+
if(msg.key !== wallet.publicKey && canAddWriter){
|
|
72
|
+
await base.append(msg);
|
|
73
|
+
}
|
|
74
|
+
} else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'removeWriter') {
|
|
75
|
+
await connection.end();
|
|
76
|
+
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
77
|
+
if(null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
78
|
+
const nodeEntry = await msb.get(msg.value.pub);
|
|
79
|
+
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
80
|
+
const canRemoveWriter = base.writable && isAlreadyWriter
|
|
81
|
+
if (msg.key !== wallet.publicKey && canRemoveWriter) {
|
|
82
|
+
await base.append(msg);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
//await connection.destroy();
|
|
87
|
+
if (base.isIndexer || !base.writable) return;
|
|
88
|
+
|
|
89
|
+
// TODO: decide if a tx rejection should be responded with
|
|
90
|
+
if (_this.tx_pool.length >= 1000) {
|
|
91
|
+
console.log('pool full');
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (b4a.byteLength(msg) > 3072) return;
|
|
96
|
+
|
|
97
|
+
const parsedPreTx = msg;
|
|
98
|
+
|
|
99
|
+
if (_this.check.sanitizePreTx(parsedPreTx) &&
|
|
100
|
+
wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
|
|
101
|
+
parsedPreTx.wp === wallet.publicKey &&
|
|
102
|
+
null === await base.view.get(parsedPreTx.tx)
|
|
103
|
+
) {
|
|
104
|
+
const nonce = MsgUtils.generateNonce();
|
|
105
|
+
const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
|
|
106
|
+
const append_tx = {
|
|
107
|
+
op: OperationType.POST_TX,
|
|
108
|
+
tx: parsedPreTx.tx,
|
|
109
|
+
is: parsedPreTx.is,
|
|
110
|
+
w: writingKey,
|
|
111
|
+
i: parsedPreTx.i,
|
|
112
|
+
ipk: parsedPreTx.ipk,
|
|
113
|
+
ch: parsedPreTx.ch,
|
|
114
|
+
in: parsedPreTx.in,
|
|
115
|
+
bs: parsedPreTx.bs,
|
|
116
|
+
mbs: parsedPreTx.mbs,
|
|
117
|
+
ws: signature.toString('hex'),
|
|
118
|
+
wp: wallet.publicKey,
|
|
119
|
+
wn: nonce
|
|
120
|
+
};
|
|
121
|
+
_this.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}catch(e){
|
|
125
|
+
console.log(e);
|
|
126
|
+
try{ await connection.end(); } catch (e){ }
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
52
131
|
if (!isStreaming) {
|
|
53
132
|
emit(EventType.READY_MSB);
|
|
54
133
|
}
|
|
@@ -68,96 +147,6 @@ class Network {
|
|
|
68
147
|
return swarm;
|
|
69
148
|
}
|
|
70
149
|
|
|
71
|
-
static async dhtServer(msb, dhtServer, base, wallet, writingKey, networkInstance){
|
|
72
|
-
try{
|
|
73
|
-
dhtServer.on('connection', function (connection) {
|
|
74
|
-
connection.on('message', async (msg) => {
|
|
75
|
-
try{
|
|
76
|
-
msg = b4a.toString(msg, 'utf-8');
|
|
77
|
-
//console.log(msg);
|
|
78
|
-
msg = JSON.parse(msg);
|
|
79
|
-
if(null === msg) return;
|
|
80
|
-
if(msg === 'get_writer_key'){
|
|
81
|
-
await connection.send(b4a.from(JSON.stringify({op:'writer_key', key : writingKey})));
|
|
82
|
-
await connection.destroy();
|
|
83
|
-
} else if(msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addWriter'){
|
|
84
|
-
await connection.destroy();
|
|
85
|
-
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
86
|
-
if(null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
87
|
-
const nodeEntry = await msb.get(msg.value.pub);
|
|
88
|
-
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
89
|
-
const isAllowedToRequestRole = await msb._isAllowedToRequestRole(msg.key, adminEntry);
|
|
90
|
-
const canAddWriter = base.writable && !isAlreadyWriter && isAllowedToRequestRole;
|
|
91
|
-
if(msg.key !== wallet.publicKey && canAddWriter){
|
|
92
|
-
await base.append(msg);
|
|
93
|
-
}
|
|
94
|
-
} else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'removeWriter') {
|
|
95
|
-
await connection.destroy();
|
|
96
|
-
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
97
|
-
if(null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
98
|
-
const nodeEntry = await msb.get(msg.value.pub);
|
|
99
|
-
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
100
|
-
const canRemoveWriter = base.writable && isAlreadyWriter
|
|
101
|
-
if (msg.key !== wallet.publicKey && canRemoveWriter) {
|
|
102
|
-
await base.append(msg);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
//await connection.destroy();
|
|
107
|
-
if (base.isIndexer || !base.writable) return;
|
|
108
|
-
|
|
109
|
-
// TODO: decide if a tx rejection should be responded with
|
|
110
|
-
if (networkInstance.tx_pool.length >= 1000) {
|
|
111
|
-
console.log('pool full');
|
|
112
|
-
return
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (b4a.byteLength(msg) > 3072) return;
|
|
116
|
-
|
|
117
|
-
const parsedPreTx = msg;
|
|
118
|
-
|
|
119
|
-
if (networkInstance.check.sanitizePreTx(parsedPreTx) &&
|
|
120
|
-
wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
|
|
121
|
-
parsedPreTx.wp === wallet.publicKey &&
|
|
122
|
-
null === await base.view.get(parsedPreTx.tx)
|
|
123
|
-
) {
|
|
124
|
-
const nonce = MsgUtils.generateNonce();
|
|
125
|
-
const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
|
|
126
|
-
const append_tx = {
|
|
127
|
-
op: OperationType.POST_TX,
|
|
128
|
-
tx: parsedPreTx.tx,
|
|
129
|
-
is: parsedPreTx.is,
|
|
130
|
-
w: writingKey,
|
|
131
|
-
i: parsedPreTx.i,
|
|
132
|
-
ipk: parsedPreTx.ipk,
|
|
133
|
-
ch: parsedPreTx.ch,
|
|
134
|
-
in: parsedPreTx.in,
|
|
135
|
-
bs: parsedPreTx.bs,
|
|
136
|
-
mbs: parsedPreTx.mbs,
|
|
137
|
-
ws: signature.toString('hex'),
|
|
138
|
-
wp: wallet.publicKey,
|
|
139
|
-
wn: nonce
|
|
140
|
-
};
|
|
141
|
-
networkInstance.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}catch(e){
|
|
145
|
-
console.log(e);
|
|
146
|
-
try{ await connection.destroy(); } catch (e){ }
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
connection.on('close', () => { });
|
|
150
|
-
connection.on('error', (error) => { });
|
|
151
|
-
})
|
|
152
|
-
const keyPair = {
|
|
153
|
-
publicKey: b4a.from(wallet.publicKey, 'hex'),
|
|
154
|
-
secretKey: b4a.from(wallet.secretKey, 'hex')
|
|
155
|
-
};
|
|
156
|
-
await dhtServer.listen(keyPair)
|
|
157
|
-
console.log('DHT node is listening on public key', wallet.publicKey);
|
|
158
|
-
} catch(e) { console.log(e) }
|
|
159
|
-
}
|
|
160
|
-
|
|
161
150
|
async pool(base) {
|
|
162
151
|
while (true) {
|
|
163
152
|
if (this.tx_pool.length > 0) {
|