I want to sign btc transaction but when I want to broadcast trx to network even mainnet nor testnet, with both networks I got the same error.: bad-txns-inputs-missingorspent "error": { "code": -25, "message": "bad-txns-inputs-missingorspent" }, UTXOS of the account.
{ "address": "bc1qfhndl48dpng7vlwltmyq7ulnggk68nzcjkcj5z", "total_received": 459522, "total_sent": 0, "balance": 459522, "unconfirmed_balance": 0, "final_balance": 459522, "n_tx": 2, "unconfirmed_n_tx": 0, "final_n_tx": 2, "txrefs": [ { "tx_hash": "9d8c4d07f003ba9854e0c90adbd2c5e0d3bb60cb81a818b727d4a67675902173", "block_height": 718010, "tx_input_n": -1, "tx_output_n": 0, "value": 359522, "ref_balance": 459522, "spent": false, "confirmations": 3, "confirmed": "2022-01-10T11:49:43Z", "double_spend": false, "script": "00144de6dfd4ed0cd1e67ddf5ec80f73f3422da3cc58" }, { "tx_hash": "59de87c51b51ca0b5b1ecb8b3868319fcd0df58c40f61a5c7c66e2c7ae39dd66", "block_height": 717924, "tx_input_n": -1, "tx_output_n": 0, "value": 100000, "ref_balance": 100000, "spent": false, "confirmations": 89, "confirmed": "2022-01-09T22:41:15Z", "double_spend": false, "script": "00144de6dfd4ed0cd1e67ddf5ec80f73f3422da3cc58" } ] }
hex raw : 0100000000010166dd39aec7e2667c5c1af6408cf50dcd9f3168388bcb1e5b0bca511bc587de5902000000000000000002204e000000000000160014e0d31f509e0939975bcb41ca32e03da24f82828432350100000000001600144de6dfd4ed0cd1e67ddf5ec80f73f3422da3cc580248304502210080c0035b0a0128b82a2a20851204a5d039d9778a16e4bec8cbdd8fc744e9b29402202bc5bbaba90c146f213409feaec0671b62ecc3001b47f11fc3686332c3578df101210308dd04bcd865b7219fe683ff3a94470f99222686c28caa91a8d775b06da669b700000000
decoded trx:
{ "txid": "0ff84c4e6bebfa77d009f8f978668e2cbe7eebb52b5a7328af7393e3dea45d3d",
"hash": "e080aa3cf21c780c0cc34a4ef5ba35589f964a9329a519906673ec24a1b373e3",
"version": 1,
"size": 223,
"vsize": 141,
"weight": 562,
"locktime": 0,
"vin": [ {
"txid": "59de87c51b51ca0b5b1ecb8b3868319fcd0df58c40f61a5c7c66e2c7ae39dd66",
"vout": 2,
"scriptSig": { "asm": "", "hex": "" },
"txinwitness": [ "304502210080c0035b0a0128b82a2a20851204a5d039d9778a16e4bec8cbdd8fc744e9b29402202bc5bbaba90c146f213409feaec0671b62ecc3001b47f11fc3686332c3578df101", "0308dd04bcd865b7219fe683ff3a94470f99222686c28caa91a8d775b06da669b7" ],
"sequence": 0 } ],
"vout": [ {
"value": 0.0002,
"n": 0,
"scriptPubKey": {
"asm": "0 e0d31f509e0939975bcb41ca32e03da24f828284",
"hex": "0014e0d31f509e0939975bcb41ca32e03da24f828284",
"address": "bc1qurf375y7pyuewk7tg89r9cpa5f8c9q5y7dagzv",
"type": "witness_v0_keyhash" }
}, { "
value": 0.00079154,
"n": 1,
"scriptPubKey": {
"asm": "0 4de6dfd4ed0cd1e67ddf5ec80f73f3422da3cc58",
"hex": "00144de6dfd4ed0cd1e67ddf5ec80f73f3422da3cc58",
"address": "bc1qfhndl48dpng7vlwltmyq7ulnggk68nzcjkcj5z",
"type": "witness_v0_keyhash" }
}
] }
The func I used for sign btc signner:
private fun signBTC() {
if (::btcTransactionFee.isInitialized.not()) return
val amount= BigDecimal(getExtAmount()).multiply(BigDecimal(1E8)).toLong()
val coinBtc: CoinType = CoinType.BITCOIN
val toAddress=getExtRecipientAddress()
val script = BitcoinScript.lockScriptForAddress(coinKey.address, coinBtc).data()
var i =0;
val input = Bitcoin.SigningInput.newBuilder().apply {
this.amount = amount
this.hashType = BitcoinSigHashType.ALL.value()
this.toAddress = toAddress
this.changeAddress = coinKey.address
this.byteFee = 6
this.coinType = coinBtc.value()
this.addPrivateKey(coinKey.privateKey)
}
for (buff in btcUtxos.items) {
i += 1;
val txHash = buff.txHash.toHexBytes()
txHash.reverse()
val outPoint = Bitcoin.OutPoint.newBuilder().apply {
this.hash = ByteString.copyFrom(txHash)
this.index = i
}.build()
val utxo = Bitcoin.UnspentTransaction.newBuilder().apply {
this.amount = buff.value.toBigDecimal().toLong()
this.outPoint = outPoint
this.script = ByteString.copyFrom(script)
}.build()
input.addUtxo(utxo);
}
val plan = AnySigner.plan(input.build(), coinBtc, Bitcoin.TransactionPlan.parser())
input.plan = plan
input.amount = plan.amount
val output = AnySigner.sign(input.build(), coinBtc, Bitcoin.SigningOutput.parser())
val encoded = output.encoded
val signedTransaction = Numeric.toHexString(encoded.toByteArray()).substring(2)
btcViewModel.sendSignedTransaction(signedTransaction)
}