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

[Electron] TypeError: Cannot read property 'randomBytes' of undefined #27

Closed
spkjp opened this issue Jul 24, 2019 · 7 comments
Closed

Comments

@spkjp
Copy link

spkjp commented Jul 24, 2019

We run into the following issue when using bcrypto 4.1.0 inside Electron:

    TypeError: Cannot read property 'randomBytes' of undefined

      42 |    */
      43 |   static getAddress (passphrase, pubKeyHash) {
    > 44 |     return Identities.Address.fromPassphrase(this.normalizePassphrase(passphrase), pubKeyHash)
         |                               ^
      45 |   }
      46 | 
      47 |   static getAddressFromPublicKey (publicKey, pubKeyHash) {

      at Object.randomBytes (node_modules/bcrypto/lib/native/random.js:20:18)
      at Function.randomBits (node_modules/bcrypto/lib/js/bn.js:3459:25)
      at Function.random (node_modules/bcrypto/lib/js/bn.js:3502:22)
      at ShortPoint._getBlinding (node_modules/bcrypto/lib/js/curves.js:785:24)
      at ShortPoint.precompute (node_modules/bcrypto/lib/js/curves.js:731:40)
      at SECP256K1.precompute (node_modules/bcrypto/lib/js/curves.js:160:12)
      at ECDSA.get curve [as curve] (node_modules/bcrypto/lib/js/ecdsa.js:52:19)
      at ECDSA.publicKeyCreate (node_modules/bcrypto/lib/js/ecdsa.js:228:20)
      at Function.fromPrivateKey (node_modules/@arkecosystem/crypto/src/identities/keys.ts:18:34)
console.log(process.versions)
 {
        node: '12.3.1',
        v8: '7.4.288.27-node.18',
        uv: '1.29.1',
        zlib: '1.2.11',
        brotli: '1.0.7',
        ares: '1.15.0',
        modules: '72',
        nghttp2: '1.38.0',
        napi: '4',
        llhttp: '1.1.3',
        http_parser: '2.8.0',
        openssl: '1.1.1b',
        cldr: '35.1',
        icu: '64.2',
        tz: '2019a',
        unicode: '12.1'
      }

From what I can tell loading the native module succeeds:
https://github.com/bcoin-org/bcrypto/blob/master/lib/random.js#L10

but calling randomBytes fails because crypto is missing. Manually forcing to ./node/random resolves it (can we get an environment variable for it? 😛 ).

I also tried a npm rebuild without success.

Many thanks in advance.

@pinheadmz
Copy link
Member

Possible duplicate issue: #15

Looks like the recommendation for now is to fall back to the JavaScript methods.

@pinheadmz
Copy link
Member

pinheadmz commented Jul 24, 2019

The environment variable is NODE_BACKEND=<js|node>

@spkjp
Copy link
Author

spkjp commented Jul 24, 2019

Thanks for the reply. I tried playing around with NODE_BACKEND=js before and the issue still persists:

    console.log node_modules/bcrypto/lib/native/binding.js:9
      js
    console.log node_modules/bcrypto/lib/native/binding.js:11
      boom
    console.log node_modules/bcrypto/lib/random.js:11
      all ok

In other words, it throws the exception here:
https://github.com/bcoin-org/bcrypto/blob/master/lib/native/binding.js#L9

but does not enter the catch block here:
https://github.com/bcoin-org/bcrypto/blob/master/lib/random.js#L12

@chjj
Copy link
Member

chjj commented Jul 24, 2019

      at Object.randomBytes (node_modules/bcrypto/lib/native/random.js:20:18)
      at Function.randomBits (node_modules/bcrypto/lib/js/bn.js:3459:25)

This stack trace is confusing. Why does electron succeed at dynamically loading the binding? The random object is always on the exposed binding no matter what. Is electron does giving us an empty object for compatibility purposes?

but does not enter the catch block here

@supaiku0, what about NODE_BACKEND=node. Does that give any better results?

We only fall back to the JS random backend in the browser (which uses getRandomValues() instead of the crypto module).

I'm still fuzzy on what electron does/doesn't support and there doesn't seem to be a simple writeup anywhere. Personally I prefer not to touch electron, so I'm basically shooting in the dark here.

@chjj
Copy link
Member

chjj commented Jul 24, 2019

Also, is there an easy way to detect electron? Do they expose a property on the process object or something?

@tynes
Copy link
Member

tynes commented Jul 25, 2019

It looks like process.versions.electron is set when running in electron.

electron/electron#2288 (comment)

@spkjp
Copy link
Author

spkjp commented Jul 26, 2019

@chjj @tynes This is a bit embarassing, but it's not actually Electron at fault here. I was only able to reproduce the error when running unit tests via jest and for some reason I mixed it up with Electron. I can no longer reproduce it after I wiped everything on my Linux system. It must have been a local issue on my side.

Sorry, for the confusion!

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

4 participants