DEV Community

Cover image for How to Create Bitcoin Blockchain Wallets using javascript?
Shantun Parmar
Shantun Parmar

Posted on

How to Create Bitcoin Blockchain Wallets using javascript?

Introduction :

The Bitcoin blockchain is one of the biggest and most impressive blockchains on the planet. It was planned principally to send Bitcoin, the cryptographic money. In this way, normally, to make a message in the Bitcoin blockchain, you should send some Bitcoins starting with one record then onto the next.

A Bitcoin wallet address is made out of 32 unique characters. It permits you to send and get Bitcoins. Your private key is a mysterious code related with your Bitcoin address that allows you to demonstrate your responsibility for Bitcoins connected with the location.

Let's start

What will be covered in this Blog

1.Generate Address Endpoint

2.Validate address

3.Address Balance Endpoint

First of all you will need API for which you can create developer account on platform like block.io or blockcypher.com , Here i am using blockcypher API. Here you can use TESTNET (Testing) and MAINNET (Deployment).

Different Bitcoin addresses:

1.P2PKH which begins with the number 1,
for e.g.: 1PkCrXg22onNePtQmiepDspLJUv7CtkACx

2.P2SH type starting with the number 3,
for e.g.: 3PZjS8D9bA15km6vfHskNnPvHDL73QH2MN

3.Bech32 type starting with bc1, for e.g.:
bc1qGh7s6BYhqV1R3N1qdK8P1KNJjbqmRBs

1. Generate Address Endpoint

API Endpoints

MAINNET

https://api.blockcypher.com/v1/btc/main/addrs
Enter fullscreen mode Exit fullscreen mode

TESTNET

https://api.blockcypher.com/v1/btc/test3/addrs
Enter fullscreen mode Exit fullscreen mode

Make HTTP request

var xhr = new XMLHttpRequest();
xhr.open("POST", url);

xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {

      var data = JSON.parse(xhr.responseText);

   }};

xhr.send();
Enter fullscreen mode Exit fullscreen mode

OUTPUT :

{
"private": "81ee75559d37cbe4b7cbbfb9931ab1ba32172c5cdfc3ac2d020259b4c1104198",
"public": "0231ff9ec76820cb36b69061f6ffb125db3793b4aced468a1261b0680e1ef4883a",
"address": "mvpW7fMSi1nbZhJJDySNS2PUau8ppnu4kY",
"wif": "cRwGhRjCuuNtPgLcoYd1CuAqjFXCV5YNCQ1LB8RsFCvu61VfSsgR"
}
Enter fullscreen mode Exit fullscreen mode

See demo here , Check MAINNET and TESTNET address

2.Validate address

A valid Bitcoin address resembles a ledger number utilizing which you store your bitcoins and check your balances. If you got wrong address then you might lose your money. So before assiging any address make sure it is valid or not.

To validate address we will use a library named as wallet-address-validator.

Installation

NPM

npm install wallet-address-validator
Enter fullscreen mode Exit fullscreen mode

Browser

<script src="wallet-address-validator.min.js"></script>
Enter fullscreen mode Exit fullscreen mode

You may get js from here

Call below function to check validation

    var address = document.getElementById('addr').value;

    var valid = WAValidator.validate(address, 'bitcoin');
    if(valid)
        alert('This is a valid address');
    else
        alert('Address INVALID');
Enter fullscreen mode Exit fullscreen mode

*Note *: This validation will allow you to validate MAINNET

3. Address Balance Endpoint

API Endpoints

TESTNET

https://api.blockcypher.com/v1/btc/test3/addrs/{address}/balance
Enter fullscreen mode Exit fullscreen mode

MAINNET

https://api.blockcypher.com/v1/btc/test3/addrs/{address}/balance
Enter fullscreen mode Exit fullscreen mode

OUTPUT

{
  "address": "mx52XPqpzygzadkk8n1rCZPrC7B4h4UcwT",
  "total_received": 2404904,
  "total_sent": 0,
  "balance": 2404904,
  "unconfirmed_balance": 0,
  "final_balance": 2404904,
  "n_tx": 4,
  "unconfirmed_n_tx": 0,
  "final_n_tx": 4
}
Enter fullscreen mode Exit fullscreen mode

You can track down all the code in my GitHub Repository. Drop a star on the off chance that you think that its helpful.

We will continue transactions in next article.

Thank you & Take care!!

Top comments (0)