What is a Mnemonic Phrase?

A mnemonic phrase, also known as a seed phrase or recovery phrase, is a sequence of words that represents a wallet's private keys. It is a human-readable way to store and recover your cryptocurrency wallet. Typically, a mnemonic phrase consists of 12, 15, 18, 21, or 24 words.

Mnemonic phrases are generated using a standard called BIP-39 (Bitcoin Improvement Proposal 39), which ensures that the same phrase will always generate the same wallet.

Using Mnemonic Phrases with Ethers.js

You can use a mnemonic phrase in Ethers.js to create a wallet or restore an existing wallet. Below are the steps to use a mnemonic phrase with Ethers.js.

1. Set Up Your Project

If you haven't set up an Ethers.js project yet, you can follow these steps:

mkdir ethers-mnemonic-project
cd ethers-mnemonic-project
npm init -y
npm install ethers

2. Create an HTML File

Create an index.html file in your project directory. You can use the following code as a starting point:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Using Mnemonic Phrase with Ethers.js</title>
<script src="https://cdn.jsdelivr.net/npm/ethers@5.7.0/dist/ethers.umd.min.js"></script>
</head>
<body>
<h1>Create Wallet from Mnemonic Phrase</h1>
<input type="text" id="mnemonic" placeholder="Enter your mnemonic phrase" />
<button id="createWalletButton">Create Wallet</button>
<pre id="walletInfo"></pre>
<script>
document.getElementById('createWalletButton').onclick = () => {
const mnemonic = document.getElementById('mnemonic').value;
try {
// Create a wallet from the mnemonic phrase
const wallet = ethers.Wallet.fromMnemonic(mnemonic);

// Display the wallet address
document.getElementById('walletInfo').innerText =
'Address: ' + wallet.address;
} catch (error) {
document.getElementById('walletInfo').innerText =
'Error: ' + error.message;
}
};
</script>
</body>
</html>

3. Run a Local Server

To view your project in a browser, you’ll need to serve it over a local server. You can use a simple server like http-server. First, install it globally if you haven't already:

npm install -g http-server

Then, run the server in your project directory:

http-server

Open your web browser and navigate to http://localhost:8080 (or the port displayed in your terminal).

How It Works

In the provided code, when you enter your mnemonic phrase and click the "Create Wallet" button, the following happens:

  • The mnemonic phrase is retrieved from the input field.
  • A new wallet is created using ethers.Wallet.fromMnemonic(mnemonic).
  • The wallet's address is displayed on the page.
  • If the mnemonic phrase is invalid, an error message is shown instead.

Conclusion

You have successfully learned how to use a mnemonic phrase with Ethers.js! This allows you to create and manage wallets securely. Always ensure that your mnemonic phrase is kept secure and never shared with anyone, as it can be used to access your wallet and funds.