[英]React Native slow / freeze whet i call ethers.Wallet.fromMnemonic() [Ethers js 5.5.1]
當我調用ethers.Wallet.fromMnemonic('....')
時,我的應用程序凍結了 5/10 秒,接下來是 go(速度很慢但可以工作)。 如果在模擬器上運行應用程序,工作正常,但在 android 或 ios 上我有這個問題。 我試圖按照https://docs.ethers.io/v5/cookbook/react-native/上的文檔進行操作,但我遇到了同樣的問題
import "react-native-get-random-values"
import "@ethersproject/shims"
import { ethers } from "ethers";
const account = ethers.Wallet.fromMnemonic(mnemonic);
您可以結合使用ethereumjs-wallet
、 bip39
和ethers
來創建錢包。
import * as Bip39 from 'bip39';
import { Wallet } from 'ethers';
import { hdkey } from 'ethereumjs-wallet';
/**
* @dev Create Wallet from Mnemonic
* @param mnemonic = Mnemonic phrase
* @param index = Account index
* @returns wallet
*/
const createWallet = async (mnemonic: string, index: number): Promise<Wallet> => {
const seed = await Bip39.mnemonicToSeed(mnemonic);
const hdNode = hdkey.fromMasterSeed(seed);
const node = hdNode.derivePath(`m/44'/60'/0'`)
// m/44'/60'/0'/0
const change = node.deriveChild(0);
// m/44'/60'/0'/0/{N}
const childNode = change.deriveChild(index);
const childWallet = childNode.getWallet();
const wallet = new Wallet(childWallet.getPrivateKey().toString('hex'));
return wallet
}
我已經在 IOS 和 Android 中進行了測試,並且能夠立即創建一個 HD 錢包。
要在React Native
環境中使用bip39
,您必須對bip39
應用一些補丁。 您可以在此處使用patch-package
參考MetaMask 移動應用程序如何應用這些補丁。 在我的補丁中,我從react-native-crypto
導入了randomBytes
而不是randombytes
模塊
var {randomBytes} = require('react-native-crypto')
我使用過的節點模塊版本,
bip39 = 2.6.0
ethers = 5.5.3
ethereumjs-wallet = 1.0.2
react-native-crypto = 2.2.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.