@@ -15,6 +15,7 @@ use bitcoin::consensus::encode;
15
15
16
16
use bitcoin:: hashes:: Hash ;
17
17
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
18
+ use bitcoin:: hashes:: sha256d:: Hash as Sha256d ;
18
19
use bitcoin:: hash_types:: { Txid , BlockHash , WPubkeyHash } ;
19
20
20
21
use bitcoin:: secp256k1:: key:: { PublicKey , SecretKey } ;
@@ -420,6 +421,10 @@ pub(super) struct Channel<Signer: Sign> {
420
421
421
422
channel_update_status : ChannelUpdateStatus ,
422
423
424
+ /// Our counterparty's channel_announcement signatures provided in announcement_signatures.
425
+ /// This can be used to rebroadcast the channel_announcement message later.
426
+ announcement_sigs : Option < ( Signature , Signature ) > ,
427
+
423
428
// We save these values so we can make sure `next_local_commit_tx_fee_msat` and
424
429
// `next_remote_commit_tx_fee_msat` properly predict what the next commitment transaction fee will
425
430
// be, by comparing the cached values to the fee of the tranaction generated by
@@ -621,6 +626,8 @@ impl<Signer: Sign> Channel<Signer> {
621
626
622
627
channel_update_status : ChannelUpdateStatus :: Enabled ,
623
628
629
+ announcement_sigs : None ,
630
+
624
631
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
625
632
next_local_commitment_tx_fee_info_cached : Mutex :: new ( None ) ,
626
633
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -862,6 +869,8 @@ impl<Signer: Sign> Channel<Signer> {
862
869
863
870
channel_update_status : ChannelUpdateStatus :: Enabled ,
864
871
872
+ announcement_sigs : None ,
873
+
865
874
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
866
875
next_local_commitment_tx_fee_info_cached : Mutex :: new ( None ) ,
867
876
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
@@ -3822,6 +3831,8 @@ impl<Signer: Sign> Channel<Signer> {
3822
3831
/// closing).
3823
3832
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
3824
3833
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
3834
+ ///
3835
+ /// This will only return ChannelError::Ignore upon failure.
3825
3836
pub fn get_channel_announcement ( & self , node_id : PublicKey , chain_hash : BlockHash ) -> Result < ( msgs:: UnsignedChannelAnnouncement , Signature ) , ChannelError > {
3826
3837
if !self . config . announced_channel {
3827
3838
return Err ( ChannelError :: Ignore ( "Channel is not available for public announcements" . to_owned ( ) ) ) ;
@@ -3852,6 +3863,63 @@ impl<Signer: Sign> Channel<Signer> {
3852
3863
Ok ( ( msg, sig) )
3853
3864
}
3854
3865
3866
+ /// Signs the given channel announcement, returning a ChannelError::Ignore if no keys are
3867
+ /// available.
3868
+ fn sign_channel_announcement ( & self , our_node_secret : & SecretKey , our_node_id : PublicKey , msghash : secp256k1:: Message , announcement : msgs:: UnsignedChannelAnnouncement , our_bitcoin_sig : Signature ) -> Result < msgs:: ChannelAnnouncement , ChannelError > {
3869
+ if let Some ( ( their_node_sig, their_bitcoin_sig) ) = self . announcement_sigs {
3870
+ let were_node_one = announcement. node_id_1 == our_node_id;
3871
+
3872
+ let our_node_sig = self . secp_ctx . sign ( & msghash, our_node_secret) ;
3873
+ Ok ( msgs:: ChannelAnnouncement {
3874
+ node_signature_1 : if were_node_one { our_node_sig } else { their_node_sig } ,
3875
+ node_signature_2 : if were_node_one { their_node_sig } else { our_node_sig } ,
3876
+ bitcoin_signature_1 : if were_node_one { our_bitcoin_sig } else { their_bitcoin_sig } ,
3877
+ bitcoin_signature_2 : if were_node_one { their_bitcoin_sig } else { our_bitcoin_sig } ,
3878
+ contents : announcement,
3879
+ } )
3880
+ } else {
3881
+ Err ( ChannelError :: Ignore ( "Attempted to sign channel announcement before we'd received announcement_signatures" . to_string ( ) ) )
3882
+ }
3883
+ }
3884
+
3885
+ /// Processes an incoming announcement_signatures message, providing a fully-signed
3886
+ /// channel_announcement message which we can broadcast and storing our counterparty's
3887
+ /// signatures for later reconstruction/rebroadcast of the channel_announcement.
3888
+ pub fn announcement_signatures ( & mut self , our_node_secret : & SecretKey , our_node_id : PublicKey , chain_hash : BlockHash , msg : & msgs:: AnnouncementSignatures ) -> Result < msgs:: ChannelAnnouncement , ChannelError > {
3889
+ let ( announcement, our_bitcoin_sig) = self . get_channel_announcement ( our_node_id. clone ( ) , chain_hash) ?;
3890
+
3891
+ let msghash = hash_to_message ! ( & Sha256d :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
3892
+
3893
+ if self . secp_ctx . verify ( & msghash, & msg. node_signature , & self . get_counterparty_node_id ( ) ) . is_err ( ) {
3894
+ return Err ( ChannelError :: Close ( format ! (
3895
+ "Bad announcement_signatures. Failed to verify node_signature. UnsignedChannelAnnouncement used for verification is {:?}. their_node_key is {:?}" ,
3896
+ & announcement, self . get_counterparty_node_id( ) ) ) ) ;
3897
+ }
3898
+ if self . secp_ctx . verify ( & msghash, & msg. bitcoin_signature , self . counterparty_funding_pubkey ( ) ) . is_err ( ) {
3899
+ return Err ( ChannelError :: Close ( format ! (
3900
+ "Bad announcement_signatures. Failed to verify bitcoin_signature. UnsignedChannelAnnouncement used for verification is {:?}. their_bitcoin_key is ({:?})" ,
3901
+ & announcement, self . counterparty_funding_pubkey( ) ) ) ) ;
3902
+ }
3903
+
3904
+ self . announcement_sigs = Some ( ( msg. node_signature , msg. bitcoin_signature ) ) ;
3905
+
3906
+ self . sign_channel_announcement ( our_node_secret, our_node_id, msghash, announcement, our_bitcoin_sig)
3907
+ }
3908
+
3909
+ /// Gets a signed channel_announcement for this channel, if we previously received an
3910
+ /// announcement_signatures from our counterparty.
3911
+ pub fn get_signed_channel_announcement ( & self , our_node_secret : & SecretKey , our_node_id : PublicKey , chain_hash : BlockHash ) -> Option < msgs:: ChannelAnnouncement > {
3912
+ let ( announcement, our_bitcoin_sig) = match self . get_channel_announcement ( our_node_id. clone ( ) , chain_hash) {
3913
+ Ok ( res) => res,
3914
+ Err ( _) => return None ,
3915
+ } ;
3916
+ let msghash = hash_to_message ! ( & Sha256d :: hash( & announcement. encode( ) [ ..] ) [ ..] ) ;
3917
+ match self . sign_channel_announcement ( our_node_secret, our_node_id, msghash, announcement, our_bitcoin_sig) {
3918
+ Ok ( res) => Some ( res) ,
3919
+ Err ( _) => None ,
3920
+ }
3921
+ }
3922
+
3855
3923
/// May panic if called on a channel that wasn't immediately-previously
3856
3924
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
3857
3925
pub fn get_channel_reestablish < L : Deref > ( & self , logger : & L ) -> msgs:: ChannelReestablish where L :: Target : Logger {
@@ -4375,8 +4443,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
4375
4443
// Note that we write out as if remove_uncommitted_htlcs_and_mark_paused had just been
4376
4444
// called.
4377
4445
4378
- writer. write_all ( & [ SERIALIZATION_VERSION ; 1 ] ) ?;
4379
- writer. write_all ( & [ MIN_SERIALIZATION_VERSION ; 1 ] ) ?;
4446
+ write_ver_prefix ! ( writer, SERIALIZATION_VERSION , MIN_SERIALIZATION_VERSION ) ;
4380
4447
4381
4448
self . user_id . write ( writer) ?;
4382
4449
self . config . write ( writer) ?;
@@ -4565,6 +4632,9 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
4565
4632
self . commitment_secrets . write ( writer) ?;
4566
4633
4567
4634
self . channel_update_status . write ( writer) ?;
4635
+
4636
+ write_tlv_fields ! ( writer, { } , { ( 0 , self . announcement_sigs) } ) ;
4637
+
4568
4638
Ok ( ( ) )
4569
4639
}
4570
4640
}
@@ -4573,11 +4643,7 @@ const MAX_ALLOC_SIZE: usize = 64*1024;
4573
4643
impl < ' a , Signer : Sign , K : Deref > ReadableArgs < & ' a K > for Channel < Signer >
4574
4644
where K :: Target : KeysInterface < Signer = Signer > {
4575
4645
fn read < R : :: std:: io:: Read > ( reader : & mut R , keys_source : & ' a K ) -> Result < Self , DecodeError > {
4576
- let _ver: u8 = Readable :: read ( reader) ?;
4577
- let min_ver: u8 = Readable :: read ( reader) ?;
4578
- if min_ver > SERIALIZATION_VERSION {
4579
- return Err ( DecodeError :: UnknownVersion ) ;
4580
- }
4646
+ let _ver = read_ver_prefix ! ( reader, SERIALIZATION_VERSION ) ;
4581
4647
4582
4648
let user_id = Readable :: read ( reader) ?;
4583
4649
let config: ChannelConfig = Readable :: read ( reader) ?;
@@ -4739,6 +4805,9 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
4739
4805
4740
4806
let channel_update_status = Readable :: read ( reader) ?;
4741
4807
4808
+ let mut announcement_sigs = None ;
4809
+ read_tlv_fields ! ( reader, { } , { ( 0 , announcement_sigs) } ) ;
4810
+
4742
4811
let mut secp_ctx = Secp256k1 :: new ( ) ;
4743
4812
secp_ctx. seeded_randomize ( & keys_source. get_secure_random_bytes ( ) ) ;
4744
4813
@@ -4815,6 +4884,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
4815
4884
4816
4885
channel_update_status,
4817
4886
4887
+ announcement_sigs,
4888
+
4818
4889
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
4819
4890
next_local_commitment_tx_fee_info_cached : Mutex :: new ( None ) ,
4820
4891
#[ cfg( any( test, feature = "fuzztarget" ) ) ]
0 commit comments