@account-kit/wallet-client 0.1.0-alpha.1 → 0.1.0-alpha.10

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 (258) hide show
  1. package/dist/esm/client/actions/createAccount.d.ts +27 -2
  2. package/dist/esm/client/actions/createAccount.js +25 -0
  3. package/dist/esm/client/actions/createAccount.js.map +1 -1
  4. package/dist/esm/client/actions/formatSign.d.ts +28 -0
  5. package/dist/esm/client/actions/formatSign.js +30 -0
  6. package/dist/esm/client/actions/formatSign.js.map +1 -0
  7. package/dist/esm/client/actions/getCallsStatus.d.ts +22 -4
  8. package/dist/esm/client/actions/getCallsStatus.js +19 -0
  9. package/dist/esm/client/actions/getCallsStatus.js.map +1 -1
  10. package/dist/esm/client/actions/grantPermissions.d.ts +63 -6
  11. package/dist/esm/client/actions/grantPermissions.js +63 -3
  12. package/dist/esm/client/actions/grantPermissions.js.map +1 -1
  13. package/dist/esm/client/actions/listAccounts.d.ts +32 -4
  14. package/dist/esm/client/actions/listAccounts.js +35 -2
  15. package/dist/esm/client/actions/listAccounts.js.map +1 -1
  16. package/dist/esm/client/actions/prepareCalls.d.ts +32 -6
  17. package/dist/esm/client/actions/prepareCalls.js +39 -5
  18. package/dist/esm/client/actions/prepareCalls.js.map +1 -1
  19. package/dist/esm/client/actions/prepareSign.d.ts +25 -0
  20. package/dist/esm/client/actions/prepareSign.js +28 -0
  21. package/dist/esm/client/actions/prepareSign.js.map +1 -0
  22. package/dist/esm/client/actions/requestAccount.d.ts +24 -6
  23. package/dist/esm/client/actions/requestAccount.js +38 -7
  24. package/dist/esm/client/actions/requestAccount.js.map +1 -1
  25. package/dist/esm/client/actions/sendPreparedCalls.d.ts +33 -4
  26. package/dist/esm/client/actions/sendPreparedCalls.js +37 -1
  27. package/dist/esm/client/actions/sendPreparedCalls.js.map +1 -1
  28. package/dist/esm/client/actions/signMessage.d.ts +25 -5
  29. package/dist/esm/client/actions/signMessage.js +22 -2
  30. package/dist/esm/client/actions/signMessage.js.map +1 -1
  31. package/dist/esm/client/actions/signPreparedCalls.d.ts +14 -0
  32. package/dist/esm/client/actions/signPreparedCalls.js +43 -0
  33. package/dist/esm/client/actions/signPreparedCalls.js.map +1 -0
  34. package/dist/esm/client/actions/signSignatureRequest.d.ts +39 -5
  35. package/dist/esm/client/actions/signSignatureRequest.js +69 -11
  36. package/dist/esm/client/actions/signSignatureRequest.js.map +1 -1
  37. package/dist/esm/client/actions/signTypedData.d.ts +37 -4
  38. package/dist/esm/client/actions/signTypedData.js +34 -1
  39. package/dist/esm/client/actions/signTypedData.js.map +1 -1
  40. package/dist/esm/client/client.e2e-test.js +210 -45
  41. package/dist/esm/client/client.e2e-test.js.map +1 -1
  42. package/dist/esm/client/decorator.d.ts +12 -8
  43. package/dist/esm/client/decorator.js +5 -3
  44. package/dist/esm/client/decorator.js.map +1 -1
  45. package/dist/esm/client/index.d.ts +38 -12
  46. package/dist/esm/client/index.js +0 -3
  47. package/dist/esm/client/index.js.map +1 -1
  48. package/dist/esm/exports/index.d.ts +5 -6
  49. package/dist/esm/exports/index.js +4 -6
  50. package/dist/esm/exports/index.js.map +1 -1
  51. package/dist/esm/exports/internal.d.ts +1 -10
  52. package/dist/esm/exports/internal.js +0 -12
  53. package/dist/esm/exports/internal.js.map +1 -1
  54. package/dist/esm/internal/decorator.d.ts +2 -0
  55. package/dist/esm/internal/decorator.js +10 -0
  56. package/dist/esm/internal/decorator.js.map +1 -0
  57. package/dist/esm/isomorphic/actions/createSession.d.ts +1 -2
  58. package/dist/esm/isomorphic/actions/createSession.js +39 -10
  59. package/dist/esm/isomorphic/actions/createSession.js.map +1 -1
  60. package/dist/esm/isomorphic/actions/formatSign.d.ts +8 -0
  61. package/dist/esm/isomorphic/actions/formatSign.js +42 -0
  62. package/dist/esm/isomorphic/actions/formatSign.js.map +1 -0
  63. package/dist/esm/isomorphic/actions/getCallsStatus.d.ts +1 -15
  64. package/dist/esm/isomorphic/actions/getCallsStatus.js +37 -26
  65. package/dist/esm/isomorphic/actions/getCallsStatus.js.map +1 -1
  66. package/dist/esm/isomorphic/actions/prepareCalls.d.ts +1 -2
  67. package/dist/esm/isomorphic/actions/prepareCalls.js +78 -20
  68. package/dist/esm/isomorphic/actions/prepareCalls.js.map +1 -1
  69. package/dist/esm/isomorphic/actions/prepareSign.d.ts +7 -0
  70. package/dist/esm/isomorphic/actions/prepareSign.js +49 -0
  71. package/dist/esm/isomorphic/actions/prepareSign.js.map +1 -0
  72. package/dist/esm/isomorphic/actions/sendPreparedCalls.d.ts +3 -4
  73. package/dist/esm/isomorphic/actions/sendPreparedCalls.js +138 -25
  74. package/dist/esm/isomorphic/actions/sendPreparedCalls.js.map +1 -1
  75. package/dist/esm/isomorphic/client.d.ts +204 -27
  76. package/dist/esm/isomorphic/client.js +6 -2
  77. package/dist/esm/isomorphic/client.js.map +1 -1
  78. package/dist/esm/isomorphic/utils/7702.d.ts +19 -0
  79. package/dist/esm/isomorphic/utils/7702.js +70 -0
  80. package/dist/esm/isomorphic/utils/7702.js.map +1 -0
  81. package/dist/esm/isomorphic/utils/createAccount.d.ts +5 -4
  82. package/dist/esm/isomorphic/utils/createAccount.js +84 -10
  83. package/dist/esm/isomorphic/utils/createAccount.js.map +1 -1
  84. package/dist/esm/isomorphic/utils/createDummySigner.js +3 -3
  85. package/dist/esm/isomorphic/utils/createDummySigner.js.map +1 -1
  86. package/dist/esm/isomorphic/utils/decodeSignature.d.ts +3 -0
  87. package/dist/esm/isomorphic/utils/decodeSignature.js +15 -0
  88. package/dist/esm/isomorphic/utils/decodeSignature.js.map +1 -0
  89. package/dist/esm/isomorphic/utils/parsePermissionsContext.d.ts +6 -5
  90. package/dist/esm/isomorphic/utils/parsePermissionsContext.js +19 -6
  91. package/dist/esm/isomorphic/utils/parsePermissionsContext.js.map +1 -1
  92. package/dist/esm/isomorphic/utils/supportsFeature.d.ts +4 -0
  93. package/dist/esm/isomorphic/utils/supportsFeature.js +21 -0
  94. package/dist/esm/isomorphic/utils/supportsFeature.js.map +1 -0
  95. package/dist/esm/local/client.d.ts +3 -3
  96. package/dist/esm/local/client.js +11 -4
  97. package/dist/esm/local/client.js.map +1 -1
  98. package/dist/esm/remote/client.d.ts +9 -3
  99. package/dist/esm/remote/client.js +10 -11
  100. package/dist/esm/remote/client.js.map +1 -1
  101. package/dist/esm/types.d.ts +27 -13
  102. package/dist/esm/types.js.map +1 -1
  103. package/dist/esm/utils.d.ts +1 -0
  104. package/dist/esm/utils.js.map +1 -1
  105. package/dist/types/client/actions/createAccount.d.ts +27 -2
  106. package/dist/types/client/actions/createAccount.d.ts.map +1 -1
  107. package/dist/types/client/actions/formatSign.d.ts +29 -0
  108. package/dist/types/client/actions/formatSign.d.ts.map +1 -0
  109. package/dist/types/client/actions/getCallsStatus.d.ts +22 -4
  110. package/dist/types/client/actions/getCallsStatus.d.ts.map +1 -1
  111. package/dist/types/client/actions/grantPermissions.d.ts +63 -6
  112. package/dist/types/client/actions/grantPermissions.d.ts.map +1 -1
  113. package/dist/types/client/actions/listAccounts.d.ts +32 -4
  114. package/dist/types/client/actions/listAccounts.d.ts.map +1 -1
  115. package/dist/types/client/actions/prepareCalls.d.ts +32 -6
  116. package/dist/types/client/actions/prepareCalls.d.ts.map +1 -1
  117. package/dist/types/client/actions/prepareSign.d.ts +26 -0
  118. package/dist/types/client/actions/prepareSign.d.ts.map +1 -0
  119. package/dist/types/client/actions/requestAccount.d.ts +24 -6
  120. package/dist/types/client/actions/requestAccount.d.ts.map +1 -1
  121. package/dist/types/client/actions/sendPreparedCalls.d.ts +33 -4
  122. package/dist/types/client/actions/sendPreparedCalls.d.ts.map +1 -1
  123. package/dist/types/client/actions/signMessage.d.ts +25 -5
  124. package/dist/types/client/actions/signMessage.d.ts.map +1 -1
  125. package/dist/types/client/actions/signPreparedCalls.d.ts +15 -0
  126. package/dist/types/client/actions/signPreparedCalls.d.ts.map +1 -0
  127. package/dist/types/client/actions/signSignatureRequest.d.ts +39 -5
  128. package/dist/types/client/actions/signSignatureRequest.d.ts.map +1 -1
  129. package/dist/types/client/actions/signTypedData.d.ts +37 -4
  130. package/dist/types/client/actions/signTypedData.d.ts.map +1 -1
  131. package/dist/types/client/decorator.d.ts +12 -8
  132. package/dist/types/client/decorator.d.ts.map +1 -1
  133. package/dist/types/client/index.d.ts +38 -12
  134. package/dist/types/client/index.d.ts.map +1 -1
  135. package/dist/types/exports/index.d.ts +5 -6
  136. package/dist/types/exports/index.d.ts.map +1 -1
  137. package/dist/types/exports/internal.d.ts +1 -10
  138. package/dist/types/exports/internal.d.ts.map +1 -1
  139. package/dist/types/internal/decorator.d.ts +3 -0
  140. package/dist/types/internal/decorator.d.ts.map +1 -0
  141. package/dist/types/isomorphic/actions/createSession.d.ts +1 -2
  142. package/dist/types/isomorphic/actions/createSession.d.ts.map +1 -1
  143. package/dist/types/isomorphic/actions/formatSign.d.ts +9 -0
  144. package/dist/types/isomorphic/actions/formatSign.d.ts.map +1 -0
  145. package/dist/types/isomorphic/actions/getCallsStatus.d.ts +1 -15
  146. package/dist/types/isomorphic/actions/getCallsStatus.d.ts.map +1 -1
  147. package/dist/types/isomorphic/actions/prepareCalls.d.ts +1 -2
  148. package/dist/types/isomorphic/actions/prepareCalls.d.ts.map +1 -1
  149. package/dist/types/isomorphic/actions/prepareSign.d.ts +8 -0
  150. package/dist/types/isomorphic/actions/prepareSign.d.ts.map +1 -0
  151. package/dist/types/isomorphic/actions/sendPreparedCalls.d.ts +3 -4
  152. package/dist/types/isomorphic/actions/sendPreparedCalls.d.ts.map +1 -1
  153. package/dist/types/isomorphic/client.d.ts +204 -27
  154. package/dist/types/isomorphic/client.d.ts.map +1 -1
  155. package/dist/types/isomorphic/utils/7702.d.ts +20 -0
  156. package/dist/types/isomorphic/utils/7702.d.ts.map +1 -0
  157. package/dist/types/isomorphic/utils/createAccount.d.ts +5 -4
  158. package/dist/types/isomorphic/utils/createAccount.d.ts.map +1 -1
  159. package/dist/types/isomorphic/utils/createDummySigner.d.ts.map +1 -1
  160. package/dist/types/isomorphic/utils/decodeSignature.d.ts +4 -0
  161. package/dist/types/isomorphic/utils/decodeSignature.d.ts.map +1 -0
  162. package/dist/types/isomorphic/utils/parsePermissionsContext.d.ts +6 -5
  163. package/dist/types/isomorphic/utils/parsePermissionsContext.d.ts.map +1 -1
  164. package/dist/types/isomorphic/utils/supportsFeature.d.ts +5 -0
  165. package/dist/types/isomorphic/utils/supportsFeature.d.ts.map +1 -0
  166. package/dist/types/local/client.d.ts +3 -3
  167. package/dist/types/local/client.d.ts.map +1 -1
  168. package/dist/types/remote/client.d.ts +9 -3
  169. package/dist/types/remote/client.d.ts.map +1 -1
  170. package/dist/types/types.d.ts +27 -13
  171. package/dist/types/types.d.ts.map +1 -1
  172. package/dist/types/utils.d.ts +1 -0
  173. package/dist/types/utils.d.ts.map +1 -1
  174. package/package.json +9 -6
  175. package/src/client/actions/createAccount.ts +27 -2
  176. package/src/client/actions/formatSign.ts +53 -0
  177. package/src/client/actions/getCallsStatus.ts +21 -12
  178. package/src/client/actions/grantPermissions.ts +69 -26
  179. package/src/client/actions/listAccounts.ts +44 -6
  180. package/src/client/actions/prepareCalls.ts +46 -26
  181. package/src/client/actions/prepareSign.ts +46 -0
  182. package/src/client/actions/requestAccount.ts +60 -41
  183. package/src/client/actions/sendPreparedCalls.ts +44 -7
  184. package/src/client/actions/signMessage.ts +26 -21
  185. package/src/client/actions/signPreparedCalls.ts +67 -0
  186. package/src/client/actions/signSignatureRequest.ts +84 -20
  187. package/src/client/actions/signTypedData.ts +40 -19
  188. package/src/client/client.e2e-test.ts +256 -54
  189. package/src/client/decorator.ts +28 -26
  190. package/src/client/index.ts +50 -40
  191. package/src/exports/index.ts +5 -13
  192. package/src/exports/internal.ts +2 -10
  193. package/src/internal/decorator.ts +12 -0
  194. package/src/isomorphic/actions/createSession.ts +55 -12
  195. package/src/isomorphic/actions/formatSign.ts +76 -0
  196. package/src/isomorphic/actions/getCallsStatus.ts +46 -32
  197. package/src/isomorphic/actions/prepareCalls.ts +95 -23
  198. package/src/isomorphic/actions/prepareSign.ts +91 -0
  199. package/src/isomorphic/actions/sendPreparedCalls.ts +181 -42
  200. package/src/isomorphic/client.ts +10 -2
  201. package/src/isomorphic/utils/7702.ts +135 -0
  202. package/src/isomorphic/utils/createAccount.ts +101 -13
  203. package/src/isomorphic/utils/createDummySigner.ts +3 -2
  204. package/src/isomorphic/utils/decodeSignature.ts +21 -0
  205. package/src/isomorphic/utils/parsePermissionsContext.ts +29 -11
  206. package/src/isomorphic/utils/supportsFeature.ts +34 -0
  207. package/src/local/client.ts +58 -55
  208. package/src/remote/client.ts +19 -19
  209. package/src/types.ts +29 -23
  210. package/src/utils.ts +2 -0
  211. package/dist/esm/capabilities/index.d.ts +0 -31
  212. package/dist/esm/capabilities/index.js +0 -10
  213. package/dist/esm/capabilities/index.js.map +0 -1
  214. package/dist/esm/capabilities/overrides.d.ts +0 -26
  215. package/dist/esm/capabilities/overrides.js +0 -14
  216. package/dist/esm/capabilities/overrides.js.map +0 -1
  217. package/dist/esm/capabilities/paymaster.d.ts +0 -3
  218. package/dist/esm/capabilities/paymaster.js +0 -5
  219. package/dist/esm/capabilities/paymaster.js.map +0 -1
  220. package/dist/esm/capabilities/permissions/index.d.ts +0 -138
  221. package/dist/esm/capabilities/permissions/index.js +0 -71
  222. package/dist/esm/capabilities/permissions/index.js.map +0 -1
  223. package/dist/esm/capabilities/permissions/mav2.d.ts +0 -36
  224. package/dist/esm/capabilities/permissions/mav2.js +0 -79
  225. package/dist/esm/capabilities/permissions/mav2.js.map +0 -1
  226. package/dist/esm/rpc/request.d.ts +0 -352
  227. package/dist/esm/rpc/request.js +0 -204
  228. package/dist/esm/rpc/request.js.map +0 -1
  229. package/dist/esm/rpc/schema.d.ts +0 -342
  230. package/dist/esm/rpc/schema.js +0 -5
  231. package/dist/esm/rpc/schema.js.map +0 -1
  232. package/dist/esm/schemas.d.ts +0 -216
  233. package/dist/esm/schemas.js +0 -211
  234. package/dist/esm/schemas.js.map +0 -1
  235. package/dist/types/capabilities/index.d.ts +0 -32
  236. package/dist/types/capabilities/index.d.ts.map +0 -1
  237. package/dist/types/capabilities/overrides.d.ts +0 -27
  238. package/dist/types/capabilities/overrides.d.ts.map +0 -1
  239. package/dist/types/capabilities/paymaster.d.ts +0 -4
  240. package/dist/types/capabilities/paymaster.d.ts.map +0 -1
  241. package/dist/types/capabilities/permissions/index.d.ts +0 -139
  242. package/dist/types/capabilities/permissions/index.d.ts.map +0 -1
  243. package/dist/types/capabilities/permissions/mav2.d.ts +0 -37
  244. package/dist/types/capabilities/permissions/mav2.d.ts.map +0 -1
  245. package/dist/types/rpc/request.d.ts +0 -353
  246. package/dist/types/rpc/request.d.ts.map +0 -1
  247. package/dist/types/rpc/schema.d.ts +0 -343
  248. package/dist/types/rpc/schema.d.ts.map +0 -1
  249. package/dist/types/schemas.d.ts +0 -217
  250. package/dist/types/schemas.d.ts.map +0 -1
  251. package/src/capabilities/index.ts +0 -13
  252. package/src/capabilities/overrides.ts +0 -20
  253. package/src/capabilities/paymaster.ts +0 -5
  254. package/src/capabilities/permissions/index.ts +0 -142
  255. package/src/capabilities/permissions/mav2.ts +0 -127
  256. package/src/rpc/request.ts +0 -273
  257. package/src/rpc/schema.ts +0 -40
  258. package/src/schemas.ts +0 -257
