Creating Testnet Bitcoin Transaction with bitcoinjs-lib

Creating Testnet Bitcoin Transaction with bitcoinjs-lib

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.setVersion(1);
  transaction.addInput(input.sender_prev_txid, 0);
  transaction.addOutput(output.address, output.satoshi);
  transaction.sign(0, sender);
  console.log(transaction.build().toHex());
};

createTestnetTransaction(wif_1, input, output);

Using the following hex code:

010000000136ebe684ed9cc3566e1909bf2071c8add8764cc6fcc529e1af6e9beac712c9ef000000006b483045022100ce67916a053e94faf940e81ae665fc81daae226d1a70e7a5bc82adac302a290802207b564151889dd22b9e755297ca502b06f1345106296379762daeae2ac46d0af301210226a0c89db3a526fc5751606bf2592bd1477a4ac0d95616a4dd6b01ba65080a96ffffffff01c0b1c207000000001976a914545ad9c5df8da219b16f8c844498090aa88a764c88ac00000000

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

I couldn't find any formal examples of what I'm trying to accomplish in the documentation, just a bunch of unit tests. If someone could help point out what I'm doing wrong and help my complete the transaction that would be great, thanks!

https://ift.tt/2IysLPo

Comments

Popular posts from this blog

bitcoin node: what is the difference between simnet and regtest?

How to check if Electrum is masking my IP with the Tor proxy?

How generic miner connects to bitcoin or ethereum network and does it needs to store entire blockchain?