-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
transaction_builder.test.ts
234 lines (188 loc) · 9.89 KB
/
transaction_builder.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0
/* eslint-disable max-len */
import nacl from "tweetnacl";
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
import { bcsSerializeUint64, bcsToBytes, Bytes } from "./bcs";
import { HexString } from "../hex_string";
import { TransactionBuilderEd25519, TransactionBuilder } from "./index";
import {
AccountAddress,
ChainId,
Ed25519Signature,
RawTransaction,
Script,
EntryFunction,
StructTag,
TransactionArgumentAddress,
TransactionArgumentU8,
TransactionArgumentU8Vector,
TransactionPayloadScript,
TransactionPayloadEntryFunction,
TypeTagStruct,
} from "./aptos_types";
const ADDRESS_1 = "0x1222";
const ADDRESS_2 = "0xdd";
const ADDRESS_3 = "0x0a550c18";
const ADDRESS_4 = "0x01";
const PRIVATE_KEY = "9bf49a6a0755f953811fce125f2683d50429c3bb49e074147e0089a52eae155f";
const TXN_EXPIRE = "18446744073709551615";
function hexSignedTxn(signedTxn: Uint8Array): string {
return bytesToHex(signedTxn);
}
function sign(rawTxn: RawTransaction): Bytes {
const privateKeyBytes = new HexString(PRIVATE_KEY).toUint8Array();
const signingKey = nacl.sign.keyPair.fromSeed(privateKeyBytes.slice(0, 32));
const { publicKey } = signingKey;
const txnBuilder = new TransactionBuilderEd25519(
(signingMessage) => new Ed25519Signature(nacl.sign(signingMessage, signingKey.secretKey).slice(0, 64)),
publicKey,
);
return txnBuilder.sign(rawTxn);
}
test("throws when preparing signing message with invalid payload", () => {
expect(() => {
// @ts-ignore
TransactionBuilder.getSigningMessage("invalid");
}).toThrow("Unknown transaction type.");
});
test("serialize entry function payload with no type args", () => {
const entryFunctionPayload = new TransactionPayloadEntryFunction(
EntryFunction.natural(
`${ADDRESS_1}::aptos_coin`,
"transfer",
[],
[bcsToBytes(AccountAddress.fromHex(ADDRESS_2)), bcsSerializeUint64(1)],
),
);
const rawTxn = new RawTransaction(
AccountAddress.fromHex(new HexString(ADDRESS_3)),
BigInt(0),
entryFunctionPayload,
BigInt(2000),
BigInt(0),
BigInt(TXN_EXPIRE),
new ChainId(4),
);
const signedTxn = sign(rawTxn);
expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000200000000000000000000000000000000000000000000000000000000000012220a6170746f735f636f696e087472616e7366657200022000000000000000000000000000000000000000000000000000000000000000dd080100000000000000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a49200409c570996380897f38b8d7008d726fb45d6ded0689216e56b73f523492cba92deb6671c27e9a44d2a6fdfdb497420d00c621297a23d6d0298895e0d58cff6060c",
);
});
test("serialize entry function payload with type args", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));
const entryFunctionPayload = new TransactionPayloadEntryFunction(
EntryFunction.natural(
`${ADDRESS_1}::coin`,
"transfer",
[token],
[bcsToBytes(AccountAddress.fromHex(ADDRESS_2)), bcsSerializeUint64(1)],
),
);
const rawTxn = new RawTransaction(
AccountAddress.fromHex(ADDRESS_3),
BigInt(0),
entryFunctionPayload,
BigInt(2000),
BigInt(0),
BigInt(TXN_EXPIRE),
new ChainId(4),
);
const signedTxn = sign(rawTxn);
expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c18000000000000000002000000000000000000000000000000000000000000000000000000000000122204636f696e087472616e73666572010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e00022000000000000000000000000000000000000000000000000000000000000000dd080100000000000000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040112162f543ca92b4f14c1b09b7f52894a127f5428b0d407c09c8efb3a136cff50e550aea7da1226f02571d79230b80bd79096ea0d796789ad594b8fbde695404",
);
});
test("serialize entry function payload with type args but no function args", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));
const entryFunctionPayload = new TransactionPayloadEntryFunction(
EntryFunction.natural(`${ADDRESS_1}::coin`, "fake_func", [token], []),
);
const rawTxn = new RawTransaction(
AccountAddress.fromHex(ADDRESS_3),
BigInt(0),
entryFunctionPayload,
BigInt(2000),
BigInt(0),
BigInt(TXN_EXPIRE),
new ChainId(4),
);
const signedTxn = sign(rawTxn);
expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c18000000000000000002000000000000000000000000000000000000000000000000000000000000122204636f696e0966616b655f66756e63010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e0000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a49200400e2d1cc4a27893cbae36d8b6a7150977c7620e065f359840413c5478a25f20a383250a9cdcb4fd71f7d171856f38972da30a9d10072e164614d96379004aa500",
);
});
test("serialize script payload with no type args and no function args", () => {
const script = hexToBytes("a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102");
const scriptPayload = new TransactionPayloadScript(new Script(script, [], []));
const rawTxn = new RawTransaction(
AccountAddress.fromHex(ADDRESS_3),
BigInt(0),
scriptPayload,
BigInt(2000),
BigInt(0),
BigInt(TXN_EXPIRE),
new ChainId(4),
);
const signedTxn = sign(rawTxn);
expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000026a11ceb0b030000000105000100000000050601000000000000000600000000000000001a01020000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040266935990105df40f3a82a3f41ad9ceb4b79451495403dd976191382bb07f8c9b401702968a64b5176762e62036f75c6fc2b770a0988716e41d469fff2349a08",
);
});
test("serialize script payload with type args but no function args", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));
const script = hexToBytes("a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102");
const scriptPayload = new TransactionPayloadScript(new Script(script, [token], []));
const rawTxn = new RawTransaction(
AccountAddress.fromHex(ADDRESS_3),
BigInt(0),
scriptPayload,
BigInt(2000),
BigInt(0),
BigInt(TXN_EXPIRE),
new ChainId(4),
);
const signedTxn = sign(rawTxn);
expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000026a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e0000d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a4920040bd241a6f31dfdfca0031ca5874fbf81800b5f632642321a11c41b4fead4b41d808617e91dd655fde7e9f263127f07bb5d56c7c925fe797728dcc9b55be120604",
);
});
test("serialize script payload with type arg and function arg", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));
const argU8 = new TransactionArgumentU8(2);
const script = hexToBytes("a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102");
const scriptPayload = new TransactionPayloadScript(new Script(script, [token], [argU8]));
const rawTxn = new RawTransaction(
AccountAddress.fromHex(ADDRESS_3),
BigInt(0),
scriptPayload,
BigInt(2000),
BigInt(0),
BigInt(TXN_EXPIRE),
new ChainId(4),
);
const signedTxn = sign(rawTxn);
expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000026a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e00010002d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a49200409936b8d22cec685e720761f6c6135e020911f1a26e220e2a0f3317f5a68942531987259ac9e8688158c77df3e7136637056047d9524edad88ee45d61a9346602",
);
});
test("serialize script payload with one type arg and two function args", () => {
const token = new TypeTagStruct(StructTag.fromString(`${ADDRESS_4}::aptos_coin::AptosCoin`));
const argU8Vec = new TransactionArgumentU8Vector(bcsSerializeUint64(1));
const argAddress = new TransactionArgumentAddress(AccountAddress.fromHex("0x01"));
const script = hexToBytes("a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102");
const scriptPayload = new TransactionPayloadScript(new Script(script, [token], [argU8Vec, argAddress]));
const rawTxn = new RawTransaction(
AccountAddress.fromHex(ADDRESS_3),
BigInt(0),
scriptPayload,
BigInt(2000),
BigInt(0),
BigInt(TXN_EXPIRE),
new ChainId(4),
);
const signedTxn = sign(rawTxn);
expect(hexSignedTxn(signedTxn)).toBe(
"000000000000000000000000000000000000000000000000000000000a550c1800000000000000000026a11ceb0b030000000105000100000000050601000000000000000600000000000000001a0102010700000000000000000000000000000000000000000000000000000000000000010a6170746f735f636f696e094170746f73436f696e000204080100000000000000030000000000000000000000000000000000000000000000000000000000000001d0070000000000000000000000000000ffffffffffffffff040020b9c6ee1630ef3e711144a648db06bbb2284f7274cfbee53ffcee503cc1a492004055c7499795ea68d7acfa64a58f19efa2ba3b977fa58ae93ae8c0732c0f6d6dd084d92bbe4edc2a0d687031cae90da117abfac16ebd902e764bdc38a2154a2102",
);
});