trac-msb 0.0.64 → 0.0.66

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.
Files changed (3) hide show
  1. package/msb.mjs +1 -1
  2. package/package.json +1 -1
  3. package/src/index.js +15 -7
package/msb.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import MainSettlementBus from './src/index.js';
1
+ import {MainSettlementBus} from './src/index.js';
2
2
 
3
3
  const opts = {
4
4
  stores_directory : 'stores/',
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.0.64",
4
+ "version": "0.0.66",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
package/src/index.js CHANGED
@@ -14,8 +14,7 @@ import Network from './network.js';
14
14
  import Check from './utils/check.js';
15
15
  //TODO: CHANGE NONCE.
16
16
 
17
-
18
- class MainSettlementBus extends ReadyResource {
17
+ export class MainSettlementBus extends ReadyResource {
19
18
  // Internal flags
20
19
  #shouldListenToAdminEvents = false;
21
20
  #shouldListenToWriterEvents = false;
@@ -68,7 +67,7 @@ class MainSettlementBus extends ReadyResource {
68
67
  this.#enable_txchannel = options.enable_txchannel !== false;
69
68
  this.#enable_updater = options.enable_updater !== false;
70
69
  this.#enable_wallet = options.enable_wallet !== false;
71
- this.#wallet = this.#enable_wallet ? new PeerWallet(options) : null;
70
+ this.#wallet = new PeerWallet(options);
72
71
  this.#replicate = options.replicate !== false;
73
72
  this.#opts = options;
74
73
  }
@@ -415,10 +414,10 @@ class MainSettlementBus extends ReadyResource {
415
414
  }
416
415
 
417
416
  #isAdmin(adminEntry, node = null) {
417
+ if(this.#enable_wallet === false) return false;
418
418
  if (!adminEntry) return false;
419
419
  if (node) return adminEntry.wk === b4a.from(node.from.key).toString('hex');
420
420
  return !!(this.#wallet.publicKey === adminEntry.tracPublicKey && adminEntry.wk === this.#writingKey);
421
-
422
421
  }
423
422
 
424
423
  async #isAllowedToRequestRole(key, adminEntry) {
@@ -464,7 +463,7 @@ class MainSettlementBus extends ReadyResource {
464
463
  const parsedRequest = JSON.parse(bufferData);
465
464
  if (parsedRequest && parsedRequest.type && parsedRequest.key && parsedRequest.value) {
466
465
  if (parsedRequest.type === OperationType.ADD_WRITER || parsedRequest.type === OperationType.REMOVE_WRITER) {
467
- //This request must be hanlded by ADMIN
466
+ //This request must be hanlded by ADMIN
468
467
  this.emit(EventType.ADMIN_EVENT, parsedRequest);
469
468
  } else if (parsedRequest.type === OperationType.ADD_ADMIN) {
470
469
  //This request must be handled by WRITER
@@ -503,6 +502,10 @@ class MainSettlementBus extends ReadyResource {
503
502
  });
504
503
 
505
504
  this.#base.on(EventType.UNWRITABLE, async () => {
505
+ if(this.#enable_wallet === false) {
506
+ console.log('Current node is unwritable');
507
+ return;
508
+ }
506
509
  const updatedNodeEntry = await this.getSigned(this.#wallet.publicKey);
507
510
  const canDisableWriterEvents = updatedNodeEntry &&
508
511
  !updatedNodeEntry.isWriter &&
@@ -518,6 +521,7 @@ class MainSettlementBus extends ReadyResource {
518
521
 
519
522
  async #adminEventListener() {
520
523
  this.on(EventType.ADMIN_EVENT, async (parsedRequest) => {
524
+ if(this.#enable_wallet === false) return;
521
525
  const isWhitelisted = await this.#isWhitelisted(parsedRequest.key);
522
526
  const isEventMessageVerifed = await MsgUtils.verifyEventMessage(parsedRequest, this.#wallet)
523
527
  if (isWhitelisted && isEventMessageVerifed) {
@@ -528,6 +532,7 @@ class MainSettlementBus extends ReadyResource {
528
532
 
529
533
  async #writerEventListener() {
530
534
  this.on(EventType.WRITER_EVENT, async (parsedRequest) => {
535
+ if(this.#enable_wallet === false) return;
531
536
  const adminEntry = await this.getSigned(EntryType.ADMIN);
532
537
  const isEventMessageVerifed = await MsgUtils.verifyEventMessage(parsedRequest, this.#wallet)
533
538
  if (adminEntry && adminEntry.tracPublicKey === parsedRequest.key && isEventMessageVerifed) {
@@ -550,7 +555,7 @@ class MainSettlementBus extends ReadyResource {
550
555
  const addAdminMessage = await MsgUtils.assembleAdminMessage(adminEntry, this.#writingKey, this.#wallet, this.#bootstrap);
551
556
  if (!adminEntry && this.#wallet && this.#writingKey && this.#writingKey === this.#bootstrap) {
552
557
  await this.#base.append(addAdminMessage);
553
- } else if (adminEntry && adminEntry.tracPublicKey === this.#wallet.publicKey && this.#writingKey && this.#writingKey !== adminEntry.wk) {
558
+ } else if (adminEntry && this.#wallet && adminEntry.tracPublicKey === this.#wallet.publicKey && this.#writingKey && this.#writingKey !== adminEntry.wk) {
554
559
  let connections = [];
555
560
  for (const conn of this.#swarm.connections) {
556
561
 
@@ -570,7 +575,7 @@ class MainSettlementBus extends ReadyResource {
570
575
  if (connections.length > 0) {
571
576
  connections[Math.floor(Math.random() * connections.length)].write(JSON.stringify(addAdminMessage));
572
577
  }
573
- //TODO: Implement an algorithm to search a new writer and connect/send the request for it.
578
+ //TODO: Implement an algorithm to search a new writer and connect/send the request for it.
574
579
  }
575
580
 
576
581
  setTimeout(async () => {
@@ -584,6 +589,7 @@ class MainSettlementBus extends ReadyResource {
584
589
  }
585
590
 
586
591
  async #handleWhitelistOperations() {
592
+ if(this.#enable_wallet === false) return;
587
593
  const adminEntry = await this.getSigned(EntryType.ADMIN);
588
594
  if (!this.#isAdmin(adminEntry)) return;
589
595
 
@@ -615,6 +621,7 @@ class MainSettlementBus extends ReadyResource {
615
621
  }
616
622
 
617
623
  async #requestWriterRole(toAdd) {
624
+ if(this.#enable_wallet === false) return;
618
625
  const adminEntry = await this.getSigned(EntryType.ADMIN);
619
626
  const nodeEntry = await this.getSigned(this.#wallet.publicKey);
620
627
  const isAlreadyWriter = !!(nodeEntry && nodeEntry.isWriter)
@@ -639,6 +646,7 @@ class MainSettlementBus extends ReadyResource {
639
646
  }
640
647
 
641
648
  async #updateIndexerRole(tracPublicKey, toAdd) {
649
+ if(this.#enable_wallet === false) return;
642
650
  const adminEntry = await this.getSigned(EntryType.ADMIN);
643
651
  if (!this.#isAdmin(adminEntry) && !this.#base.writable) return;
644
652