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.
Files changed (187) hide show
  1. package/.github/workflows/publish.yml +8 -16
  2. package/msb.mjs +13 -25
  3. package/package.json +8 -4
  4. package/proto/network.proto +74 -0
  5. package/rpc/{create_server.mjs → create_server.js} +4 -4
  6. package/rpc/{handlers.mjs → handlers.js} +7 -7
  7. package/rpc/routes/{index.mjs → index.js} +1 -1
  8. package/rpc/routes/{v1.mjs → v1.js} +1 -1
  9. package/rpc/rpc_server.js +10 -0
  10. package/rpc/rpc_services.js +48 -7
  11. package/rpc/utils/{helpers.mjs → helpers.js} +1 -1
  12. package/src/config/config.js +137 -0
  13. package/src/config/env.js +63 -0
  14. package/src/core/network/Network.js +133 -119
  15. package/src/core/network/identity/NetworkWalletFactory.js +5 -6
  16. package/src/core/network/protocols/LegacyProtocol.js +67 -0
  17. package/src/core/network/protocols/NetworkMessages.js +48 -0
  18. package/src/core/network/protocols/ProtocolInterface.js +31 -0
  19. package/src/core/network/protocols/ProtocolSession.js +59 -0
  20. package/src/core/network/protocols/V1Protocol.js +64 -0
  21. package/src/core/network/protocols/legacy/NetworkMessageRouter.js +84 -0
  22. package/src/core/network/protocols/legacy/handlers/GetRequestHandler.js +53 -0
  23. package/src/core/network/protocols/legacy/handlers/ResponseHandler.js +37 -0
  24. package/src/core/network/{messaging → protocols/legacy}/validators/ValidatorResponse.js +2 -2
  25. package/src/core/network/{messaging → protocols/legacy}/validators/base/BaseResponse.js +13 -6
  26. package/src/core/network/protocols/shared/handlers/RoleOperationHandler.js +88 -0
  27. package/src/core/network/protocols/shared/handlers/SubnetworkOperationHandler.js +93 -0
  28. package/src/core/network/protocols/shared/handlers/TransferOperationHandler.js +57 -0
  29. package/src/core/network/{messaging → protocols/shared}/handlers/base/BaseOperationHandler.js +21 -26
  30. package/src/core/network/{messaging → protocols/shared}/validators/PartialBootstrapDeployment.js +3 -3
  31. package/src/core/network/{messaging → protocols/shared}/validators/PartialRoleAccess.js +15 -12
  32. package/src/core/network/{messaging → protocols/shared}/validators/PartialTransaction.js +10 -11
  33. package/src/core/network/{messaging → protocols/shared}/validators/PartialTransfer.js +10 -7
  34. package/src/core/network/{messaging → protocols/shared}/validators/base/PartialOperation.js +40 -22
  35. package/src/core/network/protocols/v1/NetworkMessageRouter.js +15 -0
  36. package/src/core/network/services/ConnectionManager.js +13 -19
  37. package/src/core/network/services/MessageOrchestrator.js +10 -22
  38. package/src/core/network/services/TransactionPoolService.js +10 -10
  39. package/src/core/network/services/TransactionRateLimiterService.js +5 -3
  40. package/src/core/network/services/ValidatorObserverService.js +46 -21
  41. package/src/core/state/State.js +137 -141
  42. package/src/core/state/utils/address.js +18 -16
  43. package/src/core/state/utils/adminEntry.js +17 -16
  44. package/src/core/state/utils/deploymentEntry.js +15 -15
  45. package/src/core/state/utils/transaction.js +3 -95
  46. package/src/index.js +250 -325
  47. package/src/messages/network/v1/NetworkMessageBuilder.js +325 -0
  48. package/src/messages/network/v1/NetworkMessageDirector.js +137 -0
  49. package/src/messages/network/v1/networkMessageFactory.js +12 -0
  50. package/src/messages/state/ApplyStateMessageBuilder.js +661 -0
  51. package/src/messages/state/ApplyStateMessageDirector.js +516 -0
  52. package/src/messages/state/applyStateMessageFactory.js +12 -0
  53. package/src/utils/buffer.js +53 -1
  54. package/src/utils/check.js +21 -17
  55. package/src/utils/cli.js +0 -8
  56. package/src/utils/cliCommands.js +11 -11
  57. package/src/utils/constants.js +36 -24
  58. package/src/utils/fileUtils.js +1 -4
  59. package/src/utils/helpers.js +9 -20
  60. package/src/utils/migrationUtils.js +2 -2
  61. package/src/utils/normalizers.js +94 -11
  62. package/src/utils/protobuf/network.cjs +840 -0
  63. package/src/utils/protobuf/operationHelpers.js +10 -0
  64. package/tests/acceptance/v1/account/account.test.mjs +2 -2
  65. package/tests/acceptance/v1/balance/balance.test.mjs +1 -1
  66. package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +11 -2
  67. package/tests/acceptance/v1/rpc.test.mjs +10 -10
  68. package/tests/acceptance/v1/tx/tx.test.mjs +4 -2
  69. package/tests/acceptance/v1/tx-details/tx-details.test.mjs +7 -3
  70. package/tests/fixtures/check.fixtures.js +42 -42
  71. package/tests/fixtures/networkV1.fixtures.js +84 -0
  72. package/tests/fixtures/protobuf.fixtures.js +110 -26
  73. package/tests/helpers/StateNetworkFactory.js +3 -5
  74. package/tests/helpers/autobaseTestHelpers.js +1 -2
  75. package/tests/helpers/config.js +3 -0
  76. package/tests/helpers/setupApplyTests.js +113 -99
  77. package/tests/helpers/transactionPayloads.mjs +26 -12
  78. package/tests/unit/messages/messages.test.js +12 -0
  79. package/tests/unit/messages/network/NetworkMessageBuilder.test.js +276 -0
  80. package/tests/unit/messages/network/NetworkMessageDirector.test.js +203 -0
  81. package/tests/unit/messages/state/applyStateMessageBuilder.complete.test.js +521 -0
  82. package/tests/unit/messages/state/applyStateMessageBuilder.partial.test.js +233 -0
  83. package/tests/unit/network/ConnectionManager.test.js +10 -7
  84. package/tests/unit/network/NetworkWalletFactory.test.js +14 -14
  85. package/tests/unit/network/networkModule.test.js +3 -2
  86. package/tests/unit/state/apply/addAdmin/addAdminHappyPathScenario.js +10 -6
  87. package/tests/unit/state/apply/addAdmin/addAdminScenarioHelpers.js +11 -8
  88. package/tests/unit/state/apply/addAdmin/state.apply.addAdmin.test.js +11 -7
  89. package/tests/unit/state/apply/addIndexer/addIndexerScenarioHelpers.js +18 -20
  90. package/tests/unit/state/apply/addWriter/addWriterScenarioHelpers.js +57 -48
  91. package/tests/unit/state/apply/addWriter/addWriterValidatorRewardScenario.js +2 -1
  92. package/tests/unit/state/apply/adminRecovery/adminRecoveryScenarioHelpers.js +72 -57
  93. package/tests/unit/state/apply/adminRecovery/state.apply.adminRecovery.test.js +3 -7
  94. package/tests/unit/state/apply/appendWhitelist/appendWhitelistScenarioHelpers.js +12 -14
  95. package/tests/unit/state/apply/balanceInitialization/balanceInitializationScenarioHelpers.js +18 -13
  96. package/tests/unit/state/apply/balanceInitialization/nodeEntryBalanceUpdateFailureScenario.js +2 -1
  97. package/tests/unit/state/apply/banValidator/banValidatorBanAndReWhitelistScenario.js +2 -1
  98. package/tests/unit/state/apply/banValidator/banValidatorScenarioHelpers.js +27 -30
  99. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentDuplicateRegistrationScenario.js +2 -1
  100. package/tests/unit/state/apply/bootstrapDeployment/bootstrapDeploymentScenarioHelpers.js +24 -21
  101. package/tests/unit/state/apply/common/access-control/adminConsistencyMismatchScenario.js +5 -4
  102. package/tests/unit/state/apply/common/access-control/adminPublicKeyDecodeFailureScenario.js +4 -3
  103. package/tests/unit/state/apply/common/balances/base/requesterBalanceScenarioBase.js +2 -1
  104. package/tests/unit/state/apply/common/commonScenarioHelper.js +16 -16
  105. package/tests/unit/state/apply/common/payload-structure/initializationDisabledScenario.js +10 -5
  106. package/tests/unit/state/apply/common/payload-structure/invalidHashValidationScenario.js +2 -2
  107. package/tests/unit/state/apply/common/requester/requesterNodeEntryBufferMissingScenario.js +2 -1
  108. package/tests/unit/state/apply/common/requester/requesterNodeEntryDecodeFailureScenario.js +2 -1
  109. package/tests/unit/state/apply/common/validatorConsistency/base/validatorConsistencyScenarioBase.js +2 -1
  110. package/tests/unit/state/apply/common/validatorEntryValidation/base/validatorEntryValidationScenarioBase.js +2 -1
  111. package/tests/unit/state/apply/disableInitialization/disableInitializationScenarioHelpers.js +16 -9
  112. package/tests/unit/state/apply/removeIndexer/removeIndexerScenarioHelpers.js +6 -5
  113. package/tests/unit/state/apply/removeWriter/removeWriterScenarioHelpers.js +23 -19
  114. package/tests/unit/state/apply/transfer/transferDoubleSpendAcrossValidatorsScenario.js +45 -36
  115. package/tests/unit/state/apply/transfer/transferScenarioHelpers.js +48 -45
  116. package/tests/unit/state/apply/txOperation/txOperationScenarioHelpers.js +32 -29
  117. package/tests/unit/state/apply/txOperation/txOperationTransferFeeGuardScenarioFactory.js +2 -1
  118. package/tests/unit/state/stateModule.test.js +0 -1
  119. package/tests/unit/state/stateTestUtils.js +7 -3
  120. package/tests/unit/state/utils/address.test.js +3 -3
  121. package/tests/unit/state/utils/adminEntry.test.js +10 -9
  122. package/tests/unit/unit.test.js +1 -1
  123. package/tests/unit/utils/buffer/buffer.test.js +62 -1
  124. package/tests/unit/utils/check/adminControlOperation.test.js +3 -3
  125. package/tests/unit/utils/check/balanceInitializationOperation.test.js +2 -2
  126. package/tests/unit/utils/check/bootstrapDeploymentOperation.test.js +2 -3
  127. package/tests/unit/utils/check/common.test.js +7 -6
  128. package/tests/unit/utils/check/coreAdminOperation.test.js +3 -3
  129. package/tests/unit/utils/check/roleAccessOperation.test.js +3 -2
  130. package/tests/unit/utils/check/transactionOperation.test.js +3 -3
  131. package/tests/unit/utils/check/transferOperation.test.js +3 -3
  132. package/tests/unit/utils/fileUtils/readAddressesFromWhitelistFile.test.js +2 -1
  133. package/tests/unit/utils/fileUtils/readBalanceMigrationFile.test.js +2 -1
  134. package/tests/unit/utils/migrationUtils/validateAddressFromIncomingFile.test.js +7 -0
  135. package/tests/unit/utils/normalizers/normalizers.test.js +469 -0
  136. package/tests/unit/utils/protobuf/operationHelpers.test.js +120 -2
  137. package/tests/unit/utils/utils.test.js +0 -1
  138. package/rpc/rpc_server.mjs +0 -10
  139. package/src/core/network/messaging/NetworkMessages.js +0 -63
  140. package/src/core/network/messaging/handlers/GetRequestHandler.js +0 -112
  141. package/src/core/network/messaging/handlers/ResponseHandler.js +0 -108
  142. package/src/core/network/messaging/handlers/RoleOperationHandler.js +0 -116
  143. package/src/core/network/messaging/handlers/SubnetworkOperationHandler.js +0 -143
  144. package/src/core/network/messaging/handlers/TransferOperationHandler.js +0 -52
  145. package/src/core/network/messaging/routes/NetworkMessageRouter.js +0 -94
  146. package/src/core/network/messaging/validators/AdminResponse.js +0 -58
  147. package/src/core/network/messaging/validators/CustomNodeResponse.js +0 -46
  148. package/src/core/state/utils/indexerEntry.js +0 -105
  149. package/src/messages/base/StateBuilder.js +0 -25
  150. package/src/messages/completeStateMessages/CompleteStateMessageBuilder.js +0 -421
  151. package/src/messages/completeStateMessages/CompleteStateMessageDirector.js +0 -252
  152. package/src/messages/completeStateMessages/CompleteStateMessageOperations.js +0 -299
  153. package/src/messages/partialStateMessages/PartialStateMessageBuilder.js +0 -272
  154. package/src/messages/partialStateMessages/PartialStateMessageDirector.js +0 -137
  155. package/src/messages/partialStateMessages/PartialStateMessageOperations.js +0 -131
  156. package/src/utils/crypto.js +0 -11
  157. package/tests/integration/apply/addAdmin/addAdminBasic.test.js +0 -68
  158. package/tests/integration/apply/addAdmin/addAdminRecovery.test.js +0 -125
  159. package/tests/integration/apply/addIndexer.test.js +0 -237
  160. package/tests/integration/apply/addWhitelist.test.js +0 -53
  161. package/tests/integration/apply/addWriter.test.js +0 -244
  162. package/tests/integration/apply/apply.test.js +0 -19
  163. package/tests/integration/apply/banValidator.test.js +0 -109
  164. package/tests/integration/apply/postTx/invalidSubValues.test.js +0 -103
  165. package/tests/integration/apply/postTx/postTx.test.js +0 -222
  166. package/tests/integration/apply/removeIndexer.test.js +0 -128
  167. package/tests/integration/apply/removeWriter.test.js +0 -167
  168. package/tests/integration/apply/transfer.test.js +0 -81
  169. package/tests/integration/integration.test.js +0 -9
  170. package/tests/unit/messageOperations/assembleAddIndexerMessage.test.js +0 -21
  171. package/tests/unit/messageOperations/assembleAddWriterMessage.test.js +0 -16
  172. package/tests/unit/messageOperations/assembleAdminMessage.test.js +0 -69
  173. package/tests/unit/messageOperations/assembleBanWriterMessage.test.js +0 -16
  174. package/tests/unit/messageOperations/assemblePostTransaction.test.js +0 -442
  175. package/tests/unit/messageOperations/assembleRemoveIndexerMessage.test.js +0 -19
  176. package/tests/unit/messageOperations/assembleRemoveWriterMessage.test.js +0 -17
  177. package/tests/unit/messageOperations/assembleWhitelistMessages.test.js +0 -58
  178. package/tests/unit/messageOperations/commonsStateMessageOperationsTest.js +0 -277
  179. package/tests/unit/messageOperations/stateMessageOperations.test.js +0 -19
  180. package/tests/unit/state/utils/indexerEntry.test.js +0 -83
  181. package/tests/unit/state/utils/transaction.test.js +0 -97
  182. package/tests/unit/utils/crypto/createHash.test.js +0 -15
  183. /package/rpc/{constants.mjs → constants.js} +0 -0
  184. /package/rpc/{cors.mjs → cors.js} +0 -0
  185. /package/rpc/utils/{confirmedParameter.mjs → confirmedParameter.js} +0 -0
  186. /package/rpc/utils/{url.mjs → url.js} +0 -0
  187. /package/src/utils/{operations.js → applyOperations.js} +0 -0
