tradle / rn-nodeify

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

not supported by this browser #58

Closed fangwa closed 6 years ago

fangwa commented 6 years ago

hi, thanks for your repo. I want to use bitcoinjs-lib in my reactnative project, so i use your repo, when i call ECPair.makeRandom method in android, a Warning massage display. screenshot_2017-12-30-19-59-39

When I was Debug JS Remotely in chrome, there is no such problem.

mvayngrib commented 6 years ago

@fangwa react-native uses your desktop browser's JS engine when you Debug JS remotely, so some things will be different. Are you on an old version of android?

fangwa commented 6 years ago

@mvayngrib Thank you for your reply so soon, my device's android version is 5.1.1. Have a requirement on the android version? qq 20171230231008

mvayngrib commented 6 years ago

@fangwa first see https://github.com/mvayngrib/rnctest1 for a working example that uses randomBytes. See if it works for you. If it does, maybe you can figure out what's different in your setup, e.g. you forgot to import './shim.js or forgot to re-run the rn-nodeify script after installing another dependency.

fangwa commented 6 years ago

i find the error was thrown by module https://github.com/crypto-browserify/randombytes.

function oldBrowser () {
  throw new Error('secure random number generation not supported by this browser\nuse chrome, FireFox or Internet Explorer 11')
}

var Buffer = require('safe-buffer').Buffer
var crypto = global.crypto || global.msCrypto

if (crypto && crypto.getRandomValues) {
  module.exports = randomBytes
} else {
  module.exports = oldBrowser
}

and my shim.js has less code than your file below,it works for me after i copy this code,thank you very much.

if (require('./package.json').dependencies['react-native-crypto']) {
    // important that this comes before require('crypto')
    const algos = require('browserify-sign/algos')
    if (!algos.sha256) {
        algos.sha256 = {
            "sign": "ecdsa",
            "hash": "sha256",
            "id": new Buffer("")
        }
    }

    let crypto
    if (typeof window === 'object') {
        if (!window.crypto) window.crypto = {}
        crypto = window.crypto
    } else {
        crypto = require('crypto')
    }

    if (!crypto.getRandomValues) {
        crypto.getRandomValues = getRandomValues
    }

    let randomBytes

    function getRandomValues (arr) {
        if (!randomBytes) randomBytes = require('react-native-randombytes').randomBytes

        const bytes = randomBytes(arr.length)
        for (var i = 0; i < bytes.length; i++) {
            arr[i] = bytes[i]
        }
    }
}