trac-msb 0.2.8 → 0.2.10
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/.github/workflows/acceptance-tests.yml +7 -11
- package/.github/workflows/lint-pr-title.yml +26 -0
- package/.github/workflows/unit-tests.yml +2 -8
- package/CODE_OF_CONDUCT.md +128 -0
- package/README.md +33 -18
- package/docker-compose.yml +1 -0
- package/docs/trac_network_http_api.openapi.yaml +889 -0
- package/msb.mjs +5 -22
- package/package.json +14 -10
- package/proto/network.proto +74 -0
- package/rpc/create_server.js +2 -2
- package/rpc/handlers.js +165 -92
- package/rpc/routes/v1.js +3 -1
- package/rpc/rpc_server.js +4 -4
- package/rpc/rpc_services.js +62 -25
- package/rpc/utils/helpers.js +83 -52
- package/src/config/args.js +46 -0
- package/src/config/config.js +78 -5
- package/src/config/env.js +70 -3
- package/src/core/network/Network.js +34 -70
- package/src/core/network/identity/NetworkWalletFactory.js +2 -2
- package/src/core/network/protocols/LegacyProtocol.js +67 -0
- package/src/core/network/protocols/NetworkMessages.js +48 -0
- package/src/core/network/protocols/ProtocolInterface.js +31 -0
- package/src/core/network/protocols/ProtocolSession.js +59 -0
- package/src/core/network/protocols/V1Protocol.js +64 -0
- package/src/core/network/protocols/legacy/NetworkMessageRouter.js +84 -0
- package/src/core/network/protocols/legacy/handlers/GetRequestHandler.js +53 -0
- package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +37 -0
- package/src/core/network/{messaging → protocols/legacy}/validators/base/BaseResponse.js +2 -3
- package/src/core/network/protocols/shared/handlers/RoleOperationHandler.js +88 -0
- package/src/core/network/protocols/shared/handlers/SubnetworkOperationHandler.js +93 -0
- package/src/core/network/{messaging → protocols/shared}/handlers/TransferOperationHandler.js +17 -16
- package/src/core/network/{messaging → protocols/shared}/handlers/base/BaseOperationHandler.js +10 -15
- package/src/core/network/{messaging → protocols/shared}/validators/PartialBootstrapDeployment.js +2 -2
- package/src/core/network/{messaging → protocols/shared}/validators/PartialRoleAccess.js +5 -5
- package/src/core/network/{messaging → protocols/shared}/validators/PartialTransaction.js +4 -4
- package/src/core/network/{messaging → protocols/shared}/validators/PartialTransfer.js +4 -4
- package/src/core/network/{messaging → protocols/shared}/validators/base/PartialOperation.js +14 -12
- package/src/core/network/protocols/v1/NetworkMessageRouter.js +15 -0
- package/src/core/network/services/ConnectionManager.js +5 -5
- package/src/core/network/services/MessageOrchestrator.js +2 -2
- package/src/core/network/services/TransactionPoolService.js +5 -6
- package/src/core/network/services/TransactionRateLimiterService.js +12 -13
- package/src/core/network/services/ValidatorObserverService.js +5 -6
- package/src/core/state/State.js +3 -5
- package/src/index.js +156 -181
- package/src/messages/network/v1/NetworkMessageBuilder.js +325 -0
- package/src/messages/network/v1/NetworkMessageDirector.js +137 -0
- package/src/messages/network/v1/networkMessageFactory.js +12 -0
- package/src/messages/state/ApplyStateMessageBuilder.js +661 -0
- package/src/messages/state/ApplyStateMessageDirector.js +516 -0
- package/src/messages/state/applyStateMessageFactory.js +12 -0
- package/src/utils/buffer.js +53 -1
- package/src/utils/check.js +1 -1
- package/src/utils/cli.js +0 -8
- package/src/utils/constants.js +33 -30
- package/src/utils/fileUtils.js +13 -0
- package/src/utils/normalizers.js +84 -2
- package/src/utils/protobuf/network.cjs +840 -0
- package/src/utils/protobuf/operationHelpers.js +10 -0
- package/src/utils/type.js +26 -0
- package/tests/acceptance/v1/balance/balance.test.mjs +1 -2
- package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +26 -30
- package/tests/acceptance/v1/health/health.test.mjs +33 -0
- package/tests/acceptance/v1/rpc.test.mjs +4 -3
- package/tests/acceptance/v1/tx/tx.test.mjs +27 -16
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +26 -12
- package/tests/fixtures/check.fixtures.js +33 -32
- package/tests/fixtures/networkV1.fixtures.js +85 -0
- package/tests/fixtures/protobuf.fixtures.js +109 -25
- package/tests/helpers/StateNetworkFactory.js +2 -2
- package/tests/helpers/address.js +6 -0
- package/tests/helpers/autobaseTestHelpers.js +2 -1
- package/tests/helpers/config.js +2 -1
- package/tests/helpers/setupApplyTests.js +59 -56
- package/tests/unit/messages/messages.test.js +12 -0
- package/tests/unit/messages/network/NetworkMessageBuilder.test.js +276 -0
- package/tests/unit/messages/network/NetworkMessageDirector.test.js +201 -0
- package/tests/unit/messages/state/applyStateMessageBuilder.complete.test.js +521 -0
- package/tests/unit/messages/state/applyStateMessageBuilder.partial.test.js +233 -0
- package/tests/unit/network/ConnectionManager.test.js +6 -5
- package/tests/unit/network/networkModule.test.js +3 -2
- package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +10 -6
- package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +9 -6
- package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +10 -7
- package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +18 -21
- package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +53 -38
- package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +46 -35
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +13 -16
- package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +17 -11
- package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +11 -12
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +9 -7
- package/tests/unit/state/apply/common/commonScenarioHelper.js +15 -14
- package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +9 -4
- package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +17 -11
- package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +19 -14
- package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +37 -29
- package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +9 -7
- package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +11 -9
- package/tests/unit/unit.test.js +1 -1
- package/tests/unit/utils/buffer/buffer.test.js +62 -1
- package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +4 -3
- package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +3 -2
- package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +3 -2
- package/tests/unit/utils/normalizers/normalizers.test.js +469 -0
- package/tests/unit/utils/protobuf/operationHelpers.test.js +120 -2
- package/tests/unit/utils/type/type.test.js +25 -0
- package/tests/unit/utils/utils.test.js +1 -0
- package/docs/networking-dualstack-plan.md +0 -75
- package/docs/networking-layer-redesign.md +0 -155
- package/src/core/network/messaging/NetworkMessages.js +0 -64
- package/src/core/network/messaging/handlers/GetRequestHandler.js +0 -113
- package/src/core/network/messaging/handlers/ResponseHandler.js +0 -107
- package/src/core/network/messaging/handlers/RoleOperationHandler.js +0 -114
- package/src/core/network/messaging/handlers/SubnetworkOperationHandler.js +0 -149
- package/src/core/network/messaging/routes/NetworkMessageRouter.js +0 -98
- package/src/core/network/messaging/validators/AdminResponse.js +0 -58
- package/src/core/network/messaging/validators/CustomNodeResponse.js +0 -46
- package/src/messages/base/StateBuilder.js +0 -25
- package/src/messages/completeStateMessages/CompleteStateMessageBuilder.js +0 -425
- package/src/messages/completeStateMessages/CompleteStateMessageDirector.js +0 -252
- package/src/messages/completeStateMessages/CompleteStateMessageOperations.js +0 -296
- package/src/messages/partialStateMessages/PartialStateMessageBuilder.js +0 -272
- package/src/messages/partialStateMessages/PartialStateMessageDirector.js +0 -137
- package/src/messages/partialStateMessages/PartialStateMessageOperations.js +0 -138
- package/tests/integration/apply/addAdmin/addAdminBasic.test.js +0 -69
- package/tests/integration/apply/addAdmin/addAdminRecovery.test.js +0 -126
- package/tests/integration/apply/addIndexer.test.js +0 -239
- package/tests/integration/apply/addWhitelist.test.js +0 -53
- package/tests/integration/apply/addWriter.test.js +0 -245
- package/tests/integration/apply/apply.test.js +0 -19
- package/tests/integration/apply/banValidator.test.js +0 -116
- package/tests/integration/apply/postTx/invalidSubValues.test.js +0 -103
- package/tests/integration/apply/postTx/postTx.test.js +0 -196
- package/tests/integration/apply/removeIndexer.test.js +0 -132
- package/tests/integration/apply/removeWriter.test.js +0 -168
- package/tests/integration/apply/transfer.test.js +0 -83
- package/tests/integration/integration.test.js +0 -9
- package/tests/unit/messageOperations/assembleAddIndexerMessage.test.js +0 -21
- package/tests/unit/messageOperations/assembleAddWriterMessage.test.js +0 -17
- package/tests/unit/messageOperations/assembleAdminMessage.test.js +0 -68
- package/tests/unit/messageOperations/assembleBanWriterMessage.test.js +0 -17
- package/tests/unit/messageOperations/assemblePostTransaction.test.js +0 -424
- package/tests/unit/messageOperations/assembleRemoveIndexerMessage.test.js +0 -19
- package/tests/unit/messageOperations/assembleRemoveWriterMessage.test.js +0 -17
- package/tests/unit/messageOperations/assembleWhitelistMessages.test.js +0 -59
- package/tests/unit/messageOperations/commonsStateMessageOperationsTest.js +0 -278
- package/tests/unit/messageOperations/stateMessageOperations.test.js +0 -19
- /package/src/core/network/{messaging → protocols/legacy}/validators/ValidatorResponse.js +0 -0
- /package/src/utils/{operations.js → applyOperations.js} +0 -0
|
@@ -1,425 +0,0 @@
|
|
|
1
|
-
import b4a from 'b4a';
|
|
2
|
-
import PeerWallet from 'trac-wallet';
|
|
3
|
-
|
|
4
|
-
import StateBuilder from '../base/StateBuilder.js'
|
|
5
|
-
import {createMessage} from '../../utils/buffer.js';
|
|
6
|
-
import {OperationType} from '../../utils/protobuf/applyOperations.cjs'
|
|
7
|
-
import {addressToBuffer, bufferToAddress} from '../../core/state/utils/address.js';
|
|
8
|
-
import {isAddressValid} from "../../core/state/utils/address.js";
|
|
9
|
-
import {
|
|
10
|
-
isCoreAdmin,
|
|
11
|
-
isAdminControl,
|
|
12
|
-
isRoleAccess,
|
|
13
|
-
isTransaction,
|
|
14
|
-
isBootstrapDeployment,
|
|
15
|
-
isTransfer,
|
|
16
|
-
isBalanceInitialization
|
|
17
|
-
} from '../../utils/operations.js';
|
|
18
|
-
|
|
19
|
-
class CompleteStateMessageBuilder extends StateBuilder {
|
|
20
|
-
#wallet;
|
|
21
|
-
#config
|
|
22
|
-
#operationType;
|
|
23
|
-
#address;
|
|
24
|
-
#writingKey;
|
|
25
|
-
#payload;
|
|
26
|
-
#txHash;
|
|
27
|
-
#incomingAddress;
|
|
28
|
-
#incomingWriterKey;
|
|
29
|
-
#incomingNonce;
|
|
30
|
-
#contentHash;
|
|
31
|
-
#incomingSignature;
|
|
32
|
-
#externalBootstrap;
|
|
33
|
-
#channel;
|
|
34
|
-
#msbBootstrap;
|
|
35
|
-
#validatorNonce;
|
|
36
|
-
#txValidity;
|
|
37
|
-
#amount;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
*
|
|
41
|
-
* @param {PeerWallet} wallet
|
|
42
|
-
* @param {Config} config
|
|
43
|
-
*/
|
|
44
|
-
constructor(wallet, config) {
|
|
45
|
-
super();
|
|
46
|
-
this.#config = config;
|
|
47
|
-
if (!wallet || typeof wallet !== 'object') {
|
|
48
|
-
throw new Error('Wallet must be a valid wallet object');
|
|
49
|
-
}
|
|
50
|
-
if (!isAddressValid(wallet.address, this.#config.addressPrefix)) {
|
|
51
|
-
throw new Error('Wallet should have a valid TRAC address.');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
this.#wallet = wallet;
|
|
55
|
-
this.reset();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
reset() {
|
|
59
|
-
this.#operationType = OperationType.UNKNOWN;
|
|
60
|
-
this.#address = null;
|
|
61
|
-
this.#writingKey = null;
|
|
62
|
-
this.#payload = {};
|
|
63
|
-
this.#txHash = null;
|
|
64
|
-
this.#incomingAddress = null;
|
|
65
|
-
this.#incomingWriterKey = null;
|
|
66
|
-
this.#incomingNonce = null;
|
|
67
|
-
this.#contentHash = null;
|
|
68
|
-
this.#incomingSignature = null;
|
|
69
|
-
this.#externalBootstrap = null;
|
|
70
|
-
this.#channel = null;
|
|
71
|
-
this.#msbBootstrap = null;
|
|
72
|
-
this.#validatorNonce = null;
|
|
73
|
-
this.#txValidity = null;
|
|
74
|
-
this.#amount = null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
forOperationType(operationType) {
|
|
78
|
-
if (!Object.values(OperationType).includes(operationType) || OperationType === OperationType.UNKNOWN) {
|
|
79
|
-
throw new Error(`Invalid operation type: ${operationType}`);
|
|
80
|
-
}
|
|
81
|
-
this.#operationType = operationType;
|
|
82
|
-
this.#payload.type = operationType;
|
|
83
|
-
return this;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
withAddress(address) {
|
|
87
|
-
if (b4a.isBuffer(address) && address.length === this.#config.addressLength) {
|
|
88
|
-
address = bufferToAddress(address, this.#config.addressPrefix);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (!isAddressValid(address, this.#config.addressPrefix)) {
|
|
92
|
-
throw new Error(`Address field must be a valid TRAC bech32m address with length ${this.#config.addressLength}.`);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
this.#address = addressToBuffer(address, this.#config.addressPrefix);
|
|
96
|
-
this.#payload.address = this.#address;
|
|
97
|
-
return this;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
withWriterKey(writingKey) {
|
|
101
|
-
if (!b4a.isBuffer(writingKey) || writingKey.length !== 32) {
|
|
102
|
-
throw new Error('Writer key must be a 32 length buffer.');
|
|
103
|
-
}
|
|
104
|
-
this.#writingKey = writingKey;
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
withTxHash(txHash) {
|
|
109
|
-
if (!b4a.isBuffer(txHash) || txHash.length !== 32) {
|
|
110
|
-
throw new Error('Transaction hash must be a 32-byte buffer.');
|
|
111
|
-
}
|
|
112
|
-
this.#txHash = txHash;
|
|
113
|
-
return this;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
withIncomingAddress(address) {
|
|
117
|
-
if (b4a.isBuffer(address) && address.length === this.#config.addressLength) {
|
|
118
|
-
address = bufferToAddress(address, this.#config.addressPrefix);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
if (!isAddressValid(address, this.#config.addressPrefix)) {
|
|
122
|
-
throw new Error(`Address field must be a valid TRAC bech32m address with length ${this.#config.addressLength}.`);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
this.#incomingAddress = addressToBuffer(address, this.#config.addressPrefix);
|
|
126
|
-
return this;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
withIncomingWriterKey(writerKey) {
|
|
130
|
-
if (!b4a.isBuffer(writerKey) || writerKey.length !== 32) {
|
|
131
|
-
throw new Error('Incoming writer key must be a 32-byte buffer.');
|
|
132
|
-
}
|
|
133
|
-
this.#incomingWriterKey = writerKey;
|
|
134
|
-
return this;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
withIncomingNonce(nonce) {
|
|
138
|
-
if (!b4a.isBuffer(nonce) || nonce.length !== 32) {
|
|
139
|
-
throw new Error('Incoming nonce must be a 32-byte buffer.');
|
|
140
|
-
}
|
|
141
|
-
this.#incomingNonce = nonce;
|
|
142
|
-
return this;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
withContentHash(contentHash) {
|
|
146
|
-
if (!b4a.isBuffer(contentHash) || contentHash.length !== 32) {
|
|
147
|
-
throw new Error('Content hash must be a 32-byte buffer.');
|
|
148
|
-
}
|
|
149
|
-
this.#contentHash = contentHash;
|
|
150
|
-
return this;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
withIncomingSignature(signature) {
|
|
154
|
-
if (!b4a.isBuffer(signature) || signature.length !== 64) {
|
|
155
|
-
throw new Error('Incoming signature must be a 64-byte buffer.');
|
|
156
|
-
}
|
|
157
|
-
this.#incomingSignature = signature;
|
|
158
|
-
return this;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
withExternalBootstrap(bootstrapKey) {
|
|
162
|
-
if (!b4a.isBuffer(bootstrapKey) || bootstrapKey.length !== 32) {
|
|
163
|
-
throw new Error('Bootstrap key must be a 32-byte buffer.');
|
|
164
|
-
}
|
|
165
|
-
this.#externalBootstrap = bootstrapKey;
|
|
166
|
-
return this;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
withMsbBootstrap(msbBootstrap) {
|
|
170
|
-
if (!b4a.isBuffer(msbBootstrap) || msbBootstrap.length !== 32) {
|
|
171
|
-
throw new Error('MSB bootstrap must be a 32-byte buffer.');
|
|
172
|
-
}
|
|
173
|
-
this.#msbBootstrap = msbBootstrap;
|
|
174
|
-
return this;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
withChannel(channel) {
|
|
178
|
-
if (!b4a.isBuffer(channel) || channel.length !== 32) {
|
|
179
|
-
throw new Error('Channel must be a 32-byte buffer.');
|
|
180
|
-
}
|
|
181
|
-
this.#channel = channel;
|
|
182
|
-
return this;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
withTxValidity(txValidity) {
|
|
186
|
-
if (!b4a.isBuffer(txValidity) || txValidity.length !== 32) {
|
|
187
|
-
throw new Error('Transaction validity must be a 32-byte buffer.');
|
|
188
|
-
}
|
|
189
|
-
this.#txValidity = txValidity;
|
|
190
|
-
return this;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
withAmount(amount) {
|
|
194
|
-
if (!b4a.isBuffer(amount) || amount.length !== 16) {
|
|
195
|
-
throw new Error('Amount must be a 16-byte buffer.');
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
this.#amount = amount;
|
|
199
|
-
return this;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
async buildValueAndSign() {
|
|
203
|
-
if (!this.#operationType || !this.#address) {
|
|
204
|
-
throw new Error('Operation type, address must be set before building the message.');
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (this.#operationType === OperationType.UNKNOWN) {
|
|
208
|
-
throw new Error('UNKNOWN is not allowed to construct');
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const nonce = PeerWallet.generateNonce();
|
|
212
|
-
|
|
213
|
-
let msg = null;
|
|
214
|
-
let tx = null;
|
|
215
|
-
let signature = null;
|
|
216
|
-
|
|
217
|
-
// all incoming data from setters should be as buffer data type, createMessage accept only buffer and uint32
|
|
218
|
-
switch (this.#operationType) {
|
|
219
|
-
// Complete by default
|
|
220
|
-
case OperationType.ADD_ADMIN:
|
|
221
|
-
case OperationType.DISABLE_INITIALIZATION:
|
|
222
|
-
msg = createMessage(
|
|
223
|
-
this.#config.networkId,
|
|
224
|
-
this.#txValidity,
|
|
225
|
-
this.#writingKey,
|
|
226
|
-
nonce,
|
|
227
|
-
this.#operationType);
|
|
228
|
-
break;
|
|
229
|
-
// Complete by default
|
|
230
|
-
case OperationType.BALANCE_INITIALIZATION:
|
|
231
|
-
if (!this.#incomingAddress || !this.#amount || !this.#txValidity || !this.#address) {
|
|
232
|
-
throw new Error('All balance initialization fields must be set before building the message!');
|
|
233
|
-
}
|
|
234
|
-
msg = createMessage(
|
|
235
|
-
this.#config.networkId,
|
|
236
|
-
this.#txValidity,
|
|
237
|
-
this.#incomingAddress,
|
|
238
|
-
this.#amount,
|
|
239
|
-
nonce,
|
|
240
|
-
this.#operationType
|
|
241
|
-
);
|
|
242
|
-
break;
|
|
243
|
-
// Partial need to be signed
|
|
244
|
-
case OperationType.ADD_WRITER:
|
|
245
|
-
case OperationType.REMOVE_WRITER:
|
|
246
|
-
case OperationType.ADMIN_RECOVERY:
|
|
247
|
-
msg = createMessage(
|
|
248
|
-
this.#config.networkId,
|
|
249
|
-
this.#txHash,
|
|
250
|
-
nonce,
|
|
251
|
-
this.#operationType
|
|
252
|
-
);
|
|
253
|
-
break;
|
|
254
|
-
// Complete by default
|
|
255
|
-
case OperationType.APPEND_WHITELIST:
|
|
256
|
-
case OperationType.ADD_INDEXER:
|
|
257
|
-
case OperationType.REMOVE_INDEXER:
|
|
258
|
-
case OperationType.BAN_VALIDATOR:
|
|
259
|
-
if (this.#wallet.address === bufferToAddress(this.#incomingAddress, this.#config.addressPrefix)) {
|
|
260
|
-
throw new Error('Address must not be the same as the wallet address for basic operations.');
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
msg = createMessage(
|
|
264
|
-
this.#config.networkId,
|
|
265
|
-
this.#txValidity,
|
|
266
|
-
this.#incomingAddress,
|
|
267
|
-
nonce,
|
|
268
|
-
this.#operationType
|
|
269
|
-
);
|
|
270
|
-
|
|
271
|
-
break;
|
|
272
|
-
// Partial need to be signed
|
|
273
|
-
case OperationType.BOOTSTRAP_DEPLOYMENT:
|
|
274
|
-
if (!this.#txHash || !this.#externalBootstrap || !this.#channel || !this.#incomingNonce || !this.#incomingSignature) {
|
|
275
|
-
throw new Error('All bootstrap deployment fields must be set before building the message!');
|
|
276
|
-
}
|
|
277
|
-
msg = createMessage(
|
|
278
|
-
this.#config.networkId,
|
|
279
|
-
this.#txHash,
|
|
280
|
-
nonce,
|
|
281
|
-
this.#operationType
|
|
282
|
-
);
|
|
283
|
-
break;
|
|
284
|
-
|
|
285
|
-
// Partial need to be signed
|
|
286
|
-
case OperationType.TX:
|
|
287
|
-
if (!this.#txHash || !this.#txValidity || !this.#address || !this.#incomingWriterKey ||
|
|
288
|
-
!this.#incomingNonce || !this.#contentHash || !this.#incomingSignature ||
|
|
289
|
-
!this.#externalBootstrap || !this.#msbBootstrap) {
|
|
290
|
-
throw new Error('All postTx fields must be set before building the message!');
|
|
291
|
-
}
|
|
292
|
-
msg = createMessage(
|
|
293
|
-
this.#config.networkId,
|
|
294
|
-
this.#txHash,
|
|
295
|
-
nonce,
|
|
296
|
-
this.#operationType
|
|
297
|
-
);
|
|
298
|
-
break;
|
|
299
|
-
|
|
300
|
-
case OperationType.TRANSFER:
|
|
301
|
-
if (!this.#txHash || !this.#txValidity || !this.#address || !this.#incomingNonce ||
|
|
302
|
-
!this.#incomingSignature || !this.#amount || !this.#incomingAddress) {
|
|
303
|
-
throw new Error('All transfer fields must be set before building the message!');
|
|
304
|
-
}
|
|
305
|
-
msg = createMessage(
|
|
306
|
-
this.#config.networkId,
|
|
307
|
-
this.#txHash,
|
|
308
|
-
nonce,
|
|
309
|
-
this.#operationType
|
|
310
|
-
);
|
|
311
|
-
break;
|
|
312
|
-
|
|
313
|
-
default:
|
|
314
|
-
throw new Error(`Unsupported operation type for building value: ${OperationType[this.#operationType]}.`);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
tx = await PeerWallet.blake3(msg);
|
|
318
|
-
signature = this.#wallet.sign(tx);
|
|
319
|
-
|
|
320
|
-
if (isCoreAdmin(this.#operationType)) {
|
|
321
|
-
this.#payload.cao = {
|
|
322
|
-
tx: tx,
|
|
323
|
-
txv: this.#txValidity,
|
|
324
|
-
iw: this.#writingKey,
|
|
325
|
-
in: nonce,
|
|
326
|
-
is: signature
|
|
327
|
-
};
|
|
328
|
-
} else if (isAdminControl(this.#operationType)) {
|
|
329
|
-
this.#payload.aco = {
|
|
330
|
-
tx: tx,
|
|
331
|
-
txv: this.#txValidity,
|
|
332
|
-
ia: this.#incomingAddress,
|
|
333
|
-
in: nonce,
|
|
334
|
-
is: signature
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
else if (isRoleAccess(this.#operationType)) {
|
|
338
|
-
this.#payload.rao = {
|
|
339
|
-
tx: this.#txHash,
|
|
340
|
-
txv: this.#txValidity,
|
|
341
|
-
iw: this.#incomingWriterKey,
|
|
342
|
-
in: this.#incomingNonce,
|
|
343
|
-
is: this.#incomingSignature,
|
|
344
|
-
va: addressToBuffer(this.#wallet.address, this.#config.addressPrefix),
|
|
345
|
-
vn: nonce,
|
|
346
|
-
vs: signature,
|
|
347
|
-
};
|
|
348
|
-
} else if (isTransaction(this.#operationType)) {
|
|
349
|
-
this.#payload.txo = {
|
|
350
|
-
tx: this.#txHash,
|
|
351
|
-
txv: this.#txValidity,
|
|
352
|
-
iw: this.#incomingWriterKey,
|
|
353
|
-
ch: this.#contentHash,
|
|
354
|
-
bs: this.#externalBootstrap,
|
|
355
|
-
mbs: this.#msbBootstrap,
|
|
356
|
-
in: this.#incomingNonce,
|
|
357
|
-
is: this.#incomingSignature,
|
|
358
|
-
va: addressToBuffer(this.#wallet.address, this.#config.addressPrefix),
|
|
359
|
-
vn: nonce,
|
|
360
|
-
vs: signature,
|
|
361
|
-
};
|
|
362
|
-
} else if (isBootstrapDeployment(this.#operationType)) {
|
|
363
|
-
this.#payload.bdo = {
|
|
364
|
-
tx: this.#txHash,
|
|
365
|
-
txv: this.#txValidity,
|
|
366
|
-
bs: this.#externalBootstrap,
|
|
367
|
-
ic: this.#channel,
|
|
368
|
-
in: this.#incomingNonce,
|
|
369
|
-
is: this.#incomingSignature,
|
|
370
|
-
va: addressToBuffer(this.#wallet.address, this.#config.addressPrefix),
|
|
371
|
-
vn: nonce,
|
|
372
|
-
vs: signature
|
|
373
|
-
}
|
|
374
|
-
} else if (isTransfer(this.#operationType)) {
|
|
375
|
-
this.#payload.tro = {
|
|
376
|
-
tx: this.#txHash,
|
|
377
|
-
txv: this.#txValidity,
|
|
378
|
-
to: this.#incomingAddress,
|
|
379
|
-
am: this.#amount,
|
|
380
|
-
in: this.#incomingNonce,
|
|
381
|
-
is: this.#incomingSignature,
|
|
382
|
-
va: addressToBuffer(this.#wallet.address, this.#config.addressPrefix),
|
|
383
|
-
vn: nonce,
|
|
384
|
-
vs: signature
|
|
385
|
-
}
|
|
386
|
-
} else if (isBalanceInitialization(this.#operationType)) {
|
|
387
|
-
this.#payload.bio = {
|
|
388
|
-
tx: tx,
|
|
389
|
-
txv: this.#txValidity,
|
|
390
|
-
ia: this.#incomingAddress,
|
|
391
|
-
am: this.#amount,
|
|
392
|
-
in: nonce,
|
|
393
|
-
is: signature
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
else {
|
|
397
|
-
throw new Error(`No corresponding value type for operation: ${OperationType[this.#operationType]}.`);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
return this;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
getPayload() {
|
|
404
|
-
if (
|
|
405
|
-
!this.#payload.type ||
|
|
406
|
-
!this.#payload.address ||
|
|
407
|
-
(
|
|
408
|
-
!this.#payload.cao &&
|
|
409
|
-
!this.#payload.aco &&
|
|
410
|
-
!this.#payload.rao &&
|
|
411
|
-
!this.#payload.txo &&
|
|
412
|
-
!this.#payload.bdo &&
|
|
413
|
-
!this.#payload.tro &&
|
|
414
|
-
!this.#payload.bio
|
|
415
|
-
)
|
|
416
|
-
) {
|
|
417
|
-
throw new Error('Product is not fully assembled. Missing type, address, or value (cao/aco/rao/txo/bdo/tro/bio).');
|
|
418
|
-
}
|
|
419
|
-
const res = this.#payload;
|
|
420
|
-
this.reset();
|
|
421
|
-
return res;
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
export default CompleteStateMessageBuilder;
|
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import StateBuilder from '../base/StateBuilder.js'
|
|
2
|
-
import { OperationType } from '../../utils/protobuf/applyOperations.cjs'
|
|
3
|
-
|
|
4
|
-
class CompleteStateMessageDirector {
|
|
5
|
-
#builder;
|
|
6
|
-
|
|
7
|
-
set builder(builderInstance) {
|
|
8
|
-
if (!(builderInstance instanceof StateBuilder)) {
|
|
9
|
-
throw new Error('Director requires a Builder instance.');
|
|
10
|
-
}
|
|
11
|
-
this.#builder = builderInstance;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async buildAddAdminMessage(invokerAddress, writingKey, txValidity) {
|
|
15
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
16
|
-
|
|
17
|
-
await this.#builder
|
|
18
|
-
.forOperationType(OperationType.ADD_ADMIN)
|
|
19
|
-
.withAddress(invokerAddress)
|
|
20
|
-
.withWriterKey(writingKey)
|
|
21
|
-
.withTxValidity(txValidity)
|
|
22
|
-
.buildValueAndSign();
|
|
23
|
-
|
|
24
|
-
return this.#builder.getPayload();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async buildDisableInitializationMessage(invokerAddress, writingKey, txValidity) {
|
|
28
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
29
|
-
|
|
30
|
-
await this.#builder
|
|
31
|
-
.forOperationType(OperationType.DISABLE_INITIALIZATION)
|
|
32
|
-
.withAddress(invokerAddress)
|
|
33
|
-
.withWriterKey(writingKey)
|
|
34
|
-
.withTxValidity(txValidity)
|
|
35
|
-
.buildValueAndSign();
|
|
36
|
-
|
|
37
|
-
return this.#builder.getPayload();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async buildBalanceInitializationMessage(invokerAddress, recipientAddress, amount, txValidity) {
|
|
41
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
42
|
-
await this.#builder
|
|
43
|
-
.forOperationType(OperationType.BALANCE_INITIALIZATION)
|
|
44
|
-
.withAddress(invokerAddress)
|
|
45
|
-
.withIncomingAddress(recipientAddress)
|
|
46
|
-
.withAmount(amount)
|
|
47
|
-
.withTxValidity(txValidity)
|
|
48
|
-
.buildValueAndSign();
|
|
49
|
-
|
|
50
|
-
return this.#builder.getPayload();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async buildAppendWhitelistMessage(invokerAddress, incomingAddress, txValidity) {
|
|
54
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
55
|
-
|
|
56
|
-
await this.#builder
|
|
57
|
-
.forOperationType(OperationType.APPEND_WHITELIST)
|
|
58
|
-
.withAddress(invokerAddress)
|
|
59
|
-
.withTxValidity(txValidity)
|
|
60
|
-
.withIncomingAddress(incomingAddress)
|
|
61
|
-
.buildValueAndSign();
|
|
62
|
-
|
|
63
|
-
return this.#builder.getPayload();
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async buildAddWriterMessage(
|
|
67
|
-
invokerAddress,
|
|
68
|
-
txHash,
|
|
69
|
-
txValidity,
|
|
70
|
-
incomingWritingKey,
|
|
71
|
-
incomingNonce,
|
|
72
|
-
incomingSignature
|
|
73
|
-
) {
|
|
74
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
75
|
-
|
|
76
|
-
await this.#builder
|
|
77
|
-
.forOperationType(OperationType.ADD_WRITER)
|
|
78
|
-
.withAddress(invokerAddress)
|
|
79
|
-
.withTxHash(txHash)
|
|
80
|
-
.withTxValidity(txValidity)
|
|
81
|
-
.withIncomingWriterKey(incomingWritingKey)
|
|
82
|
-
.withIncomingNonce(incomingNonce)
|
|
83
|
-
.withIncomingSignature(incomingSignature)
|
|
84
|
-
.buildValueAndSign();
|
|
85
|
-
|
|
86
|
-
return this.#builder.getPayload();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async buildRemoveWriterMessage(
|
|
90
|
-
invokerAddress,
|
|
91
|
-
txHash,
|
|
92
|
-
txValidity,
|
|
93
|
-
incomingWritingKey,
|
|
94
|
-
incomingNonce,
|
|
95
|
-
incomingSignature
|
|
96
|
-
) {
|
|
97
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
98
|
-
|
|
99
|
-
await this.#builder
|
|
100
|
-
.forOperationType(OperationType.REMOVE_WRITER)
|
|
101
|
-
.withAddress(invokerAddress)
|
|
102
|
-
.withTxHash(txHash)
|
|
103
|
-
.withTxValidity(txValidity)
|
|
104
|
-
.withIncomingWriterKey(incomingWritingKey)
|
|
105
|
-
.withIncomingNonce(incomingNonce)
|
|
106
|
-
.withIncomingSignature(incomingSignature)
|
|
107
|
-
.buildValueAndSign();
|
|
108
|
-
|
|
109
|
-
return this.#builder.getPayload();
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async buildAdminRecoveryMessage(
|
|
113
|
-
invokerAddress,
|
|
114
|
-
txHash,
|
|
115
|
-
txValidity,
|
|
116
|
-
incomingWritingKey,
|
|
117
|
-
incomingNonce,
|
|
118
|
-
incomingSignature
|
|
119
|
-
) {
|
|
120
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
121
|
-
|
|
122
|
-
await this.#builder
|
|
123
|
-
.forOperationType(OperationType.ADMIN_RECOVERY)
|
|
124
|
-
.withAddress(invokerAddress)
|
|
125
|
-
.withTxHash(txHash)
|
|
126
|
-
.withTxValidity(txValidity)
|
|
127
|
-
.withIncomingWriterKey(incomingWritingKey)
|
|
128
|
-
.withIncomingNonce(incomingNonce)
|
|
129
|
-
.withIncomingSignature(incomingSignature)
|
|
130
|
-
.buildValueAndSign();
|
|
131
|
-
|
|
132
|
-
return this.#builder.getPayload();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async buildAddIndexerMessage(invokerAddress, incomingAddress, txValidity) {
|
|
136
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
137
|
-
|
|
138
|
-
await this.#builder
|
|
139
|
-
.forOperationType(OperationType.ADD_INDEXER)
|
|
140
|
-
.withAddress(invokerAddress)
|
|
141
|
-
.withTxValidity(txValidity)
|
|
142
|
-
.withIncomingAddress(incomingAddress)
|
|
143
|
-
.buildValueAndSign();
|
|
144
|
-
|
|
145
|
-
return this.#builder.getPayload();
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async buildRemoveIndexerMessage(invokerAddress, incomingAddress, txValidity) {
|
|
149
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
150
|
-
await this.#builder
|
|
151
|
-
.forOperationType(OperationType.REMOVE_INDEXER)
|
|
152
|
-
.withAddress(invokerAddress)
|
|
153
|
-
.withTxValidity(txValidity)
|
|
154
|
-
.withIncomingAddress(incomingAddress)
|
|
155
|
-
.buildValueAndSign();
|
|
156
|
-
|
|
157
|
-
return this.#builder.getPayload();
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
async buildBanWriterMessage(invokerAddress, incomingAddress, txValidity) {
|
|
161
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
162
|
-
|
|
163
|
-
await this.#builder
|
|
164
|
-
.forOperationType(OperationType.BAN_VALIDATOR)
|
|
165
|
-
.withAddress(invokerAddress)
|
|
166
|
-
.withTxValidity(txValidity)
|
|
167
|
-
.withIncomingAddress(incomingAddress)
|
|
168
|
-
.buildValueAndSign();
|
|
169
|
-
|
|
170
|
-
return this.#builder.getPayload();
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async buildTransactionOperationMessage(
|
|
174
|
-
invokerAddress,
|
|
175
|
-
txHash,
|
|
176
|
-
txValidity,
|
|
177
|
-
incomingWriterKey,
|
|
178
|
-
incomingNonce,
|
|
179
|
-
contentHash,
|
|
180
|
-
incomingSignature,
|
|
181
|
-
externalBootstrap,
|
|
182
|
-
msbBootstrap,
|
|
183
|
-
) {
|
|
184
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
185
|
-
await this.#builder
|
|
186
|
-
.forOperationType(OperationType.TX)
|
|
187
|
-
.withAddress(invokerAddress)
|
|
188
|
-
.withTxHash(txHash)
|
|
189
|
-
.withTxValidity(txValidity)
|
|
190
|
-
.withIncomingWriterKey(incomingWriterKey)
|
|
191
|
-
.withIncomingNonce(incomingNonce)
|
|
192
|
-
.withContentHash(contentHash)
|
|
193
|
-
.withIncomingSignature(incomingSignature)
|
|
194
|
-
.withExternalBootstrap(externalBootstrap)
|
|
195
|
-
.withMsbBootstrap(msbBootstrap)
|
|
196
|
-
.buildValueAndSign();
|
|
197
|
-
|
|
198
|
-
return this.#builder.getPayload();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async buildBootstrapDeploymentMessage(
|
|
202
|
-
invokerAddress,
|
|
203
|
-
transactionHash,
|
|
204
|
-
txValidity,
|
|
205
|
-
externalBootstrap,
|
|
206
|
-
channel,
|
|
207
|
-
incomingNonce,
|
|
208
|
-
incomingSignature
|
|
209
|
-
) {
|
|
210
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
211
|
-
|
|
212
|
-
await this.#builder
|
|
213
|
-
.forOperationType(OperationType.BOOTSTRAP_DEPLOYMENT)
|
|
214
|
-
.withAddress(invokerAddress)
|
|
215
|
-
.withTxHash(transactionHash)
|
|
216
|
-
.withTxValidity(txValidity)
|
|
217
|
-
.withExternalBootstrap(externalBootstrap)
|
|
218
|
-
.withChannel(channel)
|
|
219
|
-
.withIncomingNonce(incomingNonce)
|
|
220
|
-
.withIncomingSignature(incomingSignature)
|
|
221
|
-
.buildValueAndSign();
|
|
222
|
-
|
|
223
|
-
return this.#builder.getPayload();
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
async buildTransferOperationMessage(
|
|
227
|
-
invokerAddress,
|
|
228
|
-
transactionHash,
|
|
229
|
-
txValidity,
|
|
230
|
-
incomingNonce,
|
|
231
|
-
recipientAddress,
|
|
232
|
-
amount,
|
|
233
|
-
incomingSignature
|
|
234
|
-
) {
|
|
235
|
-
if (!this.#builder) throw new Error('Builder has not been set.');
|
|
236
|
-
await this.#builder
|
|
237
|
-
.forOperationType(OperationType.TRANSFER)
|
|
238
|
-
.withAddress(invokerAddress)
|
|
239
|
-
.withTxHash(transactionHash)
|
|
240
|
-
.withTxValidity(txValidity)
|
|
241
|
-
.withIncomingNonce(incomingNonce)
|
|
242
|
-
.withIncomingAddress(recipientAddress)
|
|
243
|
-
.withAmount(amount)
|
|
244
|
-
.withIncomingSignature(incomingSignature)
|
|
245
|
-
.buildValueAndSign();
|
|
246
|
-
|
|
247
|
-
return this.#builder.getPayload();
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export default CompleteStateMessageDirector;
|