Skip to content

Commit 9fde3d9

Browse files
committed
WIP: send invoice request encoding
1 parent a16cc51 commit 9fde3d9

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use io;
5858
use ln::features::OfferFeatures;
5959
use ln::msgs::DecodeError;
6060
use offers::merkle::{SignatureTlvStream, SignatureTlvStreamRef, self};
61-
use offers::offer::{Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef};
61+
use offers::offer::{Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, SendInvoiceOfferContents};
6262
use offers::parse::{ParseError, SemanticError};
6363
use offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
6464
use util::ser::{HighZeroBytesDroppedBigSize, SeekReadable, WithoutLength, Writeable, Writer};
@@ -412,7 +412,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
412412
) = tlv_stream;
413413

414414
let payer = PayerContents(metadata);
415-
let offer = OfferContents::try_from(offer_tlv_stream)?;
415+
let offer = match offer_tlv_stream.node_id {
416+
Some(_) => OfferContents::try_from(offer_tlv_stream)?,
417+
None => SendInvoiceOfferContents::try_from(offer_tlv_stream)?.0,
418+
};
416419

417420
if !offer.supports_chain(chain.unwrap_or_else(|| offer.implied_chain())) {
418421
return Err(SemanticError::UnsupportedChain);

lightning/src/offers/offer.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,57 @@ impl TryFrom<OfferTlvStream> for OfferContents {
596596
}
597597
}
598598

599+
/// [`OfferContents`] used with a "send invoice" offer (i.e., a published [`InvoiceRequest`]).
600+
///
601+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
602+
pub(super) struct SendInvoiceOfferContents(pub OfferContents);
603+
604+
impl TryFrom<OfferTlvStream> for SendInvoiceOfferContents {
605+
type Error = SemanticError;
606+
607+
fn try_from(tlv_stream: OfferTlvStream) -> Result<Self, Self::Error> {
608+
let OfferTlvStream {
609+
chains, metadata, currency, amount, description, features, absolute_expiry, paths,
610+
issuer, quantity_max, node_id,
611+
} = tlv_stream;
612+
assert!(node_id.is_none());
613+
614+
if chains.is_some() {
615+
return Err(SemanticError::UnexpectedChain);
616+
}
617+
618+
if currency.is_some() || amount.is_some() {
619+
return Err(SemanticError::UnexpectedAmount);
620+
}
621+
622+
let description = match description {
623+
None => return Err(SemanticError::MissingDescription),
624+
Some(description) => description,
625+
};
626+
627+
let features = match features {
628+
None => OfferFeatures::empty(),
629+
Some(_) => return Err(SemanticError::UnexpectedFeatures),
630+
};
631+
632+
let absolute_expiry = absolute_expiry.map(Duration::from_secs);
633+
634+
let paths = match paths {
635+
Some(paths) if paths.is_empty() => return Err(SemanticError::MissingPaths),
636+
paths => paths,
637+
};
638+
639+
if quantity_max.is_some() {
640+
return Err(SemanticError::UnexpectedQuantity);
641+
}
642+
643+
Ok(SendInvoiceOfferContents(OfferContents {
644+
chains: None, metadata, amount: None, description, features, absolute_expiry, issuer,
645+
paths, supported_quantity: Quantity::One, signing_pubkey: None,
646+
}))
647+
}
648+
}
649+
599650
impl core::fmt::Display for Offer {
600651
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
601652
self.fmt_bech32_str(f)

lightning/src/offers/parse.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,23 @@ pub enum ParseError {
7777
pub enum SemanticError {
7878
/// The provided chain hash does not correspond to a supported chain.
7979
UnsupportedChain,
80+
/// A chain was provided but was not expected.
81+
UnexpectedChain,
82+
/// An amount was not provided.
8083
/// An amount was expected but was missing.
8184
MissingAmount,
8285
/// An amount exceeded the maximum number of bitcoin.
8386
InvalidAmount,
8487
/// An amount was provided but was not sufficient in value.
8588
InsufficientAmount,
89+
/// An amount was provided but was not expected.
90+
UnexpectedAmount,
8691
/// A currency was provided that is not supported.
8792
UnsupportedCurrency,
8893
/// A feature was required but is unknown.
8994
UnknownRequiredFeatures,
95+
/// Features were provided but were not expected.
96+
UnexpectedFeatures,
9097
/// A required description was not provided.
9198
MissingDescription,
9299
/// A node id was not provided.

0 commit comments

Comments
 (0)