-
Notifications
You must be signed in to change notification settings - Fork 10
/
AccountAbstractionInvoker.ts
299 lines (265 loc) · 9.21 KB
/
AccountAbstractionInvoker.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
import { ethers, network } from "hardhat";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { AccountAbstractionInvoker } from "../typechain-types/AccountAbstractionInvoker";
import { MockContract } from "../typechain-types/MockContract";
import { expect } from "chai";
import getSignature from "../scripts/signing/getSignature";
import * as tracking from "../scripts/tracking/track";
describe("AccountAbstractionInvoker", () => {
let invoker: AccountAbstractionInvoker;
let mock: MockContract;
let alice: SignerWithAddress;
let bob: SignerWithAddress;
let alicePk: string;
// 4-byte signatures of mock functions
const increment = "0xd09de08a";
const causeRevert = "0x67192b63";
// Deployment fixture
async function deployContracts() {
const AccountAbstractionInvoker = await ethers.getContractFactory(
"AccountAbstractionInvoker"
);
const MockContract = await ethers.getContractFactory("MockContract");
const invoker = await AccountAbstractionInvoker.deploy();
await invoker.deployed();
const mock = await MockContract.deploy();
await invoker.deployed();
return { invoker, mock };
}
// Deploy contracts if scripts/tracking/out/record.json does not exist
// Otherwise, use existing contracts from record.json
// Set REDEPLOY=true to force re-deployment
before(async () => {
[alice, bob] = await ethers.getSigners();
alicePk = process.env.PK_ALICE!;
const record = tracking.read();
const redeploy = record === undefined || process.env.REDEPLOY === "true";
if (redeploy) {
const deployments = await deployContracts();
invoker = deployments.invoker;
mock = deployments.mock;
tracking.write({
chainId: network.config["chainId"],
invoker: invoker.address,
mock: mock.address,
});
} else {
invoker = await ethers.getContractAt(
"AccountAbstractionInvoker",
record.invoker
);
mock = await ethers.getContractAt("MockContract", record.mock);
}
});
describe("constructor", () => {
it("Should set types", async () => {
const eip712DomainType = ethers.utils.solidityKeccak256(
["string"],
[
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)",
]
);
const transactionType = ethers.utils.solidityKeccak256(
["string"],
[
"Transaction(address from,uint256 nonce,TransactionPayload[] payload)TransactionPayload(address to,uint256 value,uint256 gasLimit,bytes data)",
]
);
const transactionPayloadType = ethers.utils.solidityKeccak256(
["string"],
[
"TransactionPayload(address to,uint256 value,uint256 gasLimit,bytes data)",
]
);
expect(await invoker.EIP712DOMAIN_TYPE()).to.equal(eip712DomainType);
expect(await invoker.TRANSACTION_TYPE()).to.equal(transactionType);
expect(await invoker.TRANSACTION_PAYLOAD_TYPE()).to.equal(
transactionPayloadType
);
});
it("Should set domain separator", async () => {
const eip712DomainType = ethers.utils.solidityKeccak256(
["string"],
[
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)",
]
);
const name = "Account Abstraction Invoker";
const version = "1.0.0";
const domainSeparator = ethers.utils.keccak256(
ethers.utils.defaultAbiCoder.encode(
["bytes32", "bytes32", "bytes32", "uint256", "address"],
[
eip712DomainType,
ethers.utils.solidityKeccak256(["string"], [name]),
ethers.utils.solidityKeccak256(["string"], [version]),
network.config["chainId"]!,
invoker.address,
]
)
);
expect(await invoker.DOMAIN_SEPARATOR()).to.equal(domainSeparator);
});
});
describe("invoke", () => {
it("Should revert on no payload", async () => {
const nonce = await invoker.nonces(alice.address);
const messageWithoutPayload = {
from: alice.address,
nonce: nonce,
payload: [],
};
const signature = getSignature(messageWithoutPayload, alicePk);
await expect(
invoker.invoke(signature, messageWithoutPayload)
).to.be.revertedWith("No transaction payload");
});
it("Should revert on invalid signature", async () => {
const nonce = await invoker.nonces(alice.address);
const message = {
from: alice.address,
nonce: nonce,
payload: [
{ to: mock.address, value: 0, gasLimit: 1000000, data: increment },
],
};
const invalidSignature = getSignature(message, alicePk);
invalidSignature.v = !invalidSignature.v;
await expect(
invoker.invoke(invalidSignature, message)
).to.be.revertedWith("Invalid signature");
});
it("Should revert on invalid nonce", async () => {
const invalidNonce = (await invoker.nonces(alice.address)).add("1");
const messageWithInvalidNonce = {
from: alice.address,
nonce: invalidNonce,
payload: [
{ to: mock.address, value: 0, gasLimit: 1000000, data: increment },
],
};
const signature = getSignature(messageWithInvalidNonce, alicePk);
await expect(
invoker.invoke(signature, messageWithInvalidNonce)
).to.be.revertedWith("Invalid nonce");
});
it("Should revert on call failure", async () => {
const nonce = await invoker.nonces(alice.address);
const messageWithRevertingCall = {
from: alice.address,
nonce: nonce,
payload: [
{ to: mock.address, value: 0, gasLimit: 1000000, data: increment },
{ to: mock.address, value: 0, gasLimit: 1000000, data: causeRevert },
],
};
const signature = getSignature(messageWithRevertingCall, alicePk);
await expect(
invoker.invoke(signature, messageWithRevertingCall)
).to.be.revertedWith("Transaction failed");
});
it("Should revert on leftover value", async () => {
const nonce = await invoker.nonces(alice.address);
const message = {
from: alice.address,
nonce: nonce,
payload: [
{ to: mock.address, value: 0, gasLimit: 1000000, data: increment },
],
};
const signature = getSignature(message, alicePk);
await expect(
invoker.invoke(signature, message, { value: "1" })
).to.be.revertedWith("Invalid balance");
});
it("Should bundle transactions", async () => {
const nonce = await invoker.nonces(alice.address);
const message = {
from: alice.address,
nonce,
payload: [
{
to: mock.address,
value: 1,
gasLimit: 1000000,
data: increment,
},
{
to: mock.address,
value: 1,
gasLimit: 1000000,
data: increment,
},
],
};
const signature = getSignature(message, alicePk);
const mockBalance = await ethers.provider.getBalance(mock.address);
const mockCounter = await mock.counter();
const tx = await invoker.invoke(signature, message, {
value: 2,
});
await tx.wait();
expect(await ethers.provider.getBalance(mock.address)).to.equal(
mockBalance.add("2")
);
expect(await mock.lastSender()).to.equal(alice.address);
expect(await mock.counter()).to.equal(mockCounter.add("2"));
});
});
describe("Sponsoring examples", () => {
let invokerAsBob: AccountAbstractionInvoker;
before(async () => {
invokerAsBob = await ethers.getContractAt(
"AccountAbstractionInvoker",
invoker.address,
bob
);
});
it("Enables transaction sponsoring", async () => {
const nonce = await invoker.nonces(alice.address);
const message = {
from: alice.address,
nonce,
payload: [
{
to: mock.address,
value: 0,
gasLimit: 1000000,
data: increment,
},
],
};
const signature = getSignature(message, alicePk);
const aliceBalance = await ethers.provider.getBalance(alice.address);
const mockCounter = await mock.counter();
const tx = await invokerAsBob.invoke(signature, message);
await tx.wait();
expect(await ethers.provider.getBalance(alice.address)).to.equal(
aliceBalance
);
expect(await mock.lastSender()).to.equal(alice.address);
expect(await mock.counter()).to.equal(mockCounter.add("1"));
});
it("Prevents manipulation", async () => {
const nonce = await invoker.nonces(alice.address);
const message = {
from: alice.address,
nonce,
payload: [
{
to: mock.address,
value: 0,
gasLimit: 1000000,
data: increment,
},
],
};
const signature = getSignature(message, alicePk);
const modifiedMessage = message;
modifiedMessage.payload[0].gasLimit = 5000000;
await expect(
invokerAsBob.invoke(signature, modifiedMessage)
).to.be.revertedWith("Invalid signature");
});
});
});