trac-msb 0.0.81 → 0.0.83

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,2 +1,6 @@
1
1
  e247a9ceefb8a83e28d3c5845055910bc61e0b418c7578d297354708f276af04
2
- 580c44d7827fbeeb7778e4d2dce660b30f3ca3baa4de254ecd556dfb5b60fc35
2
+ 580c44d7827fbeeb7778e4d2dce660b30f3ca3baa4de254ecd556dfb5b60fc35
3
+ a3590854889d059927fed4dfdf329b75447bc9f5885b6a843b317d1c2d38258a
4
+ dd2f83c9cb1d146c8be6c1d98822cbbb3f631a0659b765c9e4564960acf6e228
5
+ 726b2885e551a98c20b37675b113476390f57321768a303e1b6c029ffca509a9
6
+ 7da7f48e1c805b551fd69a2b6e59b99c00740dea699c66e10b88bd1efb932815
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.0.81",
4
+ "version": "0.0.83",
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.#bee = null;
68
68
  this.#swarm = null;
69
69
  this.#dht_node = new DHT();
70
- this.#dht_server = this.#dht_node.createServer();
70
+ this.#dht_server = null;
71
71
  this.#base = null;
72
72
  this.#writingKey = null;
73
73
  this.#enable_txchannel = options.enable_txchannel !== false;
@@ -121,7 +121,6 @@ export class MainSettlementBus extends ReadyResource {
121
121
  for (const node of nodes) {
122
122
  const op = node.value;
123
123
  const handler = this.#getApplyOperationHandler(op.type);
124
-
125
124
  if (handler) {
126
125
  await handler(op, view, base, node, batch);
127
126
  } else {
@@ -164,7 +163,7 @@ export class MainSettlementBus extends ReadyResource {
164
163
  async #handleApplyAddAdminOperation(op, view, base, node, batch) {
165
164
  if (!this.check.sanitizeAdminAndWritersOperations(op)) return;
166
165
  const adminEntry = await batch.get(EntryType.ADMIN);
167
- if (null === adminEntry || !this.#isAdmin(adminEntry.value, node)) {
166
+ if (null === adminEntry) {
168
167
  await this.#addAdminIfNotSet(op, view, node, batch);
169
168
  }
170
169
  else if (adminEntry.value.tracPublicKey === op.key) {
@@ -358,7 +357,8 @@ export class MainSettlementBus extends ReadyResource {
358
357
  }
359
358
 
360
359
  if (this.#enable_txchannel) {
361
- await Network.dhtServer(this.#dht_server, this.#base, this.#wallet, this.#writingKey, this.#network);
360
+ this.#dht_server = this.#dht_node.createServer();
361
+ await Network.dhtServer(this, this.#dht_server, this.#base, this.#wallet, this.#writingKey, this.#network);
362
362
  }
363
363
 
364
364
  const adminEntry = await this.getSigned(EntryType.ADMIN);
@@ -415,11 +415,13 @@ export class MainSettlementBus extends ReadyResource {
415
415
  if (!adminEntry || !message) {
416
416
  return;
417
417
  }
418
- this.#swarm.connections.forEach((conn) => {
419
- if (b4a.from(conn.remotePublicKey).toString('hex') === adminEntry.tracPublicKey && conn.connected) {
420
- conn.write(JSON.stringify(message));
421
- }
418
+ const stream = this.#dht_node.connect(b4a.from(adminEntry.tracPublicKey, 'hex'))
419
+ stream.on('connect', async function () {
420
+ await stream.send(b4a.from(JSON.stringify({ op : 'add_writer', message : message })));
422
421
  });
422
+ stream.on('open', function () { });
423
+ stream.on('close', () => { });
424
+ stream.on('error', (error) => { });
423
425
  }
424
426
 
425
427
  async #verifyMessage(signature, publicKey, bufferMessage) {
@@ -435,7 +437,7 @@ export class MainSettlementBus extends ReadyResource {
435
437
  return !!(this.#wallet.publicKey === adminEntry.tracPublicKey && adminEntry.wk === this.#writingKey);
436
438
  }
437
439
 
438
- async #isAllowedToRequestRole(key, adminEntry) {
440
+ async isAllowedToRequestRole(key, adminEntry) {
439
441
  const isWhitelisted = await this.#isWhitelisted(key);
440
442
  return !!(isWhitelisted && !this.#isAdmin(adminEntry));
441
443
  }
@@ -642,7 +644,7 @@ export class MainSettlementBus extends ReadyResource {
642
644
  const isAlreadyWriter = !!(nodeEntry && nodeEntry.isWriter)
643
645
  let assembledMessage = null;
644
646
  if (toAdd) {
645
- const isAllowedToRequestRole = await this.#isAllowedToRequestRole(this.#wallet.publicKey, adminEntry);
647
+ const isAllowedToRequestRole = await this.isAllowedToRequestRole(this.#wallet.publicKey, adminEntry);
646
648
  const canAddWriter = !!(!this.#base.writable && !isAlreadyWriter && isAllowedToRequestRole);
647
649
  if (canAddWriter) {
648
650
  assembledMessage = await MsgUtils.assembleAddWriterMessage(this.#wallet, this.#writingKey);
@@ -654,6 +656,8 @@ export class MainSettlementBus extends ReadyResource {
654
656
  }
655
657
  }
656
658
 
659
+ console.log(assembledMessage);
660
+
657
661
  if (assembledMessage) {
658
662
  this.#sendMessageToAdmin(adminEntry, assembledMessage);
659
663
  }
package/src/network.js CHANGED
@@ -1,7 +1,15 @@
1
1
  import w from 'protomux-wakeup';
2
2
  import b4a from 'b4a';
3
3
  import Hyperswarm from 'hyperswarm';
4
- import { EventType, TRAC_NAMESPACE, MAX_PEERS, MAX_PARALLEL, MAX_SERVER_CONNECTIONS, OperationType } from './utils/constants.js';
4
+ import {
5
+ EventType,
6
+ TRAC_NAMESPACE,
7
+ MAX_PEERS,
8
+ MAX_PARALLEL,
9
+ MAX_SERVER_CONNECTIONS,
10
+ OperationType,
11
+ EntryType
12
+ } from './utils/constants.js';
5
13
  import {sleep } from './utils/functions.js';
6
14
  import MsgUtils from './utils/msgUtils.js';
7
15
  import Check from './utils/check.js';
@@ -52,27 +60,38 @@ class Network {
52
60
  return swarm;
53
61
  }
54
62
 
55
- static async dhtServer(dhtServer, base, wallet, writingKey, networkInstance){
63
+ static async dhtServer(msb, dhtServer, base, wallet, writingKey, networkInstance){
56
64
  try{
57
65
  dhtServer.on('connection', function (connection) {
58
66
  connection.on('message', async (msg) => {
59
- msg = b4a.toString(msg, 'utf-8');
60
- if(msg === 'get_writer_key'){
61
- await connection.send(b4a.from(JSON.stringify({op:'writer_key', key : writingKey})));
62
- } else {
63
-
64
- if (base.isIndexer || !base.writable) return;
67
+ try{
68
+ msg = b4a.toString(msg, 'utf-8');
69
+ msg = JSON.parse(msg);
70
+ if(msg === 'get_writer_key'){
71
+ await connection.send(b4a.from(JSON.stringify({op:'writer_key', key : writingKey})));
72
+ } else if(msg.op !== undefined && msg.message !== undefined && msg.op === 'add_writer'){
73
+ msg = msg.value;
74
+ const adminEntry = await msb.getSigned(EntryType.ADMIN);
75
+ const nodeEntry = await msb.getSigned(msg.message.pub);
76
+ const isAlreadyWriter = !!(nodeEntry && nodeEntry.isWriter);
77
+ const isAllowedToRequestRole = await msb.isAllowedToRequestRole(msg.message.pub, adminEntry);
78
+ const canAddWriter = !!(!base.writable && !isAlreadyWriter && isAllowedToRequestRole);
79
+ if(null !== adminEntry && adminEntry.tracPublicKey === wallet.publicKey
80
+ && msg.message.pub !== wallet.publicKey && canAddWriter){
81
+ await base.append(msg.message);
82
+ }
83
+ } else {
84
+ if (base.isIndexer || !base.writable) return;
65
85
 
66
- // TODO: decide if a tx rejection should be responded with
67
- if (networkInstance.tx_pool.length >= 1000) {
68
- console.log('pool full');
69
- return
70
- }
86
+ // TODO: decide if a tx rejection should be responded with
87
+ if (networkInstance.tx_pool.length >= 1000) {
88
+ console.log('pool full');
89
+ return
90
+ }
71
91
 
72
- if (b4a.byteLength(msg) > 3072) return;
92
+ if (b4a.byteLength(msg) > 3072) return;
73
93
 
74
- try {
75
- const parsedPreTx = JSON.parse(msg);
94
+ const parsedPreTx = msg;
76
95
 
77
96
  if (networkInstance.check.sanitizePreTx(parsedPreTx) &&
78
97
  wallet.verify(b4a.from(parsedPreTx.is, 'hex'), b4a.from(parsedPreTx.tx + parsedPreTx.in), b4a.from(parsedPreTx.ipk, 'hex')) &&
@@ -98,11 +117,11 @@ class Network {
98
117
  };
99
118
  networkInstance.tx_pool.push({ tx: parsedPreTx.tx, append_tx: append_tx });
100
119
  }
101
- } catch (e) {
102
- console.log(e)
103
120
  }
121
+ //await connection.destroy();
122
+ }catch(e){
123
+ console.log(e);
104
124
  }
105
- //await connection.destroy();
106
125
  });
107
126
  connection.on('close', () => { });
108
127
  connection.on('error', (error) => { });
@@ -113,7 +132,7 @@ class Network {
113
132
  };
114
133
  await dhtServer.listen(keyPair)
115
134
  console.log('DHT node is listening on public key', wallet.publicKey);
116
- } catch(e) { }
135
+ } catch(e) { console.log(e) }
117
136
  }
118
137
 
119
138
  async pool(base) {
@@ -69,6 +69,7 @@ class Check {
69
69
  value: {
70
70
  $$strict: true,
71
71
  $$type: "object",
72
+ pub: { type: 'is_hex_string', length: 64, required: true },
72
73
  wk: { type: 'is_hex_string', length: 64, required: true },
73
74
  nonce: { type: 'string', min: 1, max: 256, required: true },
74
75
  sig: { type: 'is_hex_string', length: 128, required: true },
@@ -39,6 +39,7 @@ class MsgUtils {
39
39
  msg = this.createMessage(wallet.publicKey, keyParam, nonce, operationType);
40
40
  hash = await createHash('sha256', msg);
41
41
  value = {
42
+ pub : wallet.publicKey,
42
43
  wk: keyParam,
43
44
  nonce: nonce,
44
45
  sig: wallet.sign(hash)