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

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 (146) hide show
  1. package/dist/esm/client/actions/getCallsStatus.d.ts +4 -3
  2. package/dist/esm/client/actions/getCallsStatus.js +1 -0
  3. package/dist/esm/client/actions/getCallsStatus.js.map +1 -1
  4. package/dist/esm/client/actions/grantPermissions.d.ts +5 -4
  5. package/dist/esm/client/actions/grantPermissions.js +21 -5
  6. package/dist/esm/client/actions/grantPermissions.js.map +1 -1
  7. package/dist/esm/client/actions/prepareCalls.d.ts +4 -3
  8. package/dist/esm/client/actions/prepareCalls.js +1 -0
  9. package/dist/esm/client/actions/prepareCalls.js.map +1 -1
  10. package/dist/esm/client/actions/prepareSign.d.ts +4 -3
  11. package/dist/esm/client/actions/prepareSign.js +1 -0
  12. package/dist/esm/client/actions/prepareSign.js.map +1 -1
  13. package/dist/esm/client/actions/requestAccount.js +3 -3
  14. package/dist/esm/client/actions/requestAccount.js.map +1 -1
  15. package/dist/esm/client/actions/signSignatureRequest.d.ts +3 -3
  16. package/dist/esm/client/actions/signSignatureRequest.js.map +1 -1
  17. package/dist/esm/client/client.e2e-test.js +190 -329
  18. package/dist/esm/client/client.e2e-test.js.map +1 -1
  19. package/dist/esm/client/index.d.ts +1 -4
  20. package/dist/esm/client/index.js +13 -18
  21. package/dist/esm/client/index.js.map +1 -1
  22. package/dist/esm/{isomorphic/utils/createAccount.d.ts → internal/account.d.ts} +3 -7
  23. package/dist/esm/{isomorphic/utils/createAccount.js → internal/account.js} +9 -33
  24. package/dist/esm/internal/account.js.map +1 -0
  25. package/dist/esm/types.d.ts +2 -8
  26. package/dist/esm/types.js.map +1 -1
  27. package/dist/types/client/actions/getCallsStatus.d.ts +4 -3
  28. package/dist/types/client/actions/getCallsStatus.d.ts.map +1 -1
  29. package/dist/types/client/actions/grantPermissions.d.ts +5 -4
  30. package/dist/types/client/actions/grantPermissions.d.ts.map +1 -1
  31. package/dist/types/client/actions/prepareCalls.d.ts +4 -3
  32. package/dist/types/client/actions/prepareCalls.d.ts.map +1 -1
  33. package/dist/types/client/actions/prepareSign.d.ts +4 -3
  34. package/dist/types/client/actions/prepareSign.d.ts.map +1 -1
  35. package/dist/types/client/actions/requestAccount.d.ts.map +1 -1
  36. package/dist/types/client/actions/signSignatureRequest.d.ts +3 -3
  37. package/dist/types/client/actions/signSignatureRequest.d.ts.map +1 -1
  38. package/dist/types/client/index.d.ts +1 -4
  39. package/dist/types/client/index.d.ts.map +1 -1
  40. package/dist/types/{isomorphic/utils/createAccount.d.ts → internal/account.d.ts} +4 -8
  41. package/dist/types/internal/account.d.ts.map +1 -0
  42. package/dist/types/types.d.ts +2 -8
  43. package/dist/types/types.d.ts.map +1 -1
  44. package/package.json +3 -8
  45. package/src/client/actions/getCallsStatus.ts +8 -6
  46. package/src/client/actions/grantPermissions.ts +41 -10
  47. package/src/client/actions/prepareCalls.ts +11 -6
  48. package/src/client/actions/prepareSign.ts +9 -6
  49. package/src/client/actions/requestAccount.ts +3 -3
  50. package/src/client/actions/signSignatureRequest.ts +8 -8
  51. package/src/client/client.e2e-test.ts +143 -298
  52. package/src/client/index.ts +22 -23
  53. package/src/{isomorphic/utils/createAccount.ts → internal/account.ts} +11 -51
  54. package/src/types.ts +4 -15
  55. package/dist/esm/exports/internal.d.ts +0 -4
  56. package/dist/esm/exports/internal.js +0 -3
  57. package/dist/esm/exports/internal.js.map +0 -1
  58. package/dist/esm/isomorphic/actions/createSession.d.ts +0 -13
  59. package/dist/esm/isomorphic/actions/createSession.js +0 -94
  60. package/dist/esm/isomorphic/actions/createSession.js.map +0 -1
  61. package/dist/esm/isomorphic/actions/formatSign.d.ts +0 -8
  62. package/dist/esm/isomorphic/actions/formatSign.js +0 -42
  63. package/dist/esm/isomorphic/actions/formatSign.js.map +0 -1
  64. package/dist/esm/isomorphic/actions/getCallsStatus.d.ts +0 -7
  65. package/dist/esm/isomorphic/actions/getCallsStatus.js +0 -71
  66. package/dist/esm/isomorphic/actions/getCallsStatus.js.map +0 -1
  67. package/dist/esm/isomorphic/actions/prepareCalls.d.ts +0 -7
  68. package/dist/esm/isomorphic/actions/prepareCalls.js +0 -116
  69. package/dist/esm/isomorphic/actions/prepareCalls.js.map +0 -1
  70. package/dist/esm/isomorphic/actions/prepareSign.d.ts +0 -7
  71. package/dist/esm/isomorphic/actions/prepareSign.js +0 -49
  72. package/dist/esm/isomorphic/actions/prepareSign.js.map +0 -1
  73. package/dist/esm/isomorphic/actions/sendPreparedCalls.d.ts +0 -7
  74. package/dist/esm/isomorphic/actions/sendPreparedCalls.js +0 -156
  75. package/dist/esm/isomorphic/actions/sendPreparedCalls.js.map +0 -1
  76. package/dist/esm/isomorphic/client.d.ts +0 -275
  77. package/dist/esm/isomorphic/client.js +0 -41
  78. package/dist/esm/isomorphic/client.js.map +0 -1
  79. package/dist/esm/isomorphic/utils/7702.d.ts +0 -19
  80. package/dist/esm/isomorphic/utils/7702.js +0 -70
  81. package/dist/esm/isomorphic/utils/7702.js.map +0 -1
  82. package/dist/esm/isomorphic/utils/createAccount.js.map +0 -1
  83. package/dist/esm/isomorphic/utils/createDummySigner.d.ts +0 -3
  84. package/dist/esm/isomorphic/utils/createDummySigner.js +0 -17
  85. package/dist/esm/isomorphic/utils/createDummySigner.js.map +0 -1
  86. package/dist/esm/isomorphic/utils/decodeSignature.d.ts +0 -3
  87. package/dist/esm/isomorphic/utils/decodeSignature.js +0 -15
  88. package/dist/esm/isomorphic/utils/decodeSignature.js.map +0 -1
  89. package/dist/esm/isomorphic/utils/parsePermissionsContext.d.ts +0 -21
  90. package/dist/esm/isomorphic/utils/parsePermissionsContext.js +0 -34
  91. package/dist/esm/isomorphic/utils/parsePermissionsContext.js.map +0 -1
  92. package/dist/esm/isomorphic/utils/supportsFeature.d.ts +0 -4
  93. package/dist/esm/isomorphic/utils/supportsFeature.js +0 -21
  94. package/dist/esm/isomorphic/utils/supportsFeature.js.map +0 -1
  95. package/dist/esm/local/client.d.ts +0 -3
  96. package/dist/esm/local/client.js +0 -97
  97. package/dist/esm/local/client.js.map +0 -1
  98. package/dist/esm/remote/client.d.ts +0 -9
  99. package/dist/esm/remote/client.js +0 -41
  100. package/dist/esm/remote/client.js.map +0 -1
  101. package/dist/types/exports/internal.d.ts +0 -5
  102. package/dist/types/exports/internal.d.ts.map +0 -1
  103. package/dist/types/isomorphic/actions/createSession.d.ts +0 -14
  104. package/dist/types/isomorphic/actions/createSession.d.ts.map +0 -1
  105. package/dist/types/isomorphic/actions/formatSign.d.ts +0 -9
  106. package/dist/types/isomorphic/actions/formatSign.d.ts.map +0 -1
  107. package/dist/types/isomorphic/actions/getCallsStatus.d.ts +0 -8
  108. package/dist/types/isomorphic/actions/getCallsStatus.d.ts.map +0 -1
  109. package/dist/types/isomorphic/actions/prepareCalls.d.ts +0 -8
  110. package/dist/types/isomorphic/actions/prepareCalls.d.ts.map +0 -1
  111. package/dist/types/isomorphic/actions/prepareSign.d.ts +0 -8
  112. package/dist/types/isomorphic/actions/prepareSign.d.ts.map +0 -1
  113. package/dist/types/isomorphic/actions/sendPreparedCalls.d.ts +0 -8
  114. package/dist/types/isomorphic/actions/sendPreparedCalls.d.ts.map +0 -1
  115. package/dist/types/isomorphic/client.d.ts +0 -276
  116. package/dist/types/isomorphic/client.d.ts.map +0 -1
  117. package/dist/types/isomorphic/utils/7702.d.ts +0 -20
  118. package/dist/types/isomorphic/utils/7702.d.ts.map +0 -1
  119. package/dist/types/isomorphic/utils/createAccount.d.ts.map +0 -1
  120. package/dist/types/isomorphic/utils/createDummySigner.d.ts +0 -4
  121. package/dist/types/isomorphic/utils/createDummySigner.d.ts.map +0 -1
  122. package/dist/types/isomorphic/utils/decodeSignature.d.ts +0 -4
  123. package/dist/types/isomorphic/utils/decodeSignature.d.ts.map +0 -1
  124. package/dist/types/isomorphic/utils/parsePermissionsContext.d.ts +0 -22
  125. package/dist/types/isomorphic/utils/parsePermissionsContext.d.ts.map +0 -1
  126. package/dist/types/isomorphic/utils/supportsFeature.d.ts +0 -5
  127. package/dist/types/isomorphic/utils/supportsFeature.d.ts.map +0 -1
  128. package/dist/types/local/client.d.ts +0 -4
  129. package/dist/types/local/client.d.ts.map +0 -1
  130. package/dist/types/remote/client.d.ts +0 -10
  131. package/dist/types/remote/client.d.ts.map +0 -1
  132. package/src/exports/internal.ts +0 -8
  133. package/src/isomorphic/actions/createSession.ts +0 -163
  134. package/src/isomorphic/actions/formatSign.ts +0 -76
  135. package/src/isomorphic/actions/getCallsStatus.ts +0 -112
  136. package/src/isomorphic/actions/prepareCalls.ts +0 -172
  137. package/src/isomorphic/actions/prepareSign.ts +0 -91
  138. package/src/isomorphic/actions/sendPreparedCalls.ts +0 -234
  139. package/src/isomorphic/client.ts +0 -102
  140. package/src/isomorphic/utils/7702.ts +0 -135
  141. package/src/isomorphic/utils/createDummySigner.ts +0 -27
  142. package/src/isomorphic/utils/decodeSignature.ts +0 -21
  143. package/src/isomorphic/utils/parsePermissionsContext.ts +0 -51
  144. package/src/isomorphic/utils/supportsFeature.ts +0 -34
  145. package/src/local/client.ts +0 -136
  146. package/src/remote/client.ts +0 -67
