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