Can someone please explain how I can send a bitcoin transaction using bitcoinjs??? I have setup two wallets using bitcoinjs.
I want to send 100000 satoshis from here: 1G4iprWu7Q8tNbQLA8UBM2GearcnzwFrxM
to here: 1HsrKvboax8J3X1sgsRdWybEwnUNWsDw4Y
In case it's needed here is the last transaction for 1G4iprWu7Q8tNbQLA8UBM2GearcnzwFrxM
The code I am using is from the bitcoinjs.org website:
var tx = new bitcoin.TransactionBuilder()
// Add the input (who is paying):
// [previous transaction hash, index of the output to use]
var txId = 'aa94ab02c182214f090e99a0d57021caffd0f195a81c24602b1028b130b63e31'
tx.addInput(txId, 0)
// Add the output (who to pay to):
// [payee's address, amount in satoshis]
tx.addOutput("1Gokm82v6DmtwKEB8AiVhm82hyFSsEvBDK", 15000)
// Initialize a private key using WIF
var privateKeyWIF = 'L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy'
var keyPair = bitcoin.ECPair.fromWIF(privateKeyWIF)
// Sign the first input with the new key
tx.sign(0, keyPair)
// Print transaction serialized as hex
console.log(tx.build().toHex())
// => 0100000001313eb630b128102b60241ca895f1d0ffca21 ...
// You could now push the transaction onto the Bitcoin network manually
// (see https://blockchain.info/pushtx)
Now I am assuming var txId
is the transaction id from the last transaction here
Is the `tx.addInput`` where I put the fee? If so is 100 enough?
tx.addOutput
is obvs so I am okay with that!
Is var privateKeyWIF*
where I put the private key from the sending address?
No idea what var keyPair
and tx.sign
do!
Anyone who can help tell me where the details are suppposed to go would be much appreciated! For this example pretend my private key for the sender address is 5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF.
Cheers
A Bitcoin transaction typically references previous transaction outputs as new transaction inputs.
First you need to look at your previous transaction. As you can see here or in the snippet above the txId is:
4303caa91ca5a2236396af3bfae26b70a223a2bcee2fe8d23a7b24626c3c4bd2
{
"hash":"4303caa91ca5a2236396af3bfae26b70a223a2bcee2fe8d23a7b24626c3c4bd2",
"ver":1,
"vin_sz":1,
"vout_sz":2,
"size":225,
"weight":900,
"fee":18713,
"relayed_by":"0.0.0.0",
"lock_time":448506,
"tx_index":7399058499716239,
"double_spend":false,
"time":1484590465,
"block_index":448507,
"block_height":448507,
"inputs":[
{
"sequence":4294967294,
"witness":"",
"script":"47304402204fdab6f26efa32f49c79d8c91b5d42ea39760cff5afc34351f8595453bd3c9d102201ff8e25e4ababe3c3515617e90f97516cda50061ad0c02f215e4a008d580ede4012102e801e52ff6e7d1ad0bb46ef6732d8fcfae1e122a47ea5690e084b9fa4a73d106",
"index":0,
"prev_out":{
"spent":true,
"script":"76a914d7819c081b247a146922b8f90f89181b02a3c66f88ac",
"spending_outpoints":[
{
"tx_index":7399058499716239,
"n":0
}
],
"tx_index":356328836300976,
"value":469871307,
"addr":"1LeVYfpp1LQQUBqsmruVW6cMbYrZtmunPr",
"n":1,
"type":0
}
}
],
"out":[
{
"type":0,
"spent":true,
"value":469352594,
"spending_outpoints":[
{
"tx_index":5608049158171792,
"n":0
}
],
"n":0,
"tx_index":7399058499716239,
"script":"76a9147adfb2779ee7710b0dffeff8fb77c82867ed43e588ac",
"addr":"1CChR9sShAc61MNLhMoLzy87w5DwP6jRvv"
},
{
"type":0,
"spent":true,
"value":500000,
"spending_outpoints":[
{
"tx_index":8253272257190039,
"n":0
}
],
"n":1,
"tx_index":7399058499716239,
"script":"76a914a53e0c670a2836baef1ea93de125f1fdd77370dd88ac",
"addr":"1G4iprWu7Q8tNbQLA8UBM2GearcnzwFrxM"
}
]
}
Your address 1G4iprWu7Q8tNbQLA8UBM2GearcnzwFrxM
shows up as the second output (index 1
).
And the total amount at this address is 500000 satoshis
.
So, lets create our transaction
var txb = new bitcoin.TransactionBuilder()
txb.addInput('4303caa91ca5a2236396af3bfae26b70a223a2bcee2fe8d23a7b24626c3c4bd2', 1)
Now we will send 100000 satoshis
txb.addOutput('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP', 100000)
We need to send the change to some address you own
txb.addOutput('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP', 398130)
What is left is the miner fee. You can use online services to estimate the ideal fee.
(in)500000 Satoshis - (out)100000 - (out)398130 = (fee)1870
Now we need to sign the transaction to confirm that you own the input address
var yourAddressPrivateKeyWIF = 'L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy'
var yourAddresskeyPair = bitcoin.ECPair.fromWIF(yourAddressPrivateKeyWIF)
txb.sign(0, yourAddresskeyPair)
If you run yourAddresskeyPair.getAdress()
the output should be your address 1G4iprWu7Q8tNbQLA8UBM2GearcnzwFrxM
Now you can send your transaction to the network. Get raw transaction in hex format:
txb.build().toHex()
Paste the result here https://blockchain.info/pushtx or check other solutions here: https://github.com/bitcoinjs/bitcoinjs-lib/issues/839
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With