-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcoinbaseSmartWallet.ts
150 lines (143 loc) · 4.38 KB
/
coinbaseSmartWallet.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
import hre from "hardhat";
import {
encodeAbiParameters,
encodeFunctionData,
encodePacked,
getAbiItem,
getContract,
} from "viem";
import {SignReturnType, sign} from "viem/accounts";
import {AccountConfig, AccountDataV06} from "../accounts";
import {COINBASE_SMART_WALLET_ARTIFACTS} from "../artifacts/coinbaseSmartWallet";
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(
COINBASE_SMART_WALLET_ARTIFACTS,
)) {
await hre.network.provider.send("hardhat_setCode", [address, bytecode]);
}
const coinbaseSmartWalletFactory = getContract({
address: COINBASE_SMART_WALLET_ARTIFACTS.CoinbaseSmartWalletFactory.address,
abi: COINBASE_SMART_WALLET_ARTIFACTS.CoinbaseSmartWalletFactory.abi,
publicClient,
walletClient,
});
return {
entryPoint: getEntryPointV06({publicClient, walletClient}),
createAccount: async (salt, ownerAddress) => {
const abiEncodedOwner = encodeAbiParameters(
[{type: "address"}],
[ownerAddress],
);
return await coinbaseSmartWalletFactory.write.createAccount([
[abiEncodedOwner],
salt,
]);
},
getAccountAddress: async (salt, ownerAddress) => {
const abiEncodedOwner = encodeAbiParameters(
[{type: "address"}],
[ownerAddress],
);
return await coinbaseSmartWalletFactory.read.getAddress([
[abiEncodedOwner],
salt,
]);
},
getOwnerSignature: async (_owner, userOp, entryPoint) => {
const userOpHash = await entryPoint.read.getUserOpHash([userOp]);
// Key of owner account.
const privateKey =
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
const signature = await sign({hash: userOpHash, privateKey});
return buildSignatureWrapperForEOA({signature, ownerIndex: 0n});
},
encodeUserOpExecute: (to, value, data) => {
return encodeFunctionData({
abi: [
getAbiItem({
abi: COINBASE_SMART_WALLET_ARTIFACTS.CoinbaseSmartWallet.abi,
name: "execute",
}),
],
args: [to, value, data],
});
},
encodeRuntimeExecute: async (to, value, data) => {
return encodeFunctionData({
abi: [
getAbiItem({
abi: COINBASE_SMART_WALLET_ARTIFACTS.CoinbaseSmartWallet.abi,
name: "execute",
}),
],
args: [to, value, data],
});
},
getDummySignature: (_userOp) => {
return "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000041000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
},
getInitCode: (salt, ownerAddress) => {
const abiEncodedOwner = encodeAbiParameters(
[{type: "address"}],
[ownerAddress],
);
return encodePacked(
["address", "bytes"],
[
coinbaseSmartWalletFactory.address,
encodeFunctionData({
abi: [
getAbiItem({
abi: coinbaseSmartWalletFactory.abi,
name: "createAccount",
}),
],
args: [[abiEncodedOwner], salt],
}),
],
);
},
};
}
export const coinbaseSmartWallet: AccountConfig = {
name: "Coinbase Smart Wallet",
accountFixture,
};
const SignatureWrapperStruct = {
components: [
{
name: "ownerIndex",
type: "uint8",
},
{
name: "signatureData",
type: "bytes",
},
],
name: "SignatureWrapper",
type: "tuple",
};
function buildSignatureWrapperForEOA({
signature,
ownerIndex,
}: {
signature: SignReturnType;
ownerIndex: bigint;
}) {
const signatureData = encodePacked(
["bytes32", "bytes32", "uint8"],
[signature.r, signature.s, parseInt(signature.v.toString())],
);
return encodeAbiParameters(
[SignatureWrapperStruct],
[
{
ownerIndex,
signatureData,
},
],
);
}