Develop Bitcoin Wallet with bitcore

Overview

This time, Let's create a Bitcoin wallet using the bitcore library. This article will confirm for the basic operation(receive and send of bitcoin) that will be needed. By the way, You will use the main net to implement the network. If you don't want to implementation on main net, please use test net.

Setting of module

You will use the bitcoin-lib module to create private key, and the bitcore-explorers module to check the information of UTXO.

npm install bitcore-lib
npm install bitcore-explorers

Generate private key and address

It reads the module and generates the private key and Bitcoin address.

let bitcore = require('bitcore-lib');
let privateKey = new bitcore.PrivateKey();
let address = privateKey.toAddress();

console.log(privateKey.toString());
console.log(address.toString());

Check TXID and Balance

By checking the UTXO associated with the address you can check the balance.
A vout is an index number for an output in a transaction.

let explorers  = require('bitcore-explorers');
let insight = new explorers.Insight();

insight.getUnspentUtxos(address, (err,utxos)=>{
    if(err){
        console.log('UTXO processing error')
    }else{
        let balance = utxos.map((v)=>{
            return{
                txid:v.txId,
                vout: v.outputIndex,
                satoshi:v.satoshis,
                btc: v.satoshis * 1e-8,
            }
        })
        console.log(JSON.stringify(balance));
    }
}); 

Send your coin

You need to create a transaction when perform coin transfer processing. The transaction include that fee, Unspent Transaction Output, destination address, sent amount, receive address of change and private key.After creating the transaction, it broadcasting to the network.

let Transaction = bitcore.Transaction;
let sendAddress = "destination address";
let changeAddress = "receive address of change";
let sendAmout = Math.floor(parseFloat("send amout")*100000000);
let fee = parseFloat(1000);

insight.getUnspentUtxos(changeAddress, (err, utxos)=>{
    if(err){
        console.log('Bitcoin network connection error');
    }else{
        let transaction = new Transaction()
        .fee(fee)
        .from(utxos)
        .to(sendAddress, sendAmout)
        .change(changeAddress)
        .sign(privateKey);

        insight.broadcast(transaction, (err, returnedTxId)=>{
            if(err){
                console.log(err);
            }else{
                console.log(returnedTxId);
            }
        });
    }
});


The below's screen is actually state of send on the main network.
Since remittance process was successful, TXID(534a6f290...) is displayed.
f:id:adrenaline2017:20180904182038j:plain
※Please don't use ”privateKey(d1c88f9a...)” and "changeAddress(1G18w9uHE1m...)" displayed on the screen in consideration of security. The fund based on this private key is 0.


By checking the TXID at the time of remittance, you can check whether the process was really successful.
f:id:adrenaline2017:20180904194821j:plain




The complete code is released to Github.