@@ -10,6 +10,7 @@ import {
1010} from '@metamask/utils' ;
1111import type { BigNumber } from 'bignumber.js' ;
1212import BN from 'bn.js' ;
13+ import BN4 from 'bnjs4' ;
1314import ensNamehash from 'eth-ens-namehash' ;
1415import deepEqual from 'fast-deep-equal' ;
1516
@@ -69,10 +70,31 @@ export function isSafeChainId(chainId: Hex): boolean {
6970 */
7071// TODO: Either fix this lint violation or explain why it's necessary to ignore.
7172// eslint-disable-next-line @typescript-eslint/naming-convention
72- export function BNToHex ( inputBn : BN | BigNumber ) {
73+ export function BNToHex ( inputBn : BN | BN4 | BigNumber ) : string {
7374 return add0x ( inputBn . toString ( 16 ) ) ;
7475}
7576
77+ function getBNImplementation ( targetBN : BN4 ) : typeof BN4 ;
78+ function getBNImplementation ( targetBN : BN ) : typeof BN ;
79+ /**
80+ * Return the bn.js library responsible for the BN in question
81+ * @param targetBN - A BN instance
82+ * @returns A bn.js instance
83+ */
84+ function getBNImplementation ( targetBN : BN | BN4 ) : typeof BN4 | typeof BN {
85+ return Object . keys ( targetBN ) . includes ( '_strip' ) ? BN4 : BN ;
86+ }
87+
88+ export function fractionBN (
89+ targetBN : BN ,
90+ numerator : number | string ,
91+ denominator : number | string ,
92+ ) : BN ;
93+ export function fractionBN (
94+ targetBN : BN4 ,
95+ numerator : number | string ,
96+ denominator : number | string ,
97+ ) : BN4 ;
7698/**
7799 * Used to multiply a BN by a fraction.
78100 *
@@ -82,12 +104,16 @@ export function BNToHex(inputBn: BN | BigNumber) {
82104 * @returns Product of the multiplication.
83105 */
84106export function fractionBN (
85- targetBN : BN ,
107+ targetBN : BN | BN4 ,
86108 numerator : number | string ,
87109 denominator : number | string ,
88- ) {
89- const numBN = new BN ( numerator ) ;
90- const denomBN = new BN ( denominator ) ;
110+ ) : BN | BN4 {
111+ // @ts -expect-error - Signature overload confusion
112+ const BNImplementation = getBNImplementation ( targetBN ) ;
113+
114+ const numBN = new BNImplementation ( numerator ) ;
115+ const denomBN = new BNImplementation ( denominator ) ;
116+ // @ts -expect-error - BNImplementation gets unexpected typed
91117 return targetBN . mul ( numBN ) . div ( denomBN ) ;
92118}
93119
@@ -192,18 +218,20 @@ export function hexToText(hex: string) {
192218 }
193219}
194220
221+ export function fromHex ( value : string | BN ) : BN ;
222+ export function fromHex ( value : BN4 ) : BN4 ;
195223/**
196224 * Parses a hex string and converts it into a number that can be operated on in a bignum-safe,
197225 * base-10 way.
198226 *
199227 * @param value - A base-16 number encoded as a string.
200228 * @returns The number as a BN object in base-16 mode.
201229 */
202- export function fromHex ( value : string | BN ) : BN {
230+ export function fromHex ( value : string | BN | BN4 ) : BN | BN4 {
203231 if ( BN . isBN ( value ) ) {
204232 return value ;
205233 }
206- return new BN ( hexToBN ( value ) . toString ( 10 ) ) ;
234+ return new BN ( hexToBN ( value as string ) . toString ( 10 ) , 10 ) ;
207235}
208236
209237/**
@@ -212,14 +240,14 @@ export function fromHex(value: string | BN): BN {
212240 * @param value - An integer, an integer encoded as a base-10 string, or a BN.
213241 * @returns The integer encoded as a hex string.
214242 */
215- export function toHex ( value : number | bigint | string | BN ) : Hex {
243+ export function toHex ( value : number | bigint | string | BN | BN4 ) : Hex {
216244 if ( typeof value === 'string' && isStrictHexString ( value ) ) {
217245 return value ;
218246 }
219247 const hexString =
220248 BN . isBN ( value ) || typeof value === 'bigint'
221249 ? value . toString ( 16 )
222- : new BN ( value . toString ( ) , 10 ) . toString ( 16 ) ;
250+ : new BN ( value . toString ( 10 ) , 10 ) . toString ( 16 ) ;
223251 return `0x${ hexString } ` ;
224252}
225253
0 commit comments