@@ -18,8 +18,6 @@ use lightning::ln::PaymentSecret;
18
18
use lightning:: routing:: gossip:: RoutingFees ;
19
19
use lightning:: routing:: router:: { RouteHint , RouteHintHop } ;
20
20
21
- use num_traits:: { CheckedAdd , CheckedMul } ;
22
-
23
21
use secp256k1:: ecdsa:: { RecoveryId , RecoverableSignature } ;
24
22
use secp256k1:: PublicKey ;
25
23
@@ -356,7 +354,7 @@ impl FromBase32 for PositiveTimestamp {
356
354
if b32. len ( ) != 7 {
357
355
return Err ( Bolt11ParseError :: InvalidSliceLength ( "PositiveTimestamp::from_base32()" . into ( ) ) ) ;
358
356
}
359
- let timestamp: u64 = parse_int_be ( b32, 32 )
357
+ let timestamp: u64 = parse_u64_be ( b32)
360
358
. expect ( "7*5bit < 64bit, no overflow possible" ) ;
361
359
match PositiveTimestamp :: from_unix_timestamp ( timestamp) {
362
360
Ok ( t) => Ok ( t) ,
@@ -382,16 +380,17 @@ impl FromBase32 for Bolt11InvoiceSignature {
382
380
}
383
381
}
384
382
385
- pub ( crate ) fn parse_int_be < T , U > ( digits : & [ U ] , base : T ) -> Option < T >
386
- where T : CheckedAdd + CheckedMul + From < u8 > + Default ,
387
- U : Into < u8 > + Copy
388
- {
389
- digits. iter ( ) . fold ( Some ( Default :: default ( ) ) , |acc, b|
390
- acc
391
- . and_then ( |x| x. checked_mul ( & base) )
392
- . and_then ( |x| x. checked_add ( & ( Into :: < u8 > :: into ( * b) ) . into ( ) ) )
393
- )
394
- }
383
+ macro_rules! define_parse_int_be { ( $name: ident, $ty: ty) => {
384
+ fn $name( digits: & [ u5] ) -> Option <$ty> {
385
+ digits. iter( ) . fold( Some ( Default :: default ( ) ) , |acc, b|
386
+ acc
387
+ . and_then( |x| x. checked_mul( 32 ) )
388
+ . and_then( |x| x. checked_add( ( Into :: <u8 >:: into( * b) ) . into( ) ) )
389
+ )
390
+ }
391
+ } }
392
+ define_parse_int_be ! ( parse_u16_be, u16 ) ;
393
+ define_parse_int_be ! ( parse_u64_be, u64 ) ;
395
394
396
395
fn parse_tagged_parts ( data : & [ u5 ] ) -> Result < Vec < RawTaggedField > , Bolt11ParseError > {
397
396
let mut parts = Vec :: < RawTaggedField > :: new ( ) ;
@@ -404,7 +403,7 @@ fn parse_tagged_parts(data: &[u5]) -> Result<Vec<RawTaggedField>, Bolt11ParseErr
404
403
405
404
// Ignore tag at data[0], it will be handled in the TaggedField parsers and
406
405
// parse the length to find the end of the tagged field's data
407
- let len = parse_int_be ( & data[ 1 ..3 ] , 32 ) . expect ( "can't overflow" ) ;
406
+ let len = parse_u16_be ( & data[ 1 ..3 ] ) . expect ( "can't overflow" ) as usize ;
408
407
let last_element = 3 + len;
409
408
410
409
if data. len ( ) < last_element {
@@ -517,7 +516,7 @@ impl FromBase32 for ExpiryTime {
517
516
type Err = Bolt11ParseError ;
518
517
519
518
fn from_base32 ( field_data : & [ u5 ] ) -> Result < ExpiryTime , Bolt11ParseError > {
520
- match parse_int_be :: < u64 , u5 > ( field_data, 32 )
519
+ match parse_u64_be ( field_data)
521
520
. map ( ExpiryTime :: from_seconds)
522
521
{
523
522
Some ( t) => Ok ( t) ,
@@ -530,7 +529,7 @@ impl FromBase32 for MinFinalCltvExpiryDelta {
530
529
type Err = Bolt11ParseError ;
531
530
532
531
fn from_base32 ( field_data : & [ u5 ] ) -> Result < MinFinalCltvExpiryDelta , Bolt11ParseError > {
533
- let expiry = parse_int_be :: < u64 , u5 > ( field_data, 32 ) ;
532
+ let expiry = parse_u64_be ( field_data) ;
534
533
if let Some ( expiry) = expiry {
535
534
Ok ( MinFinalCltvExpiryDelta ( expiry) )
536
535
} else {
@@ -602,12 +601,12 @@ impl FromBase32 for PrivateRoute {
602
601
603
602
let hop = RouteHintHop {
604
603
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?" ) ,
604
+ short_channel_id : u64 :: from_be_bytes ( channel_id) ,
606
605
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?" ) ,
606
+ base_msat : u32 :: from_be_bytes ( hop_bytes[ 41 ..45 ] . try_into ( ) . expect ( "slice too big?" ) ) ,
607
+ proportional_millionths : u32 :: from_be_bytes ( hop_bytes[ 45 ..49 ] . try_into ( ) . expect ( "slice too big?" ) ) ,
609
608
} ,
610
- cltv_expiry_delta : parse_int_be ( & hop_bytes[ 49 ..51 ] , 256 ) . expect ( "slice too big?" ) ,
609
+ cltv_expiry_delta : u16 :: from_be_bytes ( hop_bytes[ 49 ..51 ] . try_into ( ) . expect ( "slice too big?" ) ) ,
611
610
htlc_minimum_msat : None ,
612
611
htlc_maximum_msat : None ,
613
612
} ;
@@ -761,12 +760,16 @@ mod test {
761
760
762
761
#[ test]
763
762
fn test_parse_int_from_bytes_be ( ) {
764
- use crate :: de:: parse_int_be;
765
-
766
- assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 1 , 2 , 3 , 4 ] , 256 ) , Some ( 16909060 ) ) ;
767
- assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 1 , 3 ] , 32 ) , Some ( 35 ) ) ;
768
- assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 255 , 255 , 255 , 255 ] , 256 ) , Some ( 4294967295 ) ) ;
769
- assert_eq ! ( parse_int_be:: <u32 , u8 >( & [ 1 , 0 , 0 , 0 , 0 ] , 256 ) , None ) ;
763
+ use crate :: de:: parse_u16_be;
764
+
765
+ assert_eq ! ( parse_u16_be( & [
766
+ u5:: try_from_u8( 1 ) . unwrap( ) , u5:: try_from_u8( 2 ) . unwrap( ) ,
767
+ u5:: try_from_u8( 3 ) . unwrap( ) , u5:: try_from_u8( 4 ) . unwrap( ) ]
768
+ ) , Some ( 34916 ) ) ;
769
+ assert_eq ! ( parse_u16_be( & [
770
+ u5:: try_from_u8( 2 ) . unwrap( ) , u5:: try_from_u8( 0 ) . unwrap( ) ,
771
+ u5:: try_from_u8( 0 ) . unwrap( ) , u5:: try_from_u8( 0 ) . unwrap( ) ]
772
+ ) , None ) ;
770
773
}
771
774
772
775
#[ test]
@@ -916,7 +919,6 @@ mod test {
916
919
use lightning:: routing:: router:: { RouteHint , RouteHintHop } ;
917
920
use crate :: PrivateRoute ;
918
921
use bech32:: FromBase32 ;
919
- use crate :: de:: parse_int_be;
920
922
921
923
let input = from_bech32 (
922
924
"q20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqa\
@@ -932,7 +934,7 @@ mod test {
932
934
0x7e , 0x14 , 0x8f , 0x78 , 0xc7 , 0x72 , 0x55
933
935
] [ ..]
934
936
) . unwrap ( ) ,
935
- short_channel_id : parse_int_be ( & [ 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 ] , 256 ) . expect ( "short chan ID slice too big?" ) ,
937
+ short_channel_id : 0x0102030405060708 ,
936
938
fees : RoutingFees {
937
939
base_msat : 1 ,
938
940
proportional_millionths : 20 ,
@@ -949,7 +951,7 @@ mod test {
949
951
0x7e , 0x14 , 0x8f , 0x78 , 0xc7 , 0x72 , 0x55
950
952
] [ ..]
951
953
) . unwrap ( ) ,
952
- short_channel_id : parse_int_be ( & [ 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a ] , 256 ) . expect ( "short chan ID slice too big?" ) ,
954
+ short_channel_id : 0x030405060708090a ,
953
955
fees : RoutingFees {
954
956
base_msat : 2 ,
955
957
proportional_millionths : 30 ,
0 commit comments