Created
August 16, 2017 03:11
-
-
Save yfgeek/de5d5de7b6be04d5e15e0bfc26098806 to your computer and use it in GitHub Desktop.
Bitcoin testnet transaction example when using browser
This file contains hidden or 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
// For browser | |
$.getJSON("https://chain.so/api/v2/get_tx_unspent/BTCTEST/"+bitcoinAddress,function(result){ | |
var last = result.data.txs.length - 1; | |
console.log('Current:' + last + ' ' + address); | |
var unspent_txid = result.data.txs[last].txid; | |
var unspent_vout = result.data.txs[last].output_no; | |
txb = new Bitcoin.TransactionBuilder(network); | |
txb.addInput(unspent_txid, unspent_vout); | |
value = Number(result.data.txs[last].value * 100000000); | |
pay = 0.005 * 100000000; | |
change = parseInt(value - pay); | |
var commit = new Buffered("Test"); | |
var dataScript = Bitcoin.script.nullData.output.encode(commit); | |
txb.addOutput(dataScript, 0); | |
txb.addOutput(address,change); | |
txb.sign(0, keyPair); | |
var txRaw = txb.build(); | |
var txHex = txRaw.toHex(); | |
console.log('hex',txHex); | |
postdata = { tx_hex : txHex }; | |
postTransaction(itemid); | |
}); | |
function postTransaction(itemid){ | |
$.post("https://chain.so/api/v2/send_tx/BTCTEST/",postdata,function(result){ | |
if(result.status ==="fail"){ | |
setTimeout(function(){ | |
makeTransaction(postTransaction()); | |
},5000); | |
}else{ | |
console.log(result.data.txid); | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment