Upgrade rust-bitcoin to 0.31
[rust-lightning] / lightning-invoice / src / de.rs
1 #[cfg(feature = "std")]
2 use std::error;
3 #[cfg(not(feature = "std"))]
4 use core::convert::TryFrom;
5 use core::fmt;
6 use core::fmt::{Display, Formatter};
7 use core::num::ParseIntError;
8 use core::str;
9 use core::str::FromStr;
10
11 use bech32::{u5, FromBase32};
12
13 use bitcoin::{PubkeyHash, ScriptHash, WitnessVersion};
14 use bitcoin::hashes::Hash;
15 use bitcoin::hashes::sha256;
16 use crate::prelude::*;
17 use lightning::ln::types::PaymentSecret;
18 use lightning::routing::gossip::RoutingFees;
19 use lightning::routing::router::{RouteHint, RouteHintHop};
20
21 use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
22 use secp256k1::PublicKey;
23
24 use super::{Bolt11Invoice, Sha256, TaggedField, ExpiryTime, MinFinalCltvExpiryDelta, Fallback, PayeePubKey, Bolt11InvoiceSignature, PositiveTimestamp,
25         Bolt11SemanticError, PrivateRoute, Bolt11ParseError, ParseOrSemanticError, Description, RawTaggedField, Currency, RawHrp, SiPrefix, RawBolt11Invoice,
26         constants, SignedRawBolt11Invoice, RawDataPart, Bolt11InvoiceFeatures};
27
28 use self::hrp_sm::parse_hrp;
29
30 /// State machine to parse the hrp
31 mod hrp_sm {
32         use core::ops::Range;
33
34         #[derive(PartialEq, Eq, Debug)]
35         enum States {
36                 Start,
37                 ParseL,
38                 ParseN,
39                 ParseCurrencyPrefix,
40                 ParseAmountNumber,
41                 ParseAmountSiPrefix,
42         }
43
44         impl States {
45                 fn next_state(&self, read_byte: u8) -> Result<States, super::Bolt11ParseError> {
46                         let read_symbol = match char::from_u32(read_byte.into()) {
47                                 Some(symb) if symb.is_ascii() => symb,
48                                 _ => return Err(super::Bolt11ParseError::MalformedHRP),
49                         };
50                         match *self {
51                                 States::Start => {
52                                         if read_symbol == 'l' {
53                                                 Ok(States::ParseL)
54                                         } else {
55                                                 Err(super::Bolt11ParseError::MalformedHRP)
56                                         }
57                                 }
58                                 States::ParseL => {
59                                         if read_symbol == 'n' {
60                                                 Ok(States::ParseN)
61                                         } else {
62                                                 Err(super::Bolt11ParseError::MalformedHRP)
63                                         }
64                                 },
65                                 States::ParseN => {
66                                         if !read_symbol.is_numeric() {
67                                                 Ok(States::ParseCurrencyPrefix)
68                                         } else {
69                                                 Ok(States::ParseAmountNumber)
70                                         }
71                                 },
72                                 States::ParseCurrencyPrefix => {
73                                         if !read_symbol.is_numeric() {
74                                                 Ok(States::ParseCurrencyPrefix)
75                                         } else {
76                                                 Ok(States::ParseAmountNumber)
77                                         }
78                                 },
79                                 States::ParseAmountNumber => {
80                                         if read_symbol.is_numeric() {
81                                                 Ok(States::ParseAmountNumber)
82                                         } else if ['m', 'u', 'n', 'p'].contains(&read_symbol) {
83                                                 Ok(States::ParseAmountSiPrefix)
84                                         } else {
85                                                 Err(super::Bolt11ParseError::UnknownSiPrefix)
86                                         }
87                                 },
88                                 States::ParseAmountSiPrefix => Err(super::Bolt11ParseError::MalformedHRP),
89                         }
90                 }
91
92                 fn is_final(&self) -> bool {
93                         !(*self == States::ParseL || *self == States::ParseN)
94                 }
95         }
96
97
98         struct StateMachine {
99                 state: States,
100                 position: usize,
101                 currency_prefix: Option<Range<usize>>,
102                 amount_number: Option<Range<usize>>,
103                 amount_si_prefix: Option<Range<usize>>,
104         }
105
106         impl StateMachine {
107                 fn new() -> StateMachine {
108                         StateMachine {
109                                 state: States::Start,
110                                 position: 0,
111                                 currency_prefix: None,
112                                 amount_number: None,
113                                 amount_si_prefix: None,
114                         }
115                 }
116
117                 fn update_range(range: &mut Option<Range<usize>>, position: usize) {
118                         let new_range = match *range {
119                                 None => Range {start: position, end: position + 1},
120                                 Some(ref r) => Range {start: r.start, end: r.end + 1},
121                         };
122                         *range = Some(new_range);
123                 }
124
125                 fn step(&mut self, c: u8) -> Result<(), super::Bolt11ParseError> {
126                         let next_state = self.state.next_state(c)?;
127                         match next_state {
128                                 States::ParseCurrencyPrefix => {
129                                         StateMachine::update_range(&mut self.currency_prefix, self.position)
130                                 }
131                                 States::ParseAmountNumber => {
132                                         StateMachine::update_range(&mut self.amount_number, self.position)
133                                 },
134                                 States::ParseAmountSiPrefix => {
135                                         StateMachine::update_range(&mut self.amount_si_prefix, self.position)
136                                 },
137                                 _ => {}
138                         }
139
140                         self.position += 1;
141                         self.state = next_state;
142                         Ok(())
143                 }
144
145                 fn is_final(&self) -> bool {
146                         self.state.is_final()
147                 }
148
149                 fn currency_prefix(&self) -> &Option<Range<usize>> {
150                         &self.currency_prefix
151                 }
152
153                 fn amount_number(&self) -> &Option<Range<usize>> {
154                         &self.amount_number
155                 }
156
157                 fn amount_si_prefix(&self) -> &Option<Range<usize>> {
158                         &self.amount_si_prefix
159                 }
160         }
161
162         pub fn parse_hrp(input: &str) -> Result<(&str, &str, &str), super::Bolt11ParseError> {
163                 let mut sm = StateMachine::new();
164                 for c in input.bytes() {
165                         sm.step(c)?;
166                 }
167
168                 if !sm.is_final() {
169                         return Err(super::Bolt11ParseError::MalformedHRP);
170                 }
171
172                 let currency = sm.currency_prefix().clone()
173                         .map(|r| &input[r]).unwrap_or("");
174                 let amount = sm.amount_number().clone()
175                         .map(|r| &input[r]).unwrap_or("");
176                 let si = sm.amount_si_prefix().clone()
177                         .map(|r| &input[r]).unwrap_or("");
178
179                 Ok((currency, amount, si))
180         }
181 }
182
183
184 impl FromStr for super::Currency {
185         type Err = Bolt11ParseError;
186
187         fn from_str(currency_prefix: &str) -> Result<Self, Bolt11ParseError> {
188                 match currency_prefix {
189                         "bc" => Ok(Currency::Bitcoin),
190                         "tb" => Ok(Currency::BitcoinTestnet),
191                         "bcrt" => Ok(Currency::Regtest),
192                         "sb" => Ok(Currency::Simnet),
193                         "tbs" => Ok(Currency::Signet),
194                         _ => Err(Bolt11ParseError::UnknownCurrency)
195                 }
196         }
197 }
198
199 impl FromStr for SiPrefix {
200         type Err = Bolt11ParseError;
201
202         fn from_str(currency_prefix: &str) -> Result<Self, Bolt11ParseError> {
203                 use crate::SiPrefix::*;
204                 match currency_prefix {
205                         "m" => Ok(Milli),
206                         "u" => Ok(Micro),
207                         "n" => Ok(Nano),
208                         "p" => Ok(Pico),
209                         _ => Err(Bolt11ParseError::UnknownSiPrefix)
210                 }
211         }
212 }
213
214 /// ```
215 /// use lightning_invoice::Bolt11Invoice;
216 ///
217 ///
218 /// let invoice = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\
219 /// h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\
220 /// 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\
221 /// h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\
222 /// j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\
223 /// ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\
224 /// guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\
225 /// ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\
226 /// p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\
227 /// 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\
228 /// j5r6drg6k6zcqj0fcwg";
229 ///
230 /// assert!(invoice.parse::<Bolt11Invoice>().is_ok());
231 /// ```
232 impl FromStr for Bolt11Invoice {
233         type Err = ParseOrSemanticError;
234
235         fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
236                 let signed = s.parse::<SignedRawBolt11Invoice>()?;
237                 Ok(Bolt11Invoice::from_signed(signed)?)
238         }
239 }
240
241 /// ```
242 /// use lightning_invoice::*;
243 ///
244 /// let invoice = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\
245 /// h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\
246 /// 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\
247 /// h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\
248 /// j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\
249 /// ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\
250 /// guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\
251 /// ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\
252 /// p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\
253 /// 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\
254 /// j5r6drg6k6zcqj0fcwg";
255 ///
256 /// let parsed_1 = invoice.parse::<Bolt11Invoice>();
257 ///
258 /// let parsed_2 = match invoice.parse::<SignedRawBolt11Invoice>() {
259 ///     Ok(signed) => match Bolt11Invoice::from_signed(signed) {
260 ///             Ok(invoice) => Ok(invoice),
261 ///             Err(e) => Err(ParseOrSemanticError::SemanticError(e)),
262 ///     },
263 ///     Err(e) => Err(ParseOrSemanticError::ParseError(e)),
264 /// };
265 ///
266 /// assert!(parsed_1.is_ok());
267 /// assert_eq!(parsed_1, parsed_2);
268 /// ```
269 impl FromStr for SignedRawBolt11Invoice {
270         type Err = Bolt11ParseError;
271
272         fn from_str(s: &str) -> Result<Self, Self::Err> {
273                 let (hrp, data, var) = bech32::decode(s)?;
274
275                 if var == bech32::Variant::Bech32m {
276                         // Consider Bech32m addresses to be "Invalid Checksum", since that is what we'd get if
277                         // we didn't support Bech32m (which lightning does not use).
278                         return Err(Bolt11ParseError::Bech32Error(bech32::Error::InvalidChecksum));
279                 }
280
281                 if data.len() < 104 {
282                         return Err(Bolt11ParseError::TooShortDataPart);
283                 }
284
285                 let raw_hrp: RawHrp = hrp.parse()?;
286                 let data_part = RawDataPart::from_base32(&data[..data.len()-104])?;
287
288                 Ok(SignedRawBolt11Invoice {
289                         raw_invoice: RawBolt11Invoice {
290                                 hrp: raw_hrp,
291                                 data: data_part,
292                         },
293                         hash: RawBolt11Invoice::hash_from_parts(
294                                 hrp.as_bytes(),
295                                 &data[..data.len()-104]
296                         ),
297                         signature: Bolt11InvoiceSignature::from_base32(&data[data.len()-104..])?,
298                 })
299         }
300 }
301
302 impl FromStr for RawHrp {
303         type Err = Bolt11ParseError;
304
305         fn from_str(hrp: &str) -> Result<Self, <Self as FromStr>::Err> {
306                 let parts = parse_hrp(hrp)?;
307
308                 let currency = parts.0.parse::<Currency>()?;
309
310                 let amount = if !parts.1.is_empty() {
311                         Some(parts.1.parse::<u64>()?)
312                 } else {
313                         None
314                 };
315
316                 let si_prefix: Option<SiPrefix> = if parts.2.is_empty() {
317                         None
318                 } else {
319                         let si: SiPrefix = parts.2.parse()?;
320                         if let Some(amt) = amount {
321                                 if amt.checked_mul(si.multiplier()).is_none() {
322                                         return Err(Bolt11ParseError::IntegerOverflowError);
323                                 }
324                         }
325                         Some(si)
326                 };
327
328                 Ok(RawHrp {
329                         currency,
330                         raw_amount: amount,
331                         si_prefix,
332                 })
333         }
334 }
335
336 impl FromBase32 for RawDataPart {
337         type Err = Bolt11ParseError;
338
339         fn from_base32(data: &[u5]) -> Result<Self, Self::Err> {
340                 if data.len() < 7 { // timestamp length
341                         return Err(Bolt11ParseError::TooShortDataPart);
342                 }
343
344                 let timestamp = PositiveTimestamp::from_base32(&data[0..7])?;
345                 let tagged = parse_tagged_parts(&data[7..])?;
346
347                 Ok(RawDataPart {
348                         timestamp,
349                         tagged_fields: tagged,
350                 })
351         }
352 }
353
354 impl FromBase32 for PositiveTimestamp {
355         type Err = Bolt11ParseError;
356
357         fn from_base32(b32: &[u5]) -> Result<Self, Self::Err> {
358                 if b32.len() != 7 {
359                         return Err(Bolt11ParseError::InvalidSliceLength("PositiveTimestamp::from_base32()".into()));
360                 }
361                 let timestamp: u64 = parse_u64_be(b32)
362                         .expect("7*5bit < 64bit, no overflow possible");
363                 match PositiveTimestamp::from_unix_timestamp(timestamp) {
364                         Ok(t) => Ok(t),
365                         Err(_) => unreachable!(),
366                 }
367         }
368 }
369
370 impl FromBase32 for Bolt11InvoiceSignature {
371         type Err = Bolt11ParseError;
372         fn from_base32(signature: &[u5]) -> Result<Self, Self::Err> {
373                 if signature.len() != 104 {
374                         return Err(Bolt11ParseError::InvalidSliceLength("Bolt11InvoiceSignature::from_base32()".into()));
375                 }
376                 let recoverable_signature_bytes = Vec::<u8>::from_base32(signature)?;
377                 let signature = &recoverable_signature_bytes[0..64];
378                 let recovery_id = RecoveryId::from_i32(recoverable_signature_bytes[64] as i32)?;
379
380                 Ok(Bolt11InvoiceSignature(RecoverableSignature::from_compact(
381                         signature,
382                         recovery_id
383                 )?))
384         }
385 }
386
387 macro_rules! define_parse_int_be { ($name: ident, $ty: ty) => {
388         fn $name(digits: &[u5]) -> Option<$ty> {
389                 digits.iter().fold(Some(Default::default()), |acc, b|
390                         acc
391                                 .and_then(|x| x.checked_mul(32))
392                                 .and_then(|x| x.checked_add((Into::<u8>::into(*b)).into()))
393                 )
394         }
395 } }
396 define_parse_int_be!(parse_u16_be, u16);
397 define_parse_int_be!(parse_u64_be, u64);
398
399 fn parse_tagged_parts(data: &[u5]) -> Result<Vec<RawTaggedField>, Bolt11ParseError> {
400         let mut parts = Vec::<RawTaggedField>::new();
401         let mut data = data;
402
403         while !data.is_empty() {
404                 if data.len() < 3 {
405                         return Err(Bolt11ParseError::UnexpectedEndOfTaggedFields);
406                 }
407
408                 // Ignore tag at data[0], it will be handled in the TaggedField parsers and
409                 // parse the length to find the end of the tagged field's data
410                 let len = parse_u16_be(&data[1..3]).expect("can't overflow") as usize;
411                 let last_element = 3 + len;
412
413                 if data.len() < last_element {
414                         return Err(Bolt11ParseError::UnexpectedEndOfTaggedFields);
415                 }
416
417                 // Get the tagged field's data slice
418                 let field = &data[0..last_element];
419
420                 // Set data slice to remaining data
421                 data = &data[last_element..];
422
423                 match TaggedField::from_base32(field) {
424                         Ok(field) => {
425                                 parts.push(RawTaggedField::KnownSemantics(field))
426                         },
427                         Err(Bolt11ParseError::Skip)|Err(Bolt11ParseError::Bech32Error(bech32::Error::InvalidLength)) => {
428                                 parts.push(RawTaggedField::UnknownSemantics(field.into()))
429                         },
430                         Err(e) => {return Err(e)}
431                 }
432         }
433         Ok(parts)
434 }
435
436 impl FromBase32 for TaggedField {
437         type Err = Bolt11ParseError;
438
439         fn from_base32(field: &[u5]) -> Result<TaggedField, Bolt11ParseError> {
440                 if field.len() < 3 {
441                         return Err(Bolt11ParseError::UnexpectedEndOfTaggedFields);
442                 }
443
444                 let tag = field[0];
445                 let field_data =  &field[3..];
446
447                 match tag.to_u8() {
448                         constants::TAG_PAYMENT_HASH =>
449                                 Ok(TaggedField::PaymentHash(Sha256::from_base32(field_data)?)),
450                         constants::TAG_DESCRIPTION =>
451                                 Ok(TaggedField::Description(Description::from_base32(field_data)?)),
452                         constants::TAG_PAYEE_PUB_KEY =>
453                                 Ok(TaggedField::PayeePubKey(PayeePubKey::from_base32(field_data)?)),
454                         constants::TAG_DESCRIPTION_HASH =>
455                                 Ok(TaggedField::DescriptionHash(Sha256::from_base32(field_data)?)),
456                         constants::TAG_EXPIRY_TIME =>
457                                 Ok(TaggedField::ExpiryTime(ExpiryTime::from_base32(field_data)?)),
458                         constants::TAG_MIN_FINAL_CLTV_EXPIRY_DELTA =>
459                                 Ok(TaggedField::MinFinalCltvExpiryDelta(MinFinalCltvExpiryDelta::from_base32(field_data)?)),
460                         constants::TAG_FALLBACK =>
461                                 Ok(TaggedField::Fallback(Fallback::from_base32(field_data)?)),
462                         constants::TAG_PRIVATE_ROUTE =>
463                                 Ok(TaggedField::PrivateRoute(PrivateRoute::from_base32(field_data)?)),
464                         constants::TAG_PAYMENT_SECRET =>
465                                 Ok(TaggedField::PaymentSecret(PaymentSecret::from_base32(field_data)?)),
466                         constants::TAG_PAYMENT_METADATA =>
467                                 Ok(TaggedField::PaymentMetadata(Vec::<u8>::from_base32(field_data)?)),
468                         constants::TAG_FEATURES =>
469                                 Ok(TaggedField::Features(Bolt11InvoiceFeatures::from_base32(field_data)?)),
470                         _ => {
471                                 // "A reader MUST skip over unknown fields"
472                                 Err(Bolt11ParseError::Skip)
473                         }
474                 }
475         }
476 }
477
478 impl FromBase32 for Sha256 {
479         type Err = Bolt11ParseError;
480
481         fn from_base32(field_data: &[u5]) -> Result<Sha256, Bolt11ParseError> {
482                 if field_data.len() != 52 {
483                         // "A reader MUST skip over […] a p, [or] h […] field that does not have data_length 52 […]."
484                         Err(Bolt11ParseError::Skip)
485                 } else {
486                         Ok(Sha256(sha256::Hash::from_slice(&Vec::<u8>::from_base32(field_data)?)
487                                 .expect("length was checked before (52 u5 -> 32 u8)")))
488                 }
489         }
490 }
491
492 impl FromBase32 for Description {
493         type Err = Bolt11ParseError;
494
495         fn from_base32(field_data: &[u5]) -> Result<Description, Bolt11ParseError> {
496                 let bytes = Vec::<u8>::from_base32(field_data)?;
497                 let description = String::from(str::from_utf8(&bytes)?);
498                 Ok(Description::new(description).expect(
499                         "Max len is 639=floor(1023*5/8) since the len field is only 10bits long"
500                 ))
501         }
502 }
503
504 impl FromBase32 for PayeePubKey {
505         type Err = Bolt11ParseError;
506
507         fn from_base32(field_data: &[u5]) -> Result<PayeePubKey, Bolt11ParseError> {
508                 if field_data.len() != 53 {
509                         // "A reader MUST skip over […] a n […] field that does not have data_length 53 […]."
510                         Err(Bolt11ParseError::Skip)
511                 } else {
512                         let data_bytes = Vec::<u8>::from_base32(field_data)?;
513                         let pub_key = PublicKey::from_slice(&data_bytes)?;
514                         Ok(pub_key.into())
515                 }
516         }
517 }
518
519 impl FromBase32 for ExpiryTime {
520         type Err = Bolt11ParseError;
521
522         fn from_base32(field_data: &[u5]) -> Result<ExpiryTime, Bolt11ParseError> {
523                 match parse_u64_be(field_data)
524                         .map(ExpiryTime::from_seconds)
525                 {
526                         Some(t) => Ok(t),
527                         None => Err(Bolt11ParseError::IntegerOverflowError),
528                 }
529         }
530 }
531
532 impl FromBase32 for MinFinalCltvExpiryDelta {
533         type Err = Bolt11ParseError;
534
535         fn from_base32(field_data: &[u5]) -> Result<MinFinalCltvExpiryDelta, Bolt11ParseError> {
536                 let expiry = parse_u64_be(field_data);
537                 if let Some(expiry) = expiry {
538                         Ok(MinFinalCltvExpiryDelta(expiry))
539                 } else {
540                         Err(Bolt11ParseError::IntegerOverflowError)
541                 }
542         }
543 }
544
545 impl FromBase32 for Fallback {
546         type Err = Bolt11ParseError;
547
548         fn from_base32(field_data: &[u5]) -> Result<Fallback, Bolt11ParseError> {
549                 if field_data.is_empty() {
550                         return Err(Bolt11ParseError::UnexpectedEndOfTaggedFields);
551                 }
552
553                 let version = field_data[0].to_u8();
554                 let bytes = Vec::<u8>::from_base32(&field_data[1..])?;
555
556                 match version {
557                         0..=16 => {
558                                 if bytes.len() < 2 || bytes.len() > 40 {
559                                         return Err(Bolt11ParseError::InvalidSegWitProgramLength);
560                                 }
561                                 let version = WitnessVersion::try_from(version).expect("0 through 16 are valid SegWit versions");
562                                 Ok(Fallback::SegWitProgram {
563                                         version,
564                                         program: bytes
565                                 })
566                         },
567                         17 => {
568                                 let pkh = match PubkeyHash::from_slice(&bytes) {
569                                         Ok(pkh) => pkh,
570                                         Err(_) => return Err(Bolt11ParseError::InvalidPubKeyHashLength),
571                                 };
572                                 Ok(Fallback::PubKeyHash(pkh))
573                         }
574                         18 => {
575                                 let sh = match ScriptHash::from_slice(&bytes) {
576                                         Ok(sh) => sh,
577                                         Err(_) => return Err(Bolt11ParseError::InvalidScriptHashLength),
578                                 };
579                                 Ok(Fallback::ScriptHash(sh))
580                         }
581                         _ => Err(Bolt11ParseError::Skip)
582                 }
583         }
584 }
585
586 impl FromBase32 for PrivateRoute {
587         type Err = Bolt11ParseError;
588
589         fn from_base32(field_data: &[u5]) -> Result<PrivateRoute, Bolt11ParseError> {
590                 let bytes = Vec::<u8>::from_base32(field_data)?;
591
592                 if bytes.len() % 51 != 0 {
593                         return Err(Bolt11ParseError::UnexpectedEndOfTaggedFields);
594                 }
595
596                 let mut route_hops = Vec::<RouteHintHop>::new();
597
598                 let mut bytes = bytes.as_slice();
599                 while !bytes.is_empty() {
600                         let hop_bytes = &bytes[0..51];
601                         bytes = &bytes[51..];
602
603                         let mut channel_id: [u8; 8] = Default::default();
604                         channel_id.copy_from_slice(&hop_bytes[33..41]);
605
606                         let hop = RouteHintHop {
607                                 src_node_id: PublicKey::from_slice(&hop_bytes[0..33])?,
608                                 short_channel_id: u64::from_be_bytes(channel_id),
609                                 fees: RoutingFees {
610                                         base_msat: u32::from_be_bytes(hop_bytes[41..45].try_into().expect("slice too big?")),
611                                         proportional_millionths: u32::from_be_bytes(hop_bytes[45..49].try_into().expect("slice too big?")),
612                                 },
613                                 cltv_expiry_delta: u16::from_be_bytes(hop_bytes[49..51].try_into().expect("slice too big?")),
614                                 htlc_minimum_msat: None,
615                                 htlc_maximum_msat: None,
616                         };
617
618                         route_hops.push(hop);
619                 }
620
621                 Ok(PrivateRoute(RouteHint(route_hops)))
622         }
623 }
624
625 impl Display for Bolt11ParseError {
626         fn fmt(&self, f: &mut Formatter) -> fmt::Result {
627                 match *self {
628                         // TODO: find a way to combine the first three arms (e as error::Error?)
629                         Bolt11ParseError::Bech32Error(ref e) => {
630                                 write!(f, "Invalid bech32: {}", e)
631                         }
632                         Bolt11ParseError::ParseAmountError(ref e) => {
633                                 write!(f, "Invalid amount in hrp ({})", e)
634                         }
635                         Bolt11ParseError::MalformedSignature(ref e) => {
636                                 write!(f, "Invalid secp256k1 signature: {}", e)
637                         }
638                         Bolt11ParseError::DescriptionDecodeError(ref e) => {
639                                 write!(f, "Description is not a valid utf-8 string: {}", e)
640                         }
641                         Bolt11ParseError::InvalidSliceLength(ref function) => {
642                                 write!(f, "Slice in function {} had the wrong length", function)
643                         }
644                         Bolt11ParseError::BadPrefix => f.write_str("did not begin with 'ln'"),
645                         Bolt11ParseError::UnknownCurrency => f.write_str("currency code unknown"),
646                         Bolt11ParseError::UnknownSiPrefix => f.write_str("unknown SI prefix"),
647                         Bolt11ParseError::MalformedHRP => f.write_str("malformed human readable part"),
648                         Bolt11ParseError::TooShortDataPart => {
649                                 f.write_str("data part too short (should be at least 111 bech32 chars long)")
650                         },
651                         Bolt11ParseError::UnexpectedEndOfTaggedFields => {
652                                 f.write_str("tagged fields part ended unexpectedly")
653                         },
654                         Bolt11ParseError::PaddingError => f.write_str("some data field had bad padding"),
655                         Bolt11ParseError::IntegerOverflowError => {
656                                 f.write_str("parsed integer doesn't fit into receiving type")
657                         },
658                         Bolt11ParseError::InvalidSegWitProgramLength => {
659                                 f.write_str("fallback SegWit program is too long or too short")
660                         },
661                         Bolt11ParseError::InvalidPubKeyHashLength => {
662                                 f.write_str("fallback public key hash has a length unequal 20 bytes")
663                         },
664                         Bolt11ParseError::InvalidScriptHashLength => {
665                                 f.write_str("fallback script hash has a length unequal 32 bytes")
666                         },
667                         Bolt11ParseError::InvalidRecoveryId => {
668                                 f.write_str("recovery id is out of range (should be in [0,3])")
669                         },
670                         Bolt11ParseError::Skip => {
671                                 f.write_str("the tagged field has to be skipped because of an unexpected, but allowed property")
672                         },
673                 }
674         }
675 }
676
677 impl Display for ParseOrSemanticError {
678         fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
679                 match self {
680                         ParseOrSemanticError::ParseError(err) => err.fmt(f),
681                         ParseOrSemanticError::SemanticError(err) => err.fmt(f),
682                 }
683         }
684 }
685
686 #[cfg(feature = "std")]
687 impl error::Error for Bolt11ParseError {}
688
689 #[cfg(feature = "std")]
690 impl error::Error for ParseOrSemanticError {}
691
692 macro_rules! from_error {
693     ($my_error:expr, $extern_error:ty) => {
694         impl From<$extern_error> for Bolt11ParseError {
695             fn from(e: $extern_error) -> Self {
696                 $my_error(e)
697             }
698         }
699     }
700 }
701
702 from_error!(Bolt11ParseError::MalformedSignature, secp256k1::Error);
703 from_error!(Bolt11ParseError::ParseAmountError, ParseIntError);
704 from_error!(Bolt11ParseError::DescriptionDecodeError, str::Utf8Error);
705
706 impl From<bech32::Error> for Bolt11ParseError {
707         fn from(e: bech32::Error) -> Self {
708                 match e {
709                         bech32::Error::InvalidPadding => Bolt11ParseError::PaddingError,
710                         _ => Bolt11ParseError::Bech32Error(e)
711                 }
712         }
713 }
714
715 impl From<Bolt11ParseError> for ParseOrSemanticError {
716         fn from(e: Bolt11ParseError) -> Self {
717                 ParseOrSemanticError::ParseError(e)
718         }
719 }
720
721 impl From<crate::Bolt11SemanticError> for ParseOrSemanticError {
722         fn from(e: Bolt11SemanticError) -> Self {
723                 ParseOrSemanticError::SemanticError(e)
724         }
725 }
726
727 #[cfg(test)]
728 mod test {
729         use crate::de::Bolt11ParseError;
730         use secp256k1::PublicKey;
731         use bech32::u5;
732         use bitcoin::hashes::sha256;
733         use std::str::FromStr;
734
735         const CHARSET_REV: [i8; 128] = [
736                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
737                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
738                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
739                 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1,
740                 -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
741                 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1,
742                 -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1,
743                 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1
744         ];
745
746         fn from_bech32(bytes_5b: &[u8]) -> Vec<u5> {
747                 bytes_5b
748                         .iter()
749                         .map(|c| u5::try_from_u8(CHARSET_REV[*c as usize] as u8).unwrap())
750                         .collect()
751         }
752
753         #[test]
754         fn test_parse_currency_prefix() {
755                 use crate::Currency;
756
757                 assert_eq!("bc".parse::<Currency>(), Ok(Currency::Bitcoin));
758                 assert_eq!("tb".parse::<Currency>(), Ok(Currency::BitcoinTestnet));
759                 assert_eq!("bcrt".parse::<Currency>(), Ok(Currency::Regtest));
760                 assert_eq!("sb".parse::<Currency>(), Ok(Currency::Simnet));
761                 assert_eq!("tbs".parse::<Currency>(), Ok(Currency::Signet));
762                 assert_eq!("something_else".parse::<Currency>(), Err(Bolt11ParseError::UnknownCurrency))
763         }
764
765         #[test]
766         fn test_parse_int_from_bytes_be() {
767                 use crate::de::parse_u16_be;
768
769                 assert_eq!(parse_u16_be(&[
770                                 u5::try_from_u8(1).unwrap(), u5::try_from_u8(2).unwrap(),
771                                 u5::try_from_u8(3).unwrap(), u5::try_from_u8(4).unwrap()]
772                         ), Some(34916));
773                 assert_eq!(parse_u16_be(&[
774                                 u5::try_from_u8(2).unwrap(), u5::try_from_u8(0).unwrap(),
775                                 u5::try_from_u8(0).unwrap(), u5::try_from_u8(0).unwrap()]
776                         ), None);
777         }
778
779         #[test]
780         fn test_parse_sha256_hash() {
781                 use crate::Sha256;
782                 use bech32::FromBase32;
783
784                 let input = from_bech32(
785                         "qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypq".as_bytes()
786                 );
787
788                 let hash = sha256::Hash::from_str(
789                         "0001020304050607080900010203040506070809000102030405060708090102"
790                 ).unwrap();
791                 let expected = Ok(Sha256(hash));
792
793                 assert_eq!(Sha256::from_base32(&input), expected);
794
795                 // make sure hashes of unknown length get skipped
796                 let input_unexpected_length = from_bech32(
797                         "qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypyq".as_bytes()
798                 );
799                 assert_eq!(Sha256::from_base32(&input_unexpected_length), Err(Bolt11ParseError::Skip));
800         }
801
802         #[test]
803         fn test_parse_description() {
804                 use crate::Description;
805                 use bech32::FromBase32;
806
807                 let input = from_bech32("xysxxatsyp3k7enxv4js".as_bytes());
808                 let expected = Ok(Description::new("1 cup coffee".to_owned()).unwrap());
809                 assert_eq!(Description::from_base32(&input), expected);
810         }
811
812         #[test]
813         fn test_parse_payee_pub_key() {
814                 use crate::PayeePubKey;
815                 use bech32::FromBase32;
816
817                 let input = from_bech32("q0n326hr8v9zprg8gsvezcch06gfaqqhde2aj730yg0durunfhv66".as_bytes());
818                 let pk_bytes = [
819                         0x03, 0xe7, 0x15, 0x6a, 0xe3, 0x3b, 0x0a, 0x20, 0x8d, 0x07, 0x44, 0x19, 0x91, 0x63,
820                         0x17, 0x7e, 0x90, 0x9e, 0x80, 0x17, 0x6e, 0x55, 0xd9, 0x7a, 0x2f, 0x22, 0x1e, 0xde,
821                         0x0f, 0x93, 0x4d, 0xd9, 0xad
822                 ];
823                 let expected = Ok(PayeePubKey(
824                         PublicKey::from_slice(&pk_bytes[..]).unwrap()
825                 ));
826
827                 assert_eq!(PayeePubKey::from_base32(&input), expected);
828
829                 // expects 33 bytes
830                 let input_unexpected_length = from_bech32(
831                         "q0n326hr8v9zprg8gsvezcch06gfaqqhde2aj730yg0durunfhvq".as_bytes()
832                 );
833                 assert_eq!(PayeePubKey::from_base32(&input_unexpected_length), Err(Bolt11ParseError::Skip));
834         }
835
836         #[test]
837         fn test_parse_expiry_time() {
838                 use crate::ExpiryTime;
839                 use bech32::FromBase32;
840
841                 let input = from_bech32("pu".as_bytes());
842                 let expected = Ok(ExpiryTime::from_seconds(60));
843                 assert_eq!(ExpiryTime::from_base32(&input), expected);
844
845                 let input_too_large = from_bech32("sqqqqqqqqqqqq".as_bytes());
846                 assert_eq!(ExpiryTime::from_base32(&input_too_large), Err(Bolt11ParseError::IntegerOverflowError));
847         }
848
849         #[test]
850         fn test_parse_min_final_cltv_expiry_delta() {
851                 use crate::MinFinalCltvExpiryDelta;
852                 use bech32::FromBase32;
853
854                 let input = from_bech32("pr".as_bytes());
855                 let expected = Ok(MinFinalCltvExpiryDelta(35));
856
857                 assert_eq!(MinFinalCltvExpiryDelta::from_base32(&input), expected);
858         }
859
860         #[test]
861         fn test_parse_fallback() {
862                 use crate::Fallback;
863                 use bech32::FromBase32;
864                 use bitcoin::{PubkeyHash, ScriptHash, WitnessVersion};
865                 use bitcoin::hashes::Hash;
866
867                 let cases = vec![
868                         (
869                                 from_bech32("3x9et2e20v6pu37c5d9vax37wxq72un98".as_bytes()),
870                                 Ok(Fallback::PubKeyHash(PubkeyHash::from_slice(&[
871                                         0x31, 0x72, 0xb5, 0x65, 0x4f, 0x66, 0x83, 0xc8, 0xfb, 0x14, 0x69, 0x59, 0xd3,
872                                         0x47, 0xce, 0x30, 0x3c, 0xae, 0x4c, 0xa7
873                                 ]).unwrap()))
874                         ),
875                         (
876                                 from_bech32("j3a24vwu6r8ejrss3axul8rxldph2q7z9".as_bytes()),
877                                 Ok(Fallback::ScriptHash(ScriptHash::from_slice(&[
878                                         0x8f, 0x55, 0x56, 0x3b, 0x9a, 0x19, 0xf3, 0x21, 0xc2, 0x11, 0xe9, 0xb9, 0xf3,
879                                         0x8c, 0xdf, 0x68, 0x6e, 0xa0, 0x78, 0x45
880                                 ]).unwrap()))
881                         ),
882                         (
883                                 from_bech32("qw508d6qejxtdg4y5r3zarvary0c5xw7k".as_bytes()),
884                                 Ok(Fallback::SegWitProgram {
885                                         version: WitnessVersion::V0,
886                                         program: Vec::from(&[
887                                                 0x75u8, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45,
888                                                 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6
889                                         ][..])
890                                 })
891                         ),
892                         (
893                                 vec![u5::try_from_u8(21).unwrap(); 41],
894                                 Err(Bolt11ParseError::Skip)
895                         ),
896                         (
897                                 vec![],
898                                 Err(Bolt11ParseError::UnexpectedEndOfTaggedFields)
899                         ),
900                         (
901                                 vec![u5::try_from_u8(1).unwrap(); 81],
902                                 Err(Bolt11ParseError::InvalidSegWitProgramLength)
903                         ),
904                         (
905                                 vec![u5::try_from_u8(17).unwrap(); 1],
906                                 Err(Bolt11ParseError::InvalidPubKeyHashLength)
907                         ),
908                         (
909                                 vec![u5::try_from_u8(18).unwrap(); 1],
910                                 Err(Bolt11ParseError::InvalidScriptHashLength)
911                         )
912                 ];
913
914                 for (input, expected) in cases.into_iter() {
915                         assert_eq!(Fallback::from_base32(&input), expected);
916                 }
917         }
918
919         #[test]
920         fn test_parse_route() {
921                 use lightning::routing::gossip::RoutingFees;
922                 use lightning::routing::router::{RouteHint, RouteHintHop};
923                 use crate::PrivateRoute;
924                 use bech32::FromBase32;
925
926                 let input = from_bech32(
927                         "q20q82gphp2nflc7jtzrcazrra7wwgzxqc8u7754cdlpfrmccae92qgzqvzq2ps8pqqqqqqpqqqqq9qqqvpeuqa\
928                         fqxu92d8lr6fvg0r5gv0heeeqgcrqlnm6jhphu9y00rrhy4grqszsvpcgpy9qqqqqqgqqqqq7qqzq".as_bytes()
929                 );
930
931                 let mut expected = Vec::<RouteHintHop>::new();
932                 expected.push(RouteHintHop {
933                         src_node_id: PublicKey::from_slice(
934                                 &[
935                                         0x02u8, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4, 0x3c,
936                                         0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a, 0x95, 0xc3,
937                                         0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
938                                 ][..]
939                         ).unwrap(),
940                         short_channel_id: 0x0102030405060708,
941                         fees: RoutingFees {
942                                 base_msat: 1,
943                                 proportional_millionths: 20,
944                         },
945                         cltv_expiry_delta: 3,
946                         htlc_minimum_msat: None,
947                         htlc_maximum_msat: None
948                 });
949                 expected.push(RouteHintHop {
950                         src_node_id: PublicKey::from_slice(
951                                 &[
952                                         0x03u8, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4, 0x3c,
953                                         0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a, 0x95, 0xc3,
954                                         0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
955                                 ][..]
956                         ).unwrap(),
957                         short_channel_id: 0x030405060708090a,
958                         fees: RoutingFees {
959                                 base_msat: 2,
960                                 proportional_millionths: 30,
961                         },
962                         cltv_expiry_delta: 4,
963                         htlc_minimum_msat: None,
964                         htlc_maximum_msat: None
965                 });
966
967                 assert_eq!(PrivateRoute::from_base32(&input), Ok(PrivateRoute(RouteHint(expected))));
968
969                 assert_eq!(
970                         PrivateRoute::from_base32(&[u5::try_from_u8(0).unwrap(); 40][..]),
971                         Err(Bolt11ParseError::UnexpectedEndOfTaggedFields)
972                 );
973         }
974
975         #[test]
976         fn test_payment_secret_and_features_de_and_ser() {
977                 use lightning::ln::features::Bolt11InvoiceFeatures;
978                 use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
979                 use crate::TaggedField::*;
980                 use crate::{SiPrefix, SignedRawBolt11Invoice, Bolt11InvoiceSignature, RawBolt11Invoice, RawHrp, RawDataPart,
981                                  Currency, Sha256, PositiveTimestamp};
982
983                 // Feature bits 9, 15, and 99 are set.
984                 let expected_features = Bolt11InvoiceFeatures::from_le_bytes(vec![0, 130, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8]);
985                 let invoice_str = "lnbc25m1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdq5vdhkven9v5sxyetpdeessp5zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zyg3zygs9q5sqqqqqqqqqqqqqqqpqsq67gye39hfg3zd8rgc80k32tvy9xk2xunwm5lzexnvpx6fd77en8qaq424dxgt56cag2dpt359k3ssyhetktkpqh24jqnjyw6uqd08sgptq44qu";
986                 let invoice = SignedRawBolt11Invoice {
987                                         raw_invoice: RawBolt11Invoice {
988                                                 hrp: RawHrp {
989                                                         currency: Currency::Bitcoin,
990                                                         raw_amount: Some(25),
991                                                         si_prefix: Some(SiPrefix::Milli)
992                                                 },
993                                                 data: RawDataPart {
994                                                         timestamp: PositiveTimestamp::from_unix_timestamp(1496314658).unwrap(),
995                                                         tagged_fields: vec ! [
996                                                                 PaymentHash(Sha256(sha256::Hash::from_str(
997                                                                         "0001020304050607080900010203040506070809000102030405060708090102"
998                                                                 ).unwrap())).into(),
999                                                                 Description(crate::Description::new("coffee beans".to_owned()).unwrap()).into(),
1000                                                                 PaymentSecret(crate::PaymentSecret([17; 32])).into(),
1001                                                                 Features(expected_features).into()]}
1002                                                                 },
1003                                         hash: [0xb1, 0x96, 0x46, 0xc3, 0xbc, 0x56, 0x76, 0x1d, 0x20, 0x65, 0x6e, 0x0e, 0x32,
1004                                                                         0xec, 0xd2, 0x69, 0x27, 0xb7, 0x62, 0x6e, 0x2a, 0x8b, 0xe6, 0x97, 0x71, 0x9f,
1005                                                                         0xf8, 0x7e, 0x44, 0x54, 0x55, 0xb9],
1006                                         signature: Bolt11InvoiceSignature(RecoverableSignature::from_compact(
1007                                                                                 &[0xd7, 0x90, 0x4c, 0xc4, 0xb7, 0x4a, 0x22, 0x26, 0x9c, 0x68, 0xc1, 0xdf, 0x68,
1008                                                                                         0xa9, 0x6c, 0x21, 0x4d, 0x65, 0x1b, 0x93, 0x76, 0xe9, 0xf1, 0x64, 0xd3, 0x60,
1009                                                                                         0x4d, 0xa4, 0xb7, 0xde, 0xcc, 0xce, 0x0e, 0x82, 0xaa, 0xab, 0x4c, 0x85, 0xd3,
1010                                                                                         0x58, 0xea, 0x14, 0xd0, 0xae, 0x34, 0x2d, 0xa3, 0x08, 0x12, 0xf9, 0x5d, 0x97,
1011                                                                                         0x60, 0x82, 0xea, 0xac, 0x81, 0x39, 0x11, 0xda, 0xe0, 0x1a, 0xf3, 0xc1],
1012                                                                                 RecoveryId::from_i32(1).unwrap()
1013                                                                 ).unwrap()),
1014                         };
1015                 assert_eq!(invoice_str, invoice.to_string());
1016                 assert_eq!(
1017                         invoice_str.parse(),
1018                         Ok(invoice)
1019                 );
1020         }
1021
1022         #[test]
1023         fn test_raw_signed_invoice_deserialization() {
1024                 use crate::TaggedField::*;
1025                 use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
1026                 use crate::{SignedRawBolt11Invoice, Bolt11InvoiceSignature, RawBolt11Invoice, RawHrp, RawDataPart, Currency, Sha256,
1027                          PositiveTimestamp};
1028
1029                 assert_eq!(
1030                         "lnbc1pvjluezpp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqdpl2pkx2ctnv5sxxmmw\
1031                         wd5kgetjypeh2ursdae8g6twvus8g6rfwvs8qun0dfjkxaq8rkx3yf5tcsyz3d73gafnh3cax9rn449d9p5uxz9\
1032                         ezhhypd0elx87sjle52x86fux2ypatgddc6k63n7erqz25le42c4u4ecky03ylcqca784w".parse(),
1033                         Ok(SignedRawBolt11Invoice {
1034                                 raw_invoice: RawBolt11Invoice {
1035                                         hrp: RawHrp {
1036                                                 currency: Currency::Bitcoin,
1037                                                 raw_amount: None,
1038                                                 si_prefix: None,
1039                                         },
1040                                         data: RawDataPart {
1041                                         timestamp: PositiveTimestamp::from_unix_timestamp(1496314658).unwrap(),
1042                                         tagged_fields: vec ! [
1043                                                 PaymentHash(Sha256(sha256::Hash::from_str(
1044                                                         "0001020304050607080900010203040506070809000102030405060708090102"
1045                                                 ).unwrap())).into(),
1046                                                 Description(
1047                                                         crate::Description::new(
1048                                                                 "Please consider supporting this project".to_owned()
1049                                                         ).unwrap()
1050                                                 ).into(),
1051                                         ],
1052                                         },
1053                                         },
1054                                 hash: [
1055                                         0xc3, 0xd4, 0xe8, 0x3f, 0x64, 0x6f, 0xa7, 0x9a, 0x39, 0x3d, 0x75, 0x27,
1056                                         0x7b, 0x1d, 0x85, 0x8d, 0xb1, 0xd1, 0xf7, 0xab, 0x71, 0x37, 0xdc, 0xb7,
1057                                         0x83, 0x5d, 0xb2, 0xec, 0xd5, 0x18, 0xe1, 0xc9
1058                                 ],
1059                                 signature: Bolt11InvoiceSignature(RecoverableSignature::from_compact(
1060                                         & [
1061                                                 0x38u8, 0xec, 0x68, 0x91, 0x34, 0x5e, 0x20, 0x41, 0x45, 0xbe, 0x8a,
1062                                                 0x3a, 0x99, 0xde, 0x38, 0xe9, 0x8a, 0x39, 0xd6, 0xa5, 0x69, 0x43,
1063                                                 0x4e, 0x18, 0x45, 0xc8, 0xaf, 0x72, 0x05, 0xaf, 0xcf, 0xcc, 0x7f,
1064                                                 0x42, 0x5f, 0xcd, 0x14, 0x63, 0xe9, 0x3c, 0x32, 0x88, 0x1e, 0xad,
1065                                                 0x0d, 0x6e, 0x35, 0x6d, 0x46, 0x7e, 0xc8, 0xc0, 0x25, 0x53, 0xf9,
1066                                                 0xaa, 0xb1, 0x5e, 0x57, 0x38, 0xb1, 0x1f, 0x12, 0x7f
1067                                         ],
1068                                         RecoveryId::from_i32(0).unwrap()
1069                                 ).unwrap()),
1070                                 }
1071                         )
1072                 )
1073         }
1074 }