forked from alchemyplatform/aa-benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightAccountV2.ts
104 lines (98 loc) · 2.97 KB
/
lightAccountV2.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
import hre from "hardhat";
import {
concatHex,
encodeFunctionData,
encodePacked,
getAbiItem,
getContract,
toHex,
} from "viem";
import {AccountConfig, AccountDataV07} from "../accounts";
import {LIGHT_ACCOUNT_V2_ARTIFACTS} from "../artifacts/lightAccountV2";
import {getEntryPointV07} from "../utils/entryPoint";
enum SignatureType {
EOA = 0,
CONTRACT = 1,
CONTRACT_WITH_ADDR = 2,
}
async function accountFixture(): Promise<AccountDataV07> {
const [walletClient] = await hre.viem.getWalletClients();
const publicClient = await hre.viem.getPublicClient();
for (const {address, bytecode} of Object.values(LIGHT_ACCOUNT_V2_ARTIFACTS)) {
await hre.network.provider.send("hardhat_setCode", [address, bytecode]);
}
const lightAccountV2Factory = getContract({
address: LIGHT_ACCOUNT_V2_ARTIFACTS.LightAccountFactory.address,
abi: LIGHT_ACCOUNT_V2_ARTIFACTS.LightAccountFactory.abi,
publicClient,
walletClient,
});
return {
entryPoint: getEntryPointV07({walletClient, publicClient}),
createAccount: async (salt, ownerAddress) => {
return await lightAccountV2Factory.write.createAccount([
ownerAddress,
salt,
]);
},
getAccountAddress: async (salt, ownerAddress) => {
return await lightAccountV2Factory.read.getAddress([ownerAddress, salt]);
},
getOwnerSignature: async (owner, userOp, entryPoint) => {
const userOpHash = await entryPoint.read.getUserOpHash([userOp]);
const signature = await owner.signMessage({
message: {raw: userOpHash},
});
return concatHex([toHex(SignatureType.EOA, {size: 1}), signature]);
},
encodeUserOpExecute: (to, value, data) => {
return encodeFunctionData({
abi: [
getAbiItem({
abi: LIGHT_ACCOUNT_V2_ARTIFACTS.LightAccount.abi,
name: "execute",
}),
],
args: [to, value, data],
});
},
encodeRuntimeExecute: async (to, value, data) => {
return encodeFunctionData({
abi: [
getAbiItem({
abi: LIGHT_ACCOUNT_V2_ARTIFACTS.LightAccount.abi,
name: "execute",
}),
],
args: [to, value, data],
});
},
getDummySignature: (_userOp) => {
return concatHex([
toHex(SignatureType.EOA, {size: 1}),
"0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c",
]);
},
getInitCode: (salt, ownerAddress) => {
return encodePacked(
["address", "bytes"],
[
lightAccountV2Factory.address,
encodeFunctionData({
abi: [
getAbiItem({
abi: lightAccountV2Factory.abi,
name: "createAccount",
}),
],
args: [ownerAddress, salt],
}),
],
);
},
};
}
export const lightAccountV2: AccountConfig = {
name: "Light Account v2",
accountFixture,
};