-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtoMetaMaskSmartAccount.ts
More file actions
214 lines (187 loc) · 5.79 KB
/
Copy pathtoMetaMaskSmartAccount.ts
File metadata and controls
214 lines (187 loc) · 5.79 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
import {
EIP7702StatelessDeleGator,
HybridDeleGator,
MultiSigDeleGator,
} from '@metamask/delegation-abis';
import { type Address, type Hex } from 'viem';
import {
entryPoint07Abi,
toPackedUserOperation,
toSmartAccount,
} from 'viem/account-abstraction';
import { isEip7702StatelessDelegatedAccount } from './actions/isEip7702StatelessDelegatedAccount';
import { Implementation } from './constants';
import { getCounterfactualAccountData } from './counterfactualAccountData';
import {
SIGNABLE_DELEGATION_TYPED_DATA,
toDelegationStruct,
} from './delegation';
import { entryPointGetNonce as _getNonce } from './DelegationFramework/EntryPoint/read';
import { getDeleGatorEnvironment } from './delegatorEnvironment';
import { encodeCallsForCaller } from './encodeCalls';
import { resolveSignatory } from './signatory';
import type {
Call,
ToMetaMaskSmartAccountParameters,
ToMetaMaskSmartAccountReturnType,
SignDelegationParams,
SignUserOperationParams,
AbiByImplementation,
} from './types';
import { SIGNABLE_USER_OP_TYPED_DATA } from './userOp';
const ENTRYPOINT_VERSION = '0.7' as const;
/**
* Creates a MetaMask DeleGator smart account instance.
*
* @template TImplementation - The type of implementation, extending Implementation.
* @param params - The parameters for creating the smart account.
* @returns A promise that resolves to a MetaMask DeleGator smart account instance.
* @description
* This function sets up a MetaMask DeleGator smart account with the specified implementation.
* It handles both deployed accounts, and counterfactual accounts.
* A caller may specify a DeleGator environment, otherwise the environment will be inferred from the chain.
*/
export async function toMetaMaskSmartAccount<
TImplementation extends Implementation,
>(
params: ToMetaMaskSmartAccountParameters<TImplementation>,
): Promise<ToMetaMaskSmartAccountReturnType<TImplementation>> {
const {
client,
client: { chain },
implementation,
} = params;
if (!chain) {
throw new Error('Chain not specified');
}
const signatory = resolveSignatory({
implementation,
signatory: params.signatory,
});
const environment = params.environment ?? getDeleGatorEnvironment(chain.id);
let address: Address, factoryData: Hex | undefined;
if (params.address) {
factoryData = undefined;
address = params.address;
} else {
if (implementation === Implementation.Stateless7702) {
throw new Error('Stateless7702 does not support counterfactual accounts');
}
const accountData = await getCounterfactualAccountData({
factory: environment.SimpleFactory,
implementations: environment.implementations,
implementation,
deployParams: params.deployParams,
deploySalt: params.deploySalt,
});
address = accountData.address;
factoryData = accountData.factoryData;
}
const entryPoint = {
abi: entryPoint07Abi,
address: environment.EntryPoint,
version: ENTRYPOINT_VERSION,
} as const;
const { abi, contractName } = {
[Implementation.Hybrid]: {
contractName: 'HybridDeleGator',
abi: HybridDeleGator.abi,
},
[Implementation.MultiSig]: {
contractName: 'MultiSigDeleGator',
abi: MultiSigDeleGator.abi,
},
[Implementation.Stateless7702]: {
contractName: 'EIP7702StatelessDeleGator',
abi: EIP7702StatelessDeleGator.abi,
},
}[implementation] as {
contractName: string;
abi: AbiByImplementation[TImplementation];
};
const getFactoryArgs = async () => {
if (factoryData === undefined) {
throw new Error(
'Deploy params were not provided, so factory args cannot be inferred',
);
}
return {
factoryData,
factory: environment.SimpleFactory,
};
};
const signDelegation = async (delegationParams: SignDelegationParams) => {
const { delegation, chainId } = delegationParams;
const delegationStruct = toDelegationStruct({
...delegation,
signature: '0x',
});
const signature = signatory.signTypedData({
domain: {
chainId: chainId ?? chain.id,
name: 'DelegationManager',
version: '1',
verifyingContract: environment.DelegationManager,
},
types: SIGNABLE_DELEGATION_TYPED_DATA,
primaryType: 'Delegation',
message: delegationStruct,
});
return signature;
};
const signUserOperation = async (userOpParams: SignUserOperationParams) => {
const { chainId } = userOpParams;
const packedUserOp = toPackedUserOperation({
sender: address,
...userOpParams,
signature: '0x',
});
const signature = await signatory.signTypedData({
domain: {
chainId: chainId ?? chain.id,
name: contractName,
version: '1',
verifyingContract: address,
},
types: SIGNABLE_USER_OP_TYPED_DATA,
primaryType: 'PackedUserOperation',
message: { ...packedUserOp, entryPoint: entryPoint.address },
});
return signature;
};
const getAddress = async () => address;
const getNonce = async () =>
_getNonce({
client,
entryPoint: environment.EntryPoint,
contractAddress: address,
key: 0n,
});
const encodeCalls = async (calls: readonly Call[]) =>
encodeCallsForCaller(address, calls);
const smartAccount = await toSmartAccount({
abi,
client,
entryPoint,
environment,
getAddress,
getFactoryArgs,
encodeCalls,
getNonce,
signUserOperation,
signDelegation,
...signatory,
});
// For Stateless7702, override isDeployed to check specific delegation
if (implementation === Implementation.Stateless7702) {
return {
...smartAccount,
isDeployed: async () =>
isEip7702StatelessDelegatedAccount({
client,
accountAddress: address,
}),
};
}
return smartAccount;
}