tradle / rn-nodeify

hack to allow react-native projects to use node core modules, and npm modules that use them
MIT License
615 stars 112 forks source link

problem using crypto + randombytes #55

Closed nomoreboredom closed 6 years ago

nomoreboredom commented 6 years ago

Hi there,

Thanks for the great repos. I am using your solution to run node core modules on a react native app, and this is my postinstall script ./node_modules/.bin/rn-nodeify --install --hack "crypto,stream,process,vm,process,buffer,url,assert,events,querystring"

and my shim looks like this:

if (typeof __filename === 'undefined') global.__filename = ''
if (typeof process === 'undefined') {
  global.process = require('process')
} else {
  const bProcess = require('process')
  for (var p in bProcess) {
    if (!(p in process)) {
      process[p] = bProcess[p]
    }
  }
}

process.browser = false
if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer

// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__
process.env['NODE_ENV'] = isDev ? 'development' : 'production'
if (typeof localStorage !== 'undefined') {
  localStorage.debug = isDev ? '*' : ''
}

And on my app, I have a call to eth-lib, which calls this func:

var random = function random(bytes) {
  var rnd = void 0;
  if (typeof window !== "undefined" && window.crypto && window.crypto.getRandomValues) rnd = window.crypto.getRandomValues(new Uint8Array(bytes));else if (typeof require !== "undefined") rnd = require("c" + "rypto").randomBytes(bytes);else throw "Safe random numbers not available.";
  var hex = "0x";
  for (var i = 0; i < bytes; ++i) {
    hex += ("00" + rnd[i].toString(16)).slice(-2);
  }return hex;
};

which returns to me '''typeerror undefined is not an object rnd[i]'''

Any idea what may be causing this? FYI, If I turn debug mode on, it works as expected since the window object must be available.

nomoreboredom commented 6 years ago

Also as it seems monolithlabs/rn-nodeify fork has browserify-sign and such in cmd file, however this repo does not

mvayngrib commented 6 years ago

@nomoreboredom good catch! We forgot to return the input array in getRandomValues. See this PR https://github.com/mvayngrib/react-native-crypto/pull/20/files

nomoreboredom commented 6 years ago

Thanks a lot for quick fix!! It works for me now :)

mvayngrib commented 6 years ago

@nomoreboredom great!