trac-msb 0.2.7 → 0.2.8

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.
Files changed (146) hide show
  1. package/.github/workflows/publish.yml +8 -16
  2. package/docs/networking-dualstack-plan.md +75 -0
  3. package/docs/networking-layer-redesign.md +155 -0
  4. package/msb.mjs +11 -23
  5. package/package.json +2 -3
  6. package/rpc/{create_server.mjs → create_server.js} +2 -2
  7. package/rpc/{handlers.mjs → handlers.js} +5 -5
  8. package/rpc/routes/{index.mjs → index.js} +1 -1
  9. package/rpc/routes/{v1.mjs → v1.js} +1 -1
  10. package/rpc/{rpc_server.mjs → rpc_server.js} +1 -1
  11. package/rpc/rpc_services.js +4 -4
  12. package/src/config/config.js +137 -0
  13. package/src/config/env.js +61 -0
  14. package/src/core/network/Network.js +119 -73
  15. package/src/core/network/identity/NetworkWalletFactory.js +3 -4
  16. package/src/core/network/messaging/NetworkMessages.js +12 -11
  17. package/src/core/network/messaging/handlers/GetRequestHandler.js +5 -4
  18. package/src/core/network/messaging/handlers/ResponseHandler.js +4 -5
  19. package/src/core/network/messaging/handlers/RoleOperationHandler.js +17 -19
  20. package/src/core/network/messaging/handlers/SubnetworkOperationHandler.js +44 -38
  21. package/src/core/network/messaging/handlers/TransferOperationHandler.js +29 -25
  22. package/src/core/network/messaging/handlers/base/BaseOperationHandler.js +20 -21
  23. package/src/core/network/messaging/routes/NetworkMessageRouter.js +24 -20
  24. package/src/core/network/messaging/validators/AdminResponse.js +2 -2
  25. package/src/core/network/messaging/validators/CustomNodeResponse.js +2 -2
  26. package/src/core/network/messaging/validators/PartialBootstrapDeployment.js +3 -3
  27. package/src/core/network/messaging/validators/PartialRoleAccess.js +15 -12
  28. package/src/core/network/messaging/validators/PartialTransaction.js +9 -10
  29. package/src/core/network/messaging/validators/PartialTransfer.js +10 -7
  30. package/src/core/network/messaging/validators/ValidatorResponse.js +2 -2
  31. package/src/core/network/messaging/validators/base/BaseResponse.js +13 -5
  32. package/src/core/network/messaging/validators/base/PartialOperation.js +37 -21
  33. package/src/core/network/services/ConnectionManager.js +9 -15
  34. package/src/core/network/services/MessageOrchestrator.js +10 -22
  35. package/src/core/network/services/TransactionPoolService.js +9 -8
  36. package/src/core/network/services/ValidatorObserverService.js +46 -21
  37. package/src/core/state/State.js +136 -139
  38. package/src/core/state/utils/address.js +18 -16
  39. package/src/core/state/utils/adminEntry.js +17 -16
  40. package/src/core/state/utils/deploymentEntry.js +15 -15
  41. package/src/core/state/utils/transaction.js +3 -95
  42. package/src/index.js +153 -201
  43. package/src/messages/completeStateMessages/CompleteStateMessageBuilder.js +36 -32
  44. package/src/messages/completeStateMessages/CompleteStateMessageOperations.js +39 -42
  45. package/src/messages/partialStateMessages/PartialStateMessageBuilder.js +20 -20
  46. package/src/messages/partialStateMessages/PartialStateMessageOperations.js +29 -22
  47. package/src/utils/check.js +21 -17
  48. package/src/utils/cliCommands.js +11 -11
  49. package/src/utils/constants.js +2 -10
  50. package/src/utils/fileUtils.js +1 -4
  51. package/src/utils/helpers.js +9 -20
  52. package/src/utils/migrationUtils.js +2 -2
  53. package/src/utils/normalizers.js +10 -9
  54. package/tests/acceptance/v1/account/account.test.mjs +2 -2
  55. package/tests/acceptance/v1/balance/balance.test.mjs +1 -1
  56. package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +11 -2
  57. package/tests/acceptance/v1/rpc.test.mjs +9 -9
  58. package/tests/acceptance/v1/tx/tx.test.mjs +4 -2
  59. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +7 -3
  60. package/tests/fixtures/check.fixtures.js +42 -42
  61. package/tests/fixtures/protobuf.fixtures.js +27 -26
  62. package/tests/helpers/StateNetworkFactory.js +3 -5
  63. package/tests/helpers/autobaseTestHelpers.js +1 -2
  64. package/tests/helpers/config.js +3 -0
  65. package/tests/helpers/setupApplyTests.js +89 -82
  66. package/tests/helpers/transactionPayloads.mjs +26 -12
  67. package/tests/integration/apply/addAdmin/addAdminBasic.test.js +10 -9
  68. package/tests/integration/apply/addAdmin/addAdminRecovery.test.js +20 -19
  69. package/tests/integration/apply/addIndexer.test.js +23 -21
  70. package/tests/integration/apply/addWhitelist.test.js +9 -9
  71. package/tests/integration/apply/addWriter.test.js +33 -32
  72. package/tests/integration/apply/banValidator.test.js +16 -9
  73. package/tests/integration/apply/postTx/invalidSubValues.test.js +4 -4
  74. package/tests/integration/apply/postTx/postTx.test.js +7 -33
  75. package/tests/integration/apply/removeIndexer.test.js +11 -7
  76. package/tests/integration/apply/removeWriter.test.js +20 -19
  77. package/tests/integration/apply/transfer.test.js +18 -16
  78. package/tests/unit/messageOperations/assembleAddIndexerMessage.test.js +2 -2
  79. package/tests/unit/messageOperations/assembleAddWriterMessage.test.js +2 -1
  80. package/tests/unit/messageOperations/assembleAdminMessage.test.js +9 -10
  81. package/tests/unit/messageOperations/assembleBanWriterMessage.test.js +3 -2
  82. package/tests/unit/messageOperations/assemblePostTransaction.test.js +25 -43
  83. package/tests/unit/messageOperations/assembleRemoveIndexerMessage.test.js +2 -2
  84. package/tests/unit/messageOperations/assembleRemoveWriterMessage.test.js +2 -2
  85. package/tests/unit/messageOperations/assembleWhitelistMessages.test.js +5 -4
  86. package/tests/unit/messageOperations/commonsStateMessageOperationsTest.js +4 -3
  87. package/tests/unit/network/ConnectionManager.test.js +4 -2
  88. package/tests/unit/network/NetworkWalletFactory.test.js +14 -14
  89. package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +6 -6
  90. package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +8 -8
  91. package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +6 -5
  92. package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +24 -23
  93. package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +10 -16
  94. package/tests/unit/state/apply/addWriter/addWriterValidatorRewardScenario.js +2 -1
  95. package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +45 -41
  96. package/tests/unit/state/apply/adminRecovery/state.apply.adminRecovery.test.js +3 -7
  97. package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +17 -16
  98. package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +3 -4
  99. package/tests/unit/state/apply/balanceInitialization/nodeEntryBalanceUpdateFailureScenario.js +2 -1
  100. package/tests/unit/state/apply/banValidator/banValidatorBanAndReWhitelistScenario.js +2 -1
  101. package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +23 -25
  102. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentDuplicateRegistrationScenario.js +2 -1
  103. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +19 -18
  104. package/tests/unit/state/apply/common/access-control/adminConsistencyMismatchScenario.js +5 -4
  105. package/tests/unit/state/apply/common/access-control/adminPublicKeyDecodeFailureScenario.js +4 -3
  106. package/tests/unit/state/apply/common/balances/base/requesterBalanceScenarioBase.js +2 -1
  107. package/tests/unit/state/apply/common/commonScenarioHelper.js +3 -4
  108. package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +2 -2
  109. package/tests/unit/state/apply/common/payload-structure/invalidHashValidationScenario.js +2 -2
  110. package/tests/unit/state/apply/common/requester/requesterNodeEntryBufferMissingScenario.js +2 -1
  111. package/tests/unit/state/apply/common/requester/requesterNodeEntryDecodeFailureScenario.js +2 -1
  112. package/tests/unit/state/apply/common/validatorConsistency/base/validatorConsistencyScenarioBase.js +2 -1
  113. package/tests/unit/state/apply/common/validatorEntryValidation/base/validatorEntryValidationScenarioBase.js +2 -1
  114. package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +11 -10
  115. package/tests/unit/state/apply/removeIndexer/removeIndexerScenarioHelpers.js +6 -5
  116. package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +6 -7
  117. package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +35 -34
  118. package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +44 -43
  119. package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +26 -25
  120. package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardScenarioFactory.js +2 -1
  121. package/tests/unit/state/stateModule.test.js +0 -1
  122. package/tests/unit/state/stateTestUtils.js +7 -3
  123. package/tests/unit/state/utils/address.test.js +3 -3
  124. package/tests/unit/state/utils/adminEntry.test.js +10 -9
  125. package/tests/unit/utils/check/adminControlOperation.test.js +3 -3
  126. package/tests/unit/utils/check/balanceInitializationOperation.test.js +2 -2
  127. package/tests/unit/utils/check/bootstrapDeploymentOperation.test.js +2 -3
  128. package/tests/unit/utils/check/common.test.js +7 -6
  129. package/tests/unit/utils/check/coreAdminOperation.test.js +3 -3
  130. package/tests/unit/utils/check/roleAccessOperation.test.js +3 -2
  131. package/tests/unit/utils/check/transactionOperation.test.js +3 -3
  132. package/tests/unit/utils/check/transferOperation.test.js +3 -3
  133. package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +2 -1
  134. package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +2 -1
  135. package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +7 -0
  136. package/tests/unit/utils/utils.test.js +0 -1
  137. package/src/core/state/utils/indexerEntry.js +0 -105
  138. package/src/utils/crypto.js +0 -11
  139. package/tests/unit/state/utils/indexerEntry.test.js +0 -83
  140. package/tests/unit/state/utils/transaction.test.js +0 -97
  141. package/tests/unit/utils/crypto/createHash.test.js +0 -15
  142. /package/rpc/{constants.mjs → constants.js} +0 -0
  143. /package/rpc/{cors.mjs → cors.js} +0 -0
  144. /package/rpc/utils/{confirmedParameter.mjs → confirmedParameter.js} +0 -0
  145. /package/rpc/utils/{helpers.mjs → helpers.js} +0 -0
  146. /package/rpc/utils/{url.mjs → url.js} +0 -0
