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

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 (188) hide show
  1. package/dist/esm/capabilities/index.d.ts +5 -2
  2. package/dist/esm/capabilities/index.js +1 -1
  3. package/dist/esm/capabilities/index.js.map +1 -1
  4. package/dist/esm/capabilities/overrides.js +8 -8
  5. package/dist/esm/capabilities/overrides.js.map +1 -1
  6. package/dist/esm/capabilities/permissions/index.d.ts +10 -3
  7. package/dist/esm/capabilities/permissions/index.js +11 -2
  8. package/dist/esm/capabilities/permissions/index.js.map +1 -1
  9. package/dist/esm/capabilities/permissions/mav2.d.ts +3 -3
  10. package/dist/esm/capabilities/permissions/mav2.js +9 -1
  11. package/dist/esm/capabilities/permissions/mav2.js.map +1 -1
  12. package/dist/esm/client/actions/createAccount.d.ts +27 -2
  13. package/dist/esm/client/actions/createAccount.js +25 -0
  14. package/dist/esm/client/actions/createAccount.js.map +1 -1
  15. package/dist/esm/client/actions/getCallsStatus.d.ts +21 -2
  16. package/dist/esm/client/actions/getCallsStatus.js.map +1 -1
  17. package/dist/esm/client/actions/grantPermissions.d.ts +114 -2
  18. package/dist/esm/client/actions/grantPermissions.js +1 -0
  19. package/dist/esm/client/actions/grantPermissions.js.map +1 -1
  20. package/dist/esm/client/actions/listAccounts.d.ts +26 -2
  21. package/dist/esm/client/actions/listAccounts.js +24 -0
  22. package/dist/esm/client/actions/listAccounts.js.map +1 -1
  23. package/dist/esm/client/actions/prepareCalls.d.ts +28 -2
  24. package/dist/esm/client/actions/prepareCalls.js.map +1 -1
  25. package/dist/esm/client/actions/requestAccount.d.ts +21 -2
  26. package/dist/esm/client/actions/requestAccount.js +15 -4
  27. package/dist/esm/client/actions/requestAccount.js.map +1 -1
  28. package/dist/esm/client/actions/sendPreparedCalls.d.ts +39 -2
  29. package/dist/esm/client/actions/sendPreparedCalls.js +37 -0
  30. package/dist/esm/client/actions/sendPreparedCalls.js.map +1 -1
  31. package/dist/esm/client/actions/signMessage.d.ts +22 -2
  32. package/dist/esm/client/actions/signMessage.js +4 -2
  33. package/dist/esm/client/actions/signMessage.js.map +1 -1
  34. package/dist/esm/client/actions/signSignatureRequest.d.ts +34 -0
  35. package/dist/esm/client/actions/signSignatureRequest.js +22 -1
  36. package/dist/esm/client/actions/signSignatureRequest.js.map +1 -1
  37. package/dist/esm/client/actions/signTypedData.d.ts +35 -2
  38. package/dist/esm/client/actions/signTypedData.js +3 -1
  39. package/dist/esm/client/actions/signTypedData.js.map +1 -1
  40. package/dist/esm/client/client.e2e-test.js +112 -30
  41. package/dist/esm/client/client.e2e-test.js.map +1 -1
  42. package/dist/esm/client/decorator.d.ts +7 -5
  43. package/dist/esm/client/decorator.js +1 -1
  44. package/dist/esm/client/decorator.js.map +1 -1
  45. package/dist/esm/client/index.d.ts +4 -4
  46. package/dist/esm/client/index.js +29 -3
  47. package/dist/esm/client/index.js.map +1 -1
  48. package/dist/esm/exports/index.d.ts +1 -0
  49. package/dist/esm/exports/index.js +1 -0
  50. package/dist/esm/exports/index.js.map +1 -1
  51. package/dist/esm/exports/internal.d.ts +1 -1
  52. package/dist/esm/exports/internal.js.map +1 -1
  53. package/dist/esm/internal/decorator.d.ts +2 -0
  54. package/dist/esm/internal/decorator.js +10 -0
  55. package/dist/esm/internal/decorator.js.map +1 -0
  56. package/dist/esm/isomorphic/actions/createSession.js +25 -7
  57. package/dist/esm/isomorphic/actions/createSession.js.map +1 -1
  58. package/dist/esm/isomorphic/actions/prepareCalls.js +35 -12
  59. package/dist/esm/isomorphic/actions/prepareCalls.js.map +1 -1
  60. package/dist/esm/isomorphic/actions/sendPreparedCalls.js +26 -6
  61. package/dist/esm/isomorphic/actions/sendPreparedCalls.js.map +1 -1
  62. package/dist/esm/isomorphic/client.d.ts +11 -5
  63. package/dist/esm/isomorphic/utils/7702.d.ts +11 -0
  64. package/dist/esm/isomorphic/utils/7702.js +26 -0
  65. package/dist/esm/isomorphic/utils/7702.js.map +1 -0
  66. package/dist/esm/isomorphic/utils/createAccount.d.ts +2 -1
  67. package/dist/esm/isomorphic/utils/createAccount.js +30 -5
  68. package/dist/esm/isomorphic/utils/createAccount.js.map +1 -1
  69. package/dist/esm/isomorphic/utils/createDummySigner.js +3 -3
  70. package/dist/esm/isomorphic/utils/createDummySigner.js.map +1 -1
  71. package/dist/esm/isomorphic/utils/parsePermissionsContext.d.ts +2 -1
  72. package/dist/esm/isomorphic/utils/parsePermissionsContext.js +17 -5
  73. package/dist/esm/isomorphic/utils/parsePermissionsContext.js.map +1 -1
  74. package/dist/esm/local/client.d.ts +1 -1
  75. package/dist/esm/local/client.js +10 -2
  76. package/dist/esm/local/client.js.map +1 -1
  77. package/dist/esm/remote/client.d.ts +1 -1
  78. package/dist/esm/remote/client.js +12 -3
  79. package/dist/esm/remote/client.js.map +1 -1
  80. package/dist/esm/rpc/examples.d.ts +230 -0
  81. package/dist/esm/rpc/examples.js +314 -0
  82. package/dist/esm/rpc/examples.js.map +1 -0
  83. package/dist/esm/rpc/request.d.ts +48 -17
  84. package/dist/esm/rpc/request.js +53 -14
  85. package/dist/esm/rpc/request.js.map +1 -1
  86. package/dist/esm/rpc/schema.d.ts +43 -12
  87. package/dist/esm/schemas.d.ts +29 -7
  88. package/dist/esm/schemas.js +120 -38
  89. package/dist/esm/schemas.js.map +1 -1
  90. package/dist/esm/types.d.ts +15 -4
  91. package/dist/esm/types.js.map +1 -1
  92. package/dist/types/capabilities/index.d.ts +5 -2
  93. package/dist/types/capabilities/index.d.ts.map +1 -1
  94. package/dist/types/capabilities/overrides.d.ts.map +1 -1
  95. package/dist/types/capabilities/permissions/index.d.ts +10 -3
  96. package/dist/types/capabilities/permissions/index.d.ts.map +1 -1
  97. package/dist/types/capabilities/permissions/mav2.d.ts +3 -3
  98. package/dist/types/capabilities/permissions/mav2.d.ts.map +1 -1
  99. package/dist/types/client/actions/createAccount.d.ts +27 -2
  100. package/dist/types/client/actions/createAccount.d.ts.map +1 -1
  101. package/dist/types/client/actions/getCallsStatus.d.ts +21 -2
  102. package/dist/types/client/actions/getCallsStatus.d.ts.map +1 -1
  103. package/dist/types/client/actions/grantPermissions.d.ts +114 -2
  104. package/dist/types/client/actions/grantPermissions.d.ts.map +1 -1
  105. package/dist/types/client/actions/listAccounts.d.ts +26 -2
  106. package/dist/types/client/actions/listAccounts.d.ts.map +1 -1
  107. package/dist/types/client/actions/prepareCalls.d.ts +28 -2
  108. package/dist/types/client/actions/prepareCalls.d.ts.map +1 -1
  109. package/dist/types/client/actions/requestAccount.d.ts +21 -2
  110. package/dist/types/client/actions/requestAccount.d.ts.map +1 -1
  111. package/dist/types/client/actions/sendPreparedCalls.d.ts +39 -2
  112. package/dist/types/client/actions/sendPreparedCalls.d.ts.map +1 -1
  113. package/dist/types/client/actions/signMessage.d.ts +22 -2
  114. package/dist/types/client/actions/signMessage.d.ts.map +1 -1
  115. package/dist/types/client/actions/signSignatureRequest.d.ts +34 -0
  116. package/dist/types/client/actions/signSignatureRequest.d.ts.map +1 -1
  117. package/dist/types/client/actions/signTypedData.d.ts +35 -2
  118. package/dist/types/client/actions/signTypedData.d.ts.map +1 -1
  119. package/dist/types/client/decorator.d.ts +7 -5
  120. package/dist/types/client/decorator.d.ts.map +1 -1
  121. package/dist/types/client/index.d.ts +4 -4
  122. package/dist/types/client/index.d.ts.map +1 -1
  123. package/dist/types/exports/index.d.ts +1 -0
  124. package/dist/types/exports/index.d.ts.map +1 -1
  125. package/dist/types/exports/internal.d.ts +1 -1
  126. package/dist/types/exports/internal.d.ts.map +1 -1
  127. package/dist/types/internal/decorator.d.ts +3 -0
  128. package/dist/types/internal/decorator.d.ts.map +1 -0
  129. package/dist/types/isomorphic/actions/createSession.d.ts.map +1 -1
  130. package/dist/types/isomorphic/actions/prepareCalls.d.ts.map +1 -1
  131. package/dist/types/isomorphic/actions/sendPreparedCalls.d.ts.map +1 -1
  132. package/dist/types/isomorphic/client.d.ts +11 -5
  133. package/dist/types/isomorphic/client.d.ts.map +1 -1
  134. package/dist/types/isomorphic/utils/7702.d.ts +12 -0
  135. package/dist/types/isomorphic/utils/7702.d.ts.map +1 -0
  136. package/dist/types/isomorphic/utils/createAccount.d.ts +2 -1
  137. package/dist/types/isomorphic/utils/createAccount.d.ts.map +1 -1
  138. package/dist/types/isomorphic/utils/createDummySigner.d.ts.map +1 -1
  139. package/dist/types/isomorphic/utils/parsePermissionsContext.d.ts +2 -1
  140. package/dist/types/isomorphic/utils/parsePermissionsContext.d.ts.map +1 -1
  141. package/dist/types/local/client.d.ts +1 -1
  142. package/dist/types/local/client.d.ts.map +1 -1
  143. package/dist/types/remote/client.d.ts +1 -1
  144. package/dist/types/remote/client.d.ts.map +1 -1
  145. package/dist/types/rpc/examples.d.ts +231 -0
  146. package/dist/types/rpc/examples.d.ts.map +1 -0
  147. package/dist/types/rpc/request.d.ts +48 -17
  148. package/dist/types/rpc/request.d.ts.map +1 -1
  149. package/dist/types/rpc/schema.d.ts +43 -12
  150. package/dist/types/rpc/schema.d.ts.map +1 -1
  151. package/dist/types/schemas.d.ts +29 -7
  152. package/dist/types/schemas.d.ts.map +1 -1
  153. package/dist/types/types.d.ts +15 -4
  154. package/dist/types/types.d.ts.map +1 -1
  155. package/package.json +8 -6
  156. package/src/capabilities/index.ts +5 -8
  157. package/src/capabilities/overrides.ts +23 -8
  158. package/src/capabilities/permissions/index.ts +21 -5
  159. package/src/capabilities/permissions/mav2.ts +13 -3
  160. package/src/client/actions/createAccount.ts +27 -2
  161. package/src/client/actions/getCallsStatus.ts +21 -2
  162. package/src/client/actions/grantPermissions.ts +114 -2
  163. package/src/client/actions/listAccounts.ts +26 -2
  164. package/src/client/actions/prepareCalls.ts +28 -2
  165. package/src/client/actions/requestAccount.ts +41 -7
  166. package/src/client/actions/sendPreparedCalls.ts +39 -2
  167. package/src/client/actions/signMessage.ts +24 -4
  168. package/src/client/actions/signSignatureRequest.ts +61 -2
  169. package/src/client/actions/signTypedData.ts +39 -3
  170. package/src/client/client.e2e-test.ts +134 -32
  171. package/src/client/decorator.ts +10 -12
  172. package/src/client/index.ts +41 -10
  173. package/src/exports/index.ts +1 -0
  174. package/src/exports/internal.ts +1 -1
  175. package/src/internal/decorator.ts +12 -0
  176. package/src/isomorphic/actions/createSession.ts +28 -7
  177. package/src/isomorphic/actions/prepareCalls.ts +38 -11
  178. package/src/isomorphic/actions/sendPreparedCalls.ts +47 -20
  179. package/src/isomorphic/utils/7702.ts +58 -0
  180. package/src/isomorphic/utils/createAccount.ts +38 -6
  181. package/src/isomorphic/utils/createDummySigner.ts +3 -2
  182. package/src/isomorphic/utils/parsePermissionsContext.ts +23 -7
  183. package/src/local/client.ts +54 -45
  184. package/src/remote/client.ts +22 -7
  185. package/src/rpc/examples.ts +343 -0
  186. package/src/rpc/request.ts +75 -26
  187. package/src/schemas.ts +218 -87
  188. package/src/types.ts +18 -4