@@ -1,7 +1,13 @@
1
1
  import b4a from 'b4a';
2
+ import { address as addressApi } from 'trac-crypto-api';
2
3
 
3
- import { TRAC_NETWORK_MSB_MAINNET_PREFIX } from 'trac-wallet/constants.js';
4
- import { TRAC_ADDRESS_SIZE } from '../../../utils/constants.js';
4
+ const boolSafe = condition => {
5
+ try {
6
+ return condition()
7
+ } catch (_ignored) {
8
+ return false
9
+ }
10
+ }
5
11
 
6
12
  /**
7
13
  * Checks if a given address is a valid TRAC bech32m address.
@@ -9,31 +15,28 @@ import { TRAC_ADDRESS_SIZE } from '../../../utils/constants.js';
9
15
  * So, it is possible that even if an address is considered valid,
10
16
  * it may not be a real address on the network.
11
17
  * @param {string | Buffer} address - The address to validate.
12
- * @param {string} [prefix] - The HRP of the bech32m address. Default is Trac Network mainnet prefix
18
+ * @param {string} hrp - The HRP of the bech32m address.
13
19
  * @returns {boolean} True if the address is valid, false otherwise.
14
20
  */
15
- export function isAddressValid(address, prefix = TRAC_NETWORK_MSB_MAINNET_PREFIX) {
21
+ export function isAddressValid(address, hrp) {
16
22
  if (b4a.isBuffer(address)) {
17
23
  address = address.toString('ascii');
18
24
  }
19
- const bech32Chars = /^[qpzry9x8gf2tvdw0s3jn54khce6mua7l]+$/;
20
- if (typeof address === 'string' &&
21
- address.length === TRAC_ADDRESS_SIZE &&
22
- address.startsWith(prefix + '1') &&
23
- bech32Chars.test(address.slice(prefix.length + 1))) {
24
- return true;
25
- }
26
- return false;
25
+
26
+ return boolSafe(() =>
27
+ addressApi.size(hrp) === address.length && address.startsWith(`${hrp}1`) && addressApi.isValid(address)
28
+ );
27
29
  }
28
30
 
29
31
 
30
32
  /**
31
33
  * Converts a valid bech32m address string to a buffer.
32
34
  * @param {string} bech32mAddress - The bech32m address to convert.
35
+ * @param {string} hrp - The HRP of the bech32m address.
33
36
  * @returns {Buffer} The buffer representation of the address, or an empty buffer if invalid.
34
37
  */
35
38
  // TODO: Check if a try-catch is really necessary here
36
- export function addressToBuffer(bech32mAddress, hrp = TRAC_NETWORK_MSB_MAINNET_PREFIX) {
39
+ export function addressToBuffer(bech32mAddress, hrp) {
37
40
  try {
38
41
  if (!isAddressValid(bech32mAddress, hrp)) {
39
42
  return b4a.alloc(0);
@@ -48,10 +51,11 @@ export function addressToBuffer(bech32mAddress, hrp = TRAC_NETWORK_MSB_MAINNET_P
48
51
  /**
49
52
  * Converts a buffer to a bech32m address string if valid.
50
53
  * @param {Buffer} dataBuffer - The buffer to convert.
54
+ * @param {string} hrp - The HRP of the bech32m address.
51
55
  * @returns {string|null} The address string if valid, otherwise null.
52
56
  */
53
57
  // TODO: Do we really need to try-catch here? Maybe we should only validate the input buffer.
54
- export function bufferToAddress(dataBuffer, hrp = TRAC_NETWORK_MSB_MAINNET_PREFIX) {
58
+ export function bufferToAddress(dataBuffer, hrp) {
55
59
  try {
56
60
  const address = dataBuffer.toString('ascii');
57
61
  if (!isAddressValid(address, hrp)) return null;
@@ -66,6 +70,4 @@ export default {
66
70
  isAddressValid,
67
71
  addressToBuffer,
68
72
  bufferToAddress,
69
- TRAC_ADDRESS_SIZE,
70
- TRAC_NETWORK_MSB_MAINNET_PREFIX
71
73
  };
@@ -1,11 +1,9 @@
1
1
  import b4a from 'b4a';
2
2
 
3
3
  import { bufferToAddress, isAddressValid } from './address.js';
4
- import {TRAC_NETWORK_MSB_MAINNET_PREFIX } from 'trac-wallet/constants.js';
5
- import { WRITER_BYTE_LENGTH, TRAC_ADDRESS_SIZE} from '../../../utils/constants.js';
4
+ import { WRITER_BYTE_LENGTH } from '../../../utils/constants.js';
6
5
  import { isBufferValid } from '../../../utils/buffer.js';
7
-
8
- const ADMIN_ENTRY_SIZE = TRAC_ADDRESS_SIZE + WRITER_BYTE_LENGTH;
6
+ import { address as addressApi } from 'trac-crypto-api';
9
7
 
10
8
  /**
11
9
  * Encodes an admin entry as a buffer containing the TRAC address and writing key.
@@ -15,17 +13,18 @@ const ADMIN_ENTRY_SIZE = TRAC_ADDRESS_SIZE + WRITER_BYTE_LENGTH;
15
13
  *
16
14
  * @param {Buffer} address - The admin address.
17
15
  * @param {Buffer} wk - The admin's writing key buffer (must be 32 bytes).
16
+ * @param {string} addressHrp - The HRP of the bech32m address.
18
17
  * @returns {Buffer} The encoded admin entry buffer, or an empty buffer if input is invalid.
19
18
  */
20
- export function encode(address, wk) {
19
+ export function encode(address, wk, addressHrp) {
21
20
  try {
22
- if (!isAddressValid(address, TRAC_NETWORK_MSB_MAINNET_PREFIX) ||
21
+ if (!isAddressValid(address, addressHrp) ||
23
22
  !isBufferValid(wk, WRITER_BYTE_LENGTH)) {
24
23
  throw new Error('Invalid address or writing key buffer');
25
24
  }
26
- const adminEntry = b4a.alloc(TRAC_ADDRESS_SIZE + WRITER_BYTE_LENGTH);
25
+ const adminEntry = b4a.alloc(address.length + WRITER_BYTE_LENGTH);
27
26
  b4a.copy(address, adminEntry, 0);
28
- b4a.copy(wk, adminEntry, TRAC_ADDRESS_SIZE);
27
+ b4a.copy(wk, adminEntry, address.length);
29
28
  return adminEntry;
30
29
  } catch (error) {
31
30
  console.error('Error when encoding admin entry:', error);
@@ -40,19 +39,21 @@ export function encode(address, wk) {
40
39
  * Where TRAC_ADDRESS is a bech32m-encoded address without the HRP and separator.
41
40
  *
42
41
  * @param {Buffer} adminEntry - The encoded admin entry buffer.
42
+ * @param {string} addressHrp - The HRP of the bech32m address.
43
43
  * @returns {Object | null} An object with:
44
44
  * - address: String containing the TRAC address.
45
45
  * - wk: Buffer containing the writing key.
46
46
  */
47
- export function decode(adminEntry) {
48
- if (!isBufferValid(adminEntry, ADMIN_ENTRY_SIZE)) {
49
- return null;
50
- }
51
-
47
+ export function decode(adminEntry, addressHrp) {
52
48
  try {
53
- const addressPart = adminEntry.subarray(0, TRAC_ADDRESS_SIZE);
54
- const address = bufferToAddress(addressPart, TRAC_NETWORK_MSB_MAINNET_PREFIX);
55
- const wk = adminEntry.subarray(TRAC_ADDRESS_SIZE);
49
+ const addressLength = addressApi.size(addressHrp)
50
+ if (!isBufferValid(adminEntry, addressLength + WRITER_BYTE_LENGTH)) {
51
+ return null;
52
+ }
53
+
54
+ const addressPart = adminEntry.subarray(0, addressLength);
55
+ const address = bufferToAddress(addressPart, addressHrp);
56
+ const wk = adminEntry.subarray(addressLength);
56
57
  return { address, wk };
57
58
  }
58
59
  catch (error) {
@@ -1,34 +1,33 @@
1
1
  import b4a from 'b4a';
2
2
 
3
- import { bufferToAddress, isAddressValid } from './address.js';
4
- import { TRAC_NETWORK_MSB_MAINNET_PREFIX } from 'trac-wallet/constants.js';
5
- import { BOOTSTRAP_BYTE_LENGTH, HASH_BYTE_LENGTH, TRAC_ADDRESS_SIZE } from '../../../utils/constants.js';
3
+ import { isAddressValid } from './address.js';
4
+ import { HASH_BYTE_LENGTH } from '../../../utils/constants.js';
6
5
  import { isBufferValid } from '../../../utils/buffer.js';
7
6
 
8
-
9
7
  /**
10
8
  * Encodes a transaction entry into a buffer containing the TX hash and TRAC address.
11
9
  *
12
10
  * Buffer format:
13
- * [TX_HASH(32)][TRAC_ADDRESS(TRAC_ADDRESS_SIZE)]
11
+ * [TX_HASH(32)][TRAC_ADDRESS(addressLength)]
14
12
  *
15
13
  * - TX_HASH: Transaction hash buffer (must be exactly HASH_BYTE_LENGTH bytes).
16
14
  * - TRAC_ADDRESS: A bech32m-encoded address (without HRP and separator).
17
15
  *
18
16
  * @param {Buffer} txHash - Transaction hash buffer.
19
17
  * @param {Buffer} address - The account address that initiated the deployment.
18
+ * @param {string} hrp - The HRP of the bech32m address.
20
19
  * @returns {Buffer} A buffer containing the encoded transaction entry, or an empty buffer if input is invalid.
21
20
  */
22
21
 
23
- export function encode(txHash, address) {
22
+ export function encode(txHash, address, hrp) {
24
23
  try {
25
24
  if (!isBufferValid(txHash, HASH_BYTE_LENGTH) ||
26
- !isAddressValid(address, TRAC_NETWORK_MSB_MAINNET_PREFIX)) {
25
+ !isAddressValid(address, hrp)) {
27
26
  console.error('Invalid txHash or address buffer');
28
27
  return b4a.alloc(0);
29
28
  }
30
29
 
31
- const entry = b4a.alloc(HASH_BYTE_LENGTH + TRAC_ADDRESS_SIZE);
30
+ const entry = b4a.alloc(HASH_BYTE_LENGTH + address.length);
32
31
 
33
32
  b4a.copy(txHash, entry, 0);
34
33
  b4a.copy(address, entry, HASH_BYTE_LENGTH);
@@ -44,32 +43,33 @@ export function encode(txHash, address) {
44
43
  * Decodes a transaction entry buffer into its TX hash and TRAC address.
45
44
  *
46
45
  * Buffer format:
47
- * [TX_HASH(32)][TRAC_ADDRESS(TRAC_ADDRESS_SIZE)]
46
+ * [TX_HASH(32)][TRAC_ADDRESS(addressLength)]
48
47
  *
49
48
  * - TX_HASH: Transaction hash buffer.
50
49
  * - TRAC_ADDRESS: A bech32m-encoded address (without HRP and separator).
51
50
  *
52
51
  * @param {Buffer} entry - The encoded transaction entry buffer.
52
+ * @param {number} addressLength - The length of the address of the network.
53
53
  * @returns {{ txHash: Buffer, address: Buffer }} An object containing:
54
54
  * - txHash: The transaction hash buffer.
55
55
  * - address: The TRAC address buffer.
56
56
  * or null if the input is invalid.
57
57
  */
58
- export function decode(entry) {
58
+ export function decode(entry, addressLength) {
59
59
  try {
60
- if (!isBufferValid(entry, HASH_BYTE_LENGTH + TRAC_ADDRESS_SIZE)) {
60
+ if (!isBufferValid(entry, HASH_BYTE_LENGTH + addressLength)) {
61
61
  console.error('Invalid transaction entry buffer');
62
- return b4a.alloc(0);
62
+ return null;
63
63
  }
64
64
 
65
65
  const txHash = entry.subarray(0, HASH_BYTE_LENGTH);
66
- const address = entry.subarray(HASH_BYTE_LENGTH, HASH_BYTE_LENGTH + TRAC_ADDRESS_SIZE);
66
+ const address = entry.subarray(HASH_BYTE_LENGTH, HASH_BYTE_LENGTH + addressLength);
67
67
 
68
68
  return { txHash, address };
69
69
  } catch (error) {
70
70
  console.error('Error decoding transaction entry:', error);
71
- return b4a.alloc(0);
71
+ return null;
72
72
  }
73
73
  }
74
74
 
75
- export default { encode, decode };
75
+ export default { encode, decode };
@@ -1,15 +1,6 @@
1
1
  import b4a from 'b4a';
2
+ import { NONCE_BYTE_LENGTH, WRITER_BYTE_LENGTH } from '../../../utils/constants.js';
2
3
 
3
- import { HASH_BYTE_LENGTH, NONCE_BYTE_LENGTH, WRITER_BYTE_LENGTH, TRAC_ADDRESS_SIZE } from '../../../utils/constants.js';
4
- import { blake3Hash } from '../../../utils/crypto.js';
5
- import {safeWriteUInt32BE} from "../../../utils/buffer.js";
6
-
7
- /**
8
- * Total size of a transaction buffer in bytes.
9
- * Format: bootstrap + validator_address + msb_bootstrap + local_address + local_writer_key + content_hash + nonce
10
- * @type {number}
11
- */
12
- export const TRANSACTION_TOTAL_SIZE = 3 * WRITER_BYTE_LENGTH + 2 * TRAC_ADDRESS_SIZE + HASH_BYTE_LENGTH + NONCE_BYTE_LENGTH;
13
4
  export const BOOTSTRAP_DEPLOYMENT_SIZE = WRITER_BYTE_LENGTH + NONCE_BYTE_LENGTH + 4; // 4 bytes for OperationType because it is a UInt32BE
14
5
  export const MAXIMUM_OPERATION_PAYLOAD_SIZE = 4096; // Maximum size of a transaction buffer in bytes
15
6
 
@@ -27,92 +18,9 @@ export const Status = Object.freeze({
27
18
  IGNORE: 2
28
19
  });
29
20
 
30
- // TODO: This function receives too many arguments. It would be better to encapsulate them in an object.
31
- /**
32
- * Generates a transaction buffer and returns its double BLAKE-3 hash.
33
- * @param {Buffer} bootstrap - The bootstrap buffer.
34
- * @param {Buffer} msb_bootstrap - The MSB bootstrap buffer.
35
- * @param {Buffer} validator_address - The validator address buffer.
36
- * @param {Buffer} local_writer_key - The local writer key buffer.
37
- * @param {Buffer} local_address - The local address buffer.
38
- * @param {Buffer} content_hash - The content hash buffer.
39
- * @param {Buffer} nonce - The nonce buffer.
40
- * @returns {Promise<Buffer>} The double BLAKE-3 hash of the transaction buffer, or an empty buffer on error.
41
- */
42
- export async function generateTxBuffer(bootstrap, msb_bootstrap, validator_address, local_writer_key, local_address, content_hash, nonce) {
43
- try {
44
- const tx = b4a.allocUnsafe(TRANSACTION_TOTAL_SIZE);
45
- let offset = 0;
46
-
47
- bootstrap.copy(tx, offset);
48
- offset += bootstrap.length;
49
-
50
- msb_bootstrap.copy(tx, offset);
51
- offset += msb_bootstrap.length;
52
-
53
- validator_address.copy(tx, offset);
54
- offset += validator_address.length;
55
-
56
- local_writer_key.copy(tx, offset);
57
- offset += local_writer_key.length;
58
-
59
- local_address.copy(tx, offset);
60
- offset += local_address.length;
61
-
62
- content_hash.copy(tx, offset);
63
- offset += content_hash.length;
64
-
65
- nonce.copy(tx, offset);
66
- return await blake3Hash(tx)
67
- } catch (error) {
68
- console.error('Error in generateTxBuffer:', error);
69
- return b4a.alloc(0);
70
- }
71
- }
72
-
73
- /**
74
- * Generates a transaction buffer for bootstrap deployment and returns its BLAKE-3 hash.
75
- * The buffer consists of three parts concatenated in the following order:
76
- * 1. bootstrap (32 bytes) - The bootstrap identifier
77
- * 2. incoming_nonce (32 bytes) - Nonce from the requesting node
78
- * 3. operationType (4 bytes) - UInt32BE representing the operation type
79
- *
80
- * Total size: BOOTSTRAP_DEPLOYMENT_SIZE (68 bytes)
81
- *
82
- * @param {Buffer} bootstrap - The bootstrap identifier buffer (32 bytes)
83
- * @param {Buffer} incoming_nonce - The nonce from the requesting node (32 bytes)
84
- * @param {number} operationType - The operation type (should be OperationType.BOOTSTRAP_DEPLOYMENT)
85
- * @returns {Promise<Buffer>} The BLAKE-3 hash of the transaction buffer, or an empty buffer on error
86
- */
87
- export async function generateBootstrapDeploymentTxBuffer(bootstrap, incoming_nonce, operationType) {
88
- try {
89
-
90
- const opTypeBuffer = safeWriteUInt32BE(operationType, 0);
91
- if (opTypeBuffer.length !== 4) {
92
- return b4a.alloc(0);
93
- }
94
- const tx = b4a.alloc(BOOTSTRAP_DEPLOYMENT_SIZE);
95
- let offset = 0;
96
-
97
- bootstrap.copy(tx, offset);
98
- offset += bootstrap.length;
99
-
100
- incoming_nonce.copy(tx, offset);
101
- offset += incoming_nonce.length;
102
-
103
- opTypeBuffer.copy(tx, offset);
104
-
105
- return await blake3Hash(tx)
106
- } catch (error) {
107
- console.error('Error in generateBootstrapDeploymentTxBuffer:', error);
108
- return b4a.alloc(0);
109
- }
110
- }
111
-
112
21
  export default {
113
- generateTxBuffer,
114
- generateBootstrapDeploymentTxBuffer,
115
- TRANSACTION_TOTAL_SIZE,
22
+ Status,
23
+ BOOTSTRAP_DEPLOYMENT_SIZE,
116
24
  MAXIMUM_OPERATION_PAYLOAD_SIZE,
117
25
  FEE
118
26
  };