@metamask/transaction-controller 45.0.0 → 46.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 +32 -1
- package/dist/TransactionController.cjs +72 -67
- package/dist/TransactionController.cjs.map +1 -1
- package/dist/TransactionController.d.cts +10 -15
- package/dist/TransactionController.d.cts.map +1 -1
- package/dist/TransactionController.d.mts +10 -15
- package/dist/TransactionController.d.mts.map +1 -1
- package/dist/TransactionController.mjs +69 -64
- package/dist/TransactionController.mjs.map +1 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.cjs +4 -13
- package/dist/gas-flows/OracleLayer1GasFeeFlow.cjs.map +1 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.d.cts.map +1 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.d.mts.map +1 -1
- package/dist/gas-flows/OracleLayer1GasFeeFlow.mjs +4 -13
- package/dist/gas-flows/OracleLayer1GasFeeFlow.mjs.map +1 -1
- package/dist/{utils/resimulate.cjs → helpers/ResimulateHelper.cjs} +93 -4
- package/dist/helpers/ResimulateHelper.cjs.map +1 -0
- package/dist/{utils/resimulate.d.cts → helpers/ResimulateHelper.d.cts} +13 -1
- package/dist/helpers/ResimulateHelper.d.cts.map +1 -0
- package/dist/{utils/resimulate.d.mts → helpers/ResimulateHelper.d.mts} +13 -1
- package/dist/helpers/ResimulateHelper.d.mts.map +1 -0
- package/dist/{utils/resimulate.mjs → helpers/ResimulateHelper.mjs} +93 -5
- package/dist/helpers/ResimulateHelper.mjs.map +1 -0
- package/dist/index.cjs +3 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs +6 -37
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +51 -1
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +51 -1
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +6 -37
- package/dist/types.mjs.map +1 -1
- package/dist/utils/eip7702.cjs +87 -0
- package/dist/utils/eip7702.cjs.map +1 -0
- package/dist/utils/eip7702.d.cts +26 -0
- package/dist/utils/eip7702.d.cts.map +1 -0
- package/dist/utils/eip7702.d.mts +26 -0
- package/dist/utils/eip7702.d.mts.map +1 -0
- package/dist/utils/eip7702.mjs +83 -0
- package/dist/utils/eip7702.mjs.map +1 -0
- package/dist/utils/prepare.cjs +49 -0
- package/dist/utils/prepare.cjs.map +1 -0
- package/dist/utils/prepare.d.cts +21 -0
- package/dist/utils/prepare.d.cts.map +1 -0
- package/dist/utils/prepare.d.mts +21 -0
- package/dist/utils/prepare.d.mts.map +1 -0
- package/dist/utils/prepare.mjs +44 -0
- package/dist/utils/prepare.mjs.map +1 -0
- package/dist/utils/utils.cjs +3 -8
- package/dist/utils/utils.cjs.map +1 -1
- package/dist/utils/utils.d.cts.map +1 -1
- package/dist/utils/utils.d.mts.map +1 -1
- package/dist/utils/utils.mjs +3 -8
- package/dist/utils/utils.mjs.map +1 -1
- package/dist/utils/validation.cjs +91 -31
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.cts +13 -5
- package/dist/utils/validation.d.cts.map +1 -1
- package/dist/utils/validation.d.mts +13 -5
- package/dist/utils/validation.d.mts.map +1 -1
- package/dist/utils/validation.mjs +92 -32
- package/dist/utils/validation.mjs.map +1 -1
- package/package.json +9 -9
- package/dist/utils/resimulate.cjs.map +0 -1
- package/dist/utils/resimulate.d.cts.map +0 -1
- package/dist/utils/resimulate.d.mts.map +0 -1
- package/dist/utils/resimulate.mjs.map +0 -1
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.signAuthorizationList = void 0;
|
|
4
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
|
5
|
+
const utils_1 = require("@metamask/utils");
|
|
6
|
+
const logger_1 = require("../logger.cjs");
|
|
7
|
+
const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'eip-7702');
|
|
8
|
+
/**
|
|
9
|
+
* Sign an authorization list.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Options bag.
|
|
12
|
+
* @param options.authorizationList - The authorization list to sign.
|
|
13
|
+
* @param options.messenger - The controller messenger.
|
|
14
|
+
* @param options.transactionMeta - The transaction metadata.
|
|
15
|
+
* @returns The signed authorization list.
|
|
16
|
+
*/
|
|
17
|
+
async function signAuthorizationList({ authorizationList, messenger, transactionMeta, }) {
|
|
18
|
+
if (!authorizationList) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
const signedAuthorizationList = [];
|
|
22
|
+
let index = 0;
|
|
23
|
+
for (const authorization of authorizationList) {
|
|
24
|
+
const signedAuthorization = await signAuthorization(authorization, transactionMeta, messenger, index);
|
|
25
|
+
signedAuthorizationList.push(signedAuthorization);
|
|
26
|
+
index += 1;
|
|
27
|
+
}
|
|
28
|
+
return signedAuthorizationList;
|
|
29
|
+
}
|
|
30
|
+
exports.signAuthorizationList = signAuthorizationList;
|
|
31
|
+
/**
|
|
32
|
+
* Signs an authorization.
|
|
33
|
+
*
|
|
34
|
+
* @param authorization - The authorization to sign.
|
|
35
|
+
* @param transactionMeta - The associated transaction metadata.
|
|
36
|
+
* @param messenger - The messenger to use for signing.
|
|
37
|
+
* @param index - The index of the authorization in the list.
|
|
38
|
+
* @returns The signed authorization.
|
|
39
|
+
*/
|
|
40
|
+
async function signAuthorization(authorization, transactionMeta, messenger, index) {
|
|
41
|
+
const finalAuthorization = prepareAuthorization(authorization, transactionMeta, index);
|
|
42
|
+
const { address, chainId, nonce } = finalAuthorization;
|
|
43
|
+
const chainIdDecimal = parseInt(chainId, 16);
|
|
44
|
+
const nonceDecimal = parseInt(nonce, 16);
|
|
45
|
+
const signature = await messenger.call('KeyringController:signAuthorization', [chainIdDecimal, address, nonceDecimal]);
|
|
46
|
+
const r = signature.slice(0, 66);
|
|
47
|
+
const s = `0x${signature.slice(66, 130)}`;
|
|
48
|
+
const v = parseInt(signature.slice(130, 132), 16);
|
|
49
|
+
const yParity = v - 27 === 0 ? '0x' : '0x1';
|
|
50
|
+
const finalNonce = nonceDecimal === 0 ? '0x' : nonce;
|
|
51
|
+
const result = {
|
|
52
|
+
address,
|
|
53
|
+
chainId,
|
|
54
|
+
nonce: finalNonce,
|
|
55
|
+
r,
|
|
56
|
+
s,
|
|
57
|
+
yParity,
|
|
58
|
+
};
|
|
59
|
+
log('Signed authorization', result);
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Prepares an authorization for signing by populating the chainId and nonce.
|
|
64
|
+
*
|
|
65
|
+
* @param authorization - The authorization to prepare.
|
|
66
|
+
* @param transactionMeta - The associated transaction metadata.
|
|
67
|
+
* @param index - The index of the authorization in the list.
|
|
68
|
+
* @returns The prepared authorization.
|
|
69
|
+
*/
|
|
70
|
+
function prepareAuthorization(authorization, transactionMeta, index) {
|
|
71
|
+
const { chainId: existingChainId, nonce: existingNonce } = authorization;
|
|
72
|
+
const { txParams, chainId: transactionChainId } = transactionMeta;
|
|
73
|
+
const { nonce: transactionNonce } = txParams;
|
|
74
|
+
const chainId = existingChainId ?? transactionChainId;
|
|
75
|
+
let nonce = existingNonce;
|
|
76
|
+
if (nonce === undefined) {
|
|
77
|
+
nonce = (0, controller_utils_1.toHex)(parseInt(transactionNonce, 16) + 1 + index);
|
|
78
|
+
}
|
|
79
|
+
const result = {
|
|
80
|
+
...authorization,
|
|
81
|
+
chainId,
|
|
82
|
+
nonce,
|
|
83
|
+
};
|
|
84
|
+
log('Prepared authorization', result);
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=eip7702.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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AM,GAAG;QACb,GAAG,aAAa;QAChB,OAAO;QACP,KAAK;KACN,CAAC;IAEF,GAAG,CAAC,wBAAwB,EAAE,MAAM,CAAC,CAAC;IAEtC,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\nimport type {\n Authorization,\n AuthorizationList,\n TransactionMeta,\n} from '../types';\n\nexport type KeyringControllerAuthorization = [\n chainId: number,\n contractAddress: string,\n nonce: number,\n];\n\nexport type KeyringControllerSignAuthorization = {\n type: 'KeyringController:signAuthorization';\n handler: (authorization: KeyringControllerAuthorization) => Promise<string>;\n};\n\nconst log = createModuleLogger(projectLogger, 'eip-7702');\n\n/**\n * Sign an authorization list.\n *\n * @param options - Options bag.\n * @param options.authorizationList - The authorization list to sign.\n * @param options.messenger - The controller messenger.\n * @param options.transactionMeta - The transaction metadata.\n * @returns The signed authorization list.\n */\nexport async function signAuthorizationList({\n authorizationList,\n messenger,\n transactionMeta,\n}: {\n authorizationList?: AuthorizationList;\n messenger: TransactionControllerMessenger;\n transactionMeta: TransactionMeta;\n}): Promise<Required<AuthorizationList | undefined>> {\n if (!authorizationList) {\n return undefined;\n }\n\n const signedAuthorizationList: Required<AuthorizationList> = [];\n let index = 0;\n\n for (const authorization of authorizationList) {\n const signedAuthorization = await signAuthorization(\n authorization,\n transactionMeta,\n messenger,\n index,\n );\n\n signedAuthorizationList.push(signedAuthorization);\n index += 1;\n }\n\n return signedAuthorizationList;\n}\n\n/**\n * Signs an authorization.\n *\n * @param authorization - The authorization to sign.\n * @param transactionMeta - The associated transaction metadata.\n * @param messenger - The messenger to use for signing.\n * @param index - The index of the authorization in the list.\n * @returns The signed authorization.\n */\nasync function signAuthorization(\n authorization: Authorization,\n transactionMeta: TransactionMeta,\n messenger: TransactionControllerMessenger,\n index: number,\n): Promise<Required<Authorization>> {\n const finalAuthorization = prepareAuthorization(\n authorization,\n transactionMeta,\n index,\n );\n\n const { address, chainId, nonce } = finalAuthorization;\n const chainIdDecimal = parseInt(chainId, 16);\n const nonceDecimal = parseInt(nonce, 16);\n\n const signature = await messenger.call(\n 'KeyringController:signAuthorization',\n [chainIdDecimal, address, nonceDecimal],\n );\n\n const r = signature.slice(0, 66) as Hex;\n const s = `0x${signature.slice(66, 130)}` as Hex;\n const v = parseInt(signature.slice(130, 132), 16);\n const yParity = v - 27 === 0 ? '0x' : '0x1';\n const finalNonce = nonceDecimal === 0 ? '0x' : nonce;\n\n const result: Required<Authorization> = {\n address,\n chainId,\n nonce: finalNonce,\n r,\n s,\n yParity,\n };\n\n log('Signed authorization', result);\n\n return result;\n}\n\n/**\n * Prepares an authorization for signing by populating the chainId and nonce.\n *\n * @param authorization - The authorization to prepare.\n * @param transactionMeta - The associated transaction metadata.\n * @param index - The index of the authorization in the list.\n * @returns The prepared authorization.\n */\nfunction prepareAuthorization(\n authorization: Authorization,\n transactionMeta: TransactionMeta,\n index: number,\n): Authorization & { chainId: Hex; nonce: Hex } {\n const { chainId: existingChainId, nonce: existingNonce } = authorization;\n const { txParams, chainId: transactionChainId } = transactionMeta;\n const { nonce: transactionNonce } = txParams;\n\n const chainId = existingChainId ?? transactionChainId;\n let nonce = existingNonce;\n\n if (nonce === undefined) {\n nonce = toHex(parseInt(transactionNonce as string, 16) + 1 + index);\n }\n\n const result = {\n ...authorization,\n chainId,\n nonce,\n };\n\n log('Prepared authorization', result);\n\n return result;\n}\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { TransactionControllerMessenger } from "../TransactionController.cjs";
|
|
2
|
+
import type { AuthorizationList, TransactionMeta } from "../types.cjs";
|
|
3
|
+
export type KeyringControllerAuthorization = [
|
|
4
|
+
chainId: number,
|
|
5
|
+
contractAddress: string,
|
|
6
|
+
nonce: number
|
|
7
|
+
];
|
|
8
|
+
export type KeyringControllerSignAuthorization = {
|
|
9
|
+
type: 'KeyringController:signAuthorization';
|
|
10
|
+
handler: (authorization: KeyringControllerAuthorization) => Promise<string>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Sign an authorization list.
|
|
14
|
+
*
|
|
15
|
+
* @param options - Options bag.
|
|
16
|
+
* @param options.authorizationList - The authorization list to sign.
|
|
17
|
+
* @param options.messenger - The controller messenger.
|
|
18
|
+
* @param options.transactionMeta - The transaction metadata.
|
|
19
|
+
* @returns The signed authorization list.
|
|
20
|
+
*/
|
|
21
|
+
export declare function signAuthorizationList({ authorizationList, messenger, transactionMeta, }: {
|
|
22
|
+
authorizationList?: AuthorizationList;
|
|
23
|
+
messenger: TransactionControllerMessenger;
|
|
24
|
+
transactionMeta: TransactionMeta;
|
|
25
|
+
}): Promise<Required<AuthorizationList | undefined>>;
|
|
26
|
+
//# sourceMappingURL=eip7702.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eip7702.d.cts","sourceRoot":"","sources":["../../src/utils/eip7702.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EAEV,iBAAiB,EACjB,eAAe,EAChB,qBAAiB;AAElB,MAAM,MAAM,8BAA8B,GAAG;IAC3C,OAAO,EAAE,MAAM;IACf,eAAe,EAAE,MAAM;IACvB,KAAK,EAAE,MAAM;CACd,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,qCAAqC,CAAC;IAC5C,OAAO,EAAE,CAAC,aAAa,EAAE,8BAA8B,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAC7E,CAAC;AAIF;;;;;;;;GAQG;AACH,wBAAsB,qBAAqB,CAAC,EAC1C,iBAAiB,EACjB,SAAS,EACT,eAAe,GAChB,EAAE;IACD,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,SAAS,EAAE,8BAA8B,CAAC;IAC1C,eAAe,EAAE,eAAe,CAAC;CAClC,GAAG,OAAO,CAAC,QAAQ,CAAC,iBAAiB,GAAG,SAAS,CAAC,CAAC,CAqBnD"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { TransactionControllerMessenger } from "../TransactionController.mjs";
|
|
2
|
+
import type { AuthorizationList, TransactionMeta } from "../types.mjs";
|
|
3
|
+
export type KeyringControllerAuthorization = [
|
|
4
|
+
chainId: number,
|
|
5
|
+
contractAddress: string,
|
|
6
|
+
nonce: number
|
|
7
|
+
];
|
|
8
|
+
export type KeyringControllerSignAuthorization = {
|
|
9
|
+
type: 'KeyringController:signAuthorization';
|
|
10
|
+
handler: (authorization: KeyringControllerAuthorization) => Promise<string>;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Sign an authorization list.
|
|
14
|
+
*
|
|
15
|
+
* @param options - Options bag.
|
|
16
|
+
* @param options.authorizationList - The authorization list to sign.
|
|
17
|
+
* @param options.messenger - The controller messenger.
|
|
18
|
+
* @param options.transactionMeta - The transaction metadata.
|
|
19
|
+
* @returns The signed authorization list.
|
|
20
|
+
*/
|
|
21
|
+
export declare function signAuthorizationList({ authorizationList, messenger, transactionMeta, }: {
|
|
22
|
+
authorizationList?: AuthorizationList;
|
|
23
|
+
messenger: TransactionControllerMessenger;
|
|
24
|
+
transactionMeta: TransactionMeta;
|
|
25
|
+
}): Promise<Required<AuthorizationList | undefined>>;
|
|
26
|
+
//# sourceMappingURL=eip7702.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eip7702.d.mts","sourceRoot":"","sources":["../../src/utils/eip7702.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,8BAA8B,EAAE,qCAAiC;AAC/E,OAAO,KAAK,EAEV,iBAAiB,EACjB,eAAe,EAChB,qBAAiB;AAElB,MAAM,MAAM,8BAA8B,GAAG;IAC3C,OAAO,EAAE,MAAM;IACf,eAAe,EAAE,MAAM;IACvB,KAAK,EAAE,MAAM;CACd,CAAC;AAEF,MAAM,MAAM,kCAAkC,GAAG;IAC/C,IAAI,EAAE,qCAAqC,CAAC;IAC5C,OAAO,EAAE,CAAC,aAAa,EAAE,8BAA8B,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CAC7E,CAAC;AAIF;;;;;;;;GAQG;AACH,wBAAsB,qBAAqB,CAAC,EAC1C,iBAAiB,EACjB,SAAS,EACT,eAAe,GAChB,EAAE;IACD,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,SAAS,EAAE,8BAA8B,CAAC;IAC1C,eAAe,EAAE,eAAe,CAAC;CAClC,GAAG,OAAO,CAAC,QAAQ,CAAC,iBAAiB,GAAG,SAAS,CAAC,CAAC,CAqBnD"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { toHex } from "@metamask/controller-utils";
|
|
2
|
+
import { createModuleLogger } from "@metamask/utils";
|
|
3
|
+
import { projectLogger } from "../logger.mjs";
|
|
4
|
+
const log = createModuleLogger(projectLogger, 'eip-7702');
|
|
5
|
+
/**
|
|
6
|
+
* Sign an authorization list.
|
|
7
|
+
*
|
|
8
|
+
* @param options - Options bag.
|
|
9
|
+
* @param options.authorizationList - The authorization list to sign.
|
|
10
|
+
* @param options.messenger - The controller messenger.
|
|
11
|
+
* @param options.transactionMeta - The transaction metadata.
|
|
12
|
+
* @returns The signed authorization list.
|
|
13
|
+
*/
|
|
14
|
+
export async function signAuthorizationList({ authorizationList, messenger, transactionMeta, }) {
|
|
15
|
+
if (!authorizationList) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
const signedAuthorizationList = [];
|
|
19
|
+
let index = 0;
|
|
20
|
+
for (const authorization of authorizationList) {
|
|
21
|
+
const signedAuthorization = await signAuthorization(authorization, transactionMeta, messenger, index);
|
|
22
|
+
signedAuthorizationList.push(signedAuthorization);
|
|
23
|
+
index += 1;
|
|
24
|
+
}
|
|
25
|
+
return signedAuthorizationList;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Signs an authorization.
|
|
29
|
+
*
|
|
30
|
+
* @param authorization - The authorization to sign.
|
|
31
|
+
* @param transactionMeta - The associated transaction metadata.
|
|
32
|
+
* @param messenger - The messenger to use for signing.
|
|
33
|
+
* @param index - The index of the authorization in the list.
|
|
34
|
+
* @returns The signed authorization.
|
|
35
|
+
*/
|
|
36
|
+
async function signAuthorization(authorization, transactionMeta, messenger, index) {
|
|
37
|
+
const finalAuthorization = prepareAuthorization(authorization, transactionMeta, index);
|
|
38
|
+
const { address, chainId, nonce } = finalAuthorization;
|
|
39
|
+
const chainIdDecimal = parseInt(chainId, 16);
|
|
40
|
+
const nonceDecimal = parseInt(nonce, 16);
|
|
41
|
+
const signature = await messenger.call('KeyringController:signAuthorization', [chainIdDecimal, address, nonceDecimal]);
|
|
42
|
+
const r = signature.slice(0, 66);
|
|
43
|
+
const s = `0x${signature.slice(66, 130)}`;
|
|
44
|
+
const v = parseInt(signature.slice(130, 132), 16);
|
|
45
|
+
const yParity = v - 27 === 0 ? '0x' : '0x1';
|
|
46
|
+
const finalNonce = nonceDecimal === 0 ? '0x' : nonce;
|
|
47
|
+
const result = {
|
|
48
|
+
address,
|
|
49
|
+
chainId,
|
|
50
|
+
nonce: finalNonce,
|
|
51
|
+
r,
|
|
52
|
+
s,
|
|
53
|
+
yParity,
|
|
54
|
+
};
|
|
55
|
+
log('Signed authorization', result);
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Prepares an authorization for signing by populating the chainId and nonce.
|
|
60
|
+
*
|
|
61
|
+
* @param authorization - The authorization to prepare.
|
|
62
|
+
* @param transactionMeta - The associated transaction metadata.
|
|
63
|
+
* @param index - The index of the authorization in the list.
|
|
64
|
+
* @returns The prepared authorization.
|
|
65
|
+
*/
|
|
66
|
+
function prepareAuthorization(authorization, transactionMeta, index) {
|
|
67
|
+
const { chainId: existingChainId, nonce: existingNonce } = authorization;
|
|
68
|
+
const { txParams, chainId: transactionChainId } = transactionMeta;
|
|
69
|
+
const { nonce: transactionNonce } = txParams;
|
|
70
|
+
const chainId = existingChainId ?? transactionChainId;
|
|
71
|
+
let nonce = existingNonce;
|
|
72
|
+
if (nonce === undefined) {
|
|
73
|
+
nonce = toHex(parseInt(transactionNonce, 16) + 1 + index);
|
|
74
|
+
}
|
|
75
|
+
const result = {
|
|
76
|
+
...authorization,
|
|
77
|
+
chainId,
|
|
78
|
+
nonce,
|
|
79
|
+
};
|
|
80
|
+
log('Prepared authorization', result);
|
|
81
|
+
return result;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=eip7702.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eip7702.mjs","sourceRoot":"","sources":["../../src/utils/eip7702.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AACnD,OAAO,EAAE,kBAAkB,EAAY,wBAAwB;AAE/D,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAmB1C,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,UAAU,CAAC,CAAC;AAE1D;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,EAC1C,iBAAiB,EACjB,SAAS,EACT,eAAe,GAKhB;IACC,IAAI,CAAC,iBAAiB,EAAE;QACtB,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,uBAAuB,GAAgC,EAAE,CAAC;IAChE,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,KAAK,MAAM,aAAa,IAAI,iBAAiB,EAAE;QAC7C,MAAM,mBAAmB,GAAG,MAAM,iBAAiB,CACjD,aAAa,EACb,eAAe,EACf,SAAS,EACT,KAAK,CACN,CAAC;QAEF,uBAAuB,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;QAClD,KAAK,IAAI,CAAC,CAAC;KACZ;IAED,OAAO,uBAAuB,CAAC;AACjC,CAAC;AAED;;;;;;;;GAQG;AACH,KAAK,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AAgC,EAChC,KAAa;IAEb,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,KAAK,EAAE,aAAa,EAAE,GAAG,aAAa,CAAC;IACzE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,kBAAkB,EAAE,GAAG,eAAe,CAAC;IAClE,MAAM,EAAE,KAAK,EAAE,gBAAgB,EAAE,GAAG,QAAQ,CAAC;IAE7C,MAAM,OAAO,GAAG,eAAe,IAAI,kBAAkB,CAAC;IACtD,IAAI,KAAK,GAAG,aAAa,CAAC;IAE1B,IAAI,KAAK,KAAK,SAAS,EAAE;QACvB,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,gBAA0B,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC;KACrE;IAED,MAAM,MAAM,GAAG;QACb,GAAG,aAAa;QAChB,OAAO;QACP,KAAK;KACN,CAAC;IAEF,GAAG,CAAC,wBAAwB,EAAE,MAAM,CAAC,CAAC;IAEtC,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport { createModuleLogger, type Hex } from '@metamask/utils';\n\nimport { projectLogger } from '../logger';\nimport type { TransactionControllerMessenger } from '../TransactionController';\nimport type {\n Authorization,\n AuthorizationList,\n TransactionMeta,\n} from '../types';\n\nexport type KeyringControllerAuthorization = [\n chainId: number,\n contractAddress: string,\n nonce: number,\n];\n\nexport type KeyringControllerSignAuthorization = {\n type: 'KeyringController:signAuthorization';\n handler: (authorization: KeyringControllerAuthorization) => Promise<string>;\n};\n\nconst log = createModuleLogger(projectLogger, 'eip-7702');\n\n/**\n * Sign an authorization list.\n *\n * @param options - Options bag.\n * @param options.authorizationList - The authorization list to sign.\n * @param options.messenger - The controller messenger.\n * @param options.transactionMeta - The transaction metadata.\n * @returns The signed authorization list.\n */\nexport async function signAuthorizationList({\n authorizationList,\n messenger,\n transactionMeta,\n}: {\n authorizationList?: AuthorizationList;\n messenger: TransactionControllerMessenger;\n transactionMeta: TransactionMeta;\n}): Promise<Required<AuthorizationList | undefined>> {\n if (!authorizationList) {\n return undefined;\n }\n\n const signedAuthorizationList: Required<AuthorizationList> = [];\n let index = 0;\n\n for (const authorization of authorizationList) {\n const signedAuthorization = await signAuthorization(\n authorization,\n transactionMeta,\n messenger,\n index,\n );\n\n signedAuthorizationList.push(signedAuthorization);\n index += 1;\n }\n\n return signedAuthorizationList;\n}\n\n/**\n * Signs an authorization.\n *\n * @param authorization - The authorization to sign.\n * @param transactionMeta - The associated transaction metadata.\n * @param messenger - The messenger to use for signing.\n * @param index - The index of the authorization in the list.\n * @returns The signed authorization.\n */\nasync function signAuthorization(\n authorization: Authorization,\n transactionMeta: TransactionMeta,\n messenger: TransactionControllerMessenger,\n index: number,\n): Promise<Required<Authorization>> {\n const finalAuthorization = prepareAuthorization(\n authorization,\n transactionMeta,\n index,\n );\n\n const { address, chainId, nonce } = finalAuthorization;\n const chainIdDecimal = parseInt(chainId, 16);\n const nonceDecimal = parseInt(nonce, 16);\n\n const signature = await messenger.call(\n 'KeyringController:signAuthorization',\n [chainIdDecimal, address, nonceDecimal],\n );\n\n const r = signature.slice(0, 66) as Hex;\n const s = `0x${signature.slice(66, 130)}` as Hex;\n const v = parseInt(signature.slice(130, 132), 16);\n const yParity = v - 27 === 0 ? '0x' : '0x1';\n const finalNonce = nonceDecimal === 0 ? '0x' : nonce;\n\n const result: Required<Authorization> = {\n address,\n chainId,\n nonce: finalNonce,\n r,\n s,\n yParity,\n };\n\n log('Signed authorization', result);\n\n return result;\n}\n\n/**\n * Prepares an authorization for signing by populating the chainId and nonce.\n *\n * @param authorization - The authorization to prepare.\n * @param transactionMeta - The associated transaction metadata.\n * @param index - The index of the authorization in the list.\n * @returns The prepared authorization.\n */\nfunction prepareAuthorization(\n authorization: Authorization,\n transactionMeta: TransactionMeta,\n index: number,\n): Authorization & { chainId: Hex; nonce: Hex } {\n const { chainId: existingChainId, nonce: existingNonce } = authorization;\n const { txParams, chainId: transactionChainId } = transactionMeta;\n const { nonce: transactionNonce } = txParams;\n\n const chainId = existingChainId ?? transactionChainId;\n let nonce = existingNonce;\n\n if (nonce === undefined) {\n nonce = toHex(parseInt(transactionNonce as string, 16) + 1 + index);\n }\n\n const result = {\n ...authorization,\n chainId,\n nonce,\n };\n\n log('Prepared authorization', result);\n\n return result;\n}\n"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serializeTransaction = exports.prepareTransaction = exports.HARDFORK = void 0;
|
|
4
|
+
const common_1 = require("@ethereumjs/common");
|
|
5
|
+
const tx_1 = require("@ethereumjs/tx");
|
|
6
|
+
const utils_1 = require("@metamask/utils");
|
|
7
|
+
exports.HARDFORK = common_1.Hardfork.Prague;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an `etheruemjs/tx` transaction object from the raw transaction parameters.
|
|
10
|
+
*
|
|
11
|
+
* @param chainId - Chain ID of the transaction.
|
|
12
|
+
* @param txParams - Transaction parameters.
|
|
13
|
+
* @returns The transaction object.
|
|
14
|
+
*/
|
|
15
|
+
function prepareTransaction(chainId, txParams) {
|
|
16
|
+
// Does not allow `gasPrice` on type 4 transactions.
|
|
17
|
+
const data = txParams;
|
|
18
|
+
return tx_1.TransactionFactory.fromTxData(data, {
|
|
19
|
+
freeze: false,
|
|
20
|
+
common: getCommonConfiguration(chainId),
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
exports.prepareTransaction = prepareTransaction;
|
|
24
|
+
/**
|
|
25
|
+
* Serializes a transaction object into a hex string.
|
|
26
|
+
*
|
|
27
|
+
* @param transaction - The transaction object.
|
|
28
|
+
* @returns The prefixed hex string.
|
|
29
|
+
*/
|
|
30
|
+
function serializeTransaction(transaction) {
|
|
31
|
+
return (0, utils_1.bytesToHex)(transaction.serialize());
|
|
32
|
+
}
|
|
33
|
+
exports.serializeTransaction = serializeTransaction;
|
|
34
|
+
/**
|
|
35
|
+
* Generates the configuration used to prepare transactions.
|
|
36
|
+
*
|
|
37
|
+
* @param chainId - Chain ID.
|
|
38
|
+
* @returns The common configuration.
|
|
39
|
+
*/
|
|
40
|
+
function getCommonConfiguration(chainId) {
|
|
41
|
+
const customChainParams = {
|
|
42
|
+
chainId: parseInt(chainId, 16),
|
|
43
|
+
defaultHardfork: exports.HARDFORK,
|
|
44
|
+
};
|
|
45
|
+
return common_1.Common.custom(customChainParams, {
|
|
46
|
+
eips: [7702],
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=prepare.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prepare.cjs","sourceRoot":"","sources":["../../src/utils/prepare.ts"],"names":[],"mappings":";;;AACA,+CAAsD;AAEtD,uCAAoD;AACpD,2CAA6C;AAKhC,QAAA,QAAQ,GAAG,iBAAQ,CAAC,MAAM,CAAC;AAExC;;;;;;GAMG;AACH,SAAgB,kBAAkB,CAChC,OAAY,EACZ,QAA2B;IAE3B,oDAAoD;IACpD,MAAM,IAAI,GAAG,QAAuB,CAAC;IAErC,OAAO,uBAAkB,CAAC,UAAU,CAAC,IAAI,EAAE;QACzC,MAAM,EAAE,KAAK;QACb,MAAM,EAAE,sBAAsB,CAAC,OAAO,CAAC;KACxC,CAAC,CAAC;AACL,CAAC;AAXD,gDAWC;AAED;;;;;GAKG;AACH,SAAgB,oBAAoB,CAAC,WAA6B;IAChE,OAAO,IAAA,kBAAU,EAAC,WAAW,CAAC,SAAS,EAAE,CAAC,CAAC;AAC7C,CAAC;AAFD,oDAEC;AAED;;;;;GAKG;AACH,SAAS,sBAAsB,CAAC,OAAY;IAC1C,MAAM,iBAAiB,GAAyB;QAC9C,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;QAC9B,eAAe,EAAE,gBAAQ;KAC1B,CAAC;IAEF,OAAO,eAAM,CAAC,MAAM,CAAC,iBAAiB,EAAE;QACtC,IAAI,EAAE,CAAC,IAAI,CAAC;KACb,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { ChainConfig } from '@ethereumjs/common';\nimport { Common, Hardfork } from '@ethereumjs/common';\nimport type { TypedTransaction, TypedTxData } from '@ethereumjs/tx';\nimport { TransactionFactory } from '@ethereumjs/tx';\nimport { bytesToHex } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type { TransactionParams } from '../types';\n\nexport const HARDFORK = Hardfork.Prague;\n\n/**\n * Creates an `etheruemjs/tx` transaction object from the raw transaction parameters.\n *\n * @param chainId - Chain ID of the transaction.\n * @param txParams - Transaction parameters.\n * @returns The transaction object.\n */\nexport function prepareTransaction(\n chainId: Hex,\n txParams: TransactionParams,\n): TypedTransaction {\n // Does not allow `gasPrice` on type 4 transactions.\n const data = txParams as TypedTxData;\n\n return TransactionFactory.fromTxData(data, {\n freeze: false,\n common: getCommonConfiguration(chainId),\n });\n}\n\n/**\n * Serializes a transaction object into a hex string.\n *\n * @param transaction - The transaction object.\n * @returns The prefixed hex string.\n */\nexport function serializeTransaction(transaction: TypedTransaction) {\n return bytesToHex(transaction.serialize());\n}\n\n/**\n * Generates the configuration used to prepare transactions.\n *\n * @param chainId - Chain ID.\n * @returns The common configuration.\n */\nfunction getCommonConfiguration(chainId: Hex): Common {\n const customChainParams: Partial<ChainConfig> = {\n chainId: parseInt(chainId, 16),\n defaultHardfork: HARDFORK,\n };\n\n return Common.custom(customChainParams, {\n eips: [7702],\n });\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Hardfork } from "@ethereumjs/common";
|
|
2
|
+
import type { TypedTransaction } from "@ethereumjs/tx";
|
|
3
|
+
import type { Hex } from "@metamask/utils";
|
|
4
|
+
import type { TransactionParams } from "../types.cjs";
|
|
5
|
+
export declare const HARDFORK = Hardfork.Prague;
|
|
6
|
+
/**
|
|
7
|
+
* Creates an `etheruemjs/tx` transaction object from the raw transaction parameters.
|
|
8
|
+
*
|
|
9
|
+
* @param chainId - Chain ID of the transaction.
|
|
10
|
+
* @param txParams - Transaction parameters.
|
|
11
|
+
* @returns The transaction object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function prepareTransaction(chainId: Hex, txParams: TransactionParams): TypedTransaction;
|
|
14
|
+
/**
|
|
15
|
+
* Serializes a transaction object into a hex string.
|
|
16
|
+
*
|
|
17
|
+
* @param transaction - The transaction object.
|
|
18
|
+
* @returns The prefixed hex string.
|
|
19
|
+
*/
|
|
20
|
+
export declare function serializeTransaction(transaction: TypedTransaction): `0x${string}`;
|
|
21
|
+
//# sourceMappingURL=prepare.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prepare.d.cts","sourceRoot":"","sources":["../../src/utils/prepare.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,QAAQ,EAAE,2BAA2B;AACtD,OAAO,KAAK,EAAE,gBAAgB,EAAe,uBAAuB;AAGpE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAElD,eAAO,MAAM,QAAQ,kBAAkB,CAAC;AAExC;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,GAAG,EACZ,QAAQ,EAAE,iBAAiB,GAC1B,gBAAgB,CAQlB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,gBAAgB,iBAEjE"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Hardfork } from "@ethereumjs/common";
|
|
2
|
+
import type { TypedTransaction } from "@ethereumjs/tx";
|
|
3
|
+
import type { Hex } from "@metamask/utils";
|
|
4
|
+
import type { TransactionParams } from "../types.mjs";
|
|
5
|
+
export declare const HARDFORK = Hardfork.Prague;
|
|
6
|
+
/**
|
|
7
|
+
* Creates an `etheruemjs/tx` transaction object from the raw transaction parameters.
|
|
8
|
+
*
|
|
9
|
+
* @param chainId - Chain ID of the transaction.
|
|
10
|
+
* @param txParams - Transaction parameters.
|
|
11
|
+
* @returns The transaction object.
|
|
12
|
+
*/
|
|
13
|
+
export declare function prepareTransaction(chainId: Hex, txParams: TransactionParams): TypedTransaction;
|
|
14
|
+
/**
|
|
15
|
+
* Serializes a transaction object into a hex string.
|
|
16
|
+
*
|
|
17
|
+
* @param transaction - The transaction object.
|
|
18
|
+
* @returns The prefixed hex string.
|
|
19
|
+
*/
|
|
20
|
+
export declare function serializeTransaction(transaction: TypedTransaction): `0x${string}`;
|
|
21
|
+
//# sourceMappingURL=prepare.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prepare.d.mts","sourceRoot":"","sources":["../../src/utils/prepare.ts"],"names":[],"mappings":"AACA,OAAO,EAAU,QAAQ,EAAE,2BAA2B;AACtD,OAAO,KAAK,EAAE,gBAAgB,EAAe,uBAAuB;AAGpE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAE3C,OAAO,KAAK,EAAE,iBAAiB,EAAE,qBAAiB;AAElD,eAAO,MAAM,QAAQ,kBAAkB,CAAC;AAExC;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,GAAG,EACZ,QAAQ,EAAE,iBAAiB,GAC1B,gBAAgB,CAQlB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,gBAAgB,iBAEjE"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Common, Hardfork } from "@ethereumjs/common";
|
|
2
|
+
import { TransactionFactory } from "@ethereumjs/tx";
|
|
3
|
+
import { bytesToHex } from "@metamask/utils";
|
|
4
|
+
export const HARDFORK = Hardfork.Prague;
|
|
5
|
+
/**
|
|
6
|
+
* Creates an `etheruemjs/tx` transaction object from the raw transaction parameters.
|
|
7
|
+
*
|
|
8
|
+
* @param chainId - Chain ID of the transaction.
|
|
9
|
+
* @param txParams - Transaction parameters.
|
|
10
|
+
* @returns The transaction object.
|
|
11
|
+
*/
|
|
12
|
+
export function prepareTransaction(chainId, txParams) {
|
|
13
|
+
// Does not allow `gasPrice` on type 4 transactions.
|
|
14
|
+
const data = txParams;
|
|
15
|
+
return TransactionFactory.fromTxData(data, {
|
|
16
|
+
freeze: false,
|
|
17
|
+
common: getCommonConfiguration(chainId),
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Serializes a transaction object into a hex string.
|
|
22
|
+
*
|
|
23
|
+
* @param transaction - The transaction object.
|
|
24
|
+
* @returns The prefixed hex string.
|
|
25
|
+
*/
|
|
26
|
+
export function serializeTransaction(transaction) {
|
|
27
|
+
return bytesToHex(transaction.serialize());
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Generates the configuration used to prepare transactions.
|
|
31
|
+
*
|
|
32
|
+
* @param chainId - Chain ID.
|
|
33
|
+
* @returns The common configuration.
|
|
34
|
+
*/
|
|
35
|
+
function getCommonConfiguration(chainId) {
|
|
36
|
+
const customChainParams = {
|
|
37
|
+
chainId: parseInt(chainId, 16),
|
|
38
|
+
defaultHardfork: HARDFORK,
|
|
39
|
+
};
|
|
40
|
+
return Common.custom(customChainParams, {
|
|
41
|
+
eips: [7702],
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=prepare.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prepare.mjs","sourceRoot":"","sources":["../../src/utils/prepare.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,2BAA2B;AAEtD,OAAO,EAAE,kBAAkB,EAAE,uBAAuB;AACpD,OAAO,EAAE,UAAU,EAAE,wBAAwB;AAK7C,MAAM,CAAC,MAAM,QAAQ,GAAG,QAAQ,CAAC,MAAM,CAAC;AAExC;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAChC,OAAY,EACZ,QAA2B;IAE3B,oDAAoD;IACpD,MAAM,IAAI,GAAG,QAAuB,CAAC;IAErC,OAAO,kBAAkB,CAAC,UAAU,CAAC,IAAI,EAAE;QACzC,MAAM,EAAE,KAAK;QACb,MAAM,EAAE,sBAAsB,CAAC,OAAO,CAAC;KACxC,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,WAA6B;IAChE,OAAO,UAAU,CAAC,WAAW,CAAC,SAAS,EAAE,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;GAKG;AACH,SAAS,sBAAsB,CAAC,OAAY;IAC1C,MAAM,iBAAiB,GAAyB;QAC9C,OAAO,EAAE,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;QAC9B,eAAe,EAAE,QAAQ;KAC1B,CAAC;IAEF,OAAO,MAAM,CAAC,MAAM,CAAC,iBAAiB,EAAE;QACtC,IAAI,EAAE,CAAC,IAAI,CAAC;KACb,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { ChainConfig } from '@ethereumjs/common';\nimport { Common, Hardfork } from '@ethereumjs/common';\nimport type { TypedTransaction, TypedTxData } from '@ethereumjs/tx';\nimport { TransactionFactory } from '@ethereumjs/tx';\nimport { bytesToHex } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\n\nimport type { TransactionParams } from '../types';\n\nexport const HARDFORK = Hardfork.Prague;\n\n/**\n * Creates an `etheruemjs/tx` transaction object from the raw transaction parameters.\n *\n * @param chainId - Chain ID of the transaction.\n * @param txParams - Transaction parameters.\n * @returns The transaction object.\n */\nexport function prepareTransaction(\n chainId: Hex,\n txParams: TransactionParams,\n): TypedTransaction {\n // Does not allow `gasPrice` on type 4 transactions.\n const data = txParams as TypedTxData;\n\n return TransactionFactory.fromTxData(data, {\n freeze: false,\n common: getCommonConfiguration(chainId),\n });\n}\n\n/**\n * Serializes a transaction object into a hex string.\n *\n * @param transaction - The transaction object.\n * @returns The prefixed hex string.\n */\nexport function serializeTransaction(transaction: TypedTransaction) {\n return bytesToHex(transaction.serialize());\n}\n\n/**\n * Generates the configuration used to prepare transactions.\n *\n * @param chainId - Chain ID.\n * @returns The common configuration.\n */\nfunction getCommonConfiguration(chainId: Hex): Common {\n const customChainParams: Partial<ChainConfig> = {\n chainId: parseInt(chainId, 16),\n defaultHardfork: HARDFORK,\n };\n\n return Common.custom(customChainParams, {\n eips: [7702],\n });\n}\n"]}
|
package/dist/utils/utils.cjs
CHANGED
|
@@ -11,6 +11,7 @@ exports.ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';
|
|
|
11
11
|
// TODO: Replace `any` with type
|
|
12
12
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
13
|
const NORMALIZERS = {
|
|
14
|
+
authorizationList: (authorizationList) => authorizationList,
|
|
14
15
|
data: (data) => (0, utils_1.add0x)(padHexToEvenLength(data)),
|
|
15
16
|
from: (from) => (0, utils_1.add0x)(from).toLowerCase(),
|
|
16
17
|
gas: (gas) => (0, utils_1.add0x)(gas),
|
|
@@ -62,10 +63,7 @@ const validateGasValues = (gasValues) => {
|
|
|
62
63
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
63
64
|
const value = gasValues[key];
|
|
64
65
|
if (typeof value !== 'string' || !(0, utils_1.isStrictHexString)(value)) {
|
|
65
|
-
throw new TypeError(
|
|
66
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
67
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
68
|
-
`expected hex string for ${key} but received: ${value}`);
|
|
66
|
+
throw new TypeError(`expected hex string for ${key} but received: ${value}`);
|
|
69
67
|
}
|
|
70
68
|
});
|
|
71
69
|
};
|
|
@@ -79,10 +77,7 @@ exports.validateGasValues = validateGasValues;
|
|
|
79
77
|
*/
|
|
80
78
|
function validateIfTransactionUnapproved(transactionMeta, fnName) {
|
|
81
79
|
if (transactionMeta?.status !== types_1.TransactionStatus.unapproved) {
|
|
82
|
-
throw new Error(
|
|
83
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
84
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
85
|
-
`TransactionsController: Can only call ${fnName} on an unapproved transaction.\n Current tx status: ${transactionMeta?.status}`);
|
|
80
|
+
throw new Error(`TransactionsController: Can only call ${fnName} on an unapproved transaction.\n Current tx status: ${transactionMeta?.status}`);
|
|
86
81
|
}
|
|
87
82
|
}
|
|
88
83
|
exports.validateIfTransactionUnapproved = validateIfTransactionUnapproved;
|
package/dist/utils/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":";;;;;;AAAA,2CAIyB;AAEzB,kDAAuB;AAEvB,wCAA6C;AAShC,QAAA,kBAAkB,GAAG,kCAAkC,CAAC;AAErE,gCAAgC;AAChC,8DAA8D;AAC9D,MAAM,WAAW,GAAgD;IAC/D,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACvD,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,IAAI,CAAC,CAAC,WAAW,EAAE;IACjD,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,GAAG,CAAC;IAChC,QAAQ,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,GAAG,CAAC;IACrC,QAAQ,EAAE,CAAC,QAAgB,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,QAAQ,CAAC;IAC/C,KAAK,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,KAAK,CAAC;IACtC,EAAE,EAAE,CAAC,EAAU,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,EAAE,CAAC,CAAC,WAAW,EAAE;IAC3C,KAAK,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC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C,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,eAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,MAAM,sBAAsB,GAAG,aAAa,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAClE,MAAM,iBAAiB,GAAG,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAExD,MAAM,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;IAEjE,IAAI,UAAU,CAAC,MAAM,EAAE,EAAE;QACvB,OAAO,CAAC,CAAC;KACV;IAED,IAAI,sBAAsB,CAAC,MAAM,EAAE,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,EAAE;QAClE,OAAO,GAAG,CAAC;KACZ;IAED,OAAO,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;AAC3E,CAAC;AAhBD,kDAgBC","sourcesContent":["import {\n add0x,\n getKnownPropertyNames,\n isStrictHexString,\n} from '@metamask/utils';\nimport type { Json } from '@metamask/utils';\nimport BN from 'bn.js';\n\nimport { TransactionStatus } from '../types';\nimport type {\n TransactionParams,\n TransactionMeta,\n TransactionError,\n GasPriceValue,\n FeeMarketEIP1559Values,\n} from '../types';\n\nexport const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';\n\n// TODO: Replace `any` with type\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst NORMALIZERS: { [param in keyof TransactionParams]: any } = {\n data: (data: string) => add0x(padHexToEvenLength(data)),\n from: (from: string) => add0x(from).toLowerCase(),\n gas: (gas: string) => add0x(gas),\n gasLimit: (gas: string) => add0x(gas),\n gasPrice: (gasPrice: string) => add0x(gasPrice),\n nonce: (nonce: string) => add0x(nonce),\n to: (to: string) => add0x(to).toLowerCase(),\n value: (value: string) => add0x(value),\n maxFeePerGas: (maxFeePerGas: string) => add0x(maxFeePerGas),\n maxPriorityFeePerGas: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n estimatedBaseFee: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n type: (type: string) => add0x(type),\n};\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param txParams - The transaction params to normalize.\n * @returns Normalized transaction params.\n */\nexport function normalizeTransactionParams(txParams: TransactionParams) {\n const normalizedTxParams: TransactionParams = { from: '' };\n\n for (const key of getKnownPropertyNames(NORMALIZERS)) {\n if (txParams[key]) {\n normalizedTxParams[key] = NORMALIZERS[key](txParams[key]);\n }\n }\n\n if (!normalizedTxParams.value) {\n normalizedTxParams.value = '0x0';\n }\n\n return normalizedTxParams;\n}\n\n/**\n * Checks if a transaction is EIP-1559 by checking for the existence of\n * maxFeePerGas and maxPriorityFeePerGas within its parameters.\n *\n * @param txParams - Transaction params object to add.\n * @returns Boolean that is true if the transaction is EIP-1559 (has maxFeePerGas and maxPriorityFeePerGas), otherwise returns false.\n */\nexport function isEIP1559Transaction(txParams: TransactionParams): boolean {\n const hasOwnProp = (obj: TransactionParams, key: string) =>\n Object.prototype.hasOwnProperty.call(obj, key);\n return (\n hasOwnProp(txParams, 'maxFeePerGas') &&\n hasOwnProp(txParams, 'maxPriorityFeePerGas')\n );\n}\n\nexport const validateGasValues = (\n gasValues: GasPriceValue | FeeMarketEIP1559Values,\n) => {\n Object.keys(gasValues).forEach((key) => {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const value = (gasValues as any)[key];\n if (typeof value !== 'string' || !isStrictHexString(value)) {\n throw new TypeError(\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `expected hex string for ${key} but received: ${value}`,\n );\n }\n });\n};\n\n/**\n * Validates that a transaction is unapproved.\n * Throws if the transaction is not unapproved.\n *\n * @param transactionMeta - The transaction metadata to check.\n * @param fnName - The name of the function calling this helper.\n */\nexport function validateIfTransactionUnapproved(\n transactionMeta: TransactionMeta | undefined,\n fnName: string,\n) {\n if (transactionMeta?.status !== TransactionStatus.unapproved) {\n throw new Error(\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `TransactionsController: Can only call ${fnName} on an unapproved transaction.\\n Current tx status: ${transactionMeta?.status}`,\n );\n }\n}\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param error - The error to be normalize.\n * @returns Normalized transaction error.\n */\nexport function normalizeTxError(\n error: Error & { code?: string; value?: unknown },\n): TransactionError {\n return {\n name: error.name,\n message: error.message,\n stack: error.stack,\n code: error.code,\n rpc: isJsonCompatible(error.value) ? error.value : undefined,\n };\n}\n\n/**\n * Normalize an object containing gas fee values.\n *\n * @param gasFeeValues - An object containing gas fee values.\n * @returns An object containing normalized gas fee values.\n */\nexport function normalizeGasFeeValues(\n gasFeeValues: GasPriceValue | FeeMarketEIP1559Values,\n): GasPriceValue | FeeMarketEIP1559Values {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const normalize = (value: any) =>\n typeof value === 'string' ? add0x(value) : value;\n\n if ('gasPrice' in gasFeeValues) {\n return {\n gasPrice: normalize(gasFeeValues.gasPrice),\n };\n }\n\n return {\n maxFeePerGas: normalize(gasFeeValues.maxFeePerGas),\n maxPriorityFeePerGas: normalize(gasFeeValues.maxPriorityFeePerGas),\n };\n}\n\n/**\n * Determines whether the given value can be encoded as JSON.\n *\n * @param value - The value.\n * @returns True if the value is JSON-encodable, false if not.\n */\nfunction isJsonCompatible(value: unknown): value is Json {\n try {\n JSON.parse(JSON.stringify(value));\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Ensure a hex string is of even length by adding a leading 0 if necessary.\n * Any existing `0x` prefix is preserved but is not added if missing.\n *\n * @param hex - The hex string to ensure is even.\n * @returns The hex string with an even length.\n */\nexport function padHexToEvenLength(hex: string) {\n const prefix = hex.toLowerCase().startsWith('0x') ? hex.slice(0, 2) : '';\n const data = prefix ? hex.slice(2) : hex;\n const evenData = data.length % 2 === 0 ? data : `0${data}`;\n\n return prefix + evenData;\n}\n\n/**\n * Calculate the absolute percentage change between two values.\n *\n * @param originalValue - The first value.\n * @param newValue - The second value.\n * @returns The percentage change from the first value to the second value.\n * If the original value is zero and the new value is not, returns 100.\n */\nexport function getPercentageChange(originalValue: BN, newValue: BN): number {\n const precisionFactor = new BN(10).pow(new BN(18));\n const originalValuePrecision = originalValue.mul(precisionFactor);\n const newValuePrecision = newValue.mul(precisionFactor);\n\n const difference = newValuePrecision.sub(originalValuePrecision);\n\n if (difference.isZero()) {\n return 0;\n }\n\n if (originalValuePrecision.isZero() && !newValuePrecision.isZero()) {\n return 100;\n }\n\n return difference.muln(100).div(originalValuePrecision).abs().toNumber();\n}\n"]}
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":";;;;;;AACA,2CAIyB;AAEzB,kDAAuB;AAEvB,wCAA6C;AAShC,QAAA,kBAAkB,GAAG,kCAAkC,CAAC;AAErE,gCAAgC;AAChC,8DAA8D;AAC9D,MAAM,WAAW,GAAgD;IAC/D,iBAAiB,EAAE,CAAC,iBAAqC,EAAE,EAAE,CAC3D,iBAAiB;IACnB,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACvD,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,IAAI,CAAC,CAAC,WAAW,EAAE;IACjD,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,GAAG,CAAC;IAChC,QAAQ,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC,GAAG,CAAC;IACrC,QAAQ,EAAE,CAAC,QAAgB,EAAE,EAAE,CAAC,IAAA,aAAK,EAAC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C,IAAI,IAAI,EAAE,CAAC;IAE3D,OAAO,MAAM,GAAG,QAAQ,CAAC;AAC3B,CAAC;AAND,gDAMC;AAED;;;;;;;GAOG;AACH,SAAgB,mBAAmB,CAAC,aAAiB,EAAE,QAAY;IACjE,MAAM,eAAe,GAAG,IAAI,eAAE,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,eAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,MAAM,sBAAsB,GAAG,aAAa,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAClE,MAAM,iBAAiB,GAAG,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAExD,MAAM,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;IAEjE,IAAI,UAAU,CAAC,MAAM,EAAE,EAAE;QACvB,OAAO,CAAC,CAAC;KACV;IAED,IAAI,sBAAsB,CAAC,MAAM,EAAE,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,EAAE;QAClE,OAAO,GAAG,CAAC;KACZ;IAED,OAAO,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;AAC3E,CAAC;AAhBD,kDAgBC","sourcesContent":["import type { AuthorizationList } from '@ethereumjs/common';\nimport {\n add0x,\n getKnownPropertyNames,\n isStrictHexString,\n} from '@metamask/utils';\nimport type { Json } from '@metamask/utils';\nimport BN from 'bn.js';\n\nimport { TransactionStatus } from '../types';\nimport type {\n TransactionParams,\n TransactionMeta,\n TransactionError,\n GasPriceValue,\n FeeMarketEIP1559Values,\n} from '../types';\n\nexport const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';\n\n// TODO: Replace `any` with type\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst NORMALIZERS: { [param in keyof TransactionParams]: any } = {\n authorizationList: (authorizationList?: AuthorizationList) =>\n authorizationList,\n data: (data: string) => add0x(padHexToEvenLength(data)),\n from: (from: string) => add0x(from).toLowerCase(),\n gas: (gas: string) => add0x(gas),\n gasLimit: (gas: string) => add0x(gas),\n gasPrice: (gasPrice: string) => add0x(gasPrice),\n nonce: (nonce: string) => add0x(nonce),\n to: (to: string) => add0x(to).toLowerCase(),\n value: (value: string) => add0x(value),\n maxFeePerGas: (maxFeePerGas: string) => add0x(maxFeePerGas),\n maxPriorityFeePerGas: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n estimatedBaseFee: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n type: (type: string) => add0x(type),\n};\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param txParams - The transaction params to normalize.\n * @returns Normalized transaction params.\n */\nexport function normalizeTransactionParams(txParams: TransactionParams) {\n const normalizedTxParams: TransactionParams = { from: '' };\n\n for (const key of getKnownPropertyNames(NORMALIZERS)) {\n if (txParams[key]) {\n normalizedTxParams[key] = NORMALIZERS[key](txParams[key]);\n }\n }\n\n if (!normalizedTxParams.value) {\n normalizedTxParams.value = '0x0';\n }\n\n return normalizedTxParams;\n}\n\n/**\n * Checks if a transaction is EIP-1559 by checking for the existence of\n * maxFeePerGas and maxPriorityFeePerGas within its parameters.\n *\n * @param txParams - Transaction params object to add.\n * @returns Boolean that is true if the transaction is EIP-1559 (has maxFeePerGas and maxPriorityFeePerGas), otherwise returns false.\n */\nexport function isEIP1559Transaction(txParams: TransactionParams): boolean {\n const hasOwnProp = (obj: TransactionParams, key: string) =>\n Object.prototype.hasOwnProperty.call(obj, key);\n return (\n hasOwnProp(txParams, 'maxFeePerGas') &&\n hasOwnProp(txParams, 'maxPriorityFeePerGas')\n );\n}\n\nexport const validateGasValues = (\n gasValues: GasPriceValue | FeeMarketEIP1559Values,\n) => {\n Object.keys(gasValues).forEach((key) => {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const value = (gasValues as any)[key];\n if (typeof value !== 'string' || !isStrictHexString(value)) {\n throw new TypeError(\n `expected hex string for ${key} but received: ${value}`,\n );\n }\n });\n};\n\n/**\n * Validates that a transaction is unapproved.\n * Throws if the transaction is not unapproved.\n *\n * @param transactionMeta - The transaction metadata to check.\n * @param fnName - The name of the function calling this helper.\n */\nexport function validateIfTransactionUnapproved(\n transactionMeta: TransactionMeta | undefined,\n fnName: string,\n) {\n if (transactionMeta?.status !== TransactionStatus.unapproved) {\n throw new Error(\n `TransactionsController: Can only call ${fnName} on an unapproved transaction.\\n Current tx status: ${transactionMeta?.status}`,\n );\n }\n}\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param error - The error to be normalize.\n * @returns Normalized transaction error.\n */\nexport function normalizeTxError(\n error: Error & { code?: string; value?: unknown },\n): TransactionError {\n return {\n name: error.name,\n message: error.message,\n stack: error.stack,\n code: error.code,\n rpc: isJsonCompatible(error.value) ? error.value : undefined,\n };\n}\n\n/**\n * Normalize an object containing gas fee values.\n *\n * @param gasFeeValues - An object containing gas fee values.\n * @returns An object containing normalized gas fee values.\n */\nexport function normalizeGasFeeValues(\n gasFeeValues: GasPriceValue | FeeMarketEIP1559Values,\n): GasPriceValue | FeeMarketEIP1559Values {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const normalize = (value: any) =>\n typeof value === 'string' ? add0x(value) : value;\n\n if ('gasPrice' in gasFeeValues) {\n return {\n gasPrice: normalize(gasFeeValues.gasPrice),\n };\n }\n\n return {\n maxFeePerGas: normalize(gasFeeValues.maxFeePerGas),\n maxPriorityFeePerGas: normalize(gasFeeValues.maxPriorityFeePerGas),\n };\n}\n\n/**\n * Determines whether the given value can be encoded as JSON.\n *\n * @param value - The value.\n * @returns True if the value is JSON-encodable, false if not.\n */\nfunction isJsonCompatible(value: unknown): value is Json {\n try {\n JSON.parse(JSON.stringify(value));\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Ensure a hex string is of even length by adding a leading 0 if necessary.\n * Any existing `0x` prefix is preserved but is not added if missing.\n *\n * @param hex - The hex string to ensure is even.\n * @returns The hex string with an even length.\n */\nexport function padHexToEvenLength(hex: string) {\n const prefix = hex.toLowerCase().startsWith('0x') ? hex.slice(0, 2) : '';\n const data = prefix ? hex.slice(2) : hex;\n const evenData = data.length % 2 === 0 ? data : `0${data}`;\n\n return prefix + evenData;\n}\n\n/**\n * Calculate the absolute percentage change between two values.\n *\n * @param originalValue - The first value.\n * @param newValue - The second value.\n * @returns The percentage change from the first value to the second value.\n * If the original value is zero and the new value is not, returns 100.\n */\nexport function getPercentageChange(originalValue: BN, newValue: BN): number {\n const precisionFactor = new BN(10).pow(new BN(18));\n const originalValuePrecision = originalValue.mul(precisionFactor);\n const newValuePrecision = newValue.mul(precisionFactor);\n\n const difference = newValuePrecision.sub(originalValuePrecision);\n\n if (difference.isZero()) {\n return 0;\n }\n\n if (originalValuePrecision.isZero() && !newValuePrecision.isZero()) {\n return 100;\n }\n\n return difference.muln(100).div(originalValuePrecision).abs().toNumber();\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc;AAGvB,OAAO,KAAK,EACV,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,aAAa,EACb,sBAAsB,EACvB,qBAAiB;AAElB,eAAO,MAAM,kBAAkB,qCAAqC,CAAC;AAuBrE;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,iBAAiB,qBAcrE;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAOzE;AAED,eAAO,MAAM,iBAAiB,cACjB,aAAa,GAAG,sBAAsB,SAYlD,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,+BAA+B,CAC7C,eAAe,EAAE,eAAe,GAAG,SAAS,EAC5C,MAAM,EAAE,MAAM,QAOf;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,KAAK,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,GAChD,gBAAgB,CAQlB;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,YAAY,EAAE,aAAa,GAAG,sBAAsB,GACnD,aAAa,GAAG,sBAAsB,CAgBxC;AAiBD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,UAM7C;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,GAAG,MAAM,CAgB3E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,cAAc;AAGvB,OAAO,KAAK,EACV,iBAAiB,EACjB,eAAe,EACf,gBAAgB,EAChB,aAAa,EACb,sBAAsB,EACvB,qBAAiB;AAElB,eAAO,MAAM,kBAAkB,qCAAqC,CAAC;AAuBrE;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,QAAQ,EAAE,iBAAiB,qBAcrE;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAOzE;AAED,eAAO,MAAM,iBAAiB,cACjB,aAAa,GAAG,sBAAsB,SAYlD,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,+BAA+B,CAC7C,eAAe,EAAE,eAAe,GAAG,SAAS,EAC5C,MAAM,EAAE,MAAM,QAOf;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,KAAK,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,GAChD,gBAAgB,CAQlB;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CACnC,YAAY,EAAE,aAAa,GAAG,sBAAsB,GACnD,aAAa,GAAG,sBAAsB,CAgBxC;AAiBD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,UAM7C;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,GAAG,MAAM,CAgB3E"}
|
package/dist/utils/utils.mjs
CHANGED
|
@@ -12,6 +12,7 @@ export const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';
|
|
|
12
12
|
// TODO: Replace `any` with type
|
|
13
13
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
14
|
const NORMALIZERS = {
|
|
15
|
+
authorizationList: (authorizationList) => authorizationList,
|
|
15
16
|
data: (data) => add0x(padHexToEvenLength(data)),
|
|
16
17
|
from: (from) => add0x(from).toLowerCase(),
|
|
17
18
|
gas: (gas) => add0x(gas),
|
|
@@ -61,10 +62,7 @@ export const validateGasValues = (gasValues) => {
|
|
|
61
62
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
62
63
|
const value = gasValues[key];
|
|
63
64
|
if (typeof value !== 'string' || !isStrictHexString(value)) {
|
|
64
|
-
throw new TypeError(
|
|
65
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
66
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
67
|
-
`expected hex string for ${key} but received: ${value}`);
|
|
65
|
+
throw new TypeError(`expected hex string for ${key} but received: ${value}`);
|
|
68
66
|
}
|
|
69
67
|
});
|
|
70
68
|
};
|
|
@@ -77,10 +75,7 @@ export const validateGasValues = (gasValues) => {
|
|
|
77
75
|
*/
|
|
78
76
|
export function validateIfTransactionUnapproved(transactionMeta, fnName) {
|
|
79
77
|
if (transactionMeta?.status !== TransactionStatus.unapproved) {
|
|
80
|
-
throw new Error(
|
|
81
|
-
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
83
|
-
`TransactionsController: Can only call ${fnName} on an unapproved transaction.\n Current tx status: ${transactionMeta?.status}`);
|
|
78
|
+
throw new Error(`TransactionsController: Can only call ${fnName} on an unapproved transaction.\n Current tx status: ${transactionMeta?.status}`);
|
|
84
79
|
}
|
|
85
80
|
}
|
|
86
81
|
/**
|
package/dist/utils/utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EACL,KAAK,EACL,qBAAqB,EACrB,iBAAiB,EAClB,wBAAwB;AAEzB,OAAO,GAAE,cAAc;;AAEvB,OAAO,EAAE,iBAAiB,EAAE,qBAAiB;AAS7C,MAAM,CAAC,MAAM,kBAAkB,GAAG,kCAAkC,CAAC;AAErE,gCAAgC;AAChC,8DAA8D;AAC9D,MAAM,WAAW,GAAgD;IAC/D,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,KAAK,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACvD,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE;IACjD,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC;IAChC,QAAQ,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC;IACrC,QAAQ,EAAE,CAAC,QAAgB,EAAE,EAAE,CAAC,KAAK,CAAC,QAAQ,C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AAC3B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAAC,aAAiB,EAAE,QAAY;IACjE,MAAM,eAAe,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,MAAM,sBAAsB,GAAG,aAAa,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAClE,MAAM,iBAAiB,GAAG,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAExD,MAAM,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;IAEjE,IAAI,UAAU,CAAC,MAAM,EAAE,EAAE;QACvB,OAAO,CAAC,CAAC;KACV;IAED,IAAI,sBAAsB,CAAC,MAAM,EAAE,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,EAAE;QAClE,OAAO,GAAG,CAAC;KACZ;IAED,OAAO,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;AAC3E,CAAC","sourcesContent":["import {\n add0x,\n getKnownPropertyNames,\n isStrictHexString,\n} from '@metamask/utils';\nimport type { Json } from '@metamask/utils';\nimport BN from 'bn.js';\n\nimport { TransactionStatus } from '../types';\nimport type {\n TransactionParams,\n TransactionMeta,\n TransactionError,\n GasPriceValue,\n FeeMarketEIP1559Values,\n} from '../types';\n\nexport const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';\n\n// TODO: Replace `any` with type\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst NORMALIZERS: { [param in keyof TransactionParams]: any } = {\n data: (data: string) => add0x(padHexToEvenLength(data)),\n from: (from: string) => add0x(from).toLowerCase(),\n gas: (gas: string) => add0x(gas),\n gasLimit: (gas: string) => add0x(gas),\n gasPrice: (gasPrice: string) => add0x(gasPrice),\n nonce: (nonce: string) => add0x(nonce),\n to: (to: string) => add0x(to).toLowerCase(),\n value: (value: string) => add0x(value),\n maxFeePerGas: (maxFeePerGas: string) => add0x(maxFeePerGas),\n maxPriorityFeePerGas: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n estimatedBaseFee: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n type: (type: string) => add0x(type),\n};\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param txParams - The transaction params to normalize.\n * @returns Normalized transaction params.\n */\nexport function normalizeTransactionParams(txParams: TransactionParams) {\n const normalizedTxParams: TransactionParams = { from: '' };\n\n for (const key of getKnownPropertyNames(NORMALIZERS)) {\n if (txParams[key]) {\n normalizedTxParams[key] = NORMALIZERS[key](txParams[key]);\n }\n }\n\n if (!normalizedTxParams.value) {\n normalizedTxParams.value = '0x0';\n }\n\n return normalizedTxParams;\n}\n\n/**\n * Checks if a transaction is EIP-1559 by checking for the existence of\n * maxFeePerGas and maxPriorityFeePerGas within its parameters.\n *\n * @param txParams - Transaction params object to add.\n * @returns Boolean that is true if the transaction is EIP-1559 (has maxFeePerGas and maxPriorityFeePerGas), otherwise returns false.\n */\nexport function isEIP1559Transaction(txParams: TransactionParams): boolean {\n const hasOwnProp = (obj: TransactionParams, key: string) =>\n Object.prototype.hasOwnProperty.call(obj, key);\n return (\n hasOwnProp(txParams, 'maxFeePerGas') &&\n hasOwnProp(txParams, 'maxPriorityFeePerGas')\n );\n}\n\nexport const validateGasValues = (\n gasValues: GasPriceValue | FeeMarketEIP1559Values,\n) => {\n Object.keys(gasValues).forEach((key) => {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const value = (gasValues as any)[key];\n if (typeof value !== 'string' || !isStrictHexString(value)) {\n throw new TypeError(\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `expected hex string for ${key} but received: ${value}`,\n );\n }\n });\n};\n\n/**\n * Validates that a transaction is unapproved.\n * Throws if the transaction is not unapproved.\n *\n * @param transactionMeta - The transaction metadata to check.\n * @param fnName - The name of the function calling this helper.\n */\nexport function validateIfTransactionUnapproved(\n transactionMeta: TransactionMeta | undefined,\n fnName: string,\n) {\n if (transactionMeta?.status !== TransactionStatus.unapproved) {\n throw new Error(\n // TODO: Either fix this lint violation or explain why it's necessary to ignore.\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `TransactionsController: Can only call ${fnName} on an unapproved transaction.\\n Current tx status: ${transactionMeta?.status}`,\n );\n }\n}\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param error - The error to be normalize.\n * @returns Normalized transaction error.\n */\nexport function normalizeTxError(\n error: Error & { code?: string; value?: unknown },\n): TransactionError {\n return {\n name: error.name,\n message: error.message,\n stack: error.stack,\n code: error.code,\n rpc: isJsonCompatible(error.value) ? error.value : undefined,\n };\n}\n\n/**\n * Normalize an object containing gas fee values.\n *\n * @param gasFeeValues - An object containing gas fee values.\n * @returns An object containing normalized gas fee values.\n */\nexport function normalizeGasFeeValues(\n gasFeeValues: GasPriceValue | FeeMarketEIP1559Values,\n): GasPriceValue | FeeMarketEIP1559Values {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const normalize = (value: any) =>\n typeof value === 'string' ? add0x(value) : value;\n\n if ('gasPrice' in gasFeeValues) {\n return {\n gasPrice: normalize(gasFeeValues.gasPrice),\n };\n }\n\n return {\n maxFeePerGas: normalize(gasFeeValues.maxFeePerGas),\n maxPriorityFeePerGas: normalize(gasFeeValues.maxPriorityFeePerGas),\n };\n}\n\n/**\n * Determines whether the given value can be encoded as JSON.\n *\n * @param value - The value.\n * @returns True if the value is JSON-encodable, false if not.\n */\nfunction isJsonCompatible(value: unknown): value is Json {\n try {\n JSON.parse(JSON.stringify(value));\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Ensure a hex string is of even length by adding a leading 0 if necessary.\n * Any existing `0x` prefix is preserved but is not added if missing.\n *\n * @param hex - The hex string to ensure is even.\n * @returns The hex string with an even length.\n */\nexport function padHexToEvenLength(hex: string) {\n const prefix = hex.toLowerCase().startsWith('0x') ? hex.slice(0, 2) : '';\n const data = prefix ? hex.slice(2) : hex;\n const evenData = data.length % 2 === 0 ? data : `0${data}`;\n\n return prefix + evenData;\n}\n\n/**\n * Calculate the absolute percentage change between two values.\n *\n * @param originalValue - The first value.\n * @param newValue - The second value.\n * @returns The percentage change from the first value to the second value.\n * If the original value is zero and the new value is not, returns 100.\n */\nexport function getPercentageChange(originalValue: BN, newValue: BN): number {\n const precisionFactor = new BN(10).pow(new BN(18));\n const originalValuePrecision = originalValue.mul(precisionFactor);\n const newValuePrecision = newValue.mul(precisionFactor);\n\n const difference = newValuePrecision.sub(originalValuePrecision);\n\n if (difference.isZero()) {\n return 0;\n }\n\n if (originalValuePrecision.isZero() && !newValuePrecision.isZero()) {\n return 100;\n }\n\n return difference.muln(100).div(originalValuePrecision).abs().toNumber();\n}\n"]}
|
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":";;;;;;AACA,OAAO,EACL,KAAK,EACL,qBAAqB,EACrB,iBAAiB,EAClB,wBAAwB;AAEzB,OAAO,GAAE,cAAc;;AAEvB,OAAO,EAAE,iBAAiB,EAAE,qBAAiB;AAS7C,MAAM,CAAC,MAAM,kBAAkB,GAAG,kCAAkC,CAAC;AAErE,gCAAgC;AAChC,8DAA8D;AAC9D,MAAM,WAAW,GAAgD;IAC/D,iBAAiB,EAAE,CAAC,iBAAqC,EAAE,EAAE,CAC3D,iBAAiB;IACnB,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,KAAK,CAAC,kBAAkB,CAAC,IAAI,CAAC,CAAC;IACvD,IAAI,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE;IACjD,GAAG,EAAE,CAAC,GAAW,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AG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC;IAE3D,OAAO,MAAM,GAAG,QAAQ,CAAC;AAC3B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAAC,aAAiB,EAAE,QAAY;IACjE,MAAM,eAAe,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,MAAM,sBAAsB,GAAG,aAAa,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAClE,MAAM,iBAAiB,GAAG,QAAQ,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAExD,MAAM,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;IAEjE,IAAI,UAAU,CAAC,MAAM,EAAE,EAAE;QACvB,OAAO,CAAC,CAAC;KACV;IAED,IAAI,sBAAsB,CAAC,MAAM,EAAE,IAAI,CAAC,iBAAiB,CAAC,MAAM,EAAE,EAAE;QAClE,OAAO,GAAG,CAAC;KACZ;IAED,OAAO,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC;AAC3E,CAAC","sourcesContent":["import type { AuthorizationList } from '@ethereumjs/common';\nimport {\n add0x,\n getKnownPropertyNames,\n isStrictHexString,\n} from '@metamask/utils';\nimport type { Json } from '@metamask/utils';\nimport BN from 'bn.js';\n\nimport { TransactionStatus } from '../types';\nimport type {\n TransactionParams,\n TransactionMeta,\n TransactionError,\n GasPriceValue,\n FeeMarketEIP1559Values,\n} from '../types';\n\nexport const ESTIMATE_GAS_ERROR = 'eth_estimateGas rpc method error';\n\n// TODO: Replace `any` with type\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nconst NORMALIZERS: { [param in keyof TransactionParams]: any } = {\n authorizationList: (authorizationList?: AuthorizationList) =>\n authorizationList,\n data: (data: string) => add0x(padHexToEvenLength(data)),\n from: (from: string) => add0x(from).toLowerCase(),\n gas: (gas: string) => add0x(gas),\n gasLimit: (gas: string) => add0x(gas),\n gasPrice: (gasPrice: string) => add0x(gasPrice),\n nonce: (nonce: string) => add0x(nonce),\n to: (to: string) => add0x(to).toLowerCase(),\n value: (value: string) => add0x(value),\n maxFeePerGas: (maxFeePerGas: string) => add0x(maxFeePerGas),\n maxPriorityFeePerGas: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n estimatedBaseFee: (maxPriorityFeePerGas: string) =>\n add0x(maxPriorityFeePerGas),\n type: (type: string) => add0x(type),\n};\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param txParams - The transaction params to normalize.\n * @returns Normalized transaction params.\n */\nexport function normalizeTransactionParams(txParams: TransactionParams) {\n const normalizedTxParams: TransactionParams = { from: '' };\n\n for (const key of getKnownPropertyNames(NORMALIZERS)) {\n if (txParams[key]) {\n normalizedTxParams[key] = NORMALIZERS[key](txParams[key]);\n }\n }\n\n if (!normalizedTxParams.value) {\n normalizedTxParams.value = '0x0';\n }\n\n return normalizedTxParams;\n}\n\n/**\n * Checks if a transaction is EIP-1559 by checking for the existence of\n * maxFeePerGas and maxPriorityFeePerGas within its parameters.\n *\n * @param txParams - Transaction params object to add.\n * @returns Boolean that is true if the transaction is EIP-1559 (has maxFeePerGas and maxPriorityFeePerGas), otherwise returns false.\n */\nexport function isEIP1559Transaction(txParams: TransactionParams): boolean {\n const hasOwnProp = (obj: TransactionParams, key: string) =>\n Object.prototype.hasOwnProperty.call(obj, key);\n return (\n hasOwnProp(txParams, 'maxFeePerGas') &&\n hasOwnProp(txParams, 'maxPriorityFeePerGas')\n );\n}\n\nexport const validateGasValues = (\n gasValues: GasPriceValue | FeeMarketEIP1559Values,\n) => {\n Object.keys(gasValues).forEach((key) => {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const value = (gasValues as any)[key];\n if (typeof value !== 'string' || !isStrictHexString(value)) {\n throw new TypeError(\n `expected hex string for ${key} but received: ${value}`,\n );\n }\n });\n};\n\n/**\n * Validates that a transaction is unapproved.\n * Throws if the transaction is not unapproved.\n *\n * @param transactionMeta - The transaction metadata to check.\n * @param fnName - The name of the function calling this helper.\n */\nexport function validateIfTransactionUnapproved(\n transactionMeta: TransactionMeta | undefined,\n fnName: string,\n) {\n if (transactionMeta?.status !== TransactionStatus.unapproved) {\n throw new Error(\n `TransactionsController: Can only call ${fnName} on an unapproved transaction.\\n Current tx status: ${transactionMeta?.status}`,\n );\n }\n}\n\n/**\n * Normalizes properties on transaction params.\n *\n * @param error - The error to be normalize.\n * @returns Normalized transaction error.\n */\nexport function normalizeTxError(\n error: Error & { code?: string; value?: unknown },\n): TransactionError {\n return {\n name: error.name,\n message: error.message,\n stack: error.stack,\n code: error.code,\n rpc: isJsonCompatible(error.value) ? error.value : undefined,\n };\n}\n\n/**\n * Normalize an object containing gas fee values.\n *\n * @param gasFeeValues - An object containing gas fee values.\n * @returns An object containing normalized gas fee values.\n */\nexport function normalizeGasFeeValues(\n gasFeeValues: GasPriceValue | FeeMarketEIP1559Values,\n): GasPriceValue | FeeMarketEIP1559Values {\n // TODO: Replace `any` with type\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const normalize = (value: any) =>\n typeof value === 'string' ? add0x(value) : value;\n\n if ('gasPrice' in gasFeeValues) {\n return {\n gasPrice: normalize(gasFeeValues.gasPrice),\n };\n }\n\n return {\n maxFeePerGas: normalize(gasFeeValues.maxFeePerGas),\n maxPriorityFeePerGas: normalize(gasFeeValues.maxPriorityFeePerGas),\n };\n}\n\n/**\n * Determines whether the given value can be encoded as JSON.\n *\n * @param value - The value.\n * @returns True if the value is JSON-encodable, false if not.\n */\nfunction isJsonCompatible(value: unknown): value is Json {\n try {\n JSON.parse(JSON.stringify(value));\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Ensure a hex string is of even length by adding a leading 0 if necessary.\n * Any existing `0x` prefix is preserved but is not added if missing.\n *\n * @param hex - The hex string to ensure is even.\n * @returns The hex string with an even length.\n */\nexport function padHexToEvenLength(hex: string) {\n const prefix = hex.toLowerCase().startsWith('0x') ? hex.slice(0, 2) : '';\n const data = prefix ? hex.slice(2) : hex;\n const evenData = data.length % 2 === 0 ? data : `0${data}`;\n\n return prefix + evenData;\n}\n\n/**\n * Calculate the absolute percentage change between two values.\n *\n * @param originalValue - The first value.\n * @param newValue - The second value.\n * @returns The percentage change from the first value to the second value.\n * If the original value is zero and the new value is not, returns 100.\n */\nexport function getPercentageChange(originalValue: BN, newValue: BN): number {\n const precisionFactor = new BN(10).pow(new BN(18));\n const originalValuePrecision = originalValue.mul(precisionFactor);\n const newValuePrecision = newValue.mul(precisionFactor);\n\n const difference = newValuePrecision.sub(originalValuePrecision);\n\n if (difference.isZero()) {\n return 0;\n }\n\n if (originalValuePrecision.isZero() && !newValuePrecision.isZero()) {\n return 100;\n }\n\n return difference.muln(100).div(originalValuePrecision).abs().toNumber();\n}\n"]}
|