forked from safe-global/safe-modules
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExecuteWithPaymaster.spec.ts
367 lines (318 loc) · 14.2 KB
/
ExecuteWithPaymaster.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
import { expect } from 'chai'
import { deployments, ethers } from 'hardhat'
import { decodePublicKey, encodeWebAuthnSignature } from '../../src/utils/webauthn'
import { WebAuthnCredentials } from '../../test/utils/webauthnShim'
import { buildSafeUserOpTransaction, buildPackedUserOperationFromSafeUserOperation } from '@safe-global/safe-4337/dist/src/utils/userOp'
import { buildSignatureBytes } from '@safe-global/safe-4337/dist/src/utils/execution'
/**
* User story: Execute with Paymaster.
* The test cases here cover following flows:
* 1. Deploy a Safe with a passkey signer as an owner. The userOp gas is sponsored by a Paymaster.
* 2. Execute a userOp with an existing Safe with passkey signer as an owner. The userOp gas is sponsored by a Paymaster.
*
* The paymaster used in the tests is imported from @account-abstraction/contracts/samples/VerifyingPaymaster.sol.
* This contract has a verifyingSigner address which provides approval for sponsoring userOp gas via ECDSA signatures.
*
*/
describe('Execute userOps with Paymaster: [@userstory]', () => {
const generalSetup = deployments.createFixture(async ({ deployments }) => {
const { EntryPoint, Safe4337Module, SafeProxyFactory, SafeModuleSetup, SafeL2, FCLP256Verifier, SafeWebAuthnSignerFactory } =
await deployments.run()
const [relayer, verifyingSigner] = await ethers.getSigners()
const entryPoint = await ethers.getContractAt('IEntryPoint', EntryPoint.address)
const module = await ethers.getContractAt(Safe4337Module.abi, Safe4337Module.address)
const proxyFactory = await ethers.getContractAt(SafeProxyFactory.abi, SafeProxyFactory.address)
const safeModuleSetup = await ethers.getContractAt(SafeModuleSetup.abi, SafeModuleSetup.address)
const singleton = await ethers.getContractAt(SafeL2.abi, SafeL2.address)
const verifier = await ethers.getContractAt('IP256Verifier', FCLP256Verifier.address)
const signerFactory = await ethers.getContractAt('SafeWebAuthnSignerFactory', SafeWebAuthnSignerFactory.address)
// Deploy a Paymaster contract
const paymaster = await (await ethers.getContractFactory('VerifyingPaymaster')).deploy(entryPoint, verifyingSigner)
// Add deposit in the entrypoint contract so that paymaster can sponsor userOp execution
await paymaster.deposit({ value: ethers.parseEther('1') })
const navigator = {
credentials: new WebAuthnCredentials(),
}
// Create a WebAuthn credential for the signer
const credential = navigator.credentials.create({
publicKey: {
rp: {
name: 'Safe',
id: 'safe.global',
},
user: {
id: ethers.getBytes(ethers.id('chucknorris')),
name: 'chucknorris',
displayName: 'Chuck Norris',
},
challenge: ethers.toBeArray(Date.now()),
pubKeyCredParams: [{ type: 'public-key', alg: -7 }],
},
})
const publicKey = decodePublicKey(credential.response)
// Deploy signer contract
await signerFactory.createSigner(publicKey.x, publicKey.y, await verifier.getAddress())
// Get signer address
const signer = await signerFactory.getSigner(publicKey.x, publicKey.y, await verifier.getAddress())
return {
relayer,
proxyFactory,
safeModuleSetup,
module,
entryPoint,
singleton,
signerFactory,
navigator,
verifier,
SafeL2,
credential,
paymaster,
verifyingSigner,
signer,
}
})
/**
* The flow can be summarized as follows:
* Step 1: Setup the contracts.
* Step 2: Create a userOp with initCode that would deploy a Safe account.
* Step 3: Get signature from verifyingSigner account and create paymaster data.
* Step 4: Sign userOp with passkey signer.
* Step 5: Execute the userOp that deploys a Safe with passkey signer as owner.
*/
describe('New Safe', () => {
// Create a fixture to setup the contracts and signer(s)
const setupTests = generalSetup
it('should execute a userOp and deploy a Safe using Paymaster', async () => {
const {
relayer,
proxyFactory,
safeModuleSetup,
module,
entryPoint,
singleton,
navigator,
credential,
paymaster,
verifyingSigner,
SafeL2,
signer,
} = await setupTests()
// Step 2: Create a userOp with initCode that would deploy a Safe account.
// The initializer data to enable the Safe4337Module as a module on a Safe
const initializer = safeModuleSetup.interface.encodeFunctionData('enableModules', [[module.target]])
// Create setup data to deploy a Safe with passkey signer as owner, threshold 1, Safe4337Module as module and fallback handler
const setupData = singleton.interface.encodeFunctionData('setup', [
[signer],
1n,
safeModuleSetup.target,
initializer,
module.target,
ethers.ZeroAddress,
0,
ethers.ZeroAddress,
])
// Predict the Safe address to construct the userOp
const safeSalt = Date.now()
// Get predicted Safe address. Alternatively, the Safe address can be retrieved from event logs or calculated off-chain via create2
const safe = await proxyFactory.createProxyWithNonce.staticCall(singleton, setupData, safeSalt)
// Deploy data required in the initCode of the userOp
const deployData = proxyFactory.interface.encodeFunctionData('createProxyWithNonce', [singleton.target, setupData, safeSalt])
const paymasterVerificationGasLimit = 60000
const paymasterPostOpGasLimit = 60000
let paymasterAndData = ethers.solidityPacked(
['address', 'uint128', 'uint128'],
[paymaster.target, paymasterVerificationGasLimit, paymasterPostOpGasLimit],
)
const safeOp = buildSafeUserOpTransaction(
safe,
ethers.ZeroAddress,
0,
'0x',
await entryPoint.getNonce(safe, 0),
await entryPoint.getAddress(),
false,
true,
{
initCode: ethers.solidityPacked(['address', 'bytes'], [proxyFactory.target, deployData]),
// Set a higher verificationGasLimit to avoid error "AA26 over verificationGasLimit"
verificationGasLimit: 600000,
paymasterAndData: paymasterAndData,
},
)
const packedUserOp = buildPackedUserOperationFromSafeUserOperation({
safeOp,
signature: '0x',
})
// Step 3: Get signature from verifyingSigner account and create paymaster data.
const paymasterValidUntil = 0
const paymasterValidAfter = 0
const paymasterHash = await paymaster.getHash(packedUserOp, paymasterValidAfter, paymasterValidUntil)
const paymasterSignature = await verifyingSigner.signMessage(ethers.getBytes(paymasterHash))
const paymasterData = ethers.solidityPacked(
['bytes', 'bytes'],
[ethers.AbiCoder.defaultAbiCoder().encode(['uint48', 'uint48'], [paymasterValidAfter, paymasterValidUntil]), paymasterSignature],
)
paymasterAndData = ethers.solidityPacked(
['address', 'uint128', 'uint128', 'bytes'],
[paymaster.target, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData],
)
packedUserOp.paymasterAndData = paymasterAndData
// Step 4: Sign userOp with Passkey signer.
// opHash that will be signed using Passkey credentials
const opHash = await module.getOperationHash(packedUserOp)
const assertion = navigator.credentials.get({
publicKey: {
challenge: ethers.getBytes(opHash),
rpId: 'safe.global',
allowCredentials: [{ type: 'public-key', id: new Uint8Array(credential.rawId) }],
userVerification: 'required',
},
})
// Build the contract signature that a Safe will forward to the signer contract
const signature = buildSignatureBytes([
{
signer: signer,
data: encodeWebAuthnSignature(assertion.response),
dynamic: true,
},
])
// Set the signature in the packedUserOp
packedUserOp.signature = ethers.solidityPacked(['uint48', 'uint48', 'bytes'], [safeOp.validAfter, safeOp.validUntil, signature])
// Check if Safe is not already created
expect(await ethers.provider.getCode(safe)).to.equal('0x')
// Step 5: Execute the userOp that deploys a Safe with passkey signer as owner.
await entryPoint.handleOps([packedUserOp], relayer.address)
// Check if Safe is created and uses the expected Singleton
const [implementation] = ethers.AbiCoder.defaultAbiCoder().decode(['address'], await ethers.provider.getStorage(safe, 0))
expect(implementation).to.equal(singleton.target)
// Check if signer is the Safe owner
const safeInstance = await ethers.getContractAt(SafeL2.abi, safe)
expect(await safeInstance.getOwners()).to.deep.equal([signer])
})
})
/**
* The flow can be summarized as follows:
* Step 1: Setup the contracts.
* Step 2: Create a userOp with callData that transfers 0.2 ethers to address(0).
* Step 3: Get signature from verifyingSigner account and create paymaster data.
* Step 4: Sign userOp with Passkey signer.
* Step 5: Execute the userOp that with an existing Safe with passkey signer as owner.
*/
describe('Existing Safe', () => {
// Create a fixture to setup the contracts and signer(s)
const setupTests = deployments.createFixture(async ({ deployments }) => {
const {
relayer,
verifyingSigner,
proxyFactory,
safeModuleSetup,
module,
entryPoint,
singleton,
credential,
paymaster,
navigator,
signer,
} = await generalSetup(deployments)
// The initializer data to enable the Safe4337Module as a module on a Safe
const initializer = safeModuleSetup.interface.encodeFunctionData('enableModules', [[module.target]])
// Create setup data to deploy a Safe passkey signer as owner, threshold 1, Safe4337Module as module and fallback handler
const setupData = singleton.interface.encodeFunctionData('setup', [
[signer],
1n,
safeModuleSetup.target,
initializer,
module.target,
ethers.ZeroAddress,
0,
ethers.ZeroAddress,
])
// Deploy a Safe with passkey signer as owner
const safeSalt = Date.now()
// Get predicted Safe address. Alternatively, the Safe address can be retrieved from event logs or calculated off-chain via create2
const safeAddress = await proxyFactory.createProxyWithNonce.staticCall(singleton, setupData, safeSalt)
await proxyFactory.createProxyWithNonce(singleton, setupData, safeSalt)
return {
relayer,
module,
entryPoint,
navigator,
credential,
paymaster,
safeAddress,
signer,
verifyingSigner,
}
})
it('should execute a userOp with an existing Safe using Paymaster', async () => {
const { safeAddress, signer, relayer, module, entryPoint, navigator, credential, paymaster, verifyingSigner } = await setupTests()
const paymasterVerificationGasLimit = 60000
const paymasterPostOpGasLimit = 60000
let paymasterAndData = ethers.solidityPacked(
['address', 'uint128', 'uint128'],
[paymaster.target, paymasterVerificationGasLimit, paymasterPostOpGasLimit],
)
// Step 2: Create a userOp with callData that transfers 0.2 ethers to address(0).
const safeOp = buildSafeUserOpTransaction(
safeAddress,
ethers.ZeroAddress,
ethers.parseEther('0.2'),
'0x',
await entryPoint.getNonce(safeAddress, 0),
await entryPoint.getAddress(),
false,
true,
{
paymasterAndData: paymasterAndData,
},
)
const packedUserOp = buildPackedUserOperationFromSafeUserOperation({
safeOp,
signature: '0x',
})
// Step 3: Get signature from verifyingSigner account and create paymaster data.
const paymasterValidUntil = 0
const paymasterValidAfter = 0
const paymasterHash = await paymaster.getHash(packedUserOp, paymasterValidAfter, paymasterValidUntil)
const paymasterSignature = await verifyingSigner.signMessage(ethers.getBytes(paymasterHash))
const paymasterData = ethers.solidityPacked(
['bytes', 'bytes'],
[ethers.AbiCoder.defaultAbiCoder().encode(['uint48', 'uint48'], [paymasterValidAfter, paymasterValidUntil]), paymasterSignature],
)
paymasterAndData = ethers.solidityPacked(
['address', 'uint128', 'uint128', 'bytes'],
[paymaster.target, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData],
)
packedUserOp.paymasterAndData = paymasterAndData
// Step 4: Sign userOp with Passkey signer.
// opHash that will be signed using Passkey credentials
const opHash = await module.getOperationHash(packedUserOp)
const assertion = navigator.credentials.get({
publicKey: {
challenge: ethers.getBytes(opHash),
rpId: 'safe.global',
allowCredentials: [{ type: 'public-key', id: new Uint8Array(credential.rawId) }],
userVerification: 'required',
},
})
// Build the contract signature that a Safe will forward to the signer contract
const signature = buildSignatureBytes([
{
signer: signer,
data: encodeWebAuthnSignature(assertion.response),
dynamic: true,
},
])
// Set the signature in the packedUserOp
packedUserOp.signature = ethers.solidityPacked(['uint48', 'uint48', 'bytes'], [safeOp.validAfter, safeOp.validUntil, signature])
// Send 1 ETH to the Safe
await relayer.sendTransaction({ to: safeAddress, value: ethers.parseEther('1') })
const balanceBefore = await ethers.provider.getBalance(ethers.ZeroAddress)
// Step 5: Execute the userOp that with an existing Safe with passkey signer as owner using Paymaster
await entryPoint.handleOps([packedUserOp], relayer.address)
// Check if the address(0) received 0.2 ETH
expect(await ethers.provider.getBalance(ethers.ZeroAddress)).to.be.equal(balanceBefore + ethers.parseEther('0.2'))
expect(await ethers.provider.getBalance(safeAddress)).to.be.equal(ethers.parseEther('1') - ethers.parseEther('0.2'))
})
})
})