@@ -38,8 +38,11 @@ mod optional_parameters;
38
38
pub mod v2;
39
39
40
40
use bitcoin:: secp256k1:: rand:: { self , Rng } ;
41
- pub use error:: { Error , RequestError , SelectionError } ;
42
- use error:: { InternalRequestError , InternalSelectionError } ;
41
+ pub use error:: { Error , OutputSubstitutionError , RequestError , SelectionError } ;
42
+ use error:: {
43
+ InputContributionError , InternalInputContributionError , InternalOutputSubstitutionError ,
44
+ InternalRequestError , InternalSelectionError ,
45
+ } ;
43
46
use optional_parameters:: Params ;
44
47
45
48
use crate :: input_type:: InputType ;
@@ -359,7 +362,10 @@ impl WantsOutputs {
359
362
}
360
363
361
364
/// Substitute the receiver output script with the provided script.
362
- pub fn substitute_receiver_script ( self , output_script : & Script ) -> Result < WantsOutputs , Error > {
365
+ pub fn substitute_receiver_script (
366
+ self ,
367
+ output_script : & Script ,
368
+ ) -> Result < WantsOutputs , OutputSubstitutionError > {
363
369
let output_value = self . original_psbt . unsigned_tx . output [ self . change_vout ] . value ;
364
370
let outputs = vec ! [ TxOut { value: output_value, script_pubkey: output_script. into( ) } ] ;
365
371
self . replace_receiver_outputs ( outputs, output_script)
@@ -374,7 +380,7 @@ impl WantsOutputs {
374
380
self ,
375
381
replacement_outputs : Vec < TxOut > ,
376
382
drain_script : & Script ,
377
- ) -> Result < WantsOutputs , Error > {
383
+ ) -> Result < WantsOutputs , OutputSubstitutionError > {
378
384
let mut payjoin_psbt = self . original_psbt . clone ( ) ;
379
385
let mut change_vout: Option < usize > = None ;
380
386
if self . params . disable_output_substitution {
@@ -387,14 +393,16 @@ impl WantsOutputs {
387
393
. find ( |txo| txo. script_pubkey == original_output. script_pubkey )
388
394
{
389
395
Some ( txo) if txo. value < original_output. value => {
390
- return Err ( Error :: Server (
391
- "Decreasing the receiver output value is not allowed" . into ( ) ,
392
- ) ) ;
396
+ return Err ( InternalOutputSubstitutionError :: OutputSubstitutionDisabled (
397
+ "Decreasing the receiver output value is not allowed" ,
398
+ )
399
+ . into ( ) ) ;
393
400
}
394
401
None =>
395
- return Err ( Error :: Server (
396
- "Changing the receiver output script pubkey is not allowed" . into ( ) ,
397
- ) ) ,
402
+ return Err ( InternalOutputSubstitutionError :: OutputSubstitutionDisabled (
403
+ "Changing the receiver output script pubkey is not allowed" ,
404
+ )
405
+ . into ( ) ) ,
398
406
_ => log:: info!( "Receiver is augmenting outputs" ) ,
399
407
}
400
408
}
@@ -406,7 +414,7 @@ impl WantsOutputs {
406
414
if self . owned_vouts . contains ( & i) {
407
415
// Receiver output: substitute with a provided output picked randomly
408
416
if replacement_outputs. is_empty ( ) {
409
- return Err ( Error :: Server ( "Not enough outputs" . into ( ) ) ) ;
417
+ return Err ( InternalOutputSubstitutionError :: NotEnoughOutputs . into ( ) ) ;
410
418
}
411
419
let index = rng. gen_range ( 0 ..replacement_outputs. len ( ) ) ;
412
420
let txo = replacement_outputs. swap_remove ( index) ;
@@ -435,7 +443,7 @@ impl WantsOutputs {
435
443
original_psbt : self . original_psbt ,
436
444
payjoin_psbt,
437
445
params : self . params ,
438
- change_vout : change_vout. ok_or ( Error :: Server ( "Invalid drain script" . into ( ) ) ) ?,
446
+ change_vout : change_vout. ok_or ( InternalOutputSubstitutionError :: InvalidDrainScript ) ?,
439
447
owned_vouts : self . owned_vouts ,
440
448
} )
441
449
}
@@ -475,13 +483,13 @@ impl WantsInputs {
475
483
candidate_inputs : HashMap < Amount , OutPoint > ,
476
484
) -> Result < OutPoint , SelectionError > {
477
485
if candidate_inputs. is_empty ( ) {
478
- return Err ( SelectionError :: from ( InternalSelectionError :: Empty ) ) ;
486
+ return Err ( InternalSelectionError :: Empty . into ( ) ) ;
479
487
}
480
488
481
489
if self . payjoin_psbt . outputs . len ( ) > 2 {
482
490
// This UIH avoidance function supports only
483
491
// many-input, n-output transactions such that n <= 2 for now
484
- return Err ( SelectionError :: from ( InternalSelectionError :: TooManyOutputs ) ) ;
492
+ return Err ( InternalSelectionError :: TooManyOutputs . into ( ) ) ;
485
493
}
486
494
487
495
if self . payjoin_psbt . outputs . len ( ) == 2 {
@@ -531,26 +539,22 @@ impl WantsInputs {
531
539
}
532
540
533
541
// No suitable privacy preserving selection found
534
- Err ( SelectionError :: from ( InternalSelectionError :: NotFound ) )
542
+ Err ( InternalSelectionError :: NotFound . into ( ) )
535
543
}
536
544
537
545
fn select_first_candidate (
538
546
& self ,
539
547
candidate_inputs : HashMap < Amount , OutPoint > ,
540
548
) -> Result < OutPoint , SelectionError > {
541
- candidate_inputs
542
- . values ( )
543
- . next ( )
544
- . cloned ( )
545
- . ok_or ( SelectionError :: from ( InternalSelectionError :: NotFound ) )
549
+ candidate_inputs. values ( ) . next ( ) . cloned ( ) . ok_or ( InternalSelectionError :: NotFound . into ( ) )
546
550
}
547
551
548
552
/// Add the provided list of inputs to the transaction.
549
553
/// Any excess input amount is added to the change_vout output indicated previously.
550
554
pub fn contribute_witness_inputs (
551
555
self ,
552
556
inputs : impl IntoIterator < Item = ( OutPoint , TxOut ) > ,
553
- ) -> WantsInputs {
557
+ ) -> Result < WantsInputs , InputContributionError > {
554
558
let mut payjoin_psbt = self . payjoin_psbt . clone ( ) ;
555
559
// The payjoin proposal must not introduce mixed input sequence numbers
556
560
let original_sequence = self
@@ -587,15 +591,15 @@ impl WantsInputs {
587
591
let change_amount = receiver_input_amount - receiver_min_input_amount;
588
592
payjoin_psbt. unsigned_tx . output [ self . change_vout ] . value += change_amount;
589
593
} else {
590
- todo ! ( "Input amount is not enough to cover additional output value" ) ;
594
+ return Err ( InternalInputContributionError :: ValueTooLow . into ( ) ) ;
591
595
}
592
596
593
- WantsInputs {
597
+ Ok ( WantsInputs {
594
598
original_psbt : self . original_psbt ,
595
599
payjoin_psbt,
596
600
params : self . params ,
597
601
change_vout : self . change_vout ,
598
- }
602
+ } )
599
603
}
600
604
601
605
// Compute the minimum amount that the receiver must contribute to the transaction as input
0 commit comments