@metamask-previews/transaction-controller 59.0.0-preview-1abef88 → 59.0.0-preview-3f5f144a
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 +4 -0
- package/dist/utils/transaction-type.cjs +15 -10
- package/dist/utils/transaction-type.cjs.map +1 -1
- package/dist/utils/transaction-type.d.cts.map +1 -1
- package/dist/utils/transaction-type.d.mts.map +1 -1
- package/dist/utils/transaction-type.mjs +15 -10
- package/dist/utils/transaction-type.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- Prevent incorrect `simpleSend` classification when contract code lookup fails ([#6206](https://github.com/MetaMask/core/pull/6206))
|
|
13
|
+
|
|
10
14
|
## [59.0.0]
|
|
11
15
|
|
|
12
16
|
### Added
|
|
@@ -5,7 +5,9 @@ const abi_1 = require("@ethersproject/abi");
|
|
|
5
5
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
6
6
|
const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
|
|
7
7
|
const eip7702_1 = require("./eip7702.cjs");
|
|
8
|
+
const logger_1 = require("../logger.cjs");
|
|
8
9
|
const types_1 = require("../types.cjs");
|
|
10
|
+
const log = (0, logger_1.createModuleLogger)(logger_1.projectLogger, 'transaction-type');
|
|
9
11
|
exports.ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';
|
|
10
12
|
const ERC20Interface = new abi_1.Interface(metamask_eth_abis_1.abiERC20);
|
|
11
13
|
const ERC721Interface = new abi_1.Interface(metamask_eth_abis_1.abiERC721);
|
|
@@ -27,12 +29,14 @@ async function determineTransactionType(txParams, ethQuery) {
|
|
|
27
29
|
}
|
|
28
30
|
let getCodeResponse;
|
|
29
31
|
let isContractAddress = Boolean(data?.length);
|
|
32
|
+
let hasReadAddressAsContractFailed = false;
|
|
30
33
|
if (ethQuery) {
|
|
31
34
|
const response = await readAddressAsContract(ethQuery, to);
|
|
32
35
|
getCodeResponse = response.contractCode;
|
|
33
36
|
isContractAddress = response.isContractAddress;
|
|
37
|
+
hasReadAddressAsContractFailed = Boolean(response.error);
|
|
34
38
|
}
|
|
35
|
-
if (!isContractAddress) {
|
|
39
|
+
if (!isContractAddress && !hasReadAddressAsContractFailed) {
|
|
36
40
|
return { type: types_1.TransactionType.simpleSend, getCodeResponse };
|
|
37
41
|
}
|
|
38
42
|
const hasValue = Number(txParams.value ?? '0') !== 0;
|
|
@@ -100,17 +104,18 @@ async function readAddressAsContract(ethQuery, address) {
|
|
|
100
104
|
let contractCode;
|
|
101
105
|
try {
|
|
102
106
|
contractCode = await (0, controller_utils_1.query)(ethQuery, 'getCode', [address]);
|
|
103
|
-
// Not used
|
|
104
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
105
107
|
}
|
|
106
108
|
catch (error) {
|
|
107
|
-
|
|
109
|
+
log(`Error reading contract code for address ${address}:`, error);
|
|
110
|
+
return {
|
|
111
|
+
contractCode: null,
|
|
112
|
+
isContractAddress: false,
|
|
113
|
+
error: error,
|
|
114
|
+
};
|
|
108
115
|
}
|
|
109
|
-
const isContractAddress = contractCode
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
: false;
|
|
114
|
-
return { contractCode, isContractAddress };
|
|
116
|
+
const isContractAddress = contractCode !== '0x' &&
|
|
117
|
+
contractCode !== '0x0' &&
|
|
118
|
+
!contractCode.startsWith(eip7702_1.DELEGATION_PREFIX);
|
|
119
|
+
return { contractCode, isContractAddress, error: null };
|
|
115
120
|
}
|
|
116
121
|
//# sourceMappingURL=transaction-type.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-type.cjs","sourceRoot":"","sources":["../../src/utils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AC,CAAC,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IAErD,KAAK,MAAM,iBAAiB,IAAI;QAC9B,cAAc;QACd,eAAe;QACf,gBAAgB;QAChB,aAAa;KACd,EAAE;QACD,IAAI;YACF,OAAO,iBAAiB,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC;SACrD;QAAC,MAAM;YACN,sBAAsB;SACvB;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;GAMG;AACH,KAAK,UAAU,qBAAqB,CAClC,QAAkB,EAClB,OAAgB;IAMhB,IAAI,YAAY,CAAC;IACjB,IAAI;QACF,YAAY,GAAG,MAAM,IAAA,wBAAK,EAAC,QAAQ,EAAE,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;KAC5D;IAAC,OAAO,KAAK,EAAE;QACd,GAAG,CAAC,2CAA2C,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;QAClE,OAAO;YACL,YAAY,EAAE,IAAI;YAClB,iBAAiB,EAAE,KAAK;YACxB,KAAK,EAAE,KAAc;SACtB,CAAC;KACH;IAED,MAAM,iBAAiB,GACrB,YAAY,KAAK,IAAI;QACrB,YAAY,KAAK,KAAK;QACtB,CAAC,YAAY,CAAC,UAAU,CAAC,2BAAiB,CAAC,CAAC;IAC9C,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC1D,CAAC","sourcesContent":["import { Interface } from '@ethersproject/abi';\nimport { query } from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport {\n abiERC721,\n abiERC20,\n abiERC1155,\n abiFiatTokenV2,\n} from '@metamask/metamask-eth-abis';\n\nimport { DELEGATION_PREFIX } from './eip7702';\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { InferTransactionTypeResult, TransactionParams } from '../types';\nimport { TransactionType } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'transaction-type');\n\nexport const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';\n\nconst ERC20Interface = new Interface(abiERC20);\nconst ERC721Interface = new Interface(abiERC721);\nconst ERC1155Interface = new Interface(abiERC1155);\nconst USDCInterface = new Interface(abiFiatTokenV2);\n\n/**\n * Determines the type of the transaction by analyzing the txParams.\n * It will never return TRANSACTION_TYPE_CANCEL or TRANSACTION_TYPE_RETRY as these\n * represent specific events that we specify manually at transaction creation.\n *\n * @param txParams - Parameters for the transaction.\n * @param ethQuery - EthQuery instance.\n * @returns A object with the transaction type and the contract code response in Hex.\n */\nexport async function determineTransactionType(\n txParams: TransactionParams,\n ethQuery?: EthQuery,\n): Promise<InferTransactionTypeResult> {\n const { data, to } = txParams;\n\n if (data && !to) {\n return { type: TransactionType.deployContract, getCodeResponse: undefined };\n }\n\n let getCodeResponse;\n let isContractAddress = Boolean(data?.length);\n let hasReadAddressAsContractFailed: boolean = false;\n\n if (ethQuery) {\n const response = await readAddressAsContract(ethQuery, to);\n\n getCodeResponse = response.contractCode;\n isContractAddress = response.isContractAddress;\n hasReadAddressAsContractFailed = Boolean(response.error);\n }\n\n if (!isContractAddress && !hasReadAddressAsContractFailed) {\n return { type: TransactionType.simpleSend, getCodeResponse };\n }\n\n const hasValue = Number(txParams.value ?? '0') !== 0;\n\n const contractInteractionResult = {\n type: TransactionType.contractInteraction,\n getCodeResponse,\n };\n\n if (!data || hasValue) {\n return contractInteractionResult;\n }\n\n const name = getMethodName(data);\n\n if (!name) {\n return contractInteractionResult;\n }\n\n const tokenMethodName = [\n TransactionType.tokenMethodApprove,\n TransactionType.tokenMethodSetApprovalForAll,\n TransactionType.tokenMethodTransfer,\n TransactionType.tokenMethodTransferFrom,\n TransactionType.tokenMethodSafeTransferFrom,\n TransactionType.tokenMethodIncreaseAllowance,\n ].find(\n (methodName) => methodName.toLowerCase() === (name as string).toLowerCase(),\n );\n\n if (tokenMethodName) {\n return { type: tokenMethodName, getCodeResponse };\n }\n\n return contractInteractionResult;\n}\n\n/**\n * Attempts to decode transaction data using ABIs for three different token standards: ERC20, ERC721, ERC1155.\n * The data will decode correctly if the transaction is an interaction with a contract that matches one of these\n * contract standards\n *\n * @param data - Encoded transaction data.\n * @returns A representation of an ethereum contract call.\n */\nfunction getMethodName(data?: string): string | undefined {\n if (!data || data.length < 10) {\n return undefined;\n }\n\n const fourByte = data.substring(0, 10).toLowerCase();\n\n for (const interfaceInstance of [\n ERC20Interface,\n ERC721Interface,\n ERC1155Interface,\n USDCInterface,\n ]) {\n try {\n return interfaceInstance.getFunction(fourByte).name;\n } catch {\n // Intentionally empty\n }\n }\n\n return undefined;\n}\n\n/**\n * Reads an Ethereum address and determines if it is a contract address.\n *\n * @param ethQuery - The Ethereum query object used to interact with the Ethereum blockchain.\n * @param address - The Ethereum address.\n * @returns An object containing the contract code and a boolean indicating if it is a contract address.\n */\nasync function readAddressAsContract(\n ethQuery: EthQuery,\n address?: string,\n): Promise<{\n contractCode: string | null;\n isContractAddress: boolean;\n error: Error | null;\n}> {\n let contractCode;\n try {\n contractCode = await query(ethQuery, 'getCode', [address]);\n } catch (error) {\n log(`Error reading contract code for address ${address}:`, error);\n return {\n contractCode: null,\n isContractAddress: false,\n error: error as Error,\n };\n }\n\n const isContractAddress =\n contractCode !== '0x' &&\n contractCode !== '0x0' &&\n !contractCode.startsWith(DELEGATION_PREFIX);\n return { contractCode, isContractAddress, error: null };\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-type.d.cts","sourceRoot":"","sources":["../../src/utils/transaction-type.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,QAAQ,4BAA4B;
|
|
1
|
+
{"version":3,"file":"transaction-type.d.cts","sourceRoot":"","sources":["../../src/utils/transaction-type.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,QAAQ,4BAA4B;AAUhD,OAAO,KAAK,EAAE,0BAA0B,EAAE,iBAAiB,EAAE,qBAAiB;AAK9E,eAAO,MAAM,kBAAkB,qCAAqC,CAAC;AAOrE;;;;;;;;GAQG;AACH,wBAAsB,wBAAwB,CAC5C,QAAQ,EAAE,iBAAiB,EAC3B,QAAQ,CAAC,EAAE,QAAQ,GAClB,OAAO,CAAC,0BAA0B,CAAC,CAwDrC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-type.d.mts","sourceRoot":"","sources":["../../src/utils/transaction-type.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,QAAQ,4BAA4B;
|
|
1
|
+
{"version":3,"file":"transaction-type.d.mts","sourceRoot":"","sources":["../../src/utils/transaction-type.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,QAAQ,4BAA4B;AAUhD,OAAO,KAAK,EAAE,0BAA0B,EAAE,iBAAiB,EAAE,qBAAiB;AAK9E,eAAO,MAAM,kBAAkB,qCAAqC,CAAC;AAOrE;;;;;;;;GAQG;AACH,wBAAsB,wBAAwB,CAC5C,QAAQ,EAAE,iBAAiB,EAC3B,QAAQ,CAAC,EAAE,QAAQ,GAClB,OAAO,CAAC,0BAA0B,CAAC,CAwDrC"}
|
|
@@ -2,7 +2,9 @@ import { Interface } from "@ethersproject/abi";
|
|
|
2
2
|
import { query } from "@metamask/controller-utils";
|
|
3
3
|
import { abiERC721, abiERC20, abiERC1155, abiFiatTokenV2 } from "@metamask/metamask-eth-abis";
|
|
4
4
|
import { DELEGATION_PREFIX } from "./eip7702.mjs";
|
|
5
|
+
import { createModuleLogger, projectLogger } from "../logger.mjs";
|
|
5
6
|
import { TransactionType } from "../types.mjs";
|
|
7
|
+
const log = createModuleLogger(projectLogger, 'transaction-type');
|
|
6
8
|
export const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';
|
|
7
9
|
const ERC20Interface = new Interface(abiERC20);
|
|
8
10
|
const ERC721Interface = new Interface(abiERC721);
|
|
@@ -24,12 +26,14 @@ export async function determineTransactionType(txParams, ethQuery) {
|
|
|
24
26
|
}
|
|
25
27
|
let getCodeResponse;
|
|
26
28
|
let isContractAddress = Boolean(data?.length);
|
|
29
|
+
let hasReadAddressAsContractFailed = false;
|
|
27
30
|
if (ethQuery) {
|
|
28
31
|
const response = await readAddressAsContract(ethQuery, to);
|
|
29
32
|
getCodeResponse = response.contractCode;
|
|
30
33
|
isContractAddress = response.isContractAddress;
|
|
34
|
+
hasReadAddressAsContractFailed = Boolean(response.error);
|
|
31
35
|
}
|
|
32
|
-
if (!isContractAddress) {
|
|
36
|
+
if (!isContractAddress && !hasReadAddressAsContractFailed) {
|
|
33
37
|
return { type: TransactionType.simpleSend, getCodeResponse };
|
|
34
38
|
}
|
|
35
39
|
const hasValue = Number(txParams.value ?? '0') !== 0;
|
|
@@ -96,17 +100,18 @@ async function readAddressAsContract(ethQuery, address) {
|
|
|
96
100
|
let contractCode;
|
|
97
101
|
try {
|
|
98
102
|
contractCode = await query(ethQuery, 'getCode', [address]);
|
|
99
|
-
// Not used
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
101
103
|
}
|
|
102
104
|
catch (error) {
|
|
103
|
-
|
|
105
|
+
log(`Error reading contract code for address ${address}:`, error);
|
|
106
|
+
return {
|
|
107
|
+
contractCode: null,
|
|
108
|
+
isContractAddress: false,
|
|
109
|
+
error: error,
|
|
110
|
+
};
|
|
104
111
|
}
|
|
105
|
-
const isContractAddress = contractCode
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
: false;
|
|
110
|
-
return { contractCode, isContractAddress };
|
|
112
|
+
const isContractAddress = contractCode !== '0x' &&
|
|
113
|
+
contractCode !== '0x0' &&
|
|
114
|
+
!contractCode.startsWith(DELEGATION_PREFIX);
|
|
115
|
+
return { contractCode, isContractAddress, error: null };
|
|
111
116
|
}
|
|
112
117
|
//# sourceMappingURL=transaction-type.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transaction-type.mjs","sourceRoot":"","sources":["../../src/utils/transaction-type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE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Ae,EAAE,eAAe,EAAE,CAAC;KACnD;IAED,OAAO,yBAAyB,CAAC;AACnC,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,aAAa,CAAC,IAAa;IAClC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,EAAE,EAAE;QAC7B,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IAErD,KAAK,MAAM,iBAAiB,IAAI;QAC9B,cAAc;QACd,eAAe;QACf,gBAAgB;QAChB,aAAa;KACd,EAAE;QACD,IAAI;YACF,OAAO,iBAAiB,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC;SACrD;QAAC,MAAM;YACN,sBAAsB;SACvB;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED;;;;;;GAMG;AACH,KAAK,UAAU,qBAAqB,CAClC,QAAkB,EAClB,OAAgB;IAMhB,IAAI,YAAY,CAAC;IACjB,IAAI;QACF,YAAY,GAAG,MAAM,KAAK,CAAC,QAAQ,EAAE,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;KAC5D;IAAC,OAAO,KAAK,EAAE;QACd,GAAG,CAAC,2CAA2C,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;QAClE,OAAO;YACL,YAAY,EAAE,IAAI;YAClB,iBAAiB,EAAE,KAAK;YACxB,KAAK,EAAE,KAAc;SACtB,CAAC;KACH;IAED,MAAM,iBAAiB,GACrB,YAAY,KAAK,IAAI;QACrB,YAAY,KAAK,KAAK;QACtB,CAAC,YAAY,CAAC,UAAU,CAAC,iBAAiB,CAAC,CAAC;IAC9C,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;AAC1D,CAAC","sourcesContent":["import { Interface } from '@ethersproject/abi';\nimport { query } from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport {\n abiERC721,\n abiERC20,\n abiERC1155,\n abiFiatTokenV2,\n} from '@metamask/metamask-eth-abis';\n\nimport { DELEGATION_PREFIX } from './eip7702';\nimport { createModuleLogger, projectLogger } from '../logger';\nimport type { InferTransactionTypeResult, TransactionParams } from '../types';\nimport { TransactionType } from '../types';\n\nconst log = createModuleLogger(projectLogger, 'transaction-type');\n\nexport const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';\n\nconst ERC20Interface = new Interface(abiERC20);\nconst ERC721Interface = new Interface(abiERC721);\nconst ERC1155Interface = new Interface(abiERC1155);\nconst USDCInterface = new Interface(abiFiatTokenV2);\n\n/**\n * Determines the type of the transaction by analyzing the txParams.\n * It will never return TRANSACTION_TYPE_CANCEL or TRANSACTION_TYPE_RETRY as these\n * represent specific events that we specify manually at transaction creation.\n *\n * @param txParams - Parameters for the transaction.\n * @param ethQuery - EthQuery instance.\n * @returns A object with the transaction type and the contract code response in Hex.\n */\nexport async function determineTransactionType(\n txParams: TransactionParams,\n ethQuery?: EthQuery,\n): Promise<InferTransactionTypeResult> {\n const { data, to } = txParams;\n\n if (data && !to) {\n return { type: TransactionType.deployContract, getCodeResponse: undefined };\n }\n\n let getCodeResponse;\n let isContractAddress = Boolean(data?.length);\n let hasReadAddressAsContractFailed: boolean = false;\n\n if (ethQuery) {\n const response = await readAddressAsContract(ethQuery, to);\n\n getCodeResponse = response.contractCode;\n isContractAddress = response.isContractAddress;\n hasReadAddressAsContractFailed = Boolean(response.error);\n }\n\n if (!isContractAddress && !hasReadAddressAsContractFailed) {\n return { type: TransactionType.simpleSend, getCodeResponse };\n }\n\n const hasValue = Number(txParams.value ?? '0') !== 0;\n\n const contractInteractionResult = {\n type: TransactionType.contractInteraction,\n getCodeResponse,\n };\n\n if (!data || hasValue) {\n return contractInteractionResult;\n }\n\n const name = getMethodName(data);\n\n if (!name) {\n return contractInteractionResult;\n }\n\n const tokenMethodName = [\n TransactionType.tokenMethodApprove,\n TransactionType.tokenMethodSetApprovalForAll,\n TransactionType.tokenMethodTransfer,\n TransactionType.tokenMethodTransferFrom,\n TransactionType.tokenMethodSafeTransferFrom,\n TransactionType.tokenMethodIncreaseAllowance,\n ].find(\n (methodName) => methodName.toLowerCase() === (name as string).toLowerCase(),\n );\n\n if (tokenMethodName) {\n return { type: tokenMethodName, getCodeResponse };\n }\n\n return contractInteractionResult;\n}\n\n/**\n * Attempts to decode transaction data using ABIs for three different token standards: ERC20, ERC721, ERC1155.\n * The data will decode correctly if the transaction is an interaction with a contract that matches one of these\n * contract standards\n *\n * @param data - Encoded transaction data.\n * @returns A representation of an ethereum contract call.\n */\nfunction getMethodName(data?: string): string | undefined {\n if (!data || data.length < 10) {\n return undefined;\n }\n\n const fourByte = data.substring(0, 10).toLowerCase();\n\n for (const interfaceInstance of [\n ERC20Interface,\n ERC721Interface,\n ERC1155Interface,\n USDCInterface,\n ]) {\n try {\n return interfaceInstance.getFunction(fourByte).name;\n } catch {\n // Intentionally empty\n }\n }\n\n return undefined;\n}\n\n/**\n * Reads an Ethereum address and determines if it is a contract address.\n *\n * @param ethQuery - The Ethereum query object used to interact with the Ethereum blockchain.\n * @param address - The Ethereum address.\n * @returns An object containing the contract code and a boolean indicating if it is a contract address.\n */\nasync function readAddressAsContract(\n ethQuery: EthQuery,\n address?: string,\n): Promise<{\n contractCode: string | null;\n isContractAddress: boolean;\n error: Error | null;\n}> {\n let contractCode;\n try {\n contractCode = await query(ethQuery, 'getCode', [address]);\n } catch (error) {\n log(`Error reading contract code for address ${address}:`, error);\n return {\n contractCode: null,\n isContractAddress: false,\n error: error as Error,\n };\n }\n\n const isContractAddress =\n contractCode !== '0x' &&\n contractCode !== '0x0' &&\n !contractCode.startsWith(DELEGATION_PREFIX);\n return { contractCode, isContractAddress, error: null };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/transaction-controller",
|
|
3
|
-
"version": "59.0.0-preview-
|
|
3
|
+
"version": "59.0.0-preview-3f5f144a",
|
|
4
4
|
"description": "Stores transactions alongside their periodically updated statuses and manages interactions such as approval and cancellation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|