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

bn.toBuffer issue or make BN and Buffer available #227

Closed
Ayms opened this issue Aug 22, 2019 · 8 comments
Closed

bn.toBuffer issue or make BN and Buffer available #227

Ayms opened this issue Aug 22, 2019 · 8 comments

Comments

@Ayms
Copy link

Ayms commented Aug 22, 2019

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

@fanatid
Copy link
Collaborator

fanatid commented Aug 22, 2019

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.

@Ayms
Copy link
Author

Ayms commented Aug 22, 2019

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

@Ayms Ayms closed this as completed Aug 22, 2019
@fanatid
Copy link
Collaborator

fanatid commented Aug 22, 2019

How you count BN loading?

@Ayms
Copy link
Author

Ayms commented Aug 22, 2019

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)

@Ayms
Copy link
Author

Ayms commented Aug 22, 2019

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

@Ayms
Copy link
Author

Ayms commented Aug 24, 2019

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

@Ayms Ayms reopened this Aug 24, 2019
@fanatid
Copy link
Collaborator

fanatid commented Aug 25, 2019

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?

@Ayms
Copy link
Author

Ayms commented Aug 25, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants