Skip to content

Commit

Permalink
feat(controller-utils): support BigNumber as input to BNToHex (#4827)
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat authored Oct 23, 2024
1 parent 24d257a commit ce8af03
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/controller-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
"@types/jest": "^27.4.1",
"bignumber.js": "^4.1.0",
"deepmerge": "^4.2.2",
"jest": "^27.5.1",
"nock": "^13.3.1",
Expand Down
2 changes: 2 additions & 0 deletions packages/controller-utils/src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EthQuery from '@metamask/eth-query';
import BigNumber from 'bignumber.js';
import BN from 'bn.js';
import nock from 'nock';

Expand Down Expand Up @@ -31,6 +32,7 @@ describe('util', () => {

it('bNToHex', () => {
expect(util.BNToHex(new BN('1337'))).toBe('0x539');
expect(util.BNToHex(new BigNumber('1337'))).toBe('0x539');
});

it('fractionBN', () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/controller-utils/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import {
isHexString,
remove0x,
} from '@metamask/utils';
import type { BigNumber } from 'bignumber.js';
import BN from 'bn.js';
import ensNamehash from 'eth-ens-namehash';
import deepEqual from 'fast-deep-equal';

import { MAX_SAFE_CHAIN_ID } from './constants';

export type { BigNumber };

const TIMEOUT_ERROR = new Error('timeout');

export const PROTOTYPE_POLLUTION_BLOCKLIST = [
Expand Down Expand Up @@ -59,14 +62,14 @@ export function isSafeChainId(chainId: Hex): boolean {
);
}
/**
* Converts a BN object to a hex string with a '0x' prefix.
* Converts a BN or BigNumber object to a hex string with a '0x' prefix.
*
* @param inputBn - BN instance to convert to a hex string.
* @param inputBn - BN|BigNumber instance to convert to a hex string.
* @returns A '0x'-prefixed hex string.
*/
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/naming-convention
export function BNToHex(inputBn: BN) {
export function BNToHex(inputBn: BN | BigNumber) {
return add0x(inputBn.toString(16));
}

Expand Down
22 changes: 16 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,7 @@ __metadata:
"@spruceid/siwe-parser": "npm:2.1.0"
"@types/bn.js": "npm:^5.1.5"
"@types/jest": "npm:^27.4.1"
bignumber.js: "npm:^4.1.0"
bn.js: "npm:^5.2.1"
deepmerge: "npm:^4.2.2"
eth-ens-namehash: "npm:^2.0.8"
Expand Down Expand Up @@ -6392,7 +6393,7 @@ __metadata:
languageName: node
linkType: hard

"elliptic@npm:^6.5.4, elliptic@npm:^6.5.7":
"elliptic@npm:^6.5.7":
version: 6.5.7
resolution: "elliptic@npm:6.5.7"
dependencies:
Expand Down Expand Up @@ -10073,6 +10074,15 @@ __metadata:
languageName: node
linkType: hard

"node-addon-api@npm:^5.0.0":
version: 5.1.0
resolution: "node-addon-api@npm:5.1.0"
dependencies:
node-gyp: "npm:latest"
checksum: 10/595f59ffb4630564f587c502119cbd980d302e482781021f3b479f5fc7e41cf8f2f7280fdc2795f32d148e4f3259bd15043c52d4a3442796aa6f1ae97b959636
languageName: node
linkType: hard

"node-fetch@npm:^2.6.1":
version: 2.7.0
resolution: "node-fetch@npm:2.7.0"
Expand Down Expand Up @@ -11199,14 +11209,14 @@ __metadata:
linkType: hard

"secp256k1@npm:^4.0.0, secp256k1@npm:^4.0.1":
version: 4.0.3
resolution: "secp256k1@npm:4.0.3"
version: 4.0.4
resolution: "secp256k1@npm:4.0.4"
dependencies:
elliptic: "npm:^6.5.4"
node-addon-api: "npm:^2.0.0"
elliptic: "npm:^6.5.7"
node-addon-api: "npm:^5.0.0"
node-gyp: "npm:latest"
node-gyp-build: "npm:^4.2.0"
checksum: 10/8b45820cd90fd2f95cc8fdb9bf8a71e572de09f2311911ae461a951ffa9e30c99186a129d0f1afeb380dd67eca0c10493f8a7513c39063fda015e99995088e3b
checksum: 10/45000f348c853df7c1e2b67c48efb062ae78c0620ab1a5cfb02fa20d3aad39c641f4e7a18b3de3b54a7c0cc1e0addeb8ecd9d88bc332e92df17a92b60c36122a
languageName: node
linkType: hard

Expand Down

0 comments on commit ce8af03

Please sign in to comment.