trac-msb 0.1.0 → 0.1.2
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/msb.mjs +1 -2
- package/package.json +1 -1
- package/src/index.js +23 -32
- package/src/network.js +80 -101
package/Whitelist/pubkeys.csv
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
23cf5527fb3186628e944d007aeeaa3b21297182f027ae2e5afc820ccd72e08b
|
package/msb.mjs
CHANGED
|
@@ -5,7 +5,6 @@ const opts = {
|
|
|
5
5
|
store_name : typeof process !== "undefined" ? process.argv[2] : Pear.config.args[0],
|
|
6
6
|
bootstrap: '4577ebf1e84c06d48b2caa33fe54ae58be3a79cb30810129d58b725fae412e37',
|
|
7
7
|
channel: '00axtracnetworkmainsettlementbus',
|
|
8
|
-
tx : 'axtracnetworkmainsettlementbustx',
|
|
9
8
|
enable_updater : true
|
|
10
9
|
};
|
|
11
10
|
|
|
@@ -13,5 +12,5 @@ const msb = new MainSettlementBus(opts);
|
|
|
13
12
|
|
|
14
13
|
msb.ready().then(() => {
|
|
15
14
|
console.log('MSB is ready.');
|
|
16
|
-
|
|
15
|
+
msb.interactiveMode();
|
|
17
16
|
});
|
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) {
|
|
@@ -62,13 +71,11 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
62
71
|
this.#KEY_PAIR_PATH = `${this.STORES_DIRECTORY}${options.store_name}/db/keypair.json`
|
|
63
72
|
this.#bootstrap = options.bootstrap || null;
|
|
64
73
|
this.#channel = b4a.alloc(32).fill(options.channel) || null;
|
|
65
|
-
this.#tx = b4a.alloc(32).fill(options.tx) || null;
|
|
66
74
|
this.#store = new Corestore(this.STORES_DIRECTORY + options.store_name);
|
|
67
75
|
this.#bee = null;
|
|
68
76
|
this.#swarm = null;
|
|
69
77
|
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;
|
|
78
|
+
this.#dht_node = null;
|
|
72
79
|
this.#base = null;
|
|
73
80
|
this.#writingKey = null;
|
|
74
81
|
this.#enable_txchannel = options.enable_txchannel !== false;
|
|
@@ -368,12 +375,8 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
368
375
|
console.log('Writer Key:', this.#writingKey);
|
|
369
376
|
|
|
370
377
|
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);
|
|
378
|
+
this.#swarm = await Network.replicate(this, this.#network, 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));
|
|
379
|
+
this.#dht_node = this.#swarm.dht;
|
|
377
380
|
}
|
|
378
381
|
|
|
379
382
|
const adminEntry = await this.get(EntryType.ADMIN);
|
|
@@ -403,18 +406,6 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
403
406
|
if (this.#swarm) {
|
|
404
407
|
await this.#swarm.destroy();
|
|
405
408
|
}
|
|
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
409
|
await this.#base.close();
|
|
419
410
|
}
|
|
420
411
|
|
|
@@ -432,15 +423,15 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
432
423
|
if (!adminEntry || !message) {
|
|
433
424
|
return;
|
|
434
425
|
}
|
|
435
|
-
const
|
|
436
|
-
const stream = node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
|
|
426
|
+
const stream = this.#dht_node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
|
|
437
427
|
stream.on('connect', async function () {
|
|
428
|
+
console.log('Trying to send message to admin.');
|
|
438
429
|
await stream.send(b4a.from(JSON.stringify(message)));
|
|
439
|
-
await stream.
|
|
430
|
+
await stream.end();
|
|
440
431
|
});
|
|
441
|
-
stream.on('open', function () { });
|
|
442
|
-
stream.on('close', () => { });
|
|
443
|
-
stream.on('error', (error) => { });
|
|
432
|
+
stream.on('open', function () { console.log('Message channel opened') });
|
|
433
|
+
stream.on('close', () => { console.log('Message channel closed') });
|
|
434
|
+
stream.on('error', (error) => { console.log('Message send error', error) });
|
|
444
435
|
}
|
|
445
436
|
|
|
446
437
|
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,9 @@ 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, network, enable_txchannel, base, writingKey, bootstrap, swarm, walletEnabled, store, wallet, channel, isStreaming, handleIncomingEvent, emit) {
|
|
29
28
|
if (!swarm) {
|
|
29
|
+
|
|
30
30
|
let keyPair;
|
|
31
31
|
if (!walletEnabled) {
|
|
32
32
|
keyPair = await store.createKeyPair(TRAC_NAMESPACE);
|
|
@@ -49,115 +49,94 @@ class Network {
|
|
|
49
49
|
await handleIncomingEvent(data);
|
|
50
50
|
});
|
|
51
51
|
|
|
52
|
+
if(enable_txchannel){
|
|
53
|
+
connection.on('message', async (msg) => {
|
|
54
|
+
try{
|
|
55
|
+
msg = b4a.toString(msg, 'utf-8');
|
|
56
|
+
msg = JSON.parse(msg);
|
|
57
|
+
if(null === msg) return;
|
|
58
|
+
if(msg === 'get_writer_key'){
|
|
59
|
+
await connection.send(b4a.from(JSON.stringify({op:'writer_key', key : writingKey})));
|
|
60
|
+
await connection.end();
|
|
61
|
+
} else if(msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'addWriter'){
|
|
62
|
+
await connection.end();
|
|
63
|
+
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
64
|
+
if(null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
65
|
+
const nodeEntry = await msb.get(msg.value.pub);
|
|
66
|
+
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
67
|
+
const isAllowedToRequestRole = await msb._isAllowedToRequestRole(msg.key, adminEntry);
|
|
68
|
+
const canAddWriter = base.writable && !isAlreadyWriter && isAllowedToRequestRole;
|
|
69
|
+
if(msg.key !== wallet.publicKey && canAddWriter){
|
|
70
|
+
await base.append(msg);
|
|
71
|
+
}
|
|
72
|
+
} else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'removeWriter') {
|
|
73
|
+
await connection.end();
|
|
74
|
+
const adminEntry = await msb.get(EntryType.ADMIN);
|
|
75
|
+
if(null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
|
|
76
|
+
const nodeEntry = await msb.get(msg.value.pub);
|
|
77
|
+
const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
|
|
78
|
+
const canRemoveWriter = base.writable && isAlreadyWriter
|
|
79
|
+
if (msg.key !== wallet.publicKey && canRemoveWriter) {
|
|
80
|
+
await base.append(msg);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
//await connection.destroy();
|
|
85
|
+
if (base.isIndexer || !base.writable) return;
|
|
86
|
+
|
|
87
|
+
// TODO: decide if a tx rejection should be responded with
|
|
88
|
+
if (network.tx_pool.length >= 1000) {
|
|
89
|
+
console.log('pool full');
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (b4a.byteLength(msg) > 3072) return;
|
|
94
|
+
|
|
95
|
+
const parsedPreTx = msg;
|
|
96
|
+
|
|
97
|
+
if (network.check.sanitizePreTx(parsedPreTx) &&
|
|
98
|
+
wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
|
|
99
|
+
parsedPreTx.wp === wallet.publicKey &&
|
|
100
|
+
null === await base.view.get(parsedPreTx.tx)
|
|
101
|
+
) {
|
|
102
|
+
const nonce = MsgUtils.generateNonce();
|
|
103
|
+
const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
|
|
104
|
+
const append_tx = {
|
|
105
|
+
op: OperationType.POST_TX,
|
|
106
|
+
tx: parsedPreTx.tx,
|
|
107
|
+
is: parsedPreTx.is,
|
|
108
|
+
w: writingKey,
|
|
109
|
+
i: parsedPreTx.i,
|
|
110
|
+
ipk: parsedPreTx.ipk,
|
|
111
|
+
ch: parsedPreTx.ch,
|
|
112
|
+
in: parsedPreTx.in,
|
|
113
|
+
bs: parsedPreTx.bs,
|
|
114
|
+
mbs: parsedPreTx.mbs,
|
|
115
|
+
ws: signature.toString('hex'),
|
|
116
|
+
wp: wallet.publicKey,
|
|
117
|
+
wn: nonce
|
|
118
|
+
};
|
|
119
|
+
network.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}catch(e){
|
|
123
|
+
console.log(e);
|
|
124
|
+
try{ await connection.end(); } catch (e){ }
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
52
129
|
if (!isStreaming) {
|
|
53
130
|
emit(EventType.READY_MSB);
|
|
54
131
|
}
|
|
55
132
|
});
|
|
56
133
|
|
|
57
|
-
|
|
134
|
+
swarm.join(channel, { server: true, client: true });
|
|
58
135
|
await swarm.flush();
|
|
59
|
-
console.log('Joined channel');
|
|
60
|
-
async function refresh(){
|
|
61
|
-
await discovery.refresh();
|
|
62
|
-
setTimeout(function(){
|
|
63
|
-
refresh();
|
|
64
|
-
}, 30_000);
|
|
65
|
-
}
|
|
66
|
-
await refresh();
|
|
67
136
|
}
|
|
68
137
|
return swarm;
|
|
69
138
|
}
|
|
70
139
|
|
|
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
140
|
async pool(base) {
|
|
162
141
|
while (true) {
|
|
163
142
|
if (this.tx_pool.length > 0) {
|