Victor Yeo

Oct 25, 2021

2 min read

Ethereum wallet creation and recovery

In this article, we will discuss the code details in using ethereumjs-wallet to create a wallet account with seed phrase and using the seed phrase to recover the wallet account. The example is developed using React Native.

Part 1:

Firstly, to generate a wallet account, it is required to create the seed phrase using bip39 software. To create a 12 word seed phrase, we use bip39 generateMnemonic with parameter value of 128.

const seedPhrase = await bip39.generateMnemonic(128);

Secondly, we use hdkey from ethereumjs-wallet to create a HD wallet from the seed phrase.

const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(seedPhrase));

Thirdly, from the HD wallet we created, we get the wallet instance.

const wallet = hdwallet.derivePath(path).getWallet();

Note: Do NOT use the below to create wallet, this wallet instance created is not linked to seed phrase.

import Wallet from 'ethereumjs-wallet';
const wallet = Wallet.generate();

The full code to generate wallet account is as below.

import bip39 from 'react-native-bip39'
import { hdkey } from 'ethereumjs-wallet';
const seedPhrase = await bip39.generateMnemonic(128);
console.log(seedPhrase)
// generate hdwallet from seed phrase
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(seedPhrase));

// from BIP44, HD derivation path is:
// m / purpose’ / coin_type’ / account’ / change / address_index
const path = “m/44'/60'/0'/0/0”;
const wallet = hdwallet.derivePath(path).getWallet();
console.log(“privateKey: “ + wallet.getPrivateKeyString());
console.log(“address: “ + wallet.getAddressString());
// split seed phrase string to seed phrase array of strings
const seedPhraseList = seedPhrase.split(“ “);

Part 2:

To recover the wallet account from seed phrase, firstly, we need to test the seed phrase validity from the user input seed phrase (There will be a UI for user to input seed phrase, this is out of scope of this article).

const { isValidMnemonic } = ethers.utils;
isValidMnemonic(seedPhrase);

Next, we use the bip39 mnemonic to seed function to create the HD wallet. (In fact, it is the same function that is used in generate wallet account)

const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(seedPhrase));

The full code to recover wallet account from seed phrase is as below.

import { ethers } from 'ethers';
import bip39 from 'react-native-bip39';
import { hdkey } from 'ethereumjs-wallet';
const { isValidMnemonic } = ethers.utils;const validResult = isValidMnemonic(seedPhrase);// generate hdwallet from seed phrase
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(seedPhrase));
// from BIP44, HD derivation path is:
// m / purpose’ / coin_type’ / account’ / change / address_index
const path = “m/44'/60'/0'/0/0”;
const wallet = hdwallet.derivePath(path).getWallet();
console.log(`privateKey: ${ wallet.getPrivateKeyString()}`);
console.log(`address: ${ wallet.getAddressString()}`);

If there is pre-existing ethers or tokens in the recovered wallet account, it will be reflected in the recovered wallet account.

The same code can be applied to Ethereum and Binance smart chain. That’s it.