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