-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
transaction_vector.test.ts
226 lines (197 loc) · 6.3 KB
/
transaction_vector.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
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0
/**
* Do fuzzing tests with test vectors. The test vectors are produced by the same code
* used by the Aptos Blockchain. The test vectors are arrays of JSON objects.
* Each JSON object contains randomized inputs to Transaction Builder and BCS and
* the expected outputs.
*/
import path from "path";
import nacl from "tweetnacl";
import fs from "fs";
import { bytesToHex } from "@noble/hashes/utils";
import {
AccountAddress,
ChainId,
RawTransaction,
EntryFunction,
StructTag,
TypeTag,
TypeTagVector,
TransactionPayloadEntryFunction,
Identifier,
TypeTagStruct,
TypeTagAddress,
TypeTagBool,
TypeTagU8,
TypeTagU64,
TypeTagU128,
TypeTagSigner,
Ed25519Signature,
TransactionPayloadScript,
Script,
TransactionArgument,
TransactionArgumentBool,
TransactionArgumentU8,
TransactionArgumentU64,
TransactionArgumentAddress,
TransactionArgumentU8Vector,
TransactionArgumentU128,
} from "./aptos_types";
import { HexString } from "../hex_string";
import { TransactionBuilderEd25519 } from "./builder";
// eslint-disable-next-line operator-linebreak
const VECTOR_FILES_ROOT_DIR =
process.env.VECTOR_FILES_ROOT_DIR || path.resolve(__dirname, "..", "..", "..", "..", "..", "api", "goldens");
const ENTRY_FUNCTION_VECTOR = path.join(
VECTOR_FILES_ROOT_DIR,
"aptos_api__tests__transaction_vector_test__test_entry_function_payload.json",
);
const SCRIPT_VECTOR = path.join(
VECTOR_FILES_ROOT_DIR,
"aptos_api__tests__transaction_vector_test__test_script_payload.json",
);
function parseTypeTag(typeTag: any): TypeTag {
if (typeTag.vector) {
return new TypeTagVector(parseTypeTag(typeTag.vector));
}
if (typeTag.struct) {
const {
address,
module,
name,
// eslint-disable-next-line @typescript-eslint/naming-convention
type_args,
}: {
address: string;
module: string;
name: string;
type_args: any[];
} = typeTag.struct;
const typeArgs = type_args.map((arg) => parseTypeTag(arg));
const structTag = new StructTag(
AccountAddress.fromHex(address),
new Identifier(module),
new Identifier(name),
typeArgs,
);
return new TypeTagStruct(structTag);
}
switch (typeTag) {
case "bool":
return new TypeTagBool();
case "u8":
return new TypeTagU8();
case "u64":
return new TypeTagU64();
case "u128":
return new TypeTagU128();
case "address":
return new TypeTagAddress();
case "signer":
return new TypeTagSigner();
default:
throw new Error("Unknown type tag");
}
}
function parseTransactionArgument(arg: any): TransactionArgument {
const argHasOwnProperty = (propertyName: string) => Object.prototype.hasOwnProperty.call(arg, propertyName);
if (argHasOwnProperty("U8")) {
// arg.U8 is a number
return new TransactionArgumentU8(arg.U8);
}
if (argHasOwnProperty("U64")) {
// arg.U64 is a string literal
return new TransactionArgumentU64(BigInt(arg.U64));
}
if (argHasOwnProperty("U128")) {
// arg.U128 is a string literal
return new TransactionArgumentU128(BigInt(arg.U128));
}
if (argHasOwnProperty("Address")) {
// arg.Address is a hex string
return new TransactionArgumentAddress(AccountAddress.fromHex(arg.Address));
}
if (argHasOwnProperty("U8Vector")) {
// arg.U8Vector is a hex string
return new TransactionArgumentU8Vector(new HexString(arg.U8Vector).toUint8Array());
}
if (argHasOwnProperty("Bool")) {
return new TransactionArgumentBool(arg.Bool);
}
throw new Error("Invalid Transaction Argument");
}
function sign(rawTxn: RawTransaction, privateKey: string): string {
const privateKeyBytes = new HexString(privateKey).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 bytesToHex(txnBuilder.sign(rawTxn));
}
type IRawTxn = {
// hex string for an AccountAddress
sender: string;
// u64 string literal
sequence_number: string;
// u64 string literal
max_gas_amount: string;
// u64 string literal
gas_unit_price: string;
// u64 string literal
expiration_timestamp_secs: string;
chain_id: number;
};
function verify(
raw_txn: IRawTxn,
payload: TransactionPayloadEntryFunction | TransactionPayloadScript,
private_key: string,
expected_output: string,
) {
const rawTxn = new RawTransaction(
AccountAddress.fromHex(raw_txn.sender),
BigInt(raw_txn.sequence_number),
payload,
BigInt(raw_txn.max_gas_amount),
BigInt(raw_txn.gas_unit_price),
BigInt(raw_txn.expiration_timestamp_secs),
new ChainId(raw_txn.chain_id),
);
const signedTxn = sign(rawTxn, private_key);
expect(signedTxn).toBe(expected_output);
}
describe("Transaction builder vector test", () => {
it("should pass on entry function payload", () => {
const vector: any[] = JSON.parse(fs.readFileSync(ENTRY_FUNCTION_VECTOR, "utf8"));
vector.forEach(({ raw_txn, signed_txn_bcs, private_key }) => {
const payload = raw_txn.payload.EntryFunction;
const entryFunctionPayload = new TransactionPayloadEntryFunction(
EntryFunction.natural(
`${payload.module.address}::${payload.module.name}`,
payload.function,
payload.ty_args.map((tag: any) => parseTypeTag(tag)),
payload.args.map((arg: any) => new HexString(arg).toUint8Array()),
),
);
verify(raw_txn, entryFunctionPayload, private_key, signed_txn_bcs);
});
});
it("should pass on script payload", () => {
const vector: any[] = JSON.parse(fs.readFileSync(SCRIPT_VECTOR, "utf8"));
vector.forEach(({ raw_txn, signed_txn_bcs, private_key }) => {
const payload = raw_txn.payload.Script;
// payload.code is hex string
const code = new HexString(payload.code).toUint8Array();
const scriptPayload = new TransactionPayloadScript(
new Script(
code,
payload.ty_args.map((tag: any) => parseTypeTag(tag)),
payload.args.map((arg: any) => parseTransactionArgument(arg)),
),
);
verify(raw_txn, scriptPayload, private_key, signed_txn_bcs);
});
});
});