@@ -2,12 +2,10 @@ import b4a from 'b4a';
2
2
  import PeerWallet from 'trac-wallet';
3
3
 
4
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 { TRAC_ADDRESS_SIZE, NETWORK_ID } from '../../utils/constants.js';
9
- import { isAddressValid } from "../../core/state/utils/address.js";
10
- import { blake3Hash } from '../../utils/crypto.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";
11
9
  import {
12
10
  isCoreAdmin,
13
11
  isAdminControl,
@@ -20,6 +18,7 @@ import {
20
18
 
21
19
  class CompleteStateMessageBuilder extends StateBuilder {
22
20
  #wallet;
21
+ #config
23
22
  #operationType;
24
23
  #address;
25
24
  #writingKey;
@@ -37,16 +36,21 @@ class CompleteStateMessageBuilder extends StateBuilder {
37
36
  #txValidity;
38
37
  #amount;
39
38
 
40
- constructor(wallet) {
39
+ /**
40
+ *
41
+ * @param {PeerWallet} wallet
42
+ * @param {Config} config
43
+ */
44
+ constructor(wallet, config) {
41
45
  super();
46
+ this.#config = config;
42
47
  if (!wallet || typeof wallet !== 'object') {
43
48
  throw new Error('Wallet must be a valid wallet object');
44
49
  }
45
- if (!isAddressValid(wallet.address)) {
50
+ if (!isAddressValid(wallet.address, this.#config.addressPrefix)) {
46
51
  throw new Error('Wallet should have a valid TRAC address.');
47
52
  }
48
53
 
49
-
50
54
  this.#wallet = wallet;
51
55
  this.reset();
52
56
  }
@@ -80,15 +84,15 @@ class CompleteStateMessageBuilder extends StateBuilder {
80
84
  }
81
85
 
82
86
  withAddress(address) {
83
- if (b4a.isBuffer(address) && address.length === TRAC_ADDRESS_SIZE) {
84
- address = bufferToAddress(address);
87
+ if (b4a.isBuffer(address) && address.length === this.#config.addressLength) {
88
+ address = bufferToAddress(address, this.#config.addressPrefix);
85
89
  }
86
90
 
87
- if (!isAddressValid(address)) {
88
- throw new Error(`Address field must be a valid TRAC bech32m address with length ${TRAC_ADDRESS_SIZE}.`);
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}.`);
89
93
  }
90
94
 
91
- this.#address = addressToBuffer(address);
95
+ this.#address = addressToBuffer(address, this.#config.addressPrefix);
92
96
  this.#payload.address = this.#address;
93
97
  return this;
94
98
  }
@@ -110,15 +114,15 @@ class CompleteStateMessageBuilder extends StateBuilder {
110
114
  }
111
115
 
112
116
  withIncomingAddress(address) {
113
- if (b4a.isBuffer(address) && address.length === TRAC_ADDRESS_SIZE) {
114
- address = bufferToAddress(address);
117
+ if (b4a.isBuffer(address) && address.length === this.#config.addressLength) {
118
+ address = bufferToAddress(address, this.#config.addressPrefix);
115
119
  }
116
120
 
117
- if (!isAddressValid(address)) {
118
- throw new Error(`Address field must be a valid TRAC bech32m address with length ${TRAC_ADDRESS_SIZE}.`);
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}.`);
119
123
  }
120
124
 
121
- this.#incomingAddress = addressToBuffer(address);
125
+ this.#incomingAddress = addressToBuffer(address, this.#config.addressPrefix);
122
126
  return this;
123
127
  }
124
128
 
@@ -216,7 +220,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
216
220
  case OperationType.ADD_ADMIN:
217
221
  case OperationType.DISABLE_INITIALIZATION:
218
222
  msg = createMessage(
219
- NETWORK_ID,
223
+ this.#config.networkId,
220
224
  this.#txValidity,
221
225
  this.#writingKey,
222
226
  nonce,
@@ -228,7 +232,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
228
232
  throw new Error('All balance initialization fields must be set before building the message!');
229
233
  }
230
234
  msg = createMessage(
231
- NETWORK_ID,
235
+ this.#config.networkId,
232
236
  this.#txValidity,
233
237
  this.#incomingAddress,
234
238
  this.#amount,
@@ -241,7 +245,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
241
245
  case OperationType.REMOVE_WRITER:
242
246
  case OperationType.ADMIN_RECOVERY:
243
247
  msg = createMessage(
244
- NETWORK_ID,
248
+ this.#config.networkId,
245
249
  this.#txHash,
246
250
  nonce,
247
251
  this.#operationType
@@ -252,12 +256,12 @@ class CompleteStateMessageBuilder extends StateBuilder {
252
256
  case OperationType.ADD_INDEXER:
253
257
  case OperationType.REMOVE_INDEXER:
254
258
  case OperationType.BAN_VALIDATOR:
255
- if (this.#wallet.address === bufferToAddress(this.#incomingAddress)) {
259
+ if (this.#wallet.address === bufferToAddress(this.#incomingAddress, this.#config.addressPrefix)) {
256
260
  throw new Error('Address must not be the same as the wallet address for basic operations.');
257
261
  }
258
262
 
259
263
  msg = createMessage(
260
- NETWORK_ID,
264
+ this.#config.networkId,
261
265
  this.#txValidity,
262
266
  this.#incomingAddress,
263
267
  nonce,
@@ -271,7 +275,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
271
275
  throw new Error('All bootstrap deployment fields must be set before building the message!');
272
276
  }
273
277
  msg = createMessage(
274
- NETWORK_ID,
278
+ this.#config.networkId,
275
279
  this.#txHash,
276
280
  nonce,
277
281
  this.#operationType
@@ -286,7 +290,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
286
290
  throw new Error('All postTx fields must be set before building the message!');
287
291
  }
288
292
  msg = createMessage(
289
- NETWORK_ID,
293
+ this.#config.networkId,
290
294
  this.#txHash,
291
295
  nonce,
292
296
  this.#operationType
@@ -299,7 +303,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
299
303
  throw new Error('All transfer fields must be set before building the message!');
300
304
  }
301
305
  msg = createMessage(
302
- NETWORK_ID,
306
+ this.#config.networkId,
303
307
  this.#txHash,
304
308
  nonce,
305
309
  this.#operationType
@@ -310,7 +314,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
310
314
  throw new Error(`Unsupported operation type for building value: ${OperationType[this.#operationType]}.`);
311
315
  }
312
316
 
313
- tx = await blake3Hash(msg);
317
+ tx = await PeerWallet.blake3(msg);
314
318
  signature = this.#wallet.sign(tx);
315
319
 
316
320
  if (isCoreAdmin(this.#operationType)) {
@@ -337,7 +341,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
337
341
  iw: this.#incomingWriterKey,
338
342
  in: this.#incomingNonce,
339
343
  is: this.#incomingSignature,
340
- va: addressToBuffer(this.#wallet.address),
344
+ va: addressToBuffer(this.#wallet.address, this.#config.addressPrefix),
341
345
  vn: nonce,
342
346
  vs: signature,
343
347
  };
@@ -351,7 +355,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
351
355
  mbs: this.#msbBootstrap,
352
356
  in: this.#incomingNonce,
353
357
  is: this.#incomingSignature,
354
- va: addressToBuffer(this.#wallet.address),
358
+ va: addressToBuffer(this.#wallet.address, this.#config.addressPrefix),
355
359
  vn: nonce,
356
360
  vs: signature,
357
361
  };
@@ -363,7 +367,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
363
367
  ic: this.#channel,
364
368
  in: this.#incomingNonce,
365
369
  is: this.#incomingSignature,
366
- va: addressToBuffer(this.#wallet.address),
370
+ va: addressToBuffer(this.#wallet.address, this.#config.addressPrefix),
367
371
  vn: nonce,
368
372
  vs: signature
369
373
  }
@@ -375,7 +379,7 @@ class CompleteStateMessageBuilder extends StateBuilder {
375
379
  am: this.#amount,
376
380
  in: this.#incomingNonce,
377
381
  is: this.#incomingSignature,
378
- va: addressToBuffer(this.#wallet.address),
382
+ va: addressToBuffer(this.#wallet.address, this.#config.addressPrefix),
379
383
  vn: nonce,
380
384
  vs: signature
381
385
  }
@@ -1,35 +1,38 @@
1
1
  import CompleteStateMessageDirector from './CompleteStateMessageDirector.js';
2
2
  import CompleteStateMessageBuilder from './CompleteStateMessageBuilder.js';
3
3
  import { safeEncodeApplyOperation } from '../../utils/protobuf/operationHelpers.js';
4
- import fileUtils from '../../../src/utils/fileUtils.js';
5
4
 
6
5
  class CompleteStateMessageOperations {
6
+ #config
7
+ #wallet
8
+ constructor(wallet, config) {
9
+ this.#wallet = wallet
10
+ this.#config = config
11
+ }
7
12
 
8
- static async assembleAddAdminMessage(wallet, writingKey, txValidity) {
13
+ async assembleAddAdminMessage(writingKey, txValidity) {
9
14
  try {
10
- const builder = new CompleteStateMessageBuilder(wallet);
15
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
11
16
  const director = new CompleteStateMessageDirector();
12
17
  director.builder = builder;
13
18
 
14
- const payload = await director.buildAddAdminMessage(wallet.address, writingKey, txValidity);
19
+ const payload = await director.buildAddAdminMessage(this.#wallet.address, writingKey, txValidity);
15
20
  return safeEncodeApplyOperation(payload);
16
-
17
21
  } catch (error) {
18
22
  throw new Error(`Failed to assemble admin message: ${error.message}`);
19
23
  }
20
24
  }
21
25
 
22
- static async assembleDisableInitializationMessage(wallet, writingKey, txValidity) {
23
- const builder = new CompleteStateMessageBuilder(wallet);
26
+ async assembleDisableInitializationMessage(writingKey, txValidity) {
27
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
24
28
  const director = new CompleteStateMessageDirector();
25
29
  director.builder = builder;
26
30
 
27
- const payload = await director.buildDisableInitializationMessage(wallet.address, writingKey, txValidity);
31
+ const payload = await director.buildDisableInitializationMessage(this.#wallet.address, writingKey, txValidity);
28
32
  return safeEncodeApplyOperation(payload);
29
33
  }
30
34
 
31
- static async assembleAddWriterMessage(
32
- wallet,
35
+ async assembleAddWriterMessage(
33
36
  invokerAddress,
34
37
  transactionHash,
35
38
  txValidity,
@@ -38,7 +41,7 @@ class CompleteStateMessageOperations {
38
41
  incomingSignature
39
42
  ) {
40
43
  try {
41
- const builder = new CompleteStateMessageBuilder(wallet);
44
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
42
45
  const director = new CompleteStateMessageDirector();
43
46
  director.builder = builder;
44
47
 
@@ -57,8 +60,7 @@ class CompleteStateMessageOperations {
57
60
  }
58
61
  }
59
62
 
60
- static async assembleRemoveWriterMessage(
61
- wallet,
63
+ async assembleRemoveWriterMessage(
62
64
  invokerAddress,
63
65
  transactionHash,
64
66
  txValidity,
@@ -67,7 +69,7 @@ class CompleteStateMessageOperations {
67
69
  incomingSignature
68
70
  ) {
69
71
  try {
70
- const builder = new CompleteStateMessageBuilder(wallet);
72
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
71
73
  const director = new CompleteStateMessageDirector();
72
74
  director.builder = builder;
73
75
 
@@ -86,8 +88,7 @@ class CompleteStateMessageOperations {
86
88
  }
87
89
  }
88
90
 
89
- static async assembleAdminRecoveryMessage(
90
- wallet,
91
+ async assembleAdminRecoveryMessage(
91
92
  invokerAddress,
92
93
  transactionHash,
93
94
  txValidity,
@@ -96,7 +97,7 @@ class CompleteStateMessageOperations {
96
97
  incomingSignature
97
98
  ) {
98
99
  try {
99
- const builder = new CompleteStateMessageBuilder(wallet);
100
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
100
101
  const director = new CompleteStateMessageDirector();
101
102
  director.builder = builder;
102
103
 
@@ -115,13 +116,13 @@ class CompleteStateMessageOperations {
115
116
  }
116
117
  }
117
118
 
118
- static async assembleAddIndexerMessage(wallet, incomingAddress, txValidity) {
119
+ async assembleAddIndexerMessage(incomingAddress, txValidity) {
119
120
  try {
120
- const builder = new CompleteStateMessageBuilder(wallet);
121
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
121
122
  const director = new CompleteStateMessageDirector();
122
123
  director.builder = builder;
123
124
 
124
- const payload = await director.buildAddIndexerMessage(wallet.address, incomingAddress, txValidity);
125
+ const payload = await director.buildAddIndexerMessage(this.#wallet.address, incomingAddress, txValidity);
125
126
  return safeEncodeApplyOperation(payload);
126
127
 
127
128
  } catch (error) {
@@ -131,13 +132,13 @@ class CompleteStateMessageOperations {
131
132
 
132
133
 
133
134
 
134
- static async assembleRemoveIndexerMessage(wallet, incomingAddress, txValidity) {
135
+ async assembleRemoveIndexerMessage(incomingAddress, txValidity) {
135
136
  try {
136
- const builder = new CompleteStateMessageBuilder(wallet);
137
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
137
138
  const director = new CompleteStateMessageDirector();
138
139
  director.builder = builder;
139
140
 
140
- const payload = await director.buildRemoveIndexerMessage(wallet.address, incomingAddress, txValidity);
141
+ const payload = await director.buildRemoveIndexerMessage(this.#wallet.address, incomingAddress, txValidity);
141
142
  return safeEncodeApplyOperation(payload);
142
143
 
143
144
  } catch (error) {
@@ -145,14 +146,13 @@ class CompleteStateMessageOperations {
145
146
  }
146
147
  }
147
148
 
148
- static async assembleAppendWhitelistMessages(wallet, txValidity, addressToWhitelist) {
149
+ async assembleAppendWhitelistMessages(txValidity, addressToWhitelist) {
149
150
  try {
150
-
151
- const builder = new CompleteStateMessageBuilder(wallet);
151
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
152
152
  const director = new CompleteStateMessageDirector();
153
153
  director.builder = builder;
154
154
 
155
- const payload = await director.buildAppendWhitelistMessage(wallet.address, addressToWhitelist, txValidity);
155
+ const payload = await director.buildAppendWhitelistMessage(this.#wallet.address, addressToWhitelist, txValidity);
156
156
 
157
157
  return safeEncodeApplyOperation(payload);;
158
158
  } catch (error) {
@@ -160,9 +160,9 @@ class CompleteStateMessageOperations {
160
160
  }
161
161
  }
162
162
 
163
- static async assembleBalanceInitializationMessages(wallet, txValidity, addressBalancePair) {
163
+ async assembleBalanceInitializationMessages(txValidity, addressBalancePair) {
164
164
  try {
165
- const builder = new CompleteStateMessageBuilder(wallet);
165
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
166
166
  const director = new CompleteStateMessageDirector();
167
167
  director.builder = builder;
168
168
 
@@ -170,7 +170,7 @@ class CompleteStateMessageOperations {
170
170
 
171
171
  for (const [recipientAddress, balanceBuffer] of addressBalancePair) {
172
172
  const payload = await director.buildBalanceInitializationMessage(
173
- wallet.address,
173
+ this.#wallet.address,
174
174
  recipientAddress,
175
175
  balanceBuffer,
176
176
  txValidity
@@ -184,13 +184,13 @@ class CompleteStateMessageOperations {
184
184
  }
185
185
  }
186
186
 
187
- static async assembleBanWriterMessage(wallet, incomingAddress, txValidity) {
187
+ async assembleBanWriterMessage(incomingAddress, txValidity) {
188
188
  try {
189
- const builder = new CompleteStateMessageBuilder(wallet);
189
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
190
190
  const director = new CompleteStateMessageDirector();
191
191
  director.builder = builder;
192
192
 
193
- const payload = await director.buildBanWriterMessage(wallet.address, incomingAddress, txValidity);
193
+ const payload = await director.buildBanWriterMessage(this.#wallet.address, incomingAddress, txValidity);
194
194
  return safeEncodeApplyOperation(payload);
195
195
 
196
196
  } catch (error) {
@@ -198,8 +198,7 @@ class CompleteStateMessageOperations {
198
198
  }
199
199
  }
200
200
 
201
- static async assembleCompleteTransactionOperationMessage(
202
- wallet,
201
+ async assembleCompleteTransactionOperationMessage(
203
202
  invokerAddress,
204
203
  txHash,
205
204
  txValidity,
@@ -211,7 +210,7 @@ class CompleteStateMessageOperations {
211
210
  msbBootstrap
212
211
  ) {
213
212
  try {
214
- const builder = new CompleteStateMessageBuilder(wallet);
213
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
215
214
  const director = new CompleteStateMessageDirector();
216
215
  director.builder = builder;
217
216
  const payload = await director.buildTransactionOperationMessage(
@@ -232,8 +231,7 @@ class CompleteStateMessageOperations {
232
231
  }
233
232
  }
234
233
 
235
- static async assembleCompleteBootstrapDeployment(
236
- wallet,
234
+ async assembleCompleteBootstrapDeployment(
237
235
  invokerAddress,
238
236
  transactionHash,
239
237
  txValidity,
@@ -243,7 +241,7 @@ class CompleteStateMessageOperations {
243
241
  incomingSignature
244
242
  ) {
245
243
  try {
246
- const builder = new CompleteStateMessageBuilder(wallet);
244
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
247
245
  const director = new CompleteStateMessageDirector();
248
246
  director.builder = builder;
249
247
 
@@ -263,8 +261,7 @@ class CompleteStateMessageOperations {
263
261
  }
264
262
  }
265
263
 
266
- static async assembleCompleteTransferOperationMessage(
267
- wallet,
264
+ async assembleCompleteTransferOperationMessage(
268
265
  invokerAddress,
269
266
  transactionHash,
270
267
  txValidity,
@@ -274,7 +271,7 @@ class CompleteStateMessageOperations {
274
271
  incomingSignature
275
272
  ) {
276
273
  try {
277
- const builder = new CompleteStateMessageBuilder(wallet);
274
+ const builder = new CompleteStateMessageBuilder(this.#wallet, this.#config);
278
275
  const director = new CompleteStateMessageDirector();
279
276
  director.builder = builder;
280
277
 
@@ -2,10 +2,9 @@ import PeerWallet from "trac-wallet";
2
2
  import b4a from "b4a";
3
3
 
4
4
  import StateBuilder from '../base/StateBuilder.js'
5
- import { OperationType, TRAC_ADDRESS_SIZE, NETWORK_ID } from '../../utils/constants.js';
6
- import { addressToBuffer, bufferToAddress, isAddressValid } from '../../core/state/utils/address.js';
5
+ import { OperationType } from '../../utils/constants.js';
6
+ import { addressToBuffer, isAddressValid } from '../../core/state/utils/address.js';
7
7
  import { isHexString } from "../../utils/helpers.js";
8
- import { blake3Hash } from "../../utils/crypto.js";
9
8
  import { createMessage } from "../../utils/buffer.js";
10
9
  import { isTransaction, isRoleAccess, isBootstrapDeployment, isTransfer } from "../../utils/operations.js";
11
10
 
@@ -18,23 +17,26 @@ class PartialStateMessageBuilder extends StateBuilder {
18
17
  #contentHash;
19
18
  #externalBootstrap;
20
19
  #withMsbBootstrap;
21
- #bootstrapNonce;
22
- #bootstrapSignature;
23
20
  #channel;
24
21
  #incomingAddress;
25
22
  #amount;
26
23
  #payload;
24
+ #config;
27
25
 
28
- constructor(wallet) {
26
+ /**
27
+ * @param {PeerWallet} wallet
28
+ * @param {object} config
29
+ **/
30
+ constructor(wallet, config) {
29
31
  super();
32
+ this.#config = config;
30
33
  if (!wallet || typeof wallet !== 'object') {
31
34
  throw new Error('Wallet must be a valid wallet object');
32
35
  }
33
- if (!isAddressValid(wallet.address)) {
36
+ if (!isAddressValid(wallet.address, this.#config.addressPrefix)) {
34
37
  throw new Error('Wallet should have a valid TRAC address.');
35
38
  }
36
39
 
37
-
38
40
  this.#wallet = wallet;
39
41
  this.reset();
40
42
  }
@@ -47,8 +49,6 @@ class PartialStateMessageBuilder extends StateBuilder {
47
49
  this.#contentHash = null;
48
50
  this.#externalBootstrap = null;
49
51
  this.#withMsbBootstrap = false;
50
- this.#bootstrapNonce = null;
51
- this.#bootstrapSignature = null;
52
52
  this.#incomingAddress = null;
53
53
  this.#amount = null;
54
54
  this.#channel = null;
@@ -66,8 +66,8 @@ class PartialStateMessageBuilder extends StateBuilder {
66
66
  }
67
67
 
68
68
  withAddress(address) {
69
- if (!isAddressValid(address)) {
70
- throw new Error(`Address field must be a valid TRAC bech32m address with length ${TRAC_ADDRESS_SIZE}.`);
69
+ if (!isAddressValid(address, this.#config.addressPrefix)) {
70
+ throw new Error(`Address field must be a valid TRAC bech32m address with length ${this.#config.addressLength}.`);
71
71
  }
72
72
 
73
73
  this.#address = address;
@@ -116,8 +116,8 @@ class PartialStateMessageBuilder extends StateBuilder {
116
116
  }
117
117
 
118
118
  withIncomingAddress(address) {
119
- if (!isAddressValid(address)) {
120
- throw new Error(`Incoming address field must be a valid TRAC bech32m address with length ${TRAC_ADDRESS_SIZE}.`);
119
+ if (!isAddressValid(address, this.#config.addressPrefix)) {
120
+ throw new Error(`Incoming address field must be a valid TRAC bech32m address with length ${this.#config.addressLength}.`);
121
121
  }
122
122
 
123
123
  this.#incomingAddress = address;
@@ -155,7 +155,7 @@ class PartialStateMessageBuilder extends StateBuilder {
155
155
  case OperationType.REMOVE_WRITER:
156
156
  case OperationType.ADMIN_RECOVERY:
157
157
  txMsg = createMessage(
158
- NETWORK_ID,
158
+ this.#config.networkId,
159
159
  b4a.from(this.#txValidity, 'hex'),
160
160
  b4a.from(this.#writingKey, 'hex'),
161
161
  nonce,
@@ -168,7 +168,7 @@ class PartialStateMessageBuilder extends StateBuilder {
168
168
  throw new Error('External bootstrap key must be set for BOOTSTRAP DEPLOYMENT operation.');
169
169
  }
170
170
  txMsg = createMessage(
171
- NETWORK_ID,
171
+ this.#config.networkId,
172
172
  b4a.from(this.#txValidity, 'hex'),
173
173
  b4a.from(this.#externalBootstrap, 'hex'),
174
174
  b4a.from(this.#channel, 'hex'),
@@ -179,7 +179,7 @@ class PartialStateMessageBuilder extends StateBuilder {
179
179
 
180
180
  case OperationType.TX:
181
181
  txMsg = createMessage(
182
- NETWORK_ID,
182
+ this.#config.networkId,
183
183
  b4a.from(this.#txValidity, 'hex'),
184
184
  b4a.from(this.#writingKey, 'hex'),
185
185
  b4a.from(this.#contentHash, 'hex'),
@@ -191,9 +191,9 @@ class PartialStateMessageBuilder extends StateBuilder {
191
191
  break;
192
192
  case OperationType.TRANSFER:
193
193
  txMsg = createMessage(
194
- NETWORK_ID,
194
+ this.#config.networkId,
195
195
  b4a.from(this.#txValidity, 'hex'),
196
- addressToBuffer(this.#incomingAddress), // we need to sign address of the recipient as well
196
+ addressToBuffer(this.#incomingAddress, this.#config.addressPrefix), // we need to sign address of the recipient as well
197
197
  b4a.from(this.#amount, 'hex'),
198
198
  nonce,
199
199
  OperationType.TRANSFER
@@ -204,7 +204,7 @@ class PartialStateMessageBuilder extends StateBuilder {
204
204
  }
205
205
 
206
206
  // tx and signature
207
- tx = await blake3Hash(txMsg);
207
+ tx = await PeerWallet.blake3(txMsg);
208
208
  signature = this.#wallet.sign(tx);
209
209
 
210
210
  // Build the payload based on operation type