From c89b96a4b9dec12b9715bf3c30badaf102116e30 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 13 Mar 2024 19:16:20 +0000 Subject: [PATCH] Use std's `from_be_bytes` rather than our `to_int_be` for int conv `lightning-invoice` was mostly written before std's `from_be_bytes` was stabilized, so used its own `to_int_be` utility to do int conversions from `u8` arrays. Now that the std option has been stable for quite some time, we should juse use it instead. --- lightning-invoice/src/de.rs | 13 ++++++------- lightning-invoice/src/lib.rs | 6 +++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lightning-invoice/src/de.rs b/lightning-invoice/src/de.rs index c75373ea9..b05563912 100644 --- a/lightning-invoice/src/de.rs +++ b/lightning-invoice/src/de.rs @@ -602,12 +602,12 @@ impl FromBase32 for PrivateRoute { let hop = RouteHintHop { src_node_id: PublicKey::from_slice(&hop_bytes[0..33])?, - short_channel_id: parse_int_be(&channel_id, 256).expect("short chan ID slice too big?"), + short_channel_id: u64::from_be_bytes(channel_id), fees: RoutingFees { - base_msat: parse_int_be(&hop_bytes[41..45], 256).expect("slice too big?"), - proportional_millionths: parse_int_be(&hop_bytes[45..49], 256).expect("slice too big?"), + base_msat: u32::from_be_bytes(hop_bytes[41..45].try_into().expect("slice too big?")), + proportional_millionths: u32::from_be_bytes(hop_bytes[45..49].try_into().expect("slice too big?")), }, - cltv_expiry_delta: parse_int_be(&hop_bytes[49..51], 256).expect("slice too big?"), + cltv_expiry_delta: u16::from_be_bytes(hop_bytes[49..51].try_into().expect("slice too big?")), htlc_minimum_msat: None, htlc_maximum_msat: None, }; @@ -916,7 +916,6 @@ mod test { use lightning::routing::router::{RouteHint, RouteHintHop}; use crate::PrivateRoute; use bech32::FromBase32; - use crate::de::parse_int_be; let input = from_bech32( "q20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqa\ @@ -932,7 +931,7 @@ mod test { 0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55 ][..] ).unwrap(), - short_channel_id: parse_int_be(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 256).expect("short chan ID slice too big?"), + short_channel_id: 0x0102030405060708, fees: RoutingFees { base_msat: 1, proportional_millionths: 20, @@ -949,7 +948,7 @@ mod test { 0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55 ][..] ).unwrap(), - short_channel_id: parse_int_be(&[0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a], 256).expect("short chan ID slice too big?"), + short_channel_id: 0x030405060708090a, fees: RoutingFees { base_msat: 2, proportional_millionths: 30, diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index 5b3269114..a029c5f3d 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -2064,7 +2064,7 @@ mod test { let route_1 = RouteHint(vec![ RouteHintHop { src_node_id: public_key, - short_channel_id: de::parse_int_be(&[123; 8], 256).expect("short chan ID slice too big?"), + short_channel_id: u64::from_be_bytes([123; 8]), fees: RoutingFees { base_msat: 2, proportional_millionths: 1, @@ -2075,7 +2075,7 @@ mod test { }, RouteHintHop { src_node_id: public_key, - short_channel_id: de::parse_int_be(&[42; 8], 256).expect("short chan ID slice too big?"), + short_channel_id: u64::from_be_bytes([42; 8]), fees: RoutingFees { base_msat: 3, proportional_millionths: 2, @@ -2100,7 +2100,7 @@ mod test { }, RouteHintHop { src_node_id: public_key, - short_channel_id: de::parse_int_be(&[1; 8], 256).expect("short chan ID slice too big?"), + short_channel_id: u64::from_be_bytes([1; 8]), fees: RoutingFees { base_msat: 5, proportional_millionths: 4, -- 2.39.5