@@ -1,112 +0,0 @@
1
- import {
2
- type SmartAccountClient,
3
- type SmartContractAccount,
4
- } from "@aa-sdk/core";
5
- import type { Static } from "@sinclair/typebox";
6
- import { Value } from "@sinclair/typebox/value";
7
- import {
8
- ChainNotFoundError,
9
- isHex,
10
- type Chain,
11
- type Hex,
12
- type TransactionReceipt,
13
- type Transport,
14
- } from "viem";
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";
23
- import { castToHex } from "../../utils.js";
24
-
25
- export type GetCallsStatusParams = Static<
26
- (typeof wallet_getCallsStatus)["properties"]["Request"]["properties"]["params"]
27
- >[0];
28
-
29
- export type GetCallsStatusResponse = Static<
30
- (typeof wallet_getCallsStatus)["properties"]["ReturnType"]
31
- >;
32
-
33
- export async function getCallsStatus(
34
- client: SmartAccountClient<
35
- Transport,
36
- Chain,
37
- SmartContractAccount | undefined,
38
- Record<string, unknown>,
39
- WalletServerViemRpcSchema
40
- >,
41
- callId: GetCallsStatusParams,
42
- ): Promise<GetCallsStatusResponse> {
43
- if (!client.chain) {
44
- throw new ChainNotFoundError();
45
- }
46
- const { chainId, hash } = Value.Decode(TypeCallId, callId);
47
-
48
- const baseResp = {
49
- id: callId,
50
- chainId,
51
- atomic: true,
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)],
84
- };
85
- }
86
-
87
- const ReceiptStatus: Record<TransactionReceipt["status"], Hex> = {
88
- reverted: "0x0",
89
- success: "0x1",
90
- };
91
-
92
- function transformReceipt(
93
- receipt: TransactionReceipt,
94
- ): NonNullable<GetCallsStatusResponse["receipts"]>[number] {
95
- return {
96
- // viem's type for status is "success" | "reverted", but the actual value seems to already be 0x0 or 0x1
97
- status: isHex(receipt.status)
98
- ? receipt.status
99
- : ReceiptStatus[receipt.status],
100
- blockHash: receipt.blockHash,
101
- // viem's type for blockNumber is bigint, but the actual value seems to already be a hex string
102
- blockNumber: castToHex(receipt.blockNumber),
103
- // viem's type for gasUsed is bigint, but the actual value seems to already be a hex string
104
- gasUsed: castToHex(receipt.gasUsed),
105
- transactionHash: receipt.transactionHash,
106
- logs: receipt.logs.map((log) => ({
107
- address: log.address,
108
- data: log.data,
109
- topics: log.topics,
110
- })),
111
- };
112
- }
@@ -1,172 +0,0 @@
1
- import {
2
- deepHexlify,
3
- default7702GasEstimator,
4
- type SmartAccountClient,
5
- type SmartContractAccount,
6
- } from "@aa-sdk/core";
7
- import type { Static } from "@sinclair/typebox";
8
- import {
9
- ChainNotFoundError,
10
- custom,
11
- fromHex,
12
- hashMessage,
13
- toHex,
14
- type Chain,
15
- type Transport,
16
- } from "viem";
17
- import type {
18
- wallet_prepareCalls,
19
- WalletServerViemRpcSchema,
20
- } from "@alchemy/wallet-api-types/rpc";
21
- import { createAccount } from "../utils/createAccount.js";
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";
27
-
28
- export type PrepareCallsParams = Omit<
29
- Static<
30
- (typeof wallet_prepareCalls)["properties"]["Request"]["properties"]["params"]
31
- >[0],
32
- "chainId"
33
- >;
34
-
35
- export type PrepareCallsResult = Static<
36
- (typeof wallet_prepareCalls)["properties"]["ReturnType"]
37
- >;
38
-
39
- export async function prepareCalls(
40
- client: SmartAccountClient<
41
- Transport,
42
- Chain,
43
- SmartContractAccount | undefined,
44
- Record<string, unknown>,
45
- WalletServerViemRpcSchema
46
- >,
47
- params: PrepareCallsParams,
48
- ): Promise<PrepareCallsResult> {
49
- if (!client.chain) {
50
- throw new ChainNotFoundError();
51
- }
52
-
53
- assertValid7702AccountAddress(params.from, params.capabilities?.eip7702Auth);
54
-
55
- // in local mode, we probably want some kind of caching for this
56
- const { counterfactualInfo, delegation } = await client.request({
57
- method: "wallet_requestAccount",
58
- params: [
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
- },
71
- ],
72
- });
73
-
74
- if (!counterfactualInfo && !delegation) {
75
- throw new InvalidRequestError({
76
- message:
77
- "No counterfactual info or delegated implementation address found.",
78
- });
79
- }
80
-
81
- const account = await createAccount({
82
- chain: client.chain,
83
- transport: custom(client.transport),
84
- signer: createDummySigner(params.from),
85
- accountAddress: params.from,
86
- counterfactualInfo,
87
- permissions: params.capabilities?.permissions,
88
- delegation,
89
- });
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
-
117
- // TODO: oops we don't actually support setting the policyId as an override here
118
- // if we assume that the the isomorphic client is never used directly, then we can assume that this is handled upstream correctly
119
- const builtUo = await client.buildUserOperation({
120
- uo: params.calls.map((x) => ({
121
- target: x.to,
122
- data: x.data ?? "0x",
123
- value: x.value ? fromHex(x.value, "bigint") : undefined,
124
- })),
125
- account,
126
- overrides: {
127
- ...params.capabilities?.gasParamsOverride,
128
- nonceKey: params.capabilities?.nonceOverride?.nonceKey
129
- ? fromHex(params.capabilities.nonceOverride.nonceKey, "bigint")
130
- : undefined,
131
- },
132
- });
133
-
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();
143
-
144
- const uoHash = ep.getUserOperationHash(hexlifiedUo);
145
-
146
- const uoRequest = {
147
- type:
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,
154
- chainId: toHex(client.chain.id),
155
- signatureRequest: {
156
- type: "personal_sign" as const,
157
- data: {
158
- raw: uoHash,
159
- },
160
- rawPayload: hashMessage({
161
- raw: uoHash,
162
- }),
163
- },
164
- };
165
-
166
- return authorizationRequest
167
- ? {
168
- type: "array" as const,
169
- data: [authorizationRequest, uoRequest],
170
- }
171
- : uoRequest;
172
- }
@@ -1,91 +0,0 @@
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
- }
@@ -1,234 +0,0 @@
1
- import {
2
- getEntryPoint,
3
- type SmartAccountClient,
4
- type SmartContractAccount,
5
- } from "@aa-sdk/core";
6
- import { Value } from "@sinclair/typebox/value";
7
- import {
8
- BaseError,
9
- ChainNotFoundError,
10
- concat,
11
- concatHex,
12
- numberToHex,
13
- parseSignature,
14
- toHex,
15
- type Address,
16
- type Chain,
17
- type Hex,
18
- type Transport,
19
- } from "viem";
20
- import type {
21
- wallet_sendPreparedCalls,
22
- WalletServerViemRpcSchema,
23
- } from "@alchemy/wallet-api-types/rpc";
24
- import { decodePermissionsContext } from "@alchemy/wallet-api-types/capabilities";
25
- import { TypeCallId } from "@alchemy/wallet-api-types";
26
- import { isSupportedDelegationAddress7702 } from "../utils/7702.js";
27
- import { InvalidRequestError } from "ox/RpcResponse";
28
- import { assertNever } from "../../utils.js";
29
- import type { Static } from "@sinclair/typebox";
30
- import { decodeSignature } from "../utils/decodeSignature.js";
31
-
32
- export type SendPreparedCallsParams = Static<
33
- (typeof wallet_sendPreparedCalls)["properties"]["Request"]["properties"]["params"]
34
- >[0];
35
-
36
- export type SendPreparedCallsResult = Static<
37
- (typeof wallet_sendPreparedCalls)["properties"]["ReturnType"]
38
- >;
39
-
40
- // TODO: this only supports MAv2 right now, we need to fix this
41
- export async function sendPreparedCalls(
42
- client: SmartAccountClient<
43
- Transport,
44
- Chain,
45
- SmartContractAccount | undefined,
46
- Record<string, unknown>,
47
- WalletServerViemRpcSchema
48
- >,
49
- params: SendPreparedCallsParams,
50
- ): Promise<SendPreparedCallsResult> {
51
- if (!client.chain) {
52
- throw new ChainNotFoundError();
53
- }
54
-
55
- const deferredAction: Hex | undefined = (() => {
56
- if (!params.capabilities?.permissions) {
57
- return;
58
- }
59
-
60
- const decodedContext = decodePermissionsContext(
61
- params.capabilities.permissions,
62
- );
63
-
64
- if (decodedContext.contextVersion === "REMOTE_MODE_DEFERRED_ACTION") {
65
- throw new InvalidRequestError({
66
- message:
67
- "Remote mode deferred action not supported in isomorphic client",
68
- });
69
- }
70
-
71
- return decodedContext.deferredAction;
72
- })();
73
-
74
- const userOps =
75
- params.type === "array"
76
- ? params.data.filter((it) => {
77
- const isUserOp =
78
- it.type === "user-operation-v060" ||
79
- it.type === "user-operation-v070";
80
- if (isUserOp && it.chainId !== toHex(client.chain.id)) {
81
- throw new InvalidRequestError({
82
- message:
83
- "Multiple chain IDs in a single request are not currently supported.",
84
- });
85
- }
86
- return isUserOp;
87
- })
88
- : [params];
89
-
90
- if (!userOps.length) {
91
- throw new InvalidRequestError({
92
- message: "Calls must include at least one user operation",
93
- });
94
- }
95
-
96
- const authorizations =
97
- params.type === "array"
98
- ? params.data.filter((it) => it.type === "authorization")
99
- : [];
100
-
101
- if (authorizations.length > 1) {
102
- throw new InvalidRequestError({
103
- message:
104
- "Multiple authorizations in a single request are not currently supported",
105
- });
106
- }
107
- const [authorization] = authorizations;
108
-
109
- // One last safety check to be sure the UO wasn't modified to include an unsupported 7702 delegation address.
110
- if (
111
- authorization &&
112
- !isSupportedDelegationAddress7702(authorization.data.address)
113
- ) {
114
- throw new InvalidRequestError({
115
- message: `Unsupported 7702 delegation address: ${authorization.data.address}`,
116
- });
117
- }
118
-
119
- const hashes = await Promise.all(
120
- userOps.map(async (userOp, idx) => {
121
- const ep: { address: Address } =
122
- userOp.type === "user-operation-v060"
123
- ? getEntryPoint(client.chain, { version: "0.6.0" })
124
- : userOp.type === "user-operation-v070"
125
- ? getEntryPoint(client.chain, { version: "0.7.0" })
126
- : assertNever(userOp, "Unexpected user op type");
127
- const authSig = authorization
128
- ? parseSignature(decodeSignature(authorization.signature).data)
129
- : undefined;
130
- const uoSigHex = decodeSignature(userOp.signature).data;
131
-
132
- const { counterfactualInfo, delegation } = await client.request({
133
- method: "wallet_requestAccount",
134
- params: [
135
- {
136
- accountAddress: userOp.data.sender,
137
- includeCounterfactualInfo: true,
138
- },
139
- ],
140
- });
141
-
142
- if (!counterfactualInfo && !delegation) {
143
- throw new InvalidRequestError({
144
- message:
145
- "No counterfactual info or delegated implementation address found.",
146
- });
147
- }
148
-
149
- const factoryType = counterfactualInfo?.factoryType;
150
-
151
- // build signature based on account type
152
- const signature = (() => {
153
- switch (factoryType) {
154
- // light accounts
155
- case "LightAccountV1.0.1":
156
- case "LightAccountV1.0.2":
157
- case "LightAccountV1.1.0":
158
- case "MAv1.0.0-MultiOwner":
159
- // For LAv1 and MAv1-MultiOwner, we always just pass the signature.
160
- return uoSigHex;
161
- case "LightAccountV2.0.0":
162
- // for LAv2, we need to prepend the "SignatureType.EOA" byte.
163
- // TODO: Once we support nested smart accounts, switch this byte depending on the signature type.
164
- return concat(["0x00", uoSigHex]);
165
- case undefined: // undefined defaults to sma-b
166
- case "MAv2.0.0-sma-b":
167
- // For sma-b, we need to handle deferred actions if needed and prepend the "Reserved
168
- // Signature Segment" and "SignatureType.EOA" bytes
169
- return deferredAction != null
170
- ? concatHex([
171
- `0x${deferredAction.slice(68)}`, // Cuts off stuff prepended to the digest (nonce, etc.).
172
- "0xFF",
173
- "0x00",
174
- uoSigHex,
175
- ])
176
- : concat(["0xFF", "0x00", uoSigHex]);
177
- case "LightAccountV2.0.0-MultiOwner":
178
- // for LAv2-MultiOwner, we need to prepend the "SignatureType.EOA" byte
179
- // TODO: Once we support nested smart accounts, switch this byte depending on the signature type, and add the smart account signer's address.
180
- return concat(["0x00", uoSigHex]);
181
- case "MAv2.0.0-ma-ssv":
182
- case "MAv2.0.0-ma-webauthn":
183
- case "MAv1.0.0-MultiSig":
184
- case "unknown":
185
- throw new InvalidRequestError({
186
- message: `Unsupported factory type: ${factoryType}`,
187
- });
188
- default:
189
- return assertNever(factoryType, "Unsupported factory type");
190
- }
191
- })();
192
-
193
- return client
194
- .sendRawUserOperation(
195
- {
196
- ...userOp.data,
197
- signature,
198
- eip7702Auth:
199
- idx === 0 && authorization && authSig
200
- ? {
201
- ...authorization.data,
202
- chainId: authorization.chainId,
203
- ...{
204
- ...authSig,
205
- yParity: numberToHex(authSig.yParity),
206
- },
207
- }
208
- : undefined,
209
- },
210
- ep.address,
211
- )
212
- .catch((err) => {
213
- if (
214
- err instanceof BaseError &&
215
- err.details.endsWith("is not a contract and initCode is empty")
216
- ) {
217
- throw new BaseError(
218
- `${err.details} (If using 7702, be sure you include the signed authorization in the request parameters)`,
219
- );
220
- }
221
- throw err;
222
- });
223
- }),
224
- );
225
-
226
- return {
227
- preparedCallIds: hashes.map((hash) =>
228
- Value.Encode(TypeCallId, {
229
- chainId: toHex(client.chain.id),
230
- hash,
231
- }),
232
- ),
233
- };
234
- }
@@ -1,102 +0,0 @@
1
- import {
2
- createSmartAccountClient,
3
- type ClientMiddlewareFn,
4
- type SmartAccountClient,
5
- type SmartAccountClientRpcSchema,
6
- type SmartContractAccount,
7
- } from "@aa-sdk/core";
8
- import {
9
- alchemyFeeEstimator,
10
- alchemyGasAndPaymasterAndDataMiddleware,
11
- alchemyGasManagerMiddleware,
12
- type AlchemyTransport,
13
- } from "@account-kit/infra";
14
- import { type Chain, type Transport } from "viem";
15
- import type { WalletServerViemRpcSchema } from "@alchemy/wallet-api-types/rpc";
16
- import {
17
- createSession,
18
- type CreateSessionParams,
19
- } from "./actions/createSession.js";
20
- import {
21
- getCallsStatus,
22
- type GetCallsStatusParams,
23
- } from "./actions/getCallsStatus.js";
24
- import {
25
- prepareCalls,
26
- type PrepareCallsParams,
27
- } from "./actions/prepareCalls.js";
28
- import {
29
- sendPreparedCalls,
30
- type SendPreparedCallsParams,
31
- } from "./actions/sendPreparedCalls.js";
32
- import { prepareSign, type PrepareSignParams } from "./actions/prepareSign.js";
33
- import { formatSign, type FormatSignParams } from "./actions/formatSign.js";
34
-
35
- // let's start with something that takes in as many params as possible, then we can eliminate them as we don't need them
36
- type ClientParams = {
37
- chain: Chain;
38
- transport: Transport;
39
- policyId?: string;
40
- useErc7677middleware?: boolean;
41
- feeEstimator?: ClientMiddlewareFn;
42
- };
43
-
44
- export function isomorphicClientActions(
45
- client: SmartAccountClient<
46
- Transport,
47
- Chain,
48
- SmartContractAccount | undefined,
49
- Record<string, unknown>,
50
- WalletServerViemRpcSchema
51
- >,
52
- ) {
53
- return {
54
- prepareCalls: (params: PrepareCallsParams) => prepareCalls(client, params),
55
- sendPreparedCalls: (params: SendPreparedCallsParams) =>
56
- sendPreparedCalls(client, params),
57
- getCallsStatus: (params: GetCallsStatusParams) =>
58
- getCallsStatus(client, params),
59
- createSession: (params: CreateSessionParams) =>
60
- createSession(client, params),
61
- prepareSign: (params: PrepareSignParams) => prepareSign(client, params),
62
- formatSign: (params: FormatSignParams) => formatSign(client, params),
63
- };
64
- }
65
-
66
- // the isomorphic client likely won't be exposed directly. This client contains the business logic that can be run on the wallet server or on the client
67
- // if the dev chooses to run the client in "local" mode.
68
- export function createIsomorphicClient({
69
- chain,
70
- transport,
71
- policyId,
72
- useErc7677middleware = true,
73
- feeEstimator,
74
- }: ClientParams): SmartAccountClient<
75
- Transport,
76
- Chain,
77
- SmartContractAccount | undefined,
78
- ReturnType<typeof isomorphicClientActions>,
79
- WalletServerViemRpcSchema & SmartAccountClientRpcSchema
80
- > {
81
- // This must be a smart account client since as of now we use it to build UOs under the hood
82
- const client = createSmartAccountClient({
83
- transport,
84
- chain,
85
- // TODO: we will want to enforce alchemy transport here probably
86
- feeEstimator:
87
- feeEstimator ?? alchemyFeeEstimator(transport as AlchemyTransport),
88
- ...(policyId
89
- ? useErc7677middleware
90
- ? alchemyGasManagerMiddleware(policyId)
91
- : // NOTE: DO NOT USE THIS MIDDLEWARE in the server, currently we require using the erc7677 middleware
92
- alchemyGasAndPaymasterAndDataMiddleware({
93
- policyId,
94
- transport: transport as AlchemyTransport,
95
- })
96
- : {}),
97
- }).extend(isomorphicClientActions);
98
-
99
- return client;
100
- }
101
-
102
- export type IsomorphicClient = ReturnType<typeof createIsomorphicClient>;