-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathsignUserOperation.test.ts
More file actions
379 lines (337 loc) · 12.6 KB
/
Copy pathsignUserOperation.test.ts
File metadata and controls
379 lines (337 loc) · 12.6 KB
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
import { http, createTestClient, padHex, size, slice } from "viem"
import {
entryPoint06Address,
entryPoint07Address
} from "viem/account-abstraction"
import {
generatePrivateKey,
privateKeyToAccount,
toAccount
} from "viem/accounts"
import { foundry } from "viem/chains"
import { describe, expect } from "vitest"
import { testWithRpc } from "../../../permissionless-test/src/testWithRpc"
import {
getBundlerClient,
getPublicClient,
getSafeClient
} from "../../../permissionless-test/src/utils"
import { signUserOperation } from "./signUserOperation"
import { toSafeSmartAccount } from "./toSafeSmartAccount"
describe("signUserOperation", () => {
testWithRpc("signUserOperation_V06", async ({ rpc }) => {
const owners = [
privateKeyToAccount(generatePrivateKey()),
privateKeyToAccount(generatePrivateKey()),
privateKeyToAccount(generatePrivateKey())
]
const safeAccountClient = getBundlerClient({
account: await getSafeClient({
...rpc,
entryPoint: {
version: "0.6"
},
owners: owners.map((owner) => toAccount(owner.address))
}),
entryPoint: {
version: "0.6"
},
...rpc
})
const unSignedUserOperation =
await safeAccountClient.prepareUserOperation({
calls: [
{
to: safeAccountClient.account.address,
data: "0x"
}
]
})
let partialSignatures = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint06Address,
version: "0.6"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[0],
...unSignedUserOperation
})
partialSignatures = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint06Address,
version: "0.6"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[1],
signatures: partialSignatures,
...unSignedUserOperation
})
const finalSignature = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint06Address,
version: "0.6"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[2],
signatures: partialSignatures,
...unSignedUserOperation
})
const userOpHash = safeAccountClient.sendUserOperation({
...unSignedUserOperation,
signature: finalSignature
})
expect(userOpHash).toBeTruthy()
const receipt = await safeAccountClient.waitForUserOperationReceipt({
hash: await userOpHash
})
expect(receipt).toBeTruthy()
expect(receipt.success).toBeTruthy()
})
testWithRpc("signUserOperation_V07", async ({ rpc }) => {
const owners = [
privateKeyToAccount(generatePrivateKey()),
privateKeyToAccount(generatePrivateKey()),
privateKeyToAccount(generatePrivateKey())
]
const safeAccountClient = getBundlerClient({
account: await getSafeClient({
...rpc,
entryPoint: {
version: "0.7"
},
owners: owners.map((owner) => toAccount(owner.address))
}),
entryPoint: {
version: "0.7"
},
...rpc
})
const unSignedUserOperation =
await safeAccountClient.prepareUserOperation({
calls: [
{
to: safeAccountClient.account.address,
data: "0x"
}
]
})
let partialSignatures = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint07Address,
version: "0.7"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[0],
...unSignedUserOperation
})
partialSignatures = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint07Address,
version: "0.7"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[1],
signatures: partialSignatures,
...unSignedUserOperation
})
const finalSignature = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint07Address,
version: "0.7"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[2],
signatures: partialSignatures,
...unSignedUserOperation
})
const userOpHash = safeAccountClient.sendUserOperation({
...unSignedUserOperation,
signature: finalSignature
})
expect(userOpHash).toBeTruthy()
const receipt = await safeAccountClient.waitForUserOperationReceipt({
hash: await userOpHash
})
expect(receipt).toBeTruthy()
expect(receipt.success).toBeTruthy()
})
testWithRpc("signUserOperation_V07 with dynamic owner", async ({ rpc }) => {
const eoaOwner = privateKeyToAccount(generatePrivateKey())
// Contract owner validated through EIP-1271. The owner "signs" with a
// fixed 65-byte marker signature and the owner contract only returns
// the magic value when that marker arrives in the signature argument,
// proving the dynamic part is threaded through to isValidSignature.
const markerSignature = padHex("0xc0ffee", { dir: "right", size: 65 })
const erc1271OwnerAddress = privateKeyToAccount(
generatePrivateKey()
).address
const testClient = createTestClient({
mode: "anvil",
transport: http(rpc.anvilRpc),
chain: foundry
})
// Minimal EIP-1271 validator. Safe v1.4.1 calls the legacy
// isValidSignature(bytes _data, bytes _signature) variant, so the
// calldata layout is: 4-byte selector, then the two head words with
// the offsets of the `_data` and `_signature` tails. The code loads
// the first 32 bytes of `_signature`, requires them to start with the
// 0xc0ffee marker, and only then returns the magic value 0x20c13b0b
// (the selector of the legacy variant); anything else reverts:
//
// PUSH1 0x24 CALLDATALOAD // offset of `_signature` tail
// PUSH1 0x24 ADD // + 4-byte selector + 32-byte length
// CALLDATALOAD // signature[0..32]
// PUSH1 0xe8 SHR // >> 232 bits: keep first 3 bytes
// PUSH3 0xc0ffee EQ // matches the marker?
// PUSH1 0x17 JUMPI // if so, jump to the return block
// PUSH1 0x00 PUSH1 0x00 REVERT
// JUMPDEST // 0x17
// PUSH4 0x20c13b0b // legacy EIP-1271 magic value
// PUSH1 0xe0 SHL // left-align it as bytes4
// PUSH1 0x00 MSTORE // store word at memory offset 0
// PUSH1 0x20 PUSH1 0x00 RETURN // return that 32-byte word
await testClient.setCode({
address: erc1271OwnerAddress,
bytecode:
"0x6024356024013560e81c62c0ffee1460175760006000fd5b6320c13b0b60e01b60005260206000f3"
})
// An owner that lives at the ERC-1271 contract address and produces
// the marker signature the contract expects.
const dynamicOwner = toAccount({
address: erc1271OwnerAddress,
async signMessage() {
return markerSignature
},
async signTypedData() {
return markerSignature
},
async signTransaction() {
throw new Error("Not supported")
}
})
const account = await toSafeSmartAccount({
client: getPublicClient(rpc.anvilRpc),
entryPoint: {
address: entryPoint07Address,
version: "0.7"
},
owners: [eoaOwner, { owner: dynamicOwner, dynamic: true }],
version: "1.4.1",
saltNonce: 420n
})
const safeAccountClient = getBundlerClient({
account,
entryPoint: {
version: "0.7"
},
...rpc
})
const stubSignature = await account.getStubSignature()
// 6 bytes validAfter + 6 bytes validUntil + 2 * 65 bytes static parts
// + 32 bytes dynamic length + 65 bytes dynamic signature data
expect(size(slice(stubSignature, 12))).toBe(130 + 32 + 65)
const userOpHash = await safeAccountClient.sendUserOperation({
calls: [
{
to: account.address,
data: "0x"
}
]
})
const receipt = await safeAccountClient.waitForUserOperationReceipt({
hash: userOpHash
})
expect(receipt).toBeTruthy()
expect(receipt.success).toBeTruthy()
})
testWithRpc("signUserOperation_V07 7579", async ({ rpc }) => {
const owners = [
privateKeyToAccount(generatePrivateKey()),
privateKeyToAccount(generatePrivateKey()),
privateKeyToAccount(generatePrivateKey())
]
const safeAccountClient = getBundlerClient({
account: await getSafeClient({
...rpc,
entryPoint: {
version: "0.7"
},
owners: owners.map((owner) => toAccount(owner.address)),
erc7579: true
}),
entryPoint: {
version: "0.7"
},
...rpc
})
const unSignedUserOperation =
await safeAccountClient.prepareUserOperation({
calls: [
{
to: safeAccountClient.account.address,
data: "0x"
}
]
})
let partialSignatures = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint07Address,
version: "0.7"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[0],
safe4337ModuleAddress: "0x7579EE8307284F293B1927136486880611F20002",
...unSignedUserOperation
})
partialSignatures = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint07Address,
version: "0.7"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[1],
signatures: partialSignatures,
safe4337ModuleAddress: "0x7579EE8307284F293B1927136486880611F20002",
...unSignedUserOperation
})
const finalSignature = await signUserOperation({
version: "1.4.1",
entryPoint: {
address: entryPoint07Address,
version: "0.7"
},
chainId: foundry.id,
owners: owners.map((owner) => toAccount(owner.address)),
account: owners[2],
signatures: partialSignatures,
safe4337ModuleAddress: "0x7579EE8307284F293B1927136486880611F20002",
...unSignedUserOperation
})
const userOpHash = safeAccountClient.sendUserOperation({
...unSignedUserOperation,
signature: finalSignature
})
expect(userOpHash).toBeTruthy()
const receipt = await safeAccountClient.waitForUserOperationReceipt({
hash: await userOpHash
})
expect(receipt).toBeTruthy()
expect(receipt.success).toBeTruthy()
})
})