trac-msb 0.0.79 → 0.0.81

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.0.79",
4
+ "version": "0.0.81",
5
5
  "pear": {
6
6
  "name": "trac-msb",
7
7
  "type": "terminal"
package/src/index.js CHANGED
@@ -147,7 +147,6 @@ export class MainSettlementBus extends ReadyResource {
147
147
 
148
148
  async #handleApplyTxOperation(op, view, base, node, batch) {
149
149
  const postTx = op.value;
150
-
151
150
  if (postTx.op === OperationType.POST_TX &&
152
151
  null === await batch.get(op.key) &&
153
152
  this.check.sanitizePostTx(op) &&
@@ -164,24 +163,23 @@ export class MainSettlementBus extends ReadyResource {
164
163
 
165
164
  async #handleApplyAddAdminOperation(op, view, base, node, batch) {
166
165
  if (!this.check.sanitizeAdminAndWritersOperations(op)) return;
167
-
168
- const adminEntry = await this.getSigned(EntryType.ADMIN);
169
- if (!adminEntry) {
170
- await this.#addAdminIfNotSet(op, view, node);
166
+ const adminEntry = await batch.get(EntryType.ADMIN);
167
+ if (null === adminEntry || !this.#isAdmin(adminEntry.value, node)) {
168
+ await this.#addAdminIfNotSet(op, view, node, batch);
171
169
  }
172
- else if (adminEntry.tracPublicKey === op.key) {
173
- await this.#addAdminIfSet(adminEntry, op, view, base);
170
+ else if (adminEntry.value.tracPublicKey === op.key) {
171
+ await this.#addAdminIfSet(adminEntry.value, op, view, base, batch);
174
172
  }
175
173
  }
176
174
 
177
- async #addAdminIfSet(adminEntry, op, view, base) {
175
+ async #addAdminIfSet(adminEntry, op, view, base, batch) {
178
176
  const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.tracPublicKey, MsgUtils.createMessage(adminEntry.tracPublicKey, op.value.wk, op.value.nonce, op.type));
179
177
  if (isMessageVerifed) {
180
- const indexersEntry = await this.getSigned(EntryType.INDEXERS);
181
- if (indexersEntry && indexersEntry.includes(adminEntry.tracPublicKey)) {
178
+ const indexersEntry = await batch.get(EntryType.INDEXERS);
179
+ if (null !== indexersEntry && indexersEntry.value.includes(adminEntry.tracPublicKey)) {
182
180
  await base.removeWriter(b4a.from(adminEntry.wk, 'hex'));
183
181
  await base.addWriter(b4a.from(op.value.wk, 'hex'), { isIndexer: true })
184
- await view.put(EntryType.ADMIN, {
182
+ await batch.put(EntryType.ADMIN, {
185
183
  tracPublicKey: adminEntry.tracPublicKey,
186
184
  wk: op.value.wk
187
185
  })
@@ -190,56 +188,55 @@ export class MainSettlementBus extends ReadyResource {
190
188
  }
191
189
  }
192
190
 
193
- async #addAdminIfNotSet(op, view, node) {
191
+ async #addAdminIfNotSet(op, view, node, batch) {
194
192
  const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key, op.value.wk, op.value.nonce, op.type));
195
193
 
196
194
  if (node.from.key.toString('hex') === this.#bootstrap &&
197
195
  op.value.wk === this.#bootstrap &&
198
196
  isMessageVerifed
199
197
  ) {
200
- await view.put(EntryType.ADMIN, {
198
+ await batch.put(EntryType.ADMIN, {
201
199
  tracPublicKey: op.key,
202
200
  wk: this.#bootstrap
203
201
  })
204
202
  const initIndexers = [op.key];
205
- await view.put(EntryType.INDEXERS, initIndexers);
203
+ await batch.put(EntryType.INDEXERS, initIndexers);
206
204
  console.log(`Admin added: ${op.key}:${this.#bootstrap}`);
207
205
  }
208
206
  }
209
207
 
210
208
  async #handleApplyAppendWhitelistOperation(op, view, base, node, batch) {
211
-
212
- const adminEntry = await this.getSigned(EntryType.ADMIN);
213
- if (!this.check.sanitizeIndexerOrWhitelistOperations(op) || !this.#isAdmin(adminEntry, node)) return;
214
- const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type));
209
+ const adminEntry = await batch.get(EntryType.ADMIN);
210
+ if (null === adminEntry || !this.check.sanitizeIndexerOrWhitelistOperations(op) || !this.#isAdmin(adminEntry.value, node)) return;
211
+ const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.value.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type));
215
212
  if (!isMessageVerifed) return;
216
213
  const isWhitelisted = await this.#isWhitelisted(op.key);
217
214
  if (isWhitelisted) return;
218
- await this.#createWhitelistEntry(view, op.key);
215
+ await this.#createWhitelistEntry(batch, op.key);
219
216
  }
220
217
 
221
- async #createWhitelistEntry(view, pubKey) {
218
+ async #createWhitelistEntry(batch, pubKey) {
222
219
  const whitelistKey = WHITELIST_PREFIX + pubKey;
223
- await view.put(whitelistKey, true);
220
+ await batch.put(whitelistKey, true);
224
221
  }
225
222
 
226
223
  async #handleApplyAddWriterOperation(op, view, base, node, batch) {
227
- const adminEntry = await this.getSigned(EntryType.ADMIN);
228
- if (!this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry, node)) return;
224
+ const adminEntry = await batch.get(EntryType.ADMIN);
225
+ if (null === adminEntry || !this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry.value, node)) return;
229
226
 
230
227
  const isWhitelisted = await this.#isWhitelisted(op.key);
231
228
  if (!isWhitelisted) return;
232
229
  const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key, op.value.wk, op.value.nonce, op.type));
