cover-img

Creating a Bitcoin wallet with NodeJS

9 April, 2023

7

7

0

We're going to look at something a little bit different in this article, We are going to create our own Bitcoin wallet that will be used for both the Testnet and Mainnet.

With Nodejs we can write a program that will allow us generate a bitcoin wallet for you to receive your Bitcoin, but public and private key pairs.

If you have some actual Bitcoin that you want to receive and you're not especially tech savvy, I'd still advice that you use a third-party wallet application, so you won't lose your crypto. This is for practice purposes.

Prerequisites

In order to follow along with this tutorial, obviously you'd need to have Nodejs installed properly, it won't take any time at all, all you need is to head over to the nodejs download site and download the latest version.

Now that the installation part is settled...

let's code!

There are three types of bitcoin address that we have currently, the widely accepted address format is the P2PKH

The other formats include P2SH and Bech32

Let's create a new directory

mkdir btc-wallet

This command will create a folder in the directory, I prefer to use git bash, but you can use command line as well.

Once that's done, it's a simple command really, you can go ahead and change the directory by typing the following into your command line

cd btc-wallet

The cd means to change directory into the folder that you created earlier.

Initialize and Install

Now that we've gotten that out of the way, time to initialize the project.

npm init

Next we are to install all necessary dependencies for our wallet to function properly.

npm install bip39 bip32 bitcoinjs-lib --save

npm install tiny-secp256k1 --save

Next create your file using command line by typing

touch btcWallet.js

open VS code by type code . or just use any text editor you're comfortable with.

in btcwallet.js paste this code in:


const {BIP32Factory} = require('bip32')
const ecc = require('tiny-secp256k1')
const bip32 = BIP32Factory(ecc)
const bip39 = require('bip39')
const bitcoin = require('bitcoinjs-lib')

const network = bitcoin.networks.bitcoin

const path = `m/44'/0'/0'/0`

let mnemonic = bip39.generateMnemonic()
const seed = bip39.mnemonicToSeedSync(mnemonic)
let root = bip32.fromSeed(seed, network)

let account = root.derivePath(path)
let node = account.derive(0).derive(0)

let btcAddress = bitcoin.payments.p2pkh({
    pubkey: node.publicKey,
    network: network,
}).address

console.log(` Wallet generated: - Address : ${btcAddress}, - Key : ${node.toWIF()}, - Mnemonic : ${mnemonic} `)


Let's explain the code, yeah?

the first code blocks imports the required dependencies; bip32, bip39, tiny-secp256k1, and bitcoinjs-lib.

The second code block defines the bitcoin network, without connecting to the bitcoin network, our wallet will not function. Use networks.testnet if you want to connect to the testnet network.

The next code block would be a derivation path is a piece of data which tells a Hierarchical Deterministic wallet how to derive a specific key. In our case it would be m/44'/0'/0'/0 you can use m/44'/1'/0'/0 for the testnet.

the next code block derives the mnemonic for your wallet address which is just basically random words for the protection of your wallet.

Now that we know what how our code works, let's run it and see if we have the proper results.

node btcWallet.js to run your program

Here are my results:


Wallet generated:

- Address : 1MEupnN8UaE5wTSgUCrvUBRxL7pP6sPWue,
- Key : L53QRtoDD5kqKFq7cEYtE6uAzjpaRdew44h1KcGmRUJfxoN8GTv8,
- Mnemonic : swarm loud slot flame sea offer polar shy vapor tonight inform deba
te

Conclusion

And there we have it fellow devs, we have worked on our bitcoin wallet with nodejs, ensure that all of the libraries are properly installed and imported. Most functions will not operate without it.

Let's connect on twitter, I'm mostly active on the platform, and would love to talk to you guys.

7

7

0

Okuwobi Oluwatise
Software Engineer | Technical Writer | Developer Advocate | Web3 Enthusiast

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.