@metamask/eip-5792-middleware 1.0.0 → 1.2.0

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 (94) hide show
  1. package/CHANGELOG.md +17 -1
  2. package/dist/constants.cjs +21 -1
  3. package/dist/constants.cjs.map +1 -1
  4. package/dist/constants.d.cts +15 -0
  5. package/dist/constants.d.cts.map +1 -1
  6. package/dist/constants.d.mts +15 -0
  7. package/dist/constants.d.mts.map +1 -1
  8. package/dist/constants.mjs +20 -0
  9. package/dist/constants.mjs.map +1 -1
  10. package/dist/{methods → hooks}/getCallsStatus.cjs +5 -6
  11. package/dist/hooks/getCallsStatus.cjs.map +1 -0
  12. package/dist/{methods → hooks}/getCallsStatus.d.cts +1 -2
  13. package/dist/hooks/getCallsStatus.d.cts.map +1 -0
  14. package/dist/{methods → hooks}/getCallsStatus.d.mts +1 -2
  15. package/dist/hooks/getCallsStatus.d.mts.map +1 -0
  16. package/dist/{methods → hooks}/getCallsStatus.mjs +1 -2
  17. package/dist/hooks/getCallsStatus.mjs.map +1 -0
  18. package/dist/{methods → hooks}/getCapabilities.cjs +10 -4
  19. package/dist/hooks/getCapabilities.cjs.map +1 -0
  20. package/dist/{methods → hooks}/getCapabilities.d.cts +3 -2
  21. package/dist/hooks/getCapabilities.d.cts.map +1 -0
  22. package/dist/{methods → hooks}/getCapabilities.d.mts +3 -2
  23. package/dist/hooks/getCapabilities.d.mts.map +1 -0
  24. package/dist/{methods → hooks}/getCapabilities.mjs +10 -4
  25. package/dist/hooks/getCapabilities.mjs.map +1 -0
  26. package/dist/{methods → hooks}/processSendCalls.cjs +104 -13
  27. package/dist/hooks/processSendCalls.cjs.map +1 -0
  28. package/dist/{methods → hooks}/processSendCalls.d.cts +4 -3
  29. package/dist/hooks/processSendCalls.d.cts.map +1 -0
  30. package/dist/{methods → hooks}/processSendCalls.d.mts +4 -3
  31. package/dist/hooks/processSendCalls.d.mts.map +1 -0
  32. package/dist/{methods → hooks}/processSendCalls.mjs +107 -15
  33. package/dist/hooks/processSendCalls.mjs.map +1 -0
  34. package/dist/index.cjs +10 -4
  35. package/dist/index.cjs.map +1 -1
  36. package/dist/index.d.cts +8 -4
  37. package/dist/index.d.cts.map +1 -1
  38. package/dist/index.d.mts +8 -4
  39. package/dist/index.d.mts.map +1 -1
  40. package/dist/index.mjs +6 -3
  41. package/dist/index.mjs.map +1 -1
  42. package/dist/methods/wallet_getCallsStatus.cjs +24 -0
  43. package/dist/methods/wallet_getCallsStatus.cjs.map +1 -0
  44. package/dist/methods/wallet_getCallsStatus.d.cts +14 -0
  45. package/dist/methods/wallet_getCallsStatus.d.cts.map +1 -0
  46. package/dist/methods/wallet_getCallsStatus.d.mts +14 -0
  47. package/dist/methods/wallet_getCallsStatus.d.mts.map +1 -0
  48. package/dist/methods/wallet_getCallsStatus.mjs +20 -0
  49. package/dist/methods/wallet_getCallsStatus.mjs.map +1 -0
  50. package/dist/methods/wallet_getCapabilities.cjs +30 -0
  51. package/dist/methods/wallet_getCapabilities.cjs.map +1 -0
  52. package/dist/methods/wallet_getCapabilities.d.cts +16 -0
  53. package/dist/methods/wallet_getCapabilities.d.cts.map +1 -0
  54. package/dist/methods/wallet_getCapabilities.d.mts +16 -0
  55. package/dist/methods/wallet_getCapabilities.d.mts.map +1 -0
  56. package/dist/methods/wallet_getCapabilities.mjs +26 -0
  57. package/dist/methods/wallet_getCapabilities.mjs.map +1 -0
  58. package/dist/methods/wallet_sendCalls.cjs +34 -0
  59. package/dist/methods/wallet_sendCalls.cjs.map +1 -0
  60. package/dist/methods/wallet_sendCalls.d.cts +16 -0
  61. package/dist/methods/wallet_sendCalls.d.cts.map +1 -0
  62. package/dist/methods/wallet_sendCalls.d.mts +16 -0
  63. package/dist/methods/wallet_sendCalls.d.mts.map +1 -0
  64. package/dist/methods/wallet_sendCalls.mjs +30 -0
  65. package/dist/methods/wallet_sendCalls.mjs.map +1 -0
  66. package/dist/types.cjs +36 -0
  67. package/dist/types.cjs.map +1 -1
  68. package/dist/types.d.cts +82 -0
  69. package/dist/types.d.cts.map +1 -1
  70. package/dist/types.d.mts +82 -0
  71. package/dist/types.d.mts.map +1 -1
  72. package/dist/types.mjs +35 -1
  73. package/dist/types.mjs.map +1 -1
  74. package/dist/utils.cjs +70 -1
  75. package/dist/utils.cjs.map +1 -1
  76. package/dist/utils.d.cts +31 -1
  77. package/dist/utils.d.cts.map +1 -1
  78. package/dist/utils.d.mts +31 -1
  79. package/dist/utils.d.mts.map +1 -1
  80. package/dist/utils.mjs +67 -1
  81. package/dist/utils.mjs.map +1 -1
  82. package/package.json +7 -4
  83. package/dist/methods/getCallsStatus.cjs.map +0 -1
  84. package/dist/methods/getCallsStatus.d.cts.map +0 -1
  85. package/dist/methods/getCallsStatus.d.mts.map +0 -1
  86. package/dist/methods/getCallsStatus.mjs.map +0 -1
  87. package/dist/methods/getCapabilities.cjs.map +0 -1
  88. package/dist/methods/getCapabilities.d.cts.map +0 -1
  89. package/dist/methods/getCapabilities.d.mts.map +0 -1
  90. package/dist/methods/getCapabilities.mjs.map +0 -1
  91. package/dist/methods/processSendCalls.cjs.map +0 -1
  92. package/dist/methods/processSendCalls.d.cts.map +0 -1
  93. package/dist/methods/processSendCalls.d.mts.map +0 -1
  94. package/dist/methods/processSendCalls.mjs.map +0 -1
@@ -4,6 +4,7 @@ exports.processSendCalls = void 0;
4
4
  const rpc_errors_1 = require("@metamask/rpc-errors");
5
5
  const transaction_controller_1 = require("@metamask/transaction-controller");
6
6
  const utils_1 = require("@metamask/utils");
7
+ const lodash_1 = require("lodash");
7
8
  const uuid_1 = require("uuid");
8
9
  const constants_1 = require("../constants.cjs");
9
10
  const utils_2 = require("../utils.cjs");
@@ -17,7 +18,7 @@ const utils_2 = require("../utils.cjs");
17
18
  * @returns Promise resolving to a SendCallsResult containing the batch ID.
18
19
  */
19
20
  async function processSendCalls(hooks, messenger, params, req) {
20
- const { addTransactionBatch, addTransaction, getDismissSmartAccountSuggestionEnabled, isAtomicBatchSupported, validateSecurity: validateSecurityHook, } = hooks;
21
+ const { addTransactionBatch, addTransaction, getDismissSmartAccountSuggestionEnabled, isAtomicBatchSupported, validateSecurity: validateSecurityHook, isAuxiliaryFundsSupported, } = hooks;
21
22
  const { calls, from: paramFrom } = params;
22
23
  const { networkClientId, origin } = req;
23
24
  const transactions = calls.map((call) => ({ params: call }));
@@ -32,12 +33,14 @@ async function processSendCalls(hooks, messenger, params, req) {
32
33
  addTransaction,
33
34
  chainId,
34
35
  from,
36
+ messenger,
35
37
  networkClientId,
36
38
  origin,
37
39
  securityAlertId,
38
40
  sendCalls: params,
39
41
  transactions,
40
42
  validateSecurity,
43
+ isAuxiliaryFundsSupported,
41
44
  });
