Skip to content

Instantly share code, notes, and snippets.

@JackTanRoo
Created August 24, 2014 23:37
Show Gist options
  • Save JackTanRoo/7acb553d8bbb633a9057 to your computer and use it in GitHub Desktop.
Save JackTanRoo/7acb553d8bbb633a9057 to your computer and use it in GitHub Desktop.
sending testnet coins from one address to another. Problem: dont know how to generate ecKey using new bitcoinjs api
var bitcoin = require('bitcoinjs-lib')
var helloblock = require('helloblock-js')({
network: 'testnet',
debug: true
})
//source btc account testnet
var sourcePrivateKeyWIF = '91fKWDmVZz7y7SyM5fLUBTuK2eUSYd7wxkwujQ7YLbNxNQU4LiW'; //balance 0.051 BTC
var sourceBTCAddress = 'migNkRq9gLmdPhgFfBBUiWaZf17uFE87yq';
// @SIDA, I think to be missing a ecKey - > what is this key? what is it used for?
//destination private keys and address
var key = bitcoin.ECKey.makeRandom();
var destPrivateKeyWIF = key.toWIF(bitcoin.networks.testnet) //private key
var destBTCAddress = key2.pub.getAddress(bitcoin.networks.testnet).toString();
console.log('dest private key: ', destPrivateKeyWIF);
console.log('dest addres: ', destBTCAddress);
console.log("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
var txFee = 10000
var txTargetValue = 150000
helloblock.addresses.getUnspents(sourceBTCAddress, {
value: txTargetValue + txFee
}, function(err, res, unspents) {
if (err) throw new Error(err)
var tx = new bitcoin.Transaction()
var totalUnspentsValue = 0
unspents.forEach(function(unspent) {
tx.addInput(unspent.txHash, unspent.index)
totalUnspentsValue += unspent.value
})
tx.addOutput(destBTCAddress, txTargetValue)
var txChangeValue = totalUnspentsValue - txTargetValue - txFee
tx.addOutput(sourceBTCAddress, txChangeValue)
tx.ins.forEach(function(input, index) {
tx.sign(index, ecKey) //WHAT IS THE ECKEY? how do i get it?
})
var rawTxHex = tx.toHex() //changed from tx.serializeHex()
helloblock.transactions.propagate(rawTxHex, function(err, res, tx) {
if (err) throw new Error(err)
console.log('SUCCESS: https://test.helloblock.io/transactions/' + tx.txHash)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment