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)]
6 #![deny(non_upper_case_globals)]
7 #![deny(non_camel_case_types)]
8 #![deny(non_snake_case)]
11 #![cfg_attr(docsrs, feature(doc_auto_cfg))]
13 #![cfg_attr(feature = "strict", deny(warnings))]
14 #![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
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:
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
25 //! [`Bolt11Invoice::from_str`]: crate::Bolt11Invoice#impl-FromStr
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");
33 pub(crate) mod time_utils;
36 extern crate bitcoin_hashes;
37 #[macro_use] extern crate lightning;
38 extern crate num_traits;
39 extern crate secp256k1;
41 #[cfg(any(test, feature = "std"))]
43 #[cfg(feature = "serde")]
46 #[cfg(feature = "std")]
47 use std::time::SystemTime;
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;
60 use secp256k1::PublicKey;
61 use secp256k1::{Message, Secp256k1};
62 use secp256k1::ecdsa::RecoverableSignature;
64 use core::cmp::Ordering;
65 use core::fmt::{Display, Formatter, self};
66 use core::iter::FilterMap;
67 use core::num::ParseIntError;
69 use core::slice::Iter;
70 use core::time::Duration;
73 #[cfg(feature = "serde")]
74 use serde::{Deserialize, Deserializer,Serialize, Serializer, de::Error};
81 #[cfg(feature = "hashbrown")]
82 extern crate hashbrown;
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};
90 pub use alloc::string::ToString;
93 use crate::prelude::*;
95 /// Sync compat for std/no_std
96 #[cfg(feature = "std")]
98 pub use ::std::sync::{Mutex, MutexGuard};
101 /// Sync compat for std/no_std
102 #[cfg(not(feature = "std"))]
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),
118 UnexpectedEndOfTaggedFields,
119 DescriptionDecodeError(str::Utf8Error),
121 IntegerOverflowError,
122 InvalidSegWitProgramLength,
123 InvalidPubKeyHashLength,
124 InvalidScriptHashLength,
126 InvalidSliceLength(String),
128 /// Not an error, but used internally to signal that a part of the invoice should be ignored
129 /// according to BOLT11
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),
141 /// The invoice could be decoded but violates the BOLT11 standard
142 SemanticError(crate::SemanticError),
145 /// The number of bits used to represent timestamps as defined in BOLT 11.
146 const TIMESTAMP_BITS: usize = 35;
148 /// The maximum timestamp as [`Duration::as_secs`] since the Unix epoch allowed by [`BOLT 11`].
150 /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
151 pub const MAX_TIMESTAMP: u64 = (1 << TIMESTAMP_BITS) - 1;
153 /// Default expiry time as defined by [BOLT 11].
155 /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md
156 pub const DEFAULT_EXPIRY_TIME: u64 = 3600;
158 /// Default minimum final CLTV expiry as defined by [BOLT 11].
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`].
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;
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.
171 /// extern crate secp256k1;
172 /// extern crate lightning;
173 /// extern crate lightning_invoice;
174 /// extern crate bitcoin_hashes;
176 /// use bitcoin_hashes::Hash;
177 /// use bitcoin_hashes::sha256;
179 /// use secp256k1::Secp256k1;
180 /// use secp256k1::SecretKey;
182 /// use lightning::ln::PaymentSecret;
184 /// use lightning_invoice::{Currency, InvoiceBuilder};
186 /// # #[cfg(not(feature = "std"))]
188 /// # #[cfg(feature = "std")]
190 /// let private_key = SecretKey::from_slice(
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
198 /// let payment_hash = sha256::Hash::from_slice(&[0; 32][..]).unwrap();
199 /// let payment_secret = PaymentSecret([42u8; 32]);
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)
212 /// assert!(invoice.to_string().starts_with("lnbc1"));
216 /// # Type parameters
217 /// The two parameters `D` and `H` signal if the builder already contains the correct amount of the
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
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> {
231 si_prefix: Option<SiPrefix>,
232 timestamp: Option<PositiveTimestamp>,
233 tagged_fields: Vec<TaggedField>,
234 error: Option<CreationError>,
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>,
244 /// Represents a syntactically and semantically correct lightning BOLT11 invoice.
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`])
251 /// [`Bolt11Invoice::from_str`]: crate::Bolt11Invoice#impl-FromStr
252 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
253 pub struct Bolt11Invoice {
254 signed_invoice: SignedRawBolt11Invoice,
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.
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),
267 /// Reference to the description's hash included in the invoice
271 /// Represents a signed [`RawBolt11Invoice`] with cached hash. The signature is not checked and may be
275 /// The hash has to be either from the deserialized invoice or from the serialized [`RawBolt11Invoice`].
276 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
277 pub struct SignedRawBolt11Invoice {
278 /// The raw invoice that the signature belongs to
279 raw_invoice: RawBolt11Invoice,
281 /// Hash of the [`RawBolt11Invoice`] that will be used to check the signature.
283 /// * if the `SignedRawBolt11Invoice` 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 `SignedRawBolt11Invoice` was constructed manually the hash will be the calculated hash
287 /// from the [`RawBolt11Invoice`]
290 /// signature of the payment request
291 signature: InvoiceSignature,
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.
298 /// For methods without docs see the corresponding methods in [`Bolt11Invoice`].
299 #[derive(Eq, PartialEq, Debug, Clone, Hash, Ord, PartialOrd)]
300 pub struct RawBolt11Invoice {
301 /// human readable part
305 pub data: RawDataPart,
308 /// Data of the [`RawBolt11Invoice`] that is encoded in the human readable part.
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)]
313 /// The currency deferred from the 3rd and 4th character of the bech32 transaction
314 pub currency: Currency,
316 /// The amount that, multiplied by the SI prefix, has to be payed
317 pub raw_amount: Option<u64>,
319 /// SI prefix that gets multiplied with the `raw_amount`
320 pub si_prefix: Option<SiPrefix>,
323 /// Data of the [`RawBolt11Invoice`] 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,
329 /// tagged fields of the payment request
330 pub tagged_fields: Vec<RawTaggedField>,
333 /// A timestamp that refers to a date after 1 January 1970.
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);
342 /// SI prefixes for the human readable part
343 #[derive(Eq, PartialEq, Debug, Clone, Copy, Hash, Ord, PartialOrd)]
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 {
360 SiPrefix::Milli => 1_000_000_000,
361 SiPrefix::Micro => 1_000_000,
362 SiPrefix::Nano => 1_000,
367 /// Returns all enum variants of `SiPrefix` sorted in descending order of their associated
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];
379 /// Enum representing the crypto currencies (or networks) supported by this library
380 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
398 impl From<Network> for Currency {
399 fn from(network: Network) -> Self {
401 Network::Bitcoin => Currency::Bitcoin,
402 Network::Testnet => Currency::BitcoinTestnet,
403 Network::Regtest => Currency::Regtest,
404 Network::Signet => Currency::Signet,
409 impl From<Currency> for Network {
410 fn from(currency: Currency) -> Self {
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,
421 /// Tagged field which may have an unknown tag
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>),
432 /// Tagged field with known tag
434 /// For descriptions of the enum values please refer to the enclosed type's docs.
436 /// This is not exported to bindings users as we don't yet support enum variants with the same name the struct contained
438 #[allow(missing_docs)]
439 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
440 pub enum TaggedField {
442 Description(Description),
443 PayeePubKey(PayeePubKey),
444 DescriptionHash(Sha256),
445 ExpiryTime(ExpiryTime),
446 MinFinalCltvExpiryDelta(MinFinalCltvExpiryDelta),
448 PrivateRoute(PrivateRoute),
449 PaymentSecret(PaymentSecret),
450 PaymentMetadata(Vec<u8>),
451 Features(InvoiceFeatures),
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
460 /// Constructs a new [`Sha256`] from the given bytes, which are assumed to be the output of a
461 /// single sha256 hash.
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"))
468 /// Description string
471 /// The description can be at most 639 __bytes__ long
472 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
473 pub struct Description(String);
476 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
477 pub struct PayeePubKey(pub PublicKey);
479 /// Positive duration that defines when (relatively to the timestamp) in the future the invoice
481 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
482 pub struct ExpiryTime(Duration);
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);
488 /// Fallback address in case no LN payment is possible
489 #[allow(missing_docs)]
490 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
493 version: WitnessVersion,
496 PubKeyHash(PubkeyHash),
497 ScriptHash(ScriptHash),
500 /// Recoverable signature
501 #[derive(Clone, Debug, Hash, Eq, PartialEq)]
502 pub struct InvoiceSignature(pub RecoverableSignature);
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)
510 impl Ord for InvoiceSignature {
511 fn cmp(&self, other: &Self) -> Ordering {
512 self.0.serialize_compact().1.cmp(&other.0.serialize_compact().1)
516 /// Private routing information
519 /// The encoded route has to be <1024 5bit characters long (<=639 bytes or <=12 hops)
521 #[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
522 pub struct PrivateRoute(RouteHint);
524 /// Tag constants as specified in BOLT11
525 #[allow(missing_docs)]
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;
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 {
549 tagged_fields: Vec::new(),
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,
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,
568 si_prefix: self.si_prefix,
569 timestamp: self.timestamp,
570 tagged_fields: self.tagged_fields,
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,
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()
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);
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)));
600 /// Sets the expiry time, dropping the subsecond part (which is not representable in BOLT 11
602 pub fn expiry_time(mut self, expiry_time: Duration) -> Self {
603 self.tagged_fields.push(TaggedField::ExpiryTime(ExpiryTime::from_duration(expiry_time)));
607 /// Adds a fallback address.
608 pub fn fallback(mut self, fallback: Fallback) -> Self {
609 self.tagged_fields.push(TaggedField::Fallback(fallback));
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),
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 [`RawBolt11Invoice`] if no [`CreationError`] occurred while construction any of the
626 pub fn build_raw(self) -> Result<RawBolt11Invoice, CreationError> {
628 // If an error occurred at any time before, return it now
629 if let Some(e) = self.error {
634 currency: self.currency,
635 raw_amount: self.amount,
636 si_prefix: self.si_prefix,
639 let timestamp = self.timestamp.expect("ensured to be Some(t) by type T");
641 let tagged_fields = self.tagged_fields.into_iter().map(|tf| {
642 RawTaggedField::KnownSemantics(tf)
643 }).collect::<Vec<_>>();
645 let data = RawDataPart {
650 Ok(RawBolt11Invoice {
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),
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)));
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> {
676 InvoiceDescription::Direct(desc) => {
677 self.description(desc.clone().into_inner())
679 InvoiceDescription::Hash(hash) => {
680 self.description_hash(hash.0)
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)));
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),
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),
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"));
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)));
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();
745 self.tagged_fields.push(TaggedField::PaymentSecret(payment_secret));
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));
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.
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();
773 let mut features = InvoiceFeatures::empty();
774 features.set_payment_metadata_optional();
775 self.tagged_fields.push(TaggedField::Features(features));
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();
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();
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
813 let invoice = self.try_build_signed::<_, ()>(|hash| {
814 Ok(sign_function(hash))
819 Err(SignOrCreationError::CreationError(e)) => Err(e),
820 Err(SignOrCreationError::SignError(())) => unreachable!(),
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>
830 let raw = match self.build_raw() {
832 Err(e) => return Err(SignOrCreationError::CreationError(e)),
835 let signed = match raw.sign(sign_function) {
837 Err(e) => return Err(SignOrCreationError::SignError(e)),
840 let invoice = Bolt11Invoice {
841 signed_invoice: signed,
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");
853 impl SignedRawBolt11Invoice {
854 /// Disassembles the `SignedRawBolt11Invoice` into its three parts:
856 /// 2. hash of the raw invoice
858 pub fn into_parts(self) -> (RawBolt11Invoice, [u8; 32], InvoiceSignature) {
859 (self.raw_invoice, self.hash, self.signature)
862 /// The [`RawBolt11Invoice`] which was signed.
863 pub fn raw_invoice(&self) -> &RawBolt11Invoice {
867 /// The hash of the [`RawBolt11Invoice`] that was signed.
868 pub fn signable_hash(&self) -> &[u8; 32] {
872 /// Signature for the invoice.
873 pub fn signature(&self) -> &InvoiceSignature {
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");
882 Ok(PayeePubKey(Secp256k1::new().recover_ecdsa(
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();
893 let mut recovered_pub_key = Option::None;
894 if recovered_pub_key.is_none() {
895 let recovered = match self.recover_payee_pub_key() {
897 Err(_) => return false,
899 recovered_pub_key = Some(recovered);
902 let pub_key = included_pub_key.or(recovered_pub_key.as_ref())
903 .expect("One is always present");
905 let hash = Message::from_slice(&self.hash[..])
906 .expect("Hash is 32 bytes long, same as MESSAGE_SIZE");
908 let secp_context = Secp256k1::new();
909 let verification_result = secp_context.verify_ecdsa(
911 &self.signature.to_standard(),
915 match verification_result {
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.
925 /// The following example would extract the first B.
933 /// let elements = vec![Enum::A(1), Enum::A(2), Enum::B(3), Enum::A(4)];
935 /// assert_eq!(find_extract!(elements.iter(), Enum::B(x), x), Some(3u16));
937 macro_rules! find_extract {
938 ($iter:expr, $enm:pat, $enm_var:ident) => {
939 find_all_extract!($iter, $enm, $enm_var).next()
943 /// Finds the all elements of an enum stream of a given variant and extracts one member of the
944 /// variant through an iterator.
946 /// The following example would extract all A.
954 /// let elements = vec![Enum::A(1), Enum::A(2), Enum::B(3), Enum::A(4)];
957 /// find_all_extract!(elements.iter(), Enum::A(x), x).collect::<Vec<u8>>(),
958 /// vec![1u8, 2u8, 4u8]
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),
970 #[allow(missing_docs)]
971 impl RawBolt11Invoice {
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)[..]);
980 /// Calculate the hash of the encoded `RawBolt11Invoice` which should be signed.
981 pub fn signable_hash(&self) -> [u8; 32] {
982 use bech32::ToBase32;
984 RawBolt11Invoice::hash_from_parts(
985 self.hrp.to_string().as_bytes(),
986 &self.data.to_base32()
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 [`SignedRawBolt11Invoice`] is not required to be valid there
992 /// are no constraints regarding the validity of the produced signature.
994 /// This is not exported to bindings users as we don't currently support passing function pointers into methods
996 pub fn sign<F, E>(self, sign_method: F) -> Result<SignedRawBolt11Invoice, E>
997 where F: FnOnce(&Message) -> Result<RecoverableSignature, E>
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)?;
1004 Ok(SignedRawBolt11Invoice {
1007 signature: InvoiceSignature(signature),
1011 /// Returns an iterator over all tagged fields with known semantics.
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>>
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> {
1022 RawTaggedField::KnownSemantics(ref tf) => Some(tf),
1027 self.data.tagged_fields.iter().filter_map(match_raw )
1030 pub fn payment_hash(&self) -> Option<&Sha256> {
1031 find_extract!(self.known_tagged_fields(), TaggedField::PaymentHash(ref x), x)
1034 pub fn description(&self) -> Option<&Description> {
1035 find_extract!(self.known_tagged_fields(), TaggedField::Description(ref x), x)
1038 pub fn payee_pub_key(&self) -> Option<&PayeePubKey> {
1039 find_extract!(self.known_tagged_fields(), TaggedField::PayeePubKey(ref x), x)
1042 pub fn description_hash(&self) -> Option<&Sha256> {
1043 find_extract!(self.known_tagged_fields(), TaggedField::DescriptionHash(ref x), x)
1046 pub fn expiry_time(&self) -> Option<&ExpiryTime> {
1047 find_extract!(self.known_tagged_fields(), TaggedField::ExpiryTime(ref x), x)
1050 pub fn min_final_cltv_expiry_delta(&self) -> Option<&MinFinalCltvExpiryDelta> {
1051 find_extract!(self.known_tagged_fields(), TaggedField::MinFinalCltvExpiryDelta(ref x), x)
1054 pub fn payment_secret(&self) -> Option<&PaymentSecret> {
1055 find_extract!(self.known_tagged_fields(), TaggedField::PaymentSecret(ref x), x)
1058 pub fn payment_metadata(&self) -> Option<&Vec<u8>> {
1059 find_extract!(self.known_tagged_fields(), TaggedField::PaymentMetadata(ref x), x)
1062 pub fn features(&self) -> Option<&InvoiceFeatures> {
1063 find_extract!(self.known_tagged_fields(), TaggedField::Features(ref x), x)
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()
1071 pub fn private_routes(&self) -> Vec<&PrivateRoute> {
1072 find_all_extract!(self.known_tagged_fields(), TaggedField::PrivateRoute(ref x), x).collect()
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() })
1081 pub fn currency(&self) -> Currency {
1082 self.hrp.currency.clone()
1086 impl PositiveTimestamp {
1087 /// Creates a `PositiveTimestamp` from a Unix timestamp in the range `0..=MAX_TIMESTAMP`.
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)))
1094 Err(CreationError::TimestampOutOfBounds)
1098 /// Creates a `PositiveTimestamp` from a [`SystemTime`] with a corresponding Unix timestamp in
1099 /// the range `0..=MAX_TIMESTAMP`.
1101 /// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices.
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))
1111 /// Creates a `PositiveTimestamp` from a [`Duration`] since the Unix epoch in the range
1112 /// `0..=MAX_TIMESTAMP`.
1114 /// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices.
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())
1121 /// Returns the Unix timestamp representing the stored time
1122 pub fn as_unix_timestamp(&self) -> u64 {
1126 /// Returns the duration of the stored time since the Unix epoch
1127 pub fn as_duration_since_epoch(&self) -> Duration {
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
1138 #[cfg(feature = "std")]
1139 impl From<PositiveTimestamp> for SystemTime {
1140 fn from(val: PositiveTimestamp) -> Self {
1141 SystemTime::UNIX_EPOCH + val.0
1145 impl Bolt11Invoice {
1146 /// The hash of the [`RawBolt11Invoice`] that was signed.
1147 pub fn signable_hash(&self) -> [u8; 32] {
1148 self.signed_invoice.hash
1151 /// Transform the `Bolt11Invoice` into its unchecked version.
1152 pub fn into_signed_raw(self) -> SignedRawBolt11Invoice {
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,
1163 if payment_hash_cnt < 1 {
1164 return Err(SemanticError::NoPaymentHash);
1165 } else if payment_hash_cnt > 1 {
1166 return Err(SemanticError::MultiplePaymentHashes);
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,
1174 if description_cnt < 1 {
1175 return Err(SemanticError::NoDescription);
1176 } else if description_cnt > 1 {
1177 return Err(SemanticError::MultipleDescriptions);
1180 self.check_payment_secret()?;
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,
1192 if payment_secret_count < 1 {
1193 return Err(SemanticError::NoPaymentSecret);
1194 } else if payment_secret_count > 1 {
1195 return Err(SemanticError::MultiplePaymentSecrets);
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);
1211 /// Check that feature bits are set as required
1212 fn check_feature_bits(&self) -> Result<(), SemanticError> {
1213 self.check_payment_secret()?;
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,
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)
1233 Some(_) => unreachable!(),
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),
1248 if !self.signed_invoice.check_signature() {
1249 return Err(SemanticError::InvalidSignature);
1255 /// Constructs a `Bolt11Invoice` from a [`SignedRawBolt11Invoice`] by checking all its invariants.
1257 /// use lightning_invoice::*;
1259 /// let invoice = "lnbc100p1psj9jhxdqud3jxktt5w46x7unfv9kz6mn0v3jsnp4q0d3p2sfluzdx45tqcs\
1260 /// h2pu5qc7lgq0xs578ngs6s0s68ua4h7cvspp5q6rmq35js88zp5dvwrv9m459tnk2zunwj5jalqtyxqulh0l\
1261 /// 5gflssp5nf55ny5gcrfl30xuhzj3nphgj27rstekmr9fw3ny5989s300gyus9qyysgqcqpcrzjqw2sxwe993\
1262 /// h5pcm4dxzpvttgza8zhkqxpgffcrf5v25nwpr3cmfg7z54kuqq8rgqqqqqqqq2qqqqq9qq9qrzjqd0ylaqcl\
1263 /// j9424x9m8h2vcukcgnm6s56xfgu3j78zyqzhgs4hlpzvznlugqq9vsqqqqqqqlgqqqqqeqq9qrzjqwldmj9d\
1264 /// ha74df76zhx6l9we0vjdquygcdt3kssupehe64g6yyp5yz5rhuqqwccqqyqqqqlgqqqqjcqq9qrzjqf9e58a\
1265 /// guqr0rcun0ajlvmzq3ek63cw2w282gv3z5uupmuwvgjtq2z55qsqqg6qqqyqqqrtnqqqzq3cqygrzjqvphms\
1266 /// ywntrrhqjcraumvc4y6r8v4z5v593trte429v4hredj7ms5z52usqq9ngqqqqqqqlgqqqqqqgq9qrzjq2v0v\
1267 /// p62g49p7569ev48cmulecsxe59lvaw3wlxm7r982zxa9zzj7z5l0cqqxusqqyqqqqlgqqqqqzsqygarl9fh3\
1268 /// 8s0gyuxjjgux34w75dnc6xp2l35j7es3jd4ugt3lu0xzre26yg5m7ke54n2d5sym4xcmxtl8238xxvw5h5h5\
1269 /// j5r6drg6k6zcqj0fcwg";
1271 /// let signed = invoice.parse::<SignedRawBolt11Invoice>().unwrap();
1273 /// assert!(Bolt11Invoice::from_signed(signed).is_ok());
1275 pub fn from_signed(signed_invoice: SignedRawBolt11Invoice) -> Result<Self, SemanticError> {
1276 let invoice = Bolt11Invoice {
1279 invoice.check_field_counts()?;
1280 invoice.check_feature_bits()?;
1281 invoice.check_signature()?;
1282 invoice.check_amount()?;
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()
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
1298 /// Returns an iterator over all tagged fields of this `Bolt11Invoice`.
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()
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
1311 /// Return the description or a hash of it for longer ones
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);
1320 unreachable!("ensured by constructor");
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)
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")
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()
1338 /// Get the invoice features if they were included in the invoice
1339 pub fn features(&self) -> Option<&InvoiceFeatures> {
1340 self.signed_invoice.features()
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
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())
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()
1358 .unwrap_or(Duration::from_secs(DEFAULT_EXPIRY_TIME))
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())
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,
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))
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))
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
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()
1403 .unwrap_or(DEFAULT_MIN_FINAL_CLTV_EXPIRY_DELTA)
1406 /// Returns a list of all fallback addresses
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()
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() }
1420 Fallback::PubKeyHash(pkh) => {
1421 Payload::PubkeyHash(*pkh)
1423 Fallback::ScriptHash(sh) => {
1424 Payload::ScriptHash(*sh)
1428 Address { payload, network: self.network() }
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()
1437 /// Returns a list of all routes included in the invoice as the underlying hints
1438 pub fn route_hints(&self) -> Vec<RouteHint> {
1440 self.signed_invoice.known_tagged_fields(), TaggedField::PrivateRoute(ref x), x
1441 ).map(|route| (**route).clone()).collect()
1444 /// Returns the currency for which the invoice was issued
1445 pub fn currency(&self) -> Currency {
1446 self.signed_invoice.currency()
1449 /// Returns the network for which the invoice was issued
1451 /// This is not exported to bindings users, see [`Self::currency`] instead.
1452 pub fn network(&self) -> Network {
1453 self.signed_invoice.currency().into()
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)
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()
1467 impl From<TaggedField> for RawTaggedField {
1468 fn from(tf: TaggedField) -> Self {
1469 RawTaggedField::KnownSemantics(tf)
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,
1490 u5::try_from_u8(tag).expect("all tags defined are <32")
1496 /// Creates a new `Description` if `description` is at most 1023 __bytes__ long,
1497 /// returns [`CreationError::DescriptionTooLong`] otherwise
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)
1504 Ok(Description(description))
1508 /// Returns the underlying description [`String`]
1509 pub fn into_inner(self) -> String {
1514 impl From<Description> for String {
1515 fn from(val: Description) -> Self {
1520 impl Deref for Description {
1523 fn deref(&self) -> &str {
1528 impl From<PublicKey> for PayeePubKey {
1529 fn from(pk: PublicKey) -> Self {
1534 impl Deref for PayeePubKey {
1535 type Target = PublicKey;
1537 fn deref(&self) -> &PublicKey {
1543 /// Construct an `ExpiryTime` from seconds.
1544 pub fn from_seconds(seconds: u64) -> ExpiryTime {
1545 ExpiryTime(Duration::from_secs(seconds))
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())
1553 /// Returns the expiry time in seconds
1554 pub fn as_seconds(&self) -> u64 {
1558 /// Returns a reference to the underlying [`Duration`] (=expiry time)
1559 pub fn as_duration(&self) -> &Duration {
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))
1570 Err(CreationError::RouteTooLong)
1574 /// Returns the underlying list of hops
1575 pub fn into_inner(self) -> RouteHint {
1580 impl From<PrivateRoute> for RouteHint {
1581 fn from(val: PrivateRoute) -> Self {
1586 impl Deref for PrivateRoute {
1587 type Target = RouteHint;
1589 fn deref(&self) -> &RouteHint {
1594 impl Deref for InvoiceSignature {
1595 type Target = RecoverableSignature;
1597 fn deref(&self) -> &RecoverableSignature {
1602 impl Deref for SignedRawBolt11Invoice {
1603 type Target = RawBolt11Invoice;
1605 fn deref(&self) -> &RawBolt11Invoice {
1610 /// Errors that may occur when constructing a new [`RawBolt11Invoice`] 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`])
1616 /// The specified route has too many hops and can't be encoded
1619 /// The Unix timestamp of the supplied date is less than zero or greater than 35-bits
1620 TimestampOutOfBounds,
1622 /// The supplied millisatoshi amount was greater than the total bitcoin supply.
1625 /// Route hints were required for this invoice and were missing. Applies to
1626 /// [phantom invoices].
1628 /// [phantom invoices]: crate::utils::create_phantom_invoice
1631 /// The provided `min_final_cltv_expiry_delta` was less than [`MIN_FINAL_CLTV_EXPIRY_DELTA`].
1633 /// [`MIN_FINAL_CLTV_EXPIRY_DELTA`]: lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA
1634 MinFinalCltvExpiryDeltaTooShort,
1637 impl Display for CreationError {
1638 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
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`"),
1651 #[cfg(feature = "std")]
1652 impl std::error::Error for CreationError { }
1654 /// Errors that may occur when converting a [`RawBolt11Invoice`] 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
1661 /// The invoice has multiple payment hashes which isn't allowed
1662 MultiplePaymentHashes,
1664 /// No description or description hash are part of the invoice
1667 /// The invoice contains multiple descriptions and/or description hashes which isn't allowed
1668 MultipleDescriptions,
1670 /// The invoice is missing the mandatory payment secret, which all modern lightning nodes
1674 /// The invoice contains multiple payment secrets
1675 MultiplePaymentSecrets,
1677 /// The invoice's features are invalid
1680 /// The recovery id doesn't fit the signature/pub key
1683 /// The invoice's signature is invalid
1686 /// The invoice's amount was not a whole number of millisatoshis
1690 impl Display for SemanticError {
1691 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
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"),
1707 #[cfg(feature = "std")]
1708 impl std::error::Error for SemanticError { }
1710 /// When signing using a fallible method either an user-supplied `SignError` or a [`CreationError`]
1712 #[derive(Eq, PartialEq, Debug, Clone)]
1713 pub enum SignOrCreationError<S = ()> {
1714 /// An error occurred during signing
1717 /// An error occurred while building the transaction
1718 CreationError(CreationError),
1721 impl<S> Display for SignOrCreationError<S> {
1722 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1724 SignOrCreationError::SignError(_) => f.write_str("An error occurred during signing"),
1725 SignOrCreationError::CreationError(err) => err.fmt(f),
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())
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)))?;
1749 use bitcoin::Script;
1750 use bitcoin_hashes::hex::FromHex;
1751 use bitcoin_hashes::sha256;
1754 fn test_system_time_bounds_assumptions() {
1756 crate::PositiveTimestamp::from_unix_timestamp(crate::MAX_TIMESTAMP + 1),
1757 Err(crate::CreationError::TimestampOutOfBounds)
1762 fn test_calc_invoice_hash() {
1763 use crate::{RawBolt11Invoice, RawHrp, RawDataPart, Currency, PositiveTimestamp};
1764 use crate::TaggedField::*;
1766 let invoice = RawBolt11Invoice {
1768 currency: Currency::Bitcoin,
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()
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
1791 assert_eq!(invoice.signable_hash(), expected_hash)
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::{SignedRawBolt11Invoice, InvoiceSignature, RawBolt11Invoice, RawHrp, RawDataPart, Currency, Sha256,
1803 let invoice = SignedRawBolt11Invoice {
1804 raw_invoice: RawBolt11Invoice {
1806 currency: Currency::Bitcoin,
1811 timestamp: PositiveTimestamp::from_unix_timestamp(1496314658).unwrap(),
1812 tagged_fields: vec ! [
1813 PaymentHash(Sha256(sha256::Hash::from_hex(
1814 "0001020304050607080900010203040506070809000102030405060708090102"
1815 ).unwrap())).into(),
1817 crate::Description::new(
1818 "Please consider supporting this project".to_owned()
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
1829 signature: InvoiceSignature(RecoverableSignature::from_compact(
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
1838 RecoveryId::from_i32(0).unwrap()
1842 assert!(invoice.check_signature());
1844 let private_key = SecretKey::from_slice(
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
1851 let public_key = PublicKey::from_secret_key(&Secp256k1::new(), &private_key);
1853 assert_eq!(invoice.recover_payee_pub_key(), Ok(crate::PayeePubKey(public_key)));
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))
1860 assert!(new_signed.check_signature());
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, RawBolt11Invoice, RawHrp, RawDataPart, Currency, Sha256, PositiveTimestamp,
1872 let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
1873 let payment_secret = lightning::ln::PaymentSecret([21; 32]);
1874 let invoice_template = RawBolt11Invoice {
1876 currency: Currency::Bitcoin,
1881 timestamp: PositiveTimestamp::from_unix_timestamp(1496314658).unwrap(),
1882 tagged_fields: vec ! [
1883 PaymentHash(Sha256(sha256::Hash::from_hex(
1884 "0001020304050607080900010203040506070809000102030405060708090102"
1885 ).unwrap())).into(),
1887 crate::Description::new(
1888 "Please consider supporting this project".to_owned()
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)))
1901 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::InvalidFeatures));
1903 // Missing feature bits
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)))
1910 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::InvalidFeatures));
1912 let mut payment_secret_features = InvoiceFeatures::empty();
1913 payment_secret_features.set_payment_secret_required();
1915 // Including payment secret and feature bits
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)))
1922 assert!(Bolt11Invoice::from_signed(invoice).is_ok());
1924 // No payment secret or features
1926 let invoice = invoice_template.clone();
1927 invoice.sign::<_, ()>(|hash| Ok(Secp256k1::new().sign_ecdsa_recoverable(hash, &private_key)))
1929 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret));
1931 // No payment secret or feature bits
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)))
1937 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret));
1939 // Missing payment secret
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)))
1945 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::NoPaymentSecret));
1947 // Multiple payment secrets
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)))
1954 assert_eq!(Bolt11Invoice::from_signed(invoice), Err(SemanticError::MultiplePaymentSecrets));
1958 fn test_builder_amount() {
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));
1966 let invoice = builder.clone()
1967 .amount_milli_satoshis(1500)
1971 assert_eq!(invoice.hrp.si_prefix, Some(SiPrefix::Nano));
1972 assert_eq!(invoice.hrp.raw_amount, Some(15));
1975 let invoice = builder
1976 .amount_milli_satoshis(150)
1980 assert_eq!(invoice.hrp.si_prefix, Some(SiPrefix::Pico));
1981 assert_eq!(invoice.hrp.raw_amount, Some(1500));
1985 fn test_builder_fail() {
1987 use lightning::routing::router::RouteHintHop;
1988 use std::iter::FromIterator;
1989 use secp256k1::PublicKey;
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);
1996 let too_long_string = String::from_iter(
1997 (0..1024).map(|_| '?')
2000 let long_desc_res = builder.clone()
2001 .description(too_long_string)
2003 assert_eq!(long_desc_res, Err(CreationError::DescriptionTooLong));
2005 let route_hop = RouteHintHop {
2006 src_node_id: PublicKey::from_slice(
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
2013 short_channel_id: 0,
2016 proportional_millionths: 0,
2018 cltv_expiry_delta: 0,
2019 htlc_minimum_msat: None,
2020 htlc_maximum_msat: None,
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)
2027 assert_eq!(long_route_res, Err(CreationError::RouteTooLong));
2029 let sign_error_res = builder
2030 .description("Test".into())
2031 .payment_secret(PaymentSecret([0; 32]))
2032 .try_build_signed(|_| {
2033 Err("ImaginaryError")
2035 assert_eq!(sign_error_res, Err(SignOrCreationError::SignError("ImaginaryError")));
2039 fn test_builder_ok() {
2041 use lightning::routing::router::RouteHintHop;
2042 use secp256k1::Secp256k1;
2043 use secp256k1::{SecretKey, PublicKey};
2044 use std::time::{UNIX_EPOCH, Duration};
2046 let secp_ctx = Secp256k1::new();
2048 let private_key = SecretKey::from_slice(
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
2055 let public_key = PublicKey::from_secret_key(&secp_ctx, &private_key);
2057 let route_1 = RouteHint(vec![
2059 src_node_id: public_key,
2060 short_channel_id: de::parse_int_be(&[123; 8], 256).expect("short chan ID slice too big?"),
2063 proportional_millionths: 1,
2065 cltv_expiry_delta: 145,
2066 htlc_minimum_msat: None,
2067 htlc_maximum_msat: None,
2070 src_node_id: public_key,
2071 short_channel_id: de::parse_int_be(&[42; 8], 256).expect("short chan ID slice too big?"),
2074 proportional_millionths: 2,
2076 cltv_expiry_delta: 146,
2077 htlc_minimum_msat: None,
2078 htlc_maximum_msat: None,
2082 let route_2 = RouteHint(vec![
2084 src_node_id: public_key,
2085 short_channel_id: 0,
2088 proportional_millionths: 3,
2090 cltv_expiry_delta: 147,
2091 htlc_minimum_msat: None,
2092 htlc_maximum_msat: None,
2095 src_node_id: public_key,
2096 short_channel_id: de::parse_int_be(&[1; 8], 256).expect("short chan ID slice too big?"),
2099 proportional_millionths: 4,
2101 cltv_expiry_delta: 148,
2102 htlc_minimum_msat: None,
2103 htlc_maximum_msat: None,
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]))
2121 let invoice = builder.clone().build_signed(|hash| {
2122 secp_ctx.sign_ecdsa_recoverable(hash, &private_key)
2125 assert!(invoice.check_signature().is_ok());
2126 assert_eq!(invoice.tagged_fields().count(), 10);
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")]
2133 invoice.timestamp().duration_since(UNIX_EPOCH).unwrap().as_secs(),
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)]);
2144 invoice.description(),
2145 InvoiceDescription::Hash(&Sha256(sha256::Hash::from_slice(&[3;32][..]).unwrap()))
2147 assert_eq!(invoice.payment_hash(), &sha256::Hash::from_slice(&[21;32][..]).unwrap());
2148 assert_eq!(invoice.payment_secret(), &PaymentSecret([42; 32]));
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));
2156 let raw_invoice = builder.build_raw().unwrap();
2157 assert_eq!(raw_invoice, *invoice.into_signed_raw().raw_invoice())
2161 fn test_default_values() {
2163 use secp256k1::Secp256k1;
2164 use secp256k1::SecretKey;
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))
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))
2179 let invoice = Bolt11Invoice::from_signed(signed_invoice).unwrap();
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)));
2187 fn test_expiration() {
2189 use secp256k1::Secp256k1;
2190 use secp256k1::SecretKey;
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))
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))
2205 let invoice = Bolt11Invoice::from_signed(signed_invoice).unwrap();
2207 assert!(invoice.would_expire(Duration::from_secs(1234567 + DEFAULT_EXPIRY_TIME + 1)));
2210 #[cfg(feature = "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('\"'));