peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

Invalid number for a long hexadecimal number #228

Closed NightlySide closed 2 years ago

NightlySide commented 2 years ago

I'm using "big-integer" as a way to polyfill BigInt for react-native (with expo). And everything seems fine except from when a library (I guess libp2p) tries to convert the following number:

let number = BigInt(0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee);

I get the following error:

Error: Invalid integer: 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee

For information I have a shim.js file with the following:

if (typeof BigInt === "undefined") global.BigInt = require("big-integer");
Yaffle commented 2 years ago

The hexadecimal literal is converted to js number and this may cause precision loss, so you need to put it into quotes. Native BigInt does check that although.

peterolson commented 2 years ago

The correct way to convert that number is like this:

bigInt("7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee", 16)

using a string and an argument telling what base the number is in, without the 0x at the beginning.

D4nte commented 2 years ago

@peterolson However, looking at the BigInt Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

The radius is not needed. If big-integer can act as a polyfill then shouldn't it provide the same API?

peterolson commented 2 years ago

This library is acts as a thin wrapper over the native BigInt if the browser supports it, but it doesn't reproduce the same API. The API is as described in the README of this project.