From: Valentine Wallace Date: Thu, 9 May 2024 18:29:08 +0000 (-0400) Subject: Static invoice encoding and parsing X-Git-Tag: v0.0.124-beta~83^2~3 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=e3dea2c3c7099c63d569b8912b9633aa733be19a;p=rust-lightning Static invoice encoding and parsing Define an interface for BOLT 12 static invoice messages. The underlying format consists of the original bytes and the parsed contents. The bytes are later needed for serialization. This is because it must mirror all the offer TLV records, including unknown ones, which aren't represented in the contents. Invoices may be created from an offer. --- diff --git a/lightning/src/offers/mod.rs b/lightning/src/offers/mod.rs index eb9b18e18..b77eec161 100644 --- a/lightning/src/offers/mod.rs +++ b/lightning/src/offers/mod.rs @@ -24,5 +24,7 @@ pub mod parse; mod payer; pub mod refund; pub(crate) mod signer; +#[allow(unused)] +pub(crate) mod static_invoice; #[cfg(test)] pub(crate) mod test_utils; diff --git a/lightning/src/offers/parse.rs b/lightning/src/offers/parse.rs index 472e44f62..c48d745a9 100644 --- a/lightning/src/offers/parse.rs +++ b/lightning/src/offers/parse.rs @@ -189,6 +189,8 @@ pub enum Bolt12SemanticError { MissingCreationTime, /// An invoice payment hash was expected but was missing. MissingPaymentHash, + /// An invoice payment hash was provided but was not expected. + UnexpectedPaymentHash, /// A signature was expected but was missing. MissingSignature, } diff --git a/lightning/src/offers/static_invoice.rs b/lightning/src/offers/static_invoice.rs new file mode 100644 index 000000000..0cf5e9210 --- /dev/null +++ b/lightning/src/offers/static_invoice.rs @@ -0,0 +1,352 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +//! Data structures and encoding for static BOLT 12 invoices. + +use crate::blinded_path::BlindedPath; +use crate::io; +use crate::ln::features::{Bolt12InvoiceFeatures, OfferFeatures}; +use crate::ln::msgs::DecodeError; +use crate::offers::invoice::{ + check_invoice_signing_pubkey, construct_payment_paths, filter_fallbacks, BlindedPathIter, + BlindedPayInfo, BlindedPayInfoIter, FallbackAddress, InvoiceTlvStream, InvoiceTlvStreamRef, +}; +use crate::offers::invoice_macros::invoice_accessors_common; +use crate::offers::merkle::{self, SignatureTlvStream, TaggedHash}; +use crate::offers::offer::{Amount, OfferContents, OfferTlvStream, Quantity}; +use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage}; +use crate::util::ser::{ + HighZeroBytesDroppedBigSize, Iterable, SeekReadable, WithoutLength, Writeable, Writer, +}; +use crate::util::string::PrintableString; +use bitcoin::address::Address; +use bitcoin::blockdata::constants::ChainHash; +use bitcoin::secp256k1::schnorr::Signature; +use bitcoin::secp256k1::PublicKey; +use core::time::Duration; + +#[cfg(feature = "std")] +use crate::offers::invoice::is_expired; + +#[allow(unused_imports)] +use crate::prelude::*; + +/// Static invoices default to expiring after 2 weeks. +const DEFAULT_RELATIVE_EXPIRY: Duration = Duration::from_secs(3600 * 24 * 14); + +/// Tag for the hash function used when signing a [`StaticInvoice`]'s merkle root. +pub const SIGNATURE_TAG: &'static str = concat!("lightning", "static_invoice", "signature"); + +/// A `StaticInvoice` is a reusable payment request corresponding to an [`Offer`]. +/// +/// A static invoice may be sent in response to an [`InvoiceRequest`] and includes all the +/// information needed to pay the recipient. However, unlike [`Bolt12Invoice`]s, static invoices do +/// not provide proof-of-payment. Therefore, [`Bolt12Invoice`]s should be preferred when the +/// recipient is online to provide one. +/// +/// [`Offer`]: crate::offers::offer::Offer +/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest +/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice +#[derive(Clone, Debug)] +pub struct StaticInvoice { + bytes: Vec, + contents: InvoiceContents, + signature: Signature, +} + +/// The contents of a [`StaticInvoice`] for responding to an [`Offer`]. +/// +/// [`Offer`]: crate::offers::offer::Offer +#[derive(Clone, Debug)] +struct InvoiceContents { + offer: OfferContents, + payment_paths: Vec<(BlindedPayInfo, BlindedPath)>, + created_at: Duration, + relative_expiry: Option, + fallbacks: Option>, + features: Bolt12InvoiceFeatures, + signing_pubkey: PublicKey, + message_paths: Vec, +} + +macro_rules! invoice_accessors { ($self: ident, $contents: expr) => { + /// The chain that must be used when paying the invoice. [`StaticInvoice`]s currently can only be + /// created from offers that support a single chain. + pub fn chain(&$self) -> ChainHash { + $contents.chain() + } + + /// Opaque bytes set by the originating [`Offer::metadata`]. + /// + /// [`Offer::metadata`]: crate::offers::offer::Offer::metadata + pub fn metadata(&$self) -> Option<&Vec> { + $contents.metadata() + } + + /// The minimum amount required for a successful payment of a single item. + /// + /// From [`Offer::amount`]. + /// + /// [`Offer::amount`]: crate::offers::offer::Offer::amount + pub fn amount(&$self) -> Option { + $contents.amount() + } + + /// Features pertaining to the originating [`Offer`], from [`Offer::offer_features`]. + /// + /// [`Offer`]: crate::offers::offer::Offer + /// [`Offer::offer_features`]: crate::offers::offer::Offer::offer_features + pub fn offer_features(&$self) -> &OfferFeatures { + $contents.offer_features() + } + + /// A complete description of the purpose of the originating offer, from [`Offer::description`]. + /// + /// [`Offer::description`]: crate::offers::offer::Offer::description + pub fn description(&$self) -> Option { + $contents.description() + } + + /// Duration since the Unix epoch when an invoice should no longer be requested, from + /// [`Offer::absolute_expiry`]. + /// + /// [`Offer::absolute_expiry`]: crate::offers::offer::Offer::absolute_expiry + pub fn absolute_expiry(&$self) -> Option { + $contents.absolute_expiry() + } + + /// The issuer of the offer, from [`Offer::issuer`]. + /// + /// [`Offer::issuer`]: crate::offers::offer::Offer::issuer + pub fn issuer(&$self) -> Option { + $contents.issuer() + } + + /// Paths to the node that may supply the invoice on the recipient's behalf, originating from + /// publicly reachable nodes. Taken from [`Offer::paths`]. + /// + /// [`Offer::paths`]: crate::offers::offer::Offer::paths + pub fn offer_message_paths(&$self) -> &[BlindedPath] { + $contents.offer_message_paths() + } + + /// Paths to the recipient for indicating that a held HTLC is available to claim when they next + /// come online. + pub fn message_paths(&$self) -> &[BlindedPath] { + $contents.message_paths() + } + + /// The quantity of items supported, from [`Offer::supported_quantity`]. + /// + /// [`Offer::supported_quantity`]: crate::offers::offer::Offer::supported_quantity + pub fn supported_quantity(&$self) -> Quantity { + $contents.supported_quantity() + } +} } + +impl StaticInvoice { + invoice_accessors_common!(self, self.contents, StaticInvoice); + invoice_accessors!(self, self.contents); + + /// Signature of the invoice verified using [`StaticInvoice::signing_pubkey`]. + pub fn signature(&self) -> Signature { + self.signature + } +} + +impl InvoiceContents { + fn chain(&self) -> ChainHash { + debug_assert_eq!(self.offer.chains().len(), 1); + self.offer.chains().first().cloned().unwrap_or_else(|| self.offer.implied_chain()) + } + + fn metadata(&self) -> Option<&Vec> { + self.offer.metadata() + } + + fn amount(&self) -> Option { + self.offer.amount() + } + + fn offer_features(&self) -> &OfferFeatures { + self.offer.features() + } + + fn description(&self) -> Option { + self.offer.description() + } + + fn absolute_expiry(&self) -> Option { + self.offer.absolute_expiry() + } + + fn issuer(&self) -> Option { + self.offer.issuer() + } + + fn offer_message_paths(&self) -> &[BlindedPath] { + self.offer.paths() + } + + fn message_paths(&self) -> &[BlindedPath] { + &self.message_paths[..] + } + + fn supported_quantity(&self) -> Quantity { + self.offer.supported_quantity() + } + + fn payment_paths(&self) -> &[(BlindedPayInfo, BlindedPath)] { + &self.payment_paths[..] + } + + fn created_at(&self) -> Duration { + self.created_at + } + + fn relative_expiry(&self) -> Duration { + self.relative_expiry.unwrap_or(DEFAULT_RELATIVE_EXPIRY) + } + + #[cfg(feature = "std")] + fn is_expired(&self) -> bool { + is_expired(self.created_at(), self.relative_expiry()) + } + + fn fallbacks(&self) -> Vec
{ + let chain = self.chain(); + self.fallbacks + .as_ref() + .map(|fallbacks| filter_fallbacks(chain, fallbacks)) + .unwrap_or_else(Vec::new) + } + + fn features(&self) -> &Bolt12InvoiceFeatures { + &self.features + } + + fn signing_pubkey(&self) -> PublicKey { + self.signing_pubkey + } +} + +impl Writeable for StaticInvoice { + fn write(&self, writer: &mut W) -> Result<(), io::Error> { + WithoutLength(&self.bytes).write(writer) + } +} + +impl TryFrom> for StaticInvoice { + type Error = Bolt12ParseError; + + fn try_from(bytes: Vec) -> Result { + let parsed_invoice = ParsedMessage::::try_from(bytes)?; + StaticInvoice::try_from(parsed_invoice) + } +} + +type FullInvoiceTlvStream = (OfferTlvStream, InvoiceTlvStream, SignatureTlvStream); + +impl SeekReadable for FullInvoiceTlvStream { + fn read(r: &mut R) -> Result { + let offer = SeekReadable::read(r)?; + let invoice = SeekReadable::read(r)?; + let signature = SeekReadable::read(r)?; + + Ok((offer, invoice, signature)) + } +} + +type PartialInvoiceTlvStream = (OfferTlvStream, InvoiceTlvStream); + +impl TryFrom> for StaticInvoice { + type Error = Bolt12ParseError; + + fn try_from(invoice: ParsedMessage) -> Result { + let ParsedMessage { bytes, tlv_stream } = invoice; + let (offer_tlv_stream, invoice_tlv_stream, SignatureTlvStream { signature }) = tlv_stream; + let contents = InvoiceContents::try_from((offer_tlv_stream, invoice_tlv_stream))?; + + let signature = match signature { + None => { + return Err(Bolt12ParseError::InvalidSemantics( + Bolt12SemanticError::MissingSignature, + )) + }, + Some(signature) => signature, + }; + let tagged_hash = TaggedHash::from_valid_tlv_stream_bytes(SIGNATURE_TAG, &bytes); + let pubkey = contents.signing_pubkey; + merkle::verify_signature(&signature, &tagged_hash, pubkey)?; + + Ok(StaticInvoice { bytes, contents, signature }) + } +} + +impl TryFrom for InvoiceContents { + type Error = Bolt12SemanticError; + + fn try_from(tlv_stream: PartialInvoiceTlvStream) -> Result { + let ( + offer_tlv_stream, + InvoiceTlvStream { + paths, + blindedpay, + created_at, + relative_expiry, + fallbacks, + features, + node_id, + message_paths, + payment_hash, + amount, + }, + ) = tlv_stream; + + if payment_hash.is_some() { + return Err(Bolt12SemanticError::UnexpectedPaymentHash); + } + if amount.is_some() { + return Err(Bolt12SemanticError::UnexpectedAmount); + } + + let payment_paths = construct_payment_paths(blindedpay, paths)?; + let message_paths = message_paths.ok_or(Bolt12SemanticError::MissingPaths)?; + + let created_at = match created_at { + None => return Err(Bolt12SemanticError::MissingCreationTime), + Some(timestamp) => Duration::from_secs(timestamp), + }; + + let relative_expiry = relative_expiry.map(Into::::into).map(Duration::from_secs); + + let features = features.unwrap_or_else(Bolt12InvoiceFeatures::empty); + + let signing_pubkey = node_id.ok_or(Bolt12SemanticError::MissingSigningPubkey)?; + check_invoice_signing_pubkey(&signing_pubkey, &offer_tlv_stream)?; + + if offer_tlv_stream.paths.is_none() { + return Err(Bolt12SemanticError::MissingPaths); + } + if offer_tlv_stream.chains.as_ref().map_or(0, |chains| chains.len()) > 1 { + return Err(Bolt12SemanticError::UnexpectedChain); + } + + Ok(InvoiceContents { + offer: OfferContents::try_from(offer_tlv_stream)?, + payment_paths, + message_paths, + created_at, + relative_expiry, + fallbacks, + features, + signing_pubkey, + }) + } +}