Bitcoinjs-lib: moving bitcoins out of this bitcoinjs-lib address

Created on 15 Jan 2018  路  2Comments  路  Source: bitcoinjs/bitcoinjs-lib

Trying to do this. Seems more difficult than it should.
TransactionId required to make a transaction? This seems like the chicken before the egg.
How can I find the TXID, I checked the documentation and it's just showing hard coded values.

let send = (wif, toAddress, amount, txID) => {
    const keyPair = bitcoin.ECPair.fromWIF(wif)
    const txb = new bitcoin.TransactionBuilder()
      txb.addInput(txID , 0) 
      txb.addOutput(toAddress, amount) 
      txb.sign(0, keyPair)
      txb.build().toHex()
}

Where can I find txID?

how to / question / docs

Most helpful comment

https://testnet-api.smartbit.com.au/v1/blockchain/address/YOUR_ADDRESS/unspent?limit=1000

Where YOUR_ADDRESS is the address you are sending from.

response.unspent is an Array of objects.
The attributes you want are: txid, n and value_int.

Use txid and n to add the input.

txb.addInput(txid , n)

After which, the transaction now has value_int satoshis to add to outputs.
Track the sub-total of value_ints, e.g inputValue += unspent.value_int.

inputValue cannot be greater than the sum-total of your outputs.

fee = inputValue - outputValue

Where fee is the fee for miners.
Adjust your output values (or your input values!) to adjust your fee.

All 2 comments

https://testnet-api.smartbit.com.au/v1/blockchain/address/YOUR_ADDRESS/unspent?limit=1000

Where YOUR_ADDRESS is the address you are sending from.

response.unspent is an Array of objects.
The attributes you want are: txid, n and value_int.

Use txid and n to add the input.

txb.addInput(txid , n)

After which, the transaction now has value_int satoshis to add to outputs.
Track the sub-total of value_ints, e.g inputValue += unspent.value_int.

inputValue cannot be greater than the sum-total of your outputs.

fee = inputValue - outputValue

Where fee is the fee for miners.
Adjust your output values (or your input values!) to adjust your fee.

@junderw could we improve the examples do you think?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Mr-Mondragon picture Mr-Mondragon  路  3Comments

askucher picture askucher  路  3Comments

ishwarchandratiwari picture ishwarchandratiwari  路  3Comments

thrastarson picture thrastarson  路  3Comments

coingeek picture coingeek  路  4Comments