Skip to content

Commit a87053a

Browse files
committed
Process announcement_signatures messages in Channel and store sigs
Previously we handled most of the logic of announcement_signatures in ChannelManager, rather than Channel. This is somewhat unique as far as our message processing goes, but it also avoided having to pass the node_secret in to the Channel. Eventually, we'll move the node_secret behind the signer anyway, so there isn't much reason for this, and storing the announcement_signatures-provided signatures in the Channel allows us to recreate the channel_announcement later for rebroadcast, which may be useful.
1 parent 3d1c72e commit a87053a

File tree

2 files changed

+56
-34
lines changed

2 files changed

+56
-34
lines changed

lightning/src/ln/channel.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use bitcoin::consensus::encode;
1515

1616
use bitcoin::hashes::Hash;
1717
use bitcoin::hashes::sha256::Hash as Sha256;
18+
use bitcoin::hashes::sha256d::Hash as Sha256d;
1819
use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
1920

2021
use bitcoin::secp256k1::key::{PublicKey,SecretKey};
@@ -420,6 +421,10 @@ pub(super) struct Channel<Signer: Sign> {
420421

421422
channel_update_status: ChannelUpdateStatus,
422423

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+
423428
// We save these values so we can make sure `next_local_commit_tx_fee_msat` and
424429
// `next_remote_commit_tx_fee_msat` properly predict what the next commitment transaction fee will
425430
// be, by comparing the cached values to the fee of the tranaction generated by
@@ -621,6 +626,8 @@ impl<Signer: Sign> Channel<Signer> {
621626

622627
channel_update_status: ChannelUpdateStatus::Enabled,
623628

629+
announcement_sigs: None,
630+
624631
#[cfg(any(test, feature = "fuzztarget"))]
625632
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
626633
#[cfg(any(test, feature = "fuzztarget"))]
@@ -862,6 +869,8 @@ impl<Signer: Sign> Channel<Signer> {
862869

863870
channel_update_status: ChannelUpdateStatus::Enabled,
864871

872+
announcement_sigs: None,
873+
865874
#[cfg(any(test, feature = "fuzztarget"))]
866875
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
867876
#[cfg(any(test, feature = "fuzztarget"))]
@@ -3852,6 +3861,47 @@ impl<Signer: Sign> Channel<Signer> {
38523861
Ok((msg, sig))
38533862
}
38543863

3864+
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> {
3865+
if let Some((their_node_sig, their_bitcoin_sig)) = self.announcement_sigs {
3866+
let were_node_one = announcement.node_id_1 == our_node_id;
3867+
3868+
let our_node_sig = self.secp_ctx.sign(&msghash, our_node_secret);
3869+
Ok(msgs::ChannelAnnouncement {
3870+
node_signature_1: if were_node_one { our_node_sig } else { their_node_sig },
3871+
node_signature_2: if were_node_one { their_node_sig } else { our_node_sig },
3872+
bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { their_bitcoin_sig },
3873+
bitcoin_signature_2: if were_node_one { their_bitcoin_sig } else { our_bitcoin_sig },
3874+
contents: announcement,
3875+
})
3876+
} else {
3877+
Err(ChannelError::Ignore("Attempted to sign channel announcement before we'd received announcement_signatures".to_string()))
3878+
}
3879+
}
3880+
3881+
/// Processes an incoming announcement_signatures message, providing a fully-signed
3882+
/// channel_announcement message which we can broadcast and storing our counterparty's
3883+
/// signatures for later reconstruction/rebroadcast of the channel_announcement.
3884+
pub fn announcement_signatures(&mut self, our_node_secret: &SecretKey, our_node_id: PublicKey, chain_hash: BlockHash, msg: &msgs::AnnouncementSignatures) -> Result<msgs::ChannelAnnouncement, ChannelError> {
3885+
let (announcement, our_bitcoin_sig) = self.get_channel_announcement(our_node_id.clone(), chain_hash)?;
3886+
3887+
let msghash = hash_to_message!(&Sha256d::hash(&announcement.encode()[..])[..]);
3888+
3889+
if self.secp_ctx.verify(&msghash, &msg.node_signature, &self.get_counterparty_node_id()).is_err() {
3890+
return Err(ChannelError::Close(format!(
3891+
"Bad announcement_signatures. Failed to verify node_signature. UnsignedChannelAnnouncement used for verification is {:?}. their_node_key is {:?}",
3892+
&announcement, self.get_counterparty_node_id())));
3893+
}
3894+
if self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, self.counterparty_funding_pubkey()).is_err() {
3895+
return Err(ChannelError::Close(format!(
3896+
"Bad announcement_signatures. Failed to verify bitcoin_signature. UnsignedChannelAnnouncement used for verification is {:?}. their_bitcoin_key is ({:?})",
3897+
&announcement, self.counterparty_funding_pubkey())));
3898+
}
3899+
3900+
self.announcement_sigs = Some((msg.node_signature, msg.bitcoin_signature));
3901+
3902+
self.sign_channel_announcement(our_node_secret, our_node_id, msghash, announcement, our_bitcoin_sig)
3903+
}
3904+
38553905
/// May panic if called on a channel that wasn't immediately-previously
38563906
/// self.remove_uncommitted_htlcs_and_mark_paused()'d
38573907
pub fn get_channel_reestablish<L: Deref>(&self, logger: &L) -> msgs::ChannelReestablish where L::Target: Logger {
@@ -4565,7 +4615,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
45654615

45664616
self.channel_update_status.write(writer)?;
45674617

4568-
write_tlv_fields!(writer, {}, {});
4618+
write_tlv_fields!(writer, {}, {(0, self.announcement_sigs)});
45694619

45704620
Ok(())
45714621
}
@@ -4737,7 +4787,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
47374787

47384788
let channel_update_status = Readable::read(reader)?;
47394789

4740-
read_tlv_fields!(reader, {}, {});
4790+
let mut announcement_sigs = None;
4791+
read_tlv_fields!(reader, {}, {(0, announcement_sigs)});
47414792

47424793
let mut secp_ctx = Secp256k1::new();
47434794
secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());
@@ -4815,6 +4866,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
48154866

48164867
channel_update_status,
48174868

4869+
announcement_sigs,
4870+
48184871
#[cfg(any(test, feature = "fuzztarget"))]
48194872
next_local_commitment_tx_fee_info_cached: Mutex::new(None),
48204873
#[cfg(any(test, feature = "fuzztarget"))]

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,39 +3301,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33013301
return Err(MsgHandleErrInternal::from_no_close(LightningError{err: "Got an announcement_signatures before we were ready for it".to_owned(), action: msgs::ErrorAction::IgnoreError}));
33023302
}
33033303

