@@ -516,14 +516,20 @@ pub trait PsbtExt {
516
516
secp : & Secp256k1 < C > ,
517
517
) -> Result < bitcoin:: Transaction , Error > ;
518
518
519
- /// Update a PSBT with the derived descriptor.
519
+ /// Update PSBT input with a descriptor.
520
520
///
521
- /// Internally, uses [`PsbtInputExt::update_with_descriptor`] and checks that the `witness_utxo`
522
- /// and `non_witness_utxo` have the correct `script_pubkey` if applicable.
521
+ /// This is a convienient wrapper around [`PsbtInputExt::update_with_descriptor`] which avoids
522
+ /// looking up the inputs previous outupt index. Consult that function for further
523
+ /// documentation.
524
+ ///
525
+ /// The `descriptor` **must not have any wildcards** in it otherwise an error will be returned
526
+ /// however it can (and should) have extended keys in it.
523
527
fn update_inp_with_descriptor (
524
528
& mut self ,
525
529
input_index : usize ,
526
- desc : & Descriptor < DescriptorPublicKey > ,
530
+ descriptor : & Descriptor < DescriptorPublicKey > ,
531
+ check_witness_utxo : bool ,
532
+ check_non_witness_utxo : bool ,
527
533
) -> Result < ( ) , UtxoUpdateError > ;
528
534
529
535
/// Get the sighash message(data to sign) at input index `idx` based on the sighash
@@ -689,21 +695,28 @@ impl PsbtExt for Psbt {
689
695
& mut self ,
690
696
input_index : usize ,
691
697
desc : & Descriptor < DescriptorPublicKey > ,
698
+ check_witness_utxo : bool ,
699
+ check_non_witness_utxo : bool ,
692
700
) -> Result < ( ) , UtxoUpdateError > {
693
701
let n_inputs = self . inputs . len ( ) ;
694
702
let input = self
695
703
. inputs
696
704
. get_mut ( input_index)
697
705
. ok_or ( UtxoUpdateError :: IndexOutOfBounds ( input_index, n_inputs) ) ?;
698
- let vout = self
699
- . unsigned_tx
700
- . input
701
- . get ( input_index)
702
- . ok_or ( UtxoUpdateError :: MissingInputUtxo ) ?
703
- . previous_output
704
- . vout ;
706
+ let check_non_witness_utxo = if check_non_witness_utxo {
707
+ Some (
708
+ self . unsigned_tx
709
+ . input
710
+ . get ( input_index)
711
+ . ok_or ( UtxoUpdateError :: MissingInputUtxo ) ?
712
+ . previous_output
713
+ . vout ,
714
+ )
715
+ } else {
716
+ None
717
+ } ;
705
718
input
706
- . update_with_descriptor ( desc, true , Some ( vout ) )
719
+ . update_with_descriptor ( desc, check_witness_utxo , check_non_witness_utxo )
707
720
. map_err ( UtxoUpdateError :: InputUpdate ) ?;
708
721
Ok ( ( ) )
709
722
}
@@ -806,12 +819,22 @@ pub trait PsbtInputExt {
806
819
/// spend it.
807
820
///
808
821
/// If the descriptor contains wilcards or otherwise cannot be transformed into a concrete
809
- /// descriptor an error will returned.
822
+ /// descriptor an error will returned. The descriptor *can* (and should) have extended keys in
823
+ /// it so PSBT fields like `bip32_derivation` and `tap_key_origins` can be populated.
810
824
///
811
825
/// You can make sure the descriptor is consistent with the `witness_utxo` and/or
812
826
/// `non_witness_utxo` fields of the PSBT by setting `check_witness_utxo` and
813
- /// `check_non_witness_utxo` respectively. You should set `check_non_witness_utxo` to inputs the
814
- /// output index in the transaction being spent.
827
+ /// `check_non_witness_utxo` respectively. You should set `check_non_witness_utxo` to input's
828
+ /// output index in the transaction being spent. Note that `witness_utxo` or
829
+ /// `check_non_witness_utxo` being absent also counts as a check failure unless the field is
830
+ /// strictly not relevent to the descriptor. The two cases where the absence of a field will be
831
+ /// ignored are `witness_utxo` missing on a non-segwit descriptor and `non_witness_utxo` missing
832
+ /// on a taproot descriptor.
833
+ ///
834
+ /// Note that it may be suprising that `non_witness_utxo` would ever be required on a segwit
835
+ /// output but some PSBT signers require it to be set because of the [*segwit
836
+ /// bug*](https://bitcoinhackers.org/@lukedashjr/104287698361196952) which was fixed in the
837
+ /// design of segwitv1 (taproot).
815
838
fn update_with_descriptor (
816
839
& mut self ,
817
840
descriptor : & Descriptor < DescriptorPublicKey > ,
0 commit comments