@metamask-previews/eip-5792-middleware 2.0.0-preview-565dfca2 → 2.1.0-preview-3fa1672

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.
package/CHANGELOG.md CHANGED
@@ -7,9 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [2.1.0]
11
+
12
+ ### Added
13
+
14
+ - Id of JSON RPC request is passed to functions to create batched transaction, id is thus added to transaction meta ([#7415](https://github.com/MetaMask/core/pull/7415))
15
+
10
16
  ### Changed
11
17
 
12
- - Bump `@metamask/transaction-controller` from `^61.3.0` to `^62.6.0` ([#7007](https://github.com/MetaMask/core/pull/7007), [#7126](https://github.com/MetaMask/core/pull/7126), [#7153](https://github.com/MetaMask/core/pull/7153), [#7202](https://github.com/MetaMask/core/pull/7202), [#7215](https://github.com/MetaMask/core/pull/7202), [#7220](https://github.com/MetaMask/core/pull/7220), [#7236](https://github.com/MetaMask/core/pull/7236), [#7257](https://github.com/MetaMask/core/pull/7257), [#7289](https://github.com/MetaMask/core/pull/7289), [#7325](https://github.com/MetaMask/core/pull/7325), [#7430](https://github.com/MetaMask/core/pull/7430))
18
+ - Bump `@metamask/transaction-controller` from `^61.3.0` to `^62.7.0` ([#7007](https://github.com/MetaMask/core/pull/7007), [#7126](https://github.com/MetaMask/core/pull/7126), [#7153](https://github.com/MetaMask/core/pull/7153), [#7202](https://github.com/MetaMask/core/pull/7202), [#7215](https://github.com/MetaMask/core/pull/7202), [#7220](https://github.com/MetaMask/core/pull/7220), [#7236](https://github.com/MetaMask/core/pull/7236), [#7257](https://github.com/MetaMask/core/pull/7257), [#7289](https://github.com/MetaMask/core/pull/7289), [#7325](https://github.com/MetaMask/core/pull/7325), [#7430](https://github.com/MetaMask/core/pull/7430), [#7494](https://github.com/MetaMask/core/pull/7494))
13
19
 
14
20
  ## [2.0.0]
15
21
 
@@ -66,7 +72,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66
72
 
67
73
  - Initial release ([#6458](https://github.com/MetaMask/core/pull/6458))
68
74
 
69
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/eip-5792-middleware@2.0.0...HEAD
75
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/eip-5792-middleware@2.1.0...HEAD
76
+ [2.1.0]: https://github.com/MetaMask/core/compare/@metamask/eip-5792-middleware@2.0.0...@metamask/eip-5792-middleware@2.1.0
70
77
  [2.0.0]: https://github.com/MetaMask/core/compare/@metamask/eip-5792-middleware@1.2.4...@metamask/eip-5792-middleware@2.0.0
71
78
  [1.2.4]: https://github.com/MetaMask/core/compare/@metamask/eip-5792-middleware@1.2.3...@metamask/eip-5792-middleware@1.2.4
72
79
  [1.2.3]: https://github.com/MetaMask/core/compare/@metamask/eip-5792-middleware@1.2.2...@metamask/eip-5792-middleware@1.2.3
@@ -27,6 +27,7 @@ async function processSendCalls(hooks, messenger, params, req) {
27
27
  messenger.call('AccountsController:getSelectedAccount').address;
28
28
  const securityAlertId = (0, uuid_1.v4)();
29
29
  const validateSecurity = validateSecurityHook.bind(null, securityAlertId);
30
+ const requestId = req.id ? String(req.id) : '';
30
31
  let batchId;
31
32
  if (Object.keys(transactions).length === 1) {
32
33
  batchId = await processSingleTransaction({
@@ -36,6 +37,7 @@ async function processSendCalls(hooks, messenger, params, req) {
36
37
  messenger,
37
38
  networkClientId,
38
39
  origin,
40
+ requestId,
39
41
  securityAlertId,
40
42
  sendCalls: params,
41
43
  transactions,
@@ -53,6 +55,7 @@ async function processSendCalls(hooks, messenger, params, req) {
53
55
  messenger,
54
56
  networkClientId,
55
57
  origin,
58
+ requestId,
56
59
  sendCalls: params,
57
60
  securityAlertId,
58
61
  transactions,
@@ -73,6 +76,7 @@ exports.processSendCalls = processSendCalls;
73
76
  * @param params.messenger - Messenger instance for controller communication.
74
77
  * @param params.networkClientId - The network client ID.
75
78
  * @param params.origin - The origin of the request (optional).
79
+ * @param params.requestId - Unique requestId of the JSON-RPC request from DAPP.
76
80
  * @param params.securityAlertId - The security alert ID for this transaction.
77
81
  * @param params.sendCalls - The original sendCalls request.
78
82
  * @param params.transactions - Array containing the single transaction.
@@ -80,7 +84,7 @@ exports.processSendCalls = processSendCalls;
80
84
  * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
81
85
  * @returns Promise resolving to the generated batch ID for the transaction.
82
86
  */
83
- async function processSingleTransaction({ addTransaction, chainId, from, messenger, networkClientId, origin, securityAlertId, sendCalls, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
87
+ async function processSingleTransaction({ addTransaction, chainId, from, messenger, networkClientId, origin, requestId, securityAlertId, sendCalls, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
84
88
  const keyringType = (0, utils_2.getAccountKeyringType)(from, messenger);
85
89
  validateSingleSendCall(sendCalls, chainId, keyringType, isAuxiliaryFundsSupported);
86
90
  const txParams = {
@@ -97,6 +101,7 @@ async function processSingleTransaction({ addTransaction, chainId, from, messeng
97
101
  dedupeAuxiliaryFundsRequiredAssets(sendCalls);
98
102
  const batchId = generateBatchId();
99
103
  await addTransaction(txParams, {
104
+ requestId,
100
105
  networkClientId,
101
106
  origin,
102
107
  securityAlertResponse: { securityAlertId },
@@ -116,6 +121,7 @@ async function processSingleTransaction({ addTransaction, chainId, from, messeng
116
121
  * @param params.networkClientId - The network client ID.
117
122
  * @param params.messenger - Messenger instance for controller communication.
118
123
  * @param params.origin - The origin of the request (optional).
124
+ * @param params.requestId - Unique requestId of the JSON-RPC request from DAPP.
119
125
  * @param params.sendCalls - The original sendCalls request.
120
126
  * @param params.securityAlertId - The security alert ID for this batch.
121
127
  * @param params.transactions - Array of transactions to process.
@@ -123,7 +129,7 @@ async function processSingleTransaction({ addTransaction, chainId, from, messeng
123
129
  * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
124
130
  * @returns Promise resolving to the generated batch ID for the transaction batch.
125
131
  */
126
- async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, sendCalls, securityAlertId, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
132
+ async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, requestId, sendCalls, securityAlertId, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
127
133
  const batchSupport = await isAtomicBatchSupported({
128
134
  address: from,
129
135
  chainIds: [chainId],
@@ -137,6 +143,7 @@ async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSu
137
143
  from,
138
144
  networkClientId,
139
145
  origin,
146
+ requestId,
140
147
  securityAlertId,
141
148
  transactions,
142
149
  validateSecurity,
@@ -1 +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;;;;;;;;GAQG;AACI,KAAK,UAAU,gBAAgB,CACpC,KAA4B,EAC5B,SAA2B,EAC3B,MAAwB,EACxB,GAA4B;IAE5B,MAAM,EACJ,mBAAmB,EACnB,cAAc,EACd,uCAAuC,EACvC,sBAAsB,EACtB,gBAAgB,EAAE,oBAAoB,EACtC,yBAAyB,GAC1B,GAAG,KAAK,CAAC;IAEV,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;IAC1C,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACxC,MAAM,YAAY,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IAE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E,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,CAAC;gBACvB,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;YAClB,CAAC;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;IAC5E,CAAC;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
+ {"version":3,"file":"processSendCalls.cjs","sourceRoot":"","s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AAM,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;IAC5E,CAAC;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 const requestId = req.id ? String(req.id) : '';\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 requestId,\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 requestId,\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.requestId - Unique requestId of the JSON-RPC request from DAPP.\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 requestId,\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 requestId?: 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 requestId,\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.requestId - Unique requestId of the JSON-RPC request from DAPP.\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 requestId,\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 requestId?: 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 requestId,\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 +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,EACh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ClE,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,CAgE1B"}
@@ -25,6 +25,7 @@ export async function processSendCalls(hooks, messenger, params, req) {
25
25
  messenger.call('AccountsController:getSelectedAccount').address;
26
26
  const securityAlertId = uuid();
27
27
  const validateSecurity = validateSecurityHook.bind(null, securityAlertId);
28
+ const requestId = req.id ? String(req.id) : '';
28
29
  let batchId;
29
30
  if (Object.keys(transactions).length === 1) {
30
31
  batchId = await processSingleTransaction({
@@ -34,6 +35,7 @@ export async function processSendCalls(hooks, messenger, params, req) {
34
35
  messenger,
35
36
  networkClientId,
36
37
  origin,
38
+ requestId,
37
39
  securityAlertId,
38
40
  sendCalls: params,
39
41
  transactions,
@@ -51,6 +53,7 @@ export async function processSendCalls(hooks, messenger, params, req) {
51
53
  messenger,
52
54
  networkClientId,
53
55
  origin,
56
+ requestId,
54
57
  sendCalls: params,
55
58
  securityAlertId,
56
59
  transactions,
@@ -70,6 +73,7 @@ export async function processSendCalls(hooks, messenger, params, req) {
70
73
  * @param params.messenger - Messenger instance for controller communication.
71
74
  * @param params.networkClientId - The network client ID.
72
75
  * @param params.origin - The origin of the request (optional).
76
+ * @param params.requestId - Unique requestId of the JSON-RPC request from DAPP.
73
77
  * @param params.securityAlertId - The security alert ID for this transaction.
74
78
  * @param params.sendCalls - The original sendCalls request.
75
79
  * @param params.transactions - Array containing the single transaction.
@@ -77,7 +81,7 @@ export async function processSendCalls(hooks, messenger, params, req) {
77
81
  * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
78
82
  * @returns Promise resolving to the generated batch ID for the transaction.
79
83
  */
80
- async function processSingleTransaction({ addTransaction, chainId, from, messenger, networkClientId, origin, securityAlertId, sendCalls, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
84
+ async function processSingleTransaction({ addTransaction, chainId, from, messenger, networkClientId, origin, requestId, securityAlertId, sendCalls, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
81
85
  const keyringType = getAccountKeyringType(from, messenger);
82
86
  validateSingleSendCall(sendCalls, chainId, keyringType, isAuxiliaryFundsSupported);
83
87
  const txParams = {
@@ -94,6 +98,7 @@ async function processSingleTransaction({ addTransaction, chainId, from, messeng
94
98
  dedupeAuxiliaryFundsRequiredAssets(sendCalls);
95
99
  const batchId = generateBatchId();
96
100
  await addTransaction(txParams, {
101
+ requestId,
97
102
  networkClientId,
98
103
  origin,
99
104
  securityAlertResponse: { securityAlertId },
@@ -113,6 +118,7 @@ async function processSingleTransaction({ addTransaction, chainId, from, messeng
113
118
  * @param params.networkClientId - The network client ID.
114
119
  * @param params.messenger - Messenger instance for controller communication.
115
120
  * @param params.origin - The origin of the request (optional).
121
+ * @param params.requestId - Unique requestId of the JSON-RPC request from DAPP.
116
122
  * @param params.sendCalls - The original sendCalls request.
117
123
  * @param params.securityAlertId - The security alert ID for this batch.
118
124
  * @param params.transactions - Array of transactions to process.
@@ -120,7 +126,7 @@ async function processSingleTransaction({ addTransaction, chainId, from, messeng
120
126
  * @param params.isAuxiliaryFundsSupported - Function to validate if auxiliary funds capability is supported.
121
127
  * @returns Promise resolving to the generated batch ID for the transaction batch.
122
128
  */
123
- async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, sendCalls, securityAlertId, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
129
+ async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, requestId, sendCalls, securityAlertId, transactions, validateSecurity, isAuxiliaryFundsSupported, }) {
124
130
  const batchSupport = await isAtomicBatchSupported({
125
131
  address: from,
126
132
  chainIds: [chainId],
@@ -134,6 +140,7 @@ async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSu
134
140
  from,
135
141
  networkClientId,
136
142
  origin,
143
+ requestId,
137
144
  securityAlertId,
138
145
  transactions,
139
146
  validateSecurity,
@@ -1 +1 @@
1
- {"version":3,"file":"processSendCalls.mjs","sourceRoot":"","sources":["../../src/hooks/processSendCalls.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,6BAA6B;AAQ/D,OAAO,EAAE,uBAAuB,EAAE,yCAAyC;AAE3E,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,wBAAwB;;;AAEpD,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,IAAI,EAAE,aAAa;AAEzC,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,6BAA6B,EAC7B,WAAW,EACX,qBAAqB,EACrB,OAAO,EACR,yBAAqB;AAOtB,OAAO,EAAE,qBAAqB,EAAE,qBAAiB;AAkCjD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,K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CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,kCAAkC,CAAC,SAA2B;IACrE,IAAI,SAAS,CAAC,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,CAAC;QAC3D,MAAM,EAAE,cAAc,EAAE,GAAG,SAAS,CAAC,YAAY,CAAC,cAAc,CAAC;QACjE,0DAA0D;QAC1D,MAAM,OAAO,GAAG,OAAO,CACrB,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,CAAC;gBACvB,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;YAClB,CAAC;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,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;aACxC,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,SAAS,CAAC,YAAY,CAAC,cAAc,CAAC,cAAc,GAAG,kBAAkB,CAAC;IAC5E,CAAC;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
+ {"version":3,"file":"processSendCalls.mjs","sourceRoot":"","sources":["../../src/hooks/processSendCalls.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,6BAA6B;AAQ/D,OAAO,EAAE,uBAAuB,EAAE,yCAAyC;AAE3E,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,wBAAwB;;;AAEpD,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,IAAI,EAAE,aAAa;AAEzC,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,6BAA6B,EAC7B,WAAW,EACX,qBAAqB,EACrB,OAAO,EACR,yBAAqB;AAOtB,OAAO,EAAE,qBAAqB,EAAE,qBAAiB;AAkCjD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CACpC,KAA4B,EAC5B,SAA2B,EAC3B,MAAwB,EACxB,GAA4B;IAE5B,MAAM,EACJ,mBAAmB,EACnB,cAAc,EACd,uCAAuC,EACvC,sBAAsB,EACtB,gBAAgB,EAAE,oBAAoB,EACtC,yBAAyB,GAC1B,GAAG,KAAK,CAAC;IAEV,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,MAAM,CAAC;IAC1C,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,GAAG,GAAG,CAAC;IACxC,MAAM,YAAY,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAA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CAAC,cAAc,CAAC;QACjE,0DAA0D;QAC1D,MAAM,OAAO,GAAG,OAAO,CACrB,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,CAAC;gBACvB,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;YAClB,CAAC;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,KAAK,CAAC,WAAW,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;aACxC,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,SAAS,CAAC,YAAY,CAAC,cAAc,CAAC,cAAc,GAAG,kBAAkB,CAAC;IAC5E,CAAC;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 const requestId = req.id ? String(req.id) : '';\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 requestId,\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 requestId,\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.requestId - Unique requestId of the JSON-RPC request from DAPP.\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 requestId,\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 requestId?: 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 requestId,\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.requestId - Unique requestId of the JSON-RPC request from DAPP.\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 requestId,\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 requestId?: 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 requestId,\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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/eip-5792-middleware",
3
- "version": "2.0.0-preview-565dfca2",
3
+ "version": "2.1.0-preview-3fa1672",
4
4
  "description": "Implements the JSON-RPC methods for sending multiple calls from the user's wallet, and checking their status, as referenced in EIP-5792",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -50,7 +50,7 @@
50
50
  "dependencies": {
51
51
  "@metamask/messenger": "^0.3.0",
52
52
  "@metamask/superstruct": "^3.1.0",
53
- "@metamask/transaction-controller": "^62.6.0",
53
+ "@metamask/transaction-controller": "^62.7.0",
54
54
  "@metamask/utils": "^11.8.1",
55
55
  "lodash": "^4.17.21",
56
56
  "uuid": "^8.3.2"