permissionless 0.0.11 → 0.0.13

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 (306) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/_cjs/accounts/index.js +14 -0
  3. package/_cjs/accounts/index.js.map +1 -0
  4. package/_cjs/accounts/privateKeyToSafeSmartAccount.js +24 -0
  5. package/_cjs/accounts/privateKeyToSafeSmartAccount.js.map +1 -0
  6. package/_cjs/accounts/privateKeyToSimpleSmartAccount.js +16 -0
  7. package/_cjs/accounts/privateKeyToSimpleSmartAccount.js.map +1 -0
  8. package/_cjs/accounts/signerToSafeSmartAccount.js +492 -0
  9. package/_cjs/accounts/signerToSafeSmartAccount.js.map +1 -0
  10. package/_cjs/accounts/signerToSimpleSmartAccount.js +190 -0
  11. package/_cjs/accounts/signerToSimpleSmartAccount.js.map +1 -0
  12. package/_cjs/accounts/types.js +23 -0
  13. package/_cjs/accounts/types.js.map +1 -0
  14. package/_cjs/actions/bundler/chainId.js.map +1 -1
  15. package/_cjs/actions/bundler/estimateUserOperationGas.js +4 -1
  16. package/_cjs/actions/bundler/estimateUserOperationGas.js.map +1 -1
  17. package/_cjs/actions/bundler/getUserOperationByHash.js.map +1 -1
  18. package/_cjs/actions/bundler/getUserOperationReceipt.js.map +1 -1
  19. package/_cjs/actions/bundler/sendUserOperation.js +4 -1
  20. package/_cjs/actions/bundler/sendUserOperation.js.map +1 -1
  21. package/_cjs/actions/bundler/supportedEntryPoints.js.map +1 -1
  22. package/_cjs/actions/bundler/waitForUserOperationReceipt.js +7 -2
  23. package/_cjs/actions/bundler/waitForUserOperationReceipt.js.map +1 -1
  24. package/_cjs/actions/index.js.map +1 -1
  25. package/_cjs/actions/pimlico/getUserOperationGasPrice.js.map +1 -1
  26. package/_cjs/actions/pimlico/getUserOperationStatus.js.map +1 -1
  27. package/_cjs/actions/pimlico/sponsorUserOperation.js +4 -1
  28. package/_cjs/actions/pimlico/sponsorUserOperation.js.map +1 -1
  29. package/_cjs/actions/pimlico.js.map +1 -1
  30. package/_cjs/actions/public/getAccountNonce.js +4 -2
  31. package/_cjs/actions/public/getAccountNonce.js.map +1 -1
  32. package/_cjs/actions/public/getSenderAddress.js +7 -3
  33. package/_cjs/actions/public/getSenderAddress.js.map +1 -1
  34. package/_cjs/actions/smartAccount/deployContract.js +38 -0
  35. package/_cjs/actions/smartAccount/deployContract.js.map +1 -0
  36. package/_cjs/actions/smartAccount/prepareUserOperationRequest.js +73 -0
  37. package/_cjs/actions/smartAccount/prepareUserOperationRequest.js.map +1 -0
  38. package/_cjs/actions/smartAccount/sendTransaction.js +44 -0
  39. package/_cjs/actions/smartAccount/sendTransaction.js.map +1 -0
  40. package/_cjs/actions/smartAccount/sendTransactions.js +46 -0
  41. package/_cjs/actions/smartAccount/sendTransactions.js.map +1 -0
  42. package/_cjs/actions/smartAccount/sendUserOperation.js +22 -0
  43. package/_cjs/actions/smartAccount/sendUserOperation.js.map +1 -0
  44. package/_cjs/actions/smartAccount/signMessage.js +16 -0
  45. package/_cjs/actions/smartAccount/signMessage.js.map +1 -0
  46. package/_cjs/actions/smartAccount/signTypedData.js +34 -0
  47. package/_cjs/actions/smartAccount/signTypedData.js.map +1 -0
  48. package/_cjs/actions/smartAccount/writeContract.js +21 -0
  49. package/_cjs/actions/smartAccount/writeContract.js.map +1 -0
  50. package/_cjs/actions/smartAccount.js +18 -0
  51. package/_cjs/actions/smartAccount.js.map +1 -0
  52. package/_cjs/actions/stackup/sponsorUserOperation.js +5 -1
  53. package/_cjs/actions/stackup/sponsorUserOperation.js.map +1 -1
  54. package/_cjs/clients/{bundler.js → createBundlerClient.js} +1 -1
  55. package/_cjs/clients/createBundlerClient.js.map +1 -0
  56. package/_cjs/clients/createSmartAccountClient.js +20 -0
  57. package/_cjs/clients/createSmartAccountClient.js.map +1 -0
  58. package/_cjs/clients/decorators/bundler.js.map +1 -1
  59. package/_cjs/clients/decorators/pimlico.js.map +1 -1
  60. package/_cjs/clients/decorators/smartAccount.js +33 -0
  61. package/_cjs/clients/decorators/smartAccount.js.map +1 -0
  62. package/_cjs/clients/decorators/stackup.js.map +1 -1
  63. package/_cjs/clients/pimlico.js.map +1 -1
  64. package/_cjs/clients/stackup.js.map +1 -1
  65. package/_cjs/index.js +9 -5
  66. package/_cjs/index.js.map +1 -1
  67. package/_cjs/utils/deepHexlify.js +4 -4
  68. package/_cjs/utils/deepHexlify.js.map +1 -1
  69. package/_cjs/utils/getAction.js +8 -0
  70. package/_cjs/utils/getAction.js.map +1 -0
  71. package/_cjs/utils/getUserOperationHash.js.map +1 -1
  72. package/_cjs/utils/index.js +7 -1
  73. package/_cjs/utils/index.js.map +1 -1
  74. package/_cjs/utils/observe.js +7 -2
  75. package/_cjs/utils/observe.js.map +1 -1
  76. package/_cjs/utils/signUserOperationHashWithECDSA.js +7 -7
  77. package/_cjs/utils/signUserOperationHashWithECDSA.js.map +1 -1
  78. package/_esm/accounts/index.js +7 -0
  79. package/_esm/accounts/index.js.map +1 -0
  80. package/_esm/accounts/privateKeyToSafeSmartAccount.js +26 -0
  81. package/_esm/accounts/privateKeyToSafeSmartAccount.js.map +1 -0
  82. package/_esm/accounts/privateKeyToSimpleSmartAccount.js +18 -0
  83. package/_esm/accounts/privateKeyToSimpleSmartAccount.js.map +1 -0
  84. package/_esm/accounts/signerToSafeSmartAccount.js +499 -0
  85. package/_esm/accounts/signerToSafeSmartAccount.js.map +1 -0
  86. package/_esm/accounts/signerToSimpleSmartAccount.js +191 -0
  87. package/_esm/accounts/signerToSimpleSmartAccount.js.map +1 -0
  88. package/_esm/accounts/types.js +20 -0
  89. package/_esm/accounts/types.js.map +1 -0
  90. package/_esm/actions/bundler/chainId.js.map +1 -1
  91. package/_esm/actions/bundler/estimateUserOperationGas.js +4 -1
  92. package/_esm/actions/bundler/estimateUserOperationGas.js.map +1 -1
  93. package/_esm/actions/bundler/getUserOperationByHash.js.map +1 -1
  94. package/_esm/actions/bundler/getUserOperationReceipt.js.map +1 -1
  95. package/_esm/actions/bundler/sendUserOperation.js +4 -3
  96. package/_esm/actions/bundler/sendUserOperation.js.map +1 -1
  97. package/_esm/actions/bundler/supportedEntryPoints.js.map +1 -1
  98. package/_esm/actions/bundler/waitForUserOperationReceipt.js +7 -2
  99. package/_esm/actions/bundler/waitForUserOperationReceipt.js.map +1 -1
  100. package/_esm/actions/index.js.map +1 -1
  101. package/_esm/actions/pimlico/getUserOperationGasPrice.js +2 -2
  102. package/_esm/actions/pimlico/getUserOperationGasPrice.js.map +1 -1
  103. package/_esm/actions/pimlico/getUserOperationStatus.js +1 -1
  104. package/_esm/actions/pimlico/getUserOperationStatus.js.map +1 -1
  105. package/_esm/actions/pimlico/sponsorUserOperation.js +4 -1
  106. package/_esm/actions/pimlico/sponsorUserOperation.js.map +1 -1
  107. package/_esm/actions/pimlico.js.map +1 -1
  108. package/_esm/actions/public/getAccountNonce.js +7 -5
  109. package/_esm/actions/public/getAccountNonce.js.map +1 -1
  110. package/_esm/actions/public/getSenderAddress.js +8 -5
  111. package/_esm/actions/public/getSenderAddress.js.map +1 -1
  112. package/_esm/actions/smartAccount/deployContract.js +64 -0
  113. package/_esm/actions/smartAccount/deployContract.js.map +1 -0
  114. package/_esm/actions/smartAccount/prepareUserOperationRequest.js +69 -0
  115. package/_esm/actions/smartAccount/prepareUserOperationRequest.js.map +1 -0
  116. package/_esm/actions/smartAccount/sendTransaction.js +88 -0
  117. package/_esm/actions/smartAccount/sendTransaction.js.map +1 -0
  118. package/_esm/actions/smartAccount/sendTransactions.js +90 -0
  119. package/_esm/actions/smartAccount/sendTransactions.js.map +1 -0
  120. package/_esm/actions/smartAccount/sendUserOperation.js +18 -0
  121. package/_esm/actions/smartAccount/sendUserOperation.js.map +1 -0
  122. package/_esm/actions/smartAccount/signMessage.js +59 -0
  123. package/_esm/actions/smartAccount/signMessage.js.map +1 -0
  124. package/_esm/actions/smartAccount/signTypedData.js +129 -0
  125. package/_esm/actions/smartAccount/signTypedData.js.map +1 -0
  126. package/_esm/actions/smartAccount/writeContract.js +19 -0
  127. package/_esm/actions/smartAccount/writeContract.js.map +1 -0
  128. package/_esm/actions/smartAccount.js +9 -0
  129. package/_esm/actions/smartAccount.js.map +1 -0
  130. package/_esm/actions/stackup/accounts.js +1 -1
  131. package/_esm/actions/stackup/sponsorUserOperation.js +6 -2
  132. package/_esm/actions/stackup/sponsorUserOperation.js.map +1 -1
  133. package/_esm/clients/{bundler.js → createBundlerClient.js} +1 -1
  134. package/_esm/clients/createBundlerClient.js.map +1 -0
  135. package/_esm/clients/createSmartAccountClient.js +38 -0
  136. package/_esm/clients/createSmartAccountClient.js.map +1 -0
  137. package/_esm/clients/decorators/bundler.js.map +1 -1
  138. package/_esm/clients/decorators/pimlico.js +1 -1
  139. package/_esm/clients/decorators/pimlico.js.map +1 -1
  140. package/_esm/clients/decorators/smartAccount.js +29 -0
  141. package/_esm/clients/decorators/smartAccount.js.map +1 -0
  142. package/_esm/clients/decorators/stackup.js.map +1 -1
  143. package/_esm/clients/pimlico.js +1 -1
  144. package/_esm/clients/pimlico.js.map +1 -1
  145. package/_esm/clients/stackup.js +1 -1
  146. package/_esm/clients/stackup.js.map +1 -1
  147. package/_esm/index.js +5 -2
  148. package/_esm/index.js.map +1 -1
  149. package/_esm/utils/deepHexlify.js +6 -4
  150. package/_esm/utils/deepHexlify.js.map +1 -1
  151. package/_esm/utils/getAction.js +6 -0
  152. package/_esm/utils/getAction.js.map +1 -0
  153. package/_esm/utils/getUserOperationHash.js.map +1 -1
  154. package/_esm/utils/index.js +5 -0
  155. package/_esm/utils/index.js.map +1 -1
  156. package/_esm/utils/observe.js +7 -2
  157. package/_esm/utils/observe.js.map +1 -1
  158. package/_esm/utils/signUserOperationHashWithECDSA.js +7 -6
  159. package/_esm/utils/signUserOperationHashWithECDSA.js.map +1 -1
  160. package/_types/accounts/index.d.ts +7 -0
  161. package/_types/accounts/index.d.ts.map +1 -0
  162. package/_types/accounts/privateKeyToSafeSmartAccount.d.ts +26 -0
  163. package/_types/accounts/privateKeyToSafeSmartAccount.d.ts.map +1 -0
  164. package/_types/accounts/privateKeyToSimpleSmartAccount.d.ts +14 -0
  165. package/_types/accounts/privateKeyToSimpleSmartAccount.d.ts.map +1 -0
  166. package/_types/accounts/signerToSafeSmartAccount.d.ts +28 -0
  167. package/_types/accounts/signerToSafeSmartAccount.d.ts.map +1 -0
  168. package/_types/accounts/signerToSimpleSmartAccount.d.ts +15 -0
  169. package/_types/accounts/signerToSimpleSmartAccount.d.ts.map +1 -0
  170. package/_types/accounts/types.d.ts +32 -0
  171. package/_types/accounts/types.d.ts.map +1 -0
  172. package/_types/actions/bundler/chainId.d.ts +3 -2
  173. package/_types/actions/bundler/chainId.d.ts.map +1 -1
  174. package/_types/actions/bundler/estimateUserOperationGas.d.ts +3 -3
  175. package/_types/actions/bundler/estimateUserOperationGas.d.ts.map +1 -1
  176. package/_types/actions/bundler/getUserOperationByHash.d.ts +3 -3
  177. package/_types/actions/bundler/getUserOperationByHash.d.ts.map +1 -1
  178. package/_types/actions/bundler/getUserOperationReceipt.d.ts +3 -3
  179. package/_types/actions/bundler/getUserOperationReceipt.d.ts.map +1 -1
  180. package/_types/actions/bundler/sendUserOperation.d.ts +3 -5
  181. package/_types/actions/bundler/sendUserOperation.d.ts.map +1 -1
  182. package/_types/actions/bundler/supportedEntryPoints.d.ts +3 -3
  183. package/_types/actions/bundler/supportedEntryPoints.d.ts.map +1 -1
  184. package/_types/actions/bundler/waitForUserOperationReceipt.d.ts +2 -3
  185. package/_types/actions/bundler/waitForUserOperationReceipt.d.ts.map +1 -1
  186. package/_types/actions/index.d.ts.map +1 -1
  187. package/_types/actions/pimlico/getUserOperationGasPrice.d.ts +5 -4
  188. package/_types/actions/pimlico/getUserOperationGasPrice.d.ts.map +1 -1
  189. package/_types/actions/pimlico/getUserOperationStatus.d.ts +4 -5
  190. package/_types/actions/pimlico/getUserOperationStatus.d.ts.map +1 -1
  191. package/_types/actions/pimlico/sponsorUserOperation.d.ts +3 -3
  192. package/_types/actions/pimlico/sponsorUserOperation.d.ts.map +1 -1
  193. package/_types/actions/pimlico.d.ts.map +1 -1
  194. package/_types/actions/public/getAccountNonce.d.ts +5 -5
  195. package/_types/actions/public/getAccountNonce.d.ts.map +1 -1
  196. package/_types/actions/public/getSenderAddress.d.ts +3 -4
  197. package/_types/actions/public/getSenderAddress.d.ts.map +1 -1
  198. package/_types/actions/smartAccount/deployContract.d.ts +34 -0
  199. package/_types/actions/smartAccount/deployContract.d.ts.map +1 -0
  200. package/_types/actions/smartAccount/prepareUserOperationRequest.d.ts +20 -0
  201. package/_types/actions/smartAccount/prepareUserOperationRequest.d.ts.map +1 -0
  202. package/_types/actions/smartAccount/sendTransaction.d.ts +52 -0
  203. package/_types/actions/smartAccount/sendTransaction.d.ts.map +1 -0
  204. package/_types/actions/smartAccount/sendTransactions.d.ts +63 -0
  205. package/_types/actions/smartAccount/sendTransactions.d.ts.map +1 -0
  206. package/_types/actions/smartAccount/sendUserOperation.d.ts +10 -0
  207. package/_types/actions/smartAccount/sendUserOperation.d.ts.map +1 -0
  208. package/_types/actions/smartAccount/signMessage.d.ts +50 -0
  209. package/_types/actions/smartAccount/signMessage.d.ts.map +1 -0
  210. package/_types/actions/smartAccount/signTypedData.d.ts +104 -0
  211. package/_types/actions/smartAccount/signTypedData.d.ts.map +1 -0
  212. package/_types/actions/smartAccount/writeContract.d.ts +57 -0
  213. package/_types/actions/smartAccount/writeContract.d.ts.map +1 -0
  214. package/_types/actions/smartAccount.d.ts +9 -0
  215. package/_types/actions/smartAccount.d.ts.map +1 -0
  216. package/_types/actions/stackup/accounts.d.ts +1 -1
  217. package/_types/actions/stackup/sponsorUserOperation.d.ts +1 -1
  218. package/_types/actions/stackup/sponsorUserOperation.d.ts.map +1 -1
  219. package/_types/clients/{bundler.d.ts → createBundlerClient.d.ts} +1 -1
  220. package/_types/clients/createBundlerClient.d.ts.map +1 -0
  221. package/_types/clients/createSmartAccountClient.d.ts +282 -0
  222. package/_types/clients/createSmartAccountClient.d.ts.map +1 -0
  223. package/_types/clients/decorators/bundler.d.ts +1 -1
  224. package/_types/clients/decorators/bundler.d.ts.map +1 -1
  225. package/_types/clients/decorators/pimlico.d.ts +3 -3
  226. package/_types/clients/decorators/pimlico.d.ts.map +1 -1
  227. package/_types/clients/decorators/smartAccount.d.ts +327 -0
  228. package/_types/clients/decorators/smartAccount.d.ts.map +1 -0
  229. package/_types/clients/decorators/stackup.d.ts.map +1 -1
  230. package/_types/clients/pimlico.d.ts +1 -1
  231. package/_types/clients/pimlico.d.ts.map +1 -1
  232. package/_types/clients/stackup.d.ts +1 -1
  233. package/_types/clients/stackup.d.ts.map +1 -1
  234. package/_types/index.d.ts +6 -3
  235. package/_types/index.d.ts.map +1 -1
  236. package/_types/types/bundler.d.ts +4 -1
  237. package/_types/types/bundler.d.ts.map +1 -1
  238. package/_types/types/index.d.ts +20 -0
  239. package/_types/types/index.d.ts.map +1 -1
  240. package/_types/types/pimlico.d.ts.map +1 -1
  241. package/_types/types/stackup.d.ts.map +1 -1
  242. package/_types/utils/deepHexlify.d.ts.map +1 -1
  243. package/_types/utils/getAction.d.ts +3 -0
  244. package/_types/utils/getAction.d.ts.map +1 -0
  245. package/_types/utils/getUserOperationHash.d.ts.map +1 -1
  246. package/_types/utils/index.d.ts +2 -0
  247. package/_types/utils/index.d.ts.map +1 -1
  248. package/_types/utils/observe.d.ts.map +1 -1
  249. package/_types/utils/signUserOperationHashWithECDSA.d.ts +3 -11
  250. package/_types/utils/signUserOperationHashWithECDSA.d.ts.map +1 -1
  251. package/accounts/index.ts +33 -0
  252. package/accounts/privateKeyToSafeSmartAccount.ts +73 -0
  253. package/accounts/privateKeyToSimpleSmartAccount.ts +44 -0
  254. package/accounts/signerToSafeSmartAccount.ts +775 -0
  255. package/accounts/signerToSimpleSmartAccount.ts +267 -0
  256. package/accounts/types.ts +64 -0
  257. package/actions/bundler/chainId.ts +10 -2
  258. package/actions/bundler/estimateUserOperationGas.ts +17 -6
  259. package/actions/bundler/getUserOperationByHash.ts +16 -5
  260. package/actions/bundler/getUserOperationReceipt.ts +17 -4
  261. package/actions/bundler/sendUserOperation.ts +19 -7
  262. package/actions/bundler/supportedEntryPoints.ts +10 -3
  263. package/actions/bundler/waitForUserOperationReceipt.ts +64 -26
  264. package/actions/index.ts +4 -1
  265. package/actions/pimlico/getUserOperationGasPrice.ts +13 -6
  266. package/actions/pimlico/getUserOperationStatus.ts +12 -5
  267. package/actions/pimlico/sponsorUserOperation.ts +20 -7
  268. package/actions/pimlico.ts +8 -2
  269. package/actions/public/getAccountNonce.ts +20 -8
  270. package/actions/public/getSenderAddress.ts +25 -8
  271. package/actions/smartAccount/deployContract.ts +110 -0
  272. package/actions/smartAccount/prepareUserOperationRequest.ts +140 -0
  273. package/actions/smartAccount/sendTransaction.ts +138 -0
  274. package/actions/smartAccount/sendTransactions.ts +141 -0
  275. package/actions/smartAccount/sendUserOperation.ts +70 -0
  276. package/actions/smartAccount/signMessage.ts +79 -0
  277. package/actions/smartAccount/signTypedData.ts +161 -0
  278. package/actions/smartAccount/writeContract.ts +126 -0
  279. package/actions/smartAccount.ts +49 -0
  280. package/actions/stackup/accounts.ts +1 -1
  281. package/actions/stackup/sponsorUserOperation.ts +14 -4
  282. package/clients/{bundler.ts → createBundlerClient.ts} +14 -3
  283. package/clients/createSmartAccountClient.ts +95 -0
  284. package/clients/decorators/bundler.ts +20 -10
  285. package/clients/decorators/pimlico.ts +21 -9
  286. package/clients/decorators/smartAccount.ts +451 -0
  287. package/clients/decorators/stackup.ts +12 -4
  288. package/clients/pimlico.ts +20 -5
  289. package/clients/stackup.ts +17 -4
  290. package/index.ts +20 -3
  291. package/package.json +12 -2
  292. package/types/bundler.ts +4 -1
  293. package/types/index.ts +29 -0
  294. package/types/pimlico.ts +12 -2
  295. package/types/stackup.ts +4 -1
  296. package/utils/deepHexlify.test.ts +13 -7
  297. package/utils/deepHexlify.ts +5 -4
  298. package/utils/getAction.ts +15 -0
  299. package/utils/getUserOperationHash.ts +10 -2
  300. package/utils/index.ts +15 -2
  301. package/utils/observe.ts +18 -5
  302. package/utils/signUserOperationHashWithECDSA.ts +17 -20
  303. package/_cjs/clients/bundler.js.map +0 -1
  304. package/_esm/clients/bundler.js.map +0 -1
  305. package/_types/clients/bundler.d.ts.map +0 -1
  306. package/tsconfig.build.tsbuildinfo +0 -1
