Generate a Bitcoin Public Address and Private Key

Use Bitcoinjs to Generate a New Bitcoin Address and Private Key

A tutorial that goes over how to generate a new bitcoin address and private key using Nodejs and Bitcoinjs.

This tutorial was first published to MLG Blockchain.

You must have node and npm downloaded on your computer. After this tutorial you should have both a public bitcoin address and private key saved as variables.

npm install bitcoinjs-lib

Download bitcoinjs library from your node terminal.

var bitcoin = require("bitcoinjs-lib")

Require bitcoinjs-lib.

var keyPair = bitcoin.ECPair.makeRandom();

Make variable for keyPair.

console.log(keyPair.getAddress());

Test address by logging address to console. A valid bitcoin address should be returned.

var address = keyPair.getAddress();

Save bitcoin address result to a variable.

console.log(keyPair.toWif());

Test private key by logging to console. A valid bitcoin private key should be returned.

var pkey = keyPair.toWIF();

Save bitcoin private key result to a variable.

By now you should have successfully saved a valid public address and private key as a variable in your node console. Email michael@blockchainedu.org if you have any questions.

MLG Blockchain is a blockchain development and consulting firm based in Toronto that is focused on building next generation applications using blockchain and smart contract technology. See more blockchain development tutorials at www.mlgblockchain.com.