forked from alchemyplatform/aa-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modularAccount.ts
206 lines (197 loc) · 6.78 KB
/
modularAccount.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
import hre from "hardhat";
import {encodeFunctionData, encodePacked, getAbiItem, getContract} from "viem";
import {AccountConfig, AccountDataV06} from "../accounts";
import {MODULAR_ACCOUNT_ARTIFACTS} from "../artifacts/modularAccount";
import {getEntryPointV06} from "../utils/entryPoint";
async function accountFixture(): Promise<AccountDataV06> {
const [walletClient] = await hre.viem.getWalletClients();
const publicClient = await hre.viem.getPublicClient();
for (const {address, bytecode} of Object.values(MODULAR_ACCOUNT_ARTIFACTS)) {
await hre.network.provider.send("hardhat_setCode", [address, bytecode]);
}
const multiOwnerModularAccountFactory = getContract({
address: MODULAR_ACCOUNT_ARTIFACTS.MultiOwnerModularAccountFactory.address,
abi: MODULAR_ACCOUNT_ARTIFACTS.MultiOwnerModularAccountFactory.abi,
publicClient,
walletClient,
});
const tag =
"0x0000000000000000000000000000000000000000000000000000000000000000";
return {
entryPoint: getEntryPointV06({publicClient, walletClient}),
createAccount: async (salt, ownerAddress) => {
return await multiOwnerModularAccountFactory.write.createAccount([
salt,
[ownerAddress],
]);
},
getAccountAddress: async (salt, ownerAddress) => {
return await multiOwnerModularAccountFactory.read.getAddress([
salt,
[ownerAddress],
]);
},
getOwnerSignature: async (owner, userOp, entryPoint) => {
const userOpHash = await entryPoint.read.getUserOpHash([userOp]);
return await owner.signMessage({
message: {raw: userOpHash},
});
},
encodeUserOpExecute: (to, value, data) => {
return encodeFunctionData({
abi: [
getAbiItem({
abi: MODULAR_ACCOUNT_ARTIFACTS.UpgradeableModularAccount.abi,
name: "execute",
}),
],
args: [to, value, data],
});
},
encodeRuntimeExecute: async (to, value, data) => {
return encodeFunctionData({
abi: [
getAbiItem({
abi: MODULAR_ACCOUNT_ARTIFACTS.UpgradeableModularAccount.abi,
name: "execute",
}),
],
args: [to, value, data],
});
},
getDummySignature: (_userOp) => {
return "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
},
getInitCode: (salt, ownerAddress) => {
return encodePacked(
["address", "bytes"],
[
multiOwnerModularAccountFactory.address,
encodeFunctionData({
abi: [
getAbiItem({
abi: multiOwnerModularAccountFactory.abi,
name: "createAccount",
}),
],
args: [salt, [ownerAddress]],
}),
],
);
},
installSessionKeyPlugin: async (accountAddr, ownerAddress) => {
const account = getContract({
address: accountAddr,
abi: MODULAR_ACCOUNT_ARTIFACTS.UpgradeableModularAccount.abi,
publicClient,
walletClient,
});
return await account.write.installPlugin([
MODULAR_ACCOUNT_ARTIFACTS.SessionKeyPlugin.address,
"0x178289dadbf8d06fc233b07a6d6dbf251f100cd96cec78989f1ca58cb67b3ec5", // H(plugin manifest)
"0x0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", // abi.encode(new address[](0), new bytes32[](0), new bytes[][](0))
[
(MODULAR_ACCOUNT_ARTIFACTS.MultiOwnerPlugin.address +
"01") as `0x${string}`, //fn ref
(MODULAR_ACCOUNT_ARTIFACTS.MultiOwnerPlugin.address +
"00") as `0x${string}`, //fn ref
],
]);
},
addSessionKeyCalldata: (key, target, tokens, spendLimit, account) => {
// add permissions for tokens
const permissions = tokens.flatMap((token) => [
encodeFunctionData({
abi: [
getAbiItem({
abi: MODULAR_ACCOUNT_ARTIFACTS.ISessionKeyPermissionsUpdate.abi,
name: "updateAccessListAddressEntry",
}),
],
args: [token.address, true, false],
}),
encodeFunctionData({
abi: [
getAbiItem({
abi: MODULAR_ACCOUNT_ARTIFACTS.ISessionKeyPermissionsUpdate.abi,
name: "setERC20SpendLimit",
}),
],
args: [token.address, spendLimit, 0],
}),
]);
// add target
permissions.push(
encodeFunctionData({
abi: [
getAbiItem({
abi: MODULAR_ACCOUNT_ARTIFACTS.ISessionKeyPermissionsUpdate.abi,
name: "updateAccessListAddressEntry",
}),
],
args: [target, true, false],
}),
);
permissions.push(
encodeFunctionData({
abi: [
getAbiItem({
abi: MODULAR_ACCOUNT_ARTIFACTS.ISessionKeyPermissionsUpdate.abi,
name: "setNativeTokenSpendLimit",
}),
],
args: [spendLimit, 0],
}),
);
return encodeFunctionData({
abi: MODULAR_ACCOUNT_ARTIFACTS.SessionKeyPlugin.abi,
functionName: "addSessionKey",
args: [key, tag, permissions],
});
},
getSessionKeySignature: async (key, userOp, entryPoint) => {
const userOpHash = await entryPoint.read.getUserOpHash([userOp]);
return await key.signMessage({
message: {raw: userOpHash},
});
},
useSessionKeyERC20TransferCalldata: (token, key, to, amount) => {
const transferCall = encodeFunctionData({
abi: [
getAbiItem({
abi: token.abi,
name: "transfer",
}),
],
args: [to, amount],
});
return encodeFunctionData({
abi: [
getAbiItem({
abi: MODULAR_ACCOUNT_ARTIFACTS.SessionKeyPlugin.abi,
name: "executeWithSessionKey",
}),
],
args: [
[{target: token.address, value: BigInt(0), data: transferCall}],
key,
],
});
},
useSessionKeyNativeTokenTransferCalldata: (key, to, amount) => {
return encodeFunctionData({
abi: [
getAbiItem({
abi: MODULAR_ACCOUNT_ARTIFACTS.SessionKeyPlugin.abi,
name: "executeWithSessionKey",
}),
],
args: [[{target: to, value: amount, data: "0x"}], key],
});
},
};
}
export const modularAccount: AccountConfig = {
name: "Modular Account",
accountFixture,
};