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
|
@@ -7,14 +7,15 @@ import {
|
|
|
7
7
|
assertValidatorReward,
|
|
8
8
|
promotePeerToWriter
|
|
9
9
|
} from '../addWriter/addWriterScenarioHelpers.js';
|
|
10
|
-
import
|
|
11
|
-
import
|
|
10
|
+
import { applyStateMessageFactory } from '../../../../../src/messages/state/applyStateMessageFactory.js';
|
|
11
|
+
import { safeEncodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
|
|
12
12
|
import nodeEntryUtils from '../../../../../src/core/state/utils/nodeEntry.js';
|
|
13
13
|
import addressUtils from '../../../../../src/core/state/utils/address.js';
|
|
14
14
|
import { toBalance, BALANCE_FEE, BALANCE_TO_STAKE } from '../../../../../src/core/state/utils/balance.js';
|
|
15
15
|
import { eventFlush } from '../../../../helpers/autobaseTestHelpers.js';
|
|
16
16
|
import { safeDecodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
|
|
17
17
|
import { EntryType } from '../../../../../src/utils/constants.js';
|
|
18
|
+
import { config } from '../../../../helpers/config.js';
|
|
18
19
|
|
|
19
20
|
export async function setupRemoveWriterScenario(t, options = {}) {
|
|
20
21
|
const context = await setupAddWriterScenario(t, options);
|
|
@@ -142,7 +143,7 @@ async function assertWriterStillPromoted(t, base, writerPeer, expected) {
|
|
|
142
143
|
'writer stake remains untouched'
|
|
143
144
|
);
|
|
144
145
|
|
|
145
|
-
const writerAddressBuffer = addressUtils.addressToBuffer(writerAddress);
|
|
146
|
+
const writerAddressBuffer = addressUtils.addressToBuffer(writerAddress, config.addressPrefix);
|
|
146
147
|
const writingKeyHex = writerPeer.base.local.key.toString('hex');
|
|
147
148
|
const registryKey = EntryType.WRITER_ADDRESS + writingKeyHex;
|
|
148
149
|
const registryEntry = await base.view.get(registryKey);
|
|
@@ -161,7 +162,7 @@ async function assertPayloadProcessedByValidator(t, payload, expectedValidatorAd
|
|
|
161
162
|
const validatorAddressBuffer = decodedOperation?.rao?.va;
|
|
162
163
|
t.ok(validatorAddressBuffer, 'removeWriter payload carries validator address');
|
|
163
164
|
if (!validatorAddressBuffer) return;
|
|
164
|
-
const validatorAddress = addressUtils.bufferToAddress(validatorAddressBuffer);
|
|
165
|
+
const validatorAddress = addressUtils.bufferToAddress(validatorAddressBuffer, config.addressPrefix);
|
|
165
166
|
t.ok(validatorAddress, 'removeWriter payload validator address decodes');
|
|
166
167
|
if (!validatorAddress) return;
|
|
167
168
|
t.is(
|
|
@@ -179,20 +180,23 @@ export async function buildRemoveWriterPayloadWithTxValidity(context, mutatedTxV
|
|
|
179
180
|
}
|
|
180
181
|
const { readerPeer = selectWriterPeer(context), validatorPeer = context.adminBootstrap, writerKeyBuffer = null } = options;
|
|
181
182
|
const writerKey = writerKeyBuffer ?? readerPeer.base.local.key;
|
|
182
|
-
const partial = await
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
183
|
+
const partial = await applyStateMessageFactory(readerPeer.wallet, config)
|
|
184
|
+
.buildPartialRemoveWriterMessage(
|
|
185
|
+
readerPeer.wallet.address,
|
|
186
|
+
writerKey.toString('hex'),
|
|
187
|
+
mutatedTxValidity.toString('hex'),
|
|
188
|
+
'json'
|
|
189
|
+
);
|
|
190
|
+
const payload = await applyStateMessageFactory(validatorPeer.wallet, config)
|
|
191
|
+
.buildCompleteRemoveWriterMessage(
|
|
192
|
+
partial.address,
|
|
193
|
+
b4a.from(partial.rao.tx, 'hex'),
|
|
194
|
+
mutatedTxValidity,
|
|
195
|
+
b4a.from(partial.rao.iw, 'hex'),
|
|
196
|
+
b4a.from(partial.rao.in, 'hex'),
|
|
197
|
+
b4a.from(partial.rao.is, 'hex')
|
|
198
|
+
);
|
|
199
|
+
return safeEncodeApplyOperation(payload);
|
|
196
200
|
}
|
|
197
201
|
|
|
198
202
|
export async function snapshotDowngradedWriterEntry(context) {
|
|
@@ -256,7 +260,7 @@ export async function applyWithWriterRegistryForeignAddress(context, invalidPayl
|
|
|
256
260
|
throw new Error('Foreign registry override requires a peer with an address.');
|
|
257
261
|
}
|
|
258
262
|
const foreignEntry = {
|
|
259
|
-
value: addressUtils.addressToBuffer(foreignAddress)
|
|
263
|
+
value: addressUtils.addressToBuffer(foreignAddress, config.addressPrefix)
|
|
260
264
|
};
|
|
261
265
|
await withWriterRegistryOverrideOnApply({
|
|
262
266
|
context,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
2
|
import { test } from 'brittle';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import { applyStateMessageFactory } from '../../../../../src/messages/state/applyStateMessageFactory.js';
|
|
4
|
+
import { safeEncodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
|
|
5
5
|
import { deriveIndexerSequenceState, eventFlush } from '../../../../helpers/autobaseTestHelpers.js';
|
|
6
6
|
import {
|
|
7
7
|
setupTransferScenario,
|
|
@@ -13,6 +13,7 @@ import { promotePeerToWriter } from '../addWriter/addWriterScenarioHelpers.js';
|
|
|
13
13
|
import transactionUtils from '../../../../../src/core/state/utils/transaction.js';
|
|
14
14
|
import { toBalance } from '../../../../../src/core/state/utils/balance.js';
|
|
15
15
|
import nodeEntryUtils from '../../../../../src/core/state/utils/nodeEntry.js';
|
|
16
|
+
import { config } from '../../../../helpers/config.js';
|
|
16
17
|
|
|
17
18
|
export default function transferDoubleSpendAcrossValidatorsScenario() {
|
|
18
19
|
test('State.apply transfer prevents double spend across validators (distinct tx hashes)', async t => {
|
|
@@ -49,40 +50,48 @@ export default function transferDoubleSpendAcrossValidatorsScenario() {
|
|
|
49
50
|
const txValidityA = await deriveIndexerSequenceState(primaryValidator.base);
|
|
50
51
|
const txValidityB = await deriveIndexerSequenceState(secondaryValidator.base);
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
53
|
+
const partialA = await applyStateMessageFactory(senderPeer.wallet, config)
|
|
54
|
+
.buildPartialTransferOperationMessage(
|
|
55
|
+
senderPeer.wallet.address,
|
|
56
|
+
recipientA.wallet.address,
|
|
57
|
+
b4a.toString(DEFAULT_TRANSFER_AMOUNT, 'hex'),
|
|
58
|
+
b4a.toString(txValidityA, 'hex'),
|
|
59
|
+
'json'
|
|
60
|
+
);
|
|
61
|
+
const partialB = await applyStateMessageFactory(senderPeer.wallet, config)
|
|
62
|
+
.buildPartialTransferOperationMessage(
|
|
63
|
+
senderPeer.wallet.address,
|
|
64
|
+
recipientB.wallet.address,
|
|
65
|
+
b4a.toString(DEFAULT_TRANSFER_AMOUNT, 'hex'),
|
|
66
|
+
b4a.toString(txValidityB, 'hex'),
|
|
67
|
+
'json'
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const payloadA = safeEncodeApplyOperation(
|
|
71
|
+
await applyStateMessageFactory(primaryValidator.wallet, config)
|
|
72
|
+
.buildCompleteTransferOperationMessage(
|
|
73
|
+
partialA.address,
|
|
74
|
+
b4a.from(partialA.tro.tx, 'hex'),
|
|
75
|
+
b4a.from(partialA.tro.txv, 'hex'),
|
|
76
|
+
b4a.from(partialA.tro.in, 'hex'),
|
|
77
|
+
partialA.tro.to,
|
|
78
|
+
b4a.from(partialA.tro.am, 'hex'),
|
|
79
|
+
b4a.from(partialA.tro.is, 'hex')
|
|
80
|
+
)
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const payloadB = safeEncodeApplyOperation(
|
|
84
|
+
await applyStateMessageFactory(secondaryValidator.wallet, config)
|
|
85
|
+
.buildCompleteTransferOperationMessage(
|
|
86
|
+
partialB.address,
|
|
87
|
+
b4a.from(partialB.tro.tx, 'hex'),
|
|
88
|
+
b4a.from(partialB.tro.txv, 'hex'),
|
|
89
|
+
b4a.from(partialB.tro.in, 'hex'),
|
|
90
|
+
partialB.tro.to,
|
|
91
|
+
b4a.from(partialB.tro.am, 'hex'),
|
|
92
|
+
b4a.from(partialB.tro.is, 'hex')
|
|
93
|
+
)
|
|
94
|
+
);
|
|
86
95
|
|
|
87
96
|
// Apply first transfer successfully via primary validator.
|
|
88
97
|
await primaryValidator.base.append(payloadA);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import PeerWallet from 'trac-wallet';
|
|
3
|
+
import { applyStateMessageFactory } from '../../../../../src/messages/state/applyStateMessageFactory.js';
|
|
4
4
|
import { deriveIndexerSequenceState, eventFlush } from '../../../../helpers/autobaseTestHelpers.js';
|
|
5
5
|
import {
|
|
6
6
|
setupAdminNetwork,
|
|
@@ -15,10 +15,10 @@ import { toBalance, PERCENT_75, BALANCE_ZERO } from '../../../../../src/core/sta
|
|
|
15
15
|
import { decimalStringToBigInt, bigIntTo16ByteBuffer } from '../../../../../src/utils/amountSerialization.js';
|
|
16
16
|
import { safeDecodeApplyOperation, safeEncodeApplyOperation } from '../../../../../src/utils/protobuf/operationHelpers.js';
|
|
17
17
|
import { ZERO_WK } from '../../../../../src/utils/buffer.js';
|
|
18
|
-
import { EntryType, OperationType
|
|
18
|
+
import { EntryType, OperationType } from '../../../../../src/utils/constants.js';
|
|
19
19
|
import { createMessage } from '../../../../../src/utils/buffer.js';
|
|
20
|
-
import { blake3Hash } from '../../../../../src/utils/crypto.js';
|
|
21
20
|
import OperationValidationScenarioBase from '../common/base/OperationValidationScenarioBase.js';
|
|
21
|
+
import { config } from '../../../../helpers/config.js';
|
|
22
22
|
|
|
23
23
|
export const DEFAULT_INITIAL_BALANCE = bigIntTo16ByteBuffer(decimalStringToBigInt('10'));
|
|
24
24
|
export const DEFAULT_TRANSFER_AMOUNT = bigIntTo16ByteBuffer(decimalStringToBigInt('2'));
|
|
@@ -122,23 +122,26 @@ export async function buildTransferPayload(
|
|
|
122
122
|
const resolvedTxValidity =
|
|
123
123
|
txValidity ?? (await deriveIndexerSequenceState(validatorPeer.base));
|
|
124
124
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
125
|
+
const partial = await applyStateMessageFactory(senderPeer.wallet, config)
|
|
126
|
+
.buildPartialTransferOperationMessage(
|
|
127
|
+
senderPeer.wallet.address,
|
|
128
|
+
recipientAddress,
|
|
129
|
+
b4a.toString(amount, 'hex'),
|
|
130
|
+
b4a.toString(resolvedTxValidity, 'hex'),
|
|
131
|
+
'json'
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
const payload = await applyStateMessageFactory(validatorPeer.wallet, config)
|
|
135
|
+
.buildCompleteTransferOperationMessage(
|
|
136
|
+
partial.address,
|
|
137
|
+
b4a.from(partial.tro.tx, 'hex'),
|
|
138
|
+
b4a.from(partial.tro.txv, 'hex'),
|
|
139
|
+
b4a.from(partial.tro.in, 'hex'),
|
|
140
|
+
partial.tro.to,
|
|
141
|
+
b4a.from(partial.tro.am, 'hex'),
|
|
142
|
+
b4a.from(partial.tro.is, 'hex')
|
|
143
|
+
);
|
|
144
|
+
return safeEncodeApplyOperation(payload);
|
|
142
145
|
}
|
|
143
146
|
|
|
144
147
|
export async function buildTransferPayloadWithTxValidity(
|
|
@@ -204,9 +207,9 @@ export async function assertTransferSuccessState(
|
|
|
204
207
|
t.ok(decodedPayload?.tro, 'transfer payload decodes');
|
|
205
208
|
if (!decodedPayload?.tro) return;
|
|
206
209
|
|
|
207
|
-
const senderAddress = addressUtils.bufferToAddress(decodedPayload.address);
|
|
208
|
-
const recipientAddress = addressUtils.bufferToAddress(decodedPayload.tro.to);
|
|
209
|
-
const validatorAddress = addressUtils.bufferToAddress(decodedPayload.tro.va);
|
|
210
|
+
const senderAddress = addressUtils.bufferToAddress(decodedPayload.address, config.addressPrefix);
|
|
211
|
+
const recipientAddress = addressUtils.bufferToAddress(decodedPayload.tro.to, config.addressPrefix);
|
|
212
|
+
const validatorAddress = addressUtils.bufferToAddress(decodedPayload.tro.va, config.addressPrefix);
|
|
210
213
|
|
|
211
214
|
const amount = toBalance(decodedPayload.tro.am);
|
|
212
215
|
const fee = toBalance(transactionUtils.FEE);
|
|
@@ -611,7 +614,7 @@ async function applyTransferSenderEntryOverride(context, invalidPayload, mutateE
|
|
|
611
614
|
}
|
|
612
615
|
|
|
613
616
|
const senderAddress = senderPeer.wallet.address;
|
|
614
|
-
const senderBuffer = addressUtils.addressToBuffer(senderAddress);
|
|
617
|
+
const senderBuffer = addressUtils.addressToBuffer(senderAddress, config.addressPrefix);
|
|
615
618
|
const base = node.base;
|
|
616
619
|
const originalApply = base._handlers.apply;
|
|
617
620
|
|
|
@@ -687,8 +690,8 @@ export async function mutateTransferAmountWithRehashedTx(t, validPayload) {
|
|
|
687
690
|
mutatedAmount[mutatedAmount.length - 1] ^= 0x01;
|
|
688
691
|
parent.am = mutatedAmount;
|
|
689
692
|
|
|
690
|
-
const message = createMessage(
|
|
691
|
-
const regeneratedTxHash = await
|
|
693
|
+
const message = createMessage(config.networkId, parent.txv, parent.to, parent.am, parent.in, OperationType.TRANSFER);
|
|
694
|
+
const regeneratedTxHash = await PeerWallet.blake3(message);
|
|
692
695
|
if (regeneratedTxHash?.length === parent.tx?.length) {
|
|
693
696
|
parent.tx = regeneratedTxHash;
|
|
694
697
|
}
|
|
@@ -735,14 +738,14 @@ export async function mutateTransferAmountInvalidWithRehash(t, validPayload, con
|
|
|
735
738
|
parent.am = b4a.alloc(1); // invalid length to break toBalance
|
|
736
739
|
|
|
737
740
|
const requesterMessage = createMessage(
|
|
738
|
-
|
|
741
|
+
config.networkId,
|
|
739
742
|
parent.txv,
|
|
740
743
|
parent.to,
|
|
741
744
|
parent.am,
|
|
742
745
|
parent.in,
|
|
743
746
|
OperationType.TRANSFER
|
|
744
747
|
);
|
|
745
|
-
const regeneratedTxHash = await
|
|
748
|
+
const regeneratedTxHash = await PeerWallet.blake3(requesterMessage);
|
|
746
749
|
if (regeneratedTxHash?.length === parent.tx?.length) {
|
|
747
750
|
parent.tx = regeneratedTxHash;
|
|
748
751
|
}
|
|
@@ -751,8 +754,8 @@ export async function mutateTransferAmountInvalidWithRehash(t, validPayload, con
|
|
|
751
754
|
parent.is = requesterWallet.sign(regeneratedTxHash);
|
|
752
755
|
}
|
|
753
756
|
if (validatorWallet && parent.vn) {
|
|
754
|
-
const validatorMessage = createMessage(
|
|
755
|
-
parent.vs = validatorWallet.sign(await
|
|
757
|
+
const validatorMessage = createMessage(config.networkId, parent.tx, parent.vn, OperationType.TRANSFER);
|
|
758
|
+
parent.vs = validatorWallet.sign(await PeerWallet.blake3(validatorMessage));
|
|
756
759
|
}
|
|
757
760
|
|
|
758
761
|
return safeEncodeApplyOperation(decoded);
|
|
@@ -772,8 +775,8 @@ export async function mutateTransferRecipientAddressWithRehash(t, validPayload,
|
|
|
772
775
|
}
|
|
773
776
|
parent.to = mutatedTo;
|
|
774
777
|
|
|
775
|
-
const message = createMessage(
|
|
776
|
-
const regeneratedTxHash = await
|
|
778
|
+
const message = createMessage(config.networkId, parent.txv, parent.to, parent.am, parent.in, OperationType.TRANSFER);
|
|
779
|
+
const regeneratedTxHash = await PeerWallet.blake3(message);
|
|
777
780
|
if (regeneratedTxHash?.length === parent.tx?.length) {
|
|
778
781
|
parent.tx = regeneratedTxHash;
|
|
779
782
|
}
|
|
@@ -782,8 +785,8 @@ export async function mutateTransferRecipientAddressWithRehash(t, validPayload,
|
|
|
782
785
|
parent.is = requesterWallet.sign(regeneratedTxHash);
|
|
783
786
|
}
|
|
784
787
|
if (validatorWallet && parent.vn) {
|
|
785
|
-
const validatorMessage = createMessage(
|
|
786
|
-
parent.vs = validatorWallet.sign(await
|
|
788
|
+
const validatorMessage = createMessage(config.networkId, parent.tx, parent.vn, OperationType.TRANSFER);
|
|
789
|
+
parent.vs = validatorWallet.sign(await PeerWallet.blake3(validatorMessage));
|
|
787
790
|
}
|
|
788
791
|
|
|
789
792
|
return safeEncodeApplyOperation(decoded);
|
|
@@ -804,8 +807,8 @@ export async function mutateTransferRecipientPublicKeyInvalidWithRehash(t, valid
|
|
|
804
807
|
}
|
|
805
808
|
parent.to = mutatedTo;
|
|
806
809
|
|
|
807
|
-
const message = createMessage(
|
|
808
|
-
const regeneratedTxHash = await
|
|
810
|
+
const message = createMessage(config.networkId, parent.txv, parent.to, parent.am, parent.in, OperationType.TRANSFER);
|
|
811
|
+
const regeneratedTxHash = await PeerWallet.blake3(message);
|
|
809
812
|
if (regeneratedTxHash?.length === parent.tx?.length) {
|
|
810
813
|
parent.tx = regeneratedTxHash;
|
|
811
814
|
}
|
|
@@ -814,8 +817,8 @@ export async function mutateTransferRecipientPublicKeyInvalidWithRehash(t, valid
|
|
|
814
817
|
parent.is = requesterWallet.sign(regeneratedTxHash);
|
|
815
818
|
}
|
|
816
819
|
if (validatorWallet && parent.vn) {
|
|
817
|
-
const validatorMessage = createMessage(
|
|
818
|
-
parent.vs = validatorWallet.sign(await
|
|
820
|
+
const validatorMessage = createMessage(config.networkId, parent.tx, parent.vn, OperationType.TRANSFER);
|
|
821
|
+
parent.vs = validatorWallet.sign(await PeerWallet.blake3(validatorMessage));
|
|
819
822
|
}
|
|
820
823
|
|
|
821
824
|
return safeEncodeApplyOperation(decoded);
|
|
@@ -841,14 +844,14 @@ export async function mutateTransferAmountToInvalidValue(t, validPayload, contex
|
|
|
841
844
|
parent.am = b4a.alloc(1); // forces toBalance(amount).value === null
|
|
842
845
|
|
|
843
846
|
const requesterMessage = createMessage(
|
|
844
|
-
|
|
847
|
+
config.networkId,
|
|
845
848
|
parent.txv,
|
|
846
849
|
parent.to,
|
|
847
850
|
parent.am,
|
|
848
851
|
parent.in,
|
|
849
852
|
OperationType.TRANSFER
|
|
850
853
|
);
|
|
851
|
-
const regeneratedTxHash = await
|
|
854
|
+
const regeneratedTxHash = await PeerWallet.blake3(requesterMessage);
|
|
852
855
|
if (regeneratedTxHash?.length === parent.tx?.length) {
|
|
853
856
|
parent.tx = regeneratedTxHash;
|
|
854
857
|
}
|
|
@@ -857,8 +860,8 @@ export async function mutateTransferAmountToInvalidValue(t, validPayload, contex
|
|
|
857
860
|
parent.is = requesterWallet.sign(regeneratedTxHash);
|
|
858
861
|
}
|
|
859
862
|
if (validatorWallet && parent.vn) {
|
|
860
|
-
const validatorMessage = createMessage(
|
|
861
|
-
parent.vs = validatorWallet.sign(await
|
|
863
|
+
const validatorMessage = createMessage(config.networkId, parent.tx, parent.vn, OperationType.TRANSFER);
|
|
864
|
+
parent.vs = validatorWallet.sign(await PeerWallet.blake3(validatorMessage));
|
|
862
865
|
}
|
|
863
866
|
|
|
864
867
|
return safeEncodeApplyOperation(decoded);
|
|
@@ -948,7 +951,7 @@ async function applyTransferRecipientEntryOverride(context, invalidPayload, muta
|
|
|
948
951
|
}
|
|
949
952
|
|
|
950
953
|
const recipientAddress = recipientPeer.wallet.address;
|
|
951
|
-
const recipientBuffer = addressUtils.addressToBuffer(recipientAddress);
|
|
954
|
+
const recipientBuffer = addressUtils.addressToBuffer(recipientAddress, config.addressPrefix);
|
|
952
955
|
const base = node.base;
|
|
953
956
|
const originalApply = base._handlers.apply;
|
|
954
957
|
|
|
@@ -992,7 +995,7 @@ async function applyTransferRecipientBalanceDecodeFailure(context, invalidPayloa
|
|
|
992
995
|
}
|
|
993
996
|
|
|
994
997
|
const targetAddress = recipientPeer.wallet.address;
|
|
995
|
-
const targetBuffer = addressUtils.addressToBuffer(targetAddress);
|
|
998
|
+
const targetBuffer = addressUtils.addressToBuffer(targetAddress, config.addressPrefix);
|
|
996
999
|
const originalDecode = nodeEntryUtils.decode;
|
|
997
1000
|
let shouldMutateNextDecode = false;
|
|
998
1001
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
|
-
import
|
|
3
|
-
import CompleteStateMessageOperations from '../../../../../src/messages/completeStateMessages/CompleteStateMessageOperations.js';
|
|
2
|
+
import { applyStateMessageFactory } from '../../../../../src/messages/state/applyStateMessageFactory.js';
|
|
4
3
|
import {
|
|
5
4
|
deriveIndexerSequenceState,
|
|
6
5
|
eventFlush
|
|
@@ -25,6 +24,7 @@ import {
|
|
|
25
24
|
safeDecodeApplyOperation,
|
|
26
25
|
safeEncodeApplyOperation
|
|
27
26
|
} from '../../../../../src/utils/protobuf/operationHelpers.js';
|
|
27
|
+
import { config } from '../../../../helpers/config.js';
|
|
28
28
|
|
|
29
29
|
const DEFAULT_FUNDING = bigIntTo16ByteBuffer(decimalStringToBigInt('10'));
|
|
30
30
|
const DEFAULT_CONTENT_HASH = b4a.alloc(32, 0xab);
|
|
@@ -138,29 +138,32 @@ export async function buildTxOperationPayload(
|
|
|
138
138
|
writerKeyBuffer = broadcasterPeer.base.local.key
|
|
139
139
|
} = {}
|
|
140
140
|
) {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
141
|
+
const resolvedTxValidity = txValidity ?? (await deriveIndexerSequenceState(validatorPeer.base));
|
|
142
|
+
|
|
143
|
+
const partial = await applyStateMessageFactory(broadcasterPeer.wallet, config)
|
|
144
|
+
.buildPartialTransactionOperationMessage(
|
|
145
|
+
broadcasterPeer.wallet.address,
|
|
146
|
+
writerKeyBuffer.toString('hex'),
|
|
147
|
+
resolvedTxValidity.toString('hex'),
|
|
148
|
+
contentHash.toString('hex'),
|
|
149
|
+
externalBootstrap.toString('hex'),
|
|
150
|
+
msbBootstrap.toString('hex'),
|
|
151
|
+
'json'
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
const payload = await applyStateMessageFactory(validatorPeer.wallet, config)
|
|
155
|
+
.buildCompleteTransactionOperationMessage(
|
|
156
|
+
partial.address,
|
|
157
|
+
b4a.from(partial.txo.tx, 'hex'),
|
|
158
|
+
b4a.from(partial.txo.txv, 'hex'),
|
|
159
|
+
b4a.from(partial.txo.iw, 'hex'),
|
|
160
|
+
b4a.from(partial.txo.in, 'hex'),
|
|
161
|
+
b4a.from(partial.txo.ch, 'hex'),
|
|
162
|
+
b4a.from(partial.txo.is, 'hex'),
|
|
163
|
+
b4a.from(partial.txo.bs, 'hex'),
|
|
164
|
+
b4a.from(partial.txo.mbs, 'hex')
|
|
165
|
+
);
|
|
166
|
+
return safeEncodeApplyOperation(payload);
|
|
164
167
|
}
|
|
165
168
|
|
|
166
169
|
export async function buildTxOperationPayloadWithTxValidity(context, txValidity, options = {}) {
|
|
@@ -208,8 +211,8 @@ export async function assertTxOperationSuccessState(
|
|
|
208
211
|
t.ok(b4a.equals(msbBootstrap, context.txOperation?.msbBootstrap), 'payload MSB bootstrap matches network');
|
|
209
212
|
}
|
|
210
213
|
|
|
211
|
-
const requesterAddress = addressUtils.bufferToAddress(requesterAddressBuffer);
|
|
212
|
-
const validatorAddress = addressUtils.bufferToAddress(validatorAddressBuffer);
|
|
214
|
+
const requesterAddress = addressUtils.bufferToAddress(requesterAddressBuffer, config.addressPrefix);
|
|
215
|
+
const validatorAddress = addressUtils.bufferToAddress(validatorAddressBuffer, config.addressPrefix);
|
|
213
216
|
|
|
214
217
|
t.is(requesterAddress, broadcasterPeer.wallet.address, 'requester matches broadcaster');
|
|
215
218
|
t.is(validatorAddress, validatorPeer.wallet.address, 'validator matches selected peer');
|
|
@@ -309,10 +312,10 @@ export async function assertTxOperationSuccessState(
|
|
|
309
312
|
const deploymentKey = `${EntryType.DEPLOYMENT}${externalBootstrap.toString('hex')}`;
|
|
310
313
|
const deploymentEntry = await validatorPeer.base.view.get(deploymentKey);
|
|
311
314
|
t.ok(deploymentEntry, 'deployment entry remains present after tx');
|
|
312
|
-
const decodedDeployment = deploymentEntryUtils.decode(deploymentEntry?.value);
|
|
315
|
+
const decodedDeployment = deploymentEntryUtils.decode(deploymentEntry?.value, config.addressLength);
|
|
313
316
|
t.ok(decodedDeployment, 'deployment entry decodes after tx');
|
|
314
317
|
if (decodedDeployment?.address) {
|
|
315
|
-
const creatorAddress = addressUtils.bufferToAddress(decodedDeployment.address);
|
|
318
|
+
const creatorAddress = addressUtils.bufferToAddress(decodedDeployment.address, config.addressPrefix);
|
|
316
319
|
t.is(
|
|
317
320
|
creatorAddress,
|
|
318
321
|
creatorPeer.wallet.address,
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
assertTxOperationFailureState
|
|
9
9
|
} from './txOperationScenarioHelpers.js';
|
|
10
10
|
import { eventFlush } from '../../../../helpers/autobaseTestHelpers.js';
|
|
11
|
+
import { config } from '../../../../helpers/config.js'
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Builds a transferFeeTxOperation guard scenario that runs through the full apply path.
|
|
@@ -34,7 +35,7 @@ export function createTransferFeeGuardScenario({
|
|
|
34
35
|
applyInvalidPayload: async (context, invalidPayload) => {
|
|
35
36
|
const node = context.txOperation?.validatorPeer ?? context.peers?.[1];
|
|
36
37
|
const decoded = safeDecodeApplyOperation(invalidPayload);
|
|
37
|
-
const requesterAddressString = addressUtils.bufferToAddress(decoded?.address);
|
|
38
|
+
const requesterAddressString = addressUtils.bufferToAddress(decoded?.address, config.addressPrefix);
|
|
38
39
|
|
|
39
40
|
const patchResult = await applyPatch({ context, node, decoded, requesterAddressString });
|
|
40
41
|
const cleanup = typeof patchResult === 'function' ? patchResult : patchResult?.cleanup;
|
|
@@ -8,7 +8,6 @@ async function runStateTests() {
|
|
|
8
8
|
await import('./utils/adminEntry.test.js');
|
|
9
9
|
await import('./utils/balance.test.js');
|
|
10
10
|
await import('./utils/nodeEntry.test.js');
|
|
11
|
-
await import('./utils/indexerEntry.test.js');
|
|
12
11
|
await import('./utils/lengthEntry.test.js');
|
|
13
12
|
await import('./utils/roles.test.js');
|
|
14
13
|
// These tests are skipped temoporarily because the mock library sinon does not work with bare.
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import b4a from 'b4a';
|
|
2
2
|
import { bech32m } from 'bech32';
|
|
3
|
-
import {
|
|
3
|
+
import { address as addressApi } from 'trac-crypto-api';
|
|
4
4
|
import { TOKEN_DECIMALS } from '../../../src/utils/constants.js';
|
|
5
5
|
|
|
6
6
|
export function randomBuffer(size) {
|
|
7
7
|
return b4a.from(Array.from({ length: size }, () => Math.floor(Math.random() * 256)));
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
export function randomAddress(hrp
|
|
11
|
-
|
|
10
|
+
export function randomAddress(hrp) {
|
|
11
|
+
// Compute payload size so final string length equals addressApi.size(hrp)
|
|
12
|
+
const totalLen = addressApi.size(hrp); // expected address string length
|
|
13
|
+
const dataWordLen = totalLen - (hrp.length + 1 + 6); // data chars = total - hrp - sep - checksum
|
|
14
|
+
const dataBytesLen = Math.floor((dataWordLen * 5) / 8); // bytes that yield that many 5-bit words
|
|
15
|
+
const data = randomBuffer(dataBytesLen);
|
|
12
16
|
return bech32m.encode(hrp, bech32m.toWords(data));
|
|
13
17
|
}
|
|
14
18
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { test } from 'brittle';
|
|
2
2
|
import b4a from 'b4a';
|
|
3
|
-
import { TRAC_ADDRESS_SIZE } from '../../../../src/utils/constants.js';
|
|
4
3
|
import { randomAddress } from '../stateTestUtils.js';
|
|
5
4
|
import addressUtils from '../../../../src/core/state/utils/address.js';
|
|
5
|
+
import { address as addressApi } from 'trac-crypto-api';
|
|
6
6
|
|
|
7
7
|
test('Convert bech32m address to and from buffer - Happy Path', t => {
|
|
8
8
|
const hrp = 'test';
|
|
@@ -14,8 +14,8 @@ test('Convert bech32m address to and from buffer - Happy Path', t => {
|
|
|
14
14
|
t.ok(b4a.isBuffer(addressBuffer), 'Address buffer should be a Buffer instance');
|
|
15
15
|
t.is(typeof reconstructedAddress, 'string', 'Reconstructed address should be a string');
|
|
16
16
|
t.is(address, reconstructedAddress, 'Reconstructed address should match original');
|
|
17
|
-
t.is(address.length,
|
|
18
|
-
t.is(addressBuffer.length,
|
|
17
|
+
t.is(address.length, addressApi.size(hrp), 'Address length should match expected size');
|
|
18
|
+
t.is(addressBuffer.length, addressApi.size(hrp), 'Address buffer length should match address length');
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
test('isAddressValid returns false for wrong prefix', t => {
|
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
import { test } from 'brittle';
|
|
2
2
|
import b4a from 'b4a';
|
|
3
|
-
import { WRITER_BYTE_LENGTH
|
|
3
|
+
import { WRITER_BYTE_LENGTH } from '../../../../src/utils/constants.js';
|
|
4
4
|
import { randomAddress, randomBuffer } from '../stateTestUtils.js';
|
|
5
5
|
import addressUtils from '../../../../src/core/state/utils/address.js';
|
|
6
6
|
import adminEntryUtils from '../../../../src/core/state/utils/adminEntry.js';
|
|
7
|
+
import { config } from '../../../helpers/config.js';
|
|
7
8
|
|
|
8
|
-
const isAddressValid = addressUtils.isAddressValid;
|
|
9
|
+
const isAddressValid = address => addressUtils.isAddressValid(address, config.addressPrefix);
|
|
9
10
|
const addressToBuffer = addressUtils.addressToBuffer;
|
|
10
|
-
const encodeAdminEntry = adminEntryUtils.encode;
|
|
11
|
-
const decodeAdminEntry = adminEntryUtils.decode;
|
|
12
|
-
const ADMIN_ENTRY_SIZE =
|
|
11
|
+
const encodeAdminEntry = (address, wk) => adminEntryUtils.encode(address, wk, config.addressPrefix);
|
|
12
|
+
const decodeAdminEntry = entry => adminEntryUtils.decode(entry, config.addressPrefix);
|
|
13
|
+
const ADMIN_ENTRY_SIZE = config.addressLength + WRITER_BYTE_LENGTH;
|
|
13
14
|
|
|
14
15
|
test('Admin Entry - Encode and Decode - Happy Path', t => {
|
|
15
|
-
const address = randomAddress();
|
|
16
|
+
const address = randomAddress(config.addressPrefix);
|
|
16
17
|
const wk = randomBuffer(WRITER_BYTE_LENGTH);
|
|
17
18
|
|
|
18
|
-
const encoded = encodeAdminEntry(addressToBuffer(address), wk);
|
|
19
|
+
const encoded = encodeAdminEntry(addressToBuffer(address, config.addressPrefix), wk);
|
|
19
20
|
t.is(encoded.length, ADMIN_ENTRY_SIZE, "encoding has valid length");
|
|
20
21
|
|
|
21
22
|
const decoded = decodeAdminEntry(encoded);
|
|
@@ -26,8 +27,8 @@ test('Admin Entry - Encode and Decode - Happy Path', t => {
|
|
|
26
27
|
});
|
|
27
28
|
|
|
28
29
|
test('Admin Entry - Encode returns empty buffer on invalid input', t => {
|
|
29
|
-
const addrString = randomAddress();
|
|
30
|
-
const validAddress = addressToBuffer(addrString);
|
|
30
|
+
const addrString = randomAddress(config.addressPrefix);
|
|
31
|
+
const validAddress = addressToBuffer(addrString, config.addressPrefix);
|
|
31
32
|
const separatorIndex = addrString.indexOf('1');
|
|
32
33
|
const invalidAddress = validAddress.subarray(separatorIndex); // missing HRP
|
|
33
34
|
|
package/tests/unit/unit.test.js
CHANGED
|
@@ -7,7 +7,7 @@ async function runTests() {
|
|
|
7
7
|
await import('./network/networkModule.test.js')
|
|
8
8
|
await import('./state/stateModule.test.js');
|
|
9
9
|
await import('./utils/utils.test.js');
|
|
10
|
-
|
|
10
|
+
await import('./messages/messages.test.js');
|
|
11
11
|
test.resume();
|
|
12
12
|
}
|
|
13
13
|
|