trac-msb 0.0.78 → 0.0.80
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 +1 -1
- package/src/index.js +31 -34
- package/src/utils/functions.js +5 -4
- package/src/writerManager.js +3 -6
package/package.json
CHANGED
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
|
-
|
|
169
|
-
if (!adminEntry) {
|
|
166
|
+
const adminEntry = await batch.get(EntryType.ADMIN);
|
|
167
|
+
if (null === adminEntry) {
|
|
170
168
|
await this.#addAdminIfNotSet(op, view, node);
|
|
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
|
|
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
|
|
182
|
+
await batch.put(EntryType.ADMIN, {
|
|
185
183
|
tracPublicKey: adminEntry.tracPublicKey,
|
|
186
184
|
wk: op.value.wk
|
|
187
185
|
})
|
|
@@ -197,49 +195,48 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
197
195
|
op.value.wk === this.#bootstrap &&
|
|
198
196
|
isMessageVerifed
|
|
199
197
|
) {
|
|
200
|
-
await
|
|
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
|
|
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
|
-
|
|
213
|
-
|
|
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(
|
|
215
|
+
await this.#createWhitelistEntry(batch, op.key);
|
|
219
216
|
}
|
|
220
217
|
|
|
221
|
-
async #createWhitelistEntry(
|
|
218
|
+
async #createWhitelistEntry(batch, pubKey) {
|
|
222
219
|
const whitelistKey = WHITELIST_PREFIX + pubKey;
|
|
223
|
-
await
|
|
220
|
+
await batch.put(whitelistKey, true);
|
|
224
221
|
}
|
|
225
222
|
|
|
226
223
|
async #handleApplyAddWriterOperation(op, view, base, node, batch) {
|
|
227
|
-
const adminEntry = await
|
|
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,
|
|
231
|
+
await this.#addWriter(op, batch, base);
|
|
235
232
|
}
|
|
236
233
|
}
|
|
237
234
|
|
|
238
|
-
async #addWriter(op,
|
|
235
|
+
async #addWriter(op, batch, base) {
|
|
239
236
|
const nodeEntry = await this.getSigned(op.key);
|
|
240
237
|
if (nodeEntry === null || !nodeEntry.isWriter) {
|
|
241
238
|
await base.addWriter(b4a.from(op.value.wk, 'hex'), { isIndexer: false })
|
|
242
|
-
await
|
|
239
|
+
await batch.put(op.key, {
|
|
243
240
|
wk: op.value.wk,
|
|
244
241
|
isWriter: true,
|
|
245
242
|
isIndexer: false
|
|
@@ -253,11 +250,11 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
253
250
|
if (!this.check.sanitizeAdminAndWritersOperations(op) || !this.#isAdmin(adminEntry, 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,
|
|
253
|
+
await this.#removeWriter(op, batch, base);
|
|
257
254
|
}
|
|
258
255
|
}
|
|
259
256
|
|
|
260
|
-
async #removeWriter(op,
|
|
257
|
+
async #removeWriter(op, batch, base) {
|
|
261
258
|
const nodeEntry = await this.getSigned(op.key)
|
|
262
259
|
if (nodeEntry !== null) {
|
|
263
260
|
await base.removeWriter(b4a.from(nodeEntry.wk, 'hex'));
|
|
@@ -269,12 +266,12 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
269
266
|
const idx = indexersEntry.indexOf(op.key);
|
|
270
267
|
if (idx !== -1) {
|
|
271
268
|
indexersEntry.splice(idx, 1);
|
|
272
|
-
await
|
|
269
|
+
await batch.put(EntryType.INDEXERS, indexersEntry);
|
|
273
270
|
}
|
|
274
271
|
}
|
|
275
272
|
}
|
|
276
273
|
|
|
277
|
-
await
|
|
274
|
+
await batch.put(op.key, nodeEntry);
|
|
278
275
|
console.log(`Writer removed: ${op.key}:${op.value.wk}`);
|
|
279
276
|
}
|
|
280
277
|
}
|
|
@@ -296,11 +293,11 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
296
293
|
}
|
|
297
294
|
const isMessageVerifed = await this.#verifyMessage(op.value.sig, adminEntry.tracPublicKey, MsgUtils.createMessage(op.key, op.value.nonce, op.type))
|
|
298
295
|
if (isMessageVerifed) {
|
|
299
|
-
await this.#addIndexer(indexersEntry, op,
|
|
296
|
+
await this.#addIndexer(indexersEntry, op, batch, base);
|
|
300
297
|
}
|
|
301
298
|
}
|
|
302
299
|
|
|
303
|
-
async #addIndexer(indexersEntry, op,
|
|
300
|
+
async #addIndexer(indexersEntry, op, batch, base) {
|
|
304
301
|
const nodeEntry = await this.getSigned(op.key);
|
|
305
302
|
|
|
306
303
|
if (nodeEntry !== null && nodeEntry.isWriter && !nodeEntry.isIndexer) {
|
|
@@ -308,9 +305,9 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
308
305
|
await base.removeWriter(b4a.from(nodeEntry.wk, 'hex'));
|
|
309
306
|
await base.addWriter(b4a.from(nodeEntry.wk, 'hex'), { isIndexer: true })
|
|
310
307
|
nodeEntry.isIndexer = true;
|
|
311
|
-
await
|
|
308
|
+
await batch.put(op.key, nodeEntry);
|
|
312
309
|
indexersEntry.push(op.key);
|
|
313
|
-
await
|
|
310
|
+
await batch.put(EntryType.INDEXERS, indexersEntry);
|
|
314
311
|
console.log(`Indexer added: ${op.key}:${nodeEntry.wk}`);
|
|
315
312
|
}
|
|
316
313
|
}
|
|
@@ -328,12 +325,12 @@ export class MainSettlementBus extends ReadyResource {
|
|
|
328
325
|
|
|
329
326
|
nodeEntry.isWriter = false;
|
|
330
327
|
nodeEntry.isIndexer = false;
|
|
331
|
-
await
|
|
328
|
+
await batch.put(op.key, nodeEntry);
|
|
332
329
|
|
|
333
330
|
const idx = indexersEntry.indexOf(op.key);
|
|
334
331
|
if (idx !== -1) {
|
|
335
332
|
indexersEntry.splice(idx, 1);
|
|
336
|
-
await
|
|
333
|
+
await batch.put(EntryType.INDEXERS, indexersEntry);
|
|
337
334
|
}
|
|
338
335
|
|
|
339
336
|
console.log(`Indexer removed: ${op.key}:${nodeEntry.wk}`);
|
package/src/utils/functions.js
CHANGED
|
@@ -35,10 +35,9 @@ export async function sleep(ms) {
|
|
|
35
35
|
export async function createHash(type, message) {
|
|
36
36
|
if (type === 'sha256') {
|
|
37
37
|
const out = b4a.alloc(sodium.crypto_hash_sha256_BYTES);
|
|
38
|
-
sodium.crypto_hash_sha256(out
|
|
38
|
+
sodium.crypto_hash_sha256(out,!b4a.isBuffer(message) ? b4a.from(message) : message);
|
|
39
39
|
return b4a.toString(out, 'hex');
|
|
40
40
|
}
|
|
41
|
-
let createHash = null;
|
|
42
41
|
if (global.Pear !== undefined) {
|
|
43
42
|
let _type = '';
|
|
44
43
|
switch (type.toLowerCase()) {
|
|
@@ -48,13 +47,15 @@ export async function createHash(type, message) {
|
|
|
48
47
|
default: throw new Error('Unsupported algorithm.');
|
|
49
48
|
}
|
|
50
49
|
const encoder = new TextEncoder();
|
|
51
|
-
const data = encoder.encode(message);
|
|
50
|
+
const data = encoder.encode(b4a.isBuffer(message) ? b4a.toString(message, 'utf-8') : message);
|
|
52
51
|
const hash = await crypto.subtle.digest(_type, data);
|
|
53
52
|
const hashArray = Array.from(new Uint8Array(hash));
|
|
54
53
|
return hashArray
|
|
55
54
|
.map((b) => b.toString(16).padStart(2, "0"))
|
|
56
55
|
.join("");
|
|
57
56
|
} else {
|
|
58
|
-
|
|
57
|
+
// this is only available here for completeness and in fact will never be used in the MSB.
|
|
58
|
+
// just keep it as it is.
|
|
59
|
+
return crypto.createHash(type).update(!b4a.isBuffer(message) ? b4a.from(message) : message).digest('hex')
|
|
59
60
|
}
|
|
60
61
|
}
|
package/src/writerManager.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ReadyResource from 'ready-resource';
|
|
2
|
-
import { createHash } from '
|
|
2
|
+
import { createHash } from 'utils/functions.js';
|
|
3
3
|
import fs from 'fs';
|
|
4
4
|
//TODO: GENERATE NONCE WITH CRYPTO LIBRARY WHICH ALLOW US TO GENERATE IT WITH UNIFORM DISTRIBUTION.
|
|
5
5
|
|
|
@@ -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
|
[
|
|
@@ -25,7 +24,7 @@ export class WriterManager extends ReadyResource {
|
|
|
25
24
|
]
|
|
26
25
|
)
|
|
27
26
|
|
|
28
|
-
const hash = createHash('sha256'
|
|
27
|
+
const hash = await createHash('sha256', msg);
|
|
29
28
|
await this.msbInstance.base.append({
|
|
30
29
|
type: 'addAdmin',
|
|
31
30
|
key: 'admin',
|
|
@@ -57,7 +56,7 @@ export class WriterManager extends ReadyResource {
|
|
|
57
56
|
Buffer.from(nonce),
|
|
58
57
|
]
|
|
59
58
|
)
|
|
60
|
-
const hash = createHash('sha256'
|
|
59
|
+
const hash = await createHash('sha256', msg);
|
|
61
60
|
await this.msbInstance.base.append({
|
|
62
61
|
type: 'whitelist',
|
|
63
62
|
key: 'list',
|
|
@@ -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
|
}
|