@metamask/eip-5792-middleware 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/constants.cjs +22 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +13 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +13 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +19 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/index.cjs +10 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -0
- package/dist/methods/getCallsStatus.cjs +73 -0
- package/dist/methods/getCallsStatus.cjs.map +1 -0
- package/dist/methods/getCallsStatus.d.cts +13 -0
- package/dist/methods/getCallsStatus.d.cts.map +1 -0
- package/dist/methods/getCallsStatus.d.mts +13 -0
- package/dist/methods/getCallsStatus.d.mts.map +1 -0
- package/dist/methods/getCallsStatus.mjs +69 -0
- package/dist/methods/getCallsStatus.mjs.map +1 -0
- package/dist/methods/getCapabilities.cjs +96 -0
- package/dist/methods/getCapabilities.cjs.map +1 -0
- package/dist/methods/getCapabilities.d.cts +30 -0
- package/dist/methods/getCapabilities.d.cts.map +1 -0
- package/dist/methods/getCapabilities.d.mts +30 -0
- package/dist/methods/getCapabilities.d.mts.map +1 -0
- package/dist/methods/getCapabilities.mjs +92 -0
- package/dist/methods/getCapabilities.mjs.map +1 -0
- package/dist/methods/processSendCalls.cjs +249 -0
- package/dist/methods/processSendCalls.cjs.map +1 -0
- package/dist/methods/processSendCalls.d.cts +39 -0
- package/dist/methods/processSendCalls.d.cts.map +1 -0
- package/dist/methods/processSendCalls.d.mts +39 -0
- package/dist/methods/processSendCalls.d.mts.map +1 -0
- package/dist/methods/processSendCalls.mjs +245 -0
- package/dist/methods/processSendCalls.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +9 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +9 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/utils.cjs +24 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +13 -0
- package/dist/utils.d.cts.map +1 -0
- package/dist/utils.d.mts +13 -0
- package/dist/utils.d.mts.map +1 -0
- package/dist/utils.mjs +20 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +74 -0
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { JsonRpcError, rpcErrors } from "@metamask/rpc-errors";
|
|
2
|
+
import { TransactionEnvelopeType } from "@metamask/transaction-controller";
|
|
3
|
+
import { bytesToHex } from "@metamask/utils";
|
|
4
|
+
import { parse, v4 as uuid } from "uuid";
|
|
5
|
+
import { EIP5792ErrorCode, KEYRING_TYPES_SUPPORTING_7702, MessageType, VERSION } from "../constants.mjs";
|
|
6
|
+
import { getAccountKeyringType } from "../utils.mjs";
|
|
7
|
+
/**
|
|
8
|
+
* Processes a sendCalls request for EIP-5792 transactions.
|
|
9
|
+
*
|
|
10
|
+
* @param hooks - Object containing required controller hooks and utilities.
|
|
11
|
+
* @param messenger - Messenger instance for controller communication.
|
|
12
|
+
* @param params - The sendCalls parameters containing transaction calls and metadata.
|
|
13
|
+
* @param req - The original JSON-RPC request.
|
|
14
|
+
* @returns Promise resolving to a SendCallsResult containing the batch ID.
|
|
15
|
+
*/
|
|
16
|
+
export async function processSendCalls(hooks, messenger, params, req) {
|
|
17
|
+
const { addTransactionBatch, addTransaction, getDismissSmartAccountSuggestionEnabled, isAtomicBatchSupported, validateSecurity: validateSecurityHook, } = hooks;
|
|
18
|
+
const { calls, from: paramFrom } = params;
|
|
19
|
+
const { networkClientId, origin } = req;
|
|
20
|
+
const transactions = calls.map((call) => ({ params: call }));
|
|
21
|
+
const { chainId } = messenger.call('NetworkController:getNetworkClientById', networkClientId).configuration;
|
|
22
|
+
const from = paramFrom ??
|
|
23
|
+
messenger.call('AccountsController:getSelectedAccount').address;
|
|
24
|
+
const securityAlertId = uuid();
|
|
25
|
+
const validateSecurity = validateSecurityHook.bind(null, securityAlertId);
|
|
26
|
+
let batchId;
|
|
27
|
+
if (Object.keys(transactions).length === 1) {
|
|
28
|
+
batchId = await processSingleTransaction({
|
|
29
|
+
addTransaction,
|
|
30
|
+
chainId,
|
|
31
|
+
from,
|
|
32
|
+
networkClientId,
|
|
33
|
+
origin,
|
|
34
|
+
securityAlertId,
|
|
35
|
+
sendCalls: params,
|
|
36
|
+
transactions,
|
|
37
|
+
validateSecurity,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
batchId = await processMultipleTransaction({
|
|
42
|
+
addTransactionBatch,
|
|
43
|
+
isAtomicBatchSupported,
|
|
44
|
+
chainId,
|
|
45
|
+
from,
|
|
46
|
+
getDismissSmartAccountSuggestionEnabled,
|
|
47
|
+
messenger,
|
|
48
|
+
networkClientId,
|
|
49
|
+
origin,
|
|
50
|
+
sendCalls: params,
|
|
51
|
+
securityAlertId,
|
|
52
|
+
transactions,
|
|
53
|
+
validateSecurity,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return { id: batchId };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Processes a single transaction from a sendCalls request.
|
|
60
|
+
*
|
|
61
|
+
* @param params - Object containing all parameters needed for single transaction processing.
|
|
62
|
+
* @param params.addTransaction - Function to add a single transaction.
|
|
63
|
+
* @param params.chainId - The chain ID for the transaction.
|
|
64
|
+
* @param params.from - The sender address.
|
|
65
|
+
* @param params.networkClientId - The network client ID.
|
|
66
|
+
* @param params.origin - The origin of the request (optional).
|
|
67
|
+
* @param params.securityAlertId - The security alert ID for this transaction.
|
|
68
|
+
* @param params.sendCalls - The original sendCalls request.
|
|
69
|
+
* @param params.transactions - Array containing the single transaction.
|
|
70
|
+
* @param params.validateSecurity - Function to validate security for the transaction.
|
|
71
|
+
* @returns Promise resolving to the generated batch ID for the transaction.
|
|
72
|
+
*/
|
|
73
|
+
async function processSingleTransaction({ addTransaction, chainId, from, networkClientId, origin, securityAlertId, sendCalls, transactions, validateSecurity, }) {
|
|
74
|
+
validateSingleSendCall(sendCalls, chainId);
|
|
75
|
+
const txParams = {
|
|
76
|
+
from,
|
|
77
|
+
...transactions[0].params,
|
|
78
|
+
type: TransactionEnvelopeType.feeMarket,
|
|
79
|
+
};
|
|
80
|
+
const securityRequest = {
|
|
81
|
+
method: MessageType.SendTransaction,
|
|
82
|
+
params: [txParams],
|
|
83
|
+
origin,
|
|
84
|
+
};
|
|
85
|
+
validateSecurity(securityRequest, chainId);
|
|
86
|
+
const batchId = generateBatchId();
|
|
87
|
+
await addTransaction(txParams, {
|
|
88
|
+
networkClientId,
|
|
89
|
+
origin,
|
|
90
|
+
securityAlertResponse: { securityAlertId },
|
|
91
|
+
batchId,
|
|
92
|
+
});
|
|
93
|
+
return batchId;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Processes multiple transactions from a sendCalls request as an atomic batch.
|
|
97
|
+
*
|
|
98
|
+
* @param params - Object containing all parameters needed for multiple transaction processing.
|
|
99
|
+
* @param params.addTransactionBatch - Function to add a batch of transactions atomically.
|
|
100
|
+
* @param params.isAtomicBatchSupported - Function to check if atomic batching is supported.
|
|
101
|
+
* @param params.chainId - The chain ID for the transactions.
|
|
102
|
+
* @param params.from - The sender address.
|
|
103
|
+
* @param params.getDismissSmartAccountSuggestionEnabled - Function to check if smart account suggestions are disabled.
|
|
104
|
+
* @param params.networkClientId - The network client ID.
|
|
105
|
+
* @param params.messenger - Messenger instance for controller communication.
|
|
106
|
+
* @param params.origin - The origin of the request (optional).
|
|
107
|
+
* @param params.sendCalls - The original sendCalls request.
|
|
108
|
+
* @param params.securityAlertId - The security alert ID for this batch.
|
|
109
|
+
* @param params.transactions - Array of transactions to process.
|
|
110
|
+
* @param params.validateSecurity - Function to validate security for the transactions.
|
|
111
|
+
* @returns Promise resolving to the generated batch ID for the transaction batch.
|
|
112
|
+
*/
|
|
113
|
+
async function processMultipleTransaction({ addTransactionBatch, isAtomicBatchSupported, chainId, from, getDismissSmartAccountSuggestionEnabled, networkClientId, messenger, origin, sendCalls, securityAlertId, transactions, validateSecurity, }) {
|
|
114
|
+
const batchSupport = await isAtomicBatchSupported({
|
|
115
|
+
address: from,
|
|
116
|
+
chainIds: [chainId],
|
|
117
|
+
});
|
|
118
|
+
const chainBatchSupport = batchSupport?.[0];
|
|
119
|
+
const keyringType = getAccountKeyringType(from, messenger);
|
|
120
|
+
const dismissSmartAccountSuggestionEnabled = getDismissSmartAccountSuggestionEnabled();
|
|
121
|
+
validateSendCalls(sendCalls, chainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType);
|
|
122
|
+
const result = await addTransactionBatch({
|
|
123
|
+
from,
|
|
124
|
+
networkClientId,
|
|
125
|
+
origin,
|
|
126
|
+
securityAlertId,
|
|
127
|
+
transactions,
|
|
128
|
+
validateSecurity,
|
|
129
|
+
});
|
|
130
|
+
return result.batchId;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Generate a transaction batch ID.
|
|
134
|
+
*
|
|
135
|
+
* @returns A unique batch ID as a hexadecimal string.
|
|
136
|
+
*/
|
|
137
|
+
function generateBatchId() {
|
|
138
|
+
const idString = uuid();
|
|
139
|
+
const idBytes = new Uint8Array(parse(idString));
|
|
140
|
+
return bytesToHex(idBytes);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Validates a single sendCalls request.
|
|
144
|
+
*
|
|
145
|
+
* @param sendCalls - The sendCalls request to validate.
|
|
146
|
+
* @param dappChainId - The chain ID that the dApp is connected to.
|
|
147
|
+
*/
|
|
148
|
+
function validateSingleSendCall(sendCalls, dappChainId) {
|
|
149
|
+
validateSendCallsVersion(sendCalls);
|
|
150
|
+
validateCapabilities(sendCalls);
|
|
151
|
+
validateDappChainId(sendCalls, dappChainId);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Validates a sendCalls request for multiple transactions.
|
|
155
|
+
*
|
|
156
|
+
* @param sendCalls - The sendCalls request to validate.
|
|
157
|
+
* @param dappChainId - The chain ID that the dApp is connected to
|
|
158
|
+
* @param dismissSmartAccountSuggestionEnabled - Whether smart account suggestions are disabled.
|
|
159
|
+
* @param chainBatchSupport - Information about atomic batch support for the chain.
|
|
160
|
+
* @param keyringType - The type of keyring associated with the account.
|
|
161
|
+
*/
|
|
162
|
+
function validateSendCalls(sendCalls, dappChainId, dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType) {
|
|
163
|
+
validateSendCallsVersion(sendCalls);
|
|
164
|
+
validateSendCallsChainId(sendCalls, dappChainId, chainBatchSupport);
|
|
165
|
+
validateCapabilities(sendCalls);
|
|
166
|
+
validateUpgrade(dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Validates the version of a sendCalls request.
|
|
170
|
+
*
|
|
171
|
+
* @param sendCalls - The sendCalls request to validate.
|
|
172
|
+
* @throws JsonRpcError if the version is not supported.
|
|
173
|
+
*/
|
|
174
|
+
function validateSendCallsVersion(sendCalls) {
|
|
175
|
+
const { version } = sendCalls;
|
|
176
|
+
if (version !== VERSION) {
|
|
177
|
+
throw rpcErrors.invalidInput(`Version not supported: Got ${version}, expected ${VERSION}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Validates that the chain ID in the sendCalls request matches the dApp's selected network.
|
|
182
|
+
*
|
|
183
|
+
* @param sendCalls - The sendCalls request to validate.
|
|
184
|
+
* @param dappChainId - The chain ID that the dApp is connected to
|
|
185
|
+
* @throws JsonRpcError if the chain IDs don't match
|
|
186
|
+
*/
|
|
187
|
+
function validateDappChainId(sendCalls, dappChainId) {
|
|
188
|
+
const { chainId: requestChainId } = sendCalls;
|
|
189
|
+
if (requestChainId &&
|
|
190
|
+
requestChainId.toLowerCase() !== dappChainId.toLowerCase()) {
|
|
191
|
+
throw rpcErrors.invalidParams(`Chain ID must match the dApp selected network: Got ${requestChainId}, expected ${dappChainId}`);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Validates the chain ID for sendCalls requests with additional EIP-7702 support checks.
|
|
196
|
+
*
|
|
197
|
+
* @param sendCalls - The sendCalls request to validate.
|
|
198
|
+
* @param dappChainId - The chain ID that the dApp is connected to
|
|
199
|
+
* @param chainBatchSupport - Information about atomic batch support for the chain
|
|
200
|
+
* @throws JsonRpcError if the chain ID doesn't match or EIP-7702 is not supported
|
|
201
|
+
*/
|
|
202
|
+
function validateSendCallsChainId(sendCalls, dappChainId, chainBatchSupport) {
|
|
203
|
+
validateDappChainId(sendCalls, dappChainId);
|
|
204
|
+
if (!chainBatchSupport) {
|
|
205
|
+
throw new JsonRpcError(EIP5792ErrorCode.UnsupportedChainId, `EIP-7702 not supported on chain: ${dappChainId}`);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Validates that all required capabilities in the sendCalls request are supported.
|
|
210
|
+
*
|
|
211
|
+
* @param sendCalls - The sendCalls request to validate.
|
|
212
|
+
* @throws JsonRpcError if unsupported non-optional capabilities are requested.
|
|
213
|
+
*/
|
|
214
|
+
function validateCapabilities(sendCalls) {
|
|
215
|
+
const { calls, capabilities } = sendCalls;
|
|
216
|
+
const requiredTopLevelCapabilities = Object.keys(capabilities ?? {}).filter((name) => capabilities?.[name].optional !== true);
|
|
217
|
+
const requiredCallCapabilities = calls.flatMap((call) => Object.keys(call.capabilities ?? {}).filter((name) => call.capabilities?.[name].optional !== true));
|
|
218
|
+
const requiredCapabilities = [
|
|
219
|
+
...requiredTopLevelCapabilities,
|
|
220
|
+
...requiredCallCapabilities,
|
|
221
|
+
];
|
|
222
|
+
if (requiredCapabilities?.length) {
|
|
223
|
+
throw new JsonRpcError(EIP5792ErrorCode.UnsupportedNonOptionalCapability, `Unsupported non-optional capabilities: ${requiredCapabilities.join(', ')}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Validates whether an EIP-7702 upgrade is allowed for the given parameters.
|
|
228
|
+
*
|
|
229
|
+
* @param dismissSmartAccountSuggestionEnabled - Whether smart account suggestions are disabled.
|
|
230
|
+
* @param chainBatchSupport - Information about atomic batch support for the chain.
|
|
231
|
+
* @param keyringType - The type of keyring associated with the account.
|
|
232
|
+
* @throws JsonRpcError if the upgrade is rejected due to user settings or account type.
|
|
233
|
+
*/
|
|
234
|
+
function validateUpgrade(dismissSmartAccountSuggestionEnabled, chainBatchSupport, keyringType) {
|
|
235
|
+
if (chainBatchSupport?.delegationAddress) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (dismissSmartAccountSuggestionEnabled) {
|
|
239
|
+
throw new JsonRpcError(EIP5792ErrorCode.RejectedUpgrade, 'EIP-7702 upgrade disabled by the user');
|
|
240
|
+
}
|
|
241
|
+
if (!KEYRING_TYPES_SUPPORTING_7702.includes(keyringType)) {
|
|
242
|
+
throw new JsonRpcError(EIP5792ErrorCode.RejectedUpgrade, 'EIP-7702 upgrade not supported on account');
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
//# sourceMappingURL=processSendCalls.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processSendCalls.mjs","sourceRoot":"","sources":["../../src/methods/processSendCalls.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,6BAA6B;AAQ/D,OAAO,EAAE,uBAAuB,EAAE,yCAAyC;AAE3E,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAC7C,OAAO,EAAE,KAAK,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AAM,wBAAwB,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CACtD,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC,MAAM,CACzC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,IAAI,CAAC,CAAC,QAAQ,KAAK,IAAI,CACtD,CACF,CAAC;IAEF,MAAM,oBAAoB,GAAG;QAC3B,GAAG,4BAA4B;QAC/B,GAAG,wBAAwB;KAC5B,CAAC;IAEF,IAAI,oBAAoB,EAAE,MAAM,EAAE;QAChC,MAAM,IAAI,YAAY,CACpB,gBAAgB,CAAC,gCAAgC,EACjD,0CAA0C,oBAAoB,CAAC,IAAI,CACjE,IAAI,CACL,EAAE,CACJ,CAAC;KACH;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,eAAe,CACtB,oCAA6C,EAC7C,iBAAgE,EAChE,WAAyB;IAEzB,IAAI,iBAAiB,EAAE,iBAAiB,EAAE;QACxC,OAAO;KACR;IAED,IAAI,oCAAoC,EAAE;QACxC,MAAM,IAAI,YAAY,CACpB,gBAAgB,CAAC,eAAe,EAChC,uCAAuC,CACxC,CAAC;KACH;IAED,IAAI,CAAC,6BAA6B,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE;QACxD,MAAM,IAAI,YAAY,CACpB,gBAAgB,CAAC,eAAe,EAChC,2CAA2C,CAC5C,CAAC;KACH;AACH,CAAC","sourcesContent":["import type {\n SendCalls,\n SendCallsResult,\n} from '@metamask/eth-json-rpc-middleware';\nimport 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 { bytesToHex } from '@metamask/utils';\nimport { parse, v4 as uuid } from 'uuid';\n\nimport {\n EIP5792ErrorCode,\n KEYRING_TYPES_SUPPORTING_7702,\n MessageType,\n VERSION,\n} from '../constants';\nimport type { EIP5792Messenger } 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};\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: SendCalls,\n req: ProcessSendCallsRequest,\n): Promise<SendCallsResult> {\n const {\n addTransactionBatch,\n addTransaction,\n getDismissSmartAccountSuggestionEnabled,\n isAtomicBatchSupported,\n validateSecurity: validateSecurityHook,\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 networkClientId,\n origin,\n securityAlertId,\n sendCalls: params,\n transactions,\n validateSecurity,\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 });\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.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 * @returns Promise resolving to the generated batch ID for the transaction.\n */\nasync function processSingleTransaction({\n addTransaction,\n chainId,\n from,\n networkClientId,\n origin,\n securityAlertId,\n sendCalls,\n transactions,\n validateSecurity,\n}: {\n addTransaction: TransactionController['addTransaction'];\n chainId: Hex;\n from: Hex;\n networkClientId: string;\n origin?: string;\n securityAlertId: string;\n sendCalls: SendCalls;\n transactions: { params: BatchTransactionParams }[];\n validateSecurity: (\n securityRequest: ValidateSecurityRequest,\n chainId: Hex,\n ) => void;\n}) {\n validateSingleSendCall(sendCalls, chainId);\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 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 * @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}: {\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: SendCalls;\n securityAlertId: string;\n transactions: { params: BatchTransactionParams }[];\n validateSecurity: (\n securityRequest: ValidateSecurityRequest,\n chainId: Hex,\n ) => Promise<void>;\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 );\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 */\nfunction validateSingleSendCall(sendCalls: SendCalls, dappChainId: Hex) {\n validateSendCallsVersion(sendCalls);\n validateCapabilities(sendCalls);\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 */\nfunction validateSendCalls(\n sendCalls: SendCalls,\n dappChainId: Hex,\n dismissSmartAccountSuggestionEnabled: boolean,\n chainBatchSupport: IsAtomicBatchSupportedResultEntry | undefined,\n keyringType: KeyringTypes,\n) {\n validateSendCallsVersion(sendCalls);\n validateSendCallsChainId(sendCalls, dappChainId, chainBatchSupport);\n validateCapabilities(sendCalls);\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: SendCalls) {\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: SendCalls, 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: SendCalls,\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 * @throws JsonRpcError if unsupported non-optional capabilities are requested.\n */\nfunction validateCapabilities(sendCalls: SendCalls) {\n const { calls, capabilities } = sendCalls;\n\n const requiredTopLevelCapabilities = Object.keys(capabilities ?? {}).filter(\n (name) => capabilities?.[name].optional !== true,\n );\n\n const requiredCallCapabilities = calls.flatMap((call) =>\n Object.keys(call.capabilities ?? {}).filter(\n (name) => 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\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"]}
|
package/dist/types.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type { Messenger } from '@metamask/base-controller';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n} from '@metamask/network-controller';\nimport type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';\nimport type { TransactionControllerGetStateAction } from '@metamask/transaction-controller';\n\ntype Actions =\n | AccountsControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction\n | NetworkControllerGetNetworkClientByIdAction\n | TransactionControllerGetStateAction\n | PreferencesControllerGetStateAction\n | NetworkControllerGetStateAction;\n\nexport type EIP5792Messenger = Messenger<Actions, never>;\n"]}
|
package/dist/types.d.cts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AccountsControllerGetSelectedAccountAction, AccountsControllerGetStateAction } from "@metamask/accounts-controller";
|
|
2
|
+
import type { Messenger } from "@metamask/base-controller";
|
|
3
|
+
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction } from "@metamask/network-controller";
|
|
4
|
+
import type { PreferencesControllerGetStateAction } from "@metamask/preferences-controller";
|
|
5
|
+
import type { TransactionControllerGetStateAction } from "@metamask/transaction-controller";
|
|
6
|
+
type Actions = AccountsControllerGetStateAction | AccountsControllerGetSelectedAccountAction | NetworkControllerGetNetworkClientByIdAction | TransactionControllerGetStateAction | PreferencesControllerGetStateAction | NetworkControllerGetStateAction;
|
|
7
|
+
export type EIP5792Messenger = Messenger<Actions, never>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=types.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0CAA0C,EAC1C,gCAAgC,EACjC,sCAAsC;AACvC,OAAO,KAAK,EAAE,SAAS,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAChC,qCAAqC;AACtC,OAAO,KAAK,EAAE,mCAAmC,EAAE,yCAAyC;AAC5F,OAAO,KAAK,EAAE,mCAAmC,EAAE,yCAAyC;AAE5F,KAAK,OAAO,GACR,gCAAgC,GAChC,0CAA0C,GAC1C,2CAA2C,GAC3C,mCAAmC,GACnC,mCAAmC,GACnC,+BAA+B,CAAC;AAEpC,MAAM,MAAM,gBAAgB,GAAG,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC"}
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AccountsControllerGetSelectedAccountAction, AccountsControllerGetStateAction } from "@metamask/accounts-controller";
|
|
2
|
+
import type { Messenger } from "@metamask/base-controller";
|
|
3
|
+
import type { NetworkControllerGetNetworkClientByIdAction, NetworkControllerGetStateAction } from "@metamask/network-controller";
|
|
4
|
+
import type { PreferencesControllerGetStateAction } from "@metamask/preferences-controller";
|
|
5
|
+
import type { TransactionControllerGetStateAction } from "@metamask/transaction-controller";
|
|
6
|
+
type Actions = AccountsControllerGetStateAction | AccountsControllerGetSelectedAccountAction | NetworkControllerGetNetworkClientByIdAction | TransactionControllerGetStateAction | PreferencesControllerGetStateAction | NetworkControllerGetStateAction;
|
|
7
|
+
export type EIP5792Messenger = Messenger<Actions, never>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=types.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,0CAA0C,EAC1C,gCAAgC,EACjC,sCAAsC;AACvC,OAAO,KAAK,EAAE,SAAS,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,2CAA2C,EAC3C,+BAA+B,EAChC,qCAAqC;AACtC,OAAO,KAAK,EAAE,mCAAmC,EAAE,yCAAyC;AAC5F,OAAO,KAAK,EAAE,mCAAmC,EAAE,yCAAyC;AAE5F,KAAK,OAAO,GACR,gCAAgC,GAChC,0CAA0C,GAC1C,2CAA2C,GAC3C,mCAAmC,GACnC,mCAAmC,GACnC,+BAA+B,CAAC;AAEpC,MAAM,MAAM,gBAAgB,GAAG,SAAS,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC"}
|
package/dist/types.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type {\n AccountsControllerGetSelectedAccountAction,\n AccountsControllerGetStateAction,\n} from '@metamask/accounts-controller';\nimport type { Messenger } from '@metamask/base-controller';\nimport type {\n NetworkControllerGetNetworkClientByIdAction,\n NetworkControllerGetStateAction,\n} from '@metamask/network-controller';\nimport type { PreferencesControllerGetStateAction } from '@metamask/preferences-controller';\nimport type { TransactionControllerGetStateAction } from '@metamask/transaction-controller';\n\ntype Actions =\n | AccountsControllerGetStateAction\n | AccountsControllerGetSelectedAccountAction\n | NetworkControllerGetNetworkClientByIdAction\n | TransactionControllerGetStateAction\n | PreferencesControllerGetStateAction\n | NetworkControllerGetStateAction;\n\nexport type EIP5792Messenger = Messenger<Actions, never>;\n"]}
|
package/dist/utils.cjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAccountKeyringType = void 0;
|
|
4
|
+
const rpc_errors_1 = require("@metamask/rpc-errors");
|
|
5
|
+
const constants_1 = require("./constants.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Retrieves the keyring type for a given account address.
|
|
8
|
+
*
|
|
9
|
+
* @param accountAddress - The account address to look up.
|
|
10
|
+
* @param messenger - Messenger instance for controller communication.
|
|
11
|
+
* @returns The keyring type associated with the account.
|
|
12
|
+
* @throws JsonRpcError if the account type is unknown or not found.
|
|
13
|
+
*/
|
|
14
|
+
function getAccountKeyringType(accountAddress, messenger) {
|
|
15
|
+
const { accounts } = messenger.call('AccountsController:getState').internalAccounts;
|
|
16
|
+
const account = Object.values(accounts).find((acc) => acc.address.toLowerCase() === accountAddress.toLowerCase());
|
|
17
|
+
const keyringType = account?.metadata?.keyring?.type;
|
|
18
|
+
if (!keyringType) {
|
|
19
|
+
throw new rpc_errors_1.JsonRpcError(constants_1.EIP5792ErrorCode.RejectedUpgrade, 'EIP-7702 upgrade not supported as account type is unknown');
|
|
20
|
+
}
|
|
21
|
+
return keyringType;
|
|
22
|
+
}
|
|
23
|
+
exports.getAccountKeyringType = getAccountKeyringType;
|
|
24
|
+
//# sourceMappingURL=utils.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AACA,qDAAoD;AAGpD,+CAA+C;AAG/C;;;;;;;GAOG;AACH,SAAgB,qBAAqB,CACnC,cAAmB,EACnB,SAA2B;IAE3B,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC,IAAI,CACjC,6BAA6B,CAC9B,CAAC,gBAAgB,CAAC;IAEnB,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,CAC1C,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,cAAc,CAAC,WAAW,EAAE,CACpE,CAAC;IAEF,MAAM,WAAW,GAAG,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC;IAErD,IAAI,CAAC,WAAW,EAAE;QAChB,MAAM,IAAI,yBAAY,CACpB,4BAAgB,CAAC,eAAe,EAChC,2DAA2D,CAC5D,CAAC;KACH;IAED,OAAO,WAA2B,CAAC;AACrC,CAAC;AAtBD,sDAsBC","sourcesContent":["import type { KeyringTypes } from '@metamask/keyring-controller';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type { Hex } from '@metamask/utils';\n\nimport { EIP5792ErrorCode } from './constants';\nimport type { EIP5792Messenger } from './types';\n\n/**\n * Retrieves the keyring type for a given account address.\n *\n * @param accountAddress - The account address to look up.\n * @param messenger - Messenger instance for controller communication.\n * @returns The keyring type associated with the account.\n * @throws JsonRpcError if the account type is unknown or not found.\n */\nexport function getAccountKeyringType(\n accountAddress: Hex,\n messenger: EIP5792Messenger,\n): KeyringTypes {\n const { accounts } = messenger.call(\n 'AccountsController:getState',\n ).internalAccounts;\n\n const account = Object.values(accounts).find(\n (acc) => acc.address.toLowerCase() === accountAddress.toLowerCase(),\n );\n\n const keyringType = account?.metadata?.keyring?.type;\n\n if (!keyringType) {\n throw new JsonRpcError(\n EIP5792ErrorCode.RejectedUpgrade,\n 'EIP-7702 upgrade not supported as account type is unknown',\n );\n }\n\n return keyringType as KeyringTypes;\n}\n"]}
|
package/dist/utils.d.cts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { KeyringTypes } from "@metamask/keyring-controller";
|
|
2
|
+
import type { Hex } from "@metamask/utils";
|
|
3
|
+
import type { EIP5792Messenger } from "./types.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves the keyring type for a given account address.
|
|
6
|
+
*
|
|
7
|
+
* @param accountAddress - The account address to look up.
|
|
8
|
+
* @param messenger - Messenger instance for controller communication.
|
|
9
|
+
* @returns The keyring type associated with the account.
|
|
10
|
+
* @throws JsonRpcError if the account type is unknown or not found.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getAccountKeyringType(accountAddress: Hex, messenger: EIP5792Messenger): KeyringTypes;
|
|
13
|
+
//# sourceMappingURL=utils.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,qCAAqC;AAEjE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAG3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,oBAAgB;AAEhD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACnC,cAAc,EAAE,GAAG,EACnB,SAAS,EAAE,gBAAgB,GAC1B,YAAY,CAmBd"}
|
package/dist/utils.d.mts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { KeyringTypes } from "@metamask/keyring-controller";
|
|
2
|
+
import type { Hex } from "@metamask/utils";
|
|
3
|
+
import type { EIP5792Messenger } from "./types.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Retrieves the keyring type for a given account address.
|
|
6
|
+
*
|
|
7
|
+
* @param accountAddress - The account address to look up.
|
|
8
|
+
* @param messenger - Messenger instance for controller communication.
|
|
9
|
+
* @returns The keyring type associated with the account.
|
|
10
|
+
* @throws JsonRpcError if the account type is unknown or not found.
|
|
11
|
+
*/
|
|
12
|
+
export declare function getAccountKeyringType(accountAddress: Hex, messenger: EIP5792Messenger): KeyringTypes;
|
|
13
|
+
//# sourceMappingURL=utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,qCAAqC;AAEjE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAG3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,oBAAgB;AAEhD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACnC,cAAc,EAAE,GAAG,EACnB,SAAS,EAAE,gBAAgB,GAC1B,YAAY,CAmBd"}
|
package/dist/utils.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { JsonRpcError } from "@metamask/rpc-errors";
|
|
2
|
+
import { EIP5792ErrorCode } from "./constants.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves the keyring type for a given account address.
|
|
5
|
+
*
|
|
6
|
+
* @param accountAddress - The account address to look up.
|
|
7
|
+
* @param messenger - Messenger instance for controller communication.
|
|
8
|
+
* @returns The keyring type associated with the account.
|
|
9
|
+
* @throws JsonRpcError if the account type is unknown or not found.
|
|
10
|
+
*/
|
|
11
|
+
export function getAccountKeyringType(accountAddress, messenger) {
|
|
12
|
+
const { accounts } = messenger.call('AccountsController:getState').internalAccounts;
|
|
13
|
+
const account = Object.values(accounts).find((acc) => acc.address.toLowerCase() === accountAddress.toLowerCase());
|
|
14
|
+
const keyringType = account?.metadata?.keyring?.type;
|
|
15
|
+
if (!keyringType) {
|
|
16
|
+
throw new JsonRpcError(EIP5792ErrorCode.RejectedUpgrade, 'EIP-7702 upgrade not supported as account type is unknown');
|
|
17
|
+
}
|
|
18
|
+
return keyringType;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,6BAA6B;AAGpD,OAAO,EAAE,gBAAgB,EAAE,wBAAoB;AAG/C;;;;;;;GAOG;AACH,MAAM,UAAU,qBAAqB,CACnC,cAAmB,EACnB,SAA2B;IAE3B,MAAM,EAAE,QAAQ,EAAE,GAAG,SAAS,CAAC,IAAI,CACjC,6BAA6B,CAC9B,CAAC,gBAAgB,CAAC;IAEnB,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,CAC1C,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,cAAc,CAAC,WAAW,EAAE,CACpE,CAAC;IAEF,MAAM,WAAW,GAAG,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,CAAC;IAErD,IAAI,CAAC,WAAW,EAAE;QAChB,MAAM,IAAI,YAAY,CACpB,gBAAgB,CAAC,eAAe,EAChC,2DAA2D,CAC5D,CAAC;KACH;IAED,OAAO,WAA2B,CAAC;AACrC,CAAC","sourcesContent":["import type { KeyringTypes } from '@metamask/keyring-controller';\nimport { JsonRpcError } from '@metamask/rpc-errors';\nimport type { Hex } from '@metamask/utils';\n\nimport { EIP5792ErrorCode } from './constants';\nimport type { EIP5792Messenger } from './types';\n\n/**\n * Retrieves the keyring type for a given account address.\n *\n * @param accountAddress - The account address to look up.\n * @param messenger - Messenger instance for controller communication.\n * @returns The keyring type associated with the account.\n * @throws JsonRpcError if the account type is unknown or not found.\n */\nexport function getAccountKeyringType(\n accountAddress: Hex,\n messenger: EIP5792Messenger,\n): KeyringTypes {\n const { accounts } = messenger.call(\n 'AccountsController:getState',\n ).internalAccounts;\n\n const account = Object.values(accounts).find(\n (acc) => acc.address.toLowerCase() === accountAddress.toLowerCase(),\n );\n\n const keyringType = account?.metadata?.keyring?.type;\n\n if (!keyringType) {\n throw new JsonRpcError(\n EIP5792ErrorCode.RejectedUpgrade,\n 'EIP-7702 upgrade not supported as account type is unknown',\n );\n }\n\n return keyringType as KeyringTypes;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metamask/eip-5792-middleware",
|
|
3
|
+
"version": "1.0.0",
|
|
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
|
+
"keywords": [
|
|
6
|
+
"MetaMask",
|
|
7
|
+
"Ethereum"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/MetaMask/core/tree/main/packages/eip-5792-middleware#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/MetaMask/core/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/MetaMask/core.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/index.d.mts",
|
|
23
|
+
"default": "./dist/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/index.d.cts",
|
|
27
|
+
"default": "./dist/index.cjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"types": "./dist/index.d.cts",
|
|
34
|
+
"files": [
|
|
35
|
+
"dist/"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
|
|
39
|
+
"build:docs": "typedoc",
|
|
40
|
+
"changelog:update": "../../scripts/update-changelog.sh @metamask/eip-5792-middleware",
|
|
41
|
+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/eip-5792-middleware",
|
|
42
|
+
"publish:preview": "yarn npm publish --tag preview",
|
|
43
|
+
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
44
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
|
|
45
|
+
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
|
|
46
|
+
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
|
|
47
|
+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@metamask/eth-json-rpc-middleware": "^17.0.1",
|
|
51
|
+
"@metamask/transaction-controller": "^60.2.0",
|
|
52
|
+
"@metamask/utils": "^11.4.2",
|
|
53
|
+
"uuid": "^8.3.2"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@metamask/auto-changelog": "^3.4.4",
|
|
57
|
+
"@metamask/keyring-controller": "^23.0.0",
|
|
58
|
+
"@metamask/rpc-errors": "^7.0.2",
|
|
59
|
+
"@types/jest": "^27.4.1",
|
|
60
|
+
"deepmerge": "^4.2.2",
|
|
61
|
+
"jest": "^27.5.1",
|
|
62
|
+
"ts-jest": "^27.1.4",
|
|
63
|
+
"typedoc": "^0.24.8",
|
|
64
|
+
"typedoc-plugin-missing-exports": "^2.0.0",
|
|
65
|
+
"typescript": "~5.2.2"
|
|
66
|
+
},
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": "^18.18 || >=20"
|
|
69
|
+
},
|
|
70
|
+
"publishConfig": {
|
|
71
|
+
"access": "public",
|
|
72
|
+
"registry": "https://registry.npmjs.org/"
|
|
73
|
+
}
|
|
74
|
+
}
|