错误:尝试导入节点标准库模块“ crypto”。失败了

时间:2019-04-24 02:08:58

标签: javascript react-native node-modules expo ethereum

我打算使用ethereumjs-wallet。但是我决定使用ethereumjs-wallet-react-native,因为它用于网络。如果您查看the link,它会显示import两个模块。因此,我采用了两个模块,并尝试import进行操作,但是出现了问题。

引起错误的代码:

import { asyncRandomBytes } from 'react-native-secure-randombytes'
import safeCrypto from 'react-native-safe-crypto'

window.randomBytes = asyncRandomBytes
window.scryptsy = safeCrypto.scrypt

错误消息:

  

无法解析模块“ react-native-secure-randombytes”

我想解决这个问题,但是无法解决,所以我决定绕行。

已解决错误的代码:

import safeCrypto from "react-native-fast-crypto";
const { RNRandomBytes } = NativeModules;

window.randomBytes = RNRandomBytes.randomBytes;
window.scryptsy = safeCrypto.scrypt;

稍后可能会引起问题,但现在已解决。但是,当我们调用并执行ethereumjs-wallet-react-native.

时,还有另一个问题

错误消息:

  

位于“ node_modules / ethereumjs-wallet-react-native / index.js”的软件包   尝试导入节点标准库模块“ crypto”。它   失败,因为React Native不包含Node标准   图书馆。阅读更多   https://docs.expo.io/versions/latest/introduction/faq/#can-i-use-nodejs-packages-with-expo

所以我下载了crypto-browserify,但是没有用。

尝试解析的代码

import safeCrypto from "react-native-fast-crypto";
const { RNRandomBytes } = NativeModules;
import Wallet from "ethereumjs-wallet-react-native";
var crypto = require("crypto-browserify");

window.randomBytes = RNRandomBytes.randomBytes;
window.scryptsy = safeCrypto.scrypt;
...
 async componentDidMount() {
    const wallet = await Wallet.generate();
  }

这是package.json:

  "dependencies": {
    "crypto-browserify": "^3.12.0",
    "ethereumjs-wallet-react-native": "^0.6.7",
    "expo": "^32.0.0",
    "expokit": "32.1.1",
    "react": "16.5.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
    "react-native-fast-crypto": "^1.8.1",
    "react-native-randombytes": "^3.5.2"
  },
  "devDependencies": {
    "babel-preset-expo": "^5.0.0"
  },

“ ethereumjs-wallet-react-native / index.js”的代码位于我的文章the link上。

请帮助我们。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

react-native-randombytes被重新链接。

我曾经import crypto from "crypto";

重新创建成功的代码

import crypto from "crypto";
import safeCrypto from "react-native-fast-crypto";
import { asyncRandomBytes } from "react-native-secure-randombytes";
...
window.randomBytes = asyncRandomBytes;
window.scryptsy = safeCrypto.scrypt;