@aa-sdk/core 4.0.0-alpha.5 → 4.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (239) hide show
  1. package/dist/cjs/account/smartContractAccount.d.ts +9 -3
  2. package/dist/cjs/account/smartContractAccount.js +11 -6
  3. package/dist/cjs/account/smartContractAccount.js.map +1 -1
  4. package/dist/cjs/actions/smartAccount/buildUserOperationFromTx.js.map +1 -1
  5. package/dist/cjs/actions/smartAccount/buildUserOperationFromTxs.js.map +1 -1
  6. package/dist/cjs/actions/smartAccount/checkGasSponsorshipEligibility.js.map +1 -1
  7. package/dist/cjs/actions/smartAccount/estimateUserOperationGas.js.map +1 -1
  8. package/dist/cjs/actions/smartAccount/internal/initUserOperation.js.map +1 -1
  9. package/dist/cjs/actions/smartAccount/internal/runMiddlewareStack.js.map +1 -1
  10. package/dist/cjs/actions/smartAccount/internal/sendUserOperation.js.map +1 -1
  11. package/dist/cjs/actions/smartAccount/sendUserOperation.js.map +1 -1
  12. package/dist/cjs/client/bundlerClient.js.map +1 -1
  13. package/dist/cjs/client/decorators/bundlerClient.js.map +1 -1
  14. package/dist/cjs/client/isSmartAccountClient.js.map +1 -1
  15. package/dist/cjs/client/types.d.ts +2 -7
  16. package/dist/cjs/client/types.js.map +1 -1
  17. package/dist/cjs/errors/account.js.map +1 -1
  18. package/dist/cjs/errors/client.js.map +1 -1
  19. package/dist/cjs/errors/entrypoint.js.map +1 -1
  20. package/dist/cjs/errors/signer.js.map +1 -1
  21. package/dist/cjs/errors/transaction.js.map +1 -1
  22. package/dist/cjs/errors/useroperation.js.map +1 -1
  23. package/dist/cjs/index.d.ts +3 -5
  24. package/dist/cjs/index.js +6 -9
  25. package/dist/cjs/index.js.map +1 -1
  26. package/dist/cjs/logger.js.map +1 -1
  27. package/dist/cjs/middleware/actions.js +2 -18
  28. package/dist/cjs/middleware/actions.js.map +1 -1
  29. package/dist/cjs/middleware/defaults/gasEstimator.js +3 -2
  30. package/dist/cjs/middleware/defaults/gasEstimator.js.map +1 -1
  31. package/dist/cjs/middleware/erc7677middleware.d.ts +40 -0
  32. package/dist/cjs/middleware/erc7677middleware.js +77 -0
  33. package/dist/cjs/middleware/erc7677middleware.js.map +1 -0
  34. package/dist/cjs/middleware/noopMiddleware.d.ts +2 -1
  35. package/dist/cjs/middleware/noopMiddleware.js.map +1 -1
  36. package/dist/cjs/middleware/types.d.ts +3 -2
  37. package/dist/cjs/middleware/types.js.map +1 -1
  38. package/dist/cjs/signer/local-account.d.ts +46 -46
  39. package/dist/cjs/signer/local-account.js.map +1 -1
  40. package/dist/cjs/signer/wallet-client.d.ts +46 -46
  41. package/dist/cjs/signer/wallet-client.js.map +1 -1
  42. package/dist/cjs/transport/split.js.map +1 -1
  43. package/dist/cjs/utils/bigint.js.map +1 -1
  44. package/dist/cjs/utils/bytes.js.map +1 -1
  45. package/dist/cjs/utils/defaults.d.ts +1 -3
  46. package/dist/cjs/utils/defaults.js +1 -41
  47. package/dist/cjs/utils/defaults.js.map +1 -1
  48. package/dist/cjs/utils/index.js.map +1 -1
  49. package/dist/cjs/utils/types.js.map +1 -1
  50. package/dist/cjs/utils/userop.js.map +1 -1
  51. package/dist/cjs/version.d.ts +1 -1
  52. package/dist/cjs/version.js +1 -1
  53. package/dist/cjs/version.js.map +1 -1
  54. package/dist/esm/account/smartContractAccount.d.ts +9 -3
  55. package/dist/esm/account/smartContractAccount.js +6 -1
  56. package/dist/esm/account/smartContractAccount.js.map +1 -1
  57. package/dist/esm/actions/smartAccount/buildUserOperationFromTx.js.map +1 -1
  58. package/dist/esm/actions/smartAccount/buildUserOperationFromTxs.js.map +1 -1
  59. package/dist/esm/actions/smartAccount/checkGasSponsorshipEligibility.js.map +1 -1
  60. package/dist/esm/actions/smartAccount/estimateUserOperationGas.js.map +1 -1
  61. package/dist/esm/actions/smartAccount/internal/initUserOperation.js.map +1 -1
  62. package/dist/esm/actions/smartAccount/internal/runMiddlewareStack.js.map +1 -1
  63. package/dist/esm/actions/smartAccount/internal/sendUserOperation.js.map +1 -1
  64. package/dist/esm/actions/smartAccount/sendUserOperation.js.map +1 -1
  65. package/dist/esm/client/bundlerClient.js.map +1 -1
  66. package/dist/esm/client/decorators/bundlerClient.js.map +1 -1
  67. package/dist/esm/client/isSmartAccountClient.js.map +1 -1
  68. package/dist/esm/client/types.d.ts +2 -7
  69. package/dist/esm/client/types.js.map +1 -1
  70. package/dist/esm/errors/account.js.map +1 -1
  71. package/dist/esm/errors/client.js.map +1 -1
  72. package/dist/esm/errors/entrypoint.js.map +1 -1
  73. package/dist/esm/errors/signer.js.map +1 -1
  74. package/dist/esm/errors/transaction.js.map +1 -1
  75. package/dist/esm/errors/useroperation.js.map +1 -1
  76. package/dist/esm/index.d.ts +3 -5
  77. package/dist/esm/index.js +2 -3
  78. package/dist/esm/index.js.map +1 -1
  79. package/dist/esm/logger.js.map +1 -1
  80. package/dist/esm/middleware/actions.js +3 -18
  81. package/dist/esm/middleware/actions.js.map +1 -1
  82. package/dist/esm/middleware/defaults/gasEstimator.js +3 -2
  83. package/dist/esm/middleware/defaults/gasEstimator.js.map +1 -1
  84. package/dist/esm/middleware/erc7677middleware.d.ts +40 -0
  85. package/dist/esm/middleware/erc7677middleware.js +73 -0
  86. package/dist/esm/middleware/erc7677middleware.js.map +1 -0
  87. package/dist/esm/middleware/noopMiddleware.d.ts +2 -1
  88. package/dist/esm/middleware/noopMiddleware.js.map +1 -1
  89. package/dist/esm/middleware/types.d.ts +3 -2
  90. package/dist/esm/middleware/types.js.map +1 -1
  91. package/dist/esm/signer/local-account.d.ts +46 -46
  92. package/dist/esm/signer/local-account.js.map +1 -1
  93. package/dist/esm/signer/wallet-client.d.ts +46 -46
  94. package/dist/esm/signer/wallet-client.js.map +1 -1
  95. package/dist/esm/transport/split.js.map +1 -1
  96. package/dist/esm/utils/bigint.js.map +1 -1
  97. package/dist/esm/utils/bytes.js.map +1 -1
  98. package/dist/esm/utils/defaults.d.ts +1 -3
  99. package/dist/esm/utils/defaults.js +1 -40
  100. package/dist/esm/utils/defaults.js.map +1 -1
  101. package/dist/esm/utils/index.js.map +1 -1
  102. package/dist/esm/utils/types.js.map +1 -1
  103. package/dist/esm/utils/userop.js.map +1 -1
  104. package/dist/esm/version.d.ts +1 -1
  105. package/dist/esm/version.js +1 -1
  106. package/dist/esm/version.js.map +1 -1
  107. package/dist/types/account/smartContractAccount.d.ts +17 -11
  108. package/dist/types/account/smartContractAccount.d.ts.map +1 -1
  109. package/dist/types/actions/smartAccount/buildUserOperationFromTx.d.ts +33 -33
  110. package/dist/types/actions/smartAccount/buildUserOperationFromTxs.d.ts +43 -59
  111. package/dist/types/actions/smartAccount/buildUserOperationFromTxs.d.ts.map +1 -1
  112. package/dist/types/actions/smartAccount/checkGasSponsorshipEligibility.d.ts +18 -18
  113. package/dist/types/actions/smartAccount/estimateUserOperationGas.d.ts +3 -3
  114. package/dist/types/actions/smartAccount/internal/initUserOperation.d.ts +3 -4
  115. package/dist/types/actions/smartAccount/internal/initUserOperation.d.ts.map +1 -1
  116. package/dist/types/actions/smartAccount/internal/runMiddlewareStack.d.ts +0 -14
  117. package/dist/types/actions/smartAccount/internal/runMiddlewareStack.d.ts.map +1 -1
  118. package/dist/types/actions/smartAccount/internal/sendUserOperation.d.ts +0 -7
  119. package/dist/types/actions/smartAccount/internal/sendUserOperation.d.ts.map +1 -1
  120. package/dist/types/actions/smartAccount/sendUserOperation.d.ts +17 -3
  121. package/dist/types/actions/smartAccount/sendUserOperation.d.ts.map +1 -1
  122. package/dist/types/client/bundlerClient.d.ts +2 -2
  123. package/dist/types/client/decorators/bundlerClient.d.ts +8 -8
  124. package/dist/types/client/isSmartAccountClient.d.ts +4 -4
  125. package/dist/types/client/types.d.ts +2 -7
  126. package/dist/types/client/types.d.ts.map +1 -1
  127. package/dist/types/errors/account.d.ts +53 -4
  128. package/dist/types/errors/account.d.ts.map +1 -1
  129. package/dist/types/errors/client.d.ts +15 -0
  130. package/dist/types/errors/client.d.ts.map +1 -1
  131. package/dist/types/errors/entrypoint.d.ts +12 -0
  132. package/dist/types/errors/entrypoint.d.ts.map +1 -1
  133. package/dist/types/errors/signer.d.ts +5 -0
  134. package/dist/types/errors/signer.d.ts.map +1 -1
  135. package/dist/types/errors/transaction.d.ts +8 -1
  136. package/dist/types/errors/transaction.d.ts.map +1 -1
  137. package/dist/types/errors/useroperation.d.ts +5 -5
  138. package/dist/types/errors/useroperation.d.ts.map +1 -1
  139. package/dist/types/index.d.ts +3 -5
  140. package/dist/types/index.d.ts.map +1 -1
  141. package/dist/types/logger.d.ts +88 -0
  142. package/dist/types/logger.d.ts.map +1 -1
  143. package/dist/types/middleware/actions.d.ts +3 -3
  144. package/dist/types/middleware/actions.d.ts.map +1 -1
  145. package/dist/types/middleware/defaults/gasEstimator.d.ts +2 -2
  146. package/dist/types/middleware/defaults/gasEstimator.d.ts.map +1 -1
  147. package/dist/types/middleware/erc7677middleware.d.ts +62 -0
  148. package/dist/types/middleware/erc7677middleware.d.ts.map +1 -0
  149. package/dist/types/middleware/noopMiddleware.d.ts +4 -4
  150. package/dist/types/middleware/noopMiddleware.d.ts.map +1 -1
  151. package/dist/types/middleware/types.d.ts +3 -2
  152. package/dist/types/middleware/types.d.ts.map +1 -1
  153. package/dist/types/signer/local-account.d.ts +139 -47
  154. package/dist/types/signer/local-account.d.ts.map +1 -1
  155. package/dist/types/signer/wallet-client.d.ts +135 -46
  156. package/dist/types/signer/wallet-client.d.ts.map +1 -1
  157. package/dist/types/transport/split.d.ts +2 -2
  158. package/dist/types/utils/bigint.d.ts +14 -14
  159. package/dist/types/utils/bigint.d.ts.map +1 -1
  160. package/dist/types/utils/bytes.d.ts +5 -5
  161. package/dist/types/utils/defaults.d.ts +1 -11
  162. package/dist/types/utils/defaults.d.ts.map +1 -1
  163. package/dist/types/utils/index.d.ts +12 -12
  164. package/dist/types/utils/types.d.ts +2 -2
  165. package/dist/types/utils/userop.d.ts +33 -33
  166. package/dist/types/version.d.ts +1 -1
  167. package/package.json +3 -2
  168. package/src/account/smartContractAccount.ts +19 -12
  169. package/src/actions/smartAccount/buildUserOperationFromTx.ts +35 -35
  170. package/src/actions/smartAccount/buildUserOperationFromTxs.ts +44 -60
  171. package/src/actions/smartAccount/checkGasSponsorshipEligibility.ts +19 -19
  172. package/src/actions/smartAccount/estimateUserOperationGas.ts +3 -3
  173. package/src/actions/smartAccount/internal/initUserOperation.ts +3 -4
  174. package/src/actions/smartAccount/internal/runMiddlewareStack.ts +0 -22
  175. package/src/actions/smartAccount/internal/sendUserOperation.ts +0 -7
  176. package/src/actions/smartAccount/sendUserOperation.ts +17 -3
  177. package/src/client/bundlerClient.ts +2 -2
  178. package/src/client/decorators/bundlerClient.ts +8 -8
  179. package/src/client/isSmartAccountClient.ts +4 -4
  180. package/src/client/types.ts +2 -15
  181. package/src/errors/account.ts +64 -4
  182. package/src/errors/client.ts +18 -0
  183. package/src/errors/entrypoint.ts +12 -0
  184. package/src/errors/signer.ts +6 -0
  185. package/src/errors/transaction.ts +10 -1
  186. package/src/errors/useroperation.ts +5 -6
  187. package/src/index.ts +2 -8
  188. package/src/logger.ts +88 -0
  189. package/src/middleware/actions.ts +6 -27
  190. package/src/middleware/defaults/gasEstimator.ts +8 -5
  191. package/src/middleware/erc7677middleware.ts +201 -0
  192. package/src/middleware/noopMiddleware.ts +6 -4
  193. package/src/middleware/types.ts +16 -7
  194. package/src/signer/local-account.ts +94 -2
  195. package/src/signer/wallet-client.ts +89 -0
  196. package/src/transport/split.ts +2 -2
  197. package/src/utils/bigint.ts +15 -15
  198. package/src/utils/bytes.ts +5 -5
  199. package/src/utils/defaults.ts +2 -74
  200. package/src/utils/index.ts +12 -12
  201. package/src/utils/types.ts +2 -2
  202. package/src/utils/userop.ts +33 -33
  203. package/src/version.ts +1 -1
  204. package/dist/cjs/account/base.d.ts +0 -49
  205. package/dist/cjs/account/base.js +0 -257
  206. package/dist/cjs/account/base.js.map +0 -1
  207. package/dist/cjs/account/schema.d.ts +0 -100
  208. package/dist/cjs/account/schema.js +0 -43
  209. package/dist/cjs/account/schema.js.map +0 -1
  210. package/dist/cjs/account/simple.d.ts +0 -14
  211. package/dist/cjs/account/simple.js +0 -119
  212. package/dist/cjs/account/simple.js.map +0 -1
  213. package/dist/cjs/account/types.d.ts +0 -30
  214. package/dist/cjs/account/types.js +0 -3
  215. package/dist/cjs/account/types.js.map +0 -1
  216. package/dist/esm/account/base.d.ts +0 -49
  217. package/dist/esm/account/base.js +0 -253
  218. package/dist/esm/account/base.js.map +0 -1
  219. package/dist/esm/account/schema.d.ts +0 -100
  220. package/dist/esm/account/schema.js +0 -35
  221. package/dist/esm/account/schema.js.map +0 -1
  222. package/dist/esm/account/simple.d.ts +0 -14
  223. package/dist/esm/account/simple.js +0 -115
  224. package/dist/esm/account/simple.js.map +0 -1
  225. package/dist/esm/account/types.d.ts +0 -30
  226. package/dist/esm/account/types.js +0 -2
  227. package/dist/esm/account/types.js.map +0 -1
  228. package/dist/types/account/base.d.ts +0 -128
  229. package/dist/types/account/base.d.ts.map +0 -1
  230. package/dist/types/account/schema.d.ts +0 -101
  231. package/dist/types/account/schema.d.ts.map +0 -1
  232. package/dist/types/account/simple.d.ts +0 -15
  233. package/dist/types/account/simple.d.ts.map +0 -1
  234. package/dist/types/account/types.d.ts +0 -144
  235. package/dist/types/account/types.d.ts.map +0 -1
  236. package/src/account/base.ts +0 -415
  237. package/src/account/schema.ts +0 -51
  238. package/src/account/simple.ts +0 -221
  239. package/src/account/types.ts +0 -184
