dart-bitcoin / bitcoin_flutter

A dart Bitcoin library for Flutter.
MIT License
165 stars 123 forks source link

How to construct a transaction when there are multiple inputs? #48

Closed kamike closed 3 years ago

kamike commented 3 years ago

When there is only one input, I successfully transfer,But when Multiple inputs, the transfer will fail

"error": "Error validating transaction: Error running script for input 0 referencing 040bb44e5fca0c9a7794ef92d2577bdbe34f9e55e161c6dbb08977ba5b83a128 at 1: Script was NOT verified successfully.."

this is my unspent list: { "address": "2My58TUaybVThTdVCA4FiGq3yvApTLQiJbi", "total_received": 3339321, "total_sent": 427140, "balance": 2912181, "unconfirmed_balance": 0, "final_balance": 2912181, "n_tx": 22, "unconfirmed_n_tx": 0, "final_n_tx": 22, "txrefs": [ { "tx_hash": "d5e7e8f7892442d1825f05ac17c8c3a56b4ec00b00d42a685a1bac630edcab5c", "block_height": 1938859, "tx_input_n": -1, "tx_output_n": 1, "value": 1979000, "ref_balance": 4912181, "spent": false, "confirmations": 134, "confirmed": "2021-03-04T11:02:19Z", "double_spend": false }, { "tx_hash": "50bfbe5193e01ed86135af20ee11b7a74c779e204acea235af1ad0f9ea6f9807", "block_height": 1938850, "tx_input_n": -1, "tx_output_n": 0, "value": 10000, "ref_balance": 2933181, "spent": false, "confirmations": 143, "confirmed": "2021-03-04T09:59:30Z", "double_spend": false }, { "tx_hash": "cc2c197249dab3807a40a45cedfd15ad332d86958bee36d7180e324b339f3b51", "block_height": 1938839, "tx_input_n": -1, "tx_output_n": 1, "value": 3460, "ref_balance": 2929241, "spent": false, "confirmations": 154, "confirmed": "2021-03-04T08:16:58Z", "double_spend": false }, { "tx_hash": "3662c08c41a9c2a5d75a079e5a55680e3758956b91fe76f7dc89e93787a54ef8", "block_height": 1938839, "tx_input_n": -1, "tx_output_n": 1, "value": 6700, "ref_balance": 2935781, "spent": false, "confirmations": 154, "confirmed": "2021-03-04T08:16:58Z", "double_spend": false }, { "tx_hash": "d9d91e7f5509a11e83adba9a4602086ba735ebdd68e8fe7a67052e8a2f63b724", "block_height": 1938839, "tx_input_n": -1, "tx_output_n": 1, "value": 3900, "ref_balance": 2939081, "spent": false, "confirmations": 154, "confirmed": "2021-03-04T08:16:58Z", "double_spend": false }, { "tx_hash": "040bb44e5fca0c9a7794ef92d2577bdbe34f9e55e161c6dbb08977ba5b83a128", "block_height": 1938687, "tx_input_n": -1, "tx_output_n": 1, "value": 783489, "ref_balance": 1716810, "spent": false, "confirmations": 306, "confirmed": "2021-03-03T09:34:12Z", "double_spend": false }, { "tx_hash": "06a6f827a5a22dbdf9a283c9fa72e0e45c0c905376cd903201a3f3f7b2864100", "block_height": 1938684, "tx_input_n": -1, "tx_output_n": 1, "value": 125632, "ref_balance": 1084353, "spent": false, "confirmations": 309, "confirmed": "2021-03-03T09:00:06Z", "double_spend": false } ], "tx_url": "https://api.blockcypher.com/v1/btc/test3/txs/" }

This is my code,Can you help me??

for (var item in unspentList) { txb.addInput(item.tx_hash, item.tx_output_n); totalAmount += item.value; if (totalAmount >= outAmount + fee) { break; } } txb.addOutput(toAddress, outAmount); if (totalAmount > outAmount + fee) { txb.addOutput(fromAddress, totalAmount - outAmount - fee); } for (int index = 0; index < txb.inputs.length; index++) { txb.sign(vin: index, keyPair: p2shPair, redeemScript: p2wpkh.output, witnessValue: totalAmount); } print("${txb.build().toHex()}");