@@ -14,7 +14,7 @@ use bitcoin_hashes::ripemd160::Hash as Ripemd160;
14
14
use bitcoin_hashes:: hash160:: Hash as Hash160 ;
15
15
use bitcoin_hashes:: sha256d:: Hash as Sha256dHash ;
16
16
17
- use ln:: channelmanager:: PaymentHash ;
17
+ use ln:: channelmanager:: { PaymentHash , PaymentPreimage } ;
18
18
use ln:: msgs:: DecodeError ;
19
19
use util:: ser:: { Readable , Writeable , Writer , WriterWriteAdaptor } ;
20
20
use util:: byte_utils;
@@ -23,6 +23,11 @@ use secp256k1::key::{SecretKey, PublicKey};
23
23
use secp256k1:: { Secp256k1 , Signature } ;
24
24
use secp256k1;
25
25
26
+ use std:: collections:: HashMap ;
27
+ use std:: cmp;
28
+
29
+ const MAX_ALLOC_SIZE : usize = 64 * 1024 ;
30
+
26
31
pub ( super ) const HTLC_SUCCESS_TX_WEIGHT : u64 = 703 ;
27
32
pub ( super ) const HTLC_TIMEOUT_TX_WEIGHT : u64 = 663 ;
28
33
@@ -480,7 +485,11 @@ pub fn build_htlc_transaction(prev_hash: &Sha256dHash, feerate_per_kw: u64, to_s
480
485
/// to broadcast. Eventually this will require a signer which is possibly external, but for now we
481
486
/// just pass in the SecretKeys required.
482
487
pub struct LocalCommitmentTransaction {
483
- tx : Transaction
488
+ tx : Transaction ,
489
+ //TODO: modify Channel methods to integrate HTLC material at LocalCommitmentTransaction generation to drop Option here
490
+ local_keys : Option < TxCreationKeys > ,
491
+ feerate_per_kw : Option < u64 > ,
492
+ per_htlc : HashMap < u32 , ( HTLCOutputInCommitment , Option < Signature > , Option < Transaction > ) >
484
493
}
485
494
impl LocalCommitmentTransaction {
486
495
#[ cfg( test) ]
@@ -495,7 +504,11 @@ impl LocalCommitmentTransaction {
495
504
input : vec ! [ dummy_input] ,
496
505
output : Vec :: new ( ) ,
497
506
lock_time : 0 ,
498
- } }
507
+ } ,
508
+ local_keys : None ,
509
+ feerate_per_kw : None ,
510
+ per_htlc : HashMap :: new ( )
511
+ }
499
512
}
500
513
501
514
/// Generate a new LocalCommitmentTransaction based on a raw transaction, both parties funding pubkeys and remote signatures
@@ -515,7 +528,11 @@ impl LocalCommitmentTransaction {
515
528
tx. input [ 0 ] . witness . push ( Vec :: new ( ) ) ;
516
529
}
517
530
518
- Self { tx }
531
+ Self { tx,
532
+ local_keys : None ,
533
+ feerate_per_kw : None ,
534
+ per_htlc : HashMap :: new ( )
535
+ }
519
536
}
520
537
521
538
/// Expose transaction id of LocalCommitmentTransaction
@@ -563,6 +580,63 @@ impl LocalCommitmentTransaction {
563
580
assert ! ( self . has_local_sig( ) ) ;
564
581
& self . tx
565
582
}
583
+
584
+ /// Set HTLC cache to generate any local HTLC transaction spending one of htlc ouput
585
+ /// from this local commitment transaction
586
+ pub fn set_htlc_cache ( & mut self , local_keys : TxCreationKeys , feerate_per_kw : u64 , htlc_outputs : HashMap < u32 , ( HTLCOutputInCommitment , Option < Signature > , Option < Transaction > ) > ) {
587
+ self . local_keys = Some ( local_keys) ;
588
+ self . feerate_per_kw = Some ( feerate_per_kw) ;
589
+ self . per_htlc = htlc_outputs;
590
+ }
591
+
592
+ /// Add local signature for a htlc transaction, do nothing if a cached signed transaction is
593
+ /// already present
594
+ pub fn add_htlc_sig < T : secp256k1:: Signing > ( & mut self , htlc_base_key : & SecretKey , htlc_index : u32 , preimage : Option < PaymentPreimage > , local_csv : u16 , secp_ctx : & Secp256k1 < T > ) {
595
+ if self . local_keys . is_none ( ) || self . feerate_per_kw . is_none ( ) { return ; }
596
+ let local_keys = self . local_keys . as_ref ( ) . unwrap ( ) ;
597
+ let txid = self . txid ( ) ;
598
+ if let Some ( ref mut this_htlc) = self . per_htlc . get_mut ( & htlc_index) {
599
+ if this_htlc. 2 . is_some ( ) { return ; } // we already have a cached htlc transaction at provided index
600
+ let mut htlc_tx = build_htlc_transaction ( & txid, self . feerate_per_kw . unwrap ( ) , local_csv, & this_htlc. 0 , & local_keys. a_delayed_payment_key , & local_keys. revocation_key ) ;
601
+ if !this_htlc. 0 . offered && preimage. is_none ( ) { return ; } // if we don't have preimage for HTLC-Success, don't try to generate
602
+ let htlc_secret = if !this_htlc. 0 . offered { preimage } else { None } ; // if we have a preimage for HTLC-Timeout, don't use it that's likely a duplicate HTLC hash
603
+ if this_htlc. 1 . is_none ( ) { return ; } // we don't have any remote signature for this htlc
604
+ if htlc_tx. input . len ( ) != 1 { return ; }
605
+ if htlc_tx. input [ 0 ] . witness . len ( ) != 0 { return ; }
606
+
607
+ let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys ( & this_htlc. 0 , & local_keys. a_htlc_key , & local_keys. b_htlc_key , & local_keys. revocation_key ) ;
608
+
609
+ if let Ok ( our_htlc_key) = derive_private_key ( secp_ctx, & local_keys. per_commitment_point , htlc_base_key) {
610
+ let sighash = hash_to_message ! ( & bip143:: SighashComponents :: new( & htlc_tx) . sighash_all( & htlc_tx. input[ 0 ] , & htlc_redeemscript, this_htlc. 0 . amount_msat / 1000 ) [ ..] ) ;
611
+ let our_sig = secp_ctx. sign ( & sighash, & our_htlc_key) ;
612
+
613
+ htlc_tx. input [ 0 ] . witness . push ( Vec :: new ( ) ) ; // First is the multisig dummy
614
+
615
+ htlc_tx. input [ 0 ] . witness . push ( this_htlc. 1 . unwrap ( ) . serialize_der ( ) . to_vec ( ) ) ;
616
+ htlc_tx. input [ 0 ] . witness . push ( our_sig. serialize_der ( ) . to_vec ( ) ) ;
617
+ htlc_tx. input [ 0 ] . witness [ 1 ] . push ( SigHashType :: All as u8 ) ;
618
+ htlc_tx. input [ 0 ] . witness [ 2 ] . push ( SigHashType :: All as u8 ) ;
619
+
620
+ if this_htlc. 0 . offered {
621
+ htlc_tx. input [ 0 ] . witness . push ( Vec :: new ( ) ) ;
622
+ assert ! ( htlc_secret. is_none( ) ) ;
623
+ } else {
624
+ htlc_tx. input [ 0 ] . witness . push ( htlc_secret. unwrap ( ) . 0 . to_vec ( ) ) ;
625
+ }
626
+
627
+ htlc_tx. input [ 0 ] . witness . push ( htlc_redeemscript. as_bytes ( ) . to_vec ( ) ) ;
628
+
629
+ this_htlc. 2 = Some ( htlc_tx) ;
630
+ } else { return ; }
631
+ }
632
+ }
633
+ /// Expose raw htlc transaction, guarante witness is complete if non-empty
634
+ pub fn htlc_with_valid_witness ( & self , htlc_index : u32 ) -> & Option < Transaction > {
635
+ if let Some ( ref this_htlc) = self . per_htlc . get ( & htlc_index) {
636
+ return & this_htlc. 2 ;
637
+ }
638
+ & None
639
+ }
566
640
}
567
641
impl PartialEq for LocalCommitmentTransaction {
568
642
// We dont care whether we are signed in equality comparison
@@ -578,6 +652,14 @@ impl Writeable for LocalCommitmentTransaction {
578
652
_ => panic ! ( "local tx must have been well-formed!" ) ,
579
653
}
580
654
}
655
+ self . local_keys . write ( writer) ?;
656
+ self . feerate_per_kw . write ( writer) ?;
657
+ writer. write_all ( & byte_utils:: be64_to_array ( self . per_htlc . len ( ) as u64 ) ) ?;
658
+ for ( ref _index, & ( ref htlc, ref sig, ref htlc_tx) ) in self . per_htlc . iter ( ) {
659
+ htlc. write ( writer) ?;
660
+ sig. write ( writer) ?;
661
+ htlc_tx. write ( writer) ?;
662
+ }
581
663
Ok ( ( ) )
582
664
}
583
665
}
@@ -590,12 +672,27 @@ impl Readable for LocalCommitmentTransaction {
590
672
_ => return Err ( DecodeError :: InvalidValue ) ,
591
673
} ,
592
674
} ;
675
+ let local_keys = Readable :: read ( reader) ?;
676
+ let feerate_per_kw = Readable :: read ( reader) ?;
677
+ let htlcs_count: u64 = Readable :: read ( reader) ?;
678
+ let mut per_htlc = HashMap :: with_capacity ( cmp:: min ( htlcs_count as usize , MAX_ALLOC_SIZE / 32 ) ) ;
679
+ for _ in 0 ..htlcs_count {
680
+ let htlc: HTLCOutputInCommitment = Readable :: read ( reader) ?;
681
+ let sigs = Readable :: read ( reader) ?;
682
+ let htlc_tx = Readable :: read ( reader) ?;
683
+ per_htlc. insert ( htlc. transaction_output_index . unwrap ( ) , ( htlc, sigs, htlc_tx) ) ;
684
+ }
593
685
594
686
if tx. input . len ( ) != 1 {
595
687
// Ensure tx didn't hit the 0-input ambiguity case.
596
688
return Err ( DecodeError :: InvalidValue ) ;
597
689
}
598
- Ok ( Self { tx } )
690
+ Ok ( Self {
691
+ tx,
692
+ local_keys,
693
+ feerate_per_kw,
694
+ per_htlc,
695
+ } )
599
696
}
600
697
}
601
698
0 commit comments