Using a mnemonic phrase, we'll walk through how to create Bitcoin addresses that are ready to receive funds. If you’ve got your mnemonic phrase handy, let’s get started!
My blog post about it: https://dev.to/mibii/from-mnemonic-to-bitcoin-addresses-in-javascript-1dmp
Address Generation
First things first, we'll be using a mnemonic phrase to generate Bitcoin addresses. This phrase isn't just any random collection of words – it follows specific rules and standards to ensure security and compatibility.
Here's a step-by-step guide and the corresponding JavaScript code to generate three Bitcoin addresses.
Create a new folder for your project.
Initialize Your Project (create a package.json file.):
npm init -y
Install Dependencies ( You'll need dotenv for environment variables, bip39 for mnemonic phrase handling, hdkey for hierarchical deterministic key generation, and bitcoinjs-lib for Bitcoin address creation.):
npm install dotenv bip39 hdkey bitcoinjs-lib
Create Your Script:
Make a new file, let's call it AddressGenerator.js.
Copy and paste the provided below code snippet into this file.
For simplicity, we’ll store our mnemonic phrase in an .env file.
Make sure your .env file contains your mnemonic phrase like so:
create the .env file and place to there next string:
MNEMONIC="your mnemonic phrase here"
JavaScript Code to Generate Bitcoin Addresses
Here's the JavaScript code that reads the mnemonic phrase from the .env file, derives the seed, and generates three Bitcoin addresses:
AddressGenerator.js. (this file will be inside the downloaded archive)
When you run this script,
node AddressGenerator.js
you'll get three pairs of Bitcoin addresses and their corresponding private keys in hexadecimal format.
Next let's your private keys looks more user-friendly and compatible with most Bitcoin wallets - Converting Private Keys to WIF Format
Converting Private Keys to WIF Format
To make your private keys more user-friendly and compatible with most Bitcoin wallets, you can convert them from hexadecimal format to Wallet Import Format (WIF).
Create one more file and name it - wifFormatconverter.js
Here's a snippet to do just that:
wifFormatconverter.js (this file will be inside the downloaded archive)
Done. Before run this scripte - put to this const privateKeyHex = the HEX format of your privateKey that you get on the previous step. Or create one more env variable in your .env file. And run script:
node wifFormatconverter.js
Important Tips: BIP39 and Mnemonic Phrase Validation
In the downloaded archive code snippets Mentioned upper mnemonic validation considered.