forked from matter-labs/era-system-contracts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDefaultAccount.spec.ts
399 lines (352 loc) · 13.9 KB
/
DefaultAccount.spec.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import { expect } from "chai";
import { ethers, network } from "hardhat";
import * as zksync from "zksync-web3";
import type { Wallet } from "zksync-web3";
import { serialize } from "zksync-web3/build/src/utils";
import type { DefaultAccount, DelegateCaller, MockContract } from "../typechain-types";
import { DefaultAccount__factory } from "../typechain-types";
import { TEST_BOOTLOADER_FORMAL_ADDRESS } from "./shared/constants";
import { signedTxToTransactionData } from "./shared/transactions";
import { deployContract, deployContractOnAddress, getWallets, loadArtifact } from "./shared/utils";
import { getMock } from "./shared/mocks";
// TODO: more test cases can be added.
describe("DefaultAccount tests", function () {
let wallet: Wallet;
let bootloaderAccount: ethers.Signer;
let defaultAccount: DefaultAccount;
let account: Wallet;
let callable: MockContract;
let delegateCaller: DelegateCaller;
let mockERC20: MockContract;
let paymasterFlowIface: ethers.utils.Interface;
let ERC20Iface: ethers.utils.Interface;
const RANDOM_ADDRESS = ethers.utils.getAddress("0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
before(async () => {
wallet = getWallets()[0];
account = getWallets()[2];
await deployContractOnAddress(account.address, "DefaultAccount");
defaultAccount = DefaultAccount__factory.connect(account.address, wallet);
callable = (await deployContract("MockContract")) as MockContract;
delegateCaller = (await deployContract("DelegateCaller")) as DelegateCaller;
mockERC20 = (await deployContract("MockContract")) as MockContract;
paymasterFlowIface = new ethers.utils.Interface((await loadArtifact("IPaymasterFlow")).abi);
ERC20Iface = new ethers.utils.Interface((await loadArtifact("IERC20")).abi);
bootloaderAccount = await ethers.getImpersonatedSigner(TEST_BOOTLOADER_FORMAL_ADDRESS);
});
after(async function () {
await network.provider.request({
method: "hardhat_stopImpersonatingAccount",
params: [TEST_BOOTLOADER_FORMAL_ADDRESS],
});
});
describe("validateTransaction", function () {
it("non-deployer ignored", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: RANDOM_ADDRESS,
from: account.address,
nonce: 1,
data: "0x",
value: 0,
gasLimit: 50000,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
const txData = signedTxToTransactionData(parsedTx)!;
const txHash = parsedTx.hash;
delete legacyTx.from;
const signedHash = ethers.utils.keccak256(serialize(legacyTx));
const call = {
from: wallet.address,
to: defaultAccount.address,
value: 0,
data: defaultAccount.interface.encodeFunctionData("validateTransaction", [txHash, signedHash, txData]),
};
expect(await wallet.provider.call(call)).to.be.eq("0x");
});
it("invalid signature", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: RANDOM_ADDRESS,
from: account.address,
nonce: 1,
data: "0x",
value: 0,
gasLimit: 50000,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
parsedTx.s = "0x0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0";
const txData = signedTxToTransactionData(parsedTx)!;
const txHash = parsedTx.hash;
delete legacyTx.from;
const signedHash = ethers.utils.keccak256(serialize(legacyTx));
const call = {
from: TEST_BOOTLOADER_FORMAL_ADDRESS,
to: defaultAccount.address,
value: 0,
data: defaultAccount.interface.encodeFunctionData("validateTransaction", [txHash, signedHash, txData]),
};
expect(await bootloaderAccount.provider.call(call)).to.be.eq(ethers.constants.HashZero);
});
it("valid tx", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: RANDOM_ADDRESS,
from: account.address,
nonce: 5,
data: "0x",
value: 0,
gasLimit: 50000,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
const txData = signedTxToTransactionData(parsedTx)!;
const txHash = parsedTx.hash;
delete legacyTx.from;
const signedHash = ethers.utils.keccak256(serialize(legacyTx));
const call = {
from: TEST_BOOTLOADER_FORMAL_ADDRESS,
to: defaultAccount.address,
value: 0,
data: defaultAccount.interface.encodeFunctionData("validateTransaction", [txHash, signedHash, txData]),
};
expect(await bootloaderAccount.provider.call(call)).to.be.eq(
defaultAccount.interface.getSighash("validateTransaction") + "0".repeat(56)
);
});
});
describe("executeTransaction", function () {
it("non-deployer ignored", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: callable.address,
from: account.address,
nonce: 111,
data: "0xdeadbeef",
value: 5,
gasLimit: 50000,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
const txData = signedTxToTransactionData(parsedTx)!;
const txHash = parsedTx.hash;
delete legacyTx.from;
const signedHash = ethers.utils.keccak256(serialize(legacyTx));
await expect(await defaultAccount.executeTransaction(txHash, signedHash, txData)).to.not.emit(callable, "Called");
});
it("successfully executed", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: callable.address,
from: account.address,
nonce: 111,
data: "0xdeadbeef",
value: 0,
gasLimit: 50000,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
const txData = signedTxToTransactionData(parsedTx)!;
const txHash = parsedTx.hash;
delete legacyTx.from;
const signedHash = ethers.utils.keccak256(serialize(legacyTx));
await expect(await defaultAccount.connect(bootloaderAccount).executeTransaction(txHash, signedHash, txData))
.to.emit(callable, "Called")
.withArgs(0, "0xdeadbeef");
});
it("non-zero value", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: callable.address,
from: account.address,
nonce: 111,
data: "0x",
value: 5,
gasLimit: 50000,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
const txData = signedTxToTransactionData(parsedTx)!;
const txHash = parsedTx.hash;
delete legacyTx.from;
const signedHash = ethers.utils.keccak256(serialize(legacyTx));
await expect(await defaultAccount.connect(bootloaderAccount).executeTransaction(txHash, signedHash, txData))
.to.emit(getMock("MsgValueSimulator"), "Called")
.withArgs(0, "0x");
});
});
describe("executeTransactionFromOutside", function () {
it("nothing", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: callable.address,
from: account.address,
nonce: 111,
data: "0xdeadbeef",
value: 5,
gasLimit: 50000,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
const txData = signedTxToTransactionData(parsedTx)!;
delete legacyTx.from;
await expect(await defaultAccount.executeTransactionFromOutside(txData)).to.not.emit(callable, "Called");
});
});
describe("payForTransaction", function () {
it("non-deployer ignored", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: callable.address,
from: account.address,
nonce: 1,
data: "0xdeadbeef",
value: 5,
gasLimit: 50000,
gasPrice: 200,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
const txData = signedTxToTransactionData(parsedTx)!;
const txHash = parsedTx.hash;
delete legacyTx.from;
const signedHash = ethers.utils.keccak256(serialize(legacyTx));
await expect(defaultAccount.payForTransaction(txHash, signedHash, txData)).to.not.emit(
getMock("Bootloader"),
"Called"
);
});
it("successfully payed", async () => {
const legacyTx = await account.populateTransaction({
type: 0,
to: callable.address,
from: account.address,
nonce: 2,
data: "0xdeadbeef",
value: 5,
gasLimit: 50000,
gasPrice: 200,
});
const txBytes = await account.signTransaction(legacyTx);
const parsedTx = zksync.utils.parseTransaction(txBytes);
const txData = signedTxToTransactionData(parsedTx)!;
const txHash = parsedTx.hash;
delete legacyTx.from;
const signedHash = ethers.utils.keccak256(serialize(legacyTx));
await expect(await defaultAccount.connect(bootloaderAccount).payForTransaction(txHash, signedHash, txData))
.to.emit(getMock("Bootloader"), "Called")
.withArgs(50000 * 200, "0x");
});
});
describe("prepareForPaymaster", function () {
it("non-deployer ignored", async () => {
const eip712Tx = await account.populateTransaction({
type: 113,
to: callable.address,
from: account.address,
data: "0x",
value: 0,
maxFeePerGas: 12000,
maxPriorityFeePerGas: 100,
gasLimit: 50000,
customData: {
gasPerPubdata: zksync.utils.DEFAULT_GAS_PER_PUBDATA_LIMIT,
paymasterParams: {
paymaster: RANDOM_ADDRESS,
paymasterInput: paymasterFlowIface.encodeFunctionData("approvalBased", [mockERC20.address, 2023, "0x"]),
},
},
});
const signedEip712Tx = await account.signTransaction(eip712Tx);
const parsedEIP712tx = zksync.utils.parseTransaction(signedEip712Tx);
const eip712TxData = signedTxToTransactionData(parsedEIP712tx)!;
const eip712TxHash = parsedEIP712tx.hash;
const eip712SignedHash = zksync.EIP712Signer.getSignedDigest(eip712Tx);
await expect(await defaultAccount.prepareForPaymaster(eip712TxHash, eip712SignedHash, eip712TxData)).to.not.emit(
mockERC20,
"Called"
);
});
it("successfully prepared", async () => {
await mockERC20.setResult({
input: ERC20Iface.encodeFunctionData("allowance", [account.address, RANDOM_ADDRESS]),
failure: false,
returnData: ethers.constants.HashZero,
});
const eip712Tx = await account.populateTransaction({
type: 113,
to: callable.address,
from: account.address,
data: "0x",
value: 0,
maxFeePerGas: 12000,
maxPriorityFeePerGas: 100,
gasLimit: 50000,
customData: {
gasPerPubdata: zksync.utils.DEFAULT_GAS_PER_PUBDATA_LIMIT,
paymasterParams: {
paymaster: RANDOM_ADDRESS,
paymasterInput: paymasterFlowIface.encodeFunctionData("approvalBased", [mockERC20.address, 2023, "0x"]),
},
},
});
const signedEip712Tx = await account.signTransaction(eip712Tx);
const parsedEIP712tx = zksync.utils.parseTransaction(signedEip712Tx);
const eip712TxData = signedTxToTransactionData(parsedEIP712tx)!;
const eip712TxHash = parsedEIP712tx.hash;
const eip712SignedHash = zksync.EIP712Signer.getSignedDigest(eip712Tx);
await expect(
await defaultAccount
.connect(bootloaderAccount)
.prepareForPaymaster(eip712TxHash, eip712SignedHash, eip712TxData)
)
.to.emit(mockERC20, "Called")
.withArgs(0, ERC20Iface.encodeFunctionData("approve", [RANDOM_ADDRESS, 2023]));
});
});
describe("fallback/receive", function () {
it("zero value by EOA wallet", async () => {
const call = {
from: wallet.address,
to: defaultAccount.address,
value: 0,
data: "0x872384894899834939049043904390390493434343434344433443433434344234234234",
};
expect(await wallet.provider.call(call)).to.be.eq("0x");
});
it("non-zero value by EOA wallet", async () => {
const call = {
from: wallet.address,
to: defaultAccount.address,
value: 3223,
data: "0x87238489489983493904904390431212224343434344433443433434344234234234",
};
expect(await wallet.provider.call(call)).to.be.eq("0x");
});
it("zero value by bootloader", async () => {
// Here we need to ensure that during delegatecalls even if `msg.sender` is the bootloader,
// the fallback is behaving correctly
const calldata = delegateCaller.interface.encodeFunctionData("delegateCall", [defaultAccount.address]);
const call = {
from: TEST_BOOTLOADER_FORMAL_ADDRESS,
to: delegateCaller.address,
value: 0,
data: calldata,
};
expect(await bootloaderAccount.call(call)).to.be.eq("0x");
});
it("non-zero value by bootloader", async () => {
// Here we need to ensure that during delegatecalls even if `msg.sender` is the bootloader,
// the fallback is behaving correctly
const calldata = delegateCaller.interface.encodeFunctionData("delegateCall", [defaultAccount.address]);
const call = {
from: TEST_BOOTLOADER_FORMAL_ADDRESS,
to: delegateCaller.address,
value: 3223,
data: calldata,
};
expect(await bootloaderAccount.call(call)).to.be.eq("0x");
});
});
});