@@ -0,0 +1,775 @@
1
+ import {
2
+ type Account,
3
+ type Address,
4
+ type Chain,
5
+ type Client,
6
+ type Hex,
7
+ type SignableMessage,
8
+ type Transport,
9
+ type TypedData,
10
+ type TypedDataDefinition,
11
+ concatHex,
12
+ encodeFunctionData,
13
+ encodePacked,
14
+ getContractAddress,
15
+ hashMessage,
16
+ hashTypedData,
17
+ hexToBigInt,
18
+ keccak256,
19
+ toBytes,
20
+ zeroAddress
21
+ } from "viem"
22
+ import { toAccount } from "viem/accounts"
23
+ import {
24
+ getBytecode,
25
+ getChainId,
26
+ readContract,
27
+ signMessage,
28
+ signTypedData
29
+ } from "viem/actions"
30
+ import { getAccountNonce } from "../actions/public/getAccountNonce.js"
31
+ import {
32
+ SignTransactionNotSupportedBySmartAccount,
33
+ type SmartAccount,
34
+ type SmartAccountSigner
35
+ } from "./types.js"
36
+
37
+ export type SafeVersion = "1.4.1"
38
+
39
+ const EIP712_SAFE_OPERATION_TYPE = {
40
+ SafeOp: [
41
+ { type: "address", name: "safe" },
42
+ { type: "bytes", name: "callData" },
43
+ { type: "uint256", name: "nonce" },
44
+ { type: "uint256", name: "preVerificationGas" },
45
+ { type: "uint256", name: "verificationGasLimit" },
46
+ { type: "uint256", name: "callGasLimit" },
47
+ { type: "uint256", name: "maxFeePerGas" },
48
+ { type: "uint256", name: "maxPriorityFeePerGas" },
49
+ { type: "address", name: "entryPoint" }
50
+ ]
51
+ }
52
+
53
+ const SAFE_VERSION_TO_ADDRESSES_MAP: {
54
+ [key in SafeVersion]: {
55
+ ADD_MODULES_LIB_ADDRESS: Address
56
+ SAFE_4337_MODULE_ADDRESS: Address
57
+ SAFE_PROXY_FACTORY_ADDRESS: Address
58
+ SAFE_SINGLETON_ADDRESS: Address
59
+ MULTI_SEND_ADDRESS: Address
60
+ MULTI_SEND_CALL_ONLY_ADDRESS: Address
61
+ }
62
+ } = {
63
+ "1.4.1": {
64
+ ADD_MODULES_LIB_ADDRESS: "0x191EFDC03615B575922289DC339F4c70aC5C30Af",
65
+ SAFE_4337_MODULE_ADDRESS: "0x39E54Bb2b3Aa444b4B39DEe15De3b7809c36Fc38",
66
+ SAFE_PROXY_FACTORY_ADDRESS:
67
+ "0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67",
68
+ SAFE_SINGLETON_ADDRESS: "0x41675C099F32341bf84BFc5382aF534df5C7461a",
69
+ MULTI_SEND_ADDRESS: "0x38869bf66a61cF6bDB996A6aE40D5853Fd43B526",
70
+ MULTI_SEND_CALL_ONLY_ADDRESS:
71
+ "0x9641d764fc13c8B624c04430C7356C1C7C8102e2"
72
+ }
73
+ }
74
+
75
+ const adjustVInSignature = (
76
+ signingMethod: "eth_sign" | "eth_signTypedData",
77
+ signature: string
78
+ ): Hex => {
79
+ const ETHEREUM_V_VALUES = [0, 1, 27, 28]
80
+ const MIN_VALID_V_VALUE_FOR_SAFE_ECDSA = 27
81
+ let signatureV = parseInt(signature.slice(-2), 16)
82
+ if (!ETHEREUM_V_VALUES.includes(signatureV)) {
83
+ throw new Error("Invalid signature")
84
+ }
85
+ if (signingMethod === "eth_sign") {
86
+ if (signatureV < MIN_VALID_V_VALUE_FOR_SAFE_ECDSA) {
87
+ signatureV += MIN_VALID_V_VALUE_FOR_SAFE_ECDSA
88
+ }
89
+ signatureV += 4
90
+ }
91
+ if (signingMethod === "eth_signTypedData") {
92
+ if (signatureV < MIN_VALID_V_VALUE_FOR_SAFE_ECDSA) {
93
+ signatureV += MIN_VALID_V_VALUE_FOR_SAFE_ECDSA
94
+ }
95
+ }
96
+ return (signature.slice(0, -2) + signatureV.toString(16)) as Hex
97
+ }
98
+
99
+ const generateSafeMessageMessage = <
100
+ const TTypedData extends TypedData | { [key: string]: unknown },
101
+ TPrimaryType extends string = string
102
+ >(
103
+ message: SignableMessage | TypedDataDefinition<TTypedData, TPrimaryType>
104
+ ): Hex => {
105
+ const signableMessage = message as SignableMessage
106
+
107
+ if (typeof signableMessage === "string" || signableMessage.raw) {
108
+ return hashMessage(signableMessage)
109
+ }
110
+
111
+ return hashTypedData(
112
+ message as TypedDataDefinition<TTypedData, TPrimaryType>
113
+ )
114
+ }
115
+
116
+ const encodeInternalTransaction = (tx: {
117
+ to: Address
118
+ data: Address
119
+ value: bigint
120
+ operation: 0 | 1
121
+ }): string => {
122
+ const encoded = encodePacked(
123
+ ["uint8", "address", "uint256", "uint256", "bytes"],
124
+ [
125
+ tx.operation,
126
+ tx.to,
127
+ tx.value,
128
+ BigInt(tx.data.slice(2).length / 2),
129
+ tx.data
130
+ ]
131
+ )
132
+ return encoded.slice(2)
133
+ }
134
+
135
+ const encodeMultiSend = (
136
+ txs: {
137
+ to: Address
138
+ data: Address
139
+ value: bigint
140
+ operation: 0 | 1
141
+ }[]
142
+ ): `0x${string}` => {
143
+ const data: `0x${string}` = `0x${txs
144
+ .map((tx) => encodeInternalTransaction(tx))
145
+ .join("")}`
146
+
147
+ return encodeFunctionData({
148
+ abi: [
149
+ {
150
+ inputs: [
151
+ {
152
+ internalType: "bytes",
153
+ name: "transactions",
154
+ type: "bytes"
155
+ }
156
+ ],
157
+ name: "multiSend",
158
+ outputs: [],
159
+ stateMutability: "payable",
160
+ type: "function"
161
+ }
162
+ ],
163
+ functionName: "multiSend",
164
+ args: [data]
165
+ })
166
+ }
167
+
168
+ export type SafeSmartAccount<
169
+ transport extends Transport = Transport,
170
+ chain extends Chain | undefined = Chain | undefined
171
+ > = SmartAccount<"SafeSmartAccount", transport, chain>
172
+
173
+ const getInitializerCode = async ({
174
+ owner,
175
+ addModuleLibAddress,
176
+ safe4337ModuleAddress,
177
+ multiSendAddress,
178
+ setupTransactions = [],
179
+ safeModules = []
180
+ }: {
181
+ owner: Address
182
+ addModuleLibAddress: Address
183
+ safe4337ModuleAddress: Address
184
+ multiSendAddress: Address
185
+ setupTransactions?: {
186
+ to: Address
187
+ data: Address
188
+ value: bigint
189
+ }[]
190
+ safeModules?: Address[]
191
+ }) => {
192
+ const multiSendCallData = encodeMultiSend([
193
+ {
194
+ to: addModuleLibAddress,
195
+ data: encodeFunctionData({
196
+ abi: [
197
+ {
198
+ inputs: [
199
+ {
200
+ internalType: "address[]",
201
+ name: "modules",
202
+ type: "address[]"
203
+ }
204
+ ],
205
+ name: "enableModules",
206
+ outputs: [],
207
+ stateMutability: "nonpayable",
208
+ type: "function"
209
+ }
210
+ ],
211
+ functionName: "enableModules",
212
+ args: [[safe4337ModuleAddress, ...safeModules]]
213
+ }),
214
+ value: 0n,
215
+ operation: 1
216
+ },
217
+ ...setupTransactions.map((tx) => ({ ...tx, operation: 0 as 0 | 1 }))
218
+ ])
219
+
220
+ return encodeFunctionData({
221
+ abi: [
222
+ {
223
+ inputs: [
224
+ {
225
+ internalType: "address[]",
226
+ name: "_owners",
227
+ type: "address[]"
228
+ },
229
+ {
230
+ internalType: "uint256",
231
+ name: "_threshold",
232
+ type: "uint256"
233
+ },
234
+ {
235
+ internalType: "address",
236
+ name: "to",
237
+ type: "address"
238
+ },
239
+ {
240
+ internalType: "bytes",
241
+ name: "data",
242
+ type: "bytes"
243
+ },
244
+ {
245
+ internalType: "address",
246
+ name: "fallbackHandler",
247
+ type: "address"
248
+ },
249
+ {
250
+ internalType: "address",
251
+ name: "paymentToken",
252
+ type: "address"
253
+ },
254
+ {
255
+ internalType: "uint256",
256
+ name: "payment",
257
+ type: "uint256"
258
+ },
259
+ {
260
+ internalType: "address payable",
261
+ name: "paymentReceiver",
262
+ type: "address"
263
+ }
264
+ ],
265
+ name: "setup",
266
+ outputs: [],
267
+ stateMutability: "nonpayable",
268
+ type: "function"
269
+ }
270
+ ],
271
+ functionName: "setup",
272
+ args: [
273
+ [owner],
274
+ 1n,
275
+ multiSendAddress,
276
+ multiSendCallData,
277
+ safe4337ModuleAddress,
278
+ zeroAddress,
279
+ 0n,
280
+ zeroAddress
281
+ ]
282
+ })
283
+ }
284
+
285
+ const getAccountInitCode = async ({
286
+ owner,
287
+ addModuleLibAddress,
288
+ safe4337ModuleAddress,
289
+ safeProxyFactoryAddress,
290
+ safeSingletonAddress,
291
+ multiSendAddress,
292
+ saltNonce = 0n,
293
+ setupTransactions = [],
294
+ safeModules = []
295
+ }: {
296
+ owner: Address
297
+ addModuleLibAddress: Address
298
+ safe4337ModuleAddress: Address
299
+ safeProxyFactoryAddress: Address
300
+ safeSingletonAddress: Address
301
+ multiSendAddress: Address
302
+ saltNonce?: bigint
303
+ setupTransactions?: {
304
+ to: Address
305
+ data: Address
306
+ value: bigint
307
+ }[]
308
+ safeModules?: Address[]
309
+ }): Promise<Hex> => {
310
+ if (!owner) throw new Error("Owner account not found")
311
+ const initializer = await getInitializerCode({
312
+ owner,
313
+ addModuleLibAddress,
314
+ safe4337ModuleAddress,
315
+ multiSendAddress,
316
+ setupTransactions,
317
+ safeModules
318
+ })
319
+
320
+ const initCodeCallData = encodeFunctionData({
321
+ abi: [
322
+ {
323
+ inputs: [
324
+ {
325
+ internalType: "address",
326
+ name: "_singleton",
327
+ type: "address"
328
+ },
329
+ {
330
+ internalType: "bytes",
331
+ name: "initializer",
332
+ type: "bytes"
333
+ },
334
+ {
335
+ internalType: "uint256",
336
+ name: "saltNonce",
337
+ type: "uint256"
338
+ }
339
+ ],
340
+ name: "createProxyWithNonce",
341
+ outputs: [
342
+ {
343
+ internalType: "contract SafeProxy",
344
+ name: "proxy",
345
+ type: "address"
346
+ }
347
+ ],
348
+ stateMutability: "nonpayable",
349
+ type: "function"
350
+ }
351
+ ],
352
+ functionName: "createProxyWithNonce",
353
+ args: [safeSingletonAddress, initializer, saltNonce]
354
+ })
355
+
356
+ return concatHex([safeProxyFactoryAddress, initCodeCallData])
357
+ }
358
+
359
+ const getAccountAddress = async <
360
+ TTransport extends Transport = Transport,
361
+ TChain extends Chain | undefined = Chain | undefined
362
+ >({
363
+ client,
364
+ owner,
365
+ addModuleLibAddress,
366
+ safe4337ModuleAddress,
367
+ safeProxyFactoryAddress,
368
+ safeSingletonAddress,
369
+ multiSendAddress,
370
+ setupTransactions = [],
371
+ safeModules = [],
372
+ saltNonce = 0n
373
+ }: {
374
+ client: Client<TTransport, TChain>
375
+ owner: Address
376
+ addModuleLibAddress: Address
377
+ safe4337ModuleAddress: Address
378
+ safeProxyFactoryAddress: Address
379
+ safeSingletonAddress: Address
380
+ multiSendAddress: Address
381
+ setupTransactions: {
382
+ to: Address
383
+ data: Address
384
+ value: bigint
385
+ }[]
386
+ safeModules?: Address[]
387
+ saltNonce?: bigint
388
+ }): Promise<Address> => {
389
+ const proxyCreationCode = await readContract(client, {
390
+ abi: [
391
+ {
392
+ inputs: [],
393
+ name: "proxyCreationCode",
394
+ outputs: [
395
+ {
396
+ internalType: "bytes",
397
+ name: "",
398
+ type: "bytes"
399
+ }
400
+ ],
401
+ stateMutability: "pure",
402
+ type: "function"
403
+ }
404
+ ],
405
+ address: safeProxyFactoryAddress,
406
+ functionName: "proxyCreationCode"
407
+ })
408
+
409
+ const deploymentCode = encodePacked(
410
+ ["bytes", "uint256"],
411
+ [proxyCreationCode, hexToBigInt(safeSingletonAddress)]
412
+ )
413
+
414
+ const initializer = await getInitializerCode({
415
+ owner,
416
+ addModuleLibAddress,
417
+ safe4337ModuleAddress,
418
+ multiSendAddress,
419
+ setupTransactions,
420
+ safeModules
421
+ })
422
+
423
+ const salt = keccak256(
424
+ encodePacked(
425
+ ["bytes32", "uint256"],
426
+ [keccak256(encodePacked(["bytes"], [initializer])), saltNonce]
427
+ )
428
+ )
429
+
430
+ return getContractAddress({
431
+ from: safeProxyFactoryAddress,
432
+ salt,
433
+ bytecode: deploymentCode,
434
+ opcode: "CREATE2"
435
+ })
436
+ }
437
+
438
+ const getDefaultAddresses = (
439
+ safeVersion: SafeVersion,
440
+ {
441
+ addModuleLibAddress: _addModuleLibAddress,
442
+ safe4337ModuleAddress: _safe4337ModuleAddress,
443
+ safeProxyFactoryAddress: _safeProxyFactoryAddress,
444
+ safeSingletonAddress: _safeSingletonAddress,
445
+ multiSendAddress: _multiSendAddress,
446
+ multiSendCallOnlyAddress: _multiSendCallOnlyAddress
447
+ }: {
448
+ addModuleLibAddress?: Address
449
+ safe4337ModuleAddress?: Address
450
+ safeProxyFactoryAddress?: Address
451
+ safeSingletonAddress?: Address
452
+ multiSendAddress?: Address
453
+ multiSendCallOnlyAddress?: Address
454
+ }
455
+ ) => {
456
+ const addModuleLibAddress =
457
+ _addModuleLibAddress ??
458
+ SAFE_VERSION_TO_ADDRESSES_MAP[safeVersion].ADD_MODULES_LIB_ADDRESS
459
+ const safe4337ModuleAddress =
460
+ _safe4337ModuleAddress ??
461
+ SAFE_VERSION_TO_ADDRESSES_MAP[safeVersion].SAFE_4337_MODULE_ADDRESS
462
+ const safeProxyFactoryAddress =
463
+ _safeProxyFactoryAddress ??
464
+ SAFE_VERSION_TO_ADDRESSES_MAP[safeVersion].SAFE_PROXY_FACTORY_ADDRESS
465
+ const safeSingletonAddress =
466
+ _safeSingletonAddress ??
467
+ SAFE_VERSION_TO_ADDRESSES_MAP[safeVersion].SAFE_SINGLETON_ADDRESS
468
+ const multiSendAddress =
469
+ _multiSendAddress ??
470
+ SAFE_VERSION_TO_ADDRESSES_MAP[safeVersion].MULTI_SEND_ADDRESS
471
+
472
+ const multiSendCallOnlyAddress =
473
+ _multiSendCallOnlyAddress ??
474
+ SAFE_VERSION_TO_ADDRESSES_MAP[safeVersion].MULTI_SEND_CALL_ONLY_ADDRESS
475
+
476
+ return {
477
+ addModuleLibAddress,
478
+ safe4337ModuleAddress,
479
+ safeProxyFactoryAddress,
480
+ safeSingletonAddress,
481
+ multiSendAddress,
482
+ multiSendCallOnlyAddress
483
+ }
484
+ }
485
+
486
+ /**
487
+ * @description Creates an Simple Account from a private key.
488
+ *
489
+ * @returns A Private Key Simple Account.
490
+ */
491
+ export async function signerToSafeSmartAccount<
492
+ TTransport extends Transport = Transport,
493
+ TChain extends Chain | undefined = Chain | undefined
494
+ >(
495
+ client: Client<TTransport, TChain>,
496
+ {
497
+ signer,
498
+ safeVersion,
499
+ entryPoint,
500
+ addModuleLibAddress: _addModuleLibAddress,
501
+ safe4337ModuleAddress: _safe4337ModuleAddress,
502
+ safeProxyFactoryAddress: _safeProxyFactoryAddress,
503
+ safeSingletonAddress: _safeSingletonAddress,
504
+ multiSendAddress: _multiSendAddress,
505
+ multiSendCallOnlyAddress: _multiSendCallOnlyAddress,
506
+ saltNonce = 0n,
507
+ safeModules = [],
508
+ setupTransactions = []
509
+ }: {
510
+ safeVersion: SafeVersion
511
+ signer: SmartAccountSigner
512
+ entryPoint: Address
513
+ addModuleLibAddress?: Address
514
+ safe4337ModuleAddress?: Address
515
+ safeProxyFactoryAddress?: Address
516
+ safeSingletonAddress?: Address
517
+ multiSendAddress?: Address
518
+ multiSendCallOnlyAddress?: Address
519
+ saltNonce?: bigint
520
+ setupTransactions?: {
521
+ to: Address
522
+ data: Address
523
+ value: bigint
524
+ }[]
525
+ safeModules?: Address[]
526
+ }
527
+ ): Promise<SafeSmartAccount<TTransport, TChain>> {
528
+ const chainId = await getChainId(client)
529
+
530
+ const viemSigner: Account =
531
+ signer.type === "local"
532
+ ? ({
533
+ ...signer,
534
+ signTransaction: (_, __) => {
535
+ throw new SignTransactionNotSupportedBySmartAccount()
536
+ }
537
+ } as Account)
538
+ : (signer as Account)
539
+
540
+ const {
541
+ addModuleLibAddress,
542
+ safe4337ModuleAddress,
543
+ safeProxyFactoryAddress,
544
+ safeSingletonAddress,
545
+ multiSendAddress,
546
+ multiSendCallOnlyAddress
547
+ } = getDefaultAddresses(safeVersion, {
548
+ addModuleLibAddress: _addModuleLibAddress,
549
+ safe4337ModuleAddress: _safe4337ModuleAddress,
550
+ safeProxyFactoryAddress: _safeProxyFactoryAddress,
551
+ safeSingletonAddress: _safeSingletonAddress,
552
+ multiSendAddress: _multiSendAddress,
553
+ multiSendCallOnlyAddress: _multiSendCallOnlyAddress
554
+ })
555
+
556
+ const accountAddress = await getAccountAddress<TTransport, TChain>({
557
+ client,
558
+ owner: viemSigner.address,
559
+ addModuleLibAddress,
560
+ safe4337ModuleAddress,
561
+ safeProxyFactoryAddress,
562
+ safeSingletonAddress,
563
+ multiSendAddress,
564
+ saltNonce,
565
+ setupTransactions,
566
+ safeModules
567
+ })
568
+
569
+ if (!accountAddress) throw new Error("Account address not found")
570
+
571
+ const account = toAccount({
572
+ address: accountAddress,
573
+ async signMessage({ message }) {
574
+ const messageHash = hashTypedData({
575
+ domain: {
576
+ chainId: chainId,
577
+ verifyingContract: accountAddress
578
+ },
579
+ types: {
580
+ SafeMessage: [{ name: "message", type: "bytes" }]
581
+ },
582
+ primaryType: "SafeMessage",
583
+ message: {
584
+ message: generateSafeMessageMessage(message)
585
+ }
586
+ })
587
+
588
+ return adjustVInSignature(
589
+ "eth_sign",
590
+ await signMessage(client, {
591
+ account: viemSigner,
592
+ message: {
593
+ raw: toBytes(messageHash)
594
+ }
595
+ })
596
+ )
597
+ },
598
+ async signTransaction(_, __) {
599
+ throw new SignTransactionNotSupportedBySmartAccount()
600
+ },
601
+ async signTypedData(typedData) {
602
+ return adjustVInSignature(
603
+ "eth_signTypedData",
604
+ await signTypedData(client, {
605
+ account: viemSigner,
606
+ domain: {
607
+ chainId: chainId,
608
+ verifyingContract: accountAddress
609
+ },
610
+ types: {
611
+ SafeMessage: [{ name: "message", type: "bytes" }]
612
+ },
613
+ primaryType: "SafeMessage",
614
+ message: {
615
+ message: generateSafeMessageMessage(typedData)
616
+ }
617
+ })
618
+ )
619
+ }
620
+ })
621
+
622
+ return {
623
+ ...account,
624
+ client: client,
625
+ publicKey: accountAddress,
626
+ entryPoint: entryPoint,
627
+ source: "SafeSmartAccount",
628
+ async getNonce() {
629
+ return getAccountNonce(client, {
630
+ sender: accountAddress,
631
+ entryPoint: entryPoint
632
+ })
633
+ },
634
+ async signUserOperation(userOperation) {
635
+ // const timestamps = 0n
636
+
637
+ const signatures = [
638
+ {
639
+ signer: viemSigner.address,
640
+ data: await signTypedData(client, {
641
+ account: viemSigner,
642
+ domain: {
643
+ chainId: chainId,
644
+ verifyingContract: safe4337ModuleAddress
645
+ },
646
+ types: EIP712_SAFE_OPERATION_TYPE,
647
+ primaryType: "SafeOp",
648
+ message: {
649
+ safe: accountAddress,
650
+ callData: userOperation.callData,
651
+ nonce: userOperation.nonce,
652
+ preVerificationGas:
653
+ userOperation.preVerificationGas,
654
+ verificationGasLimit:
655
+ userOperation.verificationGasLimit,
656
+ callGasLimit: userOperation.callGasLimit,
657
+ maxFeePerGas: userOperation.maxFeePerGas,
658
+ maxPriorityFeePerGas:
659
+ userOperation.maxPriorityFeePerGas,
660
+ // signatureTimestamps: timestamps,
661
+ entryPoint: entryPoint
662
+ }
663
+ })
664
+ }
665
+ ]
666
+
667
+ signatures.sort((left, right) =>
668
+ left.signer
669
+ .toLowerCase()
670
+ .localeCompare(right.signer.toLowerCase())
671
+ )
672
+
673
+ let signatureBytes: Address = "0x"
674
+ for (const sig of signatures) {
675
+ signatureBytes += sig.data.slice(2)
676
+ }
677
+
678
+ // const signatureWithTimestamps = encodePacked(
679
+ // ["uint96", "bytes"],
680
+ // [timestamps, signatureBytes]
681
+ // )
682
+
683
+ return signatureBytes
684
+ },
685
+ async getInitCode() {
686
+ const contractCode = await getBytecode(client, {
687
+ address: accountAddress
688
+ })
689
+
690
+ if ((contractCode?.length ?? 0) > 2) return "0x"
691
+
692
+ return getAccountInitCode({
693
+ owner: viemSigner.address,
694
+ addModuleLibAddress,
695
+ safe4337ModuleAddress,
696
+ safeProxyFactoryAddress,
697
+ safeSingletonAddress,
698
+ multiSendAddress,
699
+ saltNonce,
700
+ setupTransactions,
701
+ safeModules
702
+ })
703
+ },
704
+ async encodeDeployCallData(_) {
705
+ throw new Error("Safe account doesn't support account deployment")
706
+ },
707
+ async encodeCallData(args) {
708
+ let to: Address
709
+ let value: bigint
710
+ let data: Hex
711
+
712
+ if (Array.isArray(args)) {
713
+ const argsArray = args as {
714
+ to: Address
715
+ value: bigint
716
+ data: Hex
717
+ }[]
718
+
719
+ to = multiSendCallOnlyAddress
720
+ value = 0n
721
+
722
+ data = encodeMultiSend(
723
+ argsArray.map((tx) => ({ ...tx, operation: 0 }))
724
+ )
725
+ } else {
726
+ const singleTransaction = args as {
727
+ to: Address
728
+ value: bigint
729
+ data: Hex
730
+ }
731
+ to = singleTransaction.to
732
+ data = singleTransaction.data
733
+ value = singleTransaction.value
734
+ }
735
+
736
+ return encodeFunctionData({
737
+ abi: [
738
+ {
739
+ inputs: [
740
+ {
741
+ internalType: "address",
742
+ name: "to",
743
+ type: "address"
744
+ },
745
+ {
746
+ internalType: "uint256",
747
+ name: "value",
748
+ type: "uint256"
749
+ },
750
+ {
751
+ internalType: "bytes",
752
+ name: "data",
753
+ type: "bytes"
754
+ },
755
+ {
756
+ internalType: "uint8",
757
+ name: "operation",
758
+ type: "uint8"
759
+ }
760
+ ],
761
+ name: "executeUserOp",
762
+ outputs: [],
763
+ stateMutability: "nonpayable",
764
+ type: "function"
765
+ }
766
+ ],
767
+ functionName: "executeUserOp",
768
+ args: [to, value, data, 0]
769
+ })
770
+ },
771
+ async getDummySignature() {
772
+ return "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c"
773
+ }
774
+ }
775
+ }