@@ -8,7 +8,7 @@ use Types::Common -sigs;
88use List::Util qw( any) ;
99use Scalar::Util qw( blessed) ;
1010
11- use Bitcoin::Crypto qw( btc_transaction) ;
11+ use Bitcoin::Crypto qw( btc_transaction btc_utxo ) ;
1212use Bitcoin::Crypto::PSBT::Map;
1313use Bitcoin::Crypto::PSBT::Field;
1414use Bitcoin::Crypto::PSBT::FieldType;
@@ -354,6 +354,41 @@ sub get_locktime
354354 }
355355}
356356
357+ sub _get_utxo
358+ {
359+ my ($self , $input_index , $txid , $output_index ) = @_ ;
360+
361+ my $non_witness_utxo = $self -> get_all_fields(' PSBT_IN_NON_WITNESS_UTXO' , $input_index );
362+ my $witness_utxo = $self -> get_all_fields(' PSBT_IN_WITNESS_UTXO' , $input_index );
363+
364+ return unless $non_witness_utxo || $witness_utxo ;
365+
366+ my $output ;
367+
368+ if ($non_witness_utxo ) {
369+ my $utxo_tx = $non_witness_utxo -> value;
370+
371+ Bitcoin::Crypto::Exception::PSBT-> raise(
372+ ' invalid PSBT_IN_NON_WITNESS_UTXO - txid mismatch'
373+ ) unless $utxo_tx -> txid eq $txid ;
374+
375+ Bitcoin::Crypto::Exception::PSBT-> raise(
376+ " invalid PSBT_IN_NON_WITNESS_UTXO - no output $output_index "
377+ ) unless defined $utxo_tx -> outputs-> [$output_index ];
378+
379+ $output = $utxo_tx -> outputs-> [$output_index ];
380+ }
381+ elsif ($witness_utxo ) {
382+ $output = $witness_utxo -> value;
383+ }
384+
385+ return btc_utxo-> new(
386+ txid => $txid ,
387+ output_index => $output_index ,
388+ output => $output ,
389+ );
390+ }
391+
357392sub get_transaction
358393{
359394 my ($self ) = @_ ;
@@ -377,7 +412,10 @@ sub get_transaction
377412 my $sequence_field = $self -> get_all_fields(' PSBT_IN_SEQUENCE' , $input_index );
378413
379414 $tx -> add_input(
380- utxo => [$utxo_txid , $utxo_output ],
415+
416+ # try to get utxo - if not present in the PSBT, fallback to just
417+ # basic UTXO information. UTXO will not be registered.
418+ utxo => $self -> _get_utxo($input_index , $utxo_txid , $utxo_output ) // [$utxo_txid , $utxo_output ],
381419 (defined $sequence_field ? (sequence_no => $sequence_field -> value) : ()),
382420 );
383421 }
@@ -595,6 +633,10 @@ L<Bitcoin::Crypto::Constants/LOCKTIME_HEIGHT_THRESHOLD>.
595633
596634Builds a new L<Bitcoin::Crypto::Transaction> object based on the contents of the PSBT.
597635
636+ If UTXO fields in the PSBT are populated, then the resulting transaction inputs
637+ will have proper UTXOs set. However, those UTXOs will not be registered
638+ globally, and will only be available to this transaction only.
639+
598640=head3 to_serialized
599641
600642 $serialized = $object->to_serialized()
0 commit comments