I'm trying to setup a simple bitcoin testnet transaction using the bitcoinjs-lib npm module. It seems like I'm building the transaction correctly, and I receive the hex code but I think I'm unable to broadcast my transaction for some reason. Here is my code:

const address_1 = 'mzMVV43nRcdGdL9D6xuMez2GkHq8oN1965'; //balance: 130281960 satoshi
const address_2 = 'moCytanji9FNUsDJWVeDGdEFxtGT5psHM6'; //balance: 0 satoshi

const input = {
  sender_prev_txid: 'efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36'

const output = {
  address: address_2,
  satoshi: 130200000

const createTestnetTransaction = (wif, input, output) => {
  const transaction = new bitcoin.TransactionBuilder(bitcoin.networks.testnet);
  const sender = bitcoin.ECPair.fromWIF(wif, bitcoin.networks.testnet);

  transaction.addInput(input.sender_prev_txid, 0);
  transaction.addOutput(output.address, output.satoshi);
  transaction.sign(0, sender);

createTestnetTransaction(wif_1, input, output);

Using the following hex code:


I receive the error: 'Error validating transaction: Transaction efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36 referenced by input 0 of a7f8e0b48c77b5c192f301d655dffcedee1bd8aac2a5a058bb9552b4ff711002 has already been spent..'

If someone could help point out what I'm doing wrong and help my complete the transaction that would be great, thanks!

1 Answer 1


The input you are trying to spend appears to be vout 1 in the tx.

Your current code tries to spend vout 0, which has already been spent. Try changing transaction.addInput(input.sender_prev_txid, 0); to transaction.addInput(input.sender_prev_txid, 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.