TypeError:null不是对象(评估“RNRandomBytes.seed”)React Native

Moh*_*med 5 cryptojs reactjs react-native web3js

正在使用 React Native 开发一个移动应用程序,其中涉及交互web3.js ,问题是 RN 不支持核心 Node.js 模块,所以我必须安装

npm i --save react-native-crypto

npm i --save react-native-randombytes

react-native link react-native-randombytes

npm i --save-dev rn-nodeify@latest

./node_modules/.bin/rn-nodeify --hack --install

现在每当我尝试使用时我都会遇到这个错误crypto or web3.js 在此输入图像描述

任何线索是什么问题或者如何解决它?

Ste*_*ros 1

这似乎是react-native-randombytes库的安装问题。

您是否没有考虑过使用不同的、更流行的提供相同 API 的库?

npm 表示,react-native-randombytes 每周的下载量为 19,294 次。另一个名为react-native-get-random-values 的库(每周下载量约为 cca 481,572)几乎可以保证正常工作(因为建议与 - uuid等软件包结合使用)。该库的 npm 链接位于此处

通过查看上面提到的两个库的源代码,它们都使用相同的 Android API,并由 SecureRandom 支持,因此我预计 iOS 上也有相似之处。

React-native-get-random-values(链接在这里):

@ReactMethod(isBlockingSynchronousMethod = true)
  public String getRandomBase64(int byteLength) throws NoSuchAlgorithmException {
    byte[] data = new byte[byteLength];
    SecureRandom random = new SecureRandom();

    random.nextBytes(data);

    return Base64.encodeToString(data, Base64.NO_WRAP);
  }
Run Code Online (Sandbox Code Playgroud)

react-native-randombytes 库 - 链接在这里

@ReactMethod
  public void randomBytes(int size, Callback success) {
    success.invoke(null, getRandomBytes(size));
  }

private String getRandomBytes(int size) {
    SecureRandom sr = new SecureRandom();
    byte[] output = new byte[size];
    sr.nextBytes(output);
    return Base64.encodeToString(output, Base64.NO_WRAP);
  }


Run Code Online (Sandbox Code Playgroud)

  • 在 node_module 'react-native-crypro' 内部,我现在已将行更改为: `import { randomBytes } from 'react-native-get-random-values'`,但现在我又回到了 `undefined not an对象评估 process.version.split`。我一直在绕这个圈子,它是如此令人困惑。如何使流程在本机反应中可用? (2认同)