42
45
  }
43
46
  else {
@@ -54,6 +57,7 @@ async function processSendCalls(hooks, messenger, params, req) {
54
57
  securityAlertId,
55
58
  transactions,
56
59
  validateSecurity,
60
+ isAuxiliaryFundsSupported,
57
61
  });
58
62
  }
59
63
  return { id: batchId };
@@ -66,16 +70,19 @@ exports.processSendCalls = processSendCalls;
66
70
  * @param params.addTransaction - Function to add a single transaction.
67
71
  * @param params.chainId - The chain ID for the transaction.
68
72
  * @param params.from - The sender address.
73
+ * @param params.messenger - Messenger instance for controller communication.
69
74
  * @param params.networkClientId - The network client ID.
70
75
  * @param params.origin - The origin of the request (optional).
71
76
  * @param params.securityAlertId - The security alert ID for this transaction.
72
77
  * @param params.sendCalls - The original sendCalls request.
73
78
  * @param params.transactions - Array containing the single transaction.
74
79
  * @param params.validateSecurity - Function to validate security for the transaction.
80
+ * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
75
81
  * @returns Promise resolving to the generated batch ID for the transaction.
76
82
  */
77
- async function processSingleTransaction({ addTransaction, chainId, from, networkClientId, origin, securityAlertId, sendCalls, transactions, validateSecurity, }) {
78
- validateSingleSendCall(sendCalls, chainId);
83
+ async function processSingleTransaction({ addTransaction, chainId, from, messenger, networkClientId, origin, securityAlertId, sendCalls, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
84
+ const keyringType = (0, utils_2.getAccountKeyringType)(from, messenger);
85
+ validateSingleSendCall(sendCalls, chainId, keyringType, isAuxiliaryFundsSupported);
79
86
  const txParams = {
80
87
  from,
81
88
  ...transactions[0].params,
@@ -87,6 +94,7 @@ async function processSingleTransaction({ addTransaction, chainId, from, network
87
94
  origin,
88
95
  };
89
96
  validateSecurity(securityRequest, chainId);
97
+ dedupeAuxiliaryFundsRequiredAssets(sendCalls);
90
98
  const batchId = generateBatchId();
91
99
  await addTransaction(txParams, {
92
100
  networkClientId,
@@ -112,9 +120,10 @@ async function processSingleTransaction({ addTransaction, chainId, from, network
112
120
  * @param params.securityAlertId - The security alert ID for this batch.
113
121
  * @param params.transactions - Array of transactions to process.
114
122
  * @param params.validateSecurity - Function to validate security for the transactions.
123
+ * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
115
124
  * @returns Promise resolving to the generated batch ID for the transaction batch.
116
125
  */
117
- async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, sendCalls, securityAlertId, transactions, validateSecurity, }) {
126
+ async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, sendCalls, securityAlertId, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
118
127
  const batchSupport = await isAtomicBatchSupported({
119
128
  address: from,
120
129
  chainIds: [chainId],
@@ -122,7 +131,8 @@ async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSu
122
131
  const chainBatchSupport = batchSupport?.[0];
123
132
  const keyringType = (0, utils_2.getAccountKeyringType)(from, messenger);
124
133
  const dismissSmartAccountSuggestionEnabled = getDismissSmartAccountSuggestionEnabled();
125
- validateSendCalls(sendCalls, chainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType);
134
+ validateSendCalls(sendCalls, chainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType, isAuxiliaryFundsSupported);
135
+ dedupeAuxiliaryFundsRequiredAssets(sendCalls);
126
136
  const result = await addTransactionBatch({
127
137
  from,
128
138
  networkClientId,
@@ -148,10 +158,12 @@ function generateBatchId() {
148
158
  *
149
159
  * @param sendCalls - The sendCalls request to validate.
150
160
  * @param dappChainId - The chain ID that the dApp is connected to.
161
+ * @param keyringType - The type of keyring associated with the account.
162
+ * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
151
163
  */
152
- function validateSingleSendCall(sendCalls, dappChainId) {
164
+ function validateSingleSendCall(sendCalls, dappChainId, keyringType, isAuxiliaryFundsSupported) {
153
165
  validateSendCallsVersion(sendCalls);
154
- validateCapabilities(sendCalls);
166
+ validateCapabilities(sendCalls, keyringType, isAuxiliaryFundsSupported);
155
167
  validateDappChainId(sendCalls, dappChainId);
156
168
  }
157
169
  /**
@@ -162,11 +174,12 @@ function validateSingleSendCall(sendCalls, dappChainId) {
162
174
  * @param dismissSmartAccountSuggestionEnabled - Whether smart account suggestions are disabled.
163
175
  * @param chainBatchSupport - Information about atomic batch support for the chain.
164
176
  * @param keyringType - The type of keyring associated with the account.
177
+ * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
165
178
  */
166
- function validateSendCalls(sendCalls, dappChainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType) {
179
+ function validateSendCalls(sendCalls, dappChainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType, isAuxiliaryFundsSupported) {
167
180
  validateSendCallsVersion(sendCalls);
168
181
  validateSendCallsChainId(sendCalls, dappChainId, chainBatchSupport);
169
- validateCapabilities(sendCalls);
182
+ validateCapabilities(sendCalls, keyringType, isAuxiliaryFundsSupported);
170
183
  validateUpgrade(dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType);
171
184
  }
172
185
  /**
@@ -213,12 +226,19 @@ function validateSendCallsChainId(sendCalls, dappChainId, chainBatchSupport) {
213
226
  * Validates that all required capabilities in the sendCalls request are supported.
214
227
  *
215
228
  * @param sendCalls - The sendCalls request to validate.
229
+ * @param keyringType - The type of keyring associated with the account.
230
+ * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
231
+ *
216
232
  * @throws JsonRpcError if unsupported non-optional capabilities are requested.
217
233
  */
218
- function validateCapabilities(sendCalls) {
219
- const { calls, capabilities } = sendCalls;
220
- const requiredTopLevelCapabilities = Object.keys(capabilities ?? {}).filter((name) => capabilities?.[name].optional !== true);
221
- const requiredCallCapabilities = calls.flatMap((call) => Object.keys(call.capabilities ?? {}).filter((name) => call.capabilities?.[name].optional !== true));
234
+ function validateCapabilities(sendCalls, keyringType, isAuxiliaryFundsSupported) {
235
+ const { calls, capabilities, chainId } = sendCalls;
236
+ const requiredTopLevelCapabilities = Object.keys(capabilities ?? {}).filter((name) =>
237
+ // Non optional capabilities other than `auxiliaryFunds` are not supported by the wallet
238
+ name !== constants_1.SupportedCapabilities.AuxiliaryFunds.toString() &&
239
+ capabilities?.[name].optional !== true);
240
+ const requiredCallCapabilities = calls.flatMap((call) => Object.keys(call.capabilities ?? {}).filter((name) => name !== constants_1.SupportedCapabilities.AuxiliaryFunds.toString() &&
241
+ call.capabilities?.[name].optional !== true));
222
242
  const requiredCapabilities = [
223
243
  ...requiredTopLevelCapabilities,
224
244
  ...requiredCallCapabilities,
@@ -226,6 +246,50 @@ function validateCapabilities(sendCalls) {
226
246
  if (requiredCapabilities?.length) {
227
247
  throw new rpc_errors_1.JsonRpcError(constants_1.EIP5792ErrorCode.UnsupportedNonOptionalCapability, `Unsupported non-optional capabilities: ${requiredCapabilities.join(', ')}`);
228
248
  }
249
+ if (capabilities?.auxiliaryFunds) {
250
+ validateAuxFundsSupportAndRequiredAssets({
251
+ auxiliaryFunds: capabilities.auxiliaryFunds,
252
+ chainId,
253
+ keyringType,
254
+ isAuxiliaryFundsSupported,
255
+ });
256
+ }
257
+ }
258
+ /**
259
+ * Validates EIP-7682 optional `requiredAssets` to see if the account and chain are supported, and that param is well-formed.
260
+ *
261
+ * docs: {@link https://eips.ethereum.org/EIPS/eip-7682#extended-usage-requiredassets-parameter}
262
+ *
263
+ * @param param - The parameter object.
264
+ * @param param.auxiliaryFunds - The auxiliaryFunds param to validate.
265
+ * @param param.auxiliaryFunds.optional - Metadata to signal for wallets that support this optional capability, while maintaining compatibility with wallets that do not.
266
+ * @param param.auxiliaryFunds.requiredAssets - Metadata that enables a wallets support for `auxiliaryFunds` capability.
267
+ * @param param.chainId - The chain ID of the incoming request.
268
+ * @param param.keyringType - The type of keyring associated with the account.
269
+ * @param param.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
270
+ * @throws JsonRpcError if auxiliary funds capability is not supported.
271
+ */
272
+ function validateAuxFundsSupportAndRequiredAssets({ auxiliaryFunds, chainId, keyringType, isAuxiliaryFundsSupported, }) {
273
+ // If we can make use of that capability then we should, but otherwise we can process the request and ignore the capability
274
+ // so if the capability is signaled as optional, no validation is required, so we don't block the transaction from happening.
275
+ if (auxiliaryFunds.optional) {
276
+ return;
277
+ }
278
+ const isSupportedAccount = constants_1.KEYRING_TYPES_SUPPORTING_7702.includes(keyringType);
279
+ if (!isSupportedAccount) {
280
+ throw new rpc_errors_1.JsonRpcError(constants_1.EIP5792ErrorCode.UnsupportedNonOptionalCapability, `Unsupported non-optional capability: ${constants_1.SupportedCapabilities.AuxiliaryFunds}`);
281
+ }
282
+ if (!isAuxiliaryFundsSupported(chainId)) {
283
+ throw new rpc_errors_1.JsonRpcError(constants_1.EIP7682ErrorCode.UnsupportedChain, `The wallet no longer supports auxiliary funds on the requested chain: ${chainId}`);
284
+ }
285
+ if (!auxiliaryFunds?.requiredAssets) {
286
+ return;
287
+ }
288
+ for (const asset of auxiliaryFunds.requiredAssets) {
289
+ if (asset.standard !== 'erc20') {
290
+ throw new rpc_errors_1.JsonRpcError(constants_1.EIP7682ErrorCode.UnsupportedAsset, `The requested asset ${asset.address} is not available through the wallet’s auxiliary fund system: unsupported token standard ${asset.standard}`);
291
+ }
292
+ }
229
293
  }
230
294
  /**
231
295
  * Validates whether an EIP-7702 upgrade is allowed for the given parameters.
@@ -246,4 +310,31 @@ function validateUpgrade(dismissSmartAccountSuggestionEnabled, chainBatchSupport
246
310
  throw new rpc_errors_1.JsonRpcError(constants_1.EIP5792ErrorCode.RejectedUpgrade, 'EIP-7702 upgrade not supported on account');
247
311
  }
248
312
  }
313
+ /**
314
+ * Function to possibly deduplicate `auxiliaryFunds` capability `requiredAssets`.
315
+ * Does nothing if no `requiredAssets` exists in `auxiliaryFunds` capability.
316
+ *
317
+ * @param sendCalls - The original sendCalls request.
318
+ */
319
+ function dedupeAuxiliaryFundsRequiredAssets(sendCalls) {
320
+ if (sendCalls.capabilities?.auxiliaryFunds?.requiredAssets) {
321
+ const { requiredAssets } = sendCalls.capabilities.auxiliaryFunds;
322
+ // Group assets by their address (lowercased) and standard
323
+ const grouped = (0, lodash_1.groupBy)(requiredAssets, (asset) => `${asset.address.toLowerCase()}-${asset.standard}`);
324
+ // For each group, sum the amounts and return a single asset
325
+ const deduplicatedAssets = Object.values(grouped).map((group) => {
326
+ if (group.length === 1) {
327
+ return group[0];
328
+ }
329
+ const totalAmount = group.reduce((sum, asset) => {
330
+ return sum + BigInt(asset.amount);
331
+ }, 0n);
332
+ return {
333
+ ...group[0],
334
+ amount: (0, utils_1.add0x)(totalAmount.toString(16)),
335
+ };
336
+ });
337
+ sendCalls.capabilities.auxiliaryFunds.requiredAssets = deduplicatedAssets;
338
+ }
339
+ }
249
340
  //# sourceMappingURL=processSendCalls.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"processSendCalls.cjs","sourceRoot":"","sources":["../../src/hooks/processSendCalls.ts"],"names":[],"mappings":";;;AACA,qDAA+D;AAQ/D,6EAA2E;AAE3E,2CAAoD;AACpD,mCAAiC;AACjC,+BAAyC;AAEzC,gDAOsB;AAOtB,wCAAiD;AAkCjD;;;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SAAS,CAAC,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE;QAC1D,MAAM,EAAE,cAAc,EAAE,GAAG,SAAS,CAAC,YAAY,CAAC,cAAc,CAAC;QACjE,0DAA0D;QAC1D,MAAM,OAAO,GAAG,IAAA,gBAAO,EACrB,cAAc,EACd,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,IAAI,KAAK,CAAC,QAAQ,EAAE,CAC9D,CAAC;QAEF,4DAA4D;QAC5D,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YAC9D,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;gBACtB,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;aACjB;YAED,MAAM,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE;gBAC9C,OAAO,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;YACpC,CAAC,EAAE,EAAE,CAAC,CAAC;YAEP,OAAO;gBACL,GAAG,KAAK,CAAC,CAAC,CAAC;gBACX,MAAM,EAAE,IAAA,aAAK,EAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;aACxC,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,SAAS,CAAC,YAAY,CAAC,cAAc,CAAC,cAAc,GAAG,kBAAkB,CAAC;KAC3E;AACH,CAAC","sourcesContent":["import type { KeyringTypes } from '@metamask/keyring-controller';\nimport { JsonRpcError, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n BatchTransactionParams,\n IsAtomicBatchSupportedResultEntry,\n SecurityAlertResponse,\n TransactionController,\n ValidateSecurityRequest,\n} from '@metamask/transaction-controller';\nimport { TransactionEnvelopeType } from '@metamask/transaction-controller';\nimport type { Hex, JsonRpcRequest } from '@metamask/utils';\nimport { add0x, bytesToHex } from '@metamask/utils';\nimport { groupBy } from 'lodash';\nimport { parse, v4 as uuid } from 'uuid';\n\nimport {\n EIP5792ErrorCode,\n EIP7682ErrorCode,\n KEYRING_TYPES_SUPPORTING_7702,\n MessageType,\n SupportedCapabilities,\n VERSION,\n} from '../constants';\nimport type {\n EIP5792Messenger,\n SendCallsPayload,\n SendCallsRequiredAssetsParam,\n SendCallsResult,\n} from '../types';\nimport { getAccountKeyringType } from '../utils';\n\n/**\n * Type definition for required controller hooks and utilities of {@link processSendCalls}\n */\nexport type ProcessSendCallsHooks = {\n /** Function to add a batch of transactions atomically */\n addTransactionBatch: TransactionController['addTransactionBatch'];\n /** Function to add a single transaction */\n addTransaction: TransactionController['addTransaction'];\n /** Function to check if smart account suggestions are disabled */\n getDismissSmartAccountSuggestionEnabled: () => boolean;\n /** Function to check if atomic batching is supported for given parameters */\n isAtomicBatchSupported: TransactionController['isAtomicBatchSupported'];\n /** Function to validate security for transaction requests */\n validateSecurity: (\n securityAlertId: string,\n request: ValidateSecurityRequest,\n chainId: Hex,\n ) => Promise<void>;\n /** Function to validate if auxiliary funds capability is supported. */\n isAuxiliaryFundsSupported: (chainId: Hex) => boolean;\n};\n\n/**\n * A valid JSON-RPC request object for `wallet_sendCalls`.\n */\nexport type ProcessSendCallsRequest = JsonRpcRequest & {\n /** The identifier for the network client that has been created for this RPC endpoint */\n networkClientId: string;\n /** The origin of the RPC request */\n origin?: string;\n};\n\n/**\n * Processes a sendCalls request for EIP-5792 transactions.\n *\n * @param hooks - Object containing required controller hooks and utilities.\n * @param messenger - Messenger instance for controller communication.\n * @param params - The sendCalls parameters containing transaction calls and metadata.\n * @param req - The original JSON-RPC request.\n * @returns Promise resolving to a SendCallsResult containing the batch ID.\n */\nexport async function processSendCalls(\n hooks: ProcessSendCallsHooks,\n messenger: EIP5792Messenger,\n params: SendCallsPayload,\n req: ProcessSendCallsRequest,\n): Promise<SendCallsResult> {\n const {\n addTransactionBatch,\n addTransaction,\n getDismissSmartAccountSuggestionEnabled,\n isAtomicBatchSupported,\n validateSecurity: validateSecurityHook,\n isAuxiliaryFundsSupported,\n } = hooks;\n\n const { calls, from: paramFrom } = params;\n const { networkClientId, origin } = req;\n const transactions = calls.map((call) => ({ params: call }));\n\n const { chainId } = messenger.call(\n 'NetworkController:getNetworkClientById',\n networkClientId,\n ).configuration;\n\n const from =\n paramFrom ??\n (messenger.call('AccountsController:getSelectedAccount').address as Hex);\n\n const securityAlertId = uuid();\n const validateSecurity = validateSecurityHook.bind(null, securityAlertId);\n\n let batchId: Hex;\n if (Object.keys(transactions).length === 1) {\n batchId = await processSingleTransaction({\n addTransaction,\n chainId,\n from,\n messenger,\n networkClientId,\n origin,\n securityAlertId,\n sendCalls: params,\n transactions,\n validateSecurity,\n isAuxiliaryFundsSupported,\n });\n } else {\n batchId = await processMultipleTransaction({\n addTransactionBatch,\n isAtomicBatchSupported,\n chainId,\n from,\n getDismissSmartAccountSuggestionEnabled,\n messenger,\n networkClientId,\n origin,\n sendCalls: params,\n securityAlertId,\n transactions,\n validateSecurity,\n isAuxiliaryFundsSupported,\n });\n }\n\n return { id: batchId };\n}\n\n/**\n * Processes a single transaction from a sendCalls request.\n *\n * @param params - Object containing all parameters needed for single transaction processing.\n * @param params.addTransaction - Function to add a single transaction.\n * @param params.chainId - The chain ID for the transaction.\n * @param params.from - The sender address.\n * @param params.messenger - Messenger instance for controller communication.\n * @param params.networkClientId - The network client ID.\n * @param params.origin - The origin of the request (optional).\n * @param params.securityAlertId - The security alert ID for this transaction.\n * @param params.sendCalls - The original sendCalls request.\n * @param params.transactions - Array containing the single transaction.\n * @param params.validateSecurity - Function to validate security for the transaction.\n * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.\n * @returns Promise resolving to the generated batch ID for the transaction.\n */\nasync function processSingleTransaction({\n addTransaction,\n chainId,\n from,\n messenger,\n networkClientId,\n origin,\n securityAlertId,\n sendCalls,\n transactions,\n validateSecurity,\n isAuxiliaryFundsSupported,\n}: {\n addTransaction: TransactionController['addTransaction'];\n chainId: Hex;\n from: Hex;\n messenger: EIP5792Messenger;\n networkClientId: string;\n origin?: string;\n securityAlertId: string;\n sendCalls: SendCallsPayload;\n transactions: { params: BatchTransactionParams }[];\n validateSecurity: (\n securityRequest: ValidateSecurityRequest,\n chainId: Hex,\n ) => void;\n isAuxiliaryFundsSupported: (chainId: Hex) => boolean;\n}) {\n const keyringType = getAccountKeyringType(from, messenger);\n\n validateSingleSendCall(\n sendCalls,\n chainId,\n keyringType,\n isAuxiliaryFundsSupported,\n );\n\n const txParams = {\n from,\n ...transactions[0].params,\n type: TransactionEnvelopeType.feeMarket,\n };\n\n const securityRequest: ValidateSecurityRequest = {\n method: MessageType.SendTransaction,\n params: [txParams],\n origin,\n };\n validateSecurity(securityRequest, chainId);\n\n dedupeAuxiliaryFundsRequiredAssets(sendCalls);\n\n const batchId = generateBatchId();\n\n await addTransaction(txParams, {\n networkClientId,\n origin,\n securityAlertResponse: { securityAlertId } as SecurityAlertResponse,\n batchId,\n });\n return batchId;\n}\n\n/**\n * Processes multiple transactions from a sendCalls request as an atomic batch.\n *\n * @param params - Object containing all parameters needed for multiple transaction processing.\n * @param params.addTransactionBatch - Function to add a batch of transactions atomically.\n * @param params.isAtomicBatchSupported - Function to check if atomic batching is supported.\n * @param params.chainId - The chain ID for the transactions.\n * @param params.from - The sender address.\n * @param params.getDismissSmartAccountSuggestionEnabled - Function to check if smart account suggestions are disabled.\n * @param params.networkClientId - The network client ID.\n * @param params.messenger - Messenger instance for controller communication.\n * @param params.origin - The origin of the request (optional).\n * @param params.sendCalls - The original sendCalls request.\n * @param params.securityAlertId - The security alert ID for this batch.\n * @param params.transactions - Array of transactions to process.\n * @param params.validateSecurity - Function to validate security for the transactions.\n * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.\n * @returns Promise resolving to the generated batch ID for the transaction batch.\n */\nasync function processMultipleTransaction({\n addTransactionBatch,\n isAtomicBatchSupported,\n chainId,\n from,\n getDismissSmartAccountSuggestionEnabled,\n networkClientId,\n messenger,\n origin,\n sendCalls,\n securityAlertId,\n transactions,\n validateSecurity,\n isAuxiliaryFundsSupported,\n}: {\n addTransactionBatch: TransactionController['addTransactionBatch'];\n isAtomicBatchSupported: TransactionController['isAtomicBatchSupported'];\n chainId: Hex;\n from: Hex;\n getDismissSmartAccountSuggestionEnabled: () => boolean;\n messenger: EIP5792Messenger;\n networkClientId: string;\n origin?: string;\n sendCalls: SendCallsPayload;\n securityAlertId: string;\n transactions: { params: BatchTransactionParams }[];\n validateSecurity: (\n securityRequest: ValidateSecurityRequest,\n chainId: Hex,\n ) => Promise<void>;\n isAuxiliaryFundsSupported: (chainId: Hex) => boolean;\n}) {\n const batchSupport = await isAtomicBatchSupported({\n address: from,\n chainIds: [chainId],\n });\n\n const chainBatchSupport = batchSupport?.[0];\n\n const keyringType = getAccountKeyringType(from, messenger);\n\n const dismissSmartAccountSuggestionEnabled =\n getDismissSmartAccountSuggestionEnabled();\n\n validateSendCalls(\n sendCalls,\n chainId,\n dismissSmartAccountSuggestionEnabled,\n chainBatchSupport,\n keyringType,\n isAuxiliaryFundsSupported,\n );\n\n dedupeAuxiliaryFundsRequiredAssets(sendCalls);\n\n const result = await addTransactionBatch({\n from,\n networkClientId,\n origin,\n securityAlertId,\n transactions,\n validateSecurity,\n });\n return result.batchId;\n}\n\n/**\n * Generate a transaction batch ID.\n *\n * @returns A unique batch ID as a hexadecimal string.\n */\nfunction generateBatchId(): Hex {\n const idString = uuid();\n const idBytes = new Uint8Array(parse(idString));\n return bytesToHex(idBytes);\n}\n\n/**\n * Validates a single sendCalls request.\n *\n * @param sendCalls - The sendCalls request to validate.\n * @param dappChainId - The chain ID that the dApp is connected to.\n * @param keyringType - The type of keyring associated with the account.\n * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.\n */\nfunction validateSingleSendCall(\n sendCalls: SendCallsPayload,\n dappChainId: Hex,\n keyringType: KeyringTypes,\n isAuxiliaryFundsSupported: (chainId: Hex) => boolean,\n) {\n validateSendCallsVersion(sendCalls);\n validateCapabilities(sendCalls, keyringType, isAuxiliaryFundsSupported);\n validateDappChainId(sendCalls, dappChainId);\n}\n\n/**\n * Validates a sendCalls request for multiple transactions.\n *\n * @param sendCalls - The sendCalls request to validate.\n * @param dappChainId - The chain ID that the dApp is connected to\n * @param dismissSmartAccountSuggestionEnabled - Whether smart account suggestions are disabled.\n * @param chainBatchSupport - Information about atomic batch support for the chain.\n * @param keyringType - The type of keyring associated with the account.\n * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.\n */\nfunction validateSendCalls(\n sendCalls: SendCallsPayload,\n dappChainId: Hex,\n dismissSmartAccountSuggestionEnabled: boolean,\n chainBatchSupport: IsAtomicBatchSupportedResultEntry | undefined,\n keyringType: KeyringTypes,\n isAuxiliaryFundsSupported: (chainId: Hex) => boolean,\n) {\n validateSendCallsVersion(sendCalls);\n validateSendCallsChainId(sendCalls, dappChainId, chainBatchSupport);\n validateCapabilities(sendCalls, keyringType, isAuxiliaryFundsSupported);\n validateUpgrade(\n dismissSmartAccountSuggestionEnabled,\n chainBatchSupport,\n keyringType,\n );\n}\n\n/**\n * Validates the version of a sendCalls request.\n *\n * @param sendCalls - The sendCalls request to validate.\n * @throws JsonRpcError if the version is not supported.\n */\nfunction validateSendCallsVersion(sendCalls: SendCallsPayload) {\n const { version } = sendCalls;\n\n if (version !== VERSION) {\n throw rpcErrors.invalidInput(\n `Version not supported: Got ${version}, expected ${VERSION}`,\n );\n }\n}\n\n/**\n * Validates that the chain ID in the sendCalls request matches the dApp's selected network.\n *\n * @param sendCalls - The sendCalls request to validate.\n * @param dappChainId - The chain ID that the dApp is connected to\n * @throws JsonRpcError if the chain IDs don't match\n */\nfunction validateDappChainId(sendCalls: SendCallsPayload, dappChainId: Hex) {\n const { chainId: requestChainId } = sendCalls;\n\n if (\n requestChainId &&\n requestChainId.toLowerCase() !== dappChainId.toLowerCase()\n ) {\n throw rpcErrors.invalidParams(\n `Chain ID must match the dApp selected network: Got ${requestChainId}, expected ${dappChainId}`,\n );\n }\n}\n\n/**\n * Validates the chain ID for sendCalls requests with additional EIP-7702 support checks.\n *\n * @param sendCalls - The sendCalls request to validate.\n * @param dappChainId - The chain ID that the dApp is connected to\n * @param chainBatchSupport - Information about atomic batch support for the chain\n * @throws JsonRpcError if the chain ID doesn't match or EIP-7702 is not supported\n */\nfunction validateSendCallsChainId(\n sendCalls: SendCallsPayload,\n dappChainId: Hex,\n chainBatchSupport: IsAtomicBatchSupportedResultEntry | undefined,\n) {\n validateDappChainId(sendCalls, dappChainId);\n if (!chainBatchSupport) {\n throw new JsonRpcError(\n EIP5792ErrorCode.UnsupportedChainId,\n `EIP-7702 not supported on chain: ${dappChainId}`,\n );\n }\n}\n\n/**\n * Validates that all required capabilities in the sendCalls request are supported.\n *\n * @param sendCalls - The sendCalls request to validate.\n * @param keyringType - The type of keyring associated with the account.\n * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.\n *\n * @throws JsonRpcError if unsupported non-optional capabilities are requested.\n */\nfunction validateCapabilities(\n sendCalls: SendCallsPayload,\n keyringType: KeyringTypes,\n isAuxiliaryFundsSupported: (chainId: Hex) => boolean,\n) {\n const { calls, capabilities, chainId } = sendCalls;\n\n const requiredTopLevelCapabilities = Object.keys(capabilities ?? {}).filter(\n (name) =>\n // Non optional capabilities other than `auxiliaryFunds` are not supported by the wallet\n name !== SupportedCapabilities.AuxiliaryFunds.toString() &&\n capabilities?.[name].optional !== true,\n );\n\n const requiredCallCapabilities = calls.flatMap((call) =>\n Object.keys(call.capabilities ?? {}).filter(\n (name) =>\n name !== SupportedCapabilities.AuxiliaryFunds.toString() &&\n call.capabilities?.[name].optional !== true,\n ),\n );\n\n const requiredCapabilities = [\n ...requiredTopLevelCapabilities,\n ...requiredCallCapabilities,\n ];\n\n if (requiredCapabilities?.length) {\n throw new JsonRpcError(\n EIP5792ErrorCode.UnsupportedNonOptionalCapability,\n `Unsupported non-optional capabilities: ${requiredCapabilities.join(\n ', ',\n )}`,\n );\n }\n\n if (capabilities?.auxiliaryFunds) {\n validateAuxFundsSupportAndRequiredAssets({\n auxiliaryFunds: capabilities.auxiliaryFunds,\n chainId,\n keyringType,\n isAuxiliaryFundsSupported,\n });\n }\n}\n\n/**\n * Validates EIP-7682 optional `requiredAssets` to see if the account and chain are supported, and that param is well-formed.\n *\n * docs: {@link https://eips.ethereum.org/EIPS/eip-7682#extended-usage-requiredassets-parameter}\n *\n * @param param - The parameter object.\n * @param param.auxiliaryFunds - The auxiliaryFunds param to validate.\n * @param param.auxiliaryFunds.optional - Metadata to signal for wallets that support this optional capability, while maintaining compatibility with wallets that do not.\n * @param param.auxiliaryFunds.requiredAssets - Metadata that enables a wallets support for `auxiliaryFunds` capability.\n * @param param.chainId - The chain ID of the incoming request.\n * @param param.keyringType - The type of keyring associated with the account.\n * @param param.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.\n * @throws JsonRpcError if auxiliary funds capability is not supported.\n */\nfunction validateAuxFundsSupportAndRequiredAssets({\n auxiliaryFunds,\n chainId,\n keyringType,\n isAuxiliaryFundsSupported,\n}: {\n auxiliaryFunds: {\n optional?: boolean;\n requiredAssets?: SendCallsRequiredAssetsParam[];\n };\n chainId: Hex;\n keyringType: KeyringTypes;\n isAuxiliaryFundsSupported: (chainId: Hex) => boolean;\n}) {\n // If we can make use of that capability then we should, but otherwise we can process the request and ignore the capability\n // so if the capability is signaled as optional, no validation is required, so we don't block the transaction from happening.\n if (auxiliaryFunds.optional) {\n return;\n }\n const isSupportedAccount =\n KEYRING_TYPES_SUPPORTING_7702.includes(keyringType);\n\n if (!isSupportedAccount) {\n throw new JsonRpcError(\n EIP5792ErrorCode.UnsupportedNonOptionalCapability,\n `Unsupported non-optional capability: ${SupportedCapabilities.AuxiliaryFunds}`,\n );\n }\n\n if (!isAuxiliaryFundsSupported(chainId)) {\n throw new JsonRpcError(\n EIP7682ErrorCode.UnsupportedChain,\n `The wallet no longer supports auxiliary funds on the requested chain: ${chainId}`,\n );\n }\n\n if (!auxiliaryFunds?.requiredAssets) {\n return;\n }\n\n for (const asset of auxiliaryFunds.requiredAssets) {\n if (asset.standard !== 'erc20') {\n throw new JsonRpcError(\n EIP7682ErrorCode.UnsupportedAsset,\n `The requested asset ${asset.address} is not available through the wallet’s auxiliary fund system: unsupported token standard ${asset.standard}`,\n );\n }\n }\n}\n\n/**\n * Validates whether an EIP-7702 upgrade is allowed for the given parameters.\n *\n * @param dismissSmartAccountSuggestionEnabled - Whether smart account suggestions are disabled.\n * @param chainBatchSupport - Information about atomic batch support for the chain.\n * @param keyringType - The type of keyring associated with the account.\n * @throws JsonRpcError if the upgrade is rejected due to user settings or account type.\n */\nfunction validateUpgrade(\n dismissSmartAccountSuggestionEnabled: boolean,\n chainBatchSupport: IsAtomicBatchSupportedResultEntry | undefined,\n keyringType: KeyringTypes,\n) {\n if (chainBatchSupport?.delegationAddress) {\n return;\n }\n\n if (dismissSmartAccountSuggestionEnabled) {\n throw new JsonRpcError(\n EIP5792ErrorCode.RejectedUpgrade,\n 'EIP-7702 upgrade disabled by the user',\n );\n }\n\n if (!KEYRING_TYPES_SUPPORTING_7702.includes(keyringType)) {\n throw new JsonRpcError(\n EIP5792ErrorCode.RejectedUpgrade,\n 'EIP-7702 upgrade not supported on account',\n );\n }\n}\n\n/**\n * Function to possibly deduplicate `auxiliaryFunds` capability `requiredAssets`.\n * Does nothing if no `requiredAssets` exists in `auxiliaryFunds` capability.\n *\n * @param sendCalls - The original sendCalls request.\n */\nfunction dedupeAuxiliaryFundsRequiredAssets(sendCalls: SendCallsPayload): void {\n if (sendCalls.capabilities?.auxiliaryFunds?.requiredAssets) {\n const { requiredAssets } = sendCalls.capabilities.auxiliaryFunds;\n // Group assets by their address (lowercased) and standard\n const grouped = groupBy(\n requiredAssets,\n (asset) => `${asset.address.toLowerCase()}-${asset.standard}`,\n );\n\n // For each group, sum the amounts and return a single asset\n const deduplicatedAssets = Object.values(grouped).map((group) => {\n if (group.length === 1) {\n return group[0];\n }\n\n const totalAmount = group.reduce((sum, asset) => {\n return sum + BigInt(asset.amount);\n }, 0n);\n\n return {\n ...group[0],\n amount: add0x(totalAmount.toString(16)),\n };\n });\n\n sendCalls.capabilities.auxiliaryFunds.requiredAssets = deduplicatedAssets;\n }\n}\n"]}
@@ -1,7 +1,6 @@
1
- import type { SendCalls, SendCallsResult } from "@metamask/eth-json-rpc-middleware";
2
1
  import type { TransactionController, ValidateSecurityRequest } from "@metamask/transaction-controller";
3
2
  import type { Hex, JsonRpcRequest } from "@metamask/utils";
4
- import type { EIP5792Messenger } from "../types.cjs";
3
+ import type { EIP5792Messenger, SendCallsPayload, SendCallsResult } from "../types.cjs";
5
4
  /**
6
5
  * Type definition for required controller hooks and utilities of {@link processSendCalls}
7
6
  */
@@ -16,6 +15,8 @@ export type ProcessSendCallsHooks = {
16
15
  isAtomicBatchSupported: TransactionController['isAtomicBatchSupported'];
17
16
  /** Function to validate security for transaction requests */
18
17
  validateSecurity: (securityAlertId: string, request: ValidateSecurityRequest, chainId: Hex) => Promise<void>;
18
+ /** Function to validate if auxiliary funds capability is supported. */
19
+ isAuxiliaryFundsSupported: (chainId: Hex) => boolean;
19
20
  };
20
21
  /**
21
22
  * A valid JSON-RPC request object for `wallet_sendCalls`.
@@ -35,5 +36,5 @@ export type ProcessSendCallsRequest = JsonRpcRequest & {
35
36
  * @param req - The original JSON-RPC request.
36
37
  * @returns Promise resolving to a SendCallsResult containing the batch ID.
37
38
  */
38
- export declare function processSendCalls(hooks: ProcessSendCallsHooks, messenger: EIP5792Messenger, params: SendCalls, req: ProcessSendCallsRequest): Promise<SendCallsResult>;
39
+ export declare function processSendCalls(hooks: ProcessSendCallsHooks, messenger: EIP5792Messenger, params: SendCallsPayload, req: ProcessSendCallsRequest): Promise<SendCallsResult>;
39
40
  //# sourceMappingURL=processSendCalls.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"processSendCalls.d.cts","sourceRoot":"","sources":["../../src/hooks/processSendCalls.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAIV,qBAAqB,EACrB,uBAAuB,EACxB,yCAAyC;AAE1C,OAAO,KAAK,EAAE,GAAG,EAAE,cAAc,EAAE,wBAAwB;AAa3D,OAAO,KAAK,EACV,gBAAgB,EAChB,gBAAgB,EAEhB,eAAe,EAChB,qBAAiB;AAGlB;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,yDAAyD;IACzD,mBAAmB,EAAE,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;IAClE,2CAA2C;IAC3C,cAAc,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;IACxD,kEAAkE;IAClE,uCAAuC,EAAE,MAAM,OAAO,CAAC;IACvD,6EAA6E;IAC7E,sBAAsB,EAAE,qBAAqB,CAAC,wBAAwB,CAAC,CAAC;IACxE,6DAA6D;IAC7D,gBAAgB,EAAE,CAChB,eAAe,EAAE,MAAM,EACvB,OAAO,EAAE,uBAAuB,EAChC,OAAO,EAAE,GAAG,KACT,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,uEAAuE;IACvE,yBAAyB,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC;CACtD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG,cAAc,GAAG;IACrD,wFAAwF;IACxF,eAAe,EAAE,MAAM,CAAC;IACxB,oCAAoC;IACpC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,qBAAqB,EAC5B,SAAS,EAAE,gBAAgB,EAC3B,MAAM,EAAE,gBAAgB,EACxB,GAAG,EAAE,uBAAuB,GAC3B,OAAO,CAAC,eAAe,CAAC,CA4D1B"}
@@ -1,7 +1,6 @@
1
- import type { SendCalls, SendCallsResult } from "@metamask/eth-json-rpc-middleware";
2
1
  import type { TransactionController, ValidateSecurityRequest } from "@metamask/transaction-controller";
3
2
  import type { Hex, JsonRpcRequest } from "@metamask/utils";
4
- import type { EIP5792Messenger } from "../types.mjs";
3
+ import type { EIP5792Messenger, SendCallsPayload, SendCallsResult } from "../types.mjs";
5
4
  /**
6
5
  * Type definition for required controller hooks and utilities of {@link processSendCalls}
7
6
  */
@@ -16,6 +15,8 @@ export type ProcessSendCallsHooks = {
16
15
  isAtomicBatchSupported: TransactionController['isAtomicBatchSupported'];
17
16
  /** Function to validate security for transaction requests */
18
17
  validateSecurity: (securityAlertId: string, request: ValidateSecurityRequest, chainId: Hex) => Promise<void>;
18
+ /** Function to validate if auxiliary funds capability is supported. */
19
+ isAuxiliaryFundsSupported: (chainId: Hex) => boolean;
19
20
  };
20
21
  /**
21
22
  * A valid JSON-RPC request object for `wallet_sendCalls`.
@@ -35,5 +36,5 @@ export type ProcessSendCallsRequest = JsonRpcRequest & {
35
36
  * @param req - The original JSON-RPC request.
36
37
  * @returns Promise resolving to a SendCallsResult containing the batch ID.
37
38
  */
38
- export declare function processSendCalls(hooks: ProcessSendCallsHooks, messenger: EIP5792Messenger, params: SendCalls, req: ProcessSendCallsRequest): Promise<SendCallsResult>;
39
+ export declare function processSendCalls(hooks: ProcessSendCallsHooks, messenger: EIP5792Messenger, params: SendCallsPayload, req: ProcessSendCallsRequest): Promise<SendCallsResult>;
39
40
  //# sourceMappingURL=processSendCalls.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"processSendCalls.d.mts","sourceRoot":"","sources":["../../src/hooks/processSendCalls.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAIV,qBAAqB,EACrB,uBAAuB,EACxB,yCAAyC;AAE1C,OAAO,KAAK,EAAE,GAAG,EAAE,cAAc,EAAE,wBAAwB;AAa3D,OAAO,KAAK,EACV,gBAAgB,EAChB,gBAAgB,EAEhB,eAAe,EAChB,qBAAiB;AAGlB;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAClC,yDAAyD;IACzD,mBAAmB,EAAE,qBAAqB,CAAC,qBAAqB,CAAC,CAAC;IAClE,2CAA2C;IAC3C,cAAc,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;IACxD,kEAAkE;IAClE,uCAAuC,EAAE,MAAM,OAAO,CAAC;IACvD,6EAA6E;IAC7E,sBAAsB,EAAE,qBAAqB,CAAC,wBAAwB,CAAC,CAAC;IACxE,6DAA6D;IAC7D,gBAAgB,EAAE,CAChB,eAAe,EAAE,MAAM,EACvB,OAAO,EAAE,uBAAuB,EAChC,OAAO,EAAE,GAAG,KACT,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,uEAAuE;IACvE,yBAAyB,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,OAAO,CAAC;CACtD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG,cAAc,GAAG;IACrD,wFAAwF;IACxF,eAAe,EAAE,MAAM,CAAC;IACxB,oCAAoC;IACpC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,qBAAqB,EAC5B,SAAS,EAAE,gBAAgB,EAC3B,MAAM,EAAE,gBAAgB,EACxB,GAAG,EAAE,uBAAuB,GAC3B,OAAO,CAAC,eAAe,CAAC,CA4D1B"}
@@ -1,8 +1,10 @@
1
1
  import { JsonRpcError, rpcErrors } from "@metamask/rpc-errors";
2
2
  import { TransactionEnvelopeType } from "@metamask/transaction-controller";
3
- import { bytesToHex } from "@metamask/utils";
3
+ import { add0x, bytesToHex } from "@metamask/utils";
4
+ import $lodash from "lodash";
5
+ const { groupBy } = $lodash;
4
6
  import { parse, v4 as uuid } from "uuid";
5
- import { EIP5792ErrorCode, KEYRING_TYPES_SUPPORTING_7702, MessageType, VERSION } from "../constants.mjs";
7
+ import { EIP5792ErrorCode, EIP7682ErrorCode, KEYRING_TYPES_SUPPORTING_7702, MessageType, SupportedCapabilities, VERSION } from "../constants.mjs";
6
8
  import { getAccountKeyringType } from "../utils.mjs";
7
9
  /**
8
10
  * Processes a sendCalls request for EIP-5792 transactions.
@@ -14,7 +16,7 @@ import { getAccountKeyringType } from "../utils.mjs";
14
16
  * @returns Promise resolving to a SendCallsResult containing the batch ID.
15
17
  */
16
18
  export async function processSendCalls(hooks, messenger, params, req) {
17
- const { addTransactionBatch, addTransaction, getDismissSmartAccountSuggestionEnabled, isAtomicBatchSupported, validateSecurity: validateSecurityHook, } = hooks;
19
+ const { addTransactionBatch, addTransaction, getDismissSmartAccountSuggestionEnabled, isAtomicBatchSupported, validateSecurity: validateSecurityHook, isAuxiliaryFundsSupported, } = hooks;
18
20
  const { calls, from: paramFrom } = params;
19
21
  const { networkClientId, origin } = req;
20
22
  const transactions = calls.map((call) => ({ params: call }));
@@ -29,12 +31,14 @@ export async function processSendCalls(hooks, messenger, params, req) {
29
31
  addTransaction,
30
32
  chainId,
31
33
  from,
34
+ messenger,
32
35
  networkClientId,
33
36
  origin,
34
37
  securityAlertId,
35
38
  sendCalls: params,
36
39
  transactions,
37
40
  validateSecurity,
41
+ isAuxiliaryFundsSupported,
38
42
  });
39
43
  }
40
44
  else {
@@ -51,6 +55,7 @@ export async function processSendCalls(hooks, messenger, params, req) {
51
55
  securityAlertId,
52
56
  transactions,
53
57
  validateSecurity,
58
+ isAuxiliaryFundsSupported,
54
59
  });
55
60
  }
56
61
  return { id: batchId };
@@ -62,16 +67,19 @@ export async function processSendCalls(hooks, messenger, params, req) {
62
67
  * @param params.addTransaction - Function to add a single transaction.
63
68
  * @param params.chainId - The chain ID for the transaction.
64
69
  * @param params.from - The sender address.
70
+ * @param params.messenger - Messenger instance for controller communication.
65
71
  * @param params.networkClientId - The network client ID.
66
72
  * @param params.origin - The origin of the request (optional).
67
73
  * @param params.securityAlertId - The security alert ID for this transaction.
68
74
  * @param params.sendCalls - The original sendCalls request.
69
75
  * @param params.transactions - Array containing the single transaction.
70
76
  * @param params.validateSecurity - Function to validate security for the transaction.
77
+ * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
71
78
  * @returns Promise resolving to the generated batch ID for the transaction.
72
79
  */
73
- async function processSingleTransaction({ addTransaction, chainId, from, networkClientId, origin, securityAlertId, sendCalls, transactions, validateSecurity, }) {
74
- validateSingleSendCall(sendCalls, chainId);
80
+ async function processSingleTransaction({ addTransaction, chainId, from, messenger, networkClientId, origin, securityAlertId, sendCalls, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
81
+ const keyringType = getAccountKeyringType(from, messenger);
82
+ validateSingleSendCall(sendCalls, chainId, keyringType, isAuxiliaryFundsSupported);
75
83
  const txParams = {
76
84
  from,
77
85
  ...transactions[0].params,
@@ -83,6 +91,7 @@ async function processSingleTransaction({ addTransaction, chainId, from, network
83
91
  origin,
84
92
  };
85
93
  validateSecurity(securityRequest, chainId);
94
+ dedupeAuxiliaryFundsRequiredAssets(sendCalls);
86
95
  const batchId = generateBatchId();
87
96
  await addTransaction(txParams, {
88
97
  networkClientId,
@@ -108,9 +117,10 @@ async function processSingleTransaction({ addTransaction, chainId, from, network
108
117
  * @param params.securityAlertId - The security alert ID for this batch.
109
118
  * @param params.transactions - Array of transactions to process.
110
119
  * @param params.validateSecurity - Function to validate security for the transactions.
120
+ * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
111
121
  * @returns Promise resolving to the generated batch ID for the transaction batch.
112
122
  */
113
- async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, sendCalls, securityAlertId, transactions, validateSecurity, }) {
123
+ async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, sendCalls, securityAlertId, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
114
124
  const batchSupport = await isAtomicBatchSupported({
115
125
  address: from,
116
126
  chainIds: [chainId],
@@ -118,7 +128,8 @@ async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSu
118
128
  const chainBatchSupport = batchSupport?.[0];
119
129
  const keyringType = getAccountKeyringType(from, messenger);
120
130
  const dismissSmartAccountSuggestionEnabled = getDismissSmartAccountSuggestionEnabled();
121
- validateSendCalls(sendCalls, chainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType);
131
+ validateSendCalls(sendCalls, chainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType, isAuxiliaryFundsSupported);
132
+ dedupeAuxiliaryFundsRequiredAssets(sendCalls);
122
133
  const result = await addTransactionBatch({
123
134
  from,
124
135
  networkClientId,
@@ -144,10 +155,12 @@ function generateBatchId() {
144
155
  *
145
156
  * @param sendCalls - The sendCalls request to validate.
146
157
  * @param dappChainId - The chain ID that the dApp is connected to.
158
+ * @param keyringType - The type of keyring associated with the account.
159
+ * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
147
160
  */
148
- function validateSingleSendCall(sendCalls, dappChainId) {
161
+ function validateSingleSendCall(sendCalls, dappChainId, keyringType, isAuxiliaryFundsSupported) {
149
162
  validateSendCallsVersion(sendCalls);
150
- validateCapabilities(sendCalls);
163
+ validateCapabilities(sendCalls, keyringType, isAuxiliaryFundsSupported);
151
164
  validateDappChainId(sendCalls, dappChainId);
152
165
  }
153
166
  /**
@@ -158,11 +171,12 @@ function validateSingleSendCall(sendCalls, dappChainId) {
158
171
  * @param dismissSmartAccountSuggestionEnabled - Whether smart account suggestions are disabled.
159
172
  * @param chainBatchSupport - Information about atomic batch support for the chain.
160
173
  * @param keyringType - The type of keyring associated with the account.
174
+ * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
161
175
  */
162
- function validateSendCalls(sendCalls, dappChainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType) {
176
+ function validateSendCalls(sendCalls, dappChainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType, isAuxiliaryFundsSupported) {
163
177
  validateSendCallsVersion(sendCalls);
164
178
  validateSendCallsChainId(sendCalls, dappChainId, chainBatchSupport);
165
- validateCapabilities(sendCalls);
179
+ validateCapabilities(sendCalls, keyringType, isAuxiliaryFundsSupported);
166
180
  validateUpgrade(dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType);
167
181
  }
168
182
  /**
@@ -209,12 +223,19 @@ function validateSendCallsChainId(sendCalls, dappChainId, chainBatchSupport) {
209
223
  * Validates that all required capabilities in the sendCalls request are supported.
210
224
  *
211
225
  * @param sendCalls - The sendCalls request to validate.
226
+ * @param keyringType - The type of keyring associated with the account.
227
+ * @param isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
228
+ *
212
229
  * @throws JsonRpcError if unsupported non-optional capabilities are requested.
213
230
  */
214
- function validateCapabilities(sendCalls) {
215
- const { calls, capabilities } = sendCalls;
216
- const requiredTopLevelCapabilities = Object.keys(capabilities ?? {}).filter((name) => capabilities?.[name].optional !== true);
217
- const requiredCallCapabilities = calls.flatMap((call) => Object.keys(call.capabilities ?? {}).filter((name) => call.capabilities?.[name].optional !== true));
231
+ function validateCapabilities(sendCalls, keyringType, isAuxiliaryFundsSupported) {
232
+ const { calls, capabilities, chainId } = sendCalls;
233
+ const requiredTopLevelCapabilities = Object.keys(capabilities ?? {}).filter((name) =>
234
+ // Non optional capabilities other than `auxiliaryFunds` are not supported by the wallet
235
+ name !== SupportedCapabilities.AuxiliaryFunds.toString() &&
236
+ capabilities?.[name].optional !== true);
237
+ const requiredCallCapabilities = calls.flatMap((call) => Object.keys(call.capabilities ?? {}).filter((name) => name !== SupportedCapabilities.AuxiliaryFunds.toString() &&
238
+ call.capabilities?.[name].optional !== true));
218
239
  const requiredCapabilities = [
219
240
  ...requiredTopLevelCapabilities,
220
241
  ...requiredCallCapabilities,
@@ -222,6 +243,50 @@ function validateCapabilities(sendCalls) {
222
243
  if (requiredCapabilities?.length) {
223
244
  throw new JsonRpcError(EIP5792ErrorCode.UnsupportedNonOptionalCapability, `Unsupported non-optional capabilities: ${requiredCapabilities.join(', ')}`);
224
245
  }
246
+ if (capabilities?.auxiliaryFunds) {
247
+ validateAuxFundsSupportAndRequiredAssets({
248
+ auxiliaryFunds: capabilities.auxiliaryFunds,
249
+ chainId,
250
+ keyringType,
251
+ isAuxiliaryFundsSupported,
252
+ });
253
+ }
254
+ }
255
+ /**
256
+ * Validates EIP-7682 optional `requiredAssets` to see if the account and chain are supported, and that param is well-formed.
257
+ *
258
+ * docs: {@link https://eips.ethereum.org/EIPS/eip-7682#extended-usage-requiredassets-parameter}
259
+ *
260
+ * @param param - The parameter object.
261
+ * @param param.auxiliaryFunds - The auxiliaryFunds param to validate.
262
+ * @param param.auxiliaryFunds.optional - Metadata to signal for wallets that support this optional capability, while maintaining compatibility with wallets that do not.
263
+ * @param param.auxiliaryFunds.requiredAssets - Metadata that enables a wallets support for `auxiliaryFunds` capability.
264
+ * @param param.chainId - The chain ID of the incoming request.
265
+ * @param param.keyringType - The type of keyring associated with the account.
266
+ * @param param.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
267
+ * @throws JsonRpcError if auxiliary funds capability is not supported.
268
+ */
269
+ function validateAuxFundsSupportAndRequiredAssets({ auxiliaryFunds, chainId, keyringType, isAuxiliaryFundsSupported, }) {
270
+ // If we can make use of that capability then we should, but otherwise we can process the request and ignore the capability
271
+ // so if the capability is signaled as optional, no validation is required, so we don't block the transaction from happening.
272
+ if (auxiliaryFunds.optional) {
273
+ return;
274
+ }
275
+ const isSupportedAccount = KEYRING_TYPES_SUPPORTING_7702.includes(keyringType);
276
+ if (!isSupportedAccount) {
277
+ throw new JsonRpcError(EIP5792ErrorCode.UnsupportedNonOptionalCapability, `Unsupported non-optional capability: ${SupportedCapabilities.AuxiliaryFunds}`);
278
+ }
279
+ if (!isAuxiliaryFundsSupported(chainId)) {
280
+ throw new JsonRpcError(EIP7682ErrorCode.UnsupportedChain, `The wallet no longer supports auxiliary funds on the requested chain: ${chainId}`);
281
+ }
282
+ if (!auxiliaryFunds?.requiredAssets) {
283
+ return;
284
+ }
285
+ for (const asset of auxiliaryFunds.requiredAssets) {
286
+ if (asset.standard !== 'erc20') {
287
+ throw new JsonRpcError(EIP7682ErrorCode.UnsupportedAsset, `The requested asset ${asset.address} is not available through the wallet’s auxiliary fund system: unsupported token standard ${asset.standard}`);
288
+ }
289
+ }
225
290
  }
226
291
  /**
227
292
  * Validates whether an EIP-7702 upgrade is allowed for the given parameters.
@@ -242,4 +307,31 @@ function validateUpgrade(dismissSmartAccountSuggestionEnabled, chainBatchSupport
242
307
  throw new JsonRpcError(EIP5792ErrorCode.RejectedUpgrade, 'EIP-7702 upgrade not supported on account');
243
308
  }
244
309
  }
310
+ /**
311
+ * Function to possibly deduplicate `auxiliaryFunds` capability `requiredAssets`.
312
+ * Does nothing if no `requiredAssets` exists in `auxiliaryFunds` capability.
313
+ *
314
+ * @param sendCalls - The original sendCalls request.
315
+ */
316
+ function dedupeAuxiliaryFundsRequiredAssets(sendCalls) {
317
+ if (sendCalls.capabilities?.auxiliaryFunds?.requiredAssets) {
318
+ const { requiredAssets } = sendCalls.capabilities.auxiliaryFunds;
319
+ // Group assets by their address (lowercased) and standard
320
+ const grouped = groupBy(requiredAssets, (asset) => `${asset.address.toLowerCase()}-${asset.standard}`);
321
+ // For each group, sum the amounts and return a single asset
322
+ const deduplicatedAssets = Object.values(grouped).map((group) => {
323
+ if (group.length === 1) {
324
+ return group[0];
325
+ }
326
+ const totalAmount = group.reduce((sum, asset) => {
327
+ return sum + BigInt(asset.amount);
328
+ }, 0n);
329
+ return {
330
+ ...group[0],
331
+ amount: add0x(totalAmount.toString(16)),
332
+ };
333
+ });
334
+ sendCalls.capabilities.auxiliaryFunds.requiredAssets = deduplicatedAssets;
335
+ }
336
+ }
245
337
  //# sourceMappingURL=processSendCalls.mjs.map