-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathexecute-stateless7702.test.ts
More file actions
517 lines (422 loc) · 15.1 KB
/
Copy pathexecute-stateless7702.test.ts
File metadata and controls
517 lines (422 loc) · 15.1 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import { beforeEach, expect, test } from 'vitest';
import {
publicClient,
fundAddress,
randomAddress,
deployCounter,
transport,
gasPrice,
deployerClient,
} from './utils/helpers';
import { expectUserOperationToSucceed } from './utils/assertions';
import {
Implementation,
toMetaMaskSmartAccount,
MetaMaskSmartAccount,
} from '@metamask/delegation-toolkit';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { createClient, encodeFunctionData, parseEther } from 'viem';
import { chain } from '../src/config';
import CounterMetadata from './utils/counter/metadata.json';
import { sponsoredBundlerClient } from './utils/helpers';
let aliceSmartAccount: MetaMaskSmartAccount<Implementation.Stateless7702>;
let aliceAccount: ReturnType<typeof privateKeyToAccount>;
/**
* Utility function to upgrade Alice's EOA to use EIP-7702 delegation
*/
async function upgradeAliceEOAWithEIP7702() {
// because Alice is not self-submitting, we do not need to increment the nonce
const nonce = await publicClient.getTransactionCount({
address: aliceAccount.address,
});
const { EIP7702StatelessDeleGatorImpl } =
aliceSmartAccount.environment.implementations;
const signedAuthorization = await aliceAccount.signAuthorization({
contractAddress: EIP7702StatelessDeleGatorImpl,
chainId: chain.id,
nonce,
});
const txHash = await deployerClient.sendTransaction({
to: aliceAccount.address,
authorizationList: [signedAuthorization],
});
await publicClient.waitForTransactionReceipt({ hash: txHash });
const code = await publicClient.getCode({
address: aliceAccount.address,
});
// Magic 7702 delegation code
expect(code).to.equal(`0xef0100${EIP7702StatelessDeleGatorImpl.slice(2)}`);
}
beforeEach(async () => {
aliceAccount = privateKeyToAccount(generatePrivateKey());
const client = createClient({ transport, chain });
aliceSmartAccount = await toMetaMaskSmartAccount({
client,
implementation: Implementation.Stateless7702,
address: aliceAccount.address,
signatory: { account: aliceAccount },
});
await upgradeAliceEOAWithEIP7702();
});
/*
Alice creates a DeleGatorSmartAccount for a Stateless7702 delegator account using her EOA address.
She then uses the smart account to submit various user operations.
*/
test('maincase: Send value to a recipient', async () => {
const value = 1n;
const recipient = randomAddress();
const beforeBalance = await publicClient.getBalance({ address: recipient });
expect(beforeBalance, "Recipient's initial balance should be zero").toBe(0n);
const { address } = aliceSmartAccount;
await fundAddress(address, parseEther('0.1'));
const userOpHash = await sponsoredBundlerClient.sendUserOperation({
account: aliceSmartAccount,
calls: [
{
to: recipient,
data: '0x',
value,
},
],
...gasPrice,
});
const receipt = await sponsoredBundlerClient.waitForUserOperationReceipt({
hash: userOpHash,
});
expectUserOperationToSucceed(receipt);
const afterBalance = await publicClient.getBalance({ address: recipient });
expect(
afterBalance,
"Recipient's balance should match the transferred value",
).toBe(value);
});
test('Deploy Counter contract and increment via user operation', async () => {
const { address } = aliceSmartAccount;
await fundAddress(address, parseEther('1'));
const aliceCounter = await deployCounter(aliceSmartAccount.address);
const countBefore = await publicClient.readContract({
address: aliceCounter.address,
abi: CounterMetadata.abi,
functionName: 'count',
});
expect(countBefore, 'Initial counter value should be zero').toBe(0n);
const userOpHash = await sponsoredBundlerClient.sendUserOperation({
account: aliceSmartAccount,
calls: [
{
to: aliceCounter.address,
data: encodeFunctionData({
abi: CounterMetadata.abi,
functionName: 'increment',
}),
},
],
...gasPrice,
});
const receipt = await sponsoredBundlerClient.waitForUserOperationReceipt({
hash: userOpHash,
});
expectUserOperationToSucceed(receipt);
const countAfter = await publicClient.readContract({
address: aliceCounter.address,
abi: CounterMetadata.abi,
functionName: 'count',
});
expect(countAfter, 'Counter value should be incremented to 1').toBe(1n);
});
test('Execute multiple calls in a single UserOperation', async () => {
const address = await aliceSmartAccount.getAddress();
const value = parseEther('1');
await fundAddress(address, value);
const aliceCounter = await deployCounter(aliceSmartAccount.address);
const countBefore = await publicClient.readContract({
address: aliceCounter.address,
abi: CounterMetadata.abi,
functionName: 'count',
});
expect(countBefore, 'Initial counter value should be zero').toBe(0n);
const recipient = randomAddress();
const balanceBefore = await publicClient.getBalance({ address: recipient });
expect(balanceBefore, "Recipient's initial balance should be zero").toBe(0n);
const userOpHash = await sponsoredBundlerClient.sendUserOperation({
account: aliceSmartAccount,
calls: [
{
to: recipient,
value: value,
data: '0x',
},
{
to: aliceCounter.address,
data: encodeFunctionData({
abi: CounterMetadata.abi,
functionName: 'increment',
}),
},
],
...gasPrice,
});
const receipt = await sponsoredBundlerClient.waitForUserOperationReceipt({
hash: userOpHash,
});
expectUserOperationToSucceed(receipt);
const countAfter = await publicClient.readContract({
address: aliceCounter.address,
abi: CounterMetadata.abi,
functionName: 'count',
});
expect(countAfter, 'Counter value should be incremented to 1').toBe(1n);
const balanceAfter = await publicClient.getBalance({ address: recipient });
expect(
balanceAfter,
"Recipient's balance should increase by the transferred value",
).toBe(balanceBefore + value);
});
test('Alice attempts to execute transactions without funding the account', async () => {
const address = await aliceSmartAccount.getAddress();
const aliceCounter = await deployCounter(address);
const countBefore = await publicClient.readContract({
address: aliceCounter.address,
abi: CounterMetadata.abi,
functionName: 'count',
});
expect(countBefore, 'Initial counter value should be zero').toBe(0n);
const recipient = randomAddress();
const balanceBefore = await publicClient.getBalance({ address: recipient });
expect(balanceBefore, "Recipient's initial balance should be zero").toBe(0n);
const value = parseEther('1');
await expect(
sponsoredBundlerClient.sendUserOperation({
account: aliceSmartAccount,
calls: [
{
to: aliceCounter.address,
data: encodeFunctionData({
abi: CounterMetadata.abi,
functionName: 'increment',
}),
},
{
to: recipient,
value: value,
data: '0x',
},
],
...gasPrice,
}),
).rejects.toThrow();
const countAfter = await publicClient.readContract({
address: aliceCounter.address,
abi: CounterMetadata.abi,
functionName: 'count',
});
expect(countAfter, 'Counter value should remain unchanged').toBe(0n);
const balanceAfter = await publicClient.getBalance({ address: recipient });
expect(balanceAfter, "Recipient's balance should remain unchanged").toBe(0n);
});
test('Alice executes multiple calls, including a call to a function directly on her smart contract account', async () => {
const { address } = aliceSmartAccount;
const recipient = randomAddress();
const value = parseEther('0.5');
const depositAmount = parseEther('0.3');
await fundAddress(aliceSmartAccount.address, parseEther('1'));
const balanceBefore = await publicClient.getBalance({ address: recipient });
expect(balanceBefore, "Recipient's initial balance should be zero").toBe(0n);
const depositBefore = await publicClient.readContract({
address,
abi: aliceSmartAccount.abi,
functionName: 'getDeposit',
});
const callData = encodeFunctionData({
abi: aliceSmartAccount.abi,
functionName: 'addDeposit',
args: [],
});
const userOpHash = await sponsoredBundlerClient.sendUserOperation({
account: aliceSmartAccount,
calls: [
{
to: address,
data: callData,
value: depositAmount,
},
{
to: recipient,
value,
},
],
...gasPrice,
});
const userOperationReceipt =
await sponsoredBundlerClient.waitForUserOperationReceipt({
hash: userOpHash,
});
expect(userOperationReceipt.success, 'User operation should succeed').toBe(
true,
);
const depositAfter = await publicClient.readContract({
address,
abi: aliceSmartAccount.abi,
functionName: 'getDeposit',
});
expect(
depositAfter,
'Deposit should be increased by the deposit amount',
).toBe(depositBefore + depositAmount);
const balanceAfter = await publicClient.getBalance({ address: recipient });
expect(
balanceAfter,
"Recipient's balance should match the transferred value",
).toBe(value);
});
test('Alice executes multiple value transfers', async () => {
const recipient = randomAddress();
const value = parseEther('0.4');
await fundAddress(aliceSmartAccount.address, parseEther('1'));
const balanceBefore = await publicClient.getBalance({ address: recipient });
expect(balanceBefore, "Recipient's initial balance should be zero").toBe(0n);
const userOpHash = await sponsoredBundlerClient.sendUserOperation({
account: aliceSmartAccount,
calls: [
{
to: recipient,
value,
},
{
to: recipient,
value,
},
],
...gasPrice,
});
const userOperationReceipt =
await sponsoredBundlerClient.waitForUserOperationReceipt({
hash: userOpHash,
});
expect(userOperationReceipt.success, 'User operation should succeed').toBe(
true,
);
const balanceAfter = await publicClient.getBalance({ address: recipient });
expect(
balanceAfter,
"Recipient's balance should match the total transferred value",
).toBe(value + value);
});
test('Alice can check the contract version and name', async () => {
const { address } = aliceSmartAccount;
// These are view functions that don't require transactions
const contractName = await publicClient.readContract({
address,
abi: aliceSmartAccount.abi,
functionName: 'NAME',
});
const contractVersion = await publicClient.readContract({
address,
abi: aliceSmartAccount.abi,
functionName: 'VERSION',
});
const domainVersion = await publicClient.readContract({
address,
abi: aliceSmartAccount.abi,
functionName: 'DOMAIN_VERSION',
});
expect(
contractName,
'Contract name should be EIP7702StatelessDeleGator',
).toBe('EIP7702StatelessDeleGator');
expect(contractVersion, 'Contract version should be 1.3.0').toBe('1.3.0');
expect(domainVersion, 'Domain version should be 1').toBe('1');
});
test('isEip7702StatelessDelegatedAccount correctly identifies delegated accounts', async () => {
const { isEip7702StatelessDelegatedAccount } = await import(
'@metamask/delegation-toolkit'
);
// Test that Alice's account (which is delegated to EIP7702StatelessDeleGator) returns true
const isDelegated = await isEip7702StatelessDelegatedAccount({
client: publicClient,
accountAddress: aliceSmartAccount.address,
});
expect(
isDelegated,
'Alice account should be delegated to EIP7702StatelessDeleGator',
).toBe(true);
// Test with a random non-delegated address
const randomAddress = '0x1234567890123456789012345678901234567890';
const isRandomDelegated = await isEip7702StatelessDelegatedAccount({
client: publicClient,
accountAddress: randomAddress,
});
expect(
isRandomDelegated,
'Random address should not be delegated to EIP7702StatelessDeleGator',
).toBe(false);
});
test('isDeployed() method correctly identifies EIP7702 delegation for Stateless7702 accounts', async () => {
// Test that Alice's smart account's isDeployed() method returns true
// because her EOA is properly delegated to EIP7702StatelessDeleGator
const isAliceDeployed = await aliceSmartAccount.isDeployed();
expect(
isAliceDeployed,
'Alice smart account should report as deployed when properly delegated to EIP7702StatelessDeleGator',
).toBe(true);
// Create a smart account for a non-delegated EOA
const nonDelegatedAccount = privateKeyToAccount(generatePrivateKey());
const client = createClient({ transport, chain });
const nonDelegatedSmartAccount = await toMetaMaskSmartAccount({
client,
implementation: Implementation.Stateless7702,
address: nonDelegatedAccount.address,
signatory: { account: nonDelegatedAccount },
});
// Test that a non-delegated account's isDeployed() method returns false
const isNonDelegatedDeployed = await nonDelegatedSmartAccount.isDeployed();
expect(
isNonDelegatedDeployed,
'Non-delegated smart account should report as not deployed',
).toBe(false);
// Verify that the non-delegated account has no code at all
const code = await publicClient.getCode({
address: nonDelegatedAccount.address,
});
expect(code, 'Non-delegated account should have no code').toBeUndefined();
});
test('isDeployed() returns false for addresses with code that are not delegated to EIP7702StatelessDeleGator', async () => {
// Deploy a regular contract to get an address with code
const counterContract = await deployCounter(aliceSmartAccount.address);
// Verify the contract has code
const contractCode = await publicClient.getCode({
address: counterContract.address,
});
expect(contractCode, 'Contract should have code deployed').toBeDefined();
expect(
contractCode!.length,
'Contract code should not be empty',
).toBeGreaterThan(2); // More than just '0x'
// Create a Stateless7702 smart account pointing to this contract address
const contractAccount = privateKeyToAccount(generatePrivateKey());
const contractSmartAccount = await toMetaMaskSmartAccount({
client: publicClient,
implementation: Implementation.Stateless7702,
address: counterContract.address, // Point to the contract address
signatory: { account: contractAccount },
});
// Test that isDeployed() returns false even though there is code at the address
// because the code is not an EIP7702StatelessDeleGator delegation
const isContractDeployed = await contractSmartAccount.isDeployed();
expect(
isContractDeployed,
'Smart account should report as not deployed when address has non-EIP7702StatelessDeleGator code',
).toBe(false);
// Also test with the standalone function to show it would return false too
const { isEip7702StatelessDelegatedAccount } = await import(
'@metamask/delegation-toolkit'
);
const isContractDelegated = await isEip7702StatelessDelegatedAccount({
client: publicClient,
accountAddress: counterContract.address,
});
expect(
isContractDelegated,
'Contract address should not be identified as EIP7702StatelessDeleGator delegation',
).toBe(false);
});