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
@@ -0,0 +1,516 @@
1
+ import { OperationType } from '../../utils/constants.js';
2
+
3
+ /**
4
+ * Director that orchestrates ApplyStateMessageBuilder for partial and complete messages.
5
+ */
6
+ class ApplyStateMessageDirector {
7
+ #builder;
8
+
9
+ /**
10
+ * @param {ApplyStateMessageBuilder} builderInstance
11
+ */
12
+ constructor(builderInstance) {
13
+ this.#builder = builderInstance;
14
+ }
15
+
16
+ /**
17
+ * Build a partial add writer payload.
18
+ * @param {string|Buffer} invokerAddress
19
+ * @param {string|Buffer} writingKey
20
+ * @param {string|Buffer} txValidity
21
+ * @param {'json'|'buffer'} output
22
+ * @returns {Promise<object>}
23
+ */
24
+ async buildPartialAddWriterMessage(invokerAddress, writingKey, txValidity, output) {
25
+ if (!this.#builder) throw new Error('Builder has not been set.');
26
+ await this.#builder
27
+ .setPhase('partial')
28
+ .setOutput(output)
29
+ .setOperationType(OperationType.ADD_WRITER)
30
+ .setAddress(invokerAddress)
31
+ .setTxValidity(txValidity)
32
+ .setWriterKey(writingKey)
33
+ .build();
34
+ return this.#builder.getPayload();
35
+ }
36
+
37
+ /**
38
+ * Build a partial remove writer payload.
39
+ * @param {string|Buffer} invokerAddress
40
+ * @param {string|Buffer} writerKey
41
+ * @param {string|Buffer} txValidity
42
+ * @param {'json'|'buffer'} output
43
+ * @returns {Promise<object>}
44
+ */
45
+ async buildPartialRemoveWriterMessage(invokerAddress, writerKey, txValidity, output) {
46
+ if (!this.#builder) throw new Error('Builder has not been set.');
47
+ await this.#builder
48
+ .setPhase('partial')
49
+ .setOutput(output)
50
+ .setOperationType(OperationType.REMOVE_WRITER)
51
+ .setAddress(invokerAddress)
52
+ .setTxValidity(txValidity)
53
+ .setWriterKey(writerKey)
54
+ .build();
55
+ return this.#builder.getPayload();
56
+ }
57
+
58
+ /**
59
+ * Build a partial admin recovery payload.
60
+ * @param {string|Buffer} invokerAddress
61
+ * @param {string|Buffer} writingKey
62
+ * @param {string|Buffer} txValidity
63
+ * @param {'json'|'buffer'} output
64
+ * @returns {Promise<object>}
65
+ */
66
+ async buildPartialAdminRecoveryMessage(invokerAddress, writingKey, txValidity, output) {
67
+ if (!this.#builder) throw new Error('Builder has not been set.');
68
+ await this.#builder
69
+ .setPhase('partial')
70
+ .setOutput(output)
71
+ .setOperationType(OperationType.ADMIN_RECOVERY)
72
+ .setAddress(invokerAddress)
73
+ .setTxValidity(txValidity)
74
+ .setWriterKey(writingKey)
75
+ .build();
76
+ return this.#builder.getPayload();
77
+ }
78
+
79
+ /**
80
+ * Build a partial transaction payload.
81
+ * @param {string|Buffer} invokerAddress
82
+ * @param {string|Buffer} incomingWritingKey
83
+ * @param {string|Buffer} txValidity
84
+ * @param {string|Buffer} contentHash
85
+ * @param {string|Buffer} externalBootstrap
86
+ * @param {string|Buffer} msbBootstrap
87
+ * @param {'json'|'buffer'} output
88
+ * @returns {Promise<object>}
89
+ */
90
+ async buildPartialTransactionOperationMessage(
91
+ invokerAddress,
92
+ incomingWritingKey,
93
+ txValidity,
94
+ contentHash,
95
+ externalBootstrap,
96
+ msbBootstrap,
97
+ output
98
+ ) {
99
+ if (!this.#builder) throw new Error('Builder has not been set.');
100
+ await this.#builder
101
+ .setPhase('partial')
102
+ .setOutput(output)
103
+ .setOperationType(OperationType.TX)
104
+ .setAddress(invokerAddress)
105
+ .setTxValidity(txValidity)
106
+ .setWriterKey(incomingWritingKey)
107
+ .setContentHash(contentHash)
108
+ .setExternalBootstrap(externalBootstrap)
109
+ .setMsbBootstrap(msbBootstrap)
110
+ .build();
111
+ return this.#builder.getPayload();
112
+ }
113
+
114
+ /**
115
+ * Build a partial bootstrap deployment payload.
116
+ * @param {string|Buffer} invokerAddress
117
+ * @param {string|Buffer} bootstrap
118
+ * @param {string|Buffer} channel
119
+ * @param {string|Buffer} txValidity
120
+ * @param {'json'|'buffer'} output
121
+ * @returns {Promise<object>}
122
+ */
123
+ async buildPartialBootstrapDeploymentMessage(invokerAddress, bootstrap, channel, txValidity, output) {
124
+ if (!this.#builder) throw new Error('Builder has not been set.');
125
+ await this.#builder
126
+ .setPhase('partial')
127
+ .setOutput(output)
128
+ .setOperationType(OperationType.BOOTSTRAP_DEPLOYMENT)
129
+ .setAddress(invokerAddress)
130
+ .setTxValidity(txValidity)
131
+ .setExternalBootstrap(bootstrap)
132
+ .setChannel(channel)
133
+ .build();
134
+ return this.#builder.getPayload();
135
+ }
136
+
137
+ /**
138
+ * Build a partial transfer payload.
139
+ * @param {string|Buffer} invokerAddress
140
+ * @param {string|Buffer} recipientAddress
141
+ * @param {string|Buffer} amount
142
+ * @param {string|Buffer} txValidity
143
+ * @param {'json'|'buffer'} output
144
+ * @returns {Promise<object>}
145
+ */
146
+ async buildPartialTransferOperationMessage(invokerAddress, recipientAddress, amount, txValidity, output) {
147
+ if (!this.#builder) throw new Error('Builder has not been set.');
148
+ await this.#builder
149
+ .setPhase('partial')
150
+ .setOutput(output)
151
+ .setOperationType(OperationType.TRANSFER)
152
+ .setAddress(invokerAddress)
153
+ .setTxValidity(txValidity)
154
+ .setIncomingAddress(recipientAddress)
155
+ .setAmount(amount)
156
+ .build();
157
+ return this.#builder.getPayload();
158
+ }
159
+
160
+ /**
161
+ * Build a complete add admin payload.
162
+ * @param {string|Buffer} invokerAddress
163
+ * @param {string|Buffer} writingKey
164
+ * @param {string|Buffer} txValidity
165
+ * @returns {Promise<object>}
166
+ */
167
+ async buildCompleteAddAdminMessage(invokerAddress, writingKey, txValidity) {
168
+ if (!this.#builder) throw new Error('Builder has not been set.');
169
+ await this.#builder
170
+ .setPhase('complete')
171
+ .setOutput('buffer')
172
+ .setOperationType(OperationType.ADD_ADMIN)
173
+ .setAddress(invokerAddress)
174
+ .setWriterKey(writingKey)
175
+ .setTxValidity(txValidity)
176
+ .build();
177
+ return this.#builder.getPayload();
178
+ }
179
+
180
+ /**
181
+ * Build a complete disable initialization payload.
182
+ * @param {string|Buffer} invokerAddress
183
+ * @param {string|Buffer} writingKey
184
+ * @param {string|Buffer} txValidity
185
+ * @returns {Promise<object>}
186
+ */
187
+ async buildCompleteDisableInitializationMessage(invokerAddress, writingKey, txValidity) {
188
+ if (!this.#builder) throw new Error('Builder has not been set.');
189
+ await this.#builder
190
+ .setPhase('complete')
191
+ .setOutput('buffer')
192
+ .setOperationType(OperationType.DISABLE_INITIALIZATION)
193
+ .setAddress(invokerAddress)
194
+ .setWriterKey(writingKey)
195
+ .setTxValidity(txValidity)
196
+ .build();
197
+ return this.#builder.getPayload();
198
+ }
199
+
200
+ /**
201
+ * Build a complete balance initialization payload.
202
+ * @param {string|Buffer} invokerAddress
203
+ * @param {string|Buffer} recipientAddress
204
+ * @param {string|Buffer} amount
205
+ * @param {string|Buffer} txValidity
206
+ * @returns {Promise<object>}
207
+ */
208
+ async buildCompleteBalanceInitializationMessage(invokerAddress, recipientAddress, amount, txValidity) {
209
+ if (!this.#builder) throw new Error('Builder has not been set.');
210
+ await this.#builder
211
+ .setPhase('complete')
212
+ .setOutput('buffer')
213
+ .setOperationType(OperationType.BALANCE_INITIALIZATION)
214
+ .setAddress(invokerAddress)
215
+ .setIncomingAddress(recipientAddress)
216
+ .setAmount(amount)
217
+ .setTxValidity(txValidity)
218
+ .build();
219
+ return this.#builder.getPayload();
220
+ }
221
+
222
+ /**
223
+ * Build a complete append whitelist payload.
224
+ * @param {string|Buffer} invokerAddress
225
+ * @param {string|Buffer} incomingAddress
226
+ * @param {string|Buffer} txValidity
227
+ * @returns {Promise<object>}
228
+ */
229
+ async buildCompleteAppendWhitelistMessage(invokerAddress, incomingAddress, txValidity) {
230
+ if (!this.#builder) throw new Error('Builder has not been set.');
231
+ await this.#builder
232
+ .setPhase('complete')
233
+ .setOutput('buffer')
234
+ .setOperationType(OperationType.APPEND_WHITELIST)
235
+ .setAddress(invokerAddress)
236
+ .setTxValidity(txValidity)
237
+ .setIncomingAddress(incomingAddress)
238
+ .build();
239
+ return this.#builder.getPayload();
240
+ }
241
+
242
+ /**
243
+ * Build a complete add writer payload.
244
+ * @param {string|Buffer} invokerAddress
245
+ * @param {string|Buffer} txHash
246
+ * @param {string|Buffer} txValidity
247
+ * @param {string|Buffer} incomingWritingKey
248
+ * @param {string|Buffer} incomingNonce
249
+ * @param {string|Buffer} incomingSignature
250
+ * @returns {Promise<object>}
251
+ */
252
+ async buildCompleteAddWriterMessage(
253
+ invokerAddress,
254
+ txHash,
255
+ txValidity,
256
+ incomingWritingKey,
257
+ incomingNonce,
258
+ incomingSignature
259
+ ) {
260
+ if (!this.#builder) throw new Error('Builder has not been set.');
261
+ await this.#builder
262
+ .setPhase('complete')
263
+ .setOutput('buffer')
264
+ .setOperationType(OperationType.ADD_WRITER)
265
+ .setAddress(invokerAddress)
266
+ .setTxHash(txHash)
267
+ .setTxValidity(txValidity)
268
+ .setIncomingWriterKey(incomingWritingKey)
269
+ .setIncomingNonce(incomingNonce)
270
+ .setIncomingSignature(incomingSignature)
271
+ .build();
272
+ return this.#builder.getPayload();
273
+ }
274
+
275
+ /**
276
+ * Build a complete remove writer payload.
277
+ * @param {string|Buffer} invokerAddress
278
+ * @param {string|Buffer} txHash
279
+ * @param {string|Buffer} txValidity
280
+ * @param {string|Buffer} incomingWritingKey
281
+ * @param {string|Buffer} incomingNonce
282
+ * @param {string|Buffer} incomingSignature
283
+ * @returns {Promise<object>}
284
+ */
285
+ async buildCompleteRemoveWriterMessage(
286
+ invokerAddress,
287
+ txHash,
288
+ txValidity,
289
+ incomingWritingKey,
290
+ incomingNonce,
291
+ incomingSignature
292
+ ) {
293
+ if (!this.#builder) throw new Error('Builder has not been set.');
294
+ await this.#builder
295
+ .setPhase('complete')
296
+ .setOutput('buffer')
297
+ .setOperationType(OperationType.REMOVE_WRITER)
298
+ .setAddress(invokerAddress)
299
+ .setTxHash(txHash)
300
+ .setTxValidity(txValidity)
301
+ .setIncomingWriterKey(incomingWritingKey)
302
+ .setIncomingNonce(incomingNonce)
303
+ .setIncomingSignature(incomingSignature)
304
+ .build();
305
+ return this.#builder.getPayload();
306
+ }
307
+
308
+ /**
309
+ * Build a complete admin recovery payload.
310
+ * @param {string|Buffer} invokerAddress
311
+ * @param {string|Buffer} txHash
312
+ * @param {string|Buffer} txValidity
313
+ * @param {string|Buffer} incomingWritingKey
314
+ * @param {string|Buffer} incomingNonce
315
+ * @param {string|Buffer} incomingSignature
316
+ * @returns {Promise<object>}
317
+ */
318
+ async buildCompleteAdminRecoveryMessage(
319
+ invokerAddress,
320
+ txHash,
321
+ txValidity,
322
+ incomingWritingKey,
323
+ incomingNonce,
324
+ incomingSignature
325
+ ) {
326
+ if (!this.#builder) throw new Error('Builder has not been set.');
327
+ await this.#builder
328
+ .setPhase('complete')
329
+ .setOutput('buffer')
330
+ .setOperationType(OperationType.ADMIN_RECOVERY)
331
+ .setAddress(invokerAddress)
332
+ .setTxHash(txHash)
333
+ .setTxValidity(txValidity)
334
+ .setIncomingWriterKey(incomingWritingKey)
335
+ .setIncomingNonce(incomingNonce)
336
+ .setIncomingSignature(incomingSignature)
337
+ .build();
338
+ return this.#builder.getPayload();
339
+ }
340
+
341
+ /**
342
+ * Build a complete add indexer payload.
343
+ * @param {string|Buffer} invokerAddress
344
+ * @param {string|Buffer} incomingAddress
345
+ * @param {string|Buffer} txValidity
346
+ * @returns {Promise<object>}
347
+ */
348
+ async buildCompleteAddIndexerMessage(invokerAddress, incomingAddress, txValidity) {
349
+ if (!this.#builder) throw new Error('Builder has not been set.');
350
+ await this.#builder
351
+ .setPhase('complete')
352
+ .setOutput('buffer')
353
+ .setOperationType(OperationType.ADD_INDEXER)
354
+ .setAddress(invokerAddress)
355
+ .setTxValidity(txValidity)
356
+ .setIncomingAddress(incomingAddress)
357
+ .build();
358
+ return this.#builder.getPayload();
359
+ }
360
+
361
+ /**
362
+ * Build a complete remove indexer payload.
363
+ * @param {string|Buffer} invokerAddress
364
+ * @param {string|Buffer} incomingAddress
365
+ * @param {string|Buffer} txValidity
366
+ * @returns {Promise<object>}
367
+ */
368
+ async buildCompleteRemoveIndexerMessage(invokerAddress, incomingAddress, txValidity) {
369
+ if (!this.#builder) throw new Error('Builder has not been set.');
370
+ await this.#builder
371
+ .setPhase('complete')
372
+ .setOutput('buffer')
373
+ .setOperationType(OperationType.REMOVE_INDEXER)
374
+ .setAddress(invokerAddress)
375
+ .setTxValidity(txValidity)
376
+ .setIncomingAddress(incomingAddress)
377
+ .build();
378
+ return this.#builder.getPayload();
379
+ }
380
+
381
+ /**
382
+ * Build a complete ban validator payload.
383
+ * @param {string|Buffer} invokerAddress
384
+ * @param {string|Buffer} incomingAddress
385
+ * @param {string|Buffer} txValidity
386
+ * @returns {Promise<object>}
387
+ */
388
+ async buildCompleteBanWriterMessage(invokerAddress, incomingAddress, txValidity) {
389
+ if (!this.#builder) throw new Error('Builder has not been set.');
390
+ await this.#builder
391
+ .setPhase('complete')
392
+ .setOutput('buffer')
393
+ .setOperationType(OperationType.BAN_VALIDATOR)
394
+ .setAddress(invokerAddress)
395
+ .setTxValidity(txValidity)
396
+ .setIncomingAddress(incomingAddress)
397
+ .build();
398
+ return this.#builder.getPayload();
399
+ }
400
+
401
+ /**
402
+ * Build a complete transaction payload.
403
+ * @param {string|Buffer} invokerAddress
404
+ * @param {string|Buffer} txHash
405
+ * @param {string|Buffer} txValidity
406
+ * @param {string|Buffer} incomingWriterKey
407
+ * @param {string|Buffer} incomingNonce
408
+ * @param {string|Buffer} contentHash
409
+ * @param {string|Buffer} incomingSignature
410
+ * @param {string|Buffer} externalBootstrap
411
+ * @param {string|Buffer} msbBootstrap
412
+ * @returns {Promise<object>}
413
+ */
414
+ async buildCompleteTransactionOperationMessage(
415
+ invokerAddress,
416
+ txHash,
417
+ txValidity,
418
+ incomingWriterKey,
419
+ incomingNonce,
420
+ contentHash,
421
+ incomingSignature,
422
+ externalBootstrap,
423
+ msbBootstrap
424
+ ) {
425
+ if (!this.#builder) throw new Error('Builder has not been set.');
426
+ await this.#builder
427
+ .setPhase('complete')
428
+ .setOutput('buffer')
429
+ .setOperationType(OperationType.TX)
430
+ .setAddress(invokerAddress)
431
+ .setTxHash(txHash)
432
+ .setTxValidity(txValidity)
433
+ .setIncomingWriterKey(incomingWriterKey)
434
+ .setIncomingNonce(incomingNonce)
435
+ .setContentHash(contentHash)
436
+ .setIncomingSignature(incomingSignature)
437
+ .setExternalBootstrap(externalBootstrap)
438
+ .setMsbBootstrap(msbBootstrap)
439
+ .build();
440
+ return this.#builder.getPayload();
441
+ }
442
+
443
+ /**
444
+ * Build a complete bootstrap deployment payload.
445
+ * @param {string|Buffer} invokerAddress
446
+ * @param {string|Buffer} transactionHash
447
+ * @param {string|Buffer} txValidity
448
+ * @param {string|Buffer} externalBootstrap
449
+ * @param {string|Buffer} channel
450
+ * @param {string|Buffer} incomingNonce
451
+ * @param {string|Buffer} incomingSignature
452
+ * @returns {Promise<object>}
453
+ */
454
+ async buildCompleteBootstrapDeploymentMessage(
455
+ invokerAddress,
456
+ transactionHash,
457
+ txValidity,
458
+ externalBootstrap,
459
+ channel,
460
+ incomingNonce,
461
+ incomingSignature
462
+ ) {
463
+ if (!this.#builder) throw new Error('Builder has not been set.');
464
+ await this.#builder
465
+ .setPhase('complete')
466
+ .setOutput('buffer')
467
+ .setOperationType(OperationType.BOOTSTRAP_DEPLOYMENT)
468
+ .setAddress(invokerAddress)
469
+ .setTxHash(transactionHash)
470
+ .setTxValidity(txValidity)
471
+ .setExternalBootstrap(externalBootstrap)
472
+ .setChannel(channel)
473
+ .setIncomingNonce(incomingNonce)
474
+ .setIncomingSignature(incomingSignature)
475
+ .build();
476
+ return this.#builder.getPayload();
477
+ }
478
+
479
+ /**
480
+ * Build a complete transfer payload.
481
+ * @param {string|Buffer} invokerAddress
482
+ * @param {string|Buffer} transactionHash
483
+ * @param {string|Buffer} txValidity
484
+ * @param {string|Buffer} incomingNonce
485
+ * @param {string|Buffer} recipientAddress
486
+ * @param {string|Buffer} amount
487
+ * @param {string|Buffer} incomingSignature
488
+ * @returns {Promise<object>}
489
+ */
490
+ async buildCompleteTransferOperationMessage(
491
+ invokerAddress,
492
+ transactionHash,
493
+ txValidity,
494
+ incomingNonce,
495
+ recipientAddress,
496
+ amount,
497
+ incomingSignature
498
+ ) {
499
+ if (!this.#builder) throw new Error('Builder has not been set.');
500
+ await this.#builder
501
+ .setPhase('complete')
502
+ .setOutput('buffer')
503
+ .setOperationType(OperationType.TRANSFER)
504
+ .setAddress(invokerAddress)
505
+ .setTxHash(transactionHash)
506
+ .setTxValidity(txValidity)
507
+ .setIncomingNonce(incomingNonce)
508
+ .setIncomingAddress(recipientAddress)
509
+ .setAmount(amount)
510
+ .setIncomingSignature(incomingSignature)
511
+ .build();
512
+ return this.#builder.getPayload();
513
+ }
514
+ }
515
+
516
+ export default ApplyStateMessageDirector;
@@ -0,0 +1,12 @@
1
+ import ApplyStateMessageDirector from "./ApplyStateMessageDirector.js";
2
+ import ApplyStateMessageBuilder from "./ApplyStateMessageBuilder.js";
3
+
4
+ /**
5
+ * Factory helper to create a director with a builder instance.
6
+ * @param {PeerWallet} wallet
7
+ * @param {object} config
8
+ * @returns {ApplyStateMessageDirector}
9
+ */
10
+ export const applyStateMessageFactory = (wallet, config) =>{
11
+ return new ApplyStateMessageDirector(new ApplyStateMessageBuilder(wallet, config))
12
+ }
@@ -59,4 +59,56 @@ export function deepCopyBuffer(buffer) {
59
59
  const copy = b4a.alloc(buffer.length);
60
60
  buffer.copy(copy);
61
61
  return copy;
62
- }
62
+ }
63
+
64
+ function uint64ToBuffer(value, fieldName) {
65
+ if (typeof value === 'number') {
66
+ if (!Number.isSafeInteger(value) || value < 0) {
67
+ throw new Error(`${fieldName} must be a non-negative safe integer`);
68
+ }
69
+ value = BigInt(value);
70
+ } else if (typeof value !== 'bigint') {
71
+ throw new Error(`${fieldName} must be a number or bigint`);
72
+ }
73
+ if (value < 0n) {
74
+ throw new Error(`${fieldName} must be a non-negative integer`);
75
+ }
76
+
77
+ const buf = b4a.alloc(8);
78
+ buf.writeBigUInt64BE(value);
79
+ return buf;
80
+ }
81
+
82
+ export function timestampToBuffer(timestamp) {
83
+ return uint64ToBuffer(timestamp, 'timestamp');
84
+ }
85
+
86
+ export function idToBuffer(id) {
87
+ if (typeof id !== 'string') {
88
+ throw new Error('id must be a string');
89
+ }
90
+ return b4a.from(id, 'utf8');
91
+ }
92
+
93
+
94
+ export function encodeCapabilities(capabilities) {
95
+ if (!Array.isArray(capabilities)) {
96
+ throw new Error('Capabilities must be an array');
97
+ }
98
+ const validCapabilities = capabilities.map((capability) => {
99
+ if (typeof capability !== 'string') {
100
+ throw new Error('Capabilities array must contain only strings');
101
+ }
102
+ return capability;
103
+ });
104
+
105
+ const parts = [];
106
+ for (const capability of validCapabilities.slice().sort()) {
107
+ const capabilityBuffer = b4a.from(capability, 'utf8');
108
+ const bufferLen = b4a.allocUnsafe(2);
109
+ bufferLen.writeUInt16BE(capabilityBuffer.length, 0);
110
+ parts.push(bufferLen, capabilityBuffer);
111
+ }
112
+
113
+ return parts.length ? b4a.concat(parts) : b4a.alloc(0);
114
+ }