-
-
Save fhferreira/0599b22351f9bebc5312544ea82b5277 to your computer and use it in GitHub Desktop.
create bitcoin wallet, make a transaction on bitcoin network using bitcoinjs-lib and blockcypher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
const bitcoin = require('bitcoinjs-lib'); | |
var buffer = require('buffer'); | |
const bitcoinNetwork = bitcoin.networks.testnet; | |
var rootUrl = "https://api.blockcypher.com/v1/btc/test3"; | |
//create wallets | |
const TestNet = bitcoin.networks.testnet3; | |
let keyPair = bitcoin.ECPair.makeRandom({ network: TestNet }); | |
let publicKey = keyPair.publicKey.toString('hex'); | |
let {address} = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }); | |
let wif = keyPair.toWIF(); | |
var privateKey = bitcoin.ECPair.fromWIF(wif, TestNet); | |
console.log("Public: " + publicKey + " \nPrivate: " + privateKey + " \nAddress: " + address + " \nWIF: " + wif); | |
var source = { | |
public : <pub_key>, | |
address : <addr>, | |
wif : <wif>, | |
dest: <dest_addr> | |
} | |
var price_per_byte = 40; | |
//get balance of a wallet | |
request.get(rootUrl + "/addrs/" + source.address + "/balance",(error, response, body) => { | |
if(body.error || JSON.parse(body).errors){ | |
if(body.error) | |
console.log(body.error); | |
else console.log(JSON.parse(body).errors[0].error); | |
} else { | |
console.log(body); | |
let parsed_body = JSON.parse(body); | |
let balance = parsed_body.balance; | |
let unconfirmed_balance = parsed_body.unconfirmed_balance; | |
if(unconfirmed_balance < 0) | |
balance = balance + unconfirmed_balance; | |
console.log("balance : " + balance); | |
} | |
} | |
); | |
// send token from one wallet to another | |
var key = bitcoin.ECPair.fromWIF(source.wif, bitcoinNetwork); | |
request.get(rootUrl + "/addrs/" + source.address + "?unspentOnly=true",(error, response, body) => { | |
if(body.error || JSON.parse(body).errors){ | |
if(body.error) | |
console.log(body.error); | |
else console.log(JSON.parse(body).errors[0].error); | |
} else { | |
let parsed_body = JSON.parse(body); | |
let balance = parsed_body.balance; | |
let unconfirmed_balance = parsed_body.unconfirmed_balance; | |
if(unconfirmed_balance < 0) | |
balance = balance + unconfirmed_balance; | |
var tx = new bitcoin.TransactionBuilder(bitcoinNetwork); | |
let txs = JSON.parse(body).txrefs; | |
if (balance > 0 && txs) { | |
txs.forEach(function(txn) { | |
console.log(txn); | |
tx.addInput(txn.tx_hash, txn.tx_output_n); | |
}); | |
request.get("https://bitcoinfees.earn.com/api/v1/fees/recommended",(error, response, body) => { | |
try{ | |
if(!error) | |
price_per_byte = JSON.parse(body).halfHourFee; | |
} catch(ex){ | |
console.log("unable to find fee, setting default as 10satoshi per byte"); | |
} | |
console.log("price_per_byte : " + price_per_byte); | |
let fee = (txs.length * 148 + 1 * 34 + 10) * price_per_byte;//1 is the no of outputs | |
let amount_to_transfer = balance - fee; | |
console.log("fee : " + fee +"\nBalance : "+amount_to_transfer); | |
tx.addOutput(source.dest, amount_to_transfer); | |
let txn_no = txs.length; | |
while(txn_no > 0){ | |
tx.sign(txn_no-1, key); | |
txn_no--; | |
} | |
let tx_hex = tx.build().toHex(); | |
request.post(rootUrl + "/txs/push",{json :{tx: tx_hex}}, (error, response, body) => { | |
if(body.error) | |
console.log(body.error); | |
else | |
console.log(body.tx.hash); | |
}); | |
}); | |
} else console.log("Not enough balance : " + balance); | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment