@@ -17,7 +17,6 @@ use bitcoin::constants::WITNESS_SCALE_FACTOR;
17
17
use bitcoin:: ecdsa:: Signature as BitcoinSignature ;
18
18
use bitcoin:: key:: Secp256k1 ;
19
19
use bitcoin:: policy:: MAX_STANDARD_TX_WEIGHT ;
20
- use bitcoin:: secp256k1:: ecdsa:: Signature ;
21
20
use bitcoin:: secp256k1:: { Message , PublicKey } ;
22
21
use bitcoin:: sighash:: SighashCache ;
23
22
use bitcoin:: transaction:: Version ;
@@ -339,11 +338,54 @@ impl ConstructedTransaction {
339
338
. sum ( )
340
339
}
341
340
341
+ fn finalize (
342
+ & self , holder_tx_signatures : & TxSignatures , counterparty_tx_signatures : & TxSignatures ,
343
+ shared_input_sig : Option < & SharedInputSignature > ,
344
+ ) -> Option < Transaction > {
345
+ let mut tx = self . tx . clone ( ) ;
346
+ self . add_local_witnesses ( & mut tx, holder_tx_signatures. witnesses . clone ( ) ) ;
347
+ self . add_remote_witnesses ( & mut tx, counterparty_tx_signatures. witnesses . clone ( ) ) ;
348
+
349
+ if let Some ( shared_input_index) = self . shared_input_index {
350
+ let holder_shared_input_sig =
351
+ holder_tx_signatures. shared_input_signature . or_else ( || {
352
+ debug_assert ! ( false ) ;
353
+ None
354
+ } ) ?;
355
+ let counterparty_shared_input_sig =
356
+ counterparty_tx_signatures. shared_input_signature . or_else ( || {
357
+ debug_assert ! ( false ) ;
358
+ None
359
+ } ) ?;
360
+
361
+ let shared_input_sig = shared_input_sig. or_else ( || {
362
+ debug_assert ! ( false ) ;
363
+ None
364
+ } ) ?;
365
+
366
+ let mut witness = Witness :: new ( ) ;
367
+ witness. push ( Vec :: new ( ) ) ;
368
+ let holder_sig = BitcoinSignature :: sighash_all ( holder_shared_input_sig) ;
369
+ let counterparty_sig = BitcoinSignature :: sighash_all ( counterparty_shared_input_sig) ;
370
+ if shared_input_sig. holder_signature_first {
371
+ witness. push_ecdsa_signature ( & holder_sig) ;
372
+ witness. push_ecdsa_signature ( & counterparty_sig) ;
373
+ } else {
374
+ witness. push_ecdsa_signature ( & counterparty_sig) ;
375
+ witness. push_ecdsa_signature ( & holder_sig) ;
376
+ }
377
+ witness. push ( & shared_input_sig. witness_script ) ;
378
+ tx. input [ shared_input_index as usize ] . witness = witness;
379
+ }
380
+
381
+ Some ( tx)
382
+ }
383
+
342
384
/// Adds provided holder witnesses to holder inputs of unsigned transaction.
343
385
///
344
386
/// Note that it is assumed that the witness count equals the holder input count.
345
- fn add_local_witnesses ( & mut self , witnesses : Vec < Witness > ) {
346
- self . tx
387
+ fn add_local_witnesses ( & self , transaction : & mut Transaction , witnesses : Vec < Witness > ) {
388
+ transaction
347
389
. input
348
390
. iter_mut ( )
349
391
. zip ( self . input_metadata . iter ( ) )
@@ -362,8 +404,8 @@ impl ConstructedTransaction {
362
404
/// Adds counterparty witnesses to counterparty inputs of unsigned transaction.
363
405
///
364
406
/// Note that it is assumed that the witness count equals the counterparty input count.
365
- fn add_remote_witnesses ( & mut self , witnesses : Vec < Witness > ) {
366
- self . tx
407
+ fn add_remote_witnesses ( & self , transaction : & mut Transaction , witnesses : Vec < Witness > ) {
408
+ transaction
367
409
. input
368
410
. iter_mut ( )
369
411
. zip ( self . input_metadata . iter ( ) )
@@ -392,13 +434,11 @@ impl ConstructedTransaction {
392
434
pub ( crate ) struct SharedInputSignature {
393
435
holder_signature_first : bool ,
394
436
witness_script : ScriptBuf ,
395
- counterparty_signature : Option < Signature > ,
396
437
}
397
438
398
439
impl_writeable_tlv_based ! ( SharedInputSignature , {
399
440
( 1 , holder_signature_first, required) ,
400
441
( 3 , witness_script, required) ,
401
- ( 5 , counterparty_signature, required) ,
402
442
} ) ;
403
443
404
444
/// The InteractiveTxSigningSession coordinates the signing flow of interactively constructed
@@ -413,9 +453,9 @@ pub(crate) struct InteractiveTxSigningSession {
413
453
unsigned_tx : ConstructedTransaction ,
414
454
holder_sends_tx_signatures_first : bool ,
415
455
has_received_commitment_signed : bool ,
416
- has_received_tx_signatures : bool ,
417
456
shared_input_signature : Option < SharedInputSignature > ,
418
457
holder_tx_signatures : Option < TxSignatures > ,
458
+ counterparty_tx_signatures : Option < TxSignatures > ,
419
459
}
420
460
421
461
impl InteractiveTxSigningSession {
@@ -432,7 +472,7 @@ impl InteractiveTxSigningSession {
432
472
}
433
473
434
474
pub fn has_received_tx_signatures ( & self ) -> bool {
435
- self . has_received_tx_signatures
475
+ self . counterparty_tx_signatures . is_some ( )
436
476
}
437
477
438
478
pub fn holder_tx_signatures ( & self ) -> & Option < TxSignatures > {
@@ -457,7 +497,7 @@ impl InteractiveTxSigningSession {
457
497
pub fn received_tx_signatures (
458
498
& mut self , tx_signatures : & TxSignatures ,
459
499
) -> Result < ( Option < TxSignatures > , Option < Transaction > ) , String > {
460
- if self . has_received_tx_signatures {
500
+ if self . has_received_tx_signatures ( ) {
461
501
return Err ( "Already received a tx_signatures message" . to_string ( ) ) ;
462
502
}
463
503
if self . remote_inputs_count ( ) != tx_signatures. witnesses . len ( ) {
@@ -470,26 +510,15 @@ impl InteractiveTxSigningSession {
470
510
return Err ( "Unexpected shared input signature" . to_string ( ) ) ;
471
511
}
472
512
473
- self . unsigned_tx . add_remote_witnesses ( tx_signatures. witnesses . clone ( ) ) ;
474
- if let Some ( ref mut shared_input_sig) = self . shared_input_signature {
475
- shared_input_sig. counterparty_signature = tx_signatures. shared_input_signature . clone ( ) ;
476
- }
477
- self . has_received_tx_signatures = true ;
513
+ self . counterparty_tx_signatures = Some ( tx_signatures. clone ( ) ) ;
478
514
479
515
let holder_tx_signatures = if !self . holder_sends_tx_signatures_first {
480
516
self . holder_tx_signatures . clone ( )
481
517
} else {
482
518
None
483
519
} ;
484
520
485
- // Check if the holder has provided its signatures and if so,
486
- // return the finalized funding transaction.
487
- let funding_tx_opt = if self . holder_tx_signatures . is_some ( ) {
488
- Some ( self . finalize_funding_tx ( ) )
489
- } else {
490
- // This means we're still waiting for the holder to provide their signatures.
491
- None
492
- } ;
521
+ let funding_tx_opt = self . maybe_finalize_funding_tx ( ) ;
493
522
494
523
Ok ( ( holder_tx_signatures, funding_tx_opt) )
495
524
}
@@ -516,15 +545,15 @@ impl InteractiveTxSigningSession {
516
545
517
546
self . verify_interactive_tx_signatures ( secp_ctx, & tx_signatures. witnesses ) ?;
518
547
519
- self . unsigned_tx . add_local_witnesses ( tx_signatures. witnesses . clone ( ) ) ;
520
548
self . holder_tx_signatures = Some ( tx_signatures) ;
521
549
522
- let funding_tx_opt = self . has_received_tx_signatures . then ( || self . finalize_funding_tx ( ) ) ;
523
- let holder_tx_signatures =
524
- ( self . holder_sends_tx_signatures_first || self . has_received_tx_signatures ) . then ( || {
525
- debug_assert ! ( self . has_received_commitment_signed) ;
526
- self . holder_tx_signatures . clone ( ) . expect ( "Holder tx_signatures were just provided" )
527
- } ) ;
550
+ let funding_tx_opt = self . maybe_finalize_funding_tx ( ) ;
551
+ let holder_tx_signatures = ( self . holder_sends_tx_signatures_first
552
+ || self . has_received_tx_signatures ( ) )
553
+ . then ( || {
554
+ debug_assert ! ( self . has_received_commitment_signed) ;
555
+ self . holder_tx_signatures . clone ( ) . expect ( "Holder tx_signatures were just provided" )
556
+ } ) ;
528
557
529
558
Ok ( ( holder_tx_signatures, funding_tx_opt) )
530
559
}
@@ -576,43 +605,15 @@ impl InteractiveTxSigningSession {
576
605
} )
577
606
}
578
607
579
- fn finalize_funding_tx ( & mut self ) -> Transaction {
580
- if let Some ( shared_input_index) = self . unsigned_tx . shared_input_index {
581
- if let Some ( holder_shared_input_sig) = self
582
- . holder_tx_signatures
583
- . as_ref ( )
584
- . and_then ( |holder_tx_sigs| holder_tx_sigs. shared_input_signature )
585
- {
586
- if let Some ( ref shared_input_sig) = self . shared_input_signature {
587
- if let Some ( counterparty_shared_input_sig) =
588
- shared_input_sig. counterparty_signature
589
- {
590
- let mut witness = Witness :: new ( ) ;
591
- witness. push ( Vec :: new ( ) ) ;
592
- let holder_sig = BitcoinSignature :: sighash_all ( holder_shared_input_sig) ;
593
- let counterparty_sig =
594
- BitcoinSignature :: sighash_all ( counterparty_shared_input_sig) ;
595
- if shared_input_sig. holder_signature_first {
596
- witness. push_ecdsa_signature ( & holder_sig) ;
597
- witness. push_ecdsa_signature ( & counterparty_sig) ;
598
- } else {
599
- witness. push_ecdsa_signature ( & counterparty_sig) ;
600
- witness. push_ecdsa_signature ( & holder_sig) ;
601
- }
602
- witness. push ( & shared_input_sig. witness_script ) ;
603
- self . unsigned_tx . tx . input [ shared_input_index as usize ] . witness = witness;
604
- } else {
605
- debug_assert ! ( false ) ;
606
- }
607
- } else {
608
- debug_assert ! ( false ) ;
609
- }
610
- } else {
611
- debug_assert ! ( false ) ;
612
- }
613
- }
614
-
615
- self . unsigned_tx . tx . clone ( )
608
+ fn maybe_finalize_funding_tx ( & mut self ) -> Option < Transaction > {
609
+ let holder_tx_signatures = self . holder_tx_signatures . as_ref ( ) ?;
610
+ let counterparty_tx_signatures = self . counterparty_tx_signatures . as_ref ( ) ?;
611
+ let shared_input_signature = self . shared_input_signature . as_ref ( ) ;
612
+ self . unsigned_tx . finalize (
613
+ holder_tx_signatures,
614
+ counterparty_tx_signatures,
615
+ shared_input_signature,
616
+ )
616
617
}
617
618
618
619
fn verify_interactive_tx_signatures < C : bitcoin:: secp256k1:: Verification > (
@@ -781,7 +782,7 @@ impl_writeable_tlv_based!(InteractiveTxSigningSession, {
781
782
( 1 , unsigned_tx, required) ,
782
783
( 3 , has_received_commitment_signed, required) ,
783
784
( 5 , holder_tx_signatures, required) ,
784
- ( 7 , has_received_tx_signatures , required) ,
785
+ ( 7 , counterparty_tx_signatures , required) ,
785
786
( 9 , holder_sends_tx_signatures_first, required) ,
786
787
( 11 , shared_input_signature, required) ,
787
788
} ) ;
@@ -1372,7 +1373,6 @@ macro_rules! define_state_transitions {
1372
1373
. as_ref( )
1373
1374
. map( |shared_input| SharedInputSignature {
1374
1375
holder_signature_first: shared_input. holder_sig_first,
1375
- counterparty_signature: None ,
1376
1376
witness_script: shared_input. witness_script. clone( ) ,
1377
1377
} ) ;
1378
1378
let holder_node_id = context. holder_node_id;
@@ -1394,9 +1394,9 @@ macro_rules! define_state_transitions {
1394
1394
unsigned_tx: tx,
1395
1395
holder_sends_tx_signatures_first,
1396
1396
has_received_commitment_signed: false ,
1397
- has_received_tx_signatures: false ,
1398
1397
shared_input_signature,
1399
1398
holder_tx_signatures: None ,
1399
+ counterparty_tx_signatures: None ,
1400
1400
} ;
1401
1401
Ok ( NegotiationComplete ( signing_session) )
1402
1402
}
@@ -3319,9 +3319,9 @@ mod tests {
3319
3319
unsigned_tx,
3320
3320
holder_sends_tx_signatures_first : false , // N/A for test
3321
3321
has_received_commitment_signed : false , // N/A for test
3322
- has_received_tx_signatures : false , // N/A for test
3323
3322
shared_input_signature : None ,
3324
3323
holder_tx_signatures : None ,
3324
+ counterparty_tx_signatures : None ,
3325
3325
}
3326
3326
. verify_interactive_tx_signatures (
3327
3327
& secp_ctx,
0 commit comments