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.
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 toundefined
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.