3304-
let our_node_id = self.get_our_node_id();
3305-
let (announcement, our_bitcoin_sig) =
3306-
try_chan_entry!(self, chan.get_mut().get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone()), channel_state, chan);
3307-
3308-
let were_node_one = announcement.node_id_1 == our_node_id;
3309-
let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
3310-
{
3311-
let their_node_key = if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 };
3312-
let their_bitcoin_key = if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 };
3313-
match (self.secp_ctx.verify(&msghash, &msg.node_signature, their_node_key),
3314-
self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, their_bitcoin_key)) {
3315-
(Err(e), _) => {
3316-
let chan_err: ChannelError = ChannelError::Close(format!("Bad announcement_signatures. Failed to verify node_signature: {:?}. Maybe using different node_secret for transport and routing msg? UnsignedChannelAnnouncement used for verification is {:?}. their_node_key is {:?}", e, &announcement, their_node_key));
3317-
try_chan_entry!(self, Err(chan_err), channel_state, chan);
3318-
},
3319-
(_, Err(e)) => {
3320-
let chan_err: ChannelError = ChannelError::Close(format!("Bad announcement_signatures. Failed to verify bitcoin_signature: {:?}. UnsignedChannelAnnouncement used for verification is {:?}. their_bitcoin_key is ({:?})", e, &announcement, their_bitcoin_key));
3321-
try_chan_entry!(self, Err(chan_err), channel_state, chan);
3322-
},
3323-
_ => {}
3324-
}
3325-
}
3326-
3327-
let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
3328-
33293304
channel_state.pending_msg_events.push(events::MessageSendEvent::BroadcastChannelAnnouncement {
3330-
msg: msgs::ChannelAnnouncement {
3331-
node_signature_1: if were_node_one { our_node_sig } else { msg.node_signature },
3332-
node_signature_2: if were_node_one { msg.node_signature } else { our_node_sig },
3333-
bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { msg.bitcoin_signature },
3334-
bitcoin_signature_2: if were_node_one { msg.bitcoin_signature } else { our_bitcoin_sig },
3335-
contents: announcement,
3336-
},
3305+
msg: try_chan_entry!(self, chan.get_mut().announcement_signatures(&self.our_network_key, self.get_our_node_id(), self.genesis_hash.clone(), msg), channel_state, chan),
33373306
update_msg: self.get_channel_update(chan.get()).unwrap(), // can only fail if we're not in a ready state
33383307
});
33393308
},

0 commit comments

Comments
 (0)