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