Skip to content

Commit c89b96a

Browse files
committed
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.
1 parent f5ee8c2 commit c89b96a

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

lightning-invoice/src/de.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,12 +602,12 @@ impl FromBase32 for PrivateRoute {
602602

603603
let hop = RouteHintHop {
604604
src_node_id: PublicKey::from_slice(&hop_bytes[0..33])?,
605-
short_channel_id: parse_int_be(&channel_id, 256).expect("short chan ID slice too big?"),
605+
short_channel_id: u64::from_be_bytes(channel_id),
606606
fees: RoutingFees {
607-
base_msat: parse_int_be(&hop_bytes[41..45], 256).expect("slice too big?"),
608-
proportional_millionths: parse_int_be(&hop_bytes[45..49], 256).expect("slice too big?"),
607+
base_msat: u32::from_be_bytes(hop_bytes[41..45].try_into().expect("slice too big?")),
608+
proportional_millionths: u32::from_be_bytes(hop_bytes[45..49].try_into().expect("slice too big?")),
609609
},
610-
cltv_expiry_delta: parse_int_be(&hop_bytes[49..51], 256).expect("slice too big?"),
610+
cltv_expiry_delta: u16::from_be_bytes(hop_bytes[49..51].try_into().expect("slice too big?")),
611611
htlc_minimum_msat: None,
612612
htlc_maximum_msat: None,
613613
};
@@ -916,7 +916,6 @@ mod test {
916916
use lightning::routing::router::{RouteHint, RouteHintHop};
917917
use crate::PrivateRoute;
918918
use bech32::FromBase32;
919-
use crate::de::parse_int_be;
920919

921920
let input = from_bech32(
922921
"q20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqa\
@@ -932,7 +931,7 @@ mod test {
932931
0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
933932
][..]
934933
).unwrap(),
935-
short_channel_id: parse_int_be(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 256).expect("short chan ID slice too big?"),
934+
short_channel_id: 0x0102030405060708,
936935
fees: RoutingFees {
937936
base_msat: 1,
938937
proportional_millionths: 20,
@@ -949,7 +948,7 @@ mod test {
949948
0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
950949
][..]
951950
).unwrap(),
952-
short_channel_id: parse_int_be(&[0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a], 256).expect("short chan ID slice too big?"),
951+
short_channel_id: 0x030405060708090a,
953952
fees: RoutingFees {
954953
base_msat: 2,
955954
proportional_millionths: 30,

lightning-invoice/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ mod test {
20642064
let route_1 = RouteHint(vec![
20652065
RouteHintHop {
20662066
src_node_id: public_key,
2067-
short_channel_id: de::parse_int_be(&[123; 8], 256).expect("short chan ID slice too big?"),
2067+
short_channel_id: u64::from_be_bytes([123; 8]),
20682068
fees: RoutingFees {
20692069
base_msat: 2,
20702070
proportional_millionths: 1,
@@ -2075,7 +2075,7 @@ mod test {
20752075
},
20762076
RouteHintHop {
20772077
src_node_id: public_key,
2078-
short_channel_id: de::parse_int_be(&[42; 8], 256).expect("short chan ID slice too big?"),
2078+
short_channel_id: u64::from_be_bytes([42; 8]),
20792079
fees: RoutingFees {
20802080
base_msat: 3,
20812081
proportional_millionths: 2,
@@ -2100,7 +2100,7 @@ mod test {
21002100
},
21012101
RouteHintHop {
21022102
src_node_id: public_key,
2103-
short_channel_id: de::parse_int_be(&[1; 8], 256).expect("short chan ID slice too big?"),
2103+
short_channel_id: u64::from_be_bytes([1; 8]),
21042104
fees: RoutingFees {
21052105
base_msat: 5,
21062106
proportional_millionths: 4,

0 commit comments

Comments
 (0)