Skip to content

Commit bd85560

Browse files
committed
WIP: send invoice request encoding
1 parent 6c08907 commit bd85560

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use core::convert::TryFrom;
5757
use io;
5858
use ln::features::OfferFeatures;
5959
use offers::merkle::{SignatureTlvStream, SignatureTlvStreamRef, self};
60-
use offers::offer::{Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef};
60+
use offers::offer::{Amount, Offer, OfferContents, OfferTlvStream, OfferTlvStreamRef, SendInvoiceOfferContents};
6161
use offers::parse::{ParseError, SemanticError};
6262
use offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
6363
use util::ser::{Readable, WithoutLength, Writeable, Writer};
@@ -400,7 +400,10 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
400400
) = tlv_stream;
401401

402402
let payer = PayerContents(metadata);
403-
let offer = OfferContents::try_from(offer_tlv_stream)?;
403+
let offer = match offer_tlv_stream.node_id {
404+
Some(_) => OfferContents::try_from(offer_tlv_stream)?,
405+
None => SendInvoiceOfferContents::try_from(offer_tlv_stream)?.0,
406+
};
404407

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

lightning/src/offers/offer.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,56 @@ impl TryFrom<OfferTlvStream> for OfferContents {
641641
}
642642
}
643643

644+
/// [`OfferContents`] used with a "send invoice" offer (i.e., a published [`InvoiceRequest`]).
645+
///
646+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
647+
pub(super) struct SendInvoiceOfferContents(pub OfferContents);
648+
649+
impl TryFrom<OfferTlvStream> for SendInvoiceOfferContents {
650+
type Error = SemanticError;
651+
652+
fn try_from(tlv_stream: OfferTlvStream) -> Result<Self, Self::Error> {
653+
let OfferTlvStream {
654+
chains, metadata, currency, amount, description, features, absolute_expiry, paths,
655+
issuer, quantity_min, quantity_max, node_id,
656+
} = tlv_stream;
657+
assert!(node_id.is_none());
658+
659+
if chains.is_some() {
660+
return Err(SemanticError::UnexpectedChain);
661+
}
662+
663+
if currency.is_some() || amount.is_some() {
664+
return Err(SemanticError::UnexpectedAmount);
665+
}
666+
667+
let description = match description {
668+
None => return Err(SemanticError::MissingDescription),
669+
Some(description) => description,
670+
};
671+
672+
if features.is_some() {
673+
return Err(SemanticError::UnexpectedFeatures);
674+
}
675+
676+
let absolute_expiry = absolute_expiry.map(Duration::from_secs);
677+
678+
let paths = match paths {
679+
Some(paths) if paths.is_empty() => return Err(SemanticError::MissingPaths),
680+
paths => paths,
681+
};
682+
683+
if quantity_min.is_some() || quantity_max.is_some() {
684+
return Err(SemanticError::UnexpectedQuantity);
685+
}
686+
687+
Ok(SendInvoiceOfferContents(OfferContents {
688+
chains: None, metadata, amount: None, description, features: None, absolute_expiry,
689+
issuer, paths, quantity_min: None, quantity_max: None, signing_pubkey: None,
690+
}))
691+
}
692+
}
693+
644694
impl core::fmt::Display for Offer {
645695
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
646696
self.fmt_bech32_str(f)

lightning/src/offers/parse.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,23 @@ pub enum ParseError {
8080
pub enum SemanticError {
8181
/// The provided block hash does not correspond to a supported chain.
8282
UnsupportedChain,
83+
/// A chain was provided but was not expected.
84+
UnexpectedChain,
85+
/// An amount was not provided.
8386
/// An amount was expected but was missing.
8487
MissingAmount,
8588
/// An amount exceeded the maximum number of bitcoin.
8689
InvalidAmount,
8790
/// An amount was provided but was not sufficient in value.
8891
InsufficientAmount,
92+
/// An amount was provided but was not expected.
93+
UnexpectedAmount,
8994
/// A currency was provided that is not supported.
9095
UnsupportedCurrency,
9196
/// A feature was required but is unknown.
9297
UnknownRequiredFeatures,
98+
/// Features were provided but were not expected.
99+
UnexpectedFeatures,
93100
/// A required description was not provided.
94101
MissingDescription,
95102
/// A node id was not provided.

0 commit comments

Comments
 (0)