Skip to content

Commit 5cc400c

Browse files
benthecarmanTheBlueMatt
authored andcommitted
Add helper functions to verify node and channel annoucements
Right now the only real way to verify the node and channel announcements is to call `update_node_from_announcement`/ `update_channel_from_announcement`. If you want to do some processing before you add to your network graph then you need to manually verify the signature. This adds some nice helper functions to make it easier. I tried to do the same for channel update but it did not seem as easy so figured that is fine to punt on since I don't see many people doing manual things with channel updates.
1 parent 498f233 commit 5cc400c

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

lightning/src/routing/gossip.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! The [`NetworkGraph`] stores the network gossip and [`P2PGossipSync`] fetches it from peers
1111
1212
use bitcoin::secp256k1::constants::PUBLIC_KEY_SIZE;
13-
use bitcoin::secp256k1::PublicKey;
13+
use bitcoin::secp256k1::{PublicKey, Verification};
1414
use bitcoin::secp256k1::Secp256k1;
1515
use bitcoin::secp256k1;
1616

@@ -404,6 +404,29 @@ macro_rules! get_pubkey_from_node_id {
404404
}
405405
}
406406

407+
/// Verifies the signature of a [`NodeAnnouncement`].
408+
///
409+
/// Returns an error if it is invalid.
410+
pub fn verify_node_announcement<C: Verification>(msg: &NodeAnnouncement, secp_ctx: &Secp256k1<C>) -> Result<(), LightningError> {
411+
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
412+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.signature, &get_pubkey_from_node_id!(msg.contents.node_id, "node_announcement"), "node_announcement");
413+
414+
Ok(())
415+
}
416+
417+
/// Verifies all signatures included in a [`ChannelAnnouncement`].
418+
///
419+
/// Returns an error if one of the signatures is invalid.
420+
pub fn verify_channel_announcement<C: Verification>(msg: &ChannelAnnouncement, secp_ctx: &Secp256k1<C>) -> Result<(), LightningError> {
421+
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
422+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_1, &get_pubkey_from_node_id!(msg.contents.node_id_1, "channel_announcement"), "channel_announcement");
423+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.node_signature_2, &get_pubkey_from_node_id!(msg.contents.node_id_2, "channel_announcement"), "channel_announcement");
424+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &get_pubkey_from_node_id!(msg.contents.bitcoin_key_1, "channel_announcement"), "channel_announcement");
425+
secp_verify_sig!(secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &get_pubkey_from_node_id!(msg.contents.bitcoin_key_2, "channel_announcement"), "channel_announcement");
426+
427+
Ok(())
428+
}
429+
407430
impl<G: Deref<Target=NetworkGraph<L>>, U: Deref, L: Deref> RoutingMessageHandler for P2PGossipSync<G, U, L>
408431
where U::Target: UtxoLookup, L::Target: Logger
409432
{
@@ -1387,8 +1410,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
13871410
/// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
13881411
/// routing messages from a source using a protocol other than the lightning P2P protocol.
13891412
pub fn update_node_from_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<(), LightningError> {
1390-
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
1391-
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &get_pubkey_from_node_id!(msg.contents.node_id, "node_announcement"), "node_announcement");
1413+
verify_node_announcement(msg, &self.secp_ctx)?;
13921414
self.update_node_from_announcement_intern(&msg.contents, Some(&msg))
13931415
}
13941416

@@ -1451,11 +1473,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
14511473
where
14521474
U::Target: UtxoLookup,
14531475
{
1454-
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
1455-
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_1, &get_pubkey_from_node_id!(msg.contents.node_id_1, "channel_announcement"), "channel_announcement");
1456-
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.node_signature_2, &get_pubkey_from_node_id!(msg.contents.node_id_2, "channel_announcement"), "channel_announcement");
1457-
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_1, &get_pubkey_from_node_id!(msg.contents.bitcoin_key_1, "channel_announcement"), "channel_announcement");
1458-
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.bitcoin_signature_2, &get_pubkey_from_node_id!(msg.contents.bitcoin_key_2, "channel_announcement"), "channel_announcement");
1476+
verify_channel_announcement(msg, &self.secp_ctx)?;
14591477
self.update_channel_from_unsigned_announcement_intern(&msg.contents, Some(msg), utxo_lookup)
14601478
}
14611479

0 commit comments

Comments
 (0)