@@ -77,51 +77,11 @@ mod prelude {
7777
7878use crate :: prelude:: * ;
7979
80- /// Interface to write `Fe32`s into a sink
81- pub trait WriteBase32 {
82- /// Write error
83- type Err : fmt:: Debug ;
84-
85- /// Write a `Fe32` slice
86- fn write ( & mut self , data : & Vec < Fe32 > ) -> Result < ( ) , Self :: Err > {
87- for b in data {
88- self . write_fe32 ( * b) ?;
89- }
90- Ok ( ( ) )
91- }
92-
93- /// Write a single `Fe32`
94- fn write_fe32 ( & mut self , data : Fe32 ) -> Result < ( ) , Self :: Err > ;
95- }
96-
97- /// A trait for converting a value to a type `T` that represents a `Fe32` slice.
98- pub trait ToBase32 {
99- /// Convert `Self` to base32 vector
100- fn to_base32 ( & self ) -> Vec < Fe32 > {
101- let mut vec = Vec :: new ( ) ;
102- self . write_base32 ( & mut vec) . unwrap ( ) ;
103- vec
104- }
105-
106- /// Encode as base32 and write it to the supplied writer
107- /// Implementations shouldn't allocate.
108- fn write_base32 < W : WriteBase32 > ( & self , writer : & mut W ) -> Result < ( ) , <W as WriteBase32 >:: Err > ;
109- }
110-
111- /// Interface to calculate the length of the base32 representation before actually serializing
112- pub trait Base32Len : ToBase32 {
113- /// Calculate the base32 serialized length
114- fn base32_len ( & self ) -> usize ;
115- }
116-
117- /// Trait for paring/converting base32 slice. It is the reciprocal of `ToBase32`.
118- pub trait FromBase32 : Sized {
119- /// The associated error which can be returned from parsing (e.g. because of bad padding).
120- type Err ;
121-
122- /// Convert a base32 slice to `Self`.
123- fn from_base32 ( b32 : & [ Fe32 ] ) -> Result < Self , Self :: Err > ;
124- }
80+ /// Re-export serialization traits
81+ #[ cfg( fuzzing) ]
82+ pub use crate :: ser:: Base32Iterable ;
83+ #[ cfg( fuzzing) ]
84+ pub use crate :: de:: FromBase32 ;
12585
12686/// Errors that indicate what is wrong with the invoice. They have some granularity for debug
12787/// reasons, but should generally result in an "invalid BOLT11 invoice" message for the user.
@@ -347,6 +307,15 @@ pub struct RawHrp {
347307 pub si_prefix : Option < SiPrefix > ,
348308}
349309
310+ impl RawHrp {
311+ /// Convert to bech32::Hrp
312+ pub fn to_hrp ( & self ) -> bech32:: Hrp {
313+ let hrp_str = self . to_string ( ) ;
314+ let s = core:: str:: from_utf8 ( & hrp_str. as_bytes ( ) ) . expect ( "asserted to be ASCII" ) ;
315+ bech32:: Hrp :: parse_unchecked ( s)
316+ }
317+ }
318+
350319/// Data of the [`RawBolt11Invoice`] that is encoded in the data part
351320#[ derive( Eq , PartialEq , Debug , Clone , Hash , Ord , PartialOrd ) ]
352321pub struct RawDataPart {
@@ -1024,6 +993,8 @@ macro_rules! find_all_extract {
1024993impl RawBolt11Invoice {
1025994 /// Hash the HRP as bytes and signatureless data part.
1026995 fn hash_from_parts ( hrp_bytes : & [ u8 ] , data_without_signature : & [ Fe32 ] ) -> [ u8 ; 32 ] {
996+ use crate :: de:: FromBase32 ;
997+
1027998 let mut preimage = Vec :: < u8 > :: from ( hrp_bytes) ;
1028999
10291000 let mut data_part = Vec :: from ( data_without_signature) ;
@@ -1048,9 +1019,11 @@ impl RawBolt11Invoice {
10481019
10491020 /// Calculate the hash of the encoded `RawBolt11Invoice` which should be signed.
10501021 pub fn signable_hash ( & self ) -> [ u8 ; 32 ] {
1022+ use crate :: ser:: Base32Iterable ;
1023+
10511024 RawBolt11Invoice :: hash_from_parts (
10521025 self . hrp . to_string ( ) . as_bytes ( ) ,
1053- & self . data . to_base32 ( )
1026+ & self . data . fe_iter ( ) . collect :: < Vec < Fe32 > > ( ) ,
10541027 )
10551028 }
10561029
0 commit comments