簡體   English   中英

當我調用 ethers.Wallet.fromMnemonic() [Ethers js 5.5.1] 時反應本機慢/凍結

[英]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-walletbip39ethers來創建錢包。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM