Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's the need for a custom BN wrapper? #312

Closed
LogvinovLeon opened this issue Oct 16, 2018 · 6 comments
Closed

What's the need for a custom BN wrapper? #312

LogvinovLeon opened this issue Oct 16, 2018 · 6 comments
Labels
discussion Questions, feedback and general information.

Comments

@LogvinovLeon
Copy link

Why does ethers.js use a custom-written bn.js wrapper?
I know some tools in the community use BigNumber and some others use bn.js, but taking bn.js wrapping it in a restrictive way, converting it back and force on every operation between bn and hex forms and calling it BigNumber to have colliding names with an existing and popular library seems like a strange decision to me.

@ricmoo
Copy link
Member

ricmoo commented Oct 16, 2018

First, I wanted an immutable BigNumber. Often times when calling an operation, like sendTransaction, there is a number of steps, à la Promises executed, so by the time serialization actually occurs, someone could have changed the value, maliciously or by accident. By making them immutable, I can save code and effort, and pass them along.

I also wanted to abstract it, offering only what was needed, so in the future if I decide to swap it out, I only need to implement the functionality I require. For example, I would not want to implement RED myself, and since outside of ECC, it is unused, so if I just handed over the BN.js, to maintain backward compatibility, someone might have used it, so I would need to keep that going forward.

BigNumbers are a fairly fundamental part of an Ethereum library, so I wanted more control over them. I wanted an easier to use API, which I believe BigNumber provides, through its BigNumberish input parameters. I find it, personally, saves me a lot of time and code, compared to using BN.js directly.

As a side note, the reason it wraps BN.js, is that BN.js is used by elliptic, so it is absolutely required, regardless to use secp256k1. So, this reduced the code size a lot. But if, for example, there was a version of ethers without signing (or delegated signing and recovery), the BigNumber library could be massively reduced, since it only supports the subset of features required, as RED and combs could be removed.

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Oct 16, 2018
@ricmoo
Copy link
Member

ricmoo commented Oct 24, 2018

Closing this now, but please feel free to continue discussion (I still monitor closed tickets), or feel free to re-open. :)

@ricmoo ricmoo closed this as completed Oct 24, 2018
@PierreJeanjacquot
Copy link

Hello, I'm using both ethers and bn.js in my project. Is there a way to convert a value bigNumberify by ethers into a bn.js BigNumber ?

@ricmoo
Copy link
Member

ricmoo commented Nov 15, 2018

The easiest way to do conversion is using the base-10 string:

// bn => BigNumber
let u = ethers.utils.bigNumberify(someBn.toString());

// BigNumber => bn
let v =  new bn(someBigNumber.toString());

Let me know if there are any issues with that. :)

@ricmoo
Copy link
Member

ricmoo commented Nov 15, 2018

@PierreJeanjacquot (just tagging you so you can see the above answer ;))

@PierreJeanjacquot
Copy link

@ricmoo ok thanks for your answer :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

No branches or pull requests

3 participants