Transactions

使用 bitcoinjs-lib 創建測試網比特幣交易

  • May 21, 2018

我正在嘗試使用 bitcoinjs-lib npm 模組設置一個簡單的比特幣測試網交易。似乎我正在正確地建構交易,並且我收到了十六進制程式碼,但我認為由於某種原因我無法廣播我的交易。這是我的程式碼:

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);

使用以下十六進制程式碼:

010000000136ebe684ed9cc3566e1909bf2071c8add8764cc6fcc529e1af6e9beac712c9ef000000006b483045022100ce67916a053e94faf940e81ae665fc81daae226d1a70e7a5bc82adac302a290802207b564151889dd22b9e755297ca502b06f1345106296379762daeae2ac46d0af301210226a0c89db3a526fc5751606bf2592bd1477a4ac0d95616a4dd6b01ba65080a96ffffffff01c0b1c207000000001976a914545ad9c5df8da219b16f8c844498090aa88a764c88ac00000000

我收到錯誤消息:“驗證事務時出錯:事務 efc912c7ea9b6eafe129c5fcc64c76d8adc87120bf09196e56c39ced84e6eb36 由 a7f8e0b48c77b5c192f301d655dffcedee1bd8aac2a5a5a058bb9552b 的輸入 0 引用。”

如果有人可以幫助指出我做錯了什麼並幫助我完成交易,那就太好了,謝謝!

您嘗試花費的輸入似乎是tx 中的vout 1 。

您目前的程式碼嘗試花費已經花費的 vout 0。嘗試更改transaction.addInput(input.sender_prev_txid, 0);transaction.addInput(input.sender_prev_txid, 1);

引用自:https://bitcoin.stackexchange.com/questions/75369