-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy patherc7710sendTransactionWithDelegation.test.ts
More file actions
284 lines (229 loc) · 7.59 KB
/
Copy patherc7710sendTransactionWithDelegation.test.ts
File metadata and controls
284 lines (229 loc) · 7.59 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
import { beforeEach, expect, test } from 'vitest';
import {
deployCounter,
transport,
deploySmartAccount,
publicClient,
fundAddress,
randomAddress,
} from '../utils/helpers';
import { chain } from '../../src/config';
import {
Implementation,
toMetaMaskSmartAccount,
createCaveatBuilder,
createDelegation,
signDelegation,
type MetaMaskSmartAccount,
type Delegation,
} from '@metamask/delegation-toolkit';
import { erc7710WalletActions } from '@metamask/delegation-toolkit/experimental';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import {
createClient,
Address,
createWalletClient,
getContract,
Account,
encodeFunctionData,
Hex,
} from 'viem';
import { encodeDelegations } from '@metamask/delegation-toolkit/utils';
import CounterMetadata from '../utils/counter/metadata.json';
let aliceSmartAccount: MetaMaskSmartAccount<Implementation.Hybrid>;
let bob: Account;
let aliceCounterContractAddress: Address;
let permissionsContext: Hex;
let signedDelegation: Delegation;
beforeEach(async () => {
const alice = privateKeyToAccount(generatePrivateKey());
bob = privateKeyToAccount(generatePrivateKey());
await fundAddress(bob.address);
const client = createClient({ transport, chain });
aliceSmartAccount = await toMetaMaskSmartAccount({
client,
implementation: Implementation.Hybrid,
deployParams: [alice.address, [], [], []],
deploySalt: '0x',
signatory: { account: alice },
});
const aliceCounter = await deployCounter(aliceSmartAccount.address);
aliceCounterContractAddress = aliceCounter.address;
const caveats = createCaveatBuilder(aliceSmartAccount.environment)
.addCaveat('allowedTargets', [aliceCounterContractAddress])
.addCaveat('allowedMethods', ['increment()'])
.addCaveat('valueLte', 0n);
const delegation = createDelegation({
to: bob.address,
from: aliceSmartAccount.address,
caveats,
});
signedDelegation = {
...delegation,
signature: await aliceSmartAccount.signDelegation({ delegation }),
};
permissionsContext = encodeDelegations([signedDelegation]);
});
/*
Alice creates a delegation from her SmartContractAccount to Bob's EOA, allowing Bob's account to call the increment function on Alice's counter contract.
Bob redeems the delegation using the experimental ERC-7710 redemption function.
*/
test('maincase: Bob redeems the delegation in order to increment() on the counter contract', async () => {
await deploySmartAccount(aliceSmartAccount);
const bobWalletClient = createWalletClient({
account: bob,
transport,
chain,
}).extend(erc7710WalletActions());
const { DelegationManager: delegationManager } =
aliceSmartAccount.environment;
const counterContract = getContract({
address: aliceCounterContractAddress,
abi: CounterMetadata.abi,
client: publicClient,
});
const countBefore = await counterContract.read.count();
expect(countBefore).toEqual(0n);
const transactionHash = await bobWalletClient.sendTransactionWithDelegation({
//todo: this should be typed such that account and chain doesn't need to be passed in
account: bob,
chain,
to: aliceCounterContractAddress,
data: encodeFunctionData({
abi: CounterMetadata.abi,
functionName: 'increment',
}),
permissionsContext,
delegationManager,
});
await publicClient.waitForTransactionReceipt({ hash: transactionHash });
const countAfter = (await counterContract.read.count()) as any as bigint;
expect(countAfter).toEqual(1n);
});
test('Bob redelegates to Carol, who redeems the delegation to call increment() on the counter contract', async () => {
await deploySmartAccount(aliceSmartAccount);
const carol = privateKeyToAccount(generatePrivateKey());
await fundAddress(carol.address);
const carolWalletClient = createWalletClient({
account: carol,
transport,
chain,
}).extend(erc7710WalletActions());
const counterContract = getContract({
address: aliceCounterContractAddress,
abi: CounterMetadata.abi,
client: publicClient,
});
const { DelegationManager: delegationManager } =
aliceSmartAccount.environment;
const redelegation = createDelegation({
to: carol.address,
from: bob.address,
parentDelegation: signedDelegation,
caveats: createCaveatBuilder(aliceSmartAccount.environment, {
allowEmptyCaveats: true,
}),
});
const signedRedelegation: Delegation = {
...redelegation,
signature: await signDelegation({
signer: createWalletClient({
account: bob,
transport,
chain,
}),
delegation: redelegation,
delegationManager,
chainId: chain.id,
}),
};
const countBefore = await counterContract.read.count();
expect(countBefore).toEqual(0n);
const redelegatedPermissionsContext = encodeDelegations([
signedRedelegation,
signedDelegation,
]);
const transactionHash = await carolWalletClient.sendTransactionWithDelegation(
{
//todo: this should be typed such that account and chain doesn't need to be passed in
account: carol,
chain,
to: aliceCounterContractAddress,
data: encodeFunctionData({
abi: CounterMetadata.abi,
functionName: 'increment',
}),
permissionsContext: redelegatedPermissionsContext,
delegationManager,
},
);
await publicClient.waitForTransactionReceipt({ hash: transactionHash });
const countAfter = await counterContract.read.count();
expect(countAfter).toEqual(1n);
});
test('Bob attempts to call the increment function directly', async () => {
const bobWalletClient = createWalletClient({
account: bob,
transport,
chain,
}).extend(erc7710WalletActions());
const counterContract = getContract({
address: aliceCounterContractAddress,
abi: CounterMetadata.abi,
client: publicClient,
});
const countBefore = await counterContract.read.count();
expect(countBefore).toEqual(0n);
const sendTransactionResponse = bobWalletClient.sendTransaction({
account: bob,
chain,
to: aliceCounterContractAddress,
data: encodeFunctionData({
abi: CounterMetadata.abi,
functionName: 'increment',
}),
});
await expect(sendTransactionResponse).rejects.toThrow(
'Ownable: caller is not the owner',
);
const countAfter = await counterContract.read.count();
expect(countAfter).toEqual(0n);
});
test('Bob sends a native value transaction with delegation', async () => {
await deploySmartAccount(aliceSmartAccount);
const allowance = 100n;
const recipient = randomAddress();
const { DelegationManager: delegationManager } =
aliceSmartAccount.environment;
const caveats = createCaveatBuilder(aliceSmartAccount.environment).addCaveat(
'nativeTokenTransferAmount',
allowance,
);
const delegation = createDelegation({
to: bob.address,
from: aliceSmartAccount.address,
caveats,
});
const signedDelegation = {
...delegation,
signature: await aliceSmartAccount.signDelegation({ delegation }),
};
const permissionsContext = encodeDelegations([signedDelegation]);
const bobWalletClient = createWalletClient({
account: bob,
transport,
chain,
}).extend(erc7710WalletActions());
await fundAddress(aliceSmartAccount.address, allowance);
const transactionHash = await bobWalletClient.sendTransactionWithDelegation({
account: bob,
chain,
to: recipient,
value: allowance,
permissionsContext,
delegationManager,
});
await publicClient.waitForTransactionReceipt({ hash: transactionHash });
const balance = await publicClient.getBalance({ address: recipient });
expect(balance).toEqual(allowance);
});