@@ -61,15 +61,13 @@ impl InputPair {
6161 let raw = InternalInputPair { txin : & txin, psbtin : & psbtin } ;
6262 raw. validate_utxo ( ) ?;
6363
64- let expected_weight = match raw. expected_input_weight ( ) {
65- Ok ( weight) => weight,
66- Err ( InputWeightError :: NotSupported ) =>
67- if let Some ( weight) = expected_weight {
68- weight
69- } else {
70- return Err ( InternalPsbtInputError :: from ( InputWeightError :: NotSupported ) . into ( ) ) ;
71- } ,
72- Err ( e) => return Err ( InternalPsbtInputError :: from ( e) . into ( ) ) ,
64+ let expected_weight = match ( raw. expected_input_weight ( ) , expected_weight) {
65+ ( Ok ( _) , Some ( _) ) => {
66+ return Err ( InternalPsbtInputError :: ProvidedUnnecessaryWeight . into ( ) ) ;
67+ }
68+ ( Ok ( weight) , None ) => weight,
69+ ( Err ( InputWeightError :: NotSupported ) , Some ( expected_weight) ) => expected_weight,
70+ ( Err ( e) , _) => return Err ( InternalPsbtInputError :: from ( e) . into ( ) ) ,
7371 } ;
7472
7573 let input_pair = Self { expected_weight, txin, psbtin } ;
@@ -246,7 +244,9 @@ mod tests {
246244 use bitcoin:: key:: { PublicKey , WPubkeyHash } ;
247245 use bitcoin:: secp256k1:: Secp256k1 ;
248246 use bitcoin:: transaction:: InputWeightPrediction ;
249- use bitcoin:: { Amount , PubkeyHash , ScriptBuf , ScriptHash , Txid , WScriptHash , XOnlyPublicKey } ;
247+ use bitcoin:: {
248+ witness, Amount , PubkeyHash , ScriptBuf , ScriptHash , Txid , WScriptHash , XOnlyPublicKey ,
249+ } ;
250250 use payjoin_test_utils:: { DUMMY20 , DUMMY32 } ;
251251
252252 use super :: * ;
@@ -457,7 +457,7 @@ mod tests {
457457
458458 let p2wsh_pair = InputPair :: new (
459459 TxIn { previous_output : outpoint, sequence, ..Default :: default ( ) } ,
460- psbt:: Input { witness_utxo : Some ( p2wsh_txout) , ..Default :: default ( ) } ,
460+ psbt:: Input { witness_utxo : Some ( p2wsh_txout. clone ( ) ) , ..Default :: default ( ) } ,
461461 None ,
462462 ) ;
463463 // P2wsh is not supported when expected weight is not provided
@@ -475,7 +475,39 @@ mod tests {
475475 . err( )
476476 . unwrap( ) ,
477477 PsbtInputError :: from( InvalidScriptPubKey ( AddressType :: P2wsh ) )
478- )
478+ ) ;
479+
480+ let mut dummy_witness = witness:: Witness :: new ( ) ;
481+ dummy_witness. push ( DUMMY32 ) ;
482+ let txin = TxIn {
483+ previous_output : outpoint,
484+ witness : dummy_witness. clone ( ) ,
485+ ..Default :: default ( )
486+ } ;
487+ let input_weight = Weight :: from_non_witness_data_size ( txin. base_size ( ) as u64 )
488+ + Weight :: from_witness_data_size ( dummy_witness. size ( ) as u64 ) ;
489+
490+ // Add the witness straight to the txin
491+ let psbtin = psbt:: Input { witness_utxo : Some ( p2wsh_txout. clone ( ) ) , ..Default :: default ( ) } ;
492+ let p2wsh_pair = InputPair :: new ( txin, psbtin, None ) . expect ( "witness is provided for p2wsh" ) ;
493+ assert_eq ! ( p2wsh_pair. expected_weight, input_weight) ;
494+ // Same check but add the witness to the psbtin
495+ let txin = TxIn { previous_output : outpoint, ..Default :: default ( ) } ;
496+ let psbtin = psbt:: Input {
497+ witness_utxo : Some ( p2wsh_txout) ,
498+ final_script_witness : Some ( dummy_witness) ,
499+ ..Default :: default ( )
500+ } ;
501+ let p2wsh_pair = InputPair :: new ( txin. clone ( ) , psbtin. clone ( ) , None )
502+ . expect ( "witness is provided for p2wsh" ) ;
503+ assert_eq ! ( p2wsh_pair. expected_weight, input_weight) ;
504+
505+ // Should error out if expected weight is provided and witness is provided
506+ let p2wsh_pair = InputPair :: new ( txin, psbtin, Some ( expected_weight) ) ;
507+ assert_eq ! (
508+ p2wsh_pair. err( ) . unwrap( ) ,
509+ PsbtInputError :: from( InternalPsbtInputError :: ProvidedUnnecessaryWeight )
510+ ) ;
479511 }
480512
481513 #[ test]
0 commit comments