indutny / bn.js

BigNum in pure javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bn.toBuffer issue or make BN and Buffer available

Ayms opened this issue · comments

Browserifying https://github.com/Ayms/bitcoin-transactions, everything going well except an unexpected "trivial" issue, calling BN outside of elliptic in https://github.com/Ayms/bitcoin-transactions/blob/master/src/keys.js (see the first function privatekeyderive)

This causes browserify to duplicate twice BN and a call to bn.toBuffer just throws because Buffer is undefined

Not using the latest bn.js version but looking at the code it does not throw now but is just ignored, so if I am correct the "problem" is still there

Suggested fix (or simple workaround instead of making everything available from ec):

if (Buffer===undefined) { Buffer=Uint8Array; };
I don't see what it could harm and to whatever else than Uint8Array Buffer could be derived from if undefined

Hey @Ayms , if there issues, can you paste commands for reproduce too? (and which result expected by you).

Instead bn.toBuffer('be',32) you can use: bn.toArrayLike(Buffer, 'be',32).

I do not think that use Uint8Array instead Buffer by default will good because Buffer have more methods than Uint8Array.

Hi @fanatid, indeed... toArrayLike works, but still BN is loaded twice, anyway closing it, thanks for the answer

How you count BN loading?

For now I don't know very well how browserify is working, usually I prefer to do that stuff myself, but this is working quite well so it seems useless to reinvent things, now browserify is including twice the BN code, probably because I am calling elliptic and separately BN (because BN is not available from EC)

Not clear? (or again asking for things that have a trivial solution or already existing :-) )

See again https://github.com/Ayms/bitcoin-transactions/blob/master/src/keys.js

I would like to do instead const BN=require('elliptic').bn or require('elliptic').utils.bn (I am not using the latest version) and normally browserify should load BN just once, seems to be a simple change, will try it and let you know

Hi @fanatid

Looks like the issue is on elliptic side, BN is called in utils, then in elliptic and is probably loaded twice (I removed BN from my code and browserify still includes twice the BN code)

It's not dramatic but probably it should be fixed

If this on elliptic then probably issue should be created in elliptic repo :)
But most probably that issue in browserify config. Can you paste commands for creating build for browser with browserify and how you counting number of BN instances in result?

Yes, closing this one and will reopen on elliptic, I don't think it's a browserify issue, please wait ~week, the whole code of bitcoin-transactions and browserification process will be released