Skip to content

Commit eadbe69

Browse files
authored
Fix locked change output-bug (cryptoadvance#2485)
1 parent aac086b commit eadbe69

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/cryptoadvance/specter/wallet/wallet.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -712,19 +712,33 @@ def check_utxo(self):
712712
tx_copy["locked"] = txid_vout in locked_utxo_dict.keys()
713713

714714
# Adding the more complicated stuff address and amount
715-
if utxo != {}: # an unlocked one with values!
715+
if utxo != {}: # an unlocked output
716716
tx_copy["amount"] = utxo.get("amount")
717717
tx_copy["address"] = utxo["address"]
718-
else:
718+
else: # a locked output
719+
# In the case of locked outputs, the listlockunspent call does not contain reasonable UTXO data,
720+
# so we need to get the data from the original transaction.
719721
tx_from_core = self.rpc.gettransaction(tx_copy["txid"])
720-
# in the case of locked amounts, the stupid listlockunspent call does not contain reasonable utxo-data
721-
searched_vout = [
722-
_tx
723-
for _tx in tx_from_core["details"]
724-
if _tx["vout"] == utxo_vout
725-
][0]
726-
tx_copy["amount"] = searched_vout["amount"]
727-
tx_copy["address"] = searched_vout["address"]
722+
searched_vout = next(
723+
(
724+
_tx
725+
for _tx in tx_from_core["details"]
726+
if _tx["vout"] == utxo_vout
727+
),
728+
None,
729+
)
730+
731+
if searched_vout:
732+
tx_copy["amount"] = searched_vout["amount"]
733+
tx_copy["address"] = searched_vout["address"]
734+
else:
735+
# Sometimes gettransaction doesn't include all outputs (for example it does not include change outputs).
736+
# In this case, we get the raw transaction and decode it using embit to get the additional data we need.
737+
raw_transaction_hex = tx_from_core["hex"]
738+
parsed_transaction = self.TxCls.from_string(raw_transaction_hex)
739+
out = parsed_transaction.vout[utxo_vout]
740+
tx_copy["amount"] = round(out.value * 1e-8, 8)
741+
tx_copy["address"] = out.script_pubkey.address(self.network)
728742

729743
# Append the copy to the _full_utxo list
730744
_full_utxo.append(tx_copy)

0 commit comments

Comments
 (0)