trac-msb 0.2.7 → 0.2.9
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/publish.yml +8 -16
- package/msb.mjs +13 -25
- package/package.json +8 -4
- package/proto/network.proto +74 -0
- package/rpc/{create_server.mjs → create_server.js} +4 -4
- package/rpc/{handlers.mjs → handlers.js} +7 -7
- package/rpc/routes/{index.mjs → index.js} +1 -1
- package/rpc/routes/{v1.mjs → v1.js} +1 -1
- package/rpc/rpc_server.js +10 -0
- package/rpc/rpc_services.js +48 -7
- package/rpc/utils/{helpers.mjs → helpers.js} +1 -1
- package/src/config/config.js +137 -0
- package/src/config/env.js +63 -0
- package/src/core/network/Network.js +133 -119
- package/src/core/network/identity/NetworkWalletFactory.js +5 -6
- 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/ValidatorResponse.js +2 -2
- package/src/core/network/{messaging → protocols/legacy}/validators/base/BaseResponse.js +13 -6
- 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/protocols/shared/handlers/TransferOperationHandler.js +57 -0
- package/src/core/network/{messaging → protocols/shared}/handlers/base/BaseOperationHandler.js +21 -26
- package/src/core/network/{messaging → protocols/shared}/validators/PartialBootstrapDeployment.js +3 -3
- package/src/core/network/{messaging → protocols/shared}/validators/PartialRoleAccess.js +15 -12
- package/src/core/network/{messaging → protocols/shared}/validators/PartialTransaction.js +10 -11
- package/src/core/network/{messaging → protocols/shared}/validators/PartialTransfer.js +10 -7
- package/src/core/network/{messaging → protocols/shared}/validators/base/PartialOperation.js +40 -22
- package/src/core/network/protocols/v1/NetworkMessageRouter.js +15 -0
- package/src/core/network/services/ConnectionManager.js +13 -19
- package/src/core/network/services/MessageOrchestrator.js +10 -22
- package/src/core/network/services/TransactionPoolService.js +10 -10
- package/src/core/network/services/TransactionRateLimiterService.js +5 -3
- package/src/core/network/services/ValidatorObserverService.js +46 -21
- package/src/core/state/State.js +137 -141
- package/src/core/state/utils/address.js +18 -16
- package/src/core/state/utils/adminEntry.js +17 -16
- package/src/core/state/utils/deploymentEntry.js +15 -15
- package/src/core/state/utils/transaction.js +3 -95
- package/src/index.js +250 -325
- 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 +21 -17
- package/src/utils/cli.js +0 -8
- package/src/utils/cliCommands.js +11 -11
- package/src/utils/constants.js +36 -24
- package/src/utils/fileUtils.js +1 -4
- package/src/utils/helpers.js +9 -20
- package/src/utils/migrationUtils.js +2 -2
- package/src/utils/normalizers.js +94 -11
- package/src/utils/protobuf/network.cjs +840 -0
- package/src/utils/protobuf/operationHelpers.js +10 -0
- package/tests/acceptance/v1/account/account.test.mjs +2 -2
- package/tests/acceptance/v1/balance/balance.test.mjs +1 -1
- package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +11 -2
- package/tests/acceptance/v1/rpc.test.mjs +10 -10
- package/tests/acceptance/v1/tx/tx.test.mjs +4 -2
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +7 -3
- package/tests/fixtures/check.fixtures.js +42 -42
- package/tests/fixtures/networkV1.fixtures.js +84 -0
- package/tests/fixtures/protobuf.fixtures.js +110 -26
- package/tests/helpers/StateNetworkFactory.js +3 -5
- package/tests/helpers/autobaseTestHelpers.js +1 -2
- package/tests/helpers/config.js +3 -0
- package/tests/helpers/setupApplyTests.js +113 -99
- package/tests/helpers/transactionPayloads.mjs +26 -12
- 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 +203 -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 +10 -7
- package/tests/unit/network/NetworkWalletFactory.test.js +14 -14
- 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 +11 -8
- package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +11 -7
- package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +18 -20
- package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +57 -48
- package/tests/unit/state/apply/addWriter/addWriterValidatorRewardScenario.js +2 -1
- package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +72 -57
- package/tests/unit/state/apply/adminRecovery/state.apply.adminRecovery.test.js +3 -7
- package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +12 -14
- package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +18 -13
- package/tests/unit/state/apply/balanceInitialization/nodeEntryBalanceUpdateFailureScenario.js +2 -1
- package/tests/unit/state/apply/banValidator/banValidatorBanAndReWhitelistScenario.js +2 -1
- package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +27 -30
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentDuplicateRegistrationScenario.js +2 -1
- package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +24 -21
- package/tests/unit/state/apply/common/access-control/adminConsistencyMismatchScenario.js +5 -4
- package/tests/unit/state/apply/common/access-control/adminPublicKeyDecodeFailureScenario.js +4 -3
- package/tests/unit/state/apply/common/balances/base/requesterBalanceScenarioBase.js +2 -1
- package/tests/unit/state/apply/common/commonScenarioHelper.js +16 -16
- package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +10 -5
- package/tests/unit/state/apply/common/payload-structure/invalidHashValidationScenario.js +2 -2
- package/tests/unit/state/apply/common/requester/requesterNodeEntryBufferMissingScenario.js +2 -1
- package/tests/unit/state/apply/common/requester/requesterNodeEntryDecodeFailureScenario.js +2 -1
- package/tests/unit/state/apply/common/validatorConsistency/base/validatorConsistencyScenarioBase.js +2 -1
- package/tests/unit/state/apply/common/validatorEntryValidation/base/validatorEntryValidationScenarioBase.js +2 -1
- package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +16 -9
- package/tests/unit/state/apply/removeIndexer/removeIndexerScenarioHelpers.js +6 -5
- package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +23 -19
- package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +45 -36
- package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +48 -45
- package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +32 -29
- package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardScenarioFactory.js +2 -1
- package/tests/unit/state/stateModule.test.js +0 -1
- package/tests/unit/state/stateTestUtils.js +7 -3
- package/tests/unit/state/utils/address.test.js +3 -3
- package/tests/unit/state/utils/adminEntry.test.js +10 -9
- package/tests/unit/unit.test.js +1 -1
- package/tests/unit/utils/buffer/buffer.test.js +62 -1
- package/tests/unit/utils/check/adminControlOperation.test.js +3 -3
- package/tests/unit/utils/check/balanceInitializationOperation.test.js +2 -2
- package/tests/unit/utils/check/bootstrapDeploymentOperation.test.js +2 -3
- package/tests/unit/utils/check/common.test.js +7 -6
- package/tests/unit/utils/check/coreAdminOperation.test.js +3 -3
- package/tests/unit/utils/check/roleAccessOperation.test.js +3 -2
- package/tests/unit/utils/check/transactionOperation.test.js +3 -3
- package/tests/unit/utils/check/transferOperation.test.js +3 -3
- package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +2 -1
- package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +2 -1
- package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +7 -0
- package/tests/unit/utils/normalizers/normalizers.test.js +469 -0
- package/tests/unit/utils/protobuf/operationHelpers.test.js +120 -2
- package/tests/unit/utils/utils.test.js +0 -1
- package/rpc/rpc_server.mjs +0 -10
- package/src/core/network/messaging/NetworkMessages.js +0 -63
- package/src/core/network/messaging/handlers/GetRequestHandler.js +0 -112
- package/src/core/network/messaging/handlers/ResponseHandler.js +0 -108
- package/src/core/network/messaging/handlers/RoleOperationHandler.js +0 -116
- package/src/core/network/messaging/handlers/SubnetworkOperationHandler.js +0 -143
- package/src/core/network/messaging/handlers/TransferOperationHandler.js +0 -52
- package/src/core/network/messaging/routes/NetworkMessageRouter.js +0 -94
- package/src/core/network/messaging/validators/AdminResponse.js +0 -58
- package/src/core/network/messaging/validators/CustomNodeResponse.js +0 -46
- package/src/core/state/utils/indexerEntry.js +0 -105
- package/src/messages/base/StateBuilder.js +0 -25
- package/src/messages/completeStateMessages/CompleteStateMessageBuilder.js +0 -421
- package/src/messages/completeStateMessages/CompleteStateMessageDirector.js +0 -252
- package/src/messages/completeStateMessages/CompleteStateMessageOperations.js +0 -299
- package/src/messages/partialStateMessages/PartialStateMessageBuilder.js +0 -272
- package/src/messages/partialStateMessages/PartialStateMessageDirector.js +0 -137
- package/src/messages/partialStateMessages/PartialStateMessageOperations.js +0 -131
- package/src/utils/crypto.js +0 -11
- package/tests/integration/apply/addAdmin/addAdminBasic.test.js +0 -68
- package/tests/integration/apply/addAdmin/addAdminRecovery.test.js +0 -125
- package/tests/integration/apply/addIndexer.test.js +0 -237
- package/tests/integration/apply/addWhitelist.test.js +0 -53
- package/tests/integration/apply/addWriter.test.js +0 -244
- package/tests/integration/apply/apply.test.js +0 -19
- package/tests/integration/apply/banValidator.test.js +0 -109
- package/tests/integration/apply/postTx/invalidSubValues.test.js +0 -103
- package/tests/integration/apply/postTx/postTx.test.js +0 -222
- package/tests/integration/apply/removeIndexer.test.js +0 -128
- package/tests/integration/apply/removeWriter.test.js +0 -167
- package/tests/integration/apply/transfer.test.js +0 -81
- 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 -16
- package/tests/unit/messageOperations/assembleAdminMessage.test.js +0 -69
- package/tests/unit/messageOperations/assembleBanWriterMessage.test.js +0 -16
- package/tests/unit/messageOperations/assemblePostTransaction.test.js +0 -442
- 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 -58
- package/tests/unit/messageOperations/commonsStateMessageOperationsTest.js +0 -277
- package/tests/unit/messageOperations/stateMessageOperations.test.js +0 -19
- package/tests/unit/state/utils/indexerEntry.test.js +0 -83
- package/tests/unit/state/utils/transaction.test.js +0 -97
- package/tests/unit/utils/crypto/createHash.test.js +0 -15
- /package/rpc/{constants.mjs → constants.js} +0 -0
- /package/rpc/{cors.mjs → cors.js} +0 -0
- /package/rpc/utils/{confirmedParameter.mjs → confirmedParameter.js} +0 -0
- /package/rpc/utils/{url.mjs → url.js} +0 -0
- /package/src/utils/{operations.js → applyOperations.js} +0 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import PeerWallet from 'trac-wallet';
|
|
2
|
+
import b4a from 'b4a';
|
|
3
|
+
import {createMessage, safeWriteUInt32BE, idToBuffer, timestampToBuffer} from "../../../utils/buffer.js";
|
|
4
|
+
import {NetworkOperationType, ResultCode} from '../../../utils/constants.js';
|
|
5
|
+
import {addressToBuffer, isAddressValid} from "../../../core/state/utils/address.js";
|
|
6
|
+
import {encodeCapabilities} from "../../../utils/buffer.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Builder for v1 internal network protocol messages.
|
|
10
|
+
* @param {PeerWallet} wallet
|
|
11
|
+
* @param {object} config
|
|
12
|
+
*/
|
|
13
|
+
class NetworkMessageBuilder {
|
|
14
|
+
#wallet;
|
|
15
|
+
#type;
|
|
16
|
+
#capabilities;
|
|
17
|
+
#id;
|
|
18
|
+
#timestamp;
|
|
19
|
+
#issuerAddress;
|
|
20
|
+
#resultCode;
|
|
21
|
+
#data;
|
|
22
|
+
#header;
|
|
23
|
+
#payloadKey;
|
|
24
|
+
#body;
|
|
25
|
+
#config;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {PeerWallet} wallet
|
|
29
|
+
* @param {object} config
|
|
30
|
+
*/
|
|
31
|
+
constructor(wallet, config) {
|
|
32
|
+
this.#config = config;
|
|
33
|
+
if (!wallet || typeof wallet !== 'object') {
|
|
34
|
+
throw new Error('Wallet must be a valid wallet object');
|
|
35
|
+
}
|
|
36
|
+
if (!isAddressValid(wallet.address, this.#config.addressPrefix)) {
|
|
37
|
+
throw new Error('Wallet should have a valid TRAC address.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.#wallet = wallet;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
setId(id) {
|
|
44
|
+
this.#id = id;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
setTimestamp() {
|
|
49
|
+
this.#timestamp = Date.now();
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
setIssuerAddress(issuerAddress) {
|
|
54
|
+
if (!isAddressValid(issuerAddress, this.#config.addressPrefix)) {
|
|
55
|
+
throw new Error('Issuer TRAC address must be valid.');
|
|
56
|
+
}
|
|
57
|
+
this.#issuerAddress = issuerAddress;
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
setCapabilities(capabilities = []) {
|
|
62
|
+
if (!Array.isArray(capabilities) || !capabilities.every(capability => typeof capability === 'string')) {
|
|
63
|
+
throw new Error('Capabilities must be a string array.');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
this.#capabilities = capabilities
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
setType(type) {
|
|
71
|
+
if (!Object.values(NetworkOperationType).includes(type)) {
|
|
72
|
+
throw new Error(`Invalid operation type: ${type}`);
|
|
73
|
+
}
|
|
74
|
+
this.#type = type;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
setResultCode(code) {
|
|
79
|
+
if (!Object.values(ResultCode).includes(code)) {
|
|
80
|
+
throw new Error(`Invalid network result code: ${code}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this.#resultCode = code;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
setData(data) {
|
|
88
|
+
if (!b4a.isBuffer(data)) {
|
|
89
|
+
throw new Error(`Data must be a buffer.`);
|
|
90
|
+
}
|
|
91
|
+
this.#data = data;
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#setHeader() {
|
|
96
|
+
if (!this.#type) throw new Error('Header requires type to be set');
|
|
97
|
+
if (!this.#id) throw new Error('Header requires id to be set');
|
|
98
|
+
if (!this.#timestamp) throw new Error('Header requires a timestamp provider');
|
|
99
|
+
if (!Array.isArray(this.#capabilities)) throw new Error('Header requires capabilities array');
|
|
100
|
+
|
|
101
|
+
this.#header = {
|
|
102
|
+
type: this.#type,
|
|
103
|
+
id: this.#id,
|
|
104
|
+
timestamp: this.#timestamp,
|
|
105
|
+
capabilities: this.#capabilities,
|
|
106
|
+
};
|
|
107
|
+
return this;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async #buildValidatorConnectionRequestPayload() {
|
|
111
|
+
const issuer = this.#issuerAddress
|
|
112
|
+
if (!isAddressValid(issuer, this.#config.addressPrefix)) {
|
|
113
|
+
throw new Error('Issuer address must be a valid TRAC address');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (this.#issuerAddress !== this.#wallet.address) {
|
|
117
|
+
throw new Error('Issuer address must be the signer address');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const nonce = PeerWallet.generateNonce();
|
|
121
|
+
const tsBuf = timestampToBuffer(this.#timestamp);
|
|
122
|
+
const idBuf = idToBuffer(this.#id);
|
|
123
|
+
const message = createMessage(
|
|
124
|
+
this.#type,
|
|
125
|
+
idBuf,
|
|
126
|
+
tsBuf,
|
|
127
|
+
addressToBuffer(issuer, this.#config.addressPrefix),
|
|
128
|
+
nonce,
|
|
129
|
+
encodeCapabilities(this.#capabilities),
|
|
130
|
+
);
|
|
131
|
+
const hash = await PeerWallet.blake3(message);
|
|
132
|
+
const signature = this.#wallet.sign(hash);
|
|
133
|
+
|
|
134
|
+
this.#payloadKey = 'validator_connection_request';
|
|
135
|
+
this.#body = {
|
|
136
|
+
issuer_address: issuer,
|
|
137
|
+
nonce,
|
|
138
|
+
signature
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async #buildValidatorConnectionResponsePayload() {
|
|
143
|
+
const issuer = this.#issuerAddress
|
|
144
|
+
if (!isAddressValid(issuer, this.#config.addressPrefix)) {
|
|
145
|
+
throw new Error('Issuer address must be a valid TRAC address');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (this.#issuerAddress === this.#wallet.address) {
|
|
149
|
+
throw new Error('Issuer address must be the different than the signer address');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (this.#resultCode === null || this.#resultCode === undefined) {
|
|
153
|
+
throw new Error('Result code must be set before building validator connection response');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const nonce = PeerWallet.generateNonce();
|
|
157
|
+
const tsBuf = timestampToBuffer(this.#timestamp);
|
|
158
|
+
const idBuf = idToBuffer(this.#id);
|
|
159
|
+
const message = createMessage(
|
|
160
|
+
this.#type,
|
|
161
|
+
idBuf,
|
|
162
|
+
tsBuf,
|
|
163
|
+
addressToBuffer(issuer, this.#config.addressPrefix),
|
|
164
|
+
nonce,
|
|
165
|
+
safeWriteUInt32BE(this.#resultCode, 0),
|
|
166
|
+
encodeCapabilities(this.#capabilities),
|
|
167
|
+
);
|
|
168
|
+
const hash = await PeerWallet.blake3(message);
|
|
169
|
+
const signature = this.#wallet.sign(hash);
|
|
170
|
+
|
|
171
|
+
this.#payloadKey = 'validator_connection_response';
|
|
172
|
+
this.#body = {
|
|
173
|
+
issuer_address: issuer,
|
|
174
|
+
nonce,
|
|
175
|
+
signature,
|
|
176
|
+
result: this.#resultCode
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async #buildLivenessRequestPayload() {
|
|
181
|
+
const nonce = PeerWallet.generateNonce();
|
|
182
|
+
const tsBuf = timestampToBuffer(this.#timestamp);
|
|
183
|
+
const idBuf = idToBuffer(this.#id);
|
|
184
|
+
const message = createMessage(
|
|
185
|
+
this.#type,
|
|
186
|
+
idBuf,
|
|
187
|
+
tsBuf,
|
|
188
|
+
nonce,
|
|
189
|
+
encodeCapabilities(this.#capabilities),
|
|
190
|
+
);
|
|
191
|
+
const hash = await PeerWallet.blake3(message);
|
|
192
|
+
const signature = this.#wallet.sign(hash);
|
|
193
|
+
|
|
194
|
+
this.#payloadKey = 'liveness_request';
|
|
195
|
+
this.#body = {
|
|
196
|
+
nonce,
|
|
197
|
+
signature
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async #buildLivenessResponsePayload() {
|
|
202
|
+
if (this.#resultCode === null || this.#resultCode === undefined) {
|
|
203
|
+
throw new Error('Result code must be set before building liveness response');
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const nonce = PeerWallet.generateNonce();
|
|
207
|
+
const tsBuf = timestampToBuffer(this.#timestamp);
|
|
208
|
+
const idBuf = idToBuffer(this.#id);
|
|
209
|
+
const message = createMessage(
|
|
210
|
+
this.#type,
|
|
211
|
+
idBuf,
|
|
212
|
+
tsBuf,
|
|
213
|
+
nonce,
|
|
214
|
+
safeWriteUInt32BE(this.#resultCode, 0),
|
|
215
|
+
encodeCapabilities(this.#capabilities),
|
|
216
|
+
);
|
|
217
|
+
const hash = await PeerWallet.blake3(message);
|
|
218
|
+
const signature = this.#wallet.sign(hash);
|
|
219
|
+
|
|
220
|
+
this.#payloadKey = 'liveness_response';
|
|
221
|
+
this.#body = {
|
|
222
|
+
nonce,
|
|
223
|
+
signature,
|
|
224
|
+
result: this.#resultCode
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async #buildBroadcastRequestPayload() {
|
|
229
|
+
if (!b4a.isBuffer(this.#data)) {
|
|
230
|
+
throw new Error('Data must be set before building broadcast transaction request');
|
|
231
|
+
}
|
|
232
|
+
const nonce = PeerWallet.generateNonce();
|
|
233
|
+
const tsBuf = timestampToBuffer(this.#timestamp);
|
|
234
|
+
const idBuf = idToBuffer(this.#id);
|
|
235
|
+
const message = createMessage(
|
|
236
|
+
this.#type,
|
|
237
|
+
idBuf,
|
|
238
|
+
tsBuf,
|
|
239
|
+
this.#data,
|
|
240
|
+
nonce,
|
|
241
|
+
encodeCapabilities(this.#capabilities),
|
|
242
|
+
);
|
|
243
|
+
const hash = await PeerWallet.blake3(message);
|
|
244
|
+
const signature = this.#wallet.sign(hash);
|
|
245
|
+
|
|
246
|
+
this.#payloadKey = 'broadcast_transaction_request';
|
|
247
|
+
this.#body = {
|
|
248
|
+
data: this.#data,
|
|
249
|
+
nonce,
|
|
250
|
+
signature
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async #buildBroadcastTransactionResponse() {
|
|
255
|
+
if (this.#resultCode === null || this.#resultCode === undefined) {
|
|
256
|
+
throw new Error('Result code must be set before building broadcast transaction response');
|
|
257
|
+
}
|
|
258
|
+
const nonce = PeerWallet.generateNonce();
|
|
259
|
+
const tsBuf = timestampToBuffer(this.#timestamp);
|
|
260
|
+
const idBuf = idToBuffer(this.#id);
|
|
261
|
+
const message = createMessage(
|
|
262
|
+
this.#type,
|
|
263
|
+
idBuf,
|
|
264
|
+
tsBuf,
|
|
265
|
+
nonce,
|
|
266
|
+
safeWriteUInt32BE(this.#resultCode, 0),
|
|
267
|
+
encodeCapabilities(this.#capabilities),
|
|
268
|
+
);
|
|
269
|
+
const hash = await PeerWallet.blake3(message);
|
|
270
|
+
const signature = this.#wallet.sign(hash);
|
|
271
|
+
|
|
272
|
+
this.#payloadKey = 'broadcast_transaction_response';
|
|
273
|
+
this.#body = {
|
|
274
|
+
nonce,
|
|
275
|
+
signature,
|
|
276
|
+
result: this.#resultCode
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
async buildPayload() {
|
|
281
|
+
this.#setHeader();
|
|
282
|
+
|
|
283
|
+
switch (this.#type) {
|
|
284
|
+
case NetworkOperationType.VALIDATOR_CONNECTION_REQUEST: {
|
|
285
|
+
await this.#buildValidatorConnectionRequestPayload();
|
|
286
|
+
break;
|
|
287
|
+
}
|
|
288
|
+
case NetworkOperationType.VALIDATOR_CONNECTION_RESPONSE: {
|
|
289
|
+
await this.#buildValidatorConnectionResponsePayload();
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
case NetworkOperationType.LIVENESS_REQUEST: {
|
|
293
|
+
await this.#buildLivenessRequestPayload();
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case NetworkOperationType.LIVENESS_RESPONSE: {
|
|
297
|
+
await this.#buildLivenessResponsePayload();
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
case NetworkOperationType.BROADCAST_TRANSACTION_REQUEST: {
|
|
301
|
+
await this.#buildBroadcastRequestPayload();
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
case NetworkOperationType.BROADCAST_TRANSACTION_RESPONSE: {
|
|
305
|
+
await this.#buildBroadcastTransactionResponse();
|
|
306
|
+
break;
|
|
307
|
+
}
|
|
308
|
+
default:
|
|
309
|
+
throw new Error(`Unsupported network type ${this.#type}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
getResult() {
|
|
314
|
+
if (!this.#header || !this.#payloadKey || !this.#body) {
|
|
315
|
+
throw new Error('Header or payload not set before getResult');
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return {
|
|
319
|
+
...this.#header,
|
|
320
|
+
[this.#payloadKey]: this.#body
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export default NetworkMessageBuilder;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { NetworkOperationType } from '../../../utils/constants.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Director for v1 internal network protocol messages.
|
|
5
|
+
*/
|
|
6
|
+
class NetworkMessageDirector {
|
|
7
|
+
#builder;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @param {NetworkMessageBuilder} builderInstance
|
|
11
|
+
*/
|
|
12
|
+
constructor(builderInstance) {
|
|
13
|
+
this.#builder = builderInstance;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Build a validator connection request message.
|
|
18
|
+
* @param {string} id
|
|
19
|
+
* @param {string} issuerAddress
|
|
20
|
+
* @param {string[]} capabilities
|
|
21
|
+
* @returns {Promise<object>}
|
|
22
|
+
*/
|
|
23
|
+
async buildValidatorConnectionRequest(id, issuerAddress, capabilities) {
|
|
24
|
+
await this.#builder
|
|
25
|
+
.setType(NetworkOperationType.VALIDATOR_CONNECTION_REQUEST)
|
|
26
|
+
.setId(id)
|
|
27
|
+
.setTimestamp()
|
|
28
|
+
.setIssuerAddress(issuerAddress)
|
|
29
|
+
.setCapabilities(capabilities)
|
|
30
|
+
.buildPayload()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
return this.#builder.getResult();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Build a validator connection response message.
|
|
38
|
+
* @param {string} id
|
|
39
|
+
* @param {string} issuerAddress
|
|
40
|
+
* @param {string[]} capabilities
|
|
41
|
+
* @param {number} statusCode
|
|
42
|
+
* @returns {Promise<object>}
|
|
43
|
+
*/
|
|
44
|
+
async buildValidatorConnectionResponse(id, issuerAddress, capabilities, statusCode) {
|
|
45
|
+
await this.#builder
|
|
46
|
+
.setType(NetworkOperationType.VALIDATOR_CONNECTION_RESPONSE)
|
|
47
|
+
.setId(id)
|
|
48
|
+
.setTimestamp()
|
|
49
|
+
.setIssuerAddress(issuerAddress)
|
|
50
|
+
.setCapabilities(capabilities)
|
|
51
|
+
.setResultCode(statusCode)
|
|
52
|
+
.buildPayload()
|
|
53
|
+
|
|
54
|
+
return this.#builder.getResult();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Build a liveness request message.
|
|
59
|
+
* @param {string} id
|
|
60
|
+
* @param {Buffer} data
|
|
61
|
+
* @param {string[]} capabilities
|
|
62
|
+
* @returns {Promise<object>}
|
|
63
|
+
*/
|
|
64
|
+
async buildLivenessRequest(id, data, capabilities) {
|
|
65
|
+
await this.#builder
|
|
66
|
+
.setType(NetworkOperationType.LIVENESS_REQUEST)
|
|
67
|
+
.setId(id)
|
|
68
|
+
.setTimestamp()
|
|
69
|
+
.setData(data)
|
|
70
|
+
.setCapabilities(capabilities)
|
|
71
|
+
.buildPayload();
|
|
72
|
+
|
|
73
|
+
return this.#builder.getResult();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Build a liveness response message.
|
|
78
|
+
* @param {string} id
|
|
79
|
+
* @param {Buffer} data
|
|
80
|
+
* @param {string[]} capabilities
|
|
81
|
+
* @param {number} statusCode
|
|
82
|
+
* @returns {Promise<object>}
|
|
83
|
+
*/
|
|
84
|
+
async buildLivenessResponse(id, data, capabilities, statusCode) {
|
|
85
|
+
await this.#builder
|
|
86
|
+
.setType(NetworkOperationType.LIVENESS_RESPONSE)
|
|
87
|
+
.setId(id)
|
|
88
|
+
.setTimestamp()
|
|
89
|
+
.setData(data)
|
|
90
|
+
.setCapabilities(capabilities)
|
|
91
|
+
.setResultCode(statusCode)
|
|
92
|
+
.buildPayload();
|
|
93
|
+
|
|
94
|
+
return this.#builder.getResult();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Build a broadcast transaction request message.
|
|
99
|
+
* @param {string} id
|
|
100
|
+
* @param {Buffer} data
|
|
101
|
+
* @param {string[]} capabilities
|
|
102
|
+
* @returns {Promise<object>}
|
|
103
|
+
*/
|
|
104
|
+
async buildBroadcastTransactionRequest(id, data, capabilities) {
|
|
105
|
+
await this.#builder
|
|
106
|
+
.setType(NetworkOperationType.BROADCAST_TRANSACTION_REQUEST)
|
|
107
|
+
.setId(id)
|
|
108
|
+
.setTimestamp()
|
|
109
|
+
.setData(data)
|
|
110
|
+
.setCapabilities(capabilities)
|
|
111
|
+
.buildPayload();
|
|
112
|
+
|
|
113
|
+
return this.#builder.getResult();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Build a broadcast transaction response message.
|
|
118
|
+
* @param {string} id
|
|
119
|
+
* @param {string[]} capabilities
|
|
120
|
+
* @param {number} statusCode
|
|
121
|
+
* @returns {Promise<object>}
|
|
122
|
+
*/
|
|
123
|
+
async buildBroadcastTransactionResponse(id, capabilities, statusCode) {
|
|
124
|
+
await this.#builder
|
|
125
|
+
.setType(NetworkOperationType.BROADCAST_TRANSACTION_RESPONSE)
|
|
126
|
+
.setId(id)
|
|
127
|
+
.setTimestamp()
|
|
128
|
+
.setCapabilities(capabilities)
|
|
129
|
+
.setResultCode(statusCode)
|
|
130
|
+
.buildPayload();
|
|
131
|
+
|
|
132
|
+
return this.#builder.getResult();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export default NetworkMessageDirector;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import NetworkMessageDirector from "./NetworkMessageDirector.js";
|
|
2
|
+
import NetworkMessageBuilder from "./NetworkMessageBuilder.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Factory helper to create a director with a fresh builder instance.
|
|
6
|
+
* @param {PeerWallet} wallet
|
|
7
|
+
* @param {object} config
|
|
8
|
+
* @returns {NetworkMessageDirector}
|
|
9
|
+
*/
|
|
10
|
+
export const networkMessageFactory = (wallet, config) => {
|
|
11
|
+
return new NetworkMessageDirector(new NetworkMessageBuilder(wallet, config))
|
|
12
|
+
}
|