]> git.bitcoin.ninja Git - rust-lightning/blob - lightning-invoice/src/lib.rs
Qualify the BOLT 11 invoice type
[rust-lightning] / lightning-invoice / src / lib.rs
1 // Prefix these with `rustdoc::` when we update our MSRV to be >= 1.52 to remove warnings.
2 #![deny(broken_intra_doc_links)]
3 #![deny(private_intra_doc_links)]
4
5 #![deny(missing_docs)]
6 #![deny(non_upper_case_globals)]
7 #![deny(non_camel_case_types)]
8 #![deny(non_snake_case)]
9 #![deny(unused_mut)]
10
11 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
12
13 #![cfg_attr(feature = "strict", deny(warnings))]
14 #![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
15
16 //! This crate provides data structures to represent
17 //! [lightning BOLT11](https://github.com/lightning/bolts/blob/master/11-payment-encoding.md)
18 //! invoices and functions to create, encode and decode these. If you just want to use the standard
19 //! en-/decoding functionality this should get you started:
20 //!
21 //!   * For parsing use `str::parse::<Bolt11Invoice>(&self)` (see [`Bolt11Invoice::from_str`])
22 //!   * For constructing invoices use the [`InvoiceBuilder`]
23 //!   * For serializing invoices use the [`Display`]/[`ToString`] traits
24 //!
25 //! [`Bolt11Invoice::from_str`]: crate::Bolt11Invoice#impl-FromStr
26
27 #[cfg(not(any(feature = "std", feature = "no-std")))]
28 compile_error!("at least one of the `std` or `no-std` features must be enabled");
29
30 pub mod payment;
31 pub mod utils;
32
33 pub(crate) mod time_utils;
34
35 extern crate bech32;
36 extern crate bitcoin_hashes;
37 #[macro_use] extern crate lightning;
38 extern crate num_traits;
39 extern crate secp256k1;
40 extern crate alloc;
41 #[cfg(any(test, feature = "std"))]
42 extern crate core;
43 #[cfg(feature = "serde")]
44 extern crate serde;
45
46 #[cfg(feature = "std")]
47 use std::time::SystemTime;
48
49 use bech32::u5;
50 use bitcoin::{Address, Network, PubkeyHash, ScriptHash};
51 use bitcoin::util::address::{Payload, WitnessVersion};
52 use bitcoin_hashes::{Hash, sha256};
53 use lightning::ln::PaymentSecret;
54 use lightning::ln::features::InvoiceFeatures;
55 #[cfg(any(doc, test))]
56 use lightning::routing::gossip::RoutingFees;
57 use lightning::routing::router::RouteHint;
58 use lightning::util::invoice::construct_invoice_preimage;
59
60 use secp256k1::PublicKey;
61 use secp256k1::{Message, Secp256k1};
62 use secp256k1::ecdsa::RecoverableSignature;
63
64 use core::cmp::Ordering;
65 use core::fmt::{Display, Formatter, self};
66 use core::iter::FilterMap;
67 use core::num::ParseIntError;
68 use core::ops::Deref;
69 use core::slice::Iter;
70 use core::time::Duration;
71 use core::str;
72
73 #[cfg(feature = "serde")]
74 use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error};
75
76 mod de;
77 mod ser;
78 mod tb;
79
80 mod prelude {
81         #[cfg(feature = "hashbrown")]
82         extern crate hashbrown;
83
84         pub use alloc::{vec, vec::Vec, string::String, collections::VecDeque, boxed::Box};
85         #[cfg(not(feature = "hashbrown"))]
86         pub use std::collections::{HashMap, HashSet, hash_map};
87         #[cfg(feature = "hashbrown")]
88         pub use self::hashbrown::{HashMap, HashSet, hash_map};
89
90         pub use alloc::string::ToString;
91 }
92
93 use crate::prelude::*;
94
95 /// Sync compat for std/no_std
96 #[cfg(feature = "std")]
97 mod sync {
98         pub use ::std::sync::{Mutex, MutexGuard};
99 }
100
101 /// Sync compat for std/no_std
102 #[cfg(not(feature = "std"))]
103 mod sync;
104
105 /// Errors that indicate what is wrong with the invoice. They have some granularity for debug
106 /// reasons, but should generally result in an "invalid BOLT11 invoice" message for the user.
107 #[allow(missing_docs)]
108 #[derive(PartialEq, Eq, Debug, Clone)]
109 pub enum ParseError {
110         Bech32Error(bech32::Error),
111         ParseAmountError(ParseIntError),
112         MalformedSignature(secp256k1::Error),
113         BadPrefix,
114         UnknownCurrency,
115         UnknownSiPrefix,
116         MalformedHRP,
117         TooShortDataPart,
118         UnexpectedEndOfTaggedFields,
119         DescriptionDecodeError(str::Utf8Error),
120         PaddingError,
121         IntegerOverflowError,
122         InvalidSegWitProgramLength,
123         InvalidPubKeyHashLength,
124         InvalidScriptHashLength,
125         InvalidRecoveryId,
126         InvalidSliceLength(String),
127
128         /// Not an error, but used internally to signal that a part of the invoice should be ignored
129         /// according to BOLT11
130         Skip,
131 }
132
133 /// Indicates that something went wrong while parsing or validating the invoice. Parsing errors
134 /// should be mostly seen as opaque and are only there for debugging reasons. Semantic errors
135 /// like wrong signatures, missing fields etc. could mean that someone tampered with the invoice.
136 #[derive(PartialEq, Eq, Debug, Clone)]
137 pub enum ParseOrSemanticError {
138         /// The invoice couldn't be decoded
139         ParseError(ParseError),
140
141         /// The invoice could be decoded but violates the BOLT11 standard
142         SemanticError(crate::SemanticError),
143 }
144
145 /// The number of bits used to represent timestamps as defined in BOLT 11.
146 const TIMESTAMP_BITS: usize = 35;
147
148 /// The maximum timestamp as [`Duration::as_secs`] since the Unix epoch allowed by [`BOLT 11`].
149 ///
150 /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
151 pub const MAX_TIMESTAMP: u64 = (1 << TIMESTAMP_BITS) - 1;
152
153 /// Default expiry time as defined by [BOLT 11].
154 ///
155 /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
156 pub const DEFAULT_EXPIRY_TIME: u64 = 3600;
157
158 /// Default minimum final CLTV expiry as defined by [BOLT 11].
159 ///
160 /// Note that this is *not* the same value as rust-lightning's minimum CLTV expiry, which is
161 /// provided in [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
162 ///
163 /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
164 /// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA
165 pub const DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA: u64 = 18;
166
167 /// Builder for [`Bolt11Invoice`]s. It's the most convenient and advised way to use this library. It
168 /// ensures that only a semantically and syntactically correct invoice can be built using it.
169 ///
170 /// ```
171 /// extern crate secp256k1;
172 /// extern crate lightning;
173 /// extern crate lightning_invoice;
174 /// extern crate bitcoin_hashes;
175 ///
176 /// use bitcoin_hashes::Hash;
177 /// use bitcoin_hashes::sha256;
178 ///
179 /// use secp256k1::Secp256k1;
180 /// use secp256k1::SecretKey;
181 ///
182 /// use lightning::ln::PaymentSecret;
183 ///
184 /// use lightning_invoice::{Currency, InvoiceBuilder};
185 ///
186 /// # #[cfg(not(feature = "std"))]
187 /// # fn main() {}
188 /// # #[cfg(feature = "std")]
189 /// # fn main() {
190 /// let private_key = SecretKey::from_slice(
191 ///             &[
192 ///                     0xe1, 0x26, 0xf6, 0x8f, 0x7e, 0xaf, 0xcc, 0x8b, 0x74, 0xf5, 0x4d, 0x26, 0x9f,
193 ///                     0xe2, 0x06, 0xbe, 0x71, 0x50, 0x00, 0xf9, 0x4d, 0xac, 0x06, 0x7d, 0x1c, 0x04,
194 ///             0xa8, 0xca, 0x3b, 0x2d, 0xb7, 0x34
195 ///     ][..]
196 ///     ).unwrap();
197 ///
198 /// let payment_hash = sha256::Hash::from_slice(&[0; 32][..]).unwrap();
199 /// let payment_secret = PaymentSecret([42u8; 32]);
200 ///
201 /// let invoice = InvoiceBuilder::new(Currency::Bitcoin)
202 ///     .description("Coins pls!".into())
203 ///     .payment_hash(payment_hash)
204 ///     .payment_secret(payment_secret)
205 ///     .current_timestamp()
206 ///     .min_final_cltv_expiry_delta(144)
207 ///     .build_signed(|hash| {
208 ///             Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)
209 ///     })
210 ///     .unwrap();
211 ///
212 /// assert!(invoice.to_string().starts_with("lnbc1"));
213 /// # }
214 /// ```
215 ///
216 /// # Type parameters
217 /// The two parameters `D` and `H` signal if the builder already contains the correct amount of the
218 /// given field:
219 ///  * `D`: exactly one [`TaggedField::Description`] or [`TaggedField::DescriptionHash`]
220 ///  * `H`: exactly one [`TaggedField::PaymentHash`]
221 ///  * `T`: the timestamp is set
222 ///  * `C`: the CLTV expiry is set
223 ///  * `S`: the payment secret is set
224 ///  * `M`: payment metadata is set
225 ///
226 /// This is not exported to bindings users as we likely need to manually select one set of boolean type parameters.
227 #[derive(Eq, PartialEq, Debug, Clone)]
228 pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool, M: tb::Bool> {
229         currency: Currency,
230         amount: Option<u64>,
231         si_prefix: Option<SiPrefix>,
232         timestamp: Option<PositiveTimestamp>,
233         tagged_fields: Vec<TaggedField>,
234         error: Option<CreationError>,
235
236         phantom_d: core::marker::PhantomData<D>,
237         phantom_h: core::marker::PhantomData<H>,
238         phantom_t: core::marker::PhantomData<T>,
239         phantom_c: core::marker::PhantomData<C>,
240         phantom_s: core::marker::PhantomData<S>,
241         phantom_m: core::marker::PhantomData<M>,
242 }
243
244 /// Represents a syntactically and semantically correct lightning BOLT11 invoice.
245 ///
246 /// There are three ways to construct a `Bolt11Invoice`:
247 ///  1. using [`InvoiceBuilder`]
248 ///  2. using [`Bolt11Invoice::from_signed`]
249 ///  3. using `str::parse::<Bolt11Invoice>(&str)` (see [`Bolt11Invoice::from_str`])
250 ///
251 /// [`Bolt11Invoice::from_str`]: crate::Bolt11Invoice#impl-FromStr
252 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
253 pub struct Bolt11Invoice {
254         signed_invoice: SignedRawInvoice,
255 }
256
257 /// Represents the description of an invoice which has to be either a directly included string or
258 /// a hash of a description provided out of band.
259 ///
260 /// This is not exported to bindings users as we don't have a good way to map the reference lifetimes making this
261 /// practically impossible to use safely in languages like C.
262 #[derive(Eq, PartialEq, Debug, Clone, Ord, PartialOrd)]
263 pub enum InvoiceDescription<'f> {
264         /// Reference to the directly supplied description in the invoice
265         Direct(&'f Description),
266
267         /// Reference to the description's hash included in the invoice
268         Hash(&'f Sha256),
269 }
270
271 /// Represents a signed [`RawInvoice`] with cached hash. The signature is not checked and may be
272 /// invalid.
273 ///
274 /// # Invariants
275 /// The hash has to be either from the deserialized invoice or from the serialized [`RawInvoice`].
276 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
277 pub struct SignedRawInvoice {
278         /// The rawInvoice that the signature belongs to
279         raw_invoice: RawInvoice,
280
281         /// Hash of the [`RawInvoice`] that will be used to check the signature.
282         ///
283         /// * if the `SignedRawInvoice` was deserialized the hash is of from the original encoded form,
284         /// since it's not guaranteed that encoding it again will lead to the same result since integers
285         /// could have been encoded with leading zeroes etc.
286         /// * if the `SignedRawInvoice` was constructed manually the hash will be the calculated hash
287         /// from the [`RawInvoice`]
288         hash: [u8; 32],
289
290         /// signature of the payment request
291         signature: InvoiceSignature,
292 }
293
294 /// Represents an syntactically correct [`Bolt11Invoice`] for a payment on the lightning network,
295 /// but without the signature information.
296 /// Decoding and encoding should not lead to information loss but may lead to different hashes.
297 ///
298 /// For methods without docs see the corresponding methods in [`Bolt11Invoice`].
299 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
300 pub struct RawInvoice {
301         /// human readable part
302         pub hrp: RawHrp,
303
304         /// data part
305         pub data: RawDataPart,
306 }
307
308 /// Data of the [`RawInvoice`] that is encoded in the human readable part.
309 ///
310 /// This is not exported to bindings users as we don't yet support `Option<Enum>`
311 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
312 pub struct RawHrp {
313         /// The currency deferred from the 3rd and 4th character of the bech32 transaction
314         pub currency: Currency,
315
316         /// The amount that, multiplied by the SI prefix, has to be payed
317         pub raw_amount: Option<u64>,
318
319         /// SI prefix that gets multiplied with the `raw_amount`
320         pub si_prefix: Option<SiPrefix>,
321 }
322
323 /// Data of the [`RawInvoice`] that is encoded in the data part
324 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
325 pub struct RawDataPart {
326         /// generation time of the invoice
327         pub timestamp: PositiveTimestamp,
328
329         /// tagged fields of the payment request
330         pub tagged_fields: Vec<RawTaggedField>,
331 }
332
333 /// A timestamp that refers to a date after 1 January 1970.
334 ///
335 /// # Invariants
336 ///
337 /// The Unix timestamp representing the stored time has to be positive and no greater than
338 /// [`MAX_TIMESTAMP`].
339 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
340 pub struct PositiveTimestamp(Duration);
341
342 /// SI prefixes for the human readable part
343 #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Ord, PartialOrd)]
344 pub enum SiPrefix {
345         /// 10^-3
346         Milli,
347         /// 10^-6
348         Micro,
349         /// 10^-9
350         Nano,
351         /// 10^-12
352         Pico,
353 }
354
355 impl SiPrefix {
356         /// Returns the multiplier to go from a BTC value to picoBTC implied by this SiPrefix.
357         /// This is effectively 10^12 * the prefix multiplier
358         pub fn multiplier(&self) -> u64 {
359                 match *self {
360                         SiPrefix::Milli => 1_000_000_000,
361                         SiPrefix::Micro => 1_000_000,
362                         SiPrefix::Nano => 1_000,
363                         SiPrefix::Pico => 1,
364                 }
365         }
366
367         /// Returns all enum variants of `SiPrefix` sorted in descending order of their associated
368         /// multiplier.
369         ///
370         /// This is not exported to bindings users as we don't yet support a slice of enums, and also because this function
371         /// isn't the most critical to expose.
372         pub fn values_desc() -> &'static [SiPrefix] {
373                 use crate::SiPrefix::*;
374                 static VALUES: [SiPrefix; 4] = [Milli, Micro, Nano, Pico];
375                 &VALUES
376         }
377 }
378
379 /// Enum representing the crypto currencies (or networks) supported by this library
380 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
381 pub enum Currency {
382         /// Bitcoin mainnet
383         Bitcoin,
384
385         /// Bitcoin testnet
386         BitcoinTestnet,
387
388         /// Bitcoin regtest
389         Regtest,
390
391         /// Bitcoin simnet
392         Simnet,
393
394         /// Bitcoin signet
395         Signet,
396 }
397
398 impl From<Network> for Currency {
399         fn from(network: Network) -> Self {
400                 match network {
401                         Network::Bitcoin => Currency::Bitcoin,
402                         Network::Testnet => Currency::BitcoinTestnet,
403                         Network::Regtest => Currency::Regtest,
404                         Network::Signet => Currency::Signet,
405                 }
406         }
407 }
408
409 impl From<Currency> for Network {
410         fn from(currency: Currency) -> Self {
411                 match currency {
412                         Currency::Bitcoin => Network::Bitcoin,
413                         Currency::BitcoinTestnet => Network::Testnet,
414                         Currency::Regtest => Network::Regtest,
415                         Currency::Simnet => Network::Regtest,
416                         Currency::Signet => Network::Signet,
417                 }
418         }
419 }
420
421 /// Tagged field which may have an unknown tag
422 ///
423 /// This is not exported to bindings users as we don't currently support TaggedField
424 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
425 pub enum RawTaggedField {
426         /// Parsed tagged field with known tag
427         KnownSemantics(TaggedField),
428         /// tagged field which was not parsed due to an unknown tag or undefined field semantics
429         UnknownSemantics(Vec<u5>),
430 }
431
432 /// Tagged field with known tag
433 ///
434 /// For descriptions of the enum values please refer to the enclosed type's docs.
435 ///
436 /// This is not exported to bindings users as we don't yet support enum variants with the same name the struct contained
437 /// in the variant.
438 #[allow(missing_docs)]
439 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
440 pub enum TaggedField {
441         PaymentHash(Sha256),
442         Description(Description),
443         PayeePubKey(PayeePubKey),
444         DescriptionHash(Sha256),
445         ExpiryTime(ExpiryTime),
446         MinFinalCltvExpiryDelta(MinFinalCltvExpiryDelta),
447         Fallback(Fallback),
448         PrivateRoute(PrivateRoute),
449         PaymentSecret(PaymentSecret),
450         PaymentMetadata(Vec<u8>),
451         Features(InvoiceFeatures),
452 }
453
454 /// SHA-256 hash
455 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
456 pub struct Sha256(/// This is not exported to bindings users as the native hash types are not currently mapped
457         pub sha256::Hash);
458
459 impl Sha256 {
460         /// Constructs a new [`Sha256`] from the given bytes, which are assumed to be the output of a
461         /// single sha256 hash.
462         #[cfg(c_bindings)]
463         pub fn from_bytes(bytes: &[u8; 32]) -> Self {
464                 Self(sha256::Hash::from_slice(bytes).expect("from_slice only fails if len is not 32"))
465         }
466 }
467
468 /// Description string
469 ///
470 /// # Invariants
471 /// The description can be at most 639 __bytes__ long
472 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
473 pub struct Description(String);
474
475 /// Payee public key
476 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
477 pub struct PayeePubKey(pub PublicKey);
478
479 /// Positive duration that defines when (relatively to the timestamp) in the future the invoice
480 /// expires
481 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
482 pub struct ExpiryTime(Duration);
483
484 /// `min_final_cltv_expiry_delta` to use for the last HTLC in the route
485 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
486 pub struct MinFinalCltvExpiryDelta(pub u64);
487
488 /// Fallback address in case no LN payment is possible
489 #[allow(missing_docs)]
490 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
491 pub enum Fallback {
492         SegWitProgram {
493                 version: WitnessVersion,
494                 program: Vec<u8>,
495         },
496         PubKeyHash(PubkeyHash),
497         ScriptHash(ScriptHash),
498 }
499
500 /// Recoverable signature
501 #[derive(Clone, Debug, Hash, Eq, PartialEq)]
502 pub struct InvoiceSignature(pub RecoverableSignature);
503
504 impl PartialOrd for InvoiceSignature {
505         fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
506                 self.0.serialize_compact().1.partial_cmp(&other.0.serialize_compact().1)
507         }
508 }
509
510 impl Ord for InvoiceSignature {
511         fn cmp(&self, other: &Self) -> Ordering {
512                 self.0.serialize_compact().1.cmp(&other.0.serialize_compact().1)
513         }
514 }
515
516 /// Private routing information
517 ///
518 /// # Invariants
519 /// The encoded route has to be <1024 5bit characters long (<=639 bytes or <=12 hops)
520 ///
521 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
522 pub struct PrivateRoute(RouteHint);
523
524 /// Tag constants as specified in BOLT11
525 #[allow(missing_docs)]
526 pub mod constants {
527         pub const TAG_PAYMENT_HASH: u8 = 1;
528         pub const TAG_DESCRIPTION: u8 = 13;
529         pub const TAG_PAYEE_PUB_KEY: u8 = 19;
530         pub const TAG_DESCRIPTION_HASH: u8 = 23;
531         pub const TAG_EXPIRY_TIME: u8 = 6;
532         pub const TAG_MIN_FINAL_CLTV_EXPIRY_DELTA: u8 = 24;
533         pub const TAG_FALLBACK: u8 = 9;
534         pub const TAG_PRIVATE_ROUTE: u8 = 3;
535         pub const TAG_PAYMENT_SECRET: u8 = 16;
536         pub const TAG_PAYMENT_METADATA: u8 = 27;
537         pub const TAG_FEATURES: u8 = 5;
538 }
539
540 impl InvoiceBuilder<tb::False, tb::False, tb::False, tb::False, tb::False, tb::False> {
541         /// Construct new, empty `InvoiceBuilder`. All necessary fields have to be filled first before
542         /// `InvoiceBuilder::build(self)` becomes available.
543         pub fn new(currency: Currency) -> Self {
544                 InvoiceBuilder {
545                         currency,
546                         amount: None,
547                         si_prefix: None,
548                         timestamp: None,
549                         tagged_fields: Vec::new(),
550                         error: None,
551
552                         phantom_d: core::marker::PhantomData,
553                         phantom_h: core::marker::PhantomData,
554                         phantom_t: core::marker::PhantomData,
555                         phantom_c: core::marker::PhantomData,
556                         phantom_s: core::marker::PhantomData,
557                         phantom_m: core::marker::PhantomData,
558                 }
559         }
560 }
561
562 impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool, M: tb::Bool> InvoiceBuilder<D, H, T, C, S, M> {
563         /// Helper function to set the completeness flags.
564         fn set_flags<DN: tb::Bool, HN: tb::Bool, TN: tb::Bool, CN: tb::Bool, SN: tb::Bool, MN: tb::Bool>(self) -> InvoiceBuilder<DN, HN, TN, CN, SN, MN> {
565                 InvoiceBuilder::<DN, HN, TN, CN, SN, MN> {
566                         currency: self.currency,
567                         amount: self.amount,
568                         si_prefix: self.si_prefix,
569                         timestamp: self.timestamp,
570                         tagged_fields: self.tagged_fields,
571                         error: self.error,
572
573                         phantom_d: core::marker::PhantomData,
574                         phantom_h: core::marker::PhantomData,
575                         phantom_t: core::marker::PhantomData,
576                         phantom_c: core::marker::PhantomData,
577                         phantom_s: core::marker::PhantomData,
578                         phantom_m: core::marker::PhantomData,
579                 }
580         }
581
582         /// Sets the amount in millisatoshis. The optimal SI prefix is chosen automatically.
583         pub fn amount_milli_satoshis(mut self, amount_msat: u64) -> Self {
584                 let amount = amount_msat * 10; // Invoices are denominated in "pico BTC"
585                 let biggest_possible_si_prefix = SiPrefix::values_desc()
586                         .iter()
587                         .find(|prefix| amount % prefix.multiplier() == 0)
588                         .expect("Pico should always match");
589                 self.amount = Some(amount / biggest_possible_si_prefix.multiplier());
590                 self.si_prefix = Some(*biggest_possible_si_prefix);
591                 self
592         }
593
594         /// Sets the payee's public key.
595         pub fn payee_pub_key(mut self, pub_key: PublicKey) -> Self {
596                 self.tagged_fields.push(TaggedField::PayeePubKey(PayeePubKey(pub_key)));
597                 self
598         }
599
600         /// Sets the expiry time, dropping the subsecond part (which is not representable in BOLT 11
601         /// invoices).
602         pub fn expiry_time(mut self, expiry_time: Duration) -> Self {
603                 self.tagged_fields.push(TaggedField::ExpiryTime(ExpiryTime::from_duration(expiry_time)));
604                 self
605         }
606
607         /// Adds a fallback address.
608         pub fn fallback(mut self, fallback: Fallback) -> Self {
609                 self.tagged_fields.push(TaggedField::Fallback(fallback));
610                 self
611         }
612
613         /// Adds a private route.
614         pub fn private_route(mut self, hint: RouteHint) -> Self {
615                 match PrivateRoute::new(hint) {
616                         Ok(r) => self.tagged_fields.push(TaggedField::PrivateRoute(r)),
617                         Err(e) => self.error = Some(e),
618                 }
619                 self
620         }
621 }
622
623 impl<D: tb::Bool, H: tb::Bool, C: tb::Bool, S: tb::Bool, M: tb::Bool> InvoiceBuilder<D, H, tb::True, C, S, M> {
624         /// Builds a [`RawInvoice`] if no [`CreationError`] occurred while construction any of the
625         /// fields.
626         pub fn build_raw(self) -> Result<RawInvoice, CreationError> {
627
628                 // If an error occurred at any time before, return it now
629                 if let Some(e) = self.error {
630                         return Err(e);
631                 }
632
633                 let hrp = RawHrp {
634                         currency: self.currency,
635                         raw_amount: self.amount,
636                         si_prefix: self.si_prefix,
637                 };
638
639                 let timestamp = self.timestamp.expect("ensured to be Some(t) by type T");
640
641                 let tagged_fields = self.tagged_fields.into_iter().map(|tf| {
642                         RawTaggedField::KnownSemantics(tf)
643                 }).collect::<Vec<_>>();
644
645                 let data = RawDataPart {
646                         timestamp,
647                         tagged_fields,
648                 };
649
650                 Ok(RawInvoice {
651                         hrp,
652                         data,
653                 })
654         }
655 }
656
657 impl<H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool, M: tb::Bool> InvoiceBuilder<tb::False, H, T, C, S, M> {
658         /// Set the description. This function is only available if no description (hash) was set.
659         pub fn description(mut self, description: String) -> InvoiceBuilder<tb::True, H, T, C, S, M> {
660                 match Description::new(description) {
661                         Ok(d) => self.tagged_fields.push(TaggedField::Description(d)),
662                         Err(e) => self.error = Some(e),
663                 }
664                 self.set_flags()
665         }
666
667         /// Set the description hash. This function is only available if no description (hash) was set.
668         pub fn description_hash(mut self, description_hash: sha256::Hash) -> InvoiceBuilder<tb::True, H, T, C, S, M> {
669                 self.tagged_fields.push(TaggedField::DescriptionHash(Sha256(description_hash)));
670                 self.set_flags()
671         }
672
673         /// Set the description or description hash. This function is only available if no description (hash) was set.
674         pub fn invoice_description(self, description: InvoiceDescription) -> InvoiceBuilder<tb::True, H, T, C, S, M> {
675                 match description {
676                         InvoiceDescription::Direct(desc) => {
677                                 self.description(desc.clone().into_inner())
678                         }
679                         InvoiceDescription::Hash(hash) => {
680                                 self.description_hash(hash.0)
681                         }
682                 }
683         }
684 }
685
686 impl<D: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool, M: tb::Bool> InvoiceBuilder<D, tb::False, T, C, S, M> {
687         /// Set the payment hash. This function is only available if no payment hash was set.
688         pub fn payment_hash(mut self, hash: sha256::Hash) -> InvoiceBuilder<D, tb::True, T, C, S, M> {
689                 self.tagged_fields.push(TaggedField::PaymentHash(Sha256(hash)));
690                 self.set_flags()
691         }
692 }
693
694 impl<D: tb::Bool, H: tb::Bool, C: tb::Bool, S: tb::Bool, M: tb::Bool> InvoiceBuilder<D, H, tb::False, C, S, M> {
695         /// Sets the timestamp to a specific [`SystemTime`].
696         #[cfg(feature = "std")]
697         pub fn timestamp(mut self, time: SystemTime) -> InvoiceBuilder<D, H, tb::True, C, S, M> {
698                 match PositiveTimestamp::from_system_time(time) {
699                         Ok(t) => self.timestamp = Some(t),
700                         Err(e) => self.error = Some(e),
701                 }
702
703                 self.set_flags()
704         }
705
706         /// Sets the timestamp to a duration since the Unix epoch, dropping the subsecond part (which
707         /// is not representable in BOLT 11 invoices).
708         pub fn duration_since_epoch(mut self, time: Duration) -> InvoiceBuilder<D, H, tb::True, C, S, M> {
709                 match PositiveTimestamp::from_duration_since_epoch(time) {
710                         Ok(t) => self.timestamp = Some(t),
711                         Err(e) => self.error = Some(e),
712                 }
713
714                 self.set_flags()
715         }
716
717         /// Sets the timestamp to the current system time.
718         #[cfg(feature = "std")]
719         pub fn current_timestamp(mut self) -> InvoiceBuilder<D, H, tb::True, C, S, M> {
720                 let now = PositiveTimestamp::from_system_time(SystemTime::now());
721                 self.timestamp = Some(now.expect("for the foreseeable future this shouldn't happen"));
722                 self.set_flags()
723         }
724 }
725
726 impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, S: tb::Bool, M: tb::Bool> InvoiceBuilder<D, H, T, tb::False, S, M> {
727         /// Sets `min_final_cltv_expiry_delta`.
728         pub fn min_final_cltv_expiry_delta(mut self, min_final_cltv_expiry_delta: u64) -> InvoiceBuilder<D, H, T, tb::True, S, M> {
729                 self.tagged_fields.push(TaggedField::MinFinalCltvExpiryDelta(MinFinalCltvExpiryDelta(min_final_cltv_expiry_delta)));
730                 self.set_flags()
731         }
732 }
733
734 impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, M: tb::Bool> InvoiceBuilder<D, H, T, C, tb::False, M> {
735         /// Sets the payment secret and relevant features.
736         pub fn payment_secret(mut self, payment_secret: PaymentSecret) -> InvoiceBuilder<D, H, T, C, tb::True, M> {
737                 let mut found_features = false;
738                 for field in self.tagged_fields.iter_mut() {
739                         if let TaggedField::Features(f) = field {
740                                 found_features = true;
741                                 f.set_variable_length_onion_required();
742                                 f.set_payment_secret_required();
743                         }
744                 }
745                 self.tagged_fields.push(TaggedField::PaymentSecret(payment_secret));
746                 if !found_features {
747                         let mut features = InvoiceFeatures::empty();
748                         features.set_variable_length_onion_required();
749                         features.set_payment_secret_required();
750                         self.tagged_fields.push(TaggedField::Features(features));
751                 }
752                 self.set_flags()
753         }
754 }
755
756 impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBuilder<D, H, T, C, S, tb::False> {
757         /// Sets the payment metadata.
758         ///
759         /// By default features are set to *optionally* allow the sender to include the payment metadata.
760         /// If you wish to require that the sender include the metadata (and fail to parse the invoice if
761         /// they don't support payment metadata fields), you need to call
762         /// [`InvoiceBuilder::require_payment_metadata`] after this.
763         pub fn payment_metadata(mut self, payment_metadata: Vec<u8>) -> InvoiceBuilder<D, H, T, C, S, tb::True> {
764                 self.tagged_fields.push(TaggedField::PaymentMetadata(payment_metadata));
765                 let mut found_features = false;
766                 for field in self.tagged_fields.iter_mut() {
767                         if let TaggedField::Features(f) = field {
768                                 found_features = true;
769                                 f.set_payment_metadata_optional();
770                         }
771                 }
772                 if !found_features {
773                         let mut features = InvoiceFeatures::empty();
774                         features.set_payment_metadata_optional();
775                         self.tagged_fields.push(TaggedField::Features(features));
776                 }
777                 self.set_flags()
778         }
779 }
780
781 impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBuilder<D, H, T, C, S, tb::True> {
782         /// Sets forwarding of payment metadata as required. A reader of the invoice which does not
783         /// support sending payment metadata will fail to read the invoice.
784         pub fn require_payment_metadata(mut self) -> InvoiceBuilder<D, H, T, C, S, tb::True> {
785                 for field in self.tagged_fields.iter_mut() {
786                         if let TaggedField::Features(f) = field {
787                                 f.set_payment_metadata_required();
788                         }
789                 }
790                 self
791         }
792 }
793
794 impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, M: tb::Bool> InvoiceBuilder<D, H, T, C, tb::True, M> {
795         /// Sets the `basic_mpp` feature as optional.
796         pub fn basic_mpp(mut self) -> Self {
797                 for field in self.tagged_fields.iter_mut() {
798                         if let TaggedField::Features(f) = field {
799                                 f.set_basic_mpp_optional();
800                         }
801                 }
802                 self
803         }
804 }
805
806 impl<M: tb::Bool> InvoiceBuilder<tb::True, tb::True, tb::True, tb::True, tb::True, M> {
807         /// Builds and signs an invoice using the supplied `sign_function`. This function MAY NOT fail
808         /// and MUST produce a recoverable signature valid for the given hash and if applicable also for
809         /// the included payee public key.
810         pub fn build_signed<F>(self, sign_function: F) -> Result<Bolt11Invoice, CreationError>
811                 where F: FnOnce(&Message) -> RecoverableSignature
812         {
813                 let invoice = self.try_build_signed::<_, ()>(|hash| {
814                         Ok(sign_function(hash))
815                 });
816
817                 match invoice {
818                         Ok(i) => Ok(i),
819                         Err(SignOrCreationError::CreationError(e)) => Err(e),
820                         Err(SignOrCreationError::SignError(())) => unreachable!(),
821                 }
822         }
823
824         /// Builds and signs an invoice using the supplied `sign_function`. This function MAY fail with
825         /// an error of type `E` and MUST produce a recoverable signature valid for the given hash and
826         /// if applicable also for the included payee public key.
827         pub fn try_build_signed<F, E>(self, sign_function: F) -> Result<Bolt11Invoice, SignOrCreationError<E>>
828                 where F: FnOnce(&Message) -> Result<RecoverableSignature, E>
829         {
830                 let raw = match self.build_raw() {
831                         Ok(r) => r,
832                         Err(e) => return Err(SignOrCreationError::CreationError(e)),
833                 };
834
835                 let signed = match raw.sign(sign_function) {
836                         Ok(s) => s,
837                         Err(e) => return Err(SignOrCreationError::SignError(e)),
838                 };
839
840                 let invoice = Bolt11Invoice {
841                         signed_invoice: signed,
842                 };
843
844                 invoice.check_field_counts().expect("should be ensured by type signature of builder");
845                 invoice.check_feature_bits().expect("should be ensured by type signature of builder");
846                 invoice.check_amount().expect("should be ensured by type signature of builder");
847
848                 Ok(invoice)
849         }
850 }
851
852
853 impl SignedRawInvoice {
854         /// Disassembles the `SignedRawInvoice` into its three parts:
855         ///  1. raw invoice
856         ///  2. hash of the raw invoice
857         ///  3. signature
858         pub fn into_parts(self) -> (RawInvoice, [u8; 32], InvoiceSignature) {
859                 (self.raw_invoice, self.hash, self.signature)
860         }
861
862         /// The [`RawInvoice`] which was signed.
863         pub fn raw_invoice(&self) -> &RawInvoice {
864                 &self.raw_invoice
865         }
866
867         /// The hash of the [`RawInvoice`] that was signed.
868         pub fn signable_hash(&self) -> &[u8; 32] {
869                 &self.hash
870         }
871
872         /// Signature for the invoice.
873         pub fn signature(&self) -> &InvoiceSignature {
874                 &self.signature
875         }
876
877         /// Recovers the public key used for signing the invoice from the recoverable signature.
878         pub fn recover_payee_pub_key(&self) -> Result<PayeePubKey, secp256k1::Error> {
879                 let hash = Message::from_slice(&self.hash[..])
880                         .expect("Hash is 32 bytes long, same as MESSAGE_SIZE");
881
882                 Ok(PayeePubKey(Secp256k1::new().recover_ecdsa(
883                         &hash,
884                         &self.signature
885                 )?))
886         }
887
888         /// Checks if the signature is valid for the included payee public key or if none exists if it's
889         /// valid for the recovered signature (which should always be true?).
890         pub fn check_signature(&self) -> bool {
891                 let included_pub_key = self.raw_invoice.payee_pub_key();
892
893                 let mut recovered_pub_key = Option::None;
894                 if recovered_pub_key.is_none() {
895                         let recovered = match self.recover_payee_pub_key() {
896                                 Ok(pk) => pk,
897                                 Err(_) => return false,
898                         };
899                         recovered_pub_key = Some(recovered);
900                 }
901
902                 let pub_key = included_pub_key.or(recovered_pub_key.as_ref())
903                         .expect("One is always present");
904
905                 let hash = Message::from_slice(&self.hash[..])
906                         .expect("Hash is 32 bytes long, same as MESSAGE_SIZE");
907
908                 let secp_context = Secp256k1::new();
909                 let verification_result = secp_context.verify_ecdsa(
910                         &hash,
911                         &self.signature.to_standard(),
912                         pub_key
913                 );
914
915                 match verification_result {
916                         Ok(()) => true,
917                         Err(_) => false,
918                 }
919         }
920 }
921
922 /// Finds the first element of an enum stream of a given variant and extracts one member of the
923 /// variant. If no element was found `None` gets returned.
924 ///
925 /// The following example would extract the first B.
926 ///
927 /// ```ignore
928 /// enum Enum {
929 ///     A(u8),
930 ///     B(u16)
931 /// }
932 ///
933 /// let elements = vec![Enum::A(1), Enum::A(2), Enum::B(3), Enum::A(4)];
934 ///
935 /// assert_eq!(find_extract!(elements.iter(), Enum::B(x), x), Some(3u16));
936 /// ```
937 macro_rules! find_extract {
938         ($iter:expr, $enm:pat, $enm_var:ident) => {
939                 find_all_extract!($iter, $enm, $enm_var).next()
940         };
941 }
942
943 /// Finds the all elements of an enum stream of a given variant and extracts one member of the
944 /// variant through an iterator.
945 ///
946 /// The following example would extract all A.
947 ///
948 /// ```ignore
949 /// enum Enum {
950 ///     A(u8),
951 ///     B(u16)
952 /// }
953 ///
954 /// let elements = vec![Enum::A(1), Enum::A(2), Enum::B(3), Enum::A(4)];
955 ///
956 /// assert_eq!(
957 ///     find_all_extract!(elements.iter(), Enum::A(x), x).collect::<Vec<u8>>(),
958 ///     vec![1u8, 2u8, 4u8]
959 /// );
960 /// ```
961 macro_rules! find_all_extract {
962         ($iter:expr, $enm:pat, $enm_var:ident) => {
963                 $iter.filter_map(|tf| match *tf {
964                         $enm => Some($enm_var),
965                         _ => None,
966                 })
967         };
968 }
969
970 #[allow(missing_docs)]
971 impl RawInvoice {
972         /// Hash the HRP as bytes and signatureless data part.
973         fn hash_from_parts(hrp_bytes: &[u8], data_without_signature: &[u5]) -> [u8; 32] {
974                 let preimage = construct_invoice_preimage(hrp_bytes, data_without_signature);
975                 let mut hash: [u8; 32] = Default::default();
976                 hash.copy_from_slice(&sha256::Hash::hash(&preimage)[..]);
977                 hash
978         }
979
980         /// Calculate the hash of the encoded `RawInvoice` which should be signed.
981         pub fn signable_hash(&self) -> [u8; 32] {
982                 use bech32::ToBase32;
983
984                 RawInvoice::hash_from_parts(
985                         self.hrp.to_string().as_bytes(),
986                         &self.data.to_base32()
987                 )
988         }
989
990         /// Signs the invoice using the supplied `sign_method`. This function MAY fail with an error of
991         /// type `E`. Since the signature of a [`SignedRawInvoice`] is not required to be valid there
992         /// are no constraints regarding the validity of the produced signature.
993         ///
994         /// This is not exported to bindings users as we don't currently support passing function pointers into methods
995         /// explicitly.
996         pub fn sign<F, E>(self, sign_method: F) -> Result<SignedRawInvoice, E>
997                 where F: FnOnce(&Message) -> Result<RecoverableSignature, E>
998         {
999                 let raw_hash = self.signable_hash();
1000                 let hash = Message::from_slice(&raw_hash[..])
1001                         .expect("Hash is 32 bytes long, same as MESSAGE_SIZE");
1002                 let signature = sign_method(&hash)?;
1003
1004                 Ok(SignedRawInvoice {
1005                         raw_invoice: self,
1006                         hash: raw_hash,
1007                         signature: InvoiceSignature(signature),
1008                 })
1009         }
1010
1011         /// Returns an iterator over all tagged fields with known semantics.
1012         ///
1013         /// This is not exported to bindings users as there is not yet a manual mapping for a FilterMap
1014         pub fn known_tagged_fields(&self)
1015                 -> FilterMap<Iter<RawTaggedField>, fn(&RawTaggedField) -> Option<&TaggedField>>
1016         {
1017                 // For 1.14.0 compatibility: closures' types can't be written an fn()->() in the
1018                 // function's type signature.
1019                 // TODO: refactor once impl Trait is available
1020                 fn match_raw(raw: &RawTaggedField) -> Option<&TaggedField> {
1021                         match *raw {
1022                                 RawTaggedField::KnownSemantics(ref tf) => Some(tf),
1023                                 _ => None,
1024                         }
1025                 }
1026
1027                 self.data.tagged_fields.iter().filter_map(match_raw )
1028         }
1029
1030         pub fn payment_hash(&self) -> Option<&Sha256> {
1031                 find_extract!(self.known_tagged_fields(), TaggedField::PaymentHash(ref x), x)
1032         }
1033
1034         pub fn description(&self) -> Option<&Description> {
1035                 find_extract!(self.known_tagged_fields(), TaggedField::Description(ref x), x)
1036         }
1037
1038         pub fn payee_pub_key(&self) -> Option<&PayeePubKey> {
1039                 find_extract!(self.known_tagged_fields(), TaggedField::PayeePubKey(ref x), x)
1040         }
1041
1042         pub fn description_hash(&self) -> Option<&Sha256> {
1043                 find_extract!(self.known_tagged_fields(), TaggedField::DescriptionHash(ref x), x)
1044         }
1045
1046         pub fn expiry_time(&self) -> Option<&ExpiryTime> {
1047                 find_extract!(self.known_tagged_fields(), TaggedField::ExpiryTime(ref x), x)
1048         }
1049
1050         pub fn min_final_cltv_expiry_delta(&self) -> Option<&MinFinalCltvExpiryDelta> {
1051                 find_extract!(self.known_tagged_fields(), TaggedField::MinFinalCltvExpiryDelta(ref x), x)
1052         }
1053
1054         pub fn payment_secret(&self) -> Option<&PaymentSecret> {
1055                 find_extract!(self.known_tagged_fields(), TaggedField::PaymentSecret(ref x), x)
1056         }
1057
1058         pub fn payment_metadata(&self) -> Option<&Vec<u8>> {
1059                 find_extract!(self.known_tagged_fields(), TaggedField::PaymentMetadata(ref x), x)
1060         }
1061
1062         pub fn features(&self) -> Option<&InvoiceFeatures> {
1063                 find_extract!(self.known_tagged_fields(), TaggedField::Features(ref x), x)
1064         }
1065
1066         /// This is not exported to bindings users as we don't support Vec<&NonOpaqueType>
1067         pub fn fallbacks(&self) -> Vec<&Fallback> {
1068                 find_all_extract!(self.known_tagged_fields(), TaggedField::Fallback(ref x), x).collect()
1069         }
1070
1071         pub fn private_routes(&self) -> Vec<&PrivateRoute> {
1072                 find_all_extract!(self.known_tagged_fields(), TaggedField::PrivateRoute(ref x), x).collect()
1073         }
1074
1075         pub fn amount_pico_btc(&self) -> Option<u64> {
1076                 self.hrp.raw_amount.map(|v| {
1077                         v * self.hrp.si_prefix.as_ref().map_or(1_000_000_000_000, |si| { si.multiplier() })
1078                 })
1079         }
1080
1081         pub fn currency(&self) -> Currency {
1082                 self.hrp.currency.clone()
1083         }
1084 }
1085
1086 impl PositiveTimestamp {
1087         /// Creates a `PositiveTimestamp` from a Unix timestamp in the range `0..=MAX_TIMESTAMP`.
1088         ///
1089         /// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
1090         pub fn from_unix_timestamp(unix_seconds: u64) -> Result<Self, CreationError> {
1091                 if unix_seconds <= MAX_TIMESTAMP {
1092                         Ok(Self(Duration::from_secs(unix_seconds)))
1093                 } else {
1094                         Err(CreationError::TimestampOutOfBounds)
1095                 }
1096         }
1097
1098         /// Creates a `PositiveTimestamp` from a [`SystemTime`] with a corresponding Unix timestamp in
1099         /// the range `0..=MAX_TIMESTAMP`.
1100         ///
1101         /// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices.
1102         ///
1103         /// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
1104         #[cfg(feature = "std")]
1105         pub fn from_system_time(time: SystemTime) -> Result<Self, CreationError> {
1106                 time.duration_since(SystemTime::UNIX_EPOCH)
1107                         .map(Self::from_duration_since_epoch)
1108                         .unwrap_or(Err(CreationError::TimestampOutOfBounds))
1109         }
1110
1111         /// Creates a `PositiveTimestamp` from a [`Duration`] since the Unix epoch in the range
1112         /// `0..=MAX_TIMESTAMP`.
1113         ///
1114         /// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices.
1115         ///
1116         /// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
1117         pub fn from_duration_since_epoch(duration: Duration) -> Result<Self, CreationError> {
1118                 Self::from_unix_timestamp(duration.as_secs())
1119         }
1120
1121         /// Returns the Unix timestamp representing the stored time
1122         pub fn as_unix_timestamp(&self) -> u64 {
1123                 self.0.as_secs()
1124         }
1125
1126         /// Returns the duration of the stored time since the Unix epoch
1127         pub fn as_duration_since_epoch(&self) -> Duration {
1128                 self.0
1129         }
1130
1131         /// Returns the [`SystemTime`] representing the stored time
1132         #[cfg(feature = "std")]
1133         pub fn as_time(&self) -> SystemTime {
1134                 SystemTime::UNIX_EPOCH + self.0
1135         }
1136 }
1137
1138 #[cfg(feature = "std")]
1139 impl From<PositiveTimestamp> for SystemTime {
1140         fn from(val: PositiveTimestamp) -> Self {
1141                 SystemTime::UNIX_EPOCH + val.0
1142         }
1143 }
1144
1145 impl Bolt11Invoice {
1146         /// The hash of the [`RawInvoice`] that was signed.
1147         pub fn signable_hash(&self) -> [u8; 32] {
1148                 self.signed_invoice.hash
1149         }
1150
1151         /// Transform the `Bolt11Invoice` into its unchecked version.
1152         pub fn into_signed_raw(self) -> SignedRawInvoice {
1153                 self.signed_invoice
1154         }
1155
1156         /// Check that all mandatory fields are present
1157         fn check_field_counts(&self) -> Result<(), SemanticError> {
1158                 // "A writer MUST include exactly one p field […]."
1159                 let payment_hash_cnt = self.tagged_fields().filter(|&tf| match *tf {
1160                         TaggedField::PaymentHash(_) => true,
1161                         _ => false,
1162                 }).count();
1163                 if payment_hash_cnt < 1 {
1164                         return Err(SemanticError::NoPaymentHash);
1165                 } else if payment_hash_cnt > 1 {
1166                         return Err(SemanticError::MultiplePaymentHashes);
1167                 }
1168
1169                 // "A writer MUST include either exactly one d or exactly one h field."
1170                 let description_cnt = self.tagged_fields().filter(|&tf| match *tf {
1171                         TaggedField::Description(_) | TaggedField::DescriptionHash(_) => true,
1172                         _ => false,
1173                 }).count();
1174                 if  description_cnt < 1 {
1175                         return Err(SemanticError::NoDescription);
1176                 } else if description_cnt > 1 {
1177                         return  Err(SemanticError::MultipleDescriptions);
1178                 }
1179
1180                 self.check_payment_secret()?;
1181
1182                 Ok(())
1183         }
1184
1185         /// Checks that there is exactly one payment secret field
1186         fn check_payment_secret(&self) -> Result<(), SemanticError> {
1187                 // "A writer MUST include exactly one `s` field."
1188                 let payment_secret_count = self.tagged_fields().filter(|&tf| match *tf {
1189                         TaggedField::PaymentSecret(_) => true,
1190                         _ => false,
1191                 }).count();
1192                 if payment_secret_count < 1 {
1193                         return Err(SemanticError::NoPaymentSecret);
1194                 } else if payment_secret_count > 1 {
1195                         return Err(SemanticError::MultiplePaymentSecrets);
1196                 }
1197
1198                 Ok(())
1199         }
1200
1201         /// Check that amount is a whole number of millisatoshis
1202         fn check_amount(&self) -> Result<(), SemanticError> {
1203                 if let Some(amount_pico_btc) = self.amount_pico_btc() {
1204                         if amount_pico_btc % 10 != 0 {
1205                                 return Err(SemanticError::ImpreciseAmount);
1206                         }
1207                 }
1208                 Ok(())
1209         }
1210
1211         /// Check that feature bits are set as required
1212         fn check_feature_bits(&self) -> Result<(), SemanticError> {
1213                 self.check_payment_secret()?;
1214
1215                 // "A writer MUST set an s field if and only if the payment_secret feature is set."
1216                 // (this requirement has been since removed, and we now require the payment secret
1217                 // feature bit always).
1218                 let features = self.tagged_fields().find(|&tf| match *tf {
1219                         TaggedField::Features(_) => true,
1220                         _ => false,
1221                 });
1222                 match features {
1223                         None => Err(SemanticError::InvalidFeatures),
1224                         Some(TaggedField::Features(features)) => {
1225                                 if features.requires_unknown_bits() {
1226                                         Err(SemanticError::InvalidFeatures)
1227                                 } else if !features.supports_payment_secret() {
1228                                         Err(SemanticError::InvalidFeatures)
1229                                 } else {
1230                                         Ok(())
1231                                 }
1232                         },
1233                         Some(_) => unreachable!(),
1234                 }
1235         }
1236
1237         /// Check that the invoice is signed correctly and that key recovery works
1238         pub fn check_signature(&self) -> Result<(), SemanticError> {
1239                 match self.signed_invoice.recover_payee_pub_key() {
1240                         Err(secp256k1::Error::InvalidRecoveryId) =>
1241                                 return Err(SemanticError::InvalidRecoveryId),
1242                         Err(secp256k1::Error::InvalidSignature) =>
1243                                 return Err(SemanticError::InvalidSignature),
1244                         Err(e) => panic!("no other error may occur, got {:?}", e),
1245                         Ok(_) => {},
1246                 }
1247
1248                 if !self.signed_invoice.check_signature() {
1249                         return Err(SemanticError::InvalidSignature);
1250                 }
1251
1252                 Ok(())
1253         }
1254
1255         /// Constructs a `Bolt11Invoice` from a [`SignedRawInvoice`] by checking all its invariants.
1256         /// ```
1257         /// use lightning_invoice::*;
1258         ///
1259         /// let invoice = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\
1260         /// h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\
1261         /// 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\
1262         /// h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\
1263         /// j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\
1264         /// ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\
1265         /// guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\
1266         /// ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\
1267         /// p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\
1268         /// 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\
1269         /// j5r6drg6k6zcqj0fcwg";
1270         ///
1271         /// let signed = invoice.parse::<SignedRawInvoice>().unwrap();
1272         ///
1273         /// assert!(Bolt11Invoice::from_signed(signed).is_ok());
1274         /// ```
1275         pub fn from_signed(signed_invoice: SignedRawInvoice) -> Result<Self, SemanticError> {
1276                 let invoice = Bolt11Invoice {
1277                         signed_invoice,
1278                 };
1279                 invoice.check_field_counts()?;
1280                 invoice.check_feature_bits()?;
1281                 invoice.check_signature()?;
1282                 invoice.check_amount()?;
1283
1284                 Ok(invoice)
1285         }
1286
1287         /// Returns the `Bolt11Invoice`'s timestamp (should equal its creation time)
1288         #[cfg(feature = "std")]
1289         pub fn timestamp(&self) -> SystemTime {
1290                 self.signed_invoice.raw_invoice().data.timestamp.as_time()
1291         }
1292
1293         /// Returns the `Bolt11Invoice`'s timestamp as a duration since the Unix epoch
1294         pub fn duration_since_epoch(&self) -> Duration {
1295                 self.signed_invoice.raw_invoice().data.timestamp.0
1296         }
1297
1298         /// Returns an iterator over all tagged fields of this `Bolt11Invoice`.
1299         ///
1300         /// This is not exported to bindings users as there is not yet a manual mapping for a FilterMap
1301         pub fn tagged_fields(&self)
1302                 -> FilterMap<Iter<RawTaggedField>, fn(&RawTaggedField) -> Option<&TaggedField>> {
1303                 self.signed_invoice.raw_invoice().known_tagged_fields()
1304         }
1305
1306         /// Returns the hash to which we will receive the preimage on completion of the payment
1307         pub fn payment_hash(&self) -> &sha256::Hash {
1308                 &self.signed_invoice.payment_hash().expect("checked by constructor").0
1309         }
1310
1311         /// Return the description or a hash of it for longer ones
1312         ///
1313         /// This is not exported to bindings users because we don't yet export InvoiceDescription
1314         pub fn description(&self) -> InvoiceDescription {
1315                 if let Some(direct) = self.signed_invoice.description() {
1316                         return InvoiceDescription::Direct(direct);
1317                 } else if let Some(hash) = self.signed_invoice.description_hash() {
1318                         return InvoiceDescription::Hash(hash);
1319                 }
1320                 unreachable!("ensured by constructor");
1321         }
1322
1323         /// Get the payee's public key if one was included in the invoice
1324         pub fn payee_pub_key(&self) -> Option<&PublicKey> {
1325                 self.signed_invoice.payee_pub_key().map(|x| &x.0)
1326         }
1327
1328         /// Get the payment secret if one was included in the invoice
1329         pub fn payment_secret(&self) -> &PaymentSecret {
1330                 self.signed_invoice.payment_secret().expect("was checked by constructor")
1331         }
1332
1333         /// Get the payment metadata blob if one was included in the invoice
1334         pub fn payment_metadata(&self) -> Option<&Vec<u8>> {
1335                 self.signed_invoice.payment_metadata()
1336         }
1337
1338         /// Get the invoice features if they were included in the invoice
1339         pub fn features(&self) -> Option<&InvoiceFeatures> {
1340                 self.signed_invoice.features()
1341         }
1342
1343         /// Recover the payee's public key (only to be used if none was included in the invoice)
1344         pub fn recover_payee_pub_key(&self) -> PublicKey {
1345                 self.signed_invoice.recover_payee_pub_key().expect("was checked by constructor").0
1346         }
1347
1348         /// Returns the Duration since the Unix epoch at which the invoice expires.
1349         /// Returning None if overflow occurred.
1350         pub fn expires_at(&self) -> Option<Duration> {
1351                 self.duration_since_epoch().checked_add(self.expiry_time())
1352         }
1353
1354         /// Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
1355         pub fn expiry_time(&self) -> Duration {
1356                 self.signed_invoice.expiry_time()
1357                         .map(|x| x.0)
1358                         .unwrap_or(Duration::from_secs(DEFAULT_EXPIRY_TIME))
1359         }
1360
1361         /// Returns whether the invoice has expired.
1362         #[cfg(feature = "std")]
1363         pub fn is_expired(&self) -> bool {
1364                 Self::is_expired_from_epoch(&self.timestamp(), self.expiry_time())
1365         }
1366
1367         /// Returns whether the expiry time from the given epoch has passed.
1368         #[cfg(feature = "std")]
1369         pub(crate) fn is_expired_from_epoch(epoch: &SystemTime, expiry_time: Duration) -> bool {
1370                 match epoch.elapsed() {
1371                         Ok(elapsed) => elapsed > expiry_time,
1372                         Err(_) => false,
1373                 }
1374         }
1375
1376         /// Returns the Duration remaining until the invoice expires.
1377         #[cfg(feature = "std")]
1378         pub fn duration_until_expiry(&self) -> Duration {
1379                 SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
1380                         .map(|now| self.expiration_remaining_from_epoch(now))
1381                         .unwrap_or(Duration::from_nanos(0))
1382         }
1383
1384         /// Returns the Duration remaining until the invoice expires given the current time.
1385         /// `time` is the timestamp as a duration since the Unix epoch.
1386         pub fn expiration_remaining_from_epoch(&self, time: Duration) -> Duration {
1387                 self.expires_at().map(|x| x.checked_sub(time)).flatten().unwrap_or(Duration::from_nanos(0))
1388         }
1389
1390         /// Returns whether the expiry time would pass at the given point in time.
1391         /// `at_time` is the timestamp as a duration since the Unix epoch.
1392         pub fn would_expire(&self, at_time: Duration) -> bool {
1393                 self.duration_since_epoch()
1394                         .checked_add(self.expiry_time())
1395                         .unwrap_or_else(|| Duration::new(u64::max_value(), 1_000_000_000 - 1)) < at_time
1396         }
1397
1398         /// Returns the invoice's `min_final_cltv_expiry_delta` time, if present, otherwise
1399         /// [`DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA`].
1400         pub fn min_final_cltv_expiry_delta(&self) -> u64 {
1401                 self.signed_invoice.min_final_cltv_expiry_delta()
1402                         .map(|x| x.0)
1403                         .unwrap_or(DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA)
1404         }
1405
1406         /// Returns a list of all fallback addresses
1407         ///
1408         /// This is not exported to bindings users as we don't support Vec<&NonOpaqueType>
1409         pub fn fallbacks(&self) -> Vec<&Fallback> {
1410                 self.signed_invoice.fallbacks()
1411         }
1412
1413         /// Returns a list of all fallback addresses as [`Address`]es
1414         pub fn fallback_addresses(&self) -> Vec<Address> {
1415                 self.fallbacks().iter().map(|fallback| {
1416                         let payload = match fallback {
1417                                 Fallback::SegWitProgram { version, program } => {
1418                                         Payload::WitnessProgram { version: *version, program: program.to_vec() }
1419                                 }
1420                                 Fallback::PubKeyHash(pkh) => {
1421                                         Payload::PubkeyHash(*pkh)
1422                                 }
1423                                 Fallback::ScriptHash(sh) => {
1424                                         Payload::ScriptHash(*sh)
1425                                 }
1426                         };
1427
1428                         Address { payload, network: self.network() }
1429                 }).collect()
1430         }
1431
1432         /// Returns a list of all routes included in the invoice
1433         pub fn private_routes(&self) -> Vec<&PrivateRoute> {
1434                 self.signed_invoice.private_routes()
1435         }
1436
1437         /// Returns a list of all routes included in the invoice as the underlying hints
1438         pub fn route_hints(&self) -> Vec<RouteHint> {
1439                 find_all_extract!(
1440                         self.signed_invoice.known_tagged_fields(), TaggedField::PrivateRoute(ref x), x
1441                 ).map(|route| (**route).clone()).collect()
1442         }
1443
1444         /// Returns the currency for which the invoice was issued
1445         pub fn currency(&self) -> Currency {
1446                 self.signed_invoice.currency()
1447         }
1448
1449         /// Returns the network for which the invoice was issued
1450         ///
1451         /// This is not exported to bindings users, see [`Self::currency`] instead.
1452         pub fn network(&self) -> Network {
1453                 self.signed_invoice.currency().into()
1454         }
1455
1456         /// Returns the amount if specified in the invoice as millisatoshis.
1457         pub fn amount_milli_satoshis(&self) -> Option<u64> {
1458                 self.signed_invoice.amount_pico_btc().map(|v| v / 10)
1459         }
1460
1461         /// Returns the amount if specified in the invoice as pico BTC.
1462         fn amount_pico_btc(&self) -> Option<u64> {
1463                 self.signed_invoice.amount_pico_btc()
1464         }
1465 }
1466
1467 impl From<TaggedField> for RawTaggedField {
1468         fn from(tf: TaggedField) -> Self {
1469                 RawTaggedField::KnownSemantics(tf)
1470         }
1471 }
1472
1473 impl TaggedField {
1474         /// Numeric representation of the field's tag
1475         pub fn tag(&self) -> u5 {
1476                 let tag = match *self {
1477                         TaggedField::PaymentHash(_) => constants::TAG_PAYMENT_HASH,
1478                         TaggedField::Description(_) => constants::TAG_DESCRIPTION,
1479                         TaggedField::PayeePubKey(_) => constants::TAG_PAYEE_PUB_KEY,
1480                         TaggedField::DescriptionHash(_) => constants::TAG_DESCRIPTION_HASH,
1481                         TaggedField::ExpiryTime(_) => constants::TAG_EXPIRY_TIME,
1482                         TaggedField::MinFinalCltvExpiryDelta(_) => constants::TAG_MIN_FINAL_CLTV_EXPIRY_DELTA,
1483                         TaggedField::Fallback(_) => constants::TAG_FALLBACK,
1484                         TaggedField::PrivateRoute(_) => constants::TAG_PRIVATE_ROUTE,
1485                         TaggedField::PaymentSecret(_) => constants::TAG_PAYMENT_SECRET,
1486                         TaggedField::PaymentMetadata(_) => constants::TAG_PAYMENT_METADATA,
1487                         TaggedField::Features(_) => constants::TAG_FEATURES,
1488                 };
1489
1490                 u5::try_from_u8(tag).expect("all tags defined are <32")
1491         }
1492 }
1493
1494 impl Description {
1495
1496         /// Creates a new `Description` if `description` is at most 1023 __bytes__ long,
1497         /// returns [`CreationError::DescriptionTooLong`] otherwise
1498         ///
1499         /// Please note that single characters may use more than one byte due to UTF8 encoding.
1500         pub fn new(description: String) -> Result<Description, CreationError> {
1501                 if description.len() > 639 {
1502                         Err(CreationError::DescriptionTooLong)
1503                 } else {
1504                         Ok(Description(description))
1505                 }
1506         }
1507
1508         /// Returns the underlying description [`String`]
1509         pub fn into_inner(self) -> String {
1510                 self.0
1511         }
1512 }
1513
1514 impl From<Description> for String {
1515         fn from(val: Description) -> Self {
1516                 val.into_inner()
1517         }
1518 }
1519
1520 impl Deref for Description {
1521         type Target = str;
1522
1523         fn deref(&self) -> &str {
1524                 &self.0
1525         }
1526 }
1527
1528 impl From<PublicKey> for PayeePubKey {
1529         fn from(pk: PublicKey) -> Self {
1530                 PayeePubKey(pk)
1531         }
1532 }
1533
1534 impl Deref for PayeePubKey {
1535         type Target = PublicKey;
1536
1537         fn deref(&self) -> &PublicKey {
1538                 &self.0
1539         }
1540 }
1541
1542 impl ExpiryTime {
1543         /// Construct an `ExpiryTime` from seconds.
1544         pub fn from_seconds(seconds: u64) -> ExpiryTime {
1545                 ExpiryTime(Duration::from_secs(seconds))
1546         }
1547
1548         /// Construct an `ExpiryTime` from a [`Duration`], dropping the sub-second part.
1549         pub fn from_duration(duration: Duration) -> ExpiryTime {
1550                 Self::from_seconds(duration.as_secs())
1551         }
1552
1553         /// Returns the expiry time in seconds
1554         pub fn as_seconds(&self) -> u64 {
1555                 self.0.as_secs()
1556         }
1557
1558         /// Returns a reference to the underlying [`Duration`] (=expiry time)
1559         pub fn as_duration(&self) -> &Duration {
1560                 &self.0
1561         }
1562 }
1563
1564 impl PrivateRoute {
1565         /// Creates a new (partial) route from a list of hops
1566         pub fn new(hops: RouteHint) -> Result<PrivateRoute, CreationError> {
1567                 if hops.0.len() <= 12 {
1568                         Ok(PrivateRoute(hops))
1569                 } else {
1570                         Err(CreationError::RouteTooLong)
1571                 }
1572         }
1573
1574         /// Returns the underlying list of hops
1575         pub fn into_inner(self) -> RouteHint {
1576                 self.0
1577         }
1578 }
1579
1580 impl From<PrivateRoute> for RouteHint {
1581         fn from(val: PrivateRoute) -> Self {
1582                 val.into_inner()
1583         }
1584 }
1585
1586 impl Deref for PrivateRoute {
1587         type Target = RouteHint;
1588
1589         fn deref(&self) -> &RouteHint {
1590                 &self.0
1591         }
1592 }
1593
1594 impl Deref for InvoiceSignature {
1595         type Target = RecoverableSignature;
1596
1597         fn deref(&self) -> &RecoverableSignature {
1598                 &self.0
1599         }
1600 }
1601
1602 impl Deref for SignedRawInvoice {
1603         type Target = RawInvoice;
1604
1605         fn deref(&self) -> &RawInvoice {
1606                 &self.raw_invoice
1607         }
1608 }
1609
1610 /// Errors that may occur when constructing a new [`RawInvoice`] or [`Bolt11Invoice`]
1611 #[derive(Eq, PartialEq, Debug, Clone)]
1612 pub enum CreationError {
1613         /// The supplied description string was longer than 639 __bytes__ (see [`Description::new`])
1614         DescriptionTooLong,
1615
1616         /// The specified route has too many hops and can't be encoded
1617         RouteTooLong,
1618
1619         /// The Unix timestamp of the supplied date is less than zero or greater than 35-bits
1620         TimestampOutOfBounds,
1621
1622         /// The supplied millisatoshi amount was greater than the total bitcoin supply.
1623         InvalidAmount,
1624
1625         /// Route hints were required for this invoice and were missing. Applies to
1626         /// [phantom invoices].
1627         ///
1628         /// [phantom invoices]: crate::utils::create_phantom_invoice
1629         MissingRouteHints,
1630
1631         /// The provided `min_final_cltv_expiry_delta` was less than [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
1632         ///
1633         /// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA
1634         MinFinalCltvExpiryDeltaTooShort,
1635 }
1636
1637 impl Display for CreationError {
1638         fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1639                 match self {
1640                         CreationError::DescriptionTooLong => f.write_str("The supplied description string was longer than 639 bytes"),
1641                         CreationError::RouteTooLong => f.write_str("The specified route has too many hops and can't be encoded"),
1642                         CreationError::TimestampOutOfBounds => f.write_str("The Unix timestamp of the supplied date is less than zero or greater than 35-bits"),
1643                         CreationError::InvalidAmount => f.write_str("The supplied millisatoshi amount was greater than the total bitcoin supply"),
1644                         CreationError::MissingRouteHints => f.write_str("The invoice required route hints and they weren't provided"),
1645                         CreationError::MinFinalCltvExpiryDeltaTooShort => f.write_str(
1646                                 "The supplied final CLTV expiry delta was less than LDK's `MIN_FINAL_CLTV_EXPIRY_DELTA`"),
1647                 }
1648         }
1649 }
1650
1651 #[cfg(feature = "std")]
1652 impl std::error::Error for CreationError { }
1653
1654 /// Errors that may occur when converting a [`RawInvoice`] to a [`Bolt11Invoice`]. They relate to
1655 /// the requirements sections in BOLT #11
1656 #[derive(Eq, PartialEq, Debug, Clone)]
1657 pub enum SemanticError {
1658         /// The invoice is missing the mandatory payment hash
1659         NoPaymentHash,
1660
1661         /// The invoice has multiple payment hashes which isn't allowed
1662         MultiplePaymentHashes,
1663
1664         /// No description or description hash are part of the invoice
1665         NoDescription,
1666
1667         /// The invoice contains multiple descriptions and/or description hashes which isn't allowed
1668         MultipleDescriptions,
1669
1670         /// The invoice is missing the mandatory payment secret, which all modern lightning nodes
1671         /// should provide.
1672         NoPaymentSecret,
1673
1674         /// The invoice contains multiple payment secrets
1675         MultiplePaymentSecrets,
1676
1677         /// The invoice's features are invalid
1678         InvalidFeatures,
1679
1680         /// The recovery id doesn't fit the signature/pub key
1681         InvalidRecoveryId,
1682
1683         /// The invoice's signature is invalid
1684         InvalidSignature,
1685
1686         /// The invoice's amount was not a whole number of millisatoshis
1687         ImpreciseAmount,
1688 }
1689
1690 impl Display for SemanticError {
1691         fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1692                 match self {
1693                         SemanticError::NoPaymentHash => f.write_str("The invoice is missing the mandatory payment hash"),
1694                         SemanticError::MultiplePaymentHashes => f.write_str("The invoice has multiple payment hashes which isn't allowed"),
1695                         SemanticError::NoDescription => f.write_str("No description or description hash are part of the invoice"),
1696                         SemanticError::MultipleDescriptions => f.write_str("The invoice contains multiple descriptions and/or description hashes which isn't allowed"),
1697                         SemanticError::NoPaymentSecret => f.write_str("The invoice is missing the mandatory payment secret"),
1698                         SemanticError::MultiplePaymentSecrets => f.write_str("The invoice contains multiple payment secrets"),
1699                         SemanticError::InvalidFeatures => f.write_str("The invoice's features are invalid"),
1700                         SemanticError::InvalidRecoveryId => f.write_str("The recovery id doesn't fit the signature/pub key"),
1701                         SemanticError::InvalidSignature => f.write_str("The invoice's signature is invalid"),
1702                         SemanticError::ImpreciseAmount => f.write_str("The invoice's amount was not a whole number of millisatoshis"),
1703                 }
1704         }
1705 }
1706
1707 #[cfg(feature = "std")]
1708 impl std::error::Error for SemanticError { }
1709
1710 /// When signing using a fallible method either an user-supplied `SignError` or a [`CreationError`]
1711 /// may occur.
1712 #[derive(Eq, PartialEq, Debug, Clone)]
1713 pub enum SignOrCreationError<S = ()> {
1714         /// An error occurred during signing
1715         SignError(S),
1716
1717         /// An error occurred while building the transaction
1718         CreationError(CreationError),
1719 }
1720
1721 impl<S> Display for SignOrCreationError<S> {
1722         fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1723                 match self {
1724                         SignOrCreationError::SignError(_) => f.write_str("An error occurred during signing"),
1725                         SignOrCreationError::CreationError(err) => err.fmt(f),
1726                 }
1727         }
1728 }
1729
1730 #[cfg(feature = "serde")]
1731 impl Serialize for Bolt11Invoice {
1732         fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
1733                 serializer.serialize_str(self.to_string().as_str())
1734         }
1735 }
1736 #[cfg(feature = "serde")]
1737 impl<'de> Deserialize<'de> for Bolt11Invoice {
1738         fn deserialize<D>(deserializer: D) -> Result<Bolt11Invoice, D::Error> where D: Deserializer<'de> {
1739                 let bolt11 = String::deserialize(deserializer)?
1740                         .parse::<Bolt11Invoice>()
1741                         .map_err(|e| D::Error::custom(format_args!("{:?}", e)))?;
1742
1743                 Ok(bolt11)
1744         }
1745 }
1746
1747 #[cfg(test)]
1748 mod test {
1749         use bitcoin::Script;
1750         use bitcoin_hashes::hex::FromHex;
1751         use bitcoin_hashes::sha256;
1752
1753         #[test]
1754         fn test_system_time_bounds_assumptions() {
1755                 assert_eq!(
1756                         crate::PositiveTimestamp::from_unix_timestamp(crate::MAX_TIMESTAMP + 1),
1757                         Err(crate::CreationError::TimestampOutOfBounds)
1758                 );
1759         }
1760
1761         #[test]
1762         fn test_calc_invoice_hash() {
1763                 use crate::{RawInvoice, RawHrp, RawDataPart, Currency, PositiveTimestamp};
1764                 use crate::TaggedField::*;
1765
1766                 let invoice = RawInvoice {
1767                         hrp: RawHrp {
1768                                 currency: Currency::Bitcoin,
1769                                 raw_amount: None,
1770                                 si_prefix: None,
1771                         },
1772                         data: RawDataPart {
1773                                 timestamp: PositiveTimestamp::from_unix_timestamp(1496314658).unwrap(),
1774                                 tagged_fields: vec![
1775                                         PaymentHash(crate::Sha256(sha256::Hash::from_hex(
1776                                                 "0001020304050607080900010203040506070809000102030405060708090102"
1777                                         ).unwrap())).into(),
1778                                         Description(crate::Description::new(
1779                                                 "Please consider supporting this project".to_owned()
1780                                         ).unwrap()).into(),
1781                                 ],
1782                         },
1783                 };
1784
1785                 let expected_hash = [
1786                         0xc3, 0xd4, 0xe8, 0x3f, 0x64, 0x6f, 0xa7, 0x9a, 0x39, 0x3d, 0x75, 0x27, 0x7b, 0x1d,
1787                         0x85, 0x8d, 0xb1, 0xd1, 0xf7, 0xab, 0x71, 0x37, 0xdc, 0xb7, 0x83, 0x5d, 0xb2, 0xec,
1788                         0xd5, 0x18, 0xe1, 0xc9
1789                 ];
1790
1791                 assert_eq!(invoice.signable_hash(), expected_hash)
1792         }
1793
1794         #[test]
1795         fn test_check_signature() {
1796                 use crate::TaggedField::*;
1797                 use secp256k1::Secp256k1;
1798                 use secp256k1::ecdsa::{RecoveryId, RecoverableSignature};
1799                 use secp256k1::{SecretKey, PublicKey};
1800                 use crate::{SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
1801                          PositiveTimestamp};
1802
1803                 let invoice = SignedRawInvoice {
1804                         raw_invoice: RawInvoice {
1805                                 hrp: RawHrp {
1806                                         currency: Currency::Bitcoin,
1807                                         raw_amount: None,
1808                                         si_prefix: None,
1809                                 },
1810                                 data: RawDataPart {
1811                                         timestamp: PositiveTimestamp::from_unix_timestamp(1496314658).unwrap(),
1812                                         tagged_fields: vec ! [
1813                                                 PaymentHash(Sha256(sha256::Hash::from_hex(
1814                                                         "0001020304050607080900010203040506070809000102030405060708090102"
1815                                                 ).unwrap())).into(),
1816                                                 Description(
1817                                                         crate::Description::new(
1818                                                                 "Please consider supporting this project".to_owned()
1819                                                         ).unwrap()
1820                                                 ).into(),
1821                                         ],
1822                                 },
1823                         },
1824                         hash: [
1825                                 0xc3, 0xd4, 0xe8, 0x3f, 0x64, 0x6f, 0xa7, 0x9a, 0x39, 0x3d, 0x75, 0x27,
1826                                 0x7b, 0x1d, 0x85, 0x8d, 0xb1, 0xd1, 0xf7, 0xab, 0x71, 0x37, 0xdc, 0xb7,
1827                                 0x83, 0x5d, 0xb2, 0xec, 0xd5, 0x18, 0xe1, 0xc9
1828                         ],
1829                         signature: InvoiceSignature(RecoverableSignature::from_compact(
1830                                 & [
1831                                         0x38u8, 0xec, 0x68, 0x91, 0x34, 0x5e, 0x20, 0x41, 0x45, 0xbe, 0x8a,
1832                                         0x3a, 0x99, 0xde, 0x38, 0xe9, 0x8a, 0x39, 0xd6, 0xa5, 0x69, 0x43,
1833                                         0x4e, 0x18, 0x45, 0xc8, 0xaf, 0x72, 0x05, 0xaf, 0xcf, 0xcc, 0x7f,
1834                                         0x42, 0x5f, 0xcd, 0x14, 0x63, 0xe9, 0x3c, 0x32, 0x88, 0x1e, 0xad,
1835                                         0x0d, 0x6e, 0x35, 0x6d, 0x46, 0x7e, 0xc8, 0xc0, 0x25, 0x53, 0xf9,
1836                                         0xaa, 0xb1, 0x5e, 0x57, 0x38, 0xb1, 0x1f, 0x12, 0x7f
1837                                 ],
1838                                 RecoveryId::from_i32(0).unwrap()
1839                         ).unwrap()),
1840                 };
1841
1842                 assert!(invoice.check_signature());
1843
1844                 let private_key = SecretKey::from_slice(
1845                         &[
1846                                 0xe1, 0x26, 0xf6, 0x8f, 0x7e, 0xaf, 0xcc, 0x8b, 0x74, 0xf5, 0x4d, 0x26, 0x9f, 0xe2,
1847                                 0x06, 0xbe, 0x71, 0x50, 0x00, 0xf9, 0x4d, 0xac, 0x06, 0x7d, 0x1c, 0x04, 0xa8, 0xca,
1848                                 0x3b, 0x2d, 0xb7, 0x34
1849                         ][..]
1850                 ).unwrap();
1851                 let public_key = PublicKey::from_secret_key(&Secp256k1::new(), &private_key);
1852
1853                 assert_eq!(invoice.recover_payee_pub_key(), Ok(crate::PayeePubKey(public_key)));
1854
1855                 let (raw_invoice, _, _) = invoice.into_parts();
1856                 let new_signed = raw_invoice.sign::<_, ()>(|hash| {
1857                         Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key))
1858                 }).unwrap();
1859
1860                 assert!(new_signed.check_signature());
1861         }
1862
1863         #[test]
1864         fn test_check_feature_bits() {
1865                 use crate::TaggedField::*;
1866                 use lightning::ln::features::InvoiceFeatures;
1867                 use secp256k1::Secp256k1;
1868                 use secp256k1::SecretKey;
1869                 use crate::{Bolt11Invoice, RawInvoice, RawHrp, RawDataPart, Currency, Sha256, PositiveTimestamp, 
1870                          SemanticError};
1871
1872                 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
1873                 let payment_secret = lightning::ln::PaymentSecret([21; 32]);
1874                 let invoice_template = RawInvoice {
1875                         hrp: RawHrp {
1876                                 currency: Currency::Bitcoin,
1877                                 raw_amount: None,
1878                                 si_prefix: None,
1879                         },
1880                         data: RawDataPart {
1881                                 timestamp: PositiveTimestamp::from_unix_timestamp(1496314658).unwrap(),
1882                                 tagged_fields: vec ! [
1883                                         PaymentHash(Sha256(sha256::Hash::from_hex(
1884                                                 "0001020304050607080900010203040506070809000102030405060708090102"
1885                                         ).unwrap())).into(),
1886                                         Description(
1887                                                 crate::Description::new(
1888                                                         "Please consider supporting this project".to_owned()
1889                                                 ).unwrap()
1890                                         ).into(),
1891                                 ],
1892                         },
1893                 };
1894
1895                 // Missing features
1896                 let invoice = {
1897                         let mut invoice = invoice_template.clone();
1898                         invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
1899                         invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
1900                 }.unwrap();
1901                 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::InvalidFeatures));
1902
1903                 // Missing feature bits
1904                 let invoice = {
1905                         let mut invoice = invoice_template.clone();
1906                         invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
1907                         invoice.data.tagged_fields.push(Features(InvoiceFeatures::empty()).into());
1908                         invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
1909                 }.unwrap();
1910                 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::InvalidFeatures));
1911
1912                 let mut payment_secret_features = InvoiceFeatures::empty();
1913                 payment_secret_features.set_payment_secret_required();
1914
1915                 // Including payment secret and feature bits
1916                 let invoice = {
1917                         let mut invoice = invoice_template.clone();
1918                         invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
1919                         invoice.data.tagged_fields.push(Features(payment_secret_features.clone()).into());
1920                         invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
1921                 }.unwrap();
1922                 assert!(Bolt11Invoice::from_signed(invoice).is_ok());
1923
1924                 // No payment secret or features
1925                 let invoice = {
1926                         let invoice = invoice_template.clone();
1927                         invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
1928                 }.unwrap();
1929                 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret));
1930
1931                 // No payment secret or feature bits
1932                 let invoice = {
1933                         let mut invoice = invoice_template.clone();
1934                         invoice.data.tagged_fields.push(Features(InvoiceFeatures::empty()).into());
1935                         invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
1936                 }.unwrap();
1937                 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret));
1938
1939                 // Missing payment secret
1940                 let invoice = {
1941                         let mut invoice = invoice_template.clone();
1942                         invoice.data.tagged_fields.push(Features(payment_secret_features).into());
1943                         invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
1944                 }.unwrap();
1945                 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret));
1946
1947                 // Multiple payment secrets
1948                 let invoice = {
1949                         let mut invoice = invoice_template;
1950                         invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
1951                         invoice.data.tagged_fields.push(PaymentSecret(payment_secret).into());
1952                         invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
1953                 }.unwrap();
1954                 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::MultiplePaymentSecrets));
1955         }
1956
1957         #[test]
1958         fn test_builder_amount() {
1959                 use crate::*;
1960
1961                 let builder = InvoiceBuilder::new(Currency::Bitcoin)
1962                         .description("Test".into())
1963                         .payment_hash(sha256::Hash::from_slice(&[0;32][..]).unwrap())
1964                         .duration_since_epoch(Duration::from_secs(1234567));
1965
1966                 let invoice = builder.clone()
1967                         .amount_milli_satoshis(1500)
1968                         .build_raw()
1969                         .unwrap();
1970
1971                 assert_eq!(invoice.hrp.si_prefix, Some(SiPrefix::Nano));
1972                 assert_eq!(invoice.hrp.raw_amount, Some(15));
1973
1974
1975                 let invoice = builder
1976                         .amount_milli_satoshis(150)
1977                         .build_raw()
1978                         .unwrap();
1979
1980                 assert_eq!(invoice.hrp.si_prefix, Some(SiPrefix::Pico));
1981                 assert_eq!(invoice.hrp.raw_amount, Some(1500));
1982         }
1983
1984         #[test]
1985         fn test_builder_fail() {
1986                 use crate::*;
1987                 use lightning::routing::router::RouteHintHop;
1988                 use std::iter::FromIterator;
1989                 use secp256k1::PublicKey;
1990
1991                 let builder = InvoiceBuilder::new(Currency::Bitcoin)
1992                         .payment_hash(sha256::Hash::from_slice(&[0;32][..]).unwrap())
1993                         .duration_since_epoch(Duration::from_secs(1234567))
1994                         .min_final_cltv_expiry_delta(144);
1995
1996                 let too_long_string = String::from_iter(
1997                         (0..1024).map(|_| '?')
1998                 );
1999
2000                 let long_desc_res = builder.clone()
2001                         .description(too_long_string)
2002                         .build_raw();
2003                 assert_eq!(long_desc_res, Err(CreationError::DescriptionTooLong));
2004
2005                 let route_hop = RouteHintHop {
2006                         src_node_id: PublicKey::from_slice(
2007                                         &[
2008                                                 0x03, 0x9e, 0x03, 0xa9, 0x01, 0xb8, 0x55, 0x34, 0xff, 0x1e, 0x92, 0xc4,
2009                                                 0x3c, 0x74, 0x43, 0x1f, 0x7c, 0xe7, 0x20, 0x46, 0x06, 0x0f, 0xcf, 0x7a,
2010                                                 0x95, 0xc3, 0x7e, 0x14, 0x8f, 0x78, 0xc7, 0x72, 0x55
2011                                         ][..]
2012                                 ).unwrap(),
2013                         short_channel_id: 0,
2014                         fees: RoutingFees {
2015                                 base_msat: 0,
2016                                 proportional_millionths: 0,
2017                         },
2018                         cltv_expiry_delta: 0,
2019                         htlc_minimum_msat: None,
2020                         htlc_maximum_msat: None,
2021                 };
2022                 let too_long_route = RouteHint(vec![route_hop; 13]);
2023                 let long_route_res = builder.clone()
2024                         .description("Test".into())
2025                         .private_route(too_long_route)
2026                         .build_raw();
2027                 assert_eq!(long_route_res, Err(CreationError::RouteTooLong));
2028
2029                 let sign_error_res = builder
2030                         .description("Test".into())
2031                         .payment_secret(PaymentSecret([0; 32]))
2032                         .try_build_signed(|_| {
2033                                 Err("ImaginaryError")
2034                         });
2035                 assert_eq!(sign_error_res, Err(SignOrCreationError::SignError("ImaginaryError")));
2036         }
2037
2038         #[test]
2039         fn test_builder_ok() {
2040                 use crate::*;
2041                 use lightning::routing::router::RouteHintHop;
2042                 use secp256k1::Secp256k1;
2043                 use secp256k1::{SecretKey, PublicKey};
2044                 use std::time::{UNIX_EPOCH, Duration};
2045
2046                 let secp_ctx = Secp256k1::new();
2047
2048                 let private_key = SecretKey::from_slice(
2049                         &[
2050                                 0xe1, 0x26, 0xf6, 0x8f, 0x7e, 0xaf, 0xcc, 0x8b, 0x74, 0xf5, 0x4d, 0x26, 0x9f, 0xe2,
2051                                 0x06, 0xbe, 0x71, 0x50, 0x00, 0xf9, 0x4d, 0xac, 0x06, 0x7d, 0x1c, 0x04, 0xa8, 0xca,
2052                                 0x3b, 0x2d, 0xb7, 0x34
2053                         ][..]
2054                 ).unwrap();
2055                 let public_key = PublicKey::from_secret_key(&secp_ctx, &private_key);
2056
2057                 let route_1 = RouteHint(vec![
2058                         RouteHintHop {
2059                                 src_node_id: public_key,
2060                                 short_channel_id: de::parse_int_be(&[123; 8], 256).expect("short chan ID slice too big?"),
2061                                 fees: RoutingFees {
2062                                         base_msat: 2,
2063                                         proportional_millionths: 1,
2064                                 },
2065                                 cltv_expiry_delta: 145,
2066                                 htlc_minimum_msat: None,
2067                                 htlc_maximum_msat: None,
2068                         },
2069                         RouteHintHop {
2070                                 src_node_id: public_key,
2071                                 short_channel_id: de::parse_int_be(&[42; 8], 256).expect("short chan ID slice too big?"),
2072                                 fees: RoutingFees {
2073                                         base_msat: 3,
2074                                         proportional_millionths: 2,
2075                                 },
2076                                 cltv_expiry_delta: 146,
2077                                 htlc_minimum_msat: None,
2078                                 htlc_maximum_msat: None,
2079                         }
2080                 ]);
2081
2082                 let route_2 = RouteHint(vec![
2083                         RouteHintHop {
2084                                 src_node_id: public_key,
2085                                 short_channel_id: 0,
2086                                 fees: RoutingFees {
2087                                         base_msat: 4,
2088                                         proportional_millionths: 3,
2089                                 },
2090                                 cltv_expiry_delta: 147,
2091                                 htlc_minimum_msat: None,
2092                                 htlc_maximum_msat: None,
2093                         },
2094                         RouteHintHop {
2095                                 src_node_id: public_key,
2096                                 short_channel_id: de::parse_int_be(&[1; 8], 256).expect("short chan ID slice too big?"),
2097                                 fees: RoutingFees {
2098                                         base_msat: 5,
2099                                         proportional_millionths: 4,
2100                                 },
2101                                 cltv_expiry_delta: 148,
2102                                 htlc_minimum_msat: None,
2103                                 htlc_maximum_msat: None,
2104                         }
2105                 ]);
2106
2107                 let builder = InvoiceBuilder::new(Currency::BitcoinTestnet)
2108                         .amount_milli_satoshis(123)
2109                         .duration_since_epoch(Duration::from_secs(1234567))
2110                         .payee_pub_key(public_key)
2111                         .expiry_time(Duration::from_secs(54321))
2112                         .min_final_cltv_expiry_delta(144)
2113                         .fallback(Fallback::PubKeyHash(PubkeyHash::from_slice(&[0;20]).unwrap()))
2114                         .private_route(route_1.clone())
2115                         .private_route(route_2.clone())
2116                         .description_hash(sha256::Hash::from_slice(&[3;32][..]).unwrap())
2117                         .payment_hash(sha256::Hash::from_slice(&[21;32][..]).unwrap())
2118                         .payment_secret(PaymentSecret([42; 32]))
2119                         .basic_mpp();
2120
2121                 let invoice = builder.clone().build_signed(|hash| {
2122                         secp_ctx.sign_ecdsa_recoverable(hash, &private_key)
2123                 }).unwrap();
2124
2125                 assert!(invoice.check_signature().is_ok());
2126                 assert_eq!(invoice.tagged_fields().count(), 10);
2127
2128                 assert_eq!(invoice.amount_milli_satoshis(), Some(123));
2129                 assert_eq!(invoice.amount_pico_btc(), Some(1230));
2130                 assert_eq!(invoice.currency(), Currency::BitcoinTestnet);
2131                 #[cfg(feature = "std")]
2132                 assert_eq!(
2133                         invoice.timestamp().duration_since(UNIX_EPOCH).unwrap().as_secs(),
2134                         1234567
2135                 );
2136                 assert_eq!(invoice.payee_pub_key(), Some(&public_key));
2137                 assert_eq!(invoice.expiry_time(), Duration::from_secs(54321));
2138                 assert_eq!(invoice.min_final_cltv_expiry_delta(), 144);
2139                 assert_eq!(invoice.fallbacks(), vec![&Fallback::PubKeyHash(PubkeyHash::from_slice(&[0;20]).unwrap())]);
2140                 let address = Address::from_script(&Script::new_p2pkh(&PubkeyHash::from_slice(&[0;20]).unwrap()), Network::Testnet).unwrap();
2141                 assert_eq!(invoice.fallback_addresses(), vec![address]);
2142                 assert_eq!(invoice.private_routes(), vec![&PrivateRoute(route_1), &PrivateRoute(route_2)]);
2143                 assert_eq!(
2144                         invoice.description(),
2145                         InvoiceDescription::Hash(&Sha256(sha256::Hash::from_slice(&[3;32][..]).unwrap()))
2146                 );
2147                 assert_eq!(invoice.payment_hash(), &sha256::Hash::from_slice(&[21;32][..]).unwrap());
2148                 assert_eq!(invoice.payment_secret(), &PaymentSecret([42; 32]));
2149
2150                 let mut expected_features = InvoiceFeatures::empty();
2151                 expected_features.set_variable_length_onion_required();
2152                 expected_features.set_payment_secret_required();
2153                 expected_features.set_basic_mpp_optional();
2154                 assert_eq!(invoice.features(), Some(&expected_features));
2155
2156                 let raw_invoice = builder.build_raw().unwrap();
2157                 assert_eq!(raw_invoice, *invoice.into_signed_raw().raw_invoice())
2158         }
2159
2160         #[test]
2161         fn test_default_values() {
2162                 use crate::*;
2163                 use secp256k1::Secp256k1;
2164                 use secp256k1::SecretKey;
2165
2166                 let signed_invoice = InvoiceBuilder::new(Currency::Bitcoin)
2167                         .description("Test".into())
2168                         .payment_hash(sha256::Hash::from_slice(&[0;32][..]).unwrap())
2169                         .payment_secret(PaymentSecret([0; 32]))
2170                         .duration_since_epoch(Duration::from_secs(1234567))
2171                         .build_raw()
2172                         .unwrap()
2173                         .sign::<_, ()>(|hash| {
2174                                 let privkey = SecretKey::from_slice(&[41; 32]).unwrap();
2175                                 let secp_ctx = Secp256k1::new();
2176                                 Ok(secp_ctx.sign_ecdsa_recoverable(hash, &privkey))
2177                         })
2178                         .unwrap();
2179                 let invoice = Bolt11Invoice::from_signed(signed_invoice).unwrap();
2180
2181                 assert_eq!(invoice.min_final_cltv_expiry_delta(), DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA);
2182                 assert_eq!(invoice.expiry_time(), Duration::from_secs(DEFAULT_EXPIRY_TIME));
2183                 assert!(!invoice.would_expire(Duration::from_secs(1234568)));
2184         }
2185
2186         #[test]
2187         fn test_expiration() {
2188                 use crate::*;
2189                 use secp256k1::Secp256k1;
2190                 use secp256k1::SecretKey;
2191
2192                 let signed_invoice = InvoiceBuilder::new(Currency::Bitcoin)
2193                         .description("Test".into())
2194                         .payment_hash(sha256::Hash::from_slice(&[0;32][..]).unwrap())
2195                         .payment_secret(PaymentSecret([0; 32]))
2196                         .duration_since_epoch(Duration::from_secs(1234567))
2197                         .build_raw()
2198                         .unwrap()
2199                         .sign::<_, ()>(|hash| {
2200                                 let privkey = SecretKey::from_slice(&[41; 32]).unwrap();
2201                                 let secp_ctx = Secp256k1::new();
2202                                 Ok(secp_ctx.sign_ecdsa_recoverable(hash, &privkey))
2203                         })
2204                         .unwrap();
2205                 let invoice = Bolt11Invoice::from_signed(signed_invoice).unwrap();
2206
2207                 assert!(invoice.would_expire(Duration::from_secs(1234567 + DEFAULT_EXPIRY_TIME + 1)));
2208         }
2209
2210         #[cfg(feature = "serde")]
2211         #[test]
2212         fn test_serde() {
2213                 let invoice_str = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\
2214                         h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\
2215                         5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\
2216                         h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\
2217                         j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\
2218                         ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\
2219                         guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\
2220                         ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\
2221                         p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\
2222                         8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\
2223                         j5r6drg6k6zcqj0fcwg";
2224                 let invoice = invoice_str.parse::<super::Bolt11Invoice>().unwrap();
2225                 let serialized_invoice = serde_json::to_string(&invoice).unwrap();
2226                 let deserialized_invoice: super::Bolt11Invoice = serde_json::from_str(serialized_invoice.as_str()).unwrap();
2227                 assert_eq!(invoice, deserialized_invoice);
2228                 assert_eq!(invoice_str, deserialized_invoice.to_string().as_str());
2229                 assert_eq!(invoice_str, serialized_invoice.as_str().trim_matches('\"'));
2230         }
2231 }