trac-msb 0.1.8 → 0.1.9

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/msb.mjs CHANGED
@@ -10,6 +10,5 @@ const opts = {
10
10
  const msb = new MainSettlementBus(opts);
11
11
 
12
12
  msb.ready().then(() => {
13
- console.log('MSB is ready.');
14
13
  msb.interactiveMode();
15
14
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.1.8",
4
+ "version": "0.1.9",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
@@ -12,7 +12,7 @@
12
12
  "test": "brittle test/*.test.js"
13
13
  },
14
14
  "dependencies": {
15
- "trac-wallet": "^0.0.40",
15
+ "trac-wallet": "^0.0.42",
16
16
  "hyperbee": "2.24.2",
17
17
  "hypercore": "11.5.0",
18
18
  "corestore": "7.4.0",
package/src/index.js CHANGED
@@ -26,8 +26,6 @@ import Network from './network.js';
26
26
  import Check from './utils/check.js';
27
27
  import DHT from 'hyperdht'
28
28
 
29
- //TODO: CHANGE NONCE.
30
-
31
29
  export class MainSettlementBus extends ReadyResource {
32
30
  // Internal flags
33
31
  #shouldListenToAdminEvents = false;
@@ -381,13 +379,14 @@ export class MainSettlementBus extends ReadyResource {
381
379
  if (this.#enable_wallet) {
382
380
  await this.#wallet.initKeyPair(this.KEY_PAIR_PATH, this.#readline_instance);
383
381
  }
384
- console.log('View Length:', this.#base.view.core.length);
385
- console.log('View Signed Length:', this.#base.view.core.signedLength);
386
- console.log('MSB Key:', b4a.from(this.#base.view.core.key).toString('hex'));
387
382
 
383
+ console.log('');
384
+ console.log('#####################################################################################');
385
+ console.log('# MSB Address: ', this.#wallet.publicKey, '#');
388
386
  this.#writingKey = b4a.toString(this.#base.local.key, 'hex');
389
- console.log('Writer Key:', this.#writingKey);
390
-
387
+ console.log('# MSB Writer: ', this.#writingKey, '#');
388
+ console.log('#####################################################################################');
389
+ console.log('');
391
390
  if (this.#replicate) {
392
391
  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));
393
392
  this.#dht_node = this.#swarm.dht;
@@ -411,8 +410,9 @@ export class MainSettlementBus extends ReadyResource {
411
410
 
412
411
  console.log(`isIndexer: ${this.#base.isIndexer}`);
413
412
  console.log(`isWriter: ${this.#base.writable}`);
414
- console.log('View Length:', this.#base.view.core.length);
415
- console.log('View Signed Length:', this.#base.view.core.signedLength);
413
+ console.log('MSB Unsigned Length:', this.#base.view.core.length);
414
+ console.log('MSB Signed Length:', this.#base.view.core.signedLength);
415
+ console.log('');
416
416
  }
417
417
 
418
418
  async close() {
@@ -426,7 +426,6 @@ export class MainSettlementBus extends ReadyResource {
426
426
  async #setUpRoleAutomatically() {
427
427
  if (!this.#base.writable) {
428
428
  await this.#requestWriterRole(false)
429
- console.log('Wait please...');
430
429
  setTimeout(async () => {
431
430
  await this.#requestWriterRole(true)
432
431
  }, 5_000);
@@ -747,22 +746,31 @@ export class MainSettlementBus extends ReadyResource {
747
746
  await this.#requestWriterRole(false);
748
747
  }
749
748
 
749
+ printHelp(){
750
+ console.log('Available commands:');
751
+ console.log('- /add_writer: add yourself as validator to this MSB once whitelisted.');
752
+ console.log('- /remove_writer: remove yourself from this MSB.');
753
+ console.log('- /add_admin: register admin entry with bootstrap key. (initial setup)');
754
+ console.log('- /add_whitelist: add all specified whitelist addresses. (admin only)');
755
+ console.log('- /add_indexer <address>: change a role of the selected writer node to indexer role. (admin only)');
756
+ console.log('- /remove_indexer <address>: change a role of the selected indexer node to default role. (admin only)');
757
+ console.log('- /get_node_info <address>: get information about a node with the given address.');
758
+ console.log('- /dag: check system properties such as writing key, DAG, etc.');
759
+ console.log('- /exit: Exit the program.');
760
+ console.log('- /help: display this help.');
761
+ }
762
+
750
763
  async interactiveMode() {
751
764
  if(this.#readline_instance === null) return;
752
765
  const rl = this.#readline_instance;
753
766
 
754
- console.log('MSB started. Available commands:');
755
- console.log('- /add_admin: register admin entry with bootstrap key.');
756
- console.log('- /add_whitelist: add a list of Trac public keys. Nodes that own these public keys can become writers.');
757
- console.log('- /add_indexer <trac_public_key>: change a role of the selected writer node to indexer role');
758
- console.log('- /push_writer_add: try to enforce adding this peer as writer after whitelisting.');
759
- console.log('- /remove_indexer <trac_public_key>: change a role of the selected indexer node to default role');
760
- console.log('- /get_node_info <trac_public_key>: get information about a node with the given Trac public key');
761
- console.log('- /dag: check system properties such as writing key, DAG, etc.');
762
- console.log('- /exit: Exit the program');
767
+ this.printHelp();
763
768
 
764
769
  rl.on('line', async (input) => {
765
770
  switch (input) {
771
+ case '/help':
772
+ this.printHelp();
773
+ break;
766
774
  case '/exit':
767
775
  console.log('Exiting...');
768
776
  rl.close();
@@ -785,15 +793,12 @@ export class MainSettlementBus extends ReadyResource {
785
793
  await this.#handleRemoveWriterOperation();
786
794
  break
787
795
  case '/flags':
788
- // Only for DEBUG
789
796
  console.log("shouldListenToAdminEvents: ", this.#shouldListenToAdminEvents);
790
797
  console.log("shouldListenToWriterEvents: ", this.#shouldListenToWriterEvents);
791
798
  console.log("isWritable: ", this.#base.writable);
792
799
  console.log("isIndexer: ", this.#base.isIndexer);
793
800
  break
794
801
  case '/show':
795
- // /get_node_info 9da99d98f02f24bdb13d46ba5d346c9a3eda03c18ab6e1441b7bac9743cf0bcc1
796
- // Only for DEBUG
797
802
  const admin = await this.get(EntryType.ADMIN);
798
803
  console.log('Admin:', admin);
799
804
  const indexers = await this.get(EntryType.INDEXERS);
package/src/network.js CHANGED
@@ -12,8 +12,9 @@ import {
12
12
  EntryType
13
13
  } from './utils/constants.js';
14
14
  import {sleep } from './utils/functions.js';
15
- import MsgUtils from './utils/msgUtils.js';
16
15
  import Check from './utils/check.js';
16
+ import Wallet from 'trac-wallet';
17
+
17
18
  const wakeup = new w();
18
19
 
19
20
  class Network {
@@ -99,7 +100,7 @@ class Network {
99
100
  parsedPreTx.wp === wallet.publicKey &&
100
101
  null === await base.view.get(parsedPreTx.tx)
101
102
  ) {
102
- const nonce = MsgUtils.generateNonce();
103
+ const nonce = Wallet.generateNonce().toString('hex');
103
104
  const signature = wallet.sign(b4a.from(parsedPreTx.tx + nonce), b4a.from(wallet.secretKey, 'hex'));
104
105
  const append_tx = {
105
106
  op: OperationType.POST_TX,
@@ -71,7 +71,7 @@ class Check {
71
71
  $$type: "object",
72
72
  pub: { type: 'is_hex_string', length: 64, required: true },
73
73
  wk: { type: 'is_hex_string', length: 64, required: true },
74
- nonce: { type: 'string', min: 1, max: 256, required: true },
74
+ nonce: { type: 'is_hex_string', length: 64, required: true },
75
75
  sig: { type: 'is_hex_string', length: 128, required: true },
76
76
 
77
77
  }
@@ -91,7 +91,7 @@ class Check {
91
91
  value: {
92
92
  $$strict: true,
93
93
  $$type: "object",
94
- nonce: { type: 'string', min: 1, max: 256, required: true },
94
+ nonce: { type: 'is_hex_string', length: 64, required: true },
95
95
  sig: { type: 'is_hex_string', length: 128, required: true },
96
96
 
97
97
  }
@@ -113,7 +113,7 @@ class Check {
113
113
  i: { type: 'is_hex_string', length: 64, required: true },
114
114
  ipk: { type: 'is_hex_string', length: 64, required: true },
115
115
  ch: { type: 'is_hex_string', required: true }, // TODO: if we will use only 256 bit hash then change to length: 64
116
- in: { type: 'string', min: 1, max: 256, required: true },
116
+ in: { type: 'is_hex_string', length: 64, required: true },
117
117
  bs: { type: 'is_hex_string', length: 64, required: true },
118
118
  mbs: { type: 'is_hex_string', length: 64, required: true },
119
119
  };
@@ -139,12 +139,12 @@ class Check {
139
139
  i: { type: 'is_hex_string', length: 64, required: true },
140
140
  ipk: { type: 'is_hex_string', length: 64, required: true },
141
141
  ch: { type: 'is_hex_string', required: true }, // TODO: if we will use only 256 bit hash then change to length: 64
142
- in: { type: 'string', min: 1, max: 256, required: true },
142
+ in: { type: 'is_hex_string', length: 64, required: true },
143
143
  bs: { type: 'is_hex_string', length: 64, required: true },
144
144
  mbs: { type: 'is_hex_string', length: 64, required: true },
145
145
  ws: { type: 'is_hex_string', length: 128, required: true },
146
146
  wp: { type: 'is_hex_string', length: 64, required: true },
147
- wn: { type: 'string', min: 1, required: true }
147
+ wn: { type: 'is_hex_string', length: 64, required: true }
148
148
  }
149
149
  };
150
150
  return this.#_validator.compile(schema);
@@ -19,7 +19,6 @@ export async function verifyDag(base) {
19
19
  //console.log("this.base.system.core", this.base.system.core);
20
20
  console.log(`base.key: ${base.key.toString('hex')}`);
21
21
  console.log('discoveryKey:', b4a.toString(base.discoveryKey, 'hex'));
22
-
23
22
  console.log(`VIEW Dag: ${dagView.toString('hex')} (length: ${lengthdagView})`);
24
23
  console.log(`SYSTEM Dag: ${dagSystem.toString('hex')} (length: ${lengthdagSystem})`);
25
24
 
@@ -2,7 +2,7 @@ import { isHexString, createHash } from './functions.js';
2
2
  import { OperationType } from './constants.js';
3
3
  import fileUtils from './fileUtils.js';
4
4
  import b4a from 'b4a';
5
-
5
+ import Wallet from 'trac-wallet';
6
6
  // TODO: This class is trying to solve too many problems at once.
7
7
  // It is responsible for creating messages, verifying them, reading public keys from a file, etc.
8
8
  // It would be better to separate these concerns into different classes or modules.
@@ -10,9 +10,6 @@ import b4a from 'b4a';
10
10
  // This would make the code more modular and easier to maintain.
11
11
  // It would also make it easier to create tests and mocks in the future.
12
12
  class MsgUtils {
13
- static generateNonce() {
14
- return Math.random() + '-' + Date.now(); // TODO: Change it to crypto.randomBytes. Math.random might not be secure enough. It's even better to use nonce generator from sodium. GENERATE NONCE WITH CRYPTO LIBRARY WHICH ALLOW US TO GENERATE IT WITH UNIFORM DISTRIBUTION.
15
- }
16
13
 
17
14
  static createMessage(...args) {
18
15
  let buf = null;
@@ -35,7 +32,7 @@ class MsgUtils {
35
32
  case OperationType.ADD_ADMIN:
36
33
  case OperationType.ADD_WRITER:
37
34
  case OperationType.REMOVE_WRITER:
38
- nonce = this.generateNonce();
35
+ nonce = Wallet.generateNonce().toString('hex');
39
36
  msg = this.createMessage(wallet.publicKey, keyParam, nonce, operationType);
40
37
  hash = await createHash('sha256', msg);
41
38
  value = {
@@ -48,7 +45,7 @@ class MsgUtils {
48
45
  case OperationType.APPEND_WHITELIST:
49
46
  case OperationType.ADD_INDEXER:
50
47
  case OperationType.REMOVE_INDEXER:
51
- nonce = this.generateNonce();
48
+ nonce = Wallet.generateNonce().toString('hex');
52
49
  msg = this.createMessage(keyParam, nonce, operationType);
53
50
  hash = await createHash('sha256', msg);
54
51
  baseKey = keyParam;