trac-msb 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/msb.mjs +8 -12
- package/package.json +1 -1
- package/rpc/handlers.mjs +26 -33
- package/rpc/rpc_services.js +126 -0
- package/src/index.js +197 -385
- package/src/utils/cliCommands.js +280 -0
- package/tests/acceptance/v1/broadcast-transaction/broadcast-transaction.test.mjs +6 -9
- package/tests/acceptance/v1/tx/tx.test.mjs +7 -12
- package/tests/acceptance/v1/tx-details/tx-details.test.mjs +14 -22
- package/tests/helpers/transactionPayloads.mjs +78 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { ZERO_WK } from "./buffer.js";
|
|
2
|
+
import { bigIntToDecimalString, bufferToBigInt } from "./amountSerialization.js";
|
|
3
|
+
import { normalizeDecodedPayloadForJson } from "./normalizers.js";
|
|
4
|
+
import { get_confirmed_tx_info, get_unconfirmed_tx_info } from "./cli.js";
|
|
5
|
+
import { EntryType } from "./constants.js";
|
|
6
|
+
import { bufferToAddress } from "../core/state/utils/address.js";
|
|
7
|
+
import deploymentEntryUtils from "../core/state/utils/deploymentEntry.js";
|
|
8
|
+
import { safeDecodeApplyOperation } from "./protobuf/operationHelpers.js";
|
|
9
|
+
|
|
10
|
+
export async function getBalanceCommand(state, address, confirmedFlag) {
|
|
11
|
+
const unconfirmedBalance = confirmedFlag === "false";
|
|
12
|
+
const nodeEntry = unconfirmedBalance
|
|
13
|
+
? await state.getNodeEntryUnsigned(address)
|
|
14
|
+
: await state.getNodeEntry(address);
|
|
15
|
+
|
|
16
|
+
if (nodeEntry) {
|
|
17
|
+
console.log({
|
|
18
|
+
Address: address,
|
|
19
|
+
Balance: bigIntToDecimalString(bufferToBigInt(nodeEntry.balance))
|
|
20
|
+
});
|
|
21
|
+
return {
|
|
22
|
+
address,
|
|
23
|
+
balance: bufferToBigInt(nodeEntry.balance).toString()
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
console.log("Node Entry:", {
|
|
28
|
+
WritingKey: ZERO_WK.toString("hex"),
|
|
29
|
+
IsWhitelisted: false,
|
|
30
|
+
IsWriter: false,
|
|
31
|
+
IsIndexer: false,
|
|
32
|
+
balance: bigIntToDecimalString(0n)
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function nodeStatusCommand(state, address) {
|
|
37
|
+
const nodeEntry = await state.getNodeEntry(address);
|
|
38
|
+
if (nodeEntry) {
|
|
39
|
+
const licenseValue = nodeEntry.license.readUInt32BE(0);
|
|
40
|
+
const licenseDisplay = licenseValue === 0 ? "N/A" : licenseValue.toString();
|
|
41
|
+
console.log("Node Status:", {
|
|
42
|
+
Address: address,
|
|
43
|
+
WritingKey: nodeEntry.wk.toString("hex"),
|
|
44
|
+
IsWhitelisted: nodeEntry.isWhitelisted,
|
|
45
|
+
IsWriter: nodeEntry.isWriter,
|
|
46
|
+
IsIndexer: nodeEntry.isIndexer,
|
|
47
|
+
License: licenseDisplay,
|
|
48
|
+
StakedBalance: bigIntToDecimalString(bufferToBigInt(nodeEntry.stakedBalance)),
|
|
49
|
+
Balance: bigIntToDecimalString(bufferToBigInt(nodeEntry.balance))
|
|
50
|
+
});
|
|
51
|
+
return {
|
|
52
|
+
address,
|
|
53
|
+
writingKey: nodeEntry.wk.toString("hex"),
|
|
54
|
+
isWhitelisted: nodeEntry.isWhitelisted,
|
|
55
|
+
isWriter: nodeEntry.isWriter,
|
|
56
|
+
isIndexer: nodeEntry.isIndexer,
|
|
57
|
+
license: licenseDisplay,
|
|
58
|
+
stakedBalance: bigIntToDecimalString(bufferToBigInt(nodeEntry.stakedBalance))
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
console.log("Node Status:", {
|
|
63
|
+
WritingKey: ZERO_WK.toString("hex"),
|
|
64
|
+
IsWhitelisted: false,
|
|
65
|
+
IsWriter: false,
|
|
66
|
+
IsIndexer: false,
|
|
67
|
+
license: "N/A",
|
|
68
|
+
stakedBalance: "0"
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function coreInfoCommand(state) {
|
|
73
|
+
const admin = await state.getAdminEntry();
|
|
74
|
+
console.log("Admin:", admin ? {
|
|
75
|
+
address: admin.address,
|
|
76
|
+
writingKey: admin.wk.toString("hex")
|
|
77
|
+
} : null);
|
|
78
|
+
const formattedIndexers = await state.getIndexersEntry().then(entry => entry ? entry : null);
|
|
79
|
+
if (!formattedIndexers || (Array.isArray(formattedIndexers) && formattedIndexers.length === 0)) {
|
|
80
|
+
console.log("Indexers: no indexers");
|
|
81
|
+
} else {
|
|
82
|
+
console.log("Indexers:", formattedIndexers);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function getValidatorAddressCommand(state, wkHexString) {
|
|
87
|
+
const payload = await state.getSigned(EntryType.WRITER_ADDRESS + wkHexString);
|
|
88
|
+
if (payload === null) {
|
|
89
|
+
console.log(`No address assigned to the writer key: ${wkHexString}`);
|
|
90
|
+
} else {
|
|
91
|
+
console.log(`Address assigned to the writer key: ${wkHexString} - ${bufferToAddress(payload)}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function getDeploymentCommand(state, bootstrapHex) {
|
|
96
|
+
const deploymentEntry = await state.getRegisteredBootstrapEntry(bootstrapHex);
|
|
97
|
+
console.log(`Searching deployment for bootstrap: ${bootstrapHex}`);
|
|
98
|
+
if (deploymentEntry) {
|
|
99
|
+
const decodedDeploymentEntry = deploymentEntryUtils.decode(deploymentEntry);
|
|
100
|
+
const txhash = decodedDeploymentEntry.txHash.toString("hex");
|
|
101
|
+
console.log(`Bootstrap deployed under transaction hash: ${txhash}`);
|
|
102
|
+
const payload = await state.getSigned(txhash);
|
|
103
|
+
if (payload) {
|
|
104
|
+
const decoded = safeDecodeApplyOperation(payload);
|
|
105
|
+
console.log("Decoded Bootstrap Deployment Payload:", decoded);
|
|
106
|
+
} else {
|
|
107
|
+
console.log(`No payload found for transaction hash: ${txhash}`);
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
console.log(`No deployment found for bootstrap: ${bootstrapHex}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function getTxInfoCommand(state, txHash) {
|
|
115
|
+
const txInfo = await get_confirmed_tx_info(state, txHash);
|
|
116
|
+
if (txInfo) {
|
|
117
|
+
console.log(`Payload for transaction hash ${txHash}:`);
|
|
118
|
+
console.log(txInfo.decoded);
|
|
119
|
+
} else {
|
|
120
|
+
console.log(`No information found for transaction hash: ${txHash}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function getLicenseNumberCommand(state, address) {
|
|
125
|
+
const nodeEntry = await state.getNodeEntry(address);
|
|
126
|
+
if (nodeEntry) {
|
|
127
|
+
console.log({
|
|
128
|
+
Address: address,
|
|
129
|
+
License: bufferToBigInt(nodeEntry.license).toString()
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export async function getLicenseAddressCommand(state, licenseId) {
|
|
135
|
+
if (isNaN(licenseId) || licenseId < 0) {
|
|
136
|
+
console.log("Invalid license ID. Please provide a valid non-negative number.");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const address = await state.getAddressByLicenseId(licenseId);
|
|
141
|
+
if (address) {
|
|
142
|
+
console.log({
|
|
143
|
+
LicenseId: licenseId,
|
|
144
|
+
Address: address
|
|
145
|
+
});
|
|
146
|
+
} else {
|
|
147
|
+
console.log(`No address found for license ID: ${licenseId}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export async function getLicenseCountCommand(state, isAdminFn) {
|
|
152
|
+
const adminEntry = await state.getAdminEntry();
|
|
153
|
+
|
|
154
|
+
if (!adminEntry) {
|
|
155
|
+
throw new Error("Cannot read license count. Admin does not exist");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!isAdminFn(adminEntry)) {
|
|
159
|
+
throw new Error("Cannot perform this operation - you are not the admin!.");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const licenseCount = await state.getLicenseCount();
|
|
163
|
+
console.log({
|
|
164
|
+
LicensesCount: licenseCount
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export async function getTxvCommand(state) {
|
|
169
|
+
const txv = await state.getIndexerSequenceState();
|
|
170
|
+
console.log("Current TXV:", txv.toString("hex"));
|
|
171
|
+
return txv;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function getFeeCommand(state) {
|
|
175
|
+
const fee = state.getFee();
|
|
176
|
+
console.log("Current FEE:", bigIntToDecimalString(bufferToBigInt(fee)));
|
|
177
|
+
return bufferToBigInt(fee).toString();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function getConfirmedLengthCommand(state) {
|
|
181
|
+
const confirmedLength = state.getSignedLength();
|
|
182
|
+
console.log("Confirmed_length:", confirmedLength);
|
|
183
|
+
return confirmedLength;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export function getUnconfirmedLengthCommand(state) {
|
|
187
|
+
const unconfirmedLength = state.getUnsignedLength();
|
|
188
|
+
console.log("Unconfirmed_length:", unconfirmedLength);
|
|
189
|
+
return unconfirmedLength;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export async function getTxPayloadsBulkCommand(state, hashes) {
|
|
193
|
+
if (!Array.isArray(hashes) || hashes.length === 0) {
|
|
194
|
+
throw new Error("Missing hash list.");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (hashes.length > 1500) {
|
|
198
|
+
throw new Error("Length of input tx hashes exceeded.");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const res = { results: [], missing: [] };
|
|
202
|
+
|
|
203
|
+
const promises = hashes.map(hash => get_confirmed_tx_info(state, hash));
|
|
204
|
+
const results = await Promise.all(promises);
|
|
205
|
+
|
|
206
|
+
results.forEach((result, index) => {
|
|
207
|
+
const hash = hashes[index];
|
|
208
|
+
if (result === null || result === undefined) {
|
|
209
|
+
res.missing.push(hash);
|
|
210
|
+
} else {
|
|
211
|
+
const decodedResult = normalizeDecodedPayloadForJson(result.decoded);
|
|
212
|
+
res.results.push({ hash, payload: decodedResult });
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return res;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export async function getTxHashesCommand(state, start, end) {
|
|
220
|
+
try {
|
|
221
|
+
const hashes = await state.confirmedTransactionsBetween(start, end);
|
|
222
|
+
return { hashes };
|
|
223
|
+
} catch (error) {
|
|
224
|
+
throw new Error("Invalid params to perform the request.", error.message);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export async function getTxDetailsCommand(state, hash) {
|
|
229
|
+
try {
|
|
230
|
+
const rawPayload = await get_confirmed_tx_info(state, hash);
|
|
231
|
+
if (!rawPayload) {
|
|
232
|
+
console.log(`No payload found for tx hash: ${hash}`);
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
return normalizeDecodedPayloadForJson(rawPayload.decoded);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
throw new Error("Invalid params to perform the request.", error.message);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export async function getExtendedTxDetailsCommand(state, hash, confirmed) {
|
|
242
|
+
if (confirmed) {
|
|
243
|
+
const rawPayload = await get_confirmed_tx_info(state, hash);
|
|
244
|
+
if (!rawPayload) {
|
|
245
|
+
throw new Error(`No payload found for tx hash: ${hash}`);
|
|
246
|
+
}
|
|
247
|
+
const confirmedLength = await state.getTransactionConfirmedLength(hash);
|
|
248
|
+
if (confirmedLength === null) {
|
|
249
|
+
throw new Error(`No confirmed length found for tx hash: ${hash} in confirmed mode`);
|
|
250
|
+
}
|
|
251
|
+
const normalizedPayload = normalizeDecodedPayloadForJson(rawPayload.decoded, true);
|
|
252
|
+
const fee = state.getFee();
|
|
253
|
+
return {
|
|
254
|
+
txDetails: normalizedPayload,
|
|
255
|
+
confirmed_length: confirmedLength,
|
|
256
|
+
fee: bufferToBigInt(fee).toString()
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const rawPayload = await get_unconfirmed_tx_info(state, hash);
|
|
261
|
+
if (!rawPayload) {
|
|
262
|
+
throw new Error(`No payload found for tx hash: ${hash}`);
|
|
263
|
+
}
|
|
264
|
+
const normalizedPayload = normalizeDecodedPayloadForJson(rawPayload.decoded, true);
|
|
265
|
+
const length = await state.getTransactionConfirmedLength(hash);
|
|
266
|
+
if (length === null) {
|
|
267
|
+
return {
|
|
268
|
+
txDetails: normalizedPayload,
|
|
269
|
+
confirmed_length: 0,
|
|
270
|
+
fee: "0"
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const fee = state.getFee();
|
|
275
|
+
return {
|
|
276
|
+
txDetails: normalizedPayload,
|
|
277
|
+
confirmed_length: length,
|
|
278
|
+
fee: bufferToBigInt(fee).toString()
|
|
279
|
+
};
|
|
280
|
+
}
|
|
@@ -1,21 +1,18 @@
|
|
|
1
1
|
import request from "supertest"
|
|
2
|
-
import tracCrypto from "trac-crypto-api"
|
|
3
2
|
import b4a from "b4a"
|
|
4
3
|
import { $TNK } from "../../../../src/core/state/utils/balance.js"
|
|
4
|
+
import { buildRpcSelfTransferPayload } from "../../../helpers/transactionPayloads.mjs"
|
|
5
5
|
|
|
6
6
|
const toBase64 = (value) => b4a.toString(b4a.from(JSON.stringify(value)), "base64")
|
|
7
7
|
|
|
8
8
|
export const registerBroadcastTransactionTests = (context) => {
|
|
9
9
|
describe("POST /v1/broadcast-transaction", () => {
|
|
10
10
|
it("broadcasts transaction and returns lengths", async () => {
|
|
11
|
-
const
|
|
12
|
-
context.wallet
|
|
13
|
-
context.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
const payload = tracCrypto.transaction.build(txData, b4a.from(context.wallet.secretKey, 'hex'))
|
|
11
|
+
const { payload } = await buildRpcSelfTransferPayload(
|
|
12
|
+
context.wallet,
|
|
13
|
+
context.rpcMsb.state,
|
|
14
|
+
1n
|
|
15
|
+
);
|
|
19
16
|
const res = await request(context.server)
|
|
20
17
|
.post("/v1/broadcast-transaction")
|
|
21
18
|
.set("Accept", "application/json")
|
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
import request from "supertest"
|
|
2
|
-
import
|
|
3
|
-
import b4a from "b4a"
|
|
4
|
-
import { $TNK } from "../../../../src/core/state/utils/balance.js"
|
|
2
|
+
import { buildRpcSelfTransferPayload } from "../../../helpers/transactionPayloads.mjs"
|
|
5
3
|
|
|
6
4
|
export const registerTxTests = (context) => {
|
|
7
5
|
describe("GET /v1/tx/:hash", () => {
|
|
8
6
|
it("returns tx details for a broadcasted transaction", async () => {
|
|
9
|
-
const
|
|
10
|
-
context.wallet
|
|
11
|
-
context.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
const payload = tracCrypto.transaction.build(txData, b4a.from(context.wallet.secretKey, 'hex'))
|
|
7
|
+
const { payload, txHashHex } = await buildRpcSelfTransferPayload(
|
|
8
|
+
context.wallet,
|
|
9
|
+
context.rpcMsb.state,
|
|
10
|
+
1n
|
|
11
|
+
);
|
|
17
12
|
const broadcastRes = await request(context.server)
|
|
18
13
|
.post("/v1/broadcast-transaction")
|
|
19
14
|
.set("Accept", "application/json")
|
|
20
15
|
.send(JSON.stringify({ payload }))
|
|
21
16
|
expect(broadcastRes.statusCode).toBe(200)
|
|
22
17
|
|
|
23
|
-
const res = await request(context.server).get(`/v1/tx/${
|
|
18
|
+
const res = await request(context.server).get(`/v1/tx/${txHashHex}`)
|
|
24
19
|
expect(res.statusCode).toBe(200)
|
|
25
20
|
expect(res.body).toMatchObject({ txDetails: expect.any(Object) })
|
|
26
21
|
})
|
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
import request from "supertest"
|
|
2
|
-
import
|
|
3
|
-
import b4a from "b4a"
|
|
4
|
-
import { $TNK } from "../../../../src/core/state/utils/balance.js"
|
|
2
|
+
import { buildRpcSelfTransferPayload } from "../../../helpers/transactionPayloads.mjs"
|
|
5
3
|
|
|
6
4
|
export const registerTxDetailsTests = (context) => {
|
|
7
5
|
describe("GET /v1/tx/details", () => {
|
|
8
6
|
it("returns 200 for broadcasted hash (confirmed and unconfirmed)", async () => {
|
|
9
|
-
const
|
|
10
|
-
context.wallet
|
|
11
|
-
context.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
const payload = tracCrypto.transaction.build(txData, b4a.from(context.wallet.secretKey, 'hex'))
|
|
7
|
+
const { payload, txHashHex } = await buildRpcSelfTransferPayload(
|
|
8
|
+
context.wallet,
|
|
9
|
+
context.rpcMsb.state,
|
|
10
|
+
1n
|
|
11
|
+
);
|
|
17
12
|
const broadcastRes = await request(context.server)
|
|
18
13
|
.post("/v1/broadcast-transaction")
|
|
19
14
|
.set("Accept", "application/json")
|
|
@@ -21,7 +16,7 @@ export const registerTxDetailsTests = (context) => {
|
|
|
21
16
|
expect(broadcastRes.statusCode).toBe(200)
|
|
22
17
|
|
|
23
18
|
const resConfirmed = await request(context.server)
|
|
24
|
-
.get(`/v1/tx/details/${
|
|
19
|
+
.get(`/v1/tx/details/${txHashHex}?confirmed=true`)
|
|
25
20
|
expect(resConfirmed.statusCode).toBe(200)
|
|
26
21
|
|
|
27
22
|
expect(resConfirmed.body).toMatchObject({
|
|
@@ -31,7 +26,7 @@ export const registerTxDetailsTests = (context) => {
|
|
|
31
26
|
})
|
|
32
27
|
|
|
33
28
|
const resUnconfirmed = await request(context.server)
|
|
34
|
-
.get(`/v1/tx/details/${
|
|
29
|
+
.get(`/v1/tx/details/${txHashHex}?confirmed=false`)
|
|
35
30
|
expect(resUnconfirmed.statusCode).toBe(200)
|
|
36
31
|
|
|
37
32
|
expect(resUnconfirmed.body).toMatchObject({
|
|
@@ -42,14 +37,11 @@ export const registerTxDetailsTests = (context) => {
|
|
|
42
37
|
})
|
|
43
38
|
|
|
44
39
|
it("handles null confirmed_length for unconfirmed transaction", async () => {
|
|
45
|
-
const
|
|
46
|
-
context.wallet
|
|
47
|
-
context.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
const payload = tracCrypto.transaction.build(txData, b4a.from(context.wallet.secretKey, 'hex'))
|
|
40
|
+
const { payload, txHashHex } = await buildRpcSelfTransferPayload(
|
|
41
|
+
context.wallet,
|
|
42
|
+
context.rpcMsb.state,
|
|
43
|
+
1n
|
|
44
|
+
);
|
|
53
45
|
|
|
54
46
|
const originalGetConfirmedLength = context.rpcMsb.state.getTransactionConfirmedLength
|
|
55
47
|
context.rpcMsb.state.getTransactionConfirmedLength = async () => null
|
|
@@ -62,7 +54,7 @@ export const registerTxDetailsTests = (context) => {
|
|
|
62
54
|
expect(broadcastRes.statusCode).toBe(200)
|
|
63
55
|
|
|
64
56
|
const res = await request(context.server)
|
|
65
|
-
.get(`/v1/tx/details/${
|
|
57
|
+
.get(`/v1/tx/details/${txHashHex}?confirmed=false`)
|
|
66
58
|
expect(res.statusCode).toBe(200)
|
|
67
59
|
|
|
68
60
|
expect(res.body).toMatchObject({
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import b4a from "b4a";
|
|
2
|
+
import tracCrypto from "trac-crypto-api";
|
|
3
|
+
|
|
4
|
+
import { $TNK } from "../../src/core/state/utils/balance.js";
|
|
5
|
+
import { createMessage } from "../../src/utils/buffer.js";
|
|
6
|
+
import { blake3Hash } from "../../src/utils/crypto.js";
|
|
7
|
+
import { OperationType, NETWORK_ID } from "../../src/utils/constants.js";
|
|
8
|
+
import { addressToBuffer } from "../../src/core/state/utils/address.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Build a base64-encoded transfer payload and matching tx hash
|
|
12
|
+
* that are compatible with MSB's PartialTransfer validator.
|
|
13
|
+
*
|
|
14
|
+
* This helper mirrors the hashing/signing logic used by
|
|
15
|
+
* PartialOperation.validateSignature, so that tests broadcast
|
|
16
|
+
* transactions the node will accept without touching consensus code.
|
|
17
|
+
*
|
|
18
|
+
* @param {import("trac-wallet").default} wallet - Writer wallet used for signing.
|
|
19
|
+
* @param {import("../../src/core/state/State.js").default} state - MSB state instance.
|
|
20
|
+
* @param {bigint} [amountTnk=1n] - Transfer amount in TNK units.
|
|
21
|
+
* @returns {Promise<{ payload: string, txHashHex: string }>}
|
|
22
|
+
*/
|
|
23
|
+
export async function buildRpcSelfTransferPayload(wallet, state, amountTnk = 1n) {
|
|
24
|
+
const txvBuffer = await state.getIndexerSequenceState();
|
|
25
|
+
const txvHex = b4a.toString(txvBuffer, "hex");
|
|
26
|
+
|
|
27
|
+
const txData = await tracCrypto.transaction.preBuild(
|
|
28
|
+
wallet.address,
|
|
29
|
+
wallet.address,
|
|
30
|
+
b4a.toString($TNK(amountTnk), "hex"),
|
|
31
|
+
txvHex
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const nonceHex = b4a.toString(txData.nonce, "hex");
|
|
35
|
+
const amountHex = txData.amount;
|
|
36
|
+
const toAddress = txData.to;
|
|
37
|
+
|
|
38
|
+
const txvBuf = b4a.from(txData.validity, "hex");
|
|
39
|
+
const nonceBuf = b4a.from(nonceHex, "hex");
|
|
40
|
+
const amountBuf = b4a.from(amountHex, "hex");
|
|
41
|
+
const toBuf = addressToBuffer(toAddress);
|
|
42
|
+
|
|
43
|
+
const message = createMessage(
|
|
44
|
+
NETWORK_ID,
|
|
45
|
+
txvBuf,
|
|
46
|
+
toBuf,
|
|
47
|
+
amountBuf,
|
|
48
|
+
nonceBuf,
|
|
49
|
+
OperationType.TRANSFER
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const messageHash = await blake3Hash(message);
|
|
53
|
+
const signature = wallet.sign(messageHash);
|
|
54
|
+
|
|
55
|
+
const payloadObject = {
|
|
56
|
+
type: OperationType.TRANSFER,
|
|
57
|
+
address: wallet.address,
|
|
58
|
+
tro: {
|
|
59
|
+
tx: b4a.toString(messageHash, "hex"),
|
|
60
|
+
txv: txData.validity,
|
|
61
|
+
in: nonceHex,
|
|
62
|
+
to: toAddress,
|
|
63
|
+
am: amountHex,
|
|
64
|
+
is: b4a.toString(signature, "hex")
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const payload = b4a.toString(
|
|
69
|
+
b4a.from(JSON.stringify(payloadObject)),
|
|
70
|
+
"base64"
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
payload,
|
|
75
|
+
txHashHex: b4a.toString(messageHash, "hex")
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|