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