From dadeac93f3c745cd30c4b983d80440cf384e7eb7 Mon Sep 17 00:00:00 2001 From: legobeat <109787230+legobeat@users.noreply.github.com> Date: Tue, 26 Nov 2024 23:20:26 +0900 Subject: [PATCH] fix: use BN from bn.js instead of ethereumjs-util (#28146) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** - remove redundant resolutions entries - `ethereumjs-util` v5 is no longer present - fix: use `BN` from bn.js (v5) instead of `ethereumjs-util` (deprecated version) [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28146?quickstart=1) ## **Related issues** #### Blocking - #28169 - #28171 - #28170 ## **Manual testing steps** ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- app/scripts/migrations/088.ts | 4 ++-- package.json | 5 ----- shared/modules/conversion.utils.ts | 3 ++- ui/helpers/utils/util.js | 6 +++--- ui/helpers/utils/util.test.js | 3 ++- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/app/scripts/migrations/088.ts b/app/scripts/migrations/088.ts index 274b93624a85..27f324228790 100644 --- a/app/scripts/migrations/088.ts +++ b/app/scripts/migrations/088.ts @@ -1,5 +1,5 @@ import { hasProperty, Hex, isObject, isStrictHexString } from '@metamask/utils'; -import { BN } from 'ethereumjs-util'; +import BN from 'bn.js'; import { cloneDeep, mapKeys } from 'lodash'; import log from 'loglevel'; @@ -302,6 +302,6 @@ function toHex(value: number | string | BN): Hex { } const hexString = BN.isBN(value) ? value.toString(16) - : new BN(value.toString(), 10).toString(16); + : new BN(value.toString(10), 10).toString(16); return `0x${hexString}`; } diff --git a/package.json b/package.json index 5a3a7531a45b..9ed956d639cd 100644 --- a/package.json +++ b/package.json @@ -180,11 +180,6 @@ "eslint@npm:^8.7.0": "patch:eslint@npm%3A8.57.0#~/.yarn/patches/eslint-npm-8.57.0-4286e12a3a.patch", "eth-query@^2.1.2": "patch:eth-query@npm%3A2.1.2#./.yarn/patches/eth-query-npm-2.1.2-7c6adc825f.patch", "eth-query@^2.1.0": "patch:eth-query@npm%3A2.1.2#./.yarn/patches/eth-query-npm-2.1.2-7c6adc825f.patch", - "ethereumjs-util@^5.1.1": "patch:ethereumjs-util@npm%3A5.2.1#./.yarn/patches/ethereumjs-util-npm-5.2.1-72b39f4e7e.patch", - "ethereumjs-util@^5.1.2": "patch:ethereumjs-util@npm%3A5.2.1#./.yarn/patches/ethereumjs-util-npm-5.2.1-72b39f4e7e.patch", - "ethereumjs-util@^5.1.5": "patch:ethereumjs-util@npm%3A5.2.1#./.yarn/patches/ethereumjs-util-npm-5.2.1-72b39f4e7e.patch", - "ethereumjs-util@^5.0.0": "patch:ethereumjs-util@npm%3A5.2.1#./.yarn/patches/ethereumjs-util-npm-5.2.1-72b39f4e7e.patch", - "ethereumjs-util@^5.2.0": "patch:ethereumjs-util@npm%3A5.2.1#./.yarn/patches/ethereumjs-util-npm-5.2.1-72b39f4e7e.patch", "ethereumjs-util@^7.0.10": "patch:ethereumjs-util@npm%3A7.1.5#./.yarn/patches/ethereumjs-util-npm-7.1.5-5bb4d00000.patch", "ethereumjs-util@^7.1.5": "patch:ethereumjs-util@npm%3A7.1.5#./.yarn/patches/ethereumjs-util-npm-7.1.5-5bb4d00000.patch", "ethereumjs-util@^7.1.4": "patch:ethereumjs-util@npm%3A7.1.5#./.yarn/patches/ethereumjs-util-npm-7.1.5-5bb4d00000.patch", diff --git a/shared/modules/conversion.utils.ts b/shared/modules/conversion.utils.ts index 75da336eb8e6..5c70e5ecd683 100644 --- a/shared/modules/conversion.utils.ts +++ b/shared/modules/conversion.utils.ts @@ -1,6 +1,7 @@ import { Hex } from '@metamask/utils'; import { BigNumber } from 'bignumber.js'; -import { addHexPrefix, BN } from 'ethereumjs-util'; +import BN from 'bn.js'; +import { addHexPrefix } from 'ethereumjs-util'; import { EtherDenomination } from '../constants/common'; import { Numeric, NumericValue } from './Numeric'; diff --git a/ui/helpers/utils/util.js b/ui/helpers/utils/util.js index eafc8e31bfe5..d687d9b82338 100644 --- a/ui/helpers/utils/util.js +++ b/ui/helpers/utils/util.js @@ -1,7 +1,7 @@ import punycode from 'punycode/punycode'; import abi from 'human-standard-token-abi'; import BigNumber from 'bignumber.js'; -import * as ethUtil from 'ethereumjs-util'; +import BN from 'bn.js'; import { DateTime } from 'luxon'; import { getFormattedIpfsUrl, @@ -168,10 +168,10 @@ export function isOriginContractAddress(to, sendTokenAddress) { // Takes wei Hex, returns wei BN, even if input is null export function numericBalance(balance) { if (!balance) { - return new ethUtil.BN(0, 16); + return new BN(0, 16); } const stripped = stripHexPrefix(balance); - return new ethUtil.BN(stripped, 16); + return new BN(stripped, 16); } // Takes hex, returns [beforeDecimal, afterDecimal] diff --git a/ui/helpers/utils/util.test.js b/ui/helpers/utils/util.test.js index d12a57675343..bdf5c9dd9b98 100644 --- a/ui/helpers/utils/util.test.js +++ b/ui/helpers/utils/util.test.js @@ -1,5 +1,6 @@ import Bowser from 'bowser'; -import { BN, toChecksumAddress } from 'ethereumjs-util'; +import BN from 'bn.js'; +import { toChecksumAddress } from 'ethereumjs-util'; import { CHAIN_IDS } from '../../../shared/constants/network'; import { addHexPrefixToObjectValues } from '../../../shared/lib/swaps-utils'; import { toPrecisionWithoutTrailingZeros } from '../../../shared/lib/transactions-controller-utils';