trac-msb 0.1.15 → 0.1.16

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "trac-msb",
3
3
  "main": "msb.mjs",
4
- "version": "0.1.15",
4
+ "version": "0.1.16",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
package/src/index.js CHANGED
@@ -538,10 +538,8 @@ export class MainSettlementBus extends ReadyResource {
538
538
  return entry !== null ? entry.value : null
539
539
  }
540
540
 
541
- async #handleIncomingEvent(data) {
541
+ async #handleIncomingEvent(parsedRequest) {
542
542
  try {
543
- const bufferData = data.toString();
544
- const parsedRequest = JSON.parse(bufferData);
545
543
  if (parsedRequest && parsedRequest.type && parsedRequest.key && parsedRequest.value) {
546
544
  if (parsedRequest.type === OperationType.ADD_WRITER || parsedRequest.type === OperationType.REMOVE_WRITER) {
547
545
  //This request must be hanlded by ADMIN
@@ -604,7 +602,7 @@ export class MainSettlementBus extends ReadyResource {
604
602
  this.on(EventType.ADMIN_EVENT, async (parsedRequest) => {
605
603
  if (this.#enable_wallet === false) return;
606
604
  const isWhitelisted = await this.#isWhitelisted(parsedRequest.key);
607
- const isEventMessageVerifed = await MsgUtils.verifyEventMessage(parsedRequest, this.#wallet)
605
+ const isEventMessageVerifed = await MsgUtils.verifyEventMessage(parsedRequest, this.#wallet, this.check)
608
606
  if (isWhitelisted && isEventMessageVerifed) {
609
607
  await this.#base.append(parsedRequest);
610
608
  }
@@ -615,7 +613,7 @@ export class MainSettlementBus extends ReadyResource {
615
613
  this.on(EventType.WRITER_EVENT, async (parsedRequest) => {
616
614
  if (this.#enable_wallet === false) return;
617
615
  const adminEntry = await this.get(EntryType.ADMIN);
618
- const isEventMessageVerifed = await MsgUtils.verifyEventMessage(parsedRequest, this.#wallet)
616
+ const isEventMessageVerifed = await MsgUtils.verifyEventMessage(parsedRequest, this.#wallet, this.check)
619
617
  if (adminEntry && adminEntry.tracPublicKey === parsedRequest.key && isEventMessageVerifed) {
620
618
  await this.#base.append(parsedRequest);
621
619
  }
package/src/network.js CHANGED
@@ -38,7 +38,7 @@ class Network {
38
38
  };
39
39
  }
40
40
 
41
- swarm = new Hyperswarm({ keyPair, randomPunchInterval: 5_000, bootstrap : bootstrap, maxPeers: MAX_PEERS, maxParallel: MAX_PARALLEL, maxServerConnections: MAX_SERVER_CONNECTIONS, maxClientConnections : MAX_CLIENT_CONNECTIONS});
41
+ swarm = new Hyperswarm({ keyPair, bootstrap : bootstrap, maxPeers: MAX_PEERS, maxParallel: MAX_PARALLEL, maxServerConnections: MAX_SERVER_CONNECTIONS, maxClientConnections : MAX_CLIENT_CONNECTIONS});
42
42
 
43
43
  console.log(`Channel: ${b4a.toString(channel)}`);
44
44
  swarm.on('connection', async (connection) => {
@@ -46,13 +46,11 @@ class Network {
46
46
  store.replicate(connection);
47
47
  connection.on('close', () => { });
48
48
  connection.on('error', (error) => { });
49
- connection.on('data', async (data) => {
50
- await handleIncomingEvent(data);
51
- });
52
49
 
53
50
  if(enable_txchannel){
54
51
  connection.on('message', async (msg) => {
55
52
  try{
53
+ const tmp_message = msg;
56
54
  msg = b4a.toString(msg, 'utf-8');
57
55
  msg = JSON.parse(msg);
58
56
  if(null === msg) return;
@@ -67,7 +65,7 @@ class Network {
67
65
  const isAllowedToRequestRole = await msb._isAllowedToRequestRole(msg.key, adminEntry);
68
66
  const canAddWriter = base.writable && !isAlreadyWriter && isAllowedToRequestRole;
69
67
  if(msg.key !== wallet.publicKey && canAddWriter){
70
- await base.append(msg);
68
+ await handleIncomingEvent(msg);
71
69
  }
72
70
  await connection.end();
73
71
  } else if (msg.type !== undefined && msg.key !== undefined && msg.value !== undefined && msg.type === 'removeWriter') {
@@ -77,7 +75,7 @@ class Network {
77
75
  const isAlreadyWriter = null !== nodeEntry && nodeEntry.isWriter;
78
76
  const canRemoveWriter = base.writable && isAlreadyWriter
79
77
  if (msg.key !== wallet.publicKey && canRemoveWriter) {
80
- await base.append(msg);
78
+ await handleIncomingEvent(msg);
81
79
  }
82
80
  await connection.end();
83
81
  }
@@ -90,7 +88,7 @@ class Network {
90
88
  return
91
89
  }
92
90
 
93
- if (b4a.byteLength(msg) > 3072) return;
91
+ if (b4a.byteLength(tmp_message) > 3072) return;
94
92
 
95
93
  const parsedPreTx = msg;
96
94
 
@@ -115,11 +115,21 @@ class MsgUtils {
115
115
  return await this.#assembleMessageBase(wallet, validatorTracPublicKey, OperationType.BAN_VALIDATOR);
116
116
  }
117
117
 
118
- static async verifyEventMessage(parsedRequest, wallet) {
119
- //TODO: Here we can add some sanitization
120
- const msg = this.createMessage(parsedRequest.key, parsedRequest.value.wk, parsedRequest.value.nonce, parsedRequest.type);
118
+ static async verifyEventMessage(parsedRequest, wallet, check) {
119
+ const { type, key, value } = parsedRequest;
120
+ if (
121
+ type !== OperationType.ADD_ADMIN &&
122
+ type !== OperationType.ADD_WRITER &&
123
+ type !== OperationType.REMOVE_WRITER
124
+ ) {
125
+ return false;
126
+ }
127
+ const sanitizationResult = check.sanitizeAdminAndWritersOperations(parsedRequest);
128
+ if (!sanitizationResult) return false;
129
+
130
+ const msg = this.createMessage(key, value.wk, value.nonce, type);
121
131
  const hash = await createHash('sha256', msg);
122
- return wallet.verify(parsedRequest.value.sig, hash, parsedRequest.key);
132
+ return wallet.verify(value.sig, hash, key);
123
133
  }
124
134
 
125
135
  }
@@ -1,75 +0,0 @@
1
- import ReadyResource from 'ready-resource';
2
- import { createHash } from 'utils/functions.js';
3
- import fs from 'fs';
4
- //TODO: GENERATE NONCE WITH CRYPTO LIBRARY WHICH ALLOW US TO GENERATE IT WITH UNIFORM DISTRIBUTION.
5
-
6
- const FILEPATH = './whitelist/pubkeys.csv';
7
-
8
- export class WriterManager extends ReadyResource {
9
- constructor(msbInstance) {
10
- super();
11
- this.msbInstance = msbInstance;
12
- }
13
-
14
- async addAdmin() {
15
- // case where admin entry doesn't exist yet and we have to autorize Admin public key only with bootstrap writing key
16
- const adminEntry = await this.msbInstance.get('admin');
17
- if (!adminEntry && this.msbInstance.writingKey && this.msbInstance.writingKey === this.msbInstance.bootstrap) {
18
-
19
- const nonce = Math.random() + '-' + Date.now();
20
- const msg = Buffer.concat(
21
- [
22
- Buffer.from(this.msbInstance.wallet.publicKey, 'hex'),
23
- Buffer.from(nonce),
24
- ]
25
- )
26
-
27
- const hash = await createHash('sha256', msg);
28
- await this.msbInstance.base.append({
29
- type: 'addAdmin',
30
- key: 'admin',
31
- value: {
32
- tpk: this.msbInstance.wallet.publicKey,
33
- nonce: nonce,
34
- pop: this.msbInstance.wallet.sign(hash)
35
- }
36
- });
37
- }
38
- // case where admin entry exists and we won't anymore use bootstrap writig key. It can be implemented when list of authorized writers is implemented.
39
- // if (adminEntry && this.msbInstance.writingKey) {
40
- // }
41
- }
42
- async appendToWhitelist() {
43
- //who can use this method? only admin
44
- try {
45
- //TODO: IMPORTANT - IF WE GONNA STORE ~ 2K-10K PUBLIC KEYS IN THE LIST, WE NEED TO SPLIT IT INTO CHUNKS
46
- // ONE CHUNK WILL BE ~100 PUBLIC KEYS + NONCE + SIG AND ADDITIONAL BYTES < 4096 BYTES. ADMIN WILL NEED TO PERFORM MULTIPLE APPENDS. FOR NOW THIS IS NOT IMPLEMENTED.
47
- const adminEntry = await this.msbInstance.get('admin');
48
-
49
- if (adminEntry && this.msbInstance.wallet.publicKey === Buffer.from(adminEntry.tpk.data).toString('hex')) {
50
-
51
- const pubKeys = fs.readFileSync(FILEPATH, 'utf8').split('\n').map(line =>line.trim()).filter(line => line.length > 0); // pub keys are 32 bytes long. Take lines which have this length
52
- const nonce = Math.random() + '-' + Date.now();
53
- const msg = Buffer.concat(
54
- [
55
- Buffer.from(pubKeys.join('')),
56
- Buffer.from(nonce),
57
- ]
58
- )
59
- const hash = await createHash('sha256', msg);
60
- await this.msbInstance.base.append({
61
- type: 'whitelist',
62
- key: 'list',
63
- value: {
64
- nonce: nonce,
65
- pubKeysList: JSON.stringify(pubKeys),
66
- sig: this.msbInstance.wallet.sign(hash)
67
- }
68
- });
69
- }
70
- }catch(e) {
71
- console.log('Error reading file', e);
72
- }
73
- }
74
- }
75
- export default WriterManager;