indutny / bn.js

BigNum in pure javascript
MIT License
1.19k stars 150 forks source link

Buffer variable overwrites global Buffer #259

Closed kyranjamie closed 3 years ago

kyranjamie commented 3 years ago

Edit: Duplicate of https://github.com/indutny/bn.js/issues/255


BN throws buffer undefined assertion errors in some browser-like environments, despite having Buffer defined as a global. I'm using bn in an electron app with strict security settings: nodeIntegration: false, contextIsolation: true, enabledRemoteModule: false etc.

With these settings require is disabled in the renderer thread, where bn.js is invoked. Because of this, Buffer needs to be polyfilled as a global.

The following code declares a variable that redeclares the globally defined Buffer type to undefined in the local scope. Then silently fails when it tries to tries to require in the try block.

https://github.com/indutny/bn.js/blob/6ef485dc30b002ec8100602998763fec8a1a3897/lib/bn.js#L51-L55

Not sure on the most appropriate fix, but something like this should work around the issue.

Buffer = window.Buffer || global.Buffer || require('buffer').Buffer;
fanatid commented 3 years ago

Closed in https://github.com/indutny/bn.js/pull/260