trac-msb 0.0.93 → 0.0.95

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.
@@ -1,13 +1 @@
1
- 3e84de76f23d1f8f47aa5f133cb928ead44f91429df0b3f9761205049f2beeec
2
- bd3c9c5844187407baf418c3aa6329c364297b895f961a1f88a91bfe666f72c2
3
- 17e1856a71e2aef8be3dcb6776316cf90d339f8cab9bcbb3c356be2da86fd2e7
4
- ea866411e7d377efeae2eb1d243956e6a122330e697ff33d8312d1db48976658
5
- 30149ac3be7ab7c35c047f64cfa76c59ad7a3373fdc2ac568a05c3ed4169bb4f
6
- 49fd8429f26e61c7ae9382c763585c8085ac4d0c47649b3cb4b435f4b04f3a59
7
- 0e3831a32bd8b955be4050d7bcdacb38ec3b33cefd8e4f3c9921c98e1989f179
8
- eb3f93f89694374f5caabebb7499a218631e67e857197531e8422a70b038e992
9
- b675e900dce3d2d191a847230750ed98f86253888bc3ddd05c15de4f64c00881
10
- 714c6a9c4af41bfcf2c42a5f79ef85655ce398c4bbd070a03629d0a03d97dca5
11
- 35f12e13e447038d07e9ee2a1750084ca006368fced4e8e0c55d354e99b9571a
12
- 0fd54a89ea654011b800ed9f006deefdbdb68ddf1f0a392478cf7956f362adfe
13
- 1961a27b204d9bf8f1d0483103b3b510ed401d502e0b6b41dce399894f6924f1
1
+ 39bedb26635186cc6c9d9d2197db689c01710ba6696581ced49b97edea653cc6
package/msb.mjs CHANGED
@@ -4,6 +4,7 @@ const opts = {
4
4
  stores_directory : 'stores/',
5
5
  store_name : typeof process !== "undefined" ? process.argv[2] : Pear.config.args[0],
6
6
  bootstrap: '4577ebf1e84c06d48b2caa33fe54ae58be3a79cb30810129d58b725fae412e37',
7
+ enable_updater : true,
7
8
  channel: '00axtracnetworkmainsettlementbus',
8
9
  tx : 'axtracnetworkmainsettlementbustx'
9
10
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.0.93",
4
+ "version": "0.0.95",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
package/src/index.js CHANGED
@@ -58,7 +58,6 @@ export class MainSettlementBus extends ReadyResource {
58
58
  }
59
59
 
60
60
  #initInternalAttributes(options) {
61
- //TODO: change visibility of the attributes to private. Most of them should be internal.
62
61
  this.#STORES_DIRECTORY = options.stores_directory;
63
62
  this.#KEY_PAIR_PATH = `${this.STORES_DIRECTORY}${options.store_name}/db/keypair.json`
64
63
  this.#bootstrap = options.bootstrap || null;
@@ -67,7 +66,7 @@ export class MainSettlementBus extends ReadyResource {
67
66
  this.#store = new Corestore(this.STORES_DIRECTORY + options.store_name);
68
67
  this.#bee = null;
69
68
  this.#swarm = null;
70
- 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'];
69
+ 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'];
71
70
  this.#dht_node = new DHT({ bootstrap: this.#dht_bootstrap });
72
71
  this.#dht_server = null;
73
72
  this.#base = null;
@@ -208,13 +207,11 @@ export class MainSettlementBus extends ReadyResource {
208
207
 
209
208
  async #handleApplyAppendWhitelistOperation(op, view, base, node, batch) {
210
209
  const adminEntry = await batch.get(EntryType.ADMIN);
211
- if (null === adminEntry || !this.check.sanitizeIndexerOrWhitelistOperations(op) /* TODO: This cannot work, needs to be signed, not checked against wallet!!! || !this.#isAdmin(adminEntry.value, node)*/) return;
212
- // TODO: is the below an admin signature?
210
+ if (null === adminEntry || !this.check.sanitizeIndexerOrWhitelistOperations(op) || !this.#isAdmin(adminEntry.value, node)) return;
211
+ // TODO: is the below an admin signature? - yes
213
212
  const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.value.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type));
214
- console.log('isMessageVerifed', isMessageVerifed);
215
213
  if (!isMessageVerifed) return;
216
214
  const isWhitelisted = await this.#isWhitelisted2(op.key, batch);
217
- console.log('iswhitelisted', isWhitelisted);
218
215
  if (isWhitelisted) return;
219
216
  await this.#createWhitelistEntry(batch, op.key);
220
217
  }
@@ -226,12 +223,12 @@ export class MainSettlementBus extends ReadyResource {
226
223
 
227
224
  async #handleApplyAddWriterOperation(op, view, base, node, batch) {
228
225
  const adminEntry = await batch.get(EntryType.ADMIN);
229
- if (null === adminEntry || !this.check.sanitizeAdminAndWritersOperations(op) /*TODO: this cannot work, needs to be signed, not checked against wallet!!! || !this.#isAdmin(adminEntry.value, node)*/) return;
226
+ if (null === adminEntry || !this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry.value, node)) return;
230
227
 
231
228
  const isWhitelisted = await this.#isWhitelisted2(op.key, batch);
232
229
  if (!isWhitelisted || op.key !== op.value.pub) return;
233
230
  // TODO: if the below is not a message signed by admin BUT this handler is supposed to be executed by the admin, then use admin signatures in apply!
234
- const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key, op.value.wk, op.value.nonce, op.type));
231
+ const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key, op.value.pub, op.value.wk, op.value.nonce, op.type));
235
232
  if (isMessageVerifed) {
236
233
  await this.#addWriter(op, batch, base);
237
234
  }
@@ -261,9 +258,9 @@ export class MainSettlementBus extends ReadyResource {
261
258
 
262
259
  async #handleApplyRemoveWriterOperation(op, view, base, node, batch) {
263
260
  const adminEntry = await batch.get(EntryType.ADMIN);
264
- if (null === adminEntry || !this.check.sanitizeAdminAndWritersOperations(op) /*TODO: this cannot work, needs to be signed, not checked against wallet!!! || !this.#isAdmin(adminEntry.value, node)*/) return;
261
+ if (null === adminEntry || !this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry.value, node)) return;
265
262
  // TODO: if the below is not a message signed by admin BUT this handler is supposed to be executed by the admin, then use admin signatures in apply!
266
- const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key, op.value.wk, op.value.nonce, op.type));
263
+ const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key,op.value.pub, op.value.wk, op.value.nonce, op.type));
267
264
  if (isMessageVerifed) {
268
265
  await this.#removeWriter(op, batch, base);
269
266
  }
@@ -298,7 +295,7 @@ export class MainSettlementBus extends ReadyResource {
298
295
  }
299
296
 
300
297
  const adminEntry = await batch.get(EntryType.ADMIN);
301
- if (null === adminEntry /* TODO: this cannot work!!!! || !this.#isAdmin(adminEntry.value, node)*/) return;
298
+ if (null === adminEntry || !this.#isAdmin(adminEntry.value, node)) return;
302
299
 
303
300
  if (!this.#isWhitelisted2(op.key, batch)) return;
304
301
 
@@ -307,7 +304,7 @@ export class MainSettlementBus extends ReadyResource {
307
304
  Array.from(indexersEntry.value).length >= MAX_INDEXERS) {
308
305
  return;
309
306
  }
310
- // TODO: is the below an admin signature?
307
+ // TODO: is the below an admin signature? -yes
311
308
  const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.value.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
312
309
  if (isMessageVerifed) {
313
310
  await this.#addIndexer(indexersEntry.value, op, batch, base);
@@ -333,8 +330,8 @@ export class MainSettlementBus extends ReadyResource {
333
330
  if (!this.check.sanitizeIndexerOrWhitelistOperations(op)) return;
334
331
  const adminEntry = await batch.get(EntryType.ADMIN);
335
332
  let indexersEntry = await batch.get(EntryType.INDEXERS);
336
- if (null === adminEntry /* TODO: this cannot work!!! || !this.#isAdmin(adminEntry.value, node) */ || null === indexersEntry || !Array.from(indexersEntry.value).includes(op.key) || Array.from(indexersEntry.value).length <= 1) return;
337
- // TODO: is the below an admin signature?
333
+ if (null === adminEntry || !this.#isAdmin(adminEntry.value, node) || null === indexersEntry || !Array.from(indexersEntry.value).includes(op.key) || Array.from(indexersEntry.value).length <= 1) return;
334
+ // TODO: is the below an admin signature? -yes
338
335
  const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.value.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
339
336
  if (isMessageVerifed) {
340
337
  let nodeEntry = await batch.get(op.key);
@@ -379,7 +376,7 @@ export class MainSettlementBus extends ReadyResource {
379
376
  await Network.dhtServer(this, this.#dht_server, this.#base, this.#wallet, this.#writingKey, this.#network);
380
377
  }
381
378
 
382
- const adminEntry = await this.getSigned(EntryType.ADMIN);
379
+ const adminEntry = await this.get(EntryType.ADMIN);
383
380
 
384
381
  if (this.#isAdmin(adminEntry)) {
385
382
  this.#shouldListenToAdminEvents = true;
@@ -391,7 +388,7 @@ export class MainSettlementBus extends ReadyResource {
391
388
 
392
389
  //await this.#setUpRoleAutomatically(adminEntry);
393
390
 
394
- if (this.#enable_updater) {
391
+ if (this.#enable_updater && this.#base.writable) {
395
392
  this.updater();// TODO: NODE AFTER BECOMING A writer should start the updater
396
393
  }
397
394
 
@@ -452,9 +449,11 @@ export class MainSettlementBus extends ReadyResource {
452
449
  }
453
450
 
454
451
  #isAdmin(adminEntry, node = null) {
455
- if(this.#enable_wallet === false) return false;
452
+ //on-chain
456
453
  if (!adminEntry) return false;
457
454
  if (node) return adminEntry.wk === b4a.from(node.from.key).toString('hex');
455
+ //off-chain
456
+ if (this.#enable_wallet === false) return false;
458
457
  return !!(this.#wallet.publicKey === adminEntry.tracPublicKey && adminEntry.wk === this.#writingKey);
459
458
  }
460
459
 
@@ -501,7 +500,7 @@ export class MainSettlementBus extends ReadyResource {
501
500
  }
502
501
 
503
502
  async getWhitelistEntry(key) {
504
- const entry = await this.getSigned(WHITELIST_PREFIX + key);
503
+ const entry = await this.get(WHITELIST_PREFIX + key);
505
504
  return entry
506
505
  }
507
506
 
@@ -542,7 +541,7 @@ export class MainSettlementBus extends ReadyResource {
542
541
  });
543
542
 
544
543
  this.#base.on(EventType.WRITABLE, async () => {
545
- const updatedNodeEntry = await this.getSigned(this.#wallet.publicKey);
544
+ const updatedNodeEntry = await this.get(this.#wallet.publicKey);
546
545
  const canEnableWriterEvents = updatedNodeEntry &&
547
546
  updatedNodeEntry.wk === this.#writingKey &&
548
547
  !this.#shouldListenToWriterEvents;
@@ -559,7 +558,7 @@ export class MainSettlementBus extends ReadyResource {
559
558
  console.log('Current node is unwritable');
560
559
  return;
561
560
  }
562
- const updatedNodeEntry = await this.getSigned(this.#wallet.publicKey);
561
+ const updatedNodeEntry = await this.get(this.#wallet.publicKey);
563
562
  const canDisableWriterEvents = updatedNodeEntry &&
564
563
  !updatedNodeEntry.isWriter &&
565
564
  this.#shouldListenToWriterEvents;
@@ -586,7 +585,7 @@ export class MainSettlementBus extends ReadyResource {
586
585
  async #writerEventListener() {
587
586
  this.on(EventType.WRITER_EVENT, async (parsedRequest) => {
588
587
  if(this.#enable_wallet === false) return;
589
- const adminEntry = await this.getSigned(EntryType.ADMIN);
588
+ const adminEntry = await this.get(EntryType.ADMIN);
590
589
  const isEventMessageVerifed = await MsgUtils.verifyEventMessage(parsedRequest, this.#wallet)
591
590
  if (adminEntry && adminEntry.tracPublicKey === parsedRequest.key && isEventMessageVerifed) {
592
591
  await this.#base.append(parsedRequest);
@@ -604,7 +603,7 @@ export class MainSettlementBus extends ReadyResource {
604
603
 
605
604
  async #handleAdminOperations() {
606
605
  //TODO: ADJUST FOR WHITELIST STRUCTURE
607
- const adminEntry = await this.getSigned(EntryType.ADMIN);
606
+ const adminEntry = await this.get(EntryType.ADMIN);
608
607
  const addAdminMessage = await MsgUtils.assembleAdminMessage(adminEntry, this.#writingKey, this.#wallet, this.#bootstrap);
609
608
  if (!adminEntry && this.#wallet && this.#writingKey && this.#writingKey === this.#bootstrap) {
610
609
  await this.#base.append(addAdminMessage);
@@ -613,7 +612,7 @@ export class MainSettlementBus extends ReadyResource {
613
612
  for (const conn of this.#swarm.connections) {
614
613
 
615
614
  const remotePublicKeyHex = b4a.from(conn.remotePublicKey).toString('hex');
616
- const remotePublicKeyEntry = await this.getSigned(remotePublicKeyHex);
615
+ const remotePublicKeyEntry = await this.get(remotePublicKeyHex);
617
616
  const isWhitelisted = await this.#isWhitelisted(remotePublicKeyHex);
618
617
 
619
618
  if (conn.connected &&
@@ -632,7 +631,7 @@ export class MainSettlementBus extends ReadyResource {
632
631
  }
633
632
 
634
633
  setTimeout(async () => {
635
- const updatedAdminEntry = await this.getSigned(EntryType.ADMIN);
634
+ const updatedAdminEntry = await this.get(EntryType.ADMIN);
636
635
  if (this.#isAdmin(updatedAdminEntry) && !this.#shouldListenToAdminEvents) {
637
636
  this.#shouldListenToAdminEvents = true;
638
637
  this.#adminEventListener();
@@ -643,7 +642,7 @@ export class MainSettlementBus extends ReadyResource {
643
642
 
644
643
  async #handleWhitelistOperations() {
645
644
  if(this.#enable_wallet === false) return;
646
- const adminEntry = await this.getSigned(EntryType.ADMIN);
645
+ const adminEntry = await this.get(EntryType.ADMIN);
647
646
  if (!this.#isAdmin(adminEntry)) return;
648
647
 
649
648
  const assembledWhitelistMessages = await MsgUtils.assembleWhitelistMessages(adminEntry, this.#wallet);
@@ -675,8 +674,8 @@ export class MainSettlementBus extends ReadyResource {
675
674
 
676
675
  async #requestWriterRole(toAdd) {
677
676
  if(this.#enable_wallet === false) return;
678
- const adminEntry = await this.getSigned(EntryType.ADMIN);
679
- const nodeEntry = await this.getSigned(this.#wallet.publicKey);
677
+ const adminEntry = await this.get(EntryType.ADMIN);
678
+ const nodeEntry = await this.get(this.#wallet.publicKey);
680
679
  const isAlreadyWriter = !!(nodeEntry && nodeEntry.isWriter)
681
680
  let assembledMessage = null;
682
681
  if (toAdd) {
@@ -699,16 +698,16 @@ export class MainSettlementBus extends ReadyResource {
699
698
 
700
699
  async #updateIndexerRole(tracPublicKey, toAdd) {
701
700
  if(this.#enable_wallet === false) return;
702
- const adminEntry = await this.getSigned(EntryType.ADMIN);
701
+ const adminEntry = await this.get(EntryType.ADMIN);
703
702
  if (!this.#isAdmin(adminEntry) && !this.#base.writable) return;
704
703
 
705
704
  const isWhitelisted = await this.#isWhitelisted(tracPublicKey);
706
705
  if (!isWhitelisted) return;
707
706
 
708
- const nodeEntry = await this.getSigned(tracPublicKey);
707
+ const nodeEntry = await this.get(tracPublicKey);
709
708
  if (!nodeEntry || !nodeEntry.isWriter) return;
710
709
 
711
- const indexersEntry = await this.getSigned(EntryType.INDEXERS);
710
+ const indexersEntry = await this.get(EntryType.INDEXERS);
712
711
 
713
712
  if (toAdd) {
714
713
  const canAddIndexer = !nodeEntry.isIndexer && indexersEntry.length <= MAX_INDEXERS;
@@ -791,9 +790,9 @@ export class MainSettlementBus extends ReadyResource {
791
790
  case '/show':
792
791
  // /get_node_info 9da99d98f02f24bdb13d46ba5d346c9a3eda03c18ab6e1441b7bac9743cf0bcc1
793
792
  // Only for DEBUG
794
- const admin = await this.getSigned(EntryType.ADMIN);
793
+ const admin = await this.get(EntryType.ADMIN);
795
794
  console.log('Admin:', admin);
796
- const indexers = await this.getSigned(EntryType.INDEXERS);
795
+ const indexers = await this.get(EntryType.INDEXERS);
797
796
  console.log('Indexers:', indexers);
798
797
  break;
799
798
  case '/dag':
package/src/network.js CHANGED
@@ -83,9 +83,9 @@ class Network {
83
83
  } else if(msg.op !== undefined && msg.message !== undefined && msg.op === 'add_writer'){
84
84
  await connection.destroy();
85
85
  msg = msg.message;
86
- const adminEntry = await msb.getSigned(EntryType.ADMIN);
86
+ const adminEntry = await msb.get(EntryType.ADMIN);
87
87
  if(null === adminEntry || (adminEntry.tracPublicKey !== wallet.publicKey)) return;
88
- const nodeEntry = await msb.getSigned(msg.value.pub);
88
+ const nodeEntry = await msb.get(msg.value.pub);
89
89
  const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
90
90
  const isAllowedToRequestRole = await msb._isAllowedToRequestRole(msg.value.pub, adminEntry);
91
91
  const canAddWriter = base.writable && !isAlreadyWriter && isAllowedToRequestRole;
@@ -13,7 +13,7 @@ export class WriterManager extends ReadyResource {
13
13
 
14
14
  async addAdmin() {
15
15
  // case where admin entry doesn't exist yet and we have to autorize Admin public key only with bootstrap writing key
16
- const adminEntry = await this.msbInstance.getSigned('admin');
16
+ const adminEntry = await this.msbInstance.get('admin');
17
17
  if (!adminEntry && this.msbInstance.writingKey && this.msbInstance.writingKey === this.msbInstance.bootstrap) {
18
18
 
19
19
  const nonce = Math.random() + '-' + Date.now();
@@ -44,7 +44,7 @@ export class WriterManager extends ReadyResource {
44
44
  try {
45
45
  //TODO: IMPORTANT - IF WE GONNA STORE ~ 2K-10K PUBLIC KEYS IN THE LIST, WE NEED TO SPLIT IT INTO CHUNKS
46
46
  // ONE CHUNK WILL BE ~100 PUBLIC KEYS + NONCE + SIG AND ADDITIONAL BYTES < 4096 BYTES. ADMIN WILL NEED TO PERFORM MULTIPLE APPENDS. FOR NOW THIS IS NOT IMPLEMENTED.
47
- const adminEntry = await this.msbInstance.getSigned('admin');
47
+ const adminEntry = await this.msbInstance.get('admin');
48
48
 
49
49
  if (adminEntry && this.msbInstance.wallet.publicKey === Buffer.from(adminEntry.tpk.data).toString('hex')) {
50
50