@@ -712,19 +712,33 @@ def check_utxo(self):
712
712
tx_copy ["locked" ] = txid_vout in locked_utxo_dict .keys ()
713
713
714
714
# Adding the more complicated stuff address and amount
715
- if utxo != {}: # an unlocked one with values!
715
+ if utxo != {}: # an unlocked output
716
716
tx_copy ["amount" ] = utxo .get ("amount" )
717
717
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.
719
721
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 )
728
742
729
743
# Append the copy to the _full_utxo list
730
744
_full_utxo .append (tx_copy )
0 commit comments