@@ -30,7 +30,7 @@ extern crate serde;
3030#[ cfg( feature = "std" ) ]
3131use std:: time:: SystemTime ;
3232
33- use bech32:: primitives:: decode:: CheckedHrpstringError ;
33+ use bech32:: primitives:: decode:: { CheckedHrpstringError , UncheckedHrpstring } ;
3434use bech32:: Fe32 ;
3535use bitcoin:: hashes:: { sha256, Hash } ;
3636use bitcoin:: { Address , Network , PubkeyHash , ScriptHash , WitnessProgram , WitnessVersion } ;
@@ -80,6 +80,10 @@ use crate::prelude::*;
8080pub use crate :: de:: FromBase32 ;
8181#[ cfg( fuzzing) ]
8282pub use crate :: ser:: Base32Iterable ;
83+ #[ cfg( not( fuzzing) ) ]
84+ use crate :: de:: FromBase32 ;
85+ #[ cfg( not( fuzzing) ) ]
86+ use crate :: ser:: Base32Iterable ;
8387
8488/// Errors that indicate what is wrong with the invoice. They have some granularity for debug
8589/// reasons, but should generally result in an "invalid BOLT11 invoice" message for the user.
@@ -1189,6 +1193,31 @@ impl RawBolt11Invoice {
11891193 pub fn currency ( & self ) -> Currency {
11901194 self . hrp . currency . clone ( )
11911195 }
1196+
1197+ /// Convert to ascii bytes with HRP prefix and base32 encoded data part.
1198+ /// Can be used to transmit unsigned invoices for remote signing.
1199+ pub fn to_ascii ( & self ) -> Vec < u8 > {
1200+ self . hrp . to_string ( ) . into_bytes ( ) . into_iter ( )
1201+ . chain ( self . data . fe_iter ( ) . map ( |e| e. to_char ( ) as u8 ) ) . collect ( )
1202+ }
1203+
1204+ /// Convert from ascii bytes with HRP prefix and base32 encoded data part.
1205+ /// Can be used to receive unsigned invoices for remote signing.
1206+ pub fn from_ascii ( s : & str ) -> Result < Self , Bolt11ParseError > {
1207+ let parsed = UncheckedHrpstring :: new ( s)
1208+ . map_err ( |_| Bolt11ParseError :: MalformedHRP ) ?;
1209+ let hrp = parsed. hrp ( ) ;
1210+ let raw_hrp: RawHrp = hrp. to_string ( ) . to_lowercase ( ) . parse ( ) ?;
1211+
1212+ let data = parsed. data_part_ascii ( ) . iter ( ) . map ( |c| Fe32 :: from_char ( * c as char ) ) . collect :: < Result < Vec < _ > , _ > > ( )
1213+ . map_err ( |_| Bolt11ParseError :: MalformedHRP ) ?;
1214+ let data_part = RawDataPart :: from_base32 ( & data) ?;
1215+
1216+ Ok ( Self {
1217+ hrp : raw_hrp,
1218+ data : data_part,
1219+ } )
1220+ }
11921221}
11931222
11941223impl PositiveTimestamp {
0 commit comments