Search code examples
react-nativeethereumweb3js

Unable to call web3.eth.accounts.create() without remote debugger on react native app


I am building an android wallet using react-native and trying to create an account using web3.eth.accounts.create() . This works with react-native remote debugger enabled. However, when I work the same code with the debugger off, it stops working.

this is my package.json

"cryptico": "^1.0.2",
"native-base": "2.8.0",
"node-libs-browser": "2.1.0",
"react": "16.3.1",
"react-native": "0.55.4",
"react-native-firebase": "^5.2.0",
"react-native-gesture-handler": "^1.0.12",
"react-native-scripts": "1.14.0",
"react-navigation": "^3.0.9",
"socket.io-client": "2.0.4",
"web3": "1.0.0-beta.34"

Solution

  • I fixed it by using ethers.js to create new ethereum wallet.

    // this is specifically for react-native
    import 'ethers/dist/shims';
    import {ethers} from 'ethers';
    // to create a ethereum wallet
    let randomWallet = ethers.Wallet.createRandom();
    

    Web3.js is really troublesome for react-native
    read more here: https://docs.ethers.io/ethers.js/html/cookbook-react.html#

    this is a guide specifically designed for react-native