trac-msb 0.1.7 → 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.7",
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;
@@ -88,13 +86,15 @@ export class MainSettlementBus extends ReadyResource {
88
86
  this.#signature_whitelist = options.signature_whitelist !== undefined && Array.isArray(options.signature_whitelist) ? options.signature_whitelist : [];
89
87
  this.#opts = options;
90
88
  this.#readline_instance = null;
91
-
92
- try{
93
- this.#readline_instance = readline.createInterface({
94
- input: new tty.ReadStream(0),
95
- output: new tty.WriteStream(1)
96
- });
97
- }catch(e){ }
89
+ this.enable_interactive_mode = options.enable_interactive_mode !== false;
90
+ if(this.enable_interactive_mode !== false){
91
+ try{
92
+ this.#readline_instance = readline.createInterface({
93
+ input: new tty.ReadStream(0),
94
+ output: new tty.WriteStream(1)
95
+ });
96
+ }catch(e){ }
97
+ }
98
98
  }
99
99
 
100
100
  // TODO: Implement other getters as necessary
@@ -379,13 +379,14 @@ export class MainSettlementBus extends ReadyResource {
379
379
  if (this.#enable_wallet) {
380
380
  await this.#wallet.initKeyPair(this.KEY_PAIR_PATH, this.#readline_instance);
381
381
  }
382
- console.log('View Length:', this.#base.view.core.length);
383
- console.log('View Signed Length:', this.#base.view.core.signedLength);
384
- console.log('MSB Key:', b4a.from(this.#base.view.core.key).toString('hex'));
385
382
 
383
+ console.log('');
384
+ console.log('#####################################################################################');
385
+ console.log('# MSB Address: ', this.#wallet.publicKey, '#');
386
386
  this.#writingKey = b4a.toString(this.#base.local.key, 'hex');
387
- console.log('Writer Key:', this.#writingKey);
388
-
387
+ console.log('# MSB Writer: ', this.#writingKey, '#');
388
+ console.log('#####################################################################################');
389
+ console.log('');
389
390
  if (this.#replicate) {
390
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));
391
392
  this.#dht_node = this.#swarm.dht;
@@ -409,8 +410,9 @@ export class MainSettlementBus extends ReadyResource {
409
410
 
410
411
  console.log(`isIndexer: ${this.#base.isIndexer}`);
411
412
  console.log(`isWriter: ${this.#base.writable}`);
412
- console.log('View Length:', this.#base.view.core.length);
413
- 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('');
414
416
  }
415
417
 
416
418
  async close() {
@@ -424,7 +426,6 @@ export class MainSettlementBus extends ReadyResource {
424
426
  async #setUpRoleAutomatically() {
425
427
  if (!this.#base.writable) {
426
428
  await this.#requestWriterRole(false)
427
- console.log('Wait please...');
428
429
  setTimeout(async () => {
429
430
  await this.#requestWriterRole(true)
430
431
  }, 5_000);
@@ -745,22 +746,31 @@ export class MainSettlementBus extends ReadyResource {
745
746
  await this.#requestWriterRole(false);
746
747
  }
747
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
+
748
763
  async interactiveMode() {
749
764
  if(this.#readline_instance === null) return;
750
765
  const rl = this.#readline_instance;
751
766
 
752
- console.log('MSB started. Available commands:');
753
- console.log('- /add_admin: register admin entry with bootstrap key.');
754
- console.log('- /add_whitelist: add a list of Trac public keys. Nodes that own these public keys can become writers.');
755
- console.log('- /add_indexer <trac_public_key>: change a role of the selected writer node to indexer role');
756
- console.log('- /push_writer_add: try to enforce adding this peer as writer after whitelisting.');
757
- console.log('- /remove_indexer <trac_public_key>: change a role of the selected indexer node to default role');
758
- console.log('- /get_node_info <trac_public_key>: get information about a node with the given Trac public key');
759
- console.log('- /dag: check system properties such as writing key, DAG, etc.');
760
- console.log('- /exit: Exit the program');
767
+ this.printHelp();
761
768
 
762
769
  rl.on('line', async (input) => {
763
770
  switch (input) {
771
+ case '/help':
772
+ this.printHelp();
773
+ break;
764
774
  case '/exit':
765
775
  console.log('Exiting...');
766
776
  rl.close();
@@ -783,15 +793,12 @@ export class MainSettlementBus extends ReadyResource {
783
793
  await this.#handleRemoveWriterOperation();
784
794
  break
785
795
  case '/flags':
786
- // Only for DEBUG
787
796
  console.log("shouldListenToAdminEvents: ", this.#shouldListenToAdminEvents);
788
797
  console.log("shouldListenToWriterEvents: ", this.#shouldListenToWriterEvents);
789
798
  console.log("isWritable: ", this.#base.writable);
790
799
  console.log("isIndexer: ", this.#base.isIndexer);
791
800
  break
792
801
  case '/show':
793
- // /get_node_info 9da99d98f02f24bdb13d46ba5d346c9a3eda03c18ab6e1441b7bac9743cf0bcc1
794
- // Only for DEBUG
795
802
  const admin = await this.get(EntryType.ADMIN);
796
803
  console.log('Admin:', admin);
797
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;