trac-msb 0.0.80 → 0.0.82
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 +4 -1
- package/package.json +1 -1
- package/src/index.js +44 -37
- package/src/network.js +23 -5
- package/src/utils/check.js +1 -0
- package/src/utils/msgUtils.js +1 -0
package/Whitelist/pubkeys.csv
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
e247a9ceefb8a83e28d3c5845055910bc61e0b418c7578d297354708f276af04
|
|
2
|
-
580c44d7827fbeeb7778e4d2dce660b30f3ca3baa4de254ecd556dfb5b60fc35
|
|
2
|
+
580c44d7827fbeeb7778e4d2dce660b30f3ca3baa4de254ecd556dfb5b60fc35
|
|
3
|
+
a3590854889d059927fed4dfdf329b75447bc9f5885b6a843b317d1c2d38258a
|
|
4
|
+
dd2f83c9cb1d146c8be6c1d98822cbbb3f631a0659b765c9e4564960acf6e228
|
|
5
|
+
726b2885e551a98c20b37675b113476390f57321768a303e1b6c029ffca509a9
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -67,7 +67,7 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
67
67
|
this.#bee = null;
|
|
68
68
|
this.#swarm = null;
|
|
69
69
|
this.#dht_node = new DHT();
|
|
70
|
-
this.#dht_server =
|
|
70
|
+
this.#dht_server = null;
|
|
71
71
|
this.#base = null;
|
|
72
72
|
this.#writingKey = null;
|
|
73
73
|
this.#enable_txchannel = options.enable_txchannel !== false;
|
|
@@ -121,7 +121,6 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
121
121
|
for (const node of nodes) {
|
|
122
122
|
const op = node.value;
|
|
123
123
|
const handler = this.#getApplyOperationHandler(op.type);
|
|
124
|
-
|
|
125
124
|
if (handler) {
|
|
126
125
|
await handler(op, view, base, node, batch);
|
|
127
126
|
} else {
|
|
@@ -165,7 +164,7 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
165
164
|
if (!this.check.sanitizeAdminAndWritersOperations(op)) return;
|
|
166
165
|
const adminEntry = await batch.get(EntryType.ADMIN);
|
|
167
166
|
if (null === adminEntry) {
|
|
168
|
-
await this.#addAdminIfNotSet(op, view, node);
|
|
167
|
+
await this.#addAdminIfNotSet(op, view, node, batch);
|
|
169
168
|
}
|
|
170
169
|
else if (adminEntry.value.tracPublicKey === op.key) {
|
|
171
170
|
await this.#addAdminIfSet(adminEntry.value, op, view, base, batch);
|
|
@@ -188,7 +187,7 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
188
187
|
}
|
|
189
188
|
}
|
|
190
189
|
|
|
191
|
-
async #addAdminIfNotSet(op, view, node) {
|
|
190
|
+
async #addAdminIfNotSet(op, view, node, batch) {
|
|
192
191
|
const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key, op.value.wk, op.value.nonce, op.type));
|
|
193
192
|
|
|
194
193
|
if (node.from.key.toString('hex') === this.#bootstrap &&
|
|
@@ -233,8 +232,8 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
233
232
|
}
|
|
234
233
|
|
|
235
234
|
async #addWriter(op, batch, base) {
|
|
236
|
-
const nodeEntry = await
|
|
237
|
-
if (nodeEntry === null || !nodeEntry.isWriter) {
|
|
235
|
+
const nodeEntry = await batch.get(op.key);
|
|
236
|
+
if (nodeEntry === null || !nodeEntry.value.isWriter) {
|
|
238
237
|
await base.addWriter(b4a.from(op.value.wk, 'hex'), { isIndexer: false })
|
|
239
238
|
await batch.put(op.key, {
|
|
240
239
|
wk: op.value.wk,
|
|
@@ -246,8 +245,8 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
246
245
|
}
|
|
247
246
|
|
|
248
247
|
async #handleApplyRemoveWriterOperation(op, view, base, node, batch) {
|
|
249
|
-
const adminEntry = await
|
|
250
|
-
if (!this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry, node)) return;
|
|
248
|
+
const adminEntry = await batch.get(EntryType.ADMIN);
|
|
249
|
+
if (null === adminEntry || !this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry.value, node)) return;
|
|
251
250
|
const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key, op.value.wk, op.value.nonce, op.type));
|
|
252
251
|
if (isMessageVerifed) {
|
|
253
252
|
await this.#removeWriter(op, batch, base);
|
|
@@ -255,18 +254,19 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
255
254
|
}
|
|
256
255
|
|
|
257
256
|
async #removeWriter(op, batch, base) {
|
|
258
|
-
|
|
257
|
+
let nodeEntry = await batch.get(op.key)
|
|
259
258
|
if (nodeEntry !== null) {
|
|
259
|
+
nodeEntry = nodeEntry.value;
|
|
260
260
|
await base.removeWriter(b4a.from(nodeEntry.wk, 'hex'));
|
|
261
261
|
nodeEntry.isWriter = false;
|
|
262
262
|
if (nodeEntry.isIndexer) {
|
|
263
263
|
nodeEntry.isIndexer = false;
|
|
264
|
-
const indexersEntry = await
|
|
265
|
-
if (indexersEntry && indexersEntry.includes(op.key)) {
|
|
266
|
-
const idx = indexersEntry.indexOf(op.key);
|
|
264
|
+
const indexersEntry = await batch.get(EntryType.INDEXERS);
|
|
265
|
+
if (null !== indexersEntry && indexersEntry.value.includes(op.key)) {
|
|
266
|
+
const idx = indexersEntry.value.indexOf(op.key);
|
|
267
267
|
if (idx !== -1) {
|
|
268
|
-
indexersEntry.splice(idx, 1);
|
|
269
|
-
await batch.put(EntryType.INDEXERS, indexersEntry);
|
|
268
|
+
indexersEntry.value.splice(idx, 1);
|
|
269
|
+
await batch.put(EntryType.INDEXERS, indexersEntry.value);
|
|
270
270
|
}
|
|
271
271
|
}
|
|
272
272
|
}
|
|
@@ -281,27 +281,27 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
281
281
|
return;
|
|
282
282
|
}
|
|
283
283
|
|
|
284
|
-
const adminEntry = await
|
|
285
|
-
if (!this.#isAdmin(adminEntry, node)) return;
|
|
284
|
+
const adminEntry = await batch.get(EntryType.ADMIN);
|
|
285
|
+
if (null === adminEntry || !this.#isAdmin(adminEntry.value, node)) return;
|
|
286
286
|
|
|
287
287
|
if (!this.#isWhitelisted(op.key)) return;
|
|
288
288
|
|
|
289
|
-
const indexersEntry = await
|
|
290
|
-
if (
|
|
291
|
-
Array.from(indexersEntry).length >= MAX_INDEXERS) {
|
|
289
|
+
const indexersEntry = await batch.get(EntryType.INDEXERS);
|
|
290
|
+
if (null === indexersEntry || Array.from(indexersEntry.value).includes(op.key) ||
|
|
291
|
+
Array.from(indexersEntry.value).length >= MAX_INDEXERS) {
|
|
292
292
|
return;
|
|
293
293
|
}
|
|
294
|
-
const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
|
|
294
|
+
const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.value.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
|
|
295
295
|
if (isMessageVerifed) {
|
|
296
|
-
await this.#addIndexer(indexersEntry, op, batch, base);
|
|
296
|
+
await this.#addIndexer(indexersEntry.value, op, batch, base);
|
|
297
297
|
}
|
|
298
298
|
}
|
|
299
299
|
|
|
300
300
|
async #addIndexer(indexersEntry, op, batch, base) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
if (nodeEntry !== null && nodeEntry.isWriter && !nodeEntry.isIndexer) {
|
|
301
|
+
let nodeEntry = await batch.get(op.key);
|
|
304
302
|
|
|
303
|
+
if (nodeEntry !== null && nodeEntry.value.isWriter && !nodeEntry.value.isIndexer) {
|
|
304
|
+
nodeEntry = nodeEntry.value;
|
|
305
305
|
await base.removeWriter(b4a.from(nodeEntry.wk, 'hex'));
|
|
306
306
|
await base.addWriter(b4a.from(nodeEntry.wk, 'hex'), { isIndexer: true })
|
|
307
307
|
nodeEntry.isIndexer = true;
|
|
@@ -314,13 +314,15 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
314
314
|
|
|
315
315
|
async #handleApplyRemoveIndexerOperation(op, view, base, node, batch) {
|
|
316
316
|
if (!this.check.sanitizeIndexerOrWhitelistOperations(op)) return;
|
|
317
|
-
const adminEntry = await
|
|
318
|
-
|
|
319
|
-
if (!this.#isAdmin(adminEntry, node) ||
|
|
320
|
-
const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
|
|
317
|
+
const adminEntry = await batch.get(EntryType.ADMIN);
|
|
318
|
+
let indexersEntry = await batch.get(EntryType.INDEXERS);
|
|
319
|
+
if (null === adminEntry || !this.#isAdmin(adminEntry.value, node) || null === indexersEntry || !Array.from(indexersEntry.value).includes(op.key) || Array.from(indexersEntry.value).length <= 1) return;
|
|
320
|
+
const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.value.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
|
|
321
321
|
if (isMessageVerifed) {
|
|
322
|
-
|
|
323
|
-
if (nodeEntry !== null && nodeEntry.isWriter && nodeEntry.isIndexer) {
|
|
322
|
+
let nodeEntry = await batch.get(op.key);
|
|
323
|
+
if (nodeEntry !== null && nodeEntry.value.isWriter && nodeEntry.value.isIndexer) {
|
|
324
|
+
indexersEntry = indexersEntry.value;
|
|
325
|
+
nodeEntry = nodeEntry.value;
|
|
324
326
|
await base.removeWriter(b4a.from(nodeEntry.wk, 'hex'));
|
|
325
327
|
|
|
326
328
|
nodeEntry.isWriter = false;
|
|
@@ -355,7 +357,8 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
355
357
|
}
|
|
356
358
|
|
|
357
359
|
if (this.#enable_txchannel) {
|
|
358
|
-
|
|
360
|
+
this.#dht_server = this.#dht_node.createServer();
|
|
361
|
+
await Network.dhtServer(this, this.#dht_server, this.#base, this.#wallet, this.#writingKey, this.#network);
|
|
359
362
|
}
|
|
360
363
|
|
|
361
364
|
const adminEntry = await this.getSigned(EntryType.ADMIN);
|
|
@@ -412,11 +415,13 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
412
415
|
if (!adminEntry || !message) {
|
|
413
416
|
return;
|
|
414
417
|
}
|
|
415
|
-
this.#
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
}
|
|
418
|
+
const stream = this.#dht_node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
|
|
419
|
+
stream.on('connect', async function () {
|
|
420
|
+
await stream.send(b4a.from(JSON.stringify({ op : 'add_writer', message : message })));
|
|
419
421
|
});
|
|
422
|
+
stream.on('open', function () { });
|
|
423
|
+
stream.on('close', () => { });
|
|
424
|
+
stream.on('error', (error) => { });
|
|
420
425
|
}
|
|
421
426
|
|
|
422
427
|
async #verifyMessage(signature, publicKey, bufferMessage) {
|
|
@@ -432,7 +437,7 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
432
437
|
return !!(this.#wallet.publicKey === adminEntry.tracPublicKey && adminEntry.wk === this.#writingKey);
|
|
433
438
|
}
|
|
434
439
|
|
|
435
|
-
async
|
|
440
|
+
async isAllowedToRequestRole(key, adminEntry) {
|
|
436
441
|
const isWhitelisted = await this.#isWhitelisted(key);
|
|
437
442
|
return !!(isWhitelisted && !this.#isAdmin(adminEntry));
|
|
438
443
|
}
|
|
@@ -639,7 +644,7 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
639
644
|
const isAlreadyWriter = !!(nodeEntry && nodeEntry.isWriter)
|
|
640
645
|
let assembledMessage = null;
|
|
641
646
|
if (toAdd) {
|
|
642
|
-
const isAllowedToRequestRole = await this
|
|
647
|
+
const isAllowedToRequestRole = await this.isAllowedToRequestRole(this.#wallet.publicKey, adminEntry);
|
|
643
648
|
const canAddWriter = !!(!this.#base.writable && !isAlreadyWriter && isAllowedToRequestRole);
|
|
644
649
|
if (canAddWriter) {
|
|
645
650
|
assembledMessage = await MsgUtils.assembleAddWriterMessage(this.#wallet, this.#writingKey);
|
|
@@ -651,6 +656,8 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
651
656
|
}
|
|
652
657
|
}
|
|
653
658
|
|
|
659
|
+
console.log(assembledMessage);
|
|
660
|
+
|
|
654
661
|
if (assembledMessage) {
|
|
655
662
|
this.#sendMessageToAdmin(adminEntry, assembledMessage);
|
|
656
663
|
}
|
package/src/network.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import w from 'protomux-wakeup';
|
|
2
2
|
import b4a from 'b4a';
|
|
3
3
|
import Hyperswarm from 'hyperswarm';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
EventType,
|
|
6
|
+
TRAC_NAMESPACE,
|
|
7
|
+
MAX_PEERS,
|
|
8
|
+
MAX_PARALLEL,
|
|
9
|
+
MAX_SERVER_CONNECTIONS,
|
|
10
|
+
OperationType,
|
|
11
|
+
EntryType
|
|
12
|
+
} from './utils/constants.js';
|
|
5
13
|
import {sleep } from './utils/functions.js';
|
|
6
14
|
import MsgUtils from './utils/msgUtils.js';
|
|
7
15
|
import Check from './utils/check.js';
|
|
@@ -52,15 +60,25 @@ class Network {
|
|
|
52
60
|
return swarm;
|
|
53
61
|
}
|
|
54
62
|
|
|
55
|
-
static async dhtServer(dhtServer, base, wallet, writingKey, networkInstance){
|
|
63
|
+
static async dhtServer(msb, dhtServer, base, wallet, writingKey, networkInstance){
|
|
56
64
|
try{
|
|
57
65
|
dhtServer.on('connection', function (connection) {
|
|
58
66
|
connection.on('message', async (msg) => {
|
|
59
67
|
msg = b4a.toString(msg, 'utf-8');
|
|
68
|
+
msg = JSON.parse(msg);
|
|
60
69
|
if(msg === 'get_writer_key'){
|
|
61
70
|
await connection.send(b4a.from(JSON.stringify({op:'writer_key', key : writingKey})));
|
|
71
|
+
} else if(msg.op !== undefined && msg.message !== undefined && msg.op === 'add_writer'){
|
|
72
|
+
const adminEntry = await msb.getSigned(EntryType.ADMIN);
|
|
73
|
+
const nodeEntry = await msb.getSigned(msg.message.pk);
|
|
74
|
+
const isAlreadyWriter = !!(nodeEntry && nodeEntry.isWriter);
|
|
75
|
+
const isAllowedToRequestRole = await msb.isAllowedToRequestRole(msg.message.pk, adminEntry);
|
|
76
|
+
const canAddWriter = !!(!base.writable && !isAlreadyWriter && isAllowedToRequestRole);
|
|
77
|
+
if(null !== adminEntry && adminEntry.tracPublicKey === wallet.publicKey
|
|
78
|
+
&& msg.message.pk !== wallet.publicKey && canAddWriter){
|
|
79
|
+
await base.append(msg.message);
|
|
80
|
+
}
|
|
62
81
|
} else {
|
|
63
|
-
|
|
64
82
|
if (base.isIndexer || !base.writable) return;
|
|
65
83
|
|
|
66
84
|
// TODO: decide if a tx rejection should be responded with
|
|
@@ -72,7 +90,7 @@ class Network {
|
|
|
72
90
|
if (b4a.byteLength(msg) > 3072) return;
|
|
73
91
|
|
|
74
92
|
try {
|
|
75
|
-
const parsedPreTx =
|
|
93
|
+
const parsedPreTx = msg;
|
|
76
94
|
|
|
77
95
|
if (networkInstance.check.sanitizePreTx(parsedPreTx) &&
|
|
78
96
|
wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
|
|
@@ -113,7 +131,7 @@ class Network {
|
|
|
113
131
|
};
|
|
114
132
|
await dhtServer.listen(keyPair)
|
|
115
133
|
console.log('DHT node is listening on public key', wallet.publicKey);
|
|
116
|
-
} catch(e) { }
|
|
134
|
+
} catch(e) { console.log(e) }
|
|
117
135
|
}
|
|
118
136
|
|
|
119
137
|
async pool(base) {
|
package/src/utils/check.js
CHANGED
|
@@ -69,6 +69,7 @@ class Check {
|
|
|
69
69
|
value: {
|
|
70
70
|
$$strict: true,
|
|
71
71
|
$$type: "object",
|
|
72
|
+
pk: { type: 'is_hex_string', length: 64, required: true },
|
|
72
73
|
wk: { type: 'is_hex_string', length: 64, required: true },
|
|
73
74
|
nonce: { type: 'string', min: 1, max: 256, required: true },
|
|
74
75
|
sig: { type: 'is_hex_string', length: 128, required: true },
|
package/src/utils/msgUtils.js
CHANGED