trac-msb 0.0.13 → 0.0.15
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/_stores/n1/db/000071.blob +0 -0
- package/_stores/n1/db/000073.blob +0 -0
- package/_stores/n1/db/000085.blob +0 -0
- package/_stores/n1/db/000088.blob +0 -0
- package/_stores/n1/db/000089.sst +0 -0
- package/_stores/n1/db/000090.sst +0 -0
- package/_stores/n1/db/000092.blob +0 -0
- package/_stores/n1/db/000093.sst +0 -0
- package/_stores/n1/db/000162.blob +0 -0
- package/_stores/n1/db/000165.blob +0 -0
- package/_stores/n1/db/000175.blob +0 -0
- package/_stores/n1/db/000186.blob +0 -0
- package/_stores/n1/db/000189.blob +0 -0
- package/_stores/n1/db/000195.blob +0 -0
- package/_stores/n1/db/000210.blob +0 -0
- package/_stores/n1/db/000222.blob +0 -0
- package/_stores/n1/db/000225.blob +0 -0
- package/_stores/n1/db/000229.sst +0 -0
- package/_stores/n1/db/000230.sst +0 -0
- package/_stores/n1/db/000231.blob +0 -0
- package/_stores/n1/db/000232.sst +0 -0
- package/_stores/n1/db/000245.blob +0 -0
- package/_stores/n1/db/000250.log +0 -0
- package/_stores/n1/db/000252.blob +0 -0
- package/_stores/n1/db/000253.sst +0 -0
- package/_stores/n1/db/000254.sst +0 -0
- package/_stores/n1/db/CURRENT +1 -0
- package/_stores/n1/db/IDENTITY +1 -0
- package/_stores/n1/db/LOCK +0 -0
- package/_stores/n1/db/LOG +115 -0
- package/_stores/n1/db/LOG.old.1742183021576909 +0 -0
- package/_stores/n1/db/LOG.old.1742186169260958 +0 -0
- package/_stores/n1/db/LOG.old.1742356429622171 +0 -0
- package/_stores/n1/db/LOG.old.1742356454896144 +0 -0
- package/_stores/n1/db/LOG.old.1742356784525414 +0 -0
- package/_stores/n1/db/LOG.old.1742356872553496 +0 -0
- package/_stores/n1/db/LOG.old.1742356889597303 +0 -0
- package/_stores/n1/db/LOG.old.1742357459830241 +0 -0
- package/_stores/n1/db/LOG.old.1742357505153356 +0 -0
- package/_stores/n1/db/LOG.old.1742404971790711 +0 -0
- package/_stores/n1/db/LOG.old.1742419942002515 +0 -0
- package/_stores/n1/db/LOG.old.1742420819468818 +0 -0
- package/_stores/n1/db/LOG.old.1742421141667874 +0 -0
- package/_stores/n1/db/LOG.old.1742577491693110 +0 -0
- package/_stores/n1/db/LOG.old.1742589273002581 +0 -0
- package/_stores/n1/db/LOG.old.1742641251570901 +466 -0
- package/_stores/n1/db/LOG.old.1742641819650240 +115 -0
- package/_stores/n1/db/MANIFEST-000247 +0 -0
- package/_stores/n1/db/OPTIONS-000243 +0 -0
- package/_stores/n1/db/OPTIONS-000249 +0 -0
- package/_stores/n1/db/keypair.json +1 -0
- package/_stores/n2/db/000067.blob +0 -0
- package/_stores/n2/db/000073.blob +0 -0
- package/_stores/n2/db/000081.blob +0 -0
- package/_stores/n2/db/000088.blob +0 -0
- package/_stores/n2/db/000093.sst +0 -0
- package/_stores/n2/db/000094.blob +0 -0
- package/_stores/n2/db/000095.sst +0 -0
- package/_stores/n2/db/000096.sst +0 -0
- package/_stores/n2/db/000114.blob +0 -0
- package/_stores/n2/db/000117.blob +0 -0
- package/_stores/n2/db/000127.blob +0 -0
- package/_stores/n2/db/000138.blob +0 -0
- package/_stores/n2/db/000141.blob +0 -0
- package/_stores/n2/db/000150.blob +0 -0
- package/_stores/n2/db/000166.blob +0 -0
- package/_stores/n2/db/000175.blob +0 -0
- package/_stores/n2/db/000180.sst +0 -0
- package/_stores/n2/db/000196.blob +0 -0
- package/_stores/n2/db/000201.sst +0 -0
- package/_stores/n2/db/000202.sst +0 -0
- package/_stores/n2/db/000204.sst +0 -0
- package/_stores/n2/db/000205.sst +0 -0
- package/_stores/n2/db/000206.sst +0 -0
- package/_stores/n2/db/000207.sst +0 -0
- package/_stores/n2/db/000208.log +0 -0
- package/_stores/n2/db/000209.sst +0 -0
- package/_stores/n2/db/000210.blob +0 -0
- package/_stores/n2/db/CURRENT +0 -0
- package/_stores/n2/db/IDENTITY +0 -0
- package/_stores/n2/db/LOCK +0 -0
- package/_stores/n2/db/LOG +0 -0
- package/_stores/n2/db/LOG.old.1742186170827801 +0 -0
- package/_stores/n2/db/LOG.old.1742419970256241 +0 -0
- package/_stores/n2/db/LOG.old.1742420821296937 +0 -0
- package/_stores/n2/db/LOG.old.1742421143874068 +0 -0
- package/_stores/n2/db/LOG.old.1742577493694397 +0 -0
- package/_stores/n2/db/LOG.old.1742589275454624 +0 -0
- package/_stores/n2/db/MANIFEST-000198 +0 -0
- package/_stores/n2/db/OPTIONS-000194 +0 -0
- package/_stores/n2/db/OPTIONS-000200 +0 -0
- package/_stores/n2/db/keypair.json +1 -0
- package/_stores/n3/db/000067.blob +0 -0
- package/_stores/n3/db/000073.blob +0 -0
- package/_stores/n3/db/000081.blob +0 -0
- package/_stores/n3/db/000088.blob +0 -0
- package/_stores/n3/db/000093.sst +0 -0
- package/_stores/n3/db/000094.blob +0 -0
- package/_stores/n3/db/000095.sst +0 -0
- package/_stores/n3/db/000096.sst +0 -0
- package/_stores/n3/db/000114.blob +0 -0
- package/_stores/n3/db/000117.blob +0 -0
- package/_stores/n3/db/000127.blob +0 -0
- package/_stores/n3/db/000138.blob +0 -0
- package/_stores/n3/db/000141.blob +0 -0
- package/_stores/n3/db/000150.blob +0 -0
- package/_stores/n3/db/000166.blob +0 -0
- package/_stores/n3/db/000175.blob +0 -0
- package/_stores/n3/db/000180.sst +0 -0
- package/_stores/n3/db/000196.blob +0 -0
- package/_stores/n3/db/000201.sst +0 -0
- package/_stores/n3/db/000202.sst +0 -0
- package/_stores/n3/db/000204.sst +0 -0
- package/_stores/n3/db/000205.sst +0 -0
- package/_stores/n3/db/000206.sst +0 -0
- package/_stores/n3/db/000207.sst +0 -0
- package/_stores/n3/db/000208.log +0 -0
- package/_stores/n3/db/000209.sst +0 -0
- package/_stores/n3/db/000210.blob +0 -0
- package/_stores/n3/db/CURRENT +0 -0
- package/_stores/n3/db/IDENTITY +0 -0
- package/_stores/n3/db/LOCK +0 -0
- package/_stores/n3/db/LOG +0 -0
- package/_stores/n3/db/LOG.old.1742186172888124 +0 -0
- package/_stores/n3/db/LOG.old.1742420094609648 +0 -0
- package/_stores/n3/db/LOG.old.1742420822815851 +0 -0
- package/_stores/n3/db/LOG.old.1742421145506410 +0 -0
- package/_stores/n3/db/LOG.old.1742577495315796 +0 -0
- package/_stores/n3/db/LOG.old.1742589277258083 +0 -0
- package/_stores/n3/db/MANIFEST-000198 +0 -0
- package/_stores/n3/db/OPTIONS-000194 +0 -0
- package/_stores/n3/db/OPTIONS-000200 +0 -0
- package/_stores/n3/db/keypair.json +1 -0
- package/_stores/n4/db/000067.blob +0 -0
- package/_stores/n4/db/000074.blob +0 -0
- package/_stores/n4/db/000081.blob +0 -0
- package/_stores/n4/db/000088.blob +0 -0
- package/_stores/n4/db/000093.sst +0 -0
- package/_stores/n4/db/000094.blob +0 -0
- package/_stores/n4/db/000095.sst +0 -0
- package/_stores/n4/db/000096.sst +0 -0
- package/_stores/n4/db/000114.blob +0 -0
- package/_stores/n4/db/000117.blob +0 -0
- package/_stores/n4/db/000127.blob +0 -0
- package/_stores/n4/db/000138.blob +0 -0
- package/_stores/n4/db/000141.blob +0 -0
- package/_stores/n4/db/000150.blob +0 -0
- package/_stores/n4/db/000159.blob +0 -0
- package/_stores/n4/db/000166.blob +0 -0
- package/_stores/n4/db/000175.blob +0 -0
- package/_stores/n4/db/000180.sst +0 -0
- package/_stores/n4/db/000196.blob +0 -0
- package/_stores/n4/db/000201.sst +0 -0
- package/_stores/n4/db/000202.sst +0 -0
- package/_stores/n4/db/000204.sst +0 -0
- package/_stores/n4/db/000205.sst +0 -0
- package/_stores/n4/db/000206.sst +0 -0
- package/_stores/n4/db/000207.sst +0 -0
- package/_stores/n4/db/000208.log +0 -0
- package/_stores/n4/db/000209.sst +0 -0
- package/_stores/n4/db/000210.blob +0 -0
- package/_stores/n4/db/CURRENT +0 -0
- package/_stores/n4/db/IDENTITY +0 -0
- package/_stores/n4/db/LOCK +0 -0
- package/_stores/n4/db/LOG +0 -0
- package/_stores/n4/db/LOG.old.1742186174617449 +0 -0
- package/_stores/n4/db/LOG.old.1742420096536170 +0 -0
- package/_stores/n4/db/LOG.old.1742420824264952 +0 -0
- package/_stores/n4/db/LOG.old.1742421147129074 +0 -0
- package/_stores/n4/db/LOG.old.1742577500353714 +0 -0
- package/_stores/n4/db/LOG.old.1742589279083454 +0 -0
- package/_stores/n4/db/MANIFEST-000198 +0 -0
- package/_stores/n4/db/OPTIONS-000194 +0 -0
- package/_stores/n4/db/OPTIONS-000200 +0 -0
- package/_stores/n4/db/keypair.json +0 -0
- package/_stores/n5/db/000067.blob +0 -0
- package/_stores/n5/db/000074.blob +0 -0
- package/_stores/n5/db/000081.blob +0 -0
- package/_stores/n5/db/000088.blob +0 -0
- package/_stores/n5/db/000093.sst +0 -0
- package/_stores/n5/db/000094.blob +0 -0
- package/_stores/n5/db/000095.sst +0 -0
- package/_stores/n5/db/000096.sst +0 -0
- package/_stores/n5/db/000114.blob +0 -0
- package/_stores/n5/db/000117.blob +0 -0
- package/_stores/n5/db/000127.blob +0 -0
- package/_stores/n5/db/000138.blob +0 -0
- package/_stores/n5/db/000141.blob +0 -0
- package/_stores/n5/db/000150.blob +0 -0
- package/_stores/n5/db/000166.blob +0 -0
- package/_stores/n5/db/000175.blob +0 -0
- package/_stores/n5/db/000180.sst +0 -0
- package/_stores/n5/db/000196.blob +0 -0
- package/_stores/n5/db/000201.sst +0 -0
- package/_stores/n5/db/000202.sst +0 -0
- package/_stores/n5/db/000204.sst +0 -0
- package/_stores/n5/db/000205.sst +0 -0
- package/_stores/n5/db/000206.sst +0 -0
- package/_stores/n5/db/000207.sst +0 -0
- package/_stores/n5/db/000208.log +0 -0
- package/_stores/n5/db/000209.sst +0 -0
- package/_stores/n5/db/000210.blob +0 -0
- package/_stores/n5/db/CURRENT +0 -0
- package/_stores/n5/db/IDENTITY +0 -0
- package/_stores/n5/db/LOCK +0 -0
- package/_stores/n5/db/LOG +0 -0
- package/_stores/n5/db/LOG.old.1742186176624713 +0 -0
- package/_stores/n5/db/LOG.old.1742420098319634 +0 -0
- package/_stores/n5/db/LOG.old.1742420825849458 +0 -0
- package/_stores/n5/db/LOG.old.1742421148865382 +0 -0
- package/_stores/n5/db/LOG.old.1742577501880031 +0 -0
- package/_stores/n5/db/LOG.old.1742589281491031 +0 -0
- package/_stores/n5/db/MANIFEST-000198 +0 -0
- package/_stores/n5/db/OPTIONS-000194 +0 -0
- package/_stores/n5/db/OPTIONS-000200 +0 -0
- package/_stores/n5/db/keypair.json +0 -0
- package/_stores/n6/db/000074.blob +0 -0
- package/_stores/n6/db/000081.blob +0 -0
- package/_stores/n6/db/000088.blob +0 -0
- package/_stores/n6/db/000093.sst +0 -0
- package/_stores/n6/db/000094.blob +0 -0
- package/_stores/n6/db/000095.sst +0 -0
- package/_stores/n6/db/000096.sst +0 -0
- package/_stores/n6/db/000114.blob +0 -0
- package/_stores/n6/db/000117.blob +0 -0
- package/_stores/n6/db/000127.blob +0 -0
- package/_stores/n6/db/000138.blob +0 -0
- package/_stores/n6/db/000150.blob +0 -0
- package/_stores/n6/db/000159.blob +0 -0
- package/_stores/n6/db/000166.blob +0 -0
- package/_stores/n6/db/000175.blob +0 -0
- package/_stores/n6/db/000180.sst +0 -0
- package/_stores/n6/db/000196.blob +0 -0
- package/_stores/n6/db/000201.sst +0 -0
- package/_stores/n6/db/000202.sst +0 -0
- package/_stores/n6/db/000204.sst +0 -0
- package/_stores/n6/db/000205.sst +0 -0
- package/_stores/n6/db/000206.sst +0 -0
- package/_stores/n6/db/000207.sst +0 -0
- package/_stores/n6/db/000208.log +0 -0
- package/_stores/n6/db/000209.sst +0 -0
- package/_stores/n6/db/000210.blob +0 -0
- package/_stores/n6/db/CURRENT +0 -0
- package/_stores/n6/db/IDENTITY +0 -0
- package/_stores/n6/db/LOCK +0 -0
- package/_stores/n6/db/LOG +0 -0
- package/_stores/n6/db/LOG.old.1742186178370518 +0 -0
- package/_stores/n6/db/LOG.old.1742420099918640 +0 -0
- package/_stores/n6/db/LOG.old.1742420827341589 +0 -0
- package/_stores/n6/db/LOG.old.1742421150560152 +0 -0
- package/_stores/n6/db/LOG.old.1742577503396008 +0 -0
- package/_stores/n6/db/LOG.old.1742589283821852 +0 -0
- package/_stores/n6/db/MANIFEST-000198 +0 -0
- package/_stores/n6/db/OPTIONS-000194 +0 -0
- package/_stores/n6/db/OPTIONS-000200 +0 -0
- package/_stores/n6/db/keypair.json +0 -0
- package/_stores/n7/db/000074.blob +0 -0
- package/_stores/n7/db/000081.blob +0 -0
- package/_stores/n7/db/000088.blob +0 -0
- package/_stores/n7/db/000093.sst +0 -0
- package/_stores/n7/db/000094.blob +0 -0
- package/_stores/n7/db/000095.sst +0 -0
- package/_stores/n7/db/000096.sst +0 -0
- package/_stores/n7/db/000114.blob +0 -0
- package/_stores/n7/db/000117.blob +0 -0
- package/_stores/n7/db/000127.blob +0 -0
- package/_stores/n7/db/000138.blob +0 -0
- package/_stores/n7/db/000150.blob +0 -0
- package/_stores/n7/db/000159.blob +0 -0
- package/_stores/n7/db/000166.blob +0 -0
- package/_stores/n7/db/000172.blob +0 -0
- package/_stores/n7/db/000175.blob +0 -0
- package/_stores/n7/db/000180.sst +0 -0
- package/_stores/n7/db/000193.blob +0 -0
- package/_stores/n7/db/000198.log +0 -0
- package/_stores/n7/db/000200.blob +0 -0
- package/_stores/n7/db/000201.sst +0 -0
- package/_stores/n7/db/000202.sst +0 -0
- package/_stores/n7/db/000204.sst +0 -0
- package/_stores/n7/db/000205.sst +0 -0
- package/_stores/n7/db/000206.sst +0 -0
- package/_stores/n7/db/000207.sst +0 -0
- package/_stores/n7/db/CURRENT +0 -0
- package/_stores/n7/db/IDENTITY +0 -0
- package/_stores/n7/db/LOCK +0 -0
- package/_stores/n7/db/LOG +0 -0
- package/_stores/n7/db/LOG.old.1742186180169762 +0 -0
- package/_stores/n7/db/LOG.old.1742420109317101 +0 -0
- package/_stores/n7/db/LOG.old.1742420828841380 +0 -0
- package/_stores/n7/db/LOG.old.1742421152251567 +0 -0
- package/_stores/n7/db/LOG.old.1742577512090130 +0 -0
- package/_stores/n7/db/LOG.old.1742589286130264 +0 -0
- package/_stores/n7/db/MANIFEST-000195 +0 -0
- package/_stores/n7/db/OPTIONS-000191 +0 -0
- package/_stores/n7/db/OPTIONS-000197 +0 -0
- package/_stores/n7/db/keypair.json +1 -0
- package/msb.mjs +1 -1
- package/package.json +1 -1
- package/src/functions.js +7 -64
- package/src/index.js +120 -199
- package/src/writerManager.js +0 -316
package/src/writerManager.js
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import ReadyResource from 'ready-resource';
|
|
2
|
-
|
|
3
|
-
//TODO ADD THROWS
|
|
4
|
-
//TODO FOR NOW IT'S FINE. IT MUST BE INTEGRATED WITH TRAC WALLET
|
|
5
|
-
//TODO: How about nonce if edDSA is deterministic?
|
|
6
|
-
//TODO: if enable_wallet is false then user shouldn't be allowd to add or remove writer
|
|
7
|
-
/**
|
|
8
|
-
* WriterManager manages writer nodes in the TRAC NETWORK, handling events, adding/removing writers, and managing the bootstrap process.
|
|
9
|
-
* It interacts with the MainSettlementBus instance to perform key operations.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const MS_TO_WAIT = 5000;
|
|
13
|
-
|
|
14
|
-
export class WriterManager extends ReadyResource {
|
|
15
|
-
/**
|
|
16
|
-
* Initializes the WriterManager with the given MainSettlementBus instance.
|
|
17
|
-
* @param {MainSettlementBus} msbInstance - An instance of the MainSettlementBus class.
|
|
18
|
-
*/
|
|
19
|
-
constructor(msbInstance) {
|
|
20
|
-
super();
|
|
21
|
-
this.base = msbInstance.base;
|
|
22
|
-
this.wallet = msbInstance.wallet;
|
|
23
|
-
this.swarm = msbInstance.swarm;
|
|
24
|
-
this.bootstrap = msbInstance.bootstrap;
|
|
25
|
-
this.writingKey = msbInstance.writingKey;
|
|
26
|
-
|
|
27
|
-
this.#initBootstrap(msbInstance);
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Initializes the bootstrap initialization process if the node is a bootstrap node and can be only executed by the bootstrap node.
|
|
33
|
-
* @returns {Promise<void>} Resolves once the bootstrap initialization is complete.
|
|
34
|
-
*/
|
|
35
|
-
async #initBootstrap(msbInstance) {
|
|
36
|
-
try {
|
|
37
|
-
|
|
38
|
-
if (this.writingKey && this.writingKey === this.bootstrap) {
|
|
39
|
-
|
|
40
|
-
const bootStrapEntry = await this.getBootstrapEntry();
|
|
41
|
-
if (bootStrapEntry === null) {
|
|
42
|
-
|
|
43
|
-
const message = Buffer.concat([
|
|
44
|
-
Buffer.from(JSON.stringify(this.base.localWriter.core.manifest)),
|
|
45
|
-
Buffer.from(this.writingKey, 'hex'),
|
|
46
|
-
Buffer.from(this.wallet.publicKey, 'hex')
|
|
47
|
-
]);
|
|
48
|
-
|
|
49
|
-
//SEND BOOTSTRAP TRAC MANIFEST
|
|
50
|
-
await this.base.append({
|
|
51
|
-
type: 'initBootstrap',
|
|
52
|
-
key: 'bootstrap',
|
|
53
|
-
value: {
|
|
54
|
-
wk: this.writingKey,
|
|
55
|
-
hpm: this.base.localWriter.core.manifest,
|
|
56
|
-
skp: this.wallet.publicKey,
|
|
57
|
-
pop1: this.wallet.sign(message, this.base.localWriter.core.keyPair.secretKey),
|
|
58
|
-
pop2: this.wallet.sign(message)
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
console.log(`Bootstrap node ${this.writingKey} is ready to accept writers`);
|
|
63
|
-
await this.#WriterEventListener(msbInstance);
|
|
64
|
-
}
|
|
65
|
-
} catch (error) {
|
|
66
|
-
console.error(`err in `, error);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Listens for writer events and processes them by appending the parsed request to the base.
|
|
72
|
-
* At this moment only Bootstrap is listening for writer events.
|
|
73
|
-
* @private
|
|
74
|
-
*/
|
|
75
|
-
async #WriterEventListener(msbInstance) {
|
|
76
|
-
msbInstance.on('writerEvent', async (parsedRequest) => {
|
|
77
|
-
await this.base.append(parsedRequest);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Adds the current node as a writer if it is not already a writer.
|
|
83
|
-
* The node must connect to the bootstrap node to complete the operation.
|
|
84
|
-
* @returns {Promise<void>} Resolves once the writer has been added or if the operation fails.
|
|
85
|
-
*/
|
|
86
|
-
async addMe() {
|
|
87
|
-
try {
|
|
88
|
-
|
|
89
|
-
if (this.writingKey === this.bootstrap) {
|
|
90
|
-
console.log('Bootstrap node cannot add itself');
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const writerEntry = await this.base.view.get(this.wallet.publicKey);
|
|
95
|
-
if (writerEntry !== null && writerEntry.value.isValid) {
|
|
96
|
-
console.log(`Cannot perform operation because node is already writer`);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const bootstrapEntry = await this.getBootstrapEntry();
|
|
101
|
-
if (!bootstrapEntry?.value?.hpm?.signers?.[0]?.publicKey?.data) {
|
|
102
|
-
console.log(`Bootstrap key not found`);
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const bootstrapPubKey = Buffer.from((bootstrapEntry.value.hpm.signers[0].publicKey.data)).toString('hex');
|
|
107
|
-
|
|
108
|
-
this.swarm.connections.forEach(async conn => {
|
|
109
|
-
if (conn.connected && conn.remotePublicKey.toString('hex') === bootstrapPubKey) {
|
|
110
|
-
|
|
111
|
-
const message = Buffer.concat([
|
|
112
|
-
Buffer.from(JSON.stringify(this.base.local.core.manifest)),
|
|
113
|
-
Buffer.from(this.writingKey, 'hex'),
|
|
114
|
-
Buffer.from(this.wallet.publicKey, 'hex')
|
|
115
|
-
//TODO: ADD NONCE?
|
|
116
|
-
]);
|
|
117
|
-
|
|
118
|
-
//SEND TRAC MANIFEST
|
|
119
|
-
conn.write(JSON.stringify({
|
|
120
|
-
type: 'addWriter',
|
|
121
|
-
key: this.wallet.publicKey,
|
|
122
|
-
value: {
|
|
123
|
-
wk: this.writingKey,
|
|
124
|
-
hpm: this.base.local.core.manifest,
|
|
125
|
-
pop1: this.wallet.sign(message, this.base.local.core.header.keyPair.secretKey),
|
|
126
|
-
pop2: this.wallet.sign(message),
|
|
127
|
-
//TODO: ADD NONCE?
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
}));
|
|
131
|
-
|
|
132
|
-
setTimeout(async () => {
|
|
133
|
-
const updatedWriterEntry = await this.base.view.get(this.wallet.publicKey);
|
|
134
|
-
if (updatedWriterEntry !== null && updatedWriterEntry.value.isValid) {
|
|
135
|
-
console.log(`Writer ${this.writingKey} was successfully added.`);
|
|
136
|
-
} else {
|
|
137
|
-
console.warn(`Writer ${this.writingKey} was NOT added.`);
|
|
138
|
-
}
|
|
139
|
-
}, MS_TO_WAIT);
|
|
140
|
-
}
|
|
141
|
-
})
|
|
142
|
-
} catch (error) {
|
|
143
|
-
console.error(`err in `, error);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Removes the current node as a writer.
|
|
148
|
-
* The node must connect to the bootstrap node to complete the removal operation.
|
|
149
|
-
* @returns {Promise<void>} Resolves once the writer has been removed or if the operation fails.
|
|
150
|
-
*/
|
|
151
|
-
async removeMe() {
|
|
152
|
-
try {
|
|
153
|
-
|
|
154
|
-
if (this.writingKey === this.bootstrap) {
|
|
155
|
-
console.log('Bootstrap node cannot remove itself');
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const writerEntry = await this.base.view.get(this.wallet.publicKey);
|
|
160
|
-
if (writerEntry === null || !writerEntry.value.isValid) {
|
|
161
|
-
console.log(`Your key does not exist in the database or you can't remove it`);
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const bootstrapEntry = await this.getBootstrapEntry();
|
|
166
|
-
|
|
167
|
-
if (bootstrapEntry === null || !bootstrapEntry.value?.hpm?.signers?.[0]?.publicKey?.data) {
|
|
168
|
-
console.log(`Bootstrap key not found`);
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const bootstrapPubKey = Buffer.from(bootstrapEntry.value.hpm.signers[0].publicKey.data).toString('hex');
|
|
173
|
-
|
|
174
|
-
this.swarm.connections.forEach(async conn => {
|
|
175
|
-
if (conn.connected && conn.remotePublicKey.toString('hex') === bootstrapPubKey) {
|
|
176
|
-
|
|
177
|
-
const message = Buffer.concat([
|
|
178
|
-
Buffer.from(this.wallet.publicKey, 'hex') // TODO: TO REDUCE THE SIZE OF THE MESSAGE WE CAN SEND SIMPLE STRING SUCHAS "REMOVE". You can just HASH the message and sign the hash.
|
|
179
|
-
//TODO: ADD NONCE?
|
|
180
|
-
]);
|
|
181
|
-
|
|
182
|
-
//SEND TRAC MANIFEST
|
|
183
|
-
conn.write(JSON.stringify({
|
|
184
|
-
type: 'removeWriter',
|
|
185
|
-
key: this.wallet.publicKey,
|
|
186
|
-
value: {
|
|
187
|
-
pop: this.wallet.sign(message),
|
|
188
|
-
//TODO: ADD NONCE?
|
|
189
|
-
}
|
|
190
|
-
}));
|
|
191
|
-
}
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
setTimeout(async () => {
|
|
195
|
-
const updatedWriterEntry = await this.base.view.get(this.wallet.publicKey);
|
|
196
|
-
if (updatedWriterEntry !== null && !updatedWriterEntry.value.isValid) {
|
|
197
|
-
console.log(`Key successfully removed`);
|
|
198
|
-
} else {
|
|
199
|
-
console.log(`Failed to remove key`);
|
|
200
|
-
}
|
|
201
|
-
}, MS_TO_WAIT);
|
|
202
|
-
|
|
203
|
-
} catch (error) {
|
|
204
|
-
console.error(`err in `, error);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Adds a writer node as an indexer.
|
|
211
|
-
* The node must already be a valid writer to become an indexer.
|
|
212
|
-
* This operation can only be performed by the bootstrap/admin node.
|
|
213
|
-
*
|
|
214
|
-
* @param {string} peerTracPublicKey - The TRAC public key of the writer node to be promoted to indexer.
|
|
215
|
-
* @param {string} peerWritingKey - The writing key of the writer node to be promoted to indexer.
|
|
216
|
-
* @returns {Promise<void>} Resolves once the writer has been promoted to indexer or if the operation fails.
|
|
217
|
-
*/
|
|
218
|
-
async addIndexer(peerTracPublicKey, peerWritingKey) {
|
|
219
|
-
//TODO: ADD TIMEOUT WITH MESSAGE OF SUCCESS OR FAILURE.
|
|
220
|
-
//TODO IMPROVE CHECKS
|
|
221
|
-
if (this.writingKey !== this.bootstrap) {
|
|
222
|
-
console.log('Only bootstrap node can add indexer');
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const writerEntry = await this.base.view.get(peerTracPublicKey);
|
|
227
|
-
if (writerEntry === null || writerEntry.value.isValid === false || writerEntry.value.wk !== peerWritingKey || (writerEntry.value.isValid === true && writerEntry.value.isIndexer === true)) {
|
|
228
|
-
console.log(`Writer ${peerTracPublicKey}:${this.writingKey} cannot become an indexer`);
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const message = Buffer.concat([
|
|
233
|
-
Buffer.from(peerTracPublicKey, 'hex'),
|
|
234
|
-
Buffer.from(peerWritingKey, 'hex')
|
|
235
|
-
]);
|
|
236
|
-
|
|
237
|
-
const indexerRequest = {
|
|
238
|
-
type: 'addIndexer',
|
|
239
|
-
key: this.wallet.publicKey,
|
|
240
|
-
value: {
|
|
241
|
-
ptpk: peerTracPublicKey,
|
|
242
|
-
pwk: peerWritingKey,
|
|
243
|
-
pop: this.wallet.sign(message)
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
await this.base.append(indexerRequest);
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Removes the indexer status from a writer node.
|
|
250
|
-
* The node must already be a valid writer and an indexer to lose its indexer status.
|
|
251
|
-
* This operation can only be performed by the bootstrap node.
|
|
252
|
-
*
|
|
253
|
-
* @param {string} peerTracPublicKey - The TRAC public key of the writer node to lose indexer status.
|
|
254
|
-
* @param {string} peerWritingKey - The writing key of the writer node to lose indexer status.
|
|
255
|
-
* @returns {Promise<void>} Resolves once the writer has lost indexer status or if the operation fails.
|
|
256
|
-
*/
|
|
257
|
-
async removeIndexer(peerTracPublicKey, peerWritingKey) {
|
|
258
|
-
//TODO: ADD TIMEOUT WITH MESSAGE OF SUCCESS OR FAILURE.
|
|
259
|
-
//TODO IMPROVE CHECKS
|
|
260
|
-
if (this.writingKey !== this.bootstrap) {
|
|
261
|
-
console.log('Only bootstrap node can remove indexer');
|
|
262
|
-
return;
|
|
263
|
-
}
|
|
264
|
-
const writerEntry = await this.base.view.get(peerTracPublicKey);
|
|
265
|
-
|
|
266
|
-
if (writerEntry === null || writerEntry.value.isValid === false || writerEntry.value.wk !== peerWritingKey || (writerEntry.value.isValid === true && writerEntry.value.isIndexer === false)) {
|
|
267
|
-
console.log(`Writer ${peerTracPublicKey}:${this.writingKey} cannot lose indexer status`);
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
const message = Buffer.concat([
|
|
272
|
-
Buffer.from(peerTracPublicKey, 'hex'),
|
|
273
|
-
Buffer.from(peerWritingKey, 'hex')
|
|
274
|
-
]);
|
|
275
|
-
|
|
276
|
-
const indexerRequest = {
|
|
277
|
-
type: 'removeIndexer',
|
|
278
|
-
key: this.wallet.publicKey,
|
|
279
|
-
value: {
|
|
280
|
-
ptpk: peerTracPublicKey,
|
|
281
|
-
pwk: peerWritingKey,
|
|
282
|
-
pop: this.wallet.sign(message)
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
await this.base.append(indexerRequest);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Retrieves the bootstrap entry from the base view.
|
|
290
|
-
*
|
|
291
|
-
* @returns {Promise<Object|null>} The bootstrap entry if it exists, otherwise null.
|
|
292
|
-
*/
|
|
293
|
-
async getBootstrapEntry() {
|
|
294
|
-
return await this.base.view.get('bootstrap');
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Handles incoming writer events by parsing the data and emitting a writerEvent.
|
|
299
|
-
*
|
|
300
|
-
* @param {MainSettlementBus} msbInstance - An instance of the MainSettlementBus class.
|
|
301
|
-
* @param {Buffer} data - The data received from the connection.
|
|
302
|
-
*/
|
|
303
|
-
static async handleIncomingWriterEvent(msbInstance, data) {
|
|
304
|
-
try {
|
|
305
|
-
const bufferData = data.toString();
|
|
306
|
-
const parsedRequest = JSON.parse(bufferData);
|
|
307
|
-
if (parsedRequest.type === 'addWriter' || parsedRequest.type === 'removeWriter') {
|
|
308
|
-
msbInstance.emit('writerEvent', parsedRequest);
|
|
309
|
-
}
|
|
310
|
-
} catch (error) {
|
|
311
|
-
// for now ignore the error
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
export default WriterManager;
|