@@ -1,415 +0,0 @@
1
- import type { Address } from "abitype";
2
- import {
3
- getContract,
4
- http,
5
- trim,
6
- type GetContractReturnType,
7
- type Hash,
8
- type Hex,
9
- type HttpTransport,
10
- type PublicClient,
11
- type Transport,
12
- } from "viem";
13
- import { EntryPointAbi_v6 as EntryPointAbi } from "../abis/EntryPointAbi_v6.js";
14
- import {
15
- createBundlerClient,
16
- type BundlerClient,
17
- } from "../client/bundlerClient.js";
18
- import { getEntryPoint } from "../entrypoint/index.js";
19
- import {
20
- BatchExecutionNotSupportedError,
21
- FailedToGetStorageSlotError,
22
- GetCounterFactualAddressError,
23
- UpgradeToAndCallNotSupportedError,
24
- } from "../errors/account.js";
25
- import { InvalidRpcUrlError } from "../errors/client.js";
26
- import { Logger } from "../logger.js";
27
- import type { SmartAccountSigner } from "../signer/types.js";
28
- import { wrapSignatureWith6492 } from "../signer/utils.js";
29
- import type { BatchUserOperationCallData, NullAddress } from "../types.js";
30
- import { createBaseSmartAccountParamsSchema } from "./schema.js";
31
- import type {
32
- BaseSmartAccountParams,
33
- ISmartContractAccount,
34
- SignTypedDataParams,
35
- } from "./types.js";
36
-
37
- export enum DeploymentState {
38
- UNDEFINED = "0x0",
39
- NOT_DEPLOYED = "0x1",
40
- DEPLOYED = "0x2",
41
- }
42
-
43
- /**
44
- * @deprecated use `toSmartContractAccount` instead for creating SmartAccountInstances
45
- */
46
- export abstract class BaseSmartContractAccount<
47
- TTransport extends Transport = Transport,
48
- TSigner extends SmartAccountSigner = SmartAccountSigner
49
- > implements ISmartContractAccount<TTransport, TSigner>
50
- {
51
- protected factoryAddress: Address;
52
- protected deploymentState: DeploymentState = DeploymentState.UNDEFINED;
53
- protected accountAddress?: Address;
54
- protected accountInitCode?: Hex;
55
- protected signer: TSigner;
56
- protected entryPoint: GetContractReturnType<
57
- typeof EntryPointAbi,
58
- PublicClient
59
- >;
60
- protected entryPointAddress: Address;
61
- readonly rpcProvider:
62
- | BundlerClient<TTransport>
63
- | BundlerClient<HttpTransport>;
64
-
65
- constructor(params_: BaseSmartAccountParams<TTransport, TSigner>) {
66
- const params = createBaseSmartAccountParamsSchema<
67
- TTransport,
68
- TSigner
69
- >().parse(params_);
70
-
71
- this.entryPointAddress =
72
- params.entryPointAddress ?? getEntryPoint(params.chain).address;
73
-
74
- const rpcUrl =
75
- typeof params.rpcClient === "string"
76
- ? params.rpcClient
77
- : params.rpcClient.transport.type === "http"
78
- ? (
79
- params.rpcClient.transport as ReturnType<HttpTransport>["config"] &
80
- ReturnType<HttpTransport>["value"]
81
- ).url || params.chain.rpcUrls.default.http[0]
82
- : undefined;
83
-
84
- const fetchOptions =
85
- typeof params.rpcClient === "string"
86
- ? undefined
87
- : params.rpcClient.transport.type === "http"
88
- ? (
89
- params.rpcClient.transport as ReturnType<HttpTransport>["config"] &
90
- ReturnType<HttpTransport>["value"]
91
- ).fetchOptions
92
- : undefined;
93
-
94
- this.rpcProvider = rpcUrl
95
- ? createBundlerClient({
96
- chain: params.chain,
97
- transport: http(rpcUrl, {
98
- fetchOptions: {
99
- ...fetchOptions,
100
- headers: {
101
- ...fetchOptions?.headers,
102
- ...(rpcUrl.toLowerCase().indexOf("alchemy") > -1
103
- ? {
104
- "Alchemy-Aa-Sdk-Signer":
105
- params.signer?.signerType || "unknown",
106
- "Alchemy-Aa-Sdk-Factory-Address": params.factoryAddress,
107
- }
108
- : undefined),
109
- },
110
- },
111
- }),
112
- })
113
- : (params.rpcClient as BundlerClient<TTransport>);
114
-
115
- this.accountAddress = params.accountAddress;
116
- this.factoryAddress = params.factoryAddress;
117
- this.signer = params.signer as TSigner;
118
- this.accountInitCode = params.initCode as Hex;
119
-
120
- this.entryPoint = getContract({
121
- address: this.entryPointAddress,
122
- abi: EntryPointAbi,
123
- client: this.rpcProvider as PublicClient,
124
- });
125
- }
126
-
127
- //#region abstract-methods
128
-
129
- /**
130
- * This method should return a signature that will not `revert` during validation.
131
- * It does not have to pass validation, just not cause the contract to revert.
132
- * This is required for gas estimation so that the gas estimate are accurate.
133
- *
134
- */
135
- abstract getDummySignature(): Hex | Promise<Hex>;
136
-
137
- /**
138
- * this method should return the abi encoded function data for a call to your contract's `execute` method
139
- *
140
- * @param target -- equivalent to `to` in a normal transaction
141
- * @param value -- equivalent to `value` in a normal transaction
142
- * @param data -- equivalent to `data` in a normal transaction
143
- * @returns abi encoded function data for a call to your contract's `execute` method
144
- */
145
- abstract encodeExecute(
146
- target: string,
147
- value: bigint,
148
- data: string
149
- ): Promise<Hash>;
150
-
151
- /**
152
- * this should return an ERC-191 compliant message and is used to sign UO Hashes
153
- *
154
- * @param msg -- the message to sign
155
- */
156
- abstract signMessage(msg: string | Uint8Array): Promise<Hash>;
157
-
158
- /**
159
- * this should return the init code that will be used to create an account if one does not exist.
160
- * This is the concatenation of the account's factory address and the abi encoded function data of the account factory's `createAccount` method.
161
- * https://github.com/eth-infinitism/account-abstraction/blob/abff2aca61a8f0934e533d0d352978055fddbd96/contracts/core/SenderCreator.sol#L12
162
- */
163
- protected abstract getAccountInitCode(): Promise<Hash>;
164
-
165
- //#endregion abstract-methods
166
-
167
- //#region optional-methods
168
-
169
- /**
170
- * If your account handles 1271 signatures of personal_sign differently
171
- * than it does UserOperations, you can implement two different approaches to signing
172
- *
173
- * @param uoHash -- The hash of the UserOperation to sign
174
- * @returns the signature of the UserOperation
175
- */
176
- async signUserOperationHash(uoHash: Hash): Promise<Hash> {
177
- return this.signMessage(uoHash);
178
- }
179
-
180
- /**
181
- * If your contract supports signing and verifying typed data,
182
- * you should implement this method.
183
- *
184
- * @param _params -- Typed Data params to sign
185
- */
186
- async signTypedData(_params: SignTypedDataParams): Promise<`0x${string}`> {
187
- throw new Error("signTypedData not supported");
188
- }
189
-
190
- /**
191
- * This method should wrap the result of `signMessage` as per
192
- * [EIP-6492](https://eips.ethereum.org/EIPS/eip-6492)
193
- *
194
- * @param msg -- the message to sign
195
- * @returns the signature wrapped in 6492 format
196
- */
197
- async signMessageWith6492(msg: string | Uint8Array): Promise<`0x${string}`> {
198
- const [isDeployed, signature] = await Promise.all([
199
- this.isAccountDeployed(),
200
- this.signMessage(msg),
201
- ]);
202
-
203
- return this.create6492Signature(isDeployed, signature);
204
- }
205
-
206
- /**
207
- * Similar to the signMessageWith6492 method above,
208
- * this method should wrap the result of `signTypedData` as per
209
- * [EIP-6492](https://eips.ethereum.org/EIPS/eip-6492)
210
- *
211
- * @param params -- Typed Data params to sign
212
- * @returns the signature wrapped in 6492 format
213
- */
214
- async signTypedDataWith6492(
215
- params: SignTypedDataParams
216
- ): Promise<`0x${string}`> {
217
- const [isDeployed, signature] = await Promise.all([
218
- this.isAccountDeployed(),
219
- this.signTypedData(params),
220
- ]);
221
-
222
- return this.create6492Signature(isDeployed, signature);
223
- }
224
-
225
- /**
226
- * Not all contracts support batch execution.
227
- * If your contract does, this method should encode a list of
228
- * transactions into the call data that will be passed to your
229
- * contract's batch execution method.
230
- *
231
- * @param _txs -- the transactions to batch execute
232
- */
233
- async encodeBatchExecute(
234
- _txs: BatchUserOperationCallData
235
- ): Promise<`0x${string}`> {
236
- throw new BatchExecutionNotSupportedError("BaseAccount");
237
- }
238
-
239
- /**
240
- * If your contract supports UUPS, you can implement this method which can be
241
- * used to upgrade the implementation of the account.
242
- *
243
- * @param _upgradeToImplAddress -- the implementation address of the contract you want to upgrade to
244
- * @param _upgradeToInitData -- the initialization data required by that account
245
- */
246
- encodeUpgradeToAndCall = async (
247
- _upgradeToImplAddress: Address,
248
- _upgradeToInitData: Hex
249
- ): Promise<Hex> => {
250
- throw new UpgradeToAndCallNotSupportedError("BaseAccount");
251
- };
252
- //#endregion optional-methods
253
-
254
- // Extra implementations
255
- async getNonce(): Promise<bigint> {
256
- if (!(await this.isAccountDeployed())) {
257
- return 0n;
258
- }
259
- const address = await this.getAddress();
260
- return this.entryPoint.read.getNonce([address, BigInt(0)]);
261
- }
262
-
263
- async getInitCode(): Promise<Hex> {
264
- if (this.deploymentState === DeploymentState.DEPLOYED) {
265
- return "0x";
266
- }
267
-
268
- const contractCode = await this.rpcProvider.getBytecode({
269
- address: await this.getAddress(),
270
- });
271
-
272
- if ((contractCode?.length ?? 0) > 2) {
273
- this.deploymentState = DeploymentState.DEPLOYED;
274
- return "0x";
275
- } else {
276
- this.deploymentState = DeploymentState.NOT_DEPLOYED;
277
- }
278
-
279
- return this._getAccountInitCode();
280
- }
281
-
282
- async getAddress(): Promise<Address> {
283
- if (!this.accountAddress) {
284
- const initCode = await this._getAccountInitCode();
285
- Logger.verbose(
286
- "[BaseSmartContractAccount](getAddress) initCode: ",
287
- initCode
288
- );
289
- try {
290
- await this.entryPoint.simulate.getSenderAddress([initCode]);
291
- } catch (err: any) {
292
- Logger.verbose(
293
- "[BaseSmartContractAccount](getAddress) getSenderAddress err: ",
294
- err
295
- );
296
-
297
- if (err.cause?.data?.errorName === "SenderAddressResult") {
298
- this.accountAddress = err.cause.data.args[0] as Address;
299
- Logger.verbose(
300
- "[BaseSmartContractAccount](getAddress) entryPoint.getSenderAddress result:",
301
- this.accountAddress
302
- );
303
- return this.accountAddress;
304
- }
305
-
306
- if (err.details === "Invalid URL") {
307
- throw new InvalidRpcUrlError();
308
- }
309
- }
310
-
311
- throw new GetCounterFactualAddressError();
312
- }
313
-
314
- return this.accountAddress;
315
- }
316
-
317
- extend = <R>(fn: (self: this) => R): this & R => {
318
- const extended = fn(this) as any;
319
- // this should make it so extensions can't overwrite the base methods
320
- for (const key in this) {
321
- delete extended[key];
322
- }
323
- return Object.assign(this, extended);
324
- };
325
-
326
- getSigner(): TSigner {
327
- return this.signer;
328
- }
329
-
330
- getFactoryAddress(): Address {
331
- return this.factoryAddress;
332
- }
333
-
334
- getEntryPointAddress(): Address {
335
- return this.entryPointAddress;
336
- }
337
-
338
- async isAccountDeployed(): Promise<boolean> {
339
- return (await this.getDeploymentState()) === DeploymentState.DEPLOYED;
340
- }
341
-
342
- async getDeploymentState(): Promise<DeploymentState> {
343
- if (this.deploymentState === DeploymentState.UNDEFINED) {
344
- const initCode = await this.getInitCode();
345
- return initCode === "0x"
346
- ? DeploymentState.DEPLOYED
347
- : DeploymentState.NOT_DEPLOYED;
348
- } else {
349
- return this.deploymentState;
350
- }
351
- }
352
-
353
- /**
354
- * https://eips.ethereum.org/EIPS/eip-4337#first-time-account-creation
355
- * The initCode field (if non-zero length) is parsed as a 20-byte address,
356
- * followed by calldata to pass to this address.
357
- * The factory address is the first 40 char after the 0x, and the callData is the rest.
358
- *
359
- * @returns [factoryAddress, factoryCalldata]
360
- */
361
- protected async parseFactoryAddressFromAccountInitCode(): Promise<
362
- [Address, Hex]
363
- > {
364
- const initCode = await this._getAccountInitCode();
365
- const factoryAddress: Address = `0x${initCode.substring(2, 42)}`;
366
- const factoryCalldata: Hex = `0x${initCode.substring(42)}`;
367
- return [factoryAddress, factoryCalldata];
368
- }
369
-
370
- protected async getImplementationAddress(): Promise<NullAddress | Address> {
371
- const accountAddress = await this.getAddress();
372
-
373
- const storage = await this.rpcProvider.getStorageAt({
374
- address: accountAddress,
375
- // This is the default slot for the implementation address for Proxies
376
- slot: "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc",
377
- });
378
-
379
- if (storage == null) {
380
- throw new FailedToGetStorageSlotError(
381
- "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc",
382
- "Proxy Implementation Address"
383
- );
384
- }
385
-
386
- return trim(storage);
387
- }
388
-
389
- private async _getAccountInitCode(): Promise<Hash> {
390
- return this.accountInitCode ?? this.getAccountInitCode();
391
- }
392
-
393
- private async create6492Signature(
394
- isDeployed: boolean,
395
- signature: Hash
396
- ): Promise<Hash> {
397
- if (isDeployed) {
398
- return signature;
399
- }
400
-
401
- const [factoryAddress, factoryCalldata] =
402
- await this.parseFactoryAddressFromAccountInitCode();
403
-
404
- Logger.verbose(
405
- `[BaseSmartContractAccount](create6492Signature)\
406
- factoryAddress: ${factoryAddress}, factoryCalldata: ${factoryCalldata}`
407
- );
408
-
409
- return wrapSignatureWith6492({
410
- factoryAddress,
411
- factoryCalldata,
412
- signature,
413
- });
414
- }
415
- }
@@ -1,51 +0,0 @@
1
- import { Address } from "abitype/zod";
2
- import { isHex, type Transport } from "viem";
3
- import z from "zod";
4
- import { createPublicErc4337ClientSchema } from "../client/schema.js";
5
- import { isEntryPointVersion } from "../entrypoint/index.js";
6
- import type { EntryPointVersion } from "../entrypoint/types.js";
7
- import { isSigner } from "../signer/schema.js";
8
- import type { SmartAccountSigner } from "../signer/types.js";
9
- import { ChainSchema } from "../utils/index.js";
10
-
11
- export const createBaseSmartAccountParamsSchema = <
12
- TTransport extends Transport = Transport,
13
- TSigner extends SmartAccountSigner = SmartAccountSigner
14
- >() =>
15
- z.object({
16
- rpcClient: z.union([
17
- z.string(),
18
- createPublicErc4337ClientSchema<TTransport>(),
19
- ]),
20
- factoryAddress: Address,
21
- signer: z.custom<TSigner>(isSigner),
22
- entryPointAddress: Address.optional(),
23
- chain: ChainSchema,
24
- accountAddress: Address.optional().describe(
25
- "Optional override for the account address."
26
- ),
27
- initCode: z
28
- .string()
29
- .refine(
30
- (x) => isHex(x, { strict: true }),
31
- "initCode must be a valid hex."
32
- )
33
- .optional()
34
- .describe("Optional override for the account init code."),
35
- entryPointVersion: z
36
- .custom<EntryPointVersion>(isEntryPointVersion)
37
- .optional(),
38
- });
39
-
40
- export const SimpleSmartAccountParamsSchema = <
41
- TTransport extends Transport = Transport,
42
- TSigner extends SmartAccountSigner = SmartAccountSigner
43
- >() =>
44
- createBaseSmartAccountParamsSchema<TTransport, TSigner>()
45
- .omit({
46
- rpcClient: true,
47
- })
48
- .extend({
49
- transport: z.custom<TTransport>(),
50
- salt: z.bigint().optional(),
51
- });
@@ -1,221 +0,0 @@
1
- import type { Address } from "abitype";
2
- import {
3
- concatHex,
4
- encodeFunctionData,
5
- isHex,
6
- type Chain,
7
- type FallbackTransport,
8
- type Hex,
9
- type Transport,
10
- } from "viem";
11
- import { SimpleAccountAbi_v6 } from "../abis/SimpleAccountAbi_v6.js";
12
- import { SimpleAccountAbi_v7 } from "../abis/SimpleAccountAbi_v7.js";
13
- import { SimpleAccountFactoryAbi } from "../abis/SimpleAccountFactoryAbi.js";
14
- import { createBundlerClient } from "../client/bundlerClient.js";
15
- import {
16
- defaultEntryPointVersion,
17
- getEntryPoint,
18
- } from "../entrypoint/index.js";
19
- import type {
20
- DefaultEntryPointVersion,
21
- EntryPointParameter,
22
- EntryPointVersion,
23
- } from "../entrypoint/types.js";
24
- import { AccountRequiresOwnerError } from "../errors/account.js";
25
- import type { SmartAccountSigner } from "../signer/types.js";
26
- import type { BatchUserOperationCallData } from "../types.js";
27
- import { getDefaultSimpleAccountFactoryAddress } from "../utils/defaults.js";
28
- import { BaseSmartContractAccount } from "./base.js";
29
- import { SimpleSmartAccountParamsSchema } from "./schema.js";
30
- import {
31
- toSmartContractAccount,
32
- type SmartContractAccountWithSigner,
33
- type ToSmartContractAccountParams,
34
- } from "./smartContractAccount.js";
35
- import type { SimpleSmartAccountParams } from "./types.js";
36
-
37
- class SimpleSmartContractAccount<
38
- TTransport extends Transport | FallbackTransport = Transport,
39
- TSigner extends SmartAccountSigner = SmartAccountSigner
40
- > extends BaseSmartContractAccount<TTransport, TSigner> {
41
- protected index: bigint;
42
- protected entryPointVersion: EntryPointVersion;
43
-
44
- constructor(params: SimpleSmartAccountParams<TTransport, TSigner>) {
45
- SimpleSmartAccountParamsSchema().parse(params);
46
-
47
- // This is a hack for now, we should kill the SimpleSmart Account when we kill Base Account
48
- const client = createBundlerClient({
49
- transport: params.transport as TTransport,
50
- chain: params.chain,
51
- });
52
-
53
- // @ts-expect-error zod custom type not recognized as required params for signers
54
- super({ ...params, rpcClient: client });
55
- this.signer = params.signer as TSigner;
56
- this.index = params.salt ?? 0n;
57
- this.entryPointVersion =
58
- params.entryPointVersion ?? defaultEntryPointVersion;
59
- }
60
-
61
- getDummySignature(): `0x${string}` {
62
- return "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
63
- }
64
-
65
- async encodeExecute(
66
- target: Hex,
67
- value: bigint,
68
- data: Hex
69
- ): Promise<`0x${string}`> {
70
- return encodeFunctionData({
71
- abi:
72
- this.entryPointVersion === "0.6.0"
73
- ? SimpleAccountAbi_v6
74
- : SimpleAccountAbi_v7,
75
- functionName: "execute",
76
- args: [target, value, data],
77
- });
78
- }
79
-
80
- override async encodeBatchExecute(
81
- txs: BatchUserOperationCallData
82
- ): Promise<`0x${string}`> {
83
- const [targets, datas] = txs.reduce(
84
- (accum, curr) => {
85
- accum[0].push(curr.target);
86
- accum[1].push(curr.data);
87
-
88
- return accum;
89
- },
90
- [[], []] as [Address[], Hex[]]
91
- );
92
-
93
- return encodeFunctionData({
94
- abi:
95
- this.entryPointVersion === "0.6.0"
96
- ? SimpleAccountAbi_v6
97
- : SimpleAccountAbi_v7,
98
- functionName: "executeBatch",
99
- args: [targets, datas],
100
- });
101
- }
102
-
103
- signMessage(msg: Uint8Array | string): Promise<`0x${string}`> {
104
- return this.signer.signMessage(
105
- typeof msg === "string" && !isHex(msg) ? msg : { raw: msg }
106
- );
107
- }
108
-
109
- public async getAccountInitCode(): Promise<`0x${string}`> {
110
- return concatHex([
111
- this.factoryAddress,
112
- encodeFunctionData({
113
- abi: SimpleAccountFactoryAbi,
114
- functionName: "createAccount",
115
- args: [await this.signer.getAddress(), this.index],
116
- }),
117
- ]);
118
- }
119
- }
120
-
121
- export type SimpleSmartAccount<
122
- TSigner extends SmartAccountSigner = SmartAccountSigner,
123
- TEntryPointVersion extends EntryPointVersion = EntryPointVersion
124
- > = SmartContractAccountWithSigner<
125
- "SimpleAccount",
126
- TSigner,
127
- TEntryPointVersion
128
- >;
129
-
130
- export type CreateSimpleAccountParams<
131
- TTransport extends Transport = Transport,
132
- TSigner extends SmartAccountSigner = SmartAccountSigner,
133
- TEntryPointVersion extends EntryPointVersion = DefaultEntryPointVersion
134
- > = Pick<
135
- ToSmartContractAccountParams<
136
- "SimpleAccount",
137
- TTransport,
138
- Chain,
139
- TEntryPointVersion
140
- >,
141
- "chain" | "transport"
142
- > & {
143
- signer: TSigner;
144
- salt?: bigint;
145
- accountAddress?: Address;
146
- factoryAddress?: Address;
147
- initCode?: Hex;
148
- } & EntryPointParameter<TEntryPointVersion, Chain>;
149
-
150
- export async function createSimpleSmartAccount<
151
- TTransport extends Transport = Transport,
152
- TSigner extends SmartAccountSigner = SmartAccountSigner,
153
- TEntryPointVersion extends EntryPointVersion = DefaultEntryPointVersion
154
- >(
155
- config: CreateSimpleAccountParams<TTransport, TSigner, TEntryPointVersion>
156
- ): Promise<SimpleSmartAccount<TSigner, TEntryPointVersion>>;
157
-
158
- /**
159
- * Creates a simple smart account using the provided parameters, including chain, entry point, factory address, and additional parameters. This function also ensures that a signer is provided.
160
- *
161
- * @param {CreateSimpleAccountParams} config The parameters for creating a simple smart account
162
- * @returns {Promise<SimpleSmartAccount>} A promise that resolves to a `SimpleSmartAccount` object containing the created account information and methods
163
- */
164
- export async function createSimpleSmartAccount({
165
- chain,
166
- entryPoint = getEntryPoint(chain),
167
- factoryAddress = getDefaultSimpleAccountFactoryAddress(
168
- chain,
169
- entryPoint.version
170
- ),
171
- ...params
172
- }: CreateSimpleAccountParams): Promise<SimpleSmartAccount> {
173
- if (!params.signer) throw new AccountRequiresOwnerError("SimpleAccount");
174
- const simpleAccount = new SimpleSmartContractAccount(
175
- SimpleSmartAccountParamsSchema().parse({
176
- chain,
177
- entryPointAddress: entryPoint.address,
178
- factoryAddress,
179
- ...params,
180
- })
181
- );
182
-
183
- const parsedParams = SimpleSmartAccountParamsSchema().parse({
184
- chain,
185
- entryPointAddress: entryPoint.address,
186
- entryPointVersion: entryPoint.version,
187
- factoryAddress,
188
- ...params,
189
- });
190
-
191
- const base = await toSmartContractAccount({
192
- source: "SimpleAccount",
193
- transport: params.transport,
194
- chain,
195
- encodeBatchExecute: simpleAccount.encodeBatchExecute.bind(simpleAccount),
196
- encodeExecute: (tx) =>
197
- simpleAccount.encodeExecute.bind(simpleAccount)(
198
- tx.target,
199
- tx.value ?? 0n,
200
- tx.data
201
- ),
202
- entryPoint,
203
- getAccountInitCode: async () => {
204
- if (parsedParams.initCode) return parsedParams.initCode as Hex;
205
- return simpleAccount.getAccountInitCode();
206
- },
207
- getDummySignature: simpleAccount.getDummySignature.bind(simpleAccount),
208
- signMessage: ({ message }) =>
209
- simpleAccount.signMessage(
210
- typeof message === "string" ? message : message.raw
211
- ),
212
- // @ts-expect-error these types still represent the same thing, but they are just a little off in there definitions
213
- signTypedData: simpleAccount.signTypedData.bind(simpleAccount),
214
- accountAddress: parsedParams.accountAddress,
215
- });
216
-
217
- return {
218
- ...base,
219
- getSigner: () => simpleAccount.getSigner(),
220
- };
221
- }