@@ -16,6 +16,7 @@ use bitcoin::secp256k1::{PublicKey, Verification};
16
16
use bitcoin:: secp256k1:: Secp256k1 ;
17
17
use bitcoin:: secp256k1;
18
18
19
+ use bitcoin:: hashes:: sha256:: Hash as Sha256Hash ;
19
20
use bitcoin:: hashes:: sha256d:: Hash as Sha256dHash ;
20
21
use bitcoin:: hashes:: Hash ;
21
22
use bitcoin:: hashes:: hex:: FromHex ;
@@ -412,11 +413,17 @@ macro_rules! get_pubkey_from_node_id {
412
413
}
413
414
}
414
415
416
+ fn message_sha256d_hash < M : Writeable > ( msg : & M ) -> [ u8 ; 32 ] {
417
+ let mut engine = Sha256Hash :: engine ( ) ;
418
+ msg. write ( & mut engine) . expect ( "In-memory structs should not fail to serialize" ) ;
419
+ Sha256dHash :: from_engine ( engine) . into_inner ( )
420
+ }
421
+
415
422
/// Verifies the signature of a [`NodeAnnouncement`].
416
423
///
417
424
/// Returns an error if it is invalid.
418
425
pub fn verify_node_announcement < C : Verification > ( msg : & NodeAnnouncement , secp_ctx : & Secp256k1 < C > ) -> Result < ( ) , LightningError > {
419
- let msg_hash = hash_to_message ! ( & Sha256dHash :: hash ( & msg. contents. encode ( ) [ .. ] ) [ ..] ) ;
426
+ let msg_hash = hash_to_message ! ( & message_sha256d_hash ( & msg. contents) [ ..] ) ;
420
427
secp_verify_sig ! ( secp_ctx, & msg_hash, & msg. signature, & get_pubkey_from_node_id!( msg. contents. node_id, "node_announcement" ) , "node_announcement" ) ;
421
428
422
429
Ok ( ( ) )
@@ -426,7 +433,7 @@ pub fn verify_node_announcement<C: Verification>(msg: &NodeAnnouncement, secp_ct
426
433
///
427
434
/// Returns an error if one of the signatures is invalid.
428
435
pub fn verify_channel_announcement < C : Verification > ( msg : & ChannelAnnouncement , secp_ctx : & Secp256k1 < C > ) -> Result < ( ) , LightningError > {
429
- let msg_hash = hash_to_message ! ( & Sha256dHash :: hash ( & msg. contents. encode ( ) [ .. ] ) [ ..] ) ;
436
+ let msg_hash = hash_to_message ! ( & message_sha256d_hash ( & msg. contents) [ ..] ) ;
430
437
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" ) ;
431
438
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" ) ;
432
439
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" ) ;
@@ -1969,7 +1976,7 @@ impl<L: Deref> NetworkGraph<L> where L::Target: Logger {
1969
1976
} }
1970
1977
}
1971
1978
1972
- let msg_hash = hash_to_message ! ( & Sha256dHash :: hash ( & msg. encode ( ) [ .. ] ) [ ..] ) ;
1979
+ let msg_hash = hash_to_message ! ( & message_sha256d_hash ( & msg) [ ..] ) ;
1973
1980
if msg. flags & 1 == 1 {
1974
1981
check_update_latest ! ( channel. two_to_one) ;
1975
1982
if let Some ( sig) = sig {
0 commit comments