@@ -1,16 +1,8 @@
1
1
  // exports from this file are meant to be imported by our packages in the monorepo
2
2
  // we will expose this in the package.json as a named export like `this-pkg/internal`
3
3
  // TODO: name `this-pkg`
4
- export * from "../capabilities/index.js";
5
- export * from "../capabilities/paymaster.js";
6
- export * from "../capabilities/permissions/index.js";
7
- export * from "../capabilities/permissions/mav2.js";
8
4
  export type * from "../isomorphic/client.js";
9
5
  export { createIsomorphicClient } from "../isomorphic/client.js";
10
6
  export { createDummySigner } from "../isomorphic/utils/createDummySigner.js";
11
- export * from "../rpc/request.js";
12
- export * as RpcSchemas from "../rpc/request.js";
13
- export * from "../rpc/schema.js";
14
- export * from "../schemas.js";
15
- export type * from "../types";
16
- export * from "../utils.js";
7
+
8
+ export type * from "../types.ts";
@@ -0,0 +1,12 @@
1
+ import type { CachedAccount, InternalState } from "../types.ts";
2
+
3
+ export function internalStateDecorator(): InternalState {
4
+ let account: CachedAccount | undefined = undefined;
5
+
6
+ return {
7
+ setAccount: (newAccount: CachedAccount) => {
8
+ account = newAccount;
9
+ },
10
+ getAccount: () => account,
11
+ };
12
+ }
@@ -12,18 +12,26 @@ import { Value } from "@sinclair/typebox/value";
12
12
  import {
13
13
  ChainNotFoundError,
14
14
  custom,
15
+ hashTypedData,
15
16
  hexToNumber,
16
17
  toHex,
17
18
  type Chain,
18
19
  type Hex,
19
20
  type Transport,
20
21
  } from "viem";
21
- import { TypePermission } from "../../capabilities/permissions/index.js";
22
- import { isGlobalValidation } from "../../capabilities/permissions/mav2.js";
23
- import type { wallet_createSession } from "../../rpc/request.js";
24
- import type { WalletServerViemRpcSchema } from "../../rpc/schema.js";
22
+ import { InvalidRequestError } from "ox/RpcResponse";
23
+ import {
24
+ TypePermission,
25
+ isGlobalValidation,
26
+ } from "@alchemy/wallet-api-types/capabilities";
27
+ import type {
28
+ wallet_createSession,
29
+ WalletServerViemRpcSchema,
30
+ } from "@alchemy/wallet-api-types/rpc";
25
31
  import { createAccount, isModularAccountV2 } from "../utils/createAccount.js";
26
32
  import { createDummySigner } from "../utils/createDummySigner.js";
33
+ import { supportsFeature } from "../utils/supportsFeature.js";
34
+ import { isDelegated } from "../utils/7702.js";
27
35
 
28
36
  export type CreateSessionParams = Omit<
29
37
  Static<
@@ -57,29 +65,58 @@ export async function createSession(
57
65
  throw new ChainNotFoundError();
58
66
  }
59
67
 
60
- const { counterfactualInfo } = await client.request({
68
+ const { counterfactualInfo, delegation } = await client.request({
61
69
  method: "wallet_requestAccount",
62
70
  params: [
63
71
  {
64
- includeCounterfactualInfo: true,
65
72
  accountAddress: params.account,
73
+ includeCounterfactualInfo: true,
66
74
  },
67
75
  ],
68
76
  });
69
- if (!counterfactualInfo) {
70
- throw new Error("No counterfactual info found.");
77
+
78
+ if (
79
+ delegation &&
80
+ !(await isDelegated(client, {
81
+ address: params.account,
82
+ delegation,
83
+ }))
84
+ ) {
85
+ throw new InvalidRequestError({
86
+ message:
87
+ "7702 account must be delegated before calling `wallet_createSession`",
88
+ });
71
89
  }
72
90
 
91
+ if (!delegation && !counterfactualInfo) {
92
+ throw new InvalidRequestError({
93
+ message: "No counterfactual info found.",
94
+ });
95
+ }
96
+
97
+ if (
98
+ counterfactualInfo &&
99
+ !supportsFeature(counterfactualInfo, "permissions")
100
+ ) {
101
+ throw new InvalidRequestError({
102
+ message: "Account type does not support createSession",
103
+ });
104
+ }
105
+
106
+ // At this point we know the account supports the permission feature
73
107
  const account = await createAccount({
74
108
  chain: client.chain,
75
109
  transport: custom(client.transport),
76
110
  signer: createDummySigner(params.account),
77
111
  accountAddress: params.account,
78
112
  counterfactualInfo,
113
+ delegation,
79
114
  });
80
115
 
81
116
  if (!isModularAccountV2(account)) {
82
- throw new Error("Sessions are currently only supported by MAv2 accounts.");
117
+ throw new InvalidRequestError({
118
+ message: "Sessions are currently only supported by MAv2 accounts.",
119
+ });
83
120
  }
84
121
 
85
122
  const _client = createSmartAccountClient({
@@ -96,10 +133,14 @@ export async function createSession(
96
133
  const { typedData, fullPreSignatureDeferredActionDigest } =
97
134
  await new PermissionBuilder({
98
135
  client: _client,
99
- key: params.key,
136
+ key: {
137
+ ...params.key,
138
+ // Alias 'ecdsa' to 'secp256k1'
139
+ type: params.key.type === "ecdsa" ? "secp256k1" : params.key.type,
140
+ },
100
141
  entityId,
101
142
  nonce,
102
- deadline: params.expiry,
143
+ deadline: params.expirySec,
103
144
  })
104
145
  .addPermissions({
105
146
  permissions: params.permissions.map((permission) =>
@@ -110,10 +151,12 @@ export async function createSession(
110
151
 
111
152
  return {
112
153
  sessionId: null, // In remote mode, the server will set this later.
154
+ chainId: toHex(client.chain.id),
113
155
  entityId: toHex(entityId),
114
156
  signatureRequest: {
115
- type: "eth_signTypedData_v4",
157
+ type: "eth_signTypedData_v4" as const,
116
158
  data: typedData,
159
+ rawPayload: hashTypedData(typedData),
117
160
  },
118
161
  fullPreSignatureDeferredActionDigest,
119
162
  };
@@ -0,0 +1,76 @@
1
+ import type { Static } from "@sinclair/typebox";
2
+ import type { wallet_formatSign } from "@alchemy/wallet-api-types/rpc";
3
+ import type { SmartAccountClient, SmartContractAccount } from "@aa-sdk/core";
4
+ import {
5
+ ChainNotFoundError,
6
+ concat,
7
+ custom,
8
+ encodeAbiParameters,
9
+ parseAbiParameters,
10
+ type Chain,
11
+ type Transport,
12
+ } from "viem";
13
+ import type { WalletServerViemRpcSchema } from "@alchemy/wallet-api-types/rpc";
14
+ import { createAccount } from "../utils/createAccount.js";
15
+ import { createDummySigner } from "../utils/createDummySigner.js";
16
+ import { magicBytes } from "ox/erc6492/WrappedSignature";
17
+
18
+ export type FormatSignParams = Static<
19
+ (typeof wallet_formatSign)["properties"]["Request"]["properties"]["params"]
20
+ >[0];
21
+
22
+ export type FormatSignResult = Static<
23
+ (typeof wallet_formatSign)["properties"]["ReturnType"]
24
+ >;
25
+
26
+ export async function formatSign(
27
+ client: SmartAccountClient<
28
+ Transport,
29
+ Chain,
30
+ SmartContractAccount | undefined,
31
+ Record<string, unknown>,
32
+ WalletServerViemRpcSchema
33
+ >,
34
+ params: FormatSignParams,
35
+ ): Promise<FormatSignResult> {
36
+ if (!client.chain) {
37
+ throw new ChainNotFoundError();
38
+ }
39
+
40
+ const { counterfactualInfo, delegation } = await client.request({
41
+ method: "wallet_requestAccount",
42
+ params: [
43
+ {
44
+ accountAddress: params.from,
45
+ includeCounterfactualInfo: true,
46
+ },
47
+ ],
48
+ });
49
+
50
+ const account = await createAccount({
51
+ chain: client.chain,
52
+ transport: custom(client.transport),
53
+ signer: createDummySigner(params.from),
54
+ accountAddress: params.from,
55
+ counterfactualInfo,
56
+ permissions: params.capabilities?.permissions,
57
+ delegation,
58
+ });
59
+
60
+ let formattedSignature = await account.formatSign(params.signature.data);
61
+
62
+ if (counterfactualInfo && !(await account.isAccountDeployed())) {
63
+ formattedSignature = concat([
64
+ encodeAbiParameters(parseAbiParameters("address, bytes, bytes"), [
65
+ counterfactualInfo.factoryAddress,
66
+ counterfactualInfo.factoryData,
67
+ formattedSignature,
68
+ ]),
69
+ magicBytes,
70
+ ]);
71
+ }
72
+
73
+ return {
74
+ signature: formattedSignature,
75
+ };
76
+ }
@@ -12,9 +12,14 @@ import {
12
12
  type TransactionReceipt,
13
13
  type Transport,
14
14
  } from "viem";
15
- import type { wallet_getCallsStatus } from "../../rpc/request.js";
16
- import type { WalletServerViemRpcSchema } from "../../rpc/schema.js";
17
- import { TypeCallId } from "../../schemas.js";
15
+ import { BaseError } from "ox/RpcResponse";
16
+ import {
17
+ CallStatusCode,
18
+ CallStatusErrorCode,
19
+ type wallet_getCallsStatus,
20
+ type WalletServerViemRpcSchema,
21
+ } from "@alchemy/wallet-api-types/rpc";
22
+ import { TypeCallId } from "@alchemy/wallet-api-types";
18
23
  import { castToHex } from "../../utils.js";
19
24
 
20
25
  export type GetCallsStatusParams = Static<
@@ -25,25 +30,6 @@ export type GetCallsStatusResponse = Static<
25
30
  (typeof wallet_getCallsStatus)["properties"]["ReturnType"]
26
31
  >;
27
32
 
28
- const ReceiptStatus: Record<TransactionReceipt["status"], Hex> = {
29
- reverted: "0x0",
30
- success: "0x1",
31
- };
32
-
33
- /** EIP-5792 call status codes */
34
- export const CallStatusCode = {
35
- /** Batch has been received by the wallet but has not completed execution onchain */
36
- PENDING: 100,
37
- /** Batch has been included onchain without reverts, receipts array contains info of all calls */
38
- CONFIRMED: 200,
39
- /** Batch has not been included onchain and wallet will not retry */
40
- OFFCHAIN_FAILURE: 400,
41
- /** Batch reverted *completely* and only changes related to gas charge may have been included onchain */
42
- CHAIN_RULES_FAILURE: 500,
43
- /** Batch reverted *partially* and some changes related to batch calls may have been included onchain */
44
- PARTIAL_CHAIN_RULES_FAILURE: 600,
45
- } as const;
46
-
47
33
  export async function getCallsStatus(
48
34
  client: SmartAccountClient<
49
35
  Transport,
@@ -57,24 +43,52 @@ export async function getCallsStatus(
57
43
  if (!client.chain) {
58
44
  throw new ChainNotFoundError();
59
45
  }
60
-
61
46
  const { chainId, hash } = Value.Decode(TypeCallId, callId);
62
47
 
63
- const result = await client.getUserOperationReceipt(hash);
64
-
65
- return {
48
+ const baseResp = {
66
49
  id: callId,
67
50
  chainId,
68
51
  atomic: true,
69
- status: !result?.receipt
70
- ? CallStatusCode.PENDING
71
- : result.success
72
- ? CallStatusCode.CONFIRMED
73
- : CallStatusCode.CHAIN_RULES_FAILURE,
74
- receipts: !result?.receipt ? undefined : [transformReceipt(result.receipt)],
52
+ };
53
+
54
+ const result = await client.getUserOperationByHash(hash);
55
+ if (!result) {
56
+ throw new BaseError({
57
+ message: `callId ${callId} not found`,
58
+ code: CallStatusErrorCode.UNKNOWN_BUNDLE_ID,
59
+ });
60
+ }
61
+ if (result && !result.transactionHash) {
62
+ // A result but no txn hash means it's valid but pending.
63
+ return {
64
+ ...baseResp,
65
+ status: CallStatusCode.PENDING,
66
+ };
67
+ }
68
+
69
+ const receipt = await client.getUserOperationReceipt(hash);
70
+ if (!receipt) {
71
+ // Handles edge case of hash being retrieved immediately before the 150 block limit falloff.
72
+ throw new BaseError({
73
+ message: `callId ${callId} not found`,
74
+ code: CallStatusErrorCode.UNKNOWN_BUNDLE_ID,
75
+ });
76
+ }
77
+
78
+ return {
79
+ ...baseResp,
80
+ status: receipt.success
81
+ ? CallStatusCode.CONFIRMED
82
+ : CallStatusCode.CHAIN_RULES_FAILURE,
83
+ receipts: [transformReceipt(receipt.receipt)],
75
84
  };
76
85
  }
77
86
 
87
+ const ReceiptStatus: Record<TransactionReceipt["status"], Hex> = {
88
+ reverted: "0x0",
89
+ success: "0x1",
90
+ };
91
+
78
92
  function transformReceipt(
79
93
  receipt: TransactionReceipt,
80
94
  ): NonNullable<GetCallsStatusResponse["receipts"]>[number] {
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  deepHexlify,
3
+ default7702GasEstimator,
3
4
  type SmartAccountClient,
4
5
  type SmartContractAccount,
5
6
  } from "@aa-sdk/core";
@@ -8,15 +9,21 @@ import {
8
9
  ChainNotFoundError,
9
10
  custom,
10
11
  fromHex,
12
+ hashMessage,
11
13
  toHex,
12
- zeroAddress,
13
14
  type Chain,
14
15
  type Transport,
15
16
  } from "viem";
16
- import type { wallet_prepareCalls } from "../../rpc/request.js";
17
- import type { WalletServerViemRpcSchema } from "../../rpc/schema.js";
17
+ import type {
18
+ wallet_prepareCalls,
19
+ WalletServerViemRpcSchema,
20
+ } from "@alchemy/wallet-api-types/rpc";
18
21
  import { createAccount } from "../utils/createAccount.js";
19
22
  import { createDummySigner } from "../utils/createDummySigner.js";
23
+ import { createAuthorizationRequest, isDelegated } from "../utils/7702.js";
24
+ import { InvalidRequestError } from "ox/RpcResponse";
25
+ import { assertNever } from "../../utils.js";
26
+ import { assertValid7702AccountAddress } from "../utils/7702.js";
20
27
 
21
28
  export type PrepareCallsParams = Omit<
22
29
  Static<
@@ -29,7 +36,6 @@ export type PrepareCallsResult = Static<
29
36
  (typeof wallet_prepareCalls)["properties"]["ReturnType"]
30
37
  >;
31
38
 
32
- // TODO: handle capabilities like permissions and paymaster here
33
39
  export async function prepareCalls(
34
40
  client: SmartAccountClient<
35
41
  Transport,
@@ -44,29 +50,70 @@ export async function prepareCalls(
44
50
  throw new ChainNotFoundError();
45
51
  }
46
52
 
53
+ assertValid7702AccountAddress(params.from, params.capabilities?.eip7702Auth);
54
+
47
55
  // in local mode, we probably want some kind of caching for this
48
- const { counterfactualInfo } = await client.request({
56
+ const { counterfactualInfo, delegation } = await client.request({
49
57
  method: "wallet_requestAccount",
50
58
  params: [
51
- {
52
- accountAddress: params.from,
53
- includeCounterfactualInfo: true,
54
- },
59
+ params.capabilities?.eip7702Auth
60
+ ? {
61
+ signerAddress: params.from,
62
+ creationHint: {
63
+ accountType: "7702",
64
+ },
65
+ includeCounterfactualInfo: true,
66
+ }
67
+ : {
68
+ accountAddress: params.from,
69
+ includeCounterfactualInfo: true,
70
+ },
55
71
  ],
56
72
  });
57
- if (!counterfactualInfo) {
58
- throw new Error("No counterfactual info found.");
73
+
74
+ if (!counterfactualInfo && !delegation) {
75
+ throw new InvalidRequestError({
76
+ message:
77
+ "No counterfactual info or delegated implementation address found.",
78
+ });
59
79
  }
60
80
 
61
81
  const account = await createAccount({
62
82
  chain: client.chain,
63
83
  transport: custom(client.transport),
64
- signer: createDummySigner(zeroAddress),
84
+ signer: createDummySigner(params.from),
65
85
  accountAddress: params.from,
66
86
  counterfactualInfo,
67
- capabilities: params.capabilities,
87
+ permissions: params.capabilities?.permissions,
88
+ delegation,
68
89
  });
69
90
 
91
+ const authorizationRequest =
92
+ delegation &&
93
+ !(await isDelegated(client, {
94
+ address: account.address,
95
+ delegation,
96
+ }))
97
+ ? await createAuthorizationRequest(client, {
98
+ address: account.address,
99
+ delegation,
100
+ })
101
+ : undefined;
102
+
103
+ if (params.capabilities?.permissions && authorizationRequest) {
104
+ // The user shouldn't see this in most cases since we require
105
+ // the account to be delegated before creating the session.
106
+ throw new InvalidRequestError({
107
+ message:
108
+ "When using a 7702 account with a session key, the account must be delegated before preparing calls.",
109
+ });
110
+ }
111
+
112
+ if (authorizationRequest) {
113
+ // @ts-expect-error - this is available but not typed as public
114
+ client.middleware.gasEstimator = default7702GasEstimator();
115
+ }
116
+
70
117
  // TODO: oops we don't actually support setting the policyId as an override here
71
118
  // if we assume that the the isomorphic client is never used directly, then we can assume that this is handled upstream correctly
72
119
  const builtUo = await client.buildUserOperation({
@@ -76,25 +123,50 @@ export async function prepareCalls(
76
123
  value: x.value ? fromHex(x.value, "bigint") : undefined,
77
124
  })),
78
125
  account,
79
- overrides: params.capabilities?.gasParamsOverride,
126
+ overrides: {
127
+ ...params.capabilities?.gasParamsOverride,
128
+ nonceKey: params.capabilities?.nonceOverride?.nonceKey
129
+ ? fromHex(params.capabilities.nonceOverride.nonceKey, "bigint")
130
+ : undefined,
131
+ },
80
132
  });
81
133
 
82
- const uoRequest = deepHexlify(builtUo);
134
+ // The eip7702Auth field should never be included in the UO sig
135
+ // request. It's handled by a separate authorization request.
136
+ if ("eip7702Auth" in builtUo) {
137
+ builtUo.eip7702Auth = undefined;
138
+ }
139
+
140
+ const hexlifiedUo = deepHexlify(builtUo);
141
+
142
+ const ep = account.getEntryPoint();
83
143
 
84
- const hash = account.getEntryPoint().getUserOperationHash(uoRequest);
144
+ const uoHash = ep.getUserOperationHash(hexlifiedUo);
85
145
 
86
- return {
146
+ const uoRequest = {
87
147
  type:
88
- account.getEntryPoint().version === "0.7.0"
89
- ? "user-operation-v070"
90
- : "user-operation-v060",
91
- data: uoRequest,
148
+ ep.version === "0.7.0"
149
+ ? ("user-operation-v070" as const)
150
+ : ep.version === "0.6.0"
151
+ ? ("user-operation-v060" as const)
152
+ : assertNever(ep.version, "Unexpected entry point version"),
153
+ data: hexlifiedUo,
92
154
  chainId: toHex(client.chain.id),
93
155
  signatureRequest: {
94
- type: "personal_sign",
156
+ type: "personal_sign" as const,
95
157
  data: {
96
- raw: hash,
158
+ raw: uoHash,
97
159
  },
160
+ rawPayload: hashMessage({
161
+ raw: uoHash,
162
+ }),
98
163
  },
99
164
  };
165
+
166
+ return authorizationRequest
167
+ ? {
168
+ type: "array" as const,
169
+ data: [authorizationRequest, uoRequest],
170
+ }
171
+ : uoRequest;
100
172
  }
@@ -0,0 +1,91 @@
1
+ import type { Static } from "@sinclair/typebox";
2
+ import type {
3
+ SignatureRequest,
4
+ SmartAccountClient,
5
+ SmartContractAccount,
6
+ } from "@aa-sdk/core";
7
+ import {
8
+ ChainNotFoundError,
9
+ custom,
10
+ toHex,
11
+ type Chain,
12
+ type Transport,
13
+ } from "viem";
14
+ import type {
15
+ wallet_prepareSign,
16
+ WalletServerViemRpcSchema,
17
+ } from "@alchemy/wallet-api-types/rpc";
18
+ import {
19
+ TypeSignableMessage,
20
+ jsonSafeTypedData,
21
+ } from "@alchemy/wallet-api-types";
22
+ import { createAccount } from "../utils/createAccount.js";
23
+ import { createDummySigner } from "../utils/createDummySigner.js";
24
+ import { Value } from "@sinclair/typebox/value";
25
+
26
+ export type PrepareSignParams = Static<
27
+ (typeof wallet_prepareSign)["properties"]["Request"]["properties"]["params"]
28
+ >[0];
29
+
30
+ export type PrepareSignResult = Static<
31
+ (typeof wallet_prepareSign)["properties"]["ReturnType"]
32
+ >;
33
+
34
+ export async function prepareSign(
35
+ client: SmartAccountClient<
36
+ Transport,
37
+ Chain,
38
+ SmartContractAccount | undefined,
39
+ Record<string, unknown>,
40
+ WalletServerViemRpcSchema
41
+ >,
42
+ params: PrepareSignParams,
43
+ ): Promise<PrepareSignResult> {
44
+ if (!client.chain) {
45
+ throw new ChainNotFoundError();
46
+ }
47
+
48
+ const { counterfactualInfo, delegation } = await client.request({
49
+ method: "wallet_requestAccount",
50
+ params: [
51
+ {
52
+ accountAddress: params.from,
53
+ includeCounterfactualInfo: true,
54
+ },
55
+ ],
56
+ });
57
+
58
+ const account = await createAccount({
59
+ chain: client.chain,
60
+ transport: custom(client.transport),
61
+ signer: createDummySigner(params.from),
62
+ accountAddress: params.from,
63
+ counterfactualInfo,
64
+ permissions: params.capabilities?.permissions,
65
+ delegation,
66
+ });
67
+
68
+ const signatureRequest = await account.prepareSign(
69
+ params.signatureRequest as SignatureRequest,
70
+ );
71
+
72
+ if (signatureRequest.type === "personal_sign") {
73
+ return {
74
+ chainId: toHex(client.chain.id),
75
+ signatureRequest: {
76
+ type: signatureRequest.type,
77
+ data: Value.Encode(TypeSignableMessage, signatureRequest.data),
78
+ },
79
+ };
80
+ } else {
81
+ const typedData = signatureRequest.data;
82
+
83
+ return {
84
+ chainId: toHex(client.chain.id),
85
+ signatureRequest: {
86
+ type: signatureRequest.type,
87
+ data: jsonSafeTypedData(typedData),
88
+ },
89
+ };
90
+ }
91
+ }