@@ -6,6 +6,7 @@ import {
6
6
  import type { Static, StaticDecode } from "@sinclair/typebox";
7
7
  import { Value } from "@sinclair/typebox/value";
8
8
  import {
9
+ BaseError,
9
10
  ChainNotFoundError,
10
11
  concat,
11
12
  concatHex,
@@ -18,6 +19,8 @@ import { decodePermissionsContext } from "../../capabilities/permissions/mav2.js
18
19
  import type { wallet_sendPreparedCalls } from "../../rpc/request.js";
19
20
  import type { WalletServerViemRpcSchema } from "../../rpc/schema.js";
20
21
  import { TypeCallId } from "../../schemas.js";
22
+ import { isSupportedImplementationAddress7702 } from "../utils/7702.js";
23
+ import { InvalidRequestError } from "ox/RpcResponse";
21
24
 
22
25
  export type SendPreparedCallsParams = Omit<
23
26
  StaticDecode<
@@ -45,19 +48,30 @@ export async function sendPreparedCalls(
45
48
  throw new ChainNotFoundError();
46
49
  }
47
50
 
51
+ // One last safety check to be sure the UO wasn't modified to include an unsupported 7702 delegation address.
52
+ if (
53
+ params.signedAuthorization &&
54
+ !isSupportedImplementationAddress7702(params.signedAuthorization.address)
55
+ ) {
56
+ throw new InvalidRequestError({
57
+ message: `Unsupported 7702 delegation address: ${params.signedAuthorization.address}`,
58
+ });
59
+ }
60
+
48
61
  const deferredAction: Hex | undefined = (() => {
49
- if (!params.capabilities?.permissions?.context) {
62
+ if (!params.capabilities?.permissions) {
50
63
  return;
51
64
  }
52
65
 
53
66
  const decodedContext = decodePermissionsContext(
54
- params.capabilities.permissions.context,
67
+ params.capabilities.permissions,
55
68
  );
56
69
 
57
70
  if (decodedContext.contextVersion === "REMOTE_MODE_DEFERRED_ACTION") {
58
- throw new Error(
59
- "Remote mode deferred action not supported in isomorphic client",
60
- );
71
+ throw new InvalidRequestError({
72
+ message:
73
+ "Remote mode deferred action not supported in isomorphic client",
74
+ });
61
75
  }
62
76
 
63
77
  return decodedContext.deferredAction;
@@ -68,21 +82,34 @@ export async function sendPreparedCalls(
68
82
  ? getEntryPoint(client.chain, { version: "0.6.0" })
69
83
  : getEntryPoint(client.chain, { version: "0.7.0" });
70
84
 
71
- const hash = await client.sendRawUserOperation(
72
- {
73
- ...params.data,
74
- signature:
75
- deferredAction != null
76
- ? concatHex([
77
- `0x${deferredAction.slice(68)}`, // Cuts off stuff preprended to the digest (nonce, etc. that we had previously).
78
- "0xff",
79
- "0x00",
80
- params.signature.signature,
81
- ])
82
- : concat(["0xFF", "0x00", params.signature.signature]),
83
- },
84
- entryPoint.address,
85
- );
85
+ const hash = await client
86
+ .sendRawUserOperation(
87
+ {
88
+ ...params.data,
89
+ signature:
90
+ deferredAction != null
91
+ ? concatHex([
92
+ `0x${deferredAction.slice(68)}`, // Cuts off stuff preprended to the digest (nonce, etc. that we had previously).
93
+ "0xff",
94
+ "0x00",
95
+ params.signature.signature,
96
+ ])
97
+ : concat(["0xFF", "0x00", params.signature.signature]),
98
+ eip7702Auth: params.signedAuthorization,
99
+ },
100
+ entryPoint.address,
101
+ )
102
+ .catch((err) => {
103
+ if (
104
+ err instanceof BaseError &&
105
+ err.details.endsWith("is not a contract and initCode is empty")
106
+ ) {
107
+ throw new BaseError(
108
+ `${err.details} (If using 7702, be sure you include the 'signedAuthorization' field in the request parameters)`,
109
+ );
110
+ }
111
+ throw err;
112
+ });
86
113
 
87
114
  const callId = Value.Encode(TypeCallId, {
88
115
  chainId: toHex(client.chain.id),
@@ -0,0 +1,58 @@
1
+ import type { SmartAccountClient, SmartContractAccount } from "@aa-sdk/core";
2
+ import {
3
+ concatHex,
4
+ type Authorization,
5
+ type Address,
6
+ type Chain,
7
+ type Transport,
8
+ } from "viem";
9
+ import type { WalletServerViemRpcSchema } from "../../rpc/schema.js";
10
+
11
+ export const createAuthorization = async (
12
+ client: SmartAccountClient<
13
+ Transport,
14
+ Chain,
15
+ SmartContractAccount | undefined,
16
+ Record<string, unknown>,
17
+ WalletServerViemRpcSchema
18
+ >,
19
+ params: { address: Address; delegation: Address },
20
+ ): Promise<Authorization<number, false> | undefined> => {
21
+ const expectedCode = concatHex(["0xef0100", params.delegation]);
22
+ const code = (await client.getCode({ address: params.address })) ?? "0x";
23
+ if (code.toLowerCase() === expectedCode.toLowerCase()) {
24
+ return undefined; // Already authorized.
25
+ }
26
+ return {
27
+ chainId: client.chain.id,
28
+ address: params.delegation,
29
+ nonce: await client.getTransactionCount({
30
+ address: params.address,
31
+ }),
32
+ };
33
+ };
34
+
35
+ type Supported7702AccountType = "ModularAccountV2";
36
+
37
+ const IMPLEMENTATION_ADDRESS_TO_ACCOUNT_TYPE: Record<
38
+ Address,
39
+ Supported7702AccountType
40
+ > = {
41
+ "0x69007702764179f14F51cdce752f4f775d74E139": "ModularAccountV2",
42
+ };
43
+
44
+ export const getAccountTypeForImplementationAddress7702 = (
45
+ address: Address,
46
+ ): Supported7702AccountType | undefined => {
47
+ return IMPLEMENTATION_ADDRESS_TO_ACCOUNT_TYPE[address];
48
+ };
49
+
50
+ const SUPPORTED_IMPLEMENTATION_ADDRESSES = Object.keys(
51
+ IMPLEMENTATION_ADDRESS_TO_ACCOUNT_TYPE,
52
+ );
53
+
54
+ export const isSupportedImplementationAddress7702 = (
55
+ address: string,
56
+ ): boolean => {
57
+ return SUPPORTED_IMPLEMENTATION_ADDRESSES.includes(address);
58
+ };
@@ -9,13 +9,17 @@ import { concatHex, hexToNumber } from "viem";
9
9
  import type { Capabilities } from "../../capabilities/index.js";
10
10
  import type { TypeSerializedInitcode } from "../../schemas.js";
11
11
  import { parsePermissionsContext } from "./parsePermissionsContext.js";
12
+ import { assertNever } from "../../utils.js";
13
+ import { getAccountTypeForImplementationAddress7702 } from "./7702.js";
14
+ import { InternalError } from "ox/RpcResponse";
12
15
 
13
16
  type CreateAccountParams = {
14
17
  chain: Chain;
15
18
  transport: Transport;
16
19
  signer: SmartAccountSigner;
17
20
  accountAddress: Address;
18
- counterfactualInfo: StaticDecode<typeof TypeSerializedInitcode>;
21
+ counterfactualInfo?: StaticDecode<typeof TypeSerializedInitcode>; // undefined for 7702 accounts
22
+ delegation?: Address;
19
23
  capabilities?: StaticDecode<typeof Capabilities>;
20
24
  };
21
25
 
@@ -28,12 +32,38 @@ export async function createAccount(
28
32
  params: CreateAccountParams,
29
33
  ): Promise<SmartContractAccount> {
30
34
  const { counterfactualInfo: ci, ...accountParams } = params;
31
- // TODO: Implement support for other account types.
32
- if (ci.factoryType !== "MAv2.0.0-sma-b") {
33
- throw new Error("Only MAv2 SMA-B accounts are currently supported");
35
+
36
+ const mode = params.delegation ? "7702" : "default";
37
+
38
+ if (mode === "default") {
39
+ if (!ci) {
40
+ throw new InternalError({
41
+ message: "Counterfactual info not found",
42
+ });
43
+ }
44
+ if (ci.factoryType !== "MAv2.0.0-sma-b") {
45
+ throw new InternalError({
46
+ message: `Factory type ${ci.factoryType} is not currently supported.`,
47
+ });
48
+ }
49
+ } else if (mode === "7702") {
50
+ const accountType = getAccountTypeForImplementationAddress7702(
51
+ params.delegation!,
52
+ );
53
+ if (accountType !== "ModularAccountV2") {
54
+ throw new InternalError({
55
+ message: "7702 mode currently only supports ModularAccountV2",
56
+ });
57
+ }
58
+ } else {
59
+ assertNever(mode, "Unexpected mode in createAccount");
34
60
  }
35
61
 
36
- const parsedContext = parsePermissionsContext(params.capabilities, ci);
62
+ const parsedContext = parsePermissionsContext(
63
+ params.capabilities,
64
+ ci,
65
+ params.delegation,
66
+ );
37
67
 
38
68
  const signerEntity =
39
69
  parsedContext?.contextVersion === "NON_DEFERRED_ACTION"
@@ -43,11 +73,13 @@ export async function createAccount(
43
73
  }
44
74
  : undefined;
45
75
 
76
+ // TODO: clean this up to support different account types.
46
77
  return createModularAccountV2({
47
78
  ...accountParams,
48
79
  signerEntity,
49
80
  deferredAction: parsedContext?.deferredAction,
50
- initCode: concatHex([ci.factoryAddress, ci.factoryData]),
81
+ initCode: ci ? concatHex([ci.factoryAddress, ci.factoryData]) : undefined,
82
+ mode,
51
83
  });
52
84
  }
53
85
 
@@ -1,6 +1,7 @@
1
1
  import { type SmartAccountSigner } from "@aa-sdk/core";
2
2
  import type { TypedData } from "abitype";
3
3
  import {
4
+ BaseError,
4
5
  type Address,
5
6
  type Hex,
6
7
  type SignableMessage,
@@ -15,12 +16,12 @@ export const createDummySigner = (address: Address): SmartAccountSigner => ({
15
16
  },
16
17
  // Not supported on the server
17
18
  signMessage: function (_message: SignableMessage): Promise<Hex> {
18
- throw new Error("Function not implemented.");
19
+ throw new BaseError("signMessage not implemented by dummy signer.");
19
20
  },
20
21
  signTypedData: function <
21
22
  const TTypedData extends TypedData | Record<string, unknown>,
22
23
  TPrimaryType extends keyof TTypedData | "EIP712Domain" = keyof TTypedData,
23
24
  >(_params: TypedDataDefinition<TTypedData, TPrimaryType>): Promise<Hex> {
24
- throw new Error("Function not implemented.");
25
+ throw new BaseError("signTypedData not implemented by dummy signer.");
25
26
  },
26
27
  });
@@ -3,25 +3,41 @@ import { InvalidRequestError } from "ox/RpcResponse";
3
3
  import { decodePermissionsContext } from "../../capabilities/permissions/mav2.js";
4
4
  import type { TypeSerializedInitcode } from "../../schemas.js";
5
5
  import type { PrepareCallsParams } from "../actions/prepareCalls.js";
6
+ import type { Address } from "viem";
7
+ import { getAccountTypeForImplementationAddress7702 } from "./7702.js";
6
8
 
7
9
  export function parsePermissionsContext(
8
10
  capabilities: PrepareCallsParams["capabilities"],
9
- parsedCi: StaticDecode<typeof TypeSerializedInitcode>,
11
+ parsedCi: StaticDecode<typeof TypeSerializedInitcode> | undefined,
12
+ delegation7702?: Address,
10
13
  ) {
11
- if (!capabilities?.permissions?.context) {
14
+ if (!capabilities?.permissions) {
12
15
  return undefined;
13
16
  }
14
17
 
15
- if (
16
- capabilities?.permissions?.context &&
17
- parsedCi.factoryType !== "MAv2.0.0-sma-b"
18
- ) {
18
+ if ("sessionId" in capabilities.permissions) {
19
+ throw new InvalidRequestError({
20
+ message: "Remote permissions are not supported in isomorphic client",
21
+ });
22
+ }
23
+
24
+ if (!("context" in capabilities.permissions)) {
25
+ return undefined;
26
+ }
27
+
28
+ const isMAV2 =
29
+ (parsedCi && parsedCi.factoryType === "MAv2.0.0-sma-b") ||
30
+ (delegation7702 &&
31
+ getAccountTypeForImplementationAddress7702(delegation7702) ===
32
+ "ModularAccountV2");
33
+
34
+ if (!isMAV2) {
19
35
  throw new InvalidRequestError({
20
36
  message: "Permissions are currently only supported by MAv2 accounts",
21
37
  });
22
38
  }
23
39
 
24
- const context = decodePermissionsContext(capabilities.permissions.context);
40
+ const context = decodePermissionsContext(capabilities.permissions);
25
41
 
26
42
  if (context?.contextVersion === "REMOTE_MODE_DEFERRED_ACTION") {
27
43
  throw new InvalidRequestError({
@@ -6,7 +6,8 @@ import {
6
6
  prefixSignatureKeyType,
7
7
  } from "../capabilities/permissions/mav2.js";
8
8
  import { signSignatureRequest } from "../client/actions/signSignatureRequest.js";
9
- import type { PrepareCallsParams } from "../isomorphic/actions/prepareCalls";
9
+ import { internalStateDecorator } from "../internal/decorator.js";
10
+ import type { PrepareCallsParams } from "../isomorphic/actions/prepareCalls.ts";
10
11
  import {
11
12
  createIsomorphicClient,
12
13
  type IsomorphicClient,
@@ -15,7 +16,7 @@ import type {
15
16
  CreateInnerClientParams,
16
17
  InnerClientActions,
17
18
  InnerWalletApiClient,
18
- } from "../types";
19
+ } from "../types.ts";
19
20
  import { assertNever } from "../utils.js";
20
21
 
21
22
  const localMethods = [
@@ -51,46 +52,51 @@ export function createLocalClient(
51
52
  overrides: [
52
53
  {
53
54
  methods: localMethods,
54
- transport: custom({
55
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
- async request(args: { method: LocalMethod; params: any }) {
57
- switch (args.method) {
58
- case "wallet_prepareCalls": {
59
- const params = args.params[0] as PrepareCallsParams;
60
- const capabilityPolicyId =
61
- params.capabilities?.paymasterService?.policyId;
55
+ transport: custom(
56
+ {
57
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
58
+ async request(args: { method: LocalMethod; params: any }) {
59
+ switch (args.method) {
60
+ case "wallet_prepareCalls": {
61
+ const params = args.params[0] as PrepareCallsParams;
62
+ const capabilityPolicyId =
63
+ params.capabilities?.paymasterService?.policyId;
62
64
 
63
- if (
64
- capabilityPolicyId != null &&
65
- policyId !== capabilityPolicyId &&
66
- !clientMap[capabilityPolicyId]
67
- ) {
68
- clientMap[capabilityPolicyId] = createIsomorphicClient({
69
- transport,
70
- chain,
71
- policyId: capabilityPolicyId,
72
- });
73
- }
65
+ if (
66
+ capabilityPolicyId != null &&
67
+ policyId !== capabilityPolicyId &&
68
+ !clientMap[capabilityPolicyId]
69
+ ) {
70
+ clientMap[capabilityPolicyId] = createIsomorphicClient({
71
+ transport,
72
+ chain,
73
+ policyId: capabilityPolicyId,
74
+ });
75
+ }
74
76
 
75
- const client =
76
- clientMap[capabilityPolicyId ?? policyId ?? "default"];
77
- return client.prepareCalls(params);
78
- }
79
- case "wallet_sendPreparedCalls":
80
- return isomorphicClient.sendPreparedCalls(args.params[0]);
81
- case "wallet_getCallsStatus":
82
- return isomorphicClient.getCallsStatus(args.params[0]);
83
- case "wallet_createSession": {
84
- return isomorphicClient.createSession(args.params[0]);
77
+ const client =
78
+ clientMap[capabilityPolicyId ?? policyId ?? "default"];
79
+ return client.prepareCalls(params);
80
+ }
81
+ case "wallet_sendPreparedCalls":
82
+ return isomorphicClient.sendPreparedCalls(args.params[0]);
83
+ case "wallet_getCallsStatus":
84
+ return isomorphicClient.getCallsStatus(args.params[0]);
85
+ case "wallet_createSession": {
86
+ return isomorphicClient.createSession(args.params[0]);
87
+ }
88
+ default:
89
+ return assertNever(
90
+ args.method,
91
+ `Unexpected method: ${args.method}`,
92
+ );
85
93
  }
86
- default:
87
- return assertNever(
88
- args.method,
89
- `Unexpected method: ${args.method}`,
90
- );
91
- }
94
+ },
92
95
  },
93
- }),
96
+ {
97
+ retryCount: 0,
98
+ },
99
+ ),
94
100
  },
95
101
  ],
96
102
  fallback: transport,
@@ -100,10 +106,14 @@ export function createLocalClient(
100
106
  transport: innerTransport,
101
107
  chain,
102
108
  account,
103
- }).extend(() => ({
104
- policyId,
105
- ...innerClientActions(isomorphicClient),
106
- }));
109
+ })
110
+ .extend(() => ({
111
+ policyId,
112
+ ...innerClientActions(isomorphicClient),
113
+ }))
114
+ .extend(() => ({
115
+ internal: internalStateDecorator(),
116
+ }));
107
117
  }
108
118
 
109
119
  const innerClientActions = (
@@ -113,12 +123,10 @@ const innerClientActions = (
113
123
  grantPermissions: async (signer, params) => {
114
124
  const { signatureRequest, fullPreSignatureDeferredActionDigest } =
115
125
  await isomorphicClient.createSession(params);
116
-
117
- const { signature } = await signSignatureRequest(
126
+ const { signedAuthorization, signature } = await signSignatureRequest(
118
127
  signer,
119
128
  signatureRequest,
120
129
  );
121
-
122
130
  return {
123
131
  context: encodePermissionsContext({
124
132
  contextVersion: "LOCAL_MODE_DEFERRED_ACTION",
@@ -127,6 +135,7 @@ const innerClientActions = (
127
135
  sig: prefixSignatureKeyType(signature, "secp256k1"),
128
136
  }),
129
137
  }),
138
+ signedAuthorization,
130
139
  };
131
140
  },
132
141
  };
@@ -1,14 +1,21 @@
1
1
  import { Provider } from "ox";
2
- import { createClient, custom, type Address, type JsonRpcAccount } from "viem";
2
+ import {
3
+ BaseError,
4
+ createClient,
5
+ custom,
6
+ type Address,
7
+ type JsonRpcAccount,
8
+ } from "viem";
3
9
  import { encodePermissionsContext } from "../capabilities/permissions/mav2.js";
4
10
  import { signSignatureRequest } from "../client/actions/signSignatureRequest.js";
11
+ import { internalStateDecorator } from "../internal/decorator.js";
5
12
  import { WalletServerRpcSchema } from "../rpc/schema.js";
6
13
  import type {
7
14
  CreateInnerClientParams,
8
15
  InnerClientActions,
9
16
  InnerWalletApiClient,
10
17
  InnerWalletApiClientBase,
11
- } from "../types";
18
+ } from "../types.ts";
12
19
 
13
20
  export function createRemoteClient<
14
21
  TAccount extends JsonRpcAccount<Address> | undefined =
@@ -34,10 +41,14 @@ export function createRemoteClient(
34
41
  ),
35
42
  chain,
36
43
  account,
37
- }).extend((_client) => ({
38
- policyId: params.policyId,
39
- ...innerClientActions(_client),
40
- }));
44
+ })
45
+ .extend((_client) => ({
46
+ policyId: params.policyId,
47
+ ...innerClientActions(_client),
48
+ }))
49
+ .extend(() => ({
50
+ internal: internalStateDecorator(),
51
+ }));
41
52
 
42
53
  return client;
43
54
  }
@@ -51,7 +62,10 @@ const innerClientActions = (
51
62
  method: "wallet_createSession",
52
63
  params: [params],
53
64
  });
54
- const { signature } = await signSignatureRequest(
65
+ if (signatureRequest.type !== "eth_signTypedData_v4") {
66
+ throw new BaseError("Unexpected signature request type");
67
+ }
68
+ const { signedAuthorization, signature } = await signSignatureRequest(
55
69
  signer,
56
70
  signatureRequest,
57
71
  );
@@ -61,6 +75,7 @@ const innerClientActions = (
61
75
  sessionId,
62
76
  signature,
63
77
  }),
78
+ signedAuthorization,
64
79
  };
65
80
  },
66
81
  };