233
230
  if (isMessageVerifed) {
234
- await this.#addWriter(op, view, base);
231
+ await this.#addWriter(op, batch, base);
235
232
  }
236
233
  }
237
234
 
238
- async #addWriter(op, view, base) {
239
- const nodeEntry = await this.getSigned(op.key);
240
- if (nodeEntry === null || !nodeEntry.isWriter) {
235
+ async #addWriter(op, batch, base) {
236
+ const nodeEntry = await batch.get(op.key);
237
+ if (nodeEntry === null || !nodeEntry.value.isWriter) {
241
238
  await base.addWriter(b4a.from(op.value.wk, 'hex'), { isIndexer: false })
242
- await view.put(op.key, {
239
+ await batch.put(op.key, {
243
240
  wk: op.value.wk,
244
241
  isWriter: true,
245
242
  isIndexer: false
@@ -249,32 +246,33 @@ export class MainSettlementBus extends ReadyResource {
249
246
  }
250
247
 
251
248
  async #handleApplyRemoveWriterOperation(op, view, base, node, batch) {
252
- const adminEntry = await this.getSigned(EntryType.ADMIN);
253
- if (!this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry, node)) return;
249
+ const adminEntry = await batch.get(EntryType.ADMIN);
250
+ if (null === adminEntry || !this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry.value, node)) return;
254
251
  const isMessageVerifed = await this.#verifyMessage(op.value.sig, op.key, MsgUtils.createMessage(op.key, op.value.wk, op.value.nonce, op.type));
255
252
  if (isMessageVerifed) {
256
- await this.#removeWriter(op, view, base);
253
+ await this.#removeWriter(op, batch, base);
257
254
  }
258
255
  }
259
256
 
260
- async #removeWriter(op, view, base) {
261
- const nodeEntry = await this.getSigned(op.key)
257
+ async #removeWriter(op, batch, base) {
258
+ let nodeEntry = await batch.get(op.key)
262
259
  if (nodeEntry !== null) {
260
+ nodeEntry = nodeEntry.value;
263
261
  await base.removeWriter(b4a.from(nodeEntry.wk, 'hex'));
264
262
  nodeEntry.isWriter = false;
265
263
  if (nodeEntry.isIndexer) {
266
264
  nodeEntry.isIndexer = false;
267
- const indexersEntry = await this.getSigned(EntryType.INDEXERS);
268
- if (indexersEntry && indexersEntry.includes(op.key)) {
269
- const idx = indexersEntry.indexOf(op.key);
265
+ const indexersEntry = await batch.get(EntryType.INDEXERS);
266
+ if (null !== indexersEntry && indexersEntry.value.includes(op.key)) {
267
+ const idx = indexersEntry.value.indexOf(op.key);
270
268
  if (idx !== -1) {
271
- indexersEntry.splice(idx, 1);
272
- await view.put(EntryType.INDEXERS, indexersEntry);
269
+ indexersEntry.value.splice(idx, 1);
270
+ await batch.put(EntryType.INDEXERS, indexersEntry.value);
273
271
  }
274
272
  }
275
273
  }
276
274
 
277
- await view.put(op.key, nodeEntry);
275
+ await batch.put(op.key, nodeEntry);
278
276
  console.log(`Writer removed: ${op.key}:${op.value.wk}`);
279
277
  }
280
278
  }
@@ -284,56 +282,58 @@ export class MainSettlementBus extends ReadyResource {
284
282
  return;
285
283
  }
286
284
 
287
- const adminEntry = await this.getSigned(EntryType.ADMIN);
288
- if (!this.#isAdmin(adminEntry, node)) return;
285
+ const adminEntry = await batch.get(EntryType.ADMIN);
286
+ if (null === adminEntry || !this.#isAdmin(adminEntry.value, node)) return;
289
287
 
290
288
  if (!this.#isWhitelisted(op.key)) return;
291
289
 
292
- const indexersEntry = await this.getSigned(EntryType.INDEXERS);
293
- if (!indexersEntry || Array.from(indexersEntry).includes(op.key) ||
294
- Array.from(indexersEntry).length >= MAX_INDEXERS) {
290
+ const indexersEntry = await batch.get(EntryType.INDEXERS);
291
+ if (null === indexersEntry || Array.from(indexersEntry.value).includes(op.key) ||
292
+ Array.from(indexersEntry.value).length >= MAX_INDEXERS) {
295
293
  return;
296
294
  }
297
- const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
295
+ const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.value.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
298
296
  if (isMessageVerifed) {
299
- await this.#addIndexer(indexersEntry, op, view, base);
297
+ await this.#addIndexer(indexersEntry.value, op, batch, base);
300
298
  }
301
299
  }
302
300
 
303
- async #addIndexer(indexersEntry, op, view, base) {
304
- const nodeEntry = await this.getSigned(op.key);
305
-
306
- if (nodeEntry !== null && nodeEntry.isWriter && !nodeEntry.isIndexer) {
301
+ async #addIndexer(indexersEntry, op, batch, base) {
302
+ let nodeEntry = await batch.get(op.key);
307
303
 
304
+ if (nodeEntry !== null && nodeEntry.value.isWriter && !nodeEntry.value.isIndexer) {
305
+ nodeEntry = nodeEntry.value;
308
306
  await base.removeWriter(b4a.from(nodeEntry.wk, 'hex'));
309
307
  await base.addWriter(b4a.from(nodeEntry.wk, 'hex'), { isIndexer: true })
310
308
  nodeEntry.isIndexer = true;
311
- await view.put(op.key, nodeEntry);
309
+ await batch.put(op.key, nodeEntry);
312
310
  indexersEntry.push(op.key);
313
- await view.put(EntryType.INDEXERS, indexersEntry);
311
+ await batch.put(EntryType.INDEXERS, indexersEntry);
314
312
  console.log(`Indexer added: ${op.key}:${nodeEntry.wk}`);
315
313
  }
316
314
  }
317
315
 
318
316
  async #handleApplyRemoveIndexerOperation(op, view, base, node, batch) {
319
317
  if (!this.check.sanitizeIndexerOrWhitelistOperations(op)) return;
320
- const adminEntry = await this.getSigned(EntryType.ADMIN);
321
- const indexersEntry = await this.getSigned(EntryType.INDEXERS);
322
- if (!this.#isAdmin(adminEntry, node) || !indexersEntry || !Array.from(indexersEntry).includes(op.key) || Array.from(indexersEntry).length <= 1) return;
323
- const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
318
+ const adminEntry = await batch.get(EntryType.ADMIN);
319
+ let indexersEntry = await batch.get(EntryType.INDEXERS);
320
+ if (null === adminEntry || !this.#isAdmin(adminEntry.value, node) || null === indexersEntry || !Array.from(indexersEntry.value).includes(op.key) || Array.from(indexersEntry.value).length <= 1) return;
321
+ const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.value.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
324
322
  if (isMessageVerifed) {
325
- const nodeEntry = await this.getSigned(op.key);
326
- if (nodeEntry !== null && nodeEntry.isWriter && nodeEntry.isIndexer) {
323
+ let nodeEntry = await batch.get(op.key);
324
+ if (nodeEntry !== null && nodeEntry.value.isWriter && nodeEntry.value.isIndexer) {
325
+ indexersEntry = indexersEntry.value;
326
+ nodeEntry = nodeEntry.value;
327
327
  await base.removeWriter(b4a.from(nodeEntry.wk, 'hex'));
328
328
 
329
329
  nodeEntry.isWriter = false;
330
330
  nodeEntry.isIndexer = false;
331
- await view.put(op.key, nodeEntry);
331
+ await batch.put(op.key, nodeEntry);
332
332
 
333
333
  const idx = indexersEntry.indexOf(op.key);
334
334
  if (idx !== -1) {
335
335
  indexersEntry.splice(idx, 1);
336
- await view.put(EntryType.INDEXERS, indexersEntry);
336
+ await batch.put(EntryType.INDEXERS, indexersEntry);
337
337
  }
338
338
 
339
339
  console.log(`Indexer removed: ${op.key}:${nodeEntry.wk}`);
@@ -16,7 +16,6 @@ export class WriterManager extends ReadyResource {
16
16
  const adminEntry = await this.msbInstance.getSigned('admin');
17
17
  if (!adminEntry && this.msbInstance.writingKey && this.msbInstance.writingKey === this.msbInstance.bootstrap) {
18
18
 
19
-
20
19
  const nonce = Math.random() + '-' + Date.now();
21
20
  const msg = Buffer.concat(
22
21
  [
@@ -68,8 +67,6 @@ export class WriterManager extends ReadyResource {
68
67
  }
69
68
  });
70
69
  }
71
-
72
-
73
70
  }catch(e) {
74
71
  console.log('Error reading file', e);
75
72
  }