Skip to content

Commit

Permalink
Rename StaticInvoice::message_paths to be more specific.
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinewallace committed Jun 13, 2024
1 parent 807c9e2 commit 1bfd19e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
18 changes: 9 additions & 9 deletions lightning/src/offers/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ impl InvoiceFields {
fallbacks: self.fallbacks.as_ref(),
features,
node_id: Some(&self.signing_pubkey),
message_paths: None,
async_receive_message_paths: None,
}
}
}
Expand Down Expand Up @@ -1171,7 +1171,7 @@ tlv_stream!(InvoiceTlvStream, InvoiceTlvStreamRef, 160..240, {
(174, features: (Bolt12InvoiceFeatures, WithoutLength)),
(176, node_id: PublicKey),
// Only present in `StaticInvoice`s.
(238, message_paths: (Vec<BlindedPath>, WithoutLength)),
(238, async_receive_message_paths: (Vec<BlindedPath>, WithoutLength)),
});

pub(super) type BlindedPathIter<'a> = core::iter::Map<
Expand Down Expand Up @@ -1309,11 +1309,11 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
invoice_request_tlv_stream,
InvoiceTlvStream {
paths, blindedpay, created_at, relative_expiry, payment_hash, amount, fallbacks,
features, node_id, message_paths,
features, node_id, async_receive_message_paths,
},
) = tlv_stream;

if message_paths.is_some() { return Err(Bolt12SemanticError::UnexpectedPaths) }
if async_receive_message_paths.is_some() { return Err(Bolt12SemanticError::UnexpectedPaths) }

let payment_paths = construct_payment_paths(blindedpay, paths)?;

Expand Down Expand Up @@ -1580,7 +1580,7 @@ mod tests {
fallbacks: None,
features: None,
node_id: Some(&recipient_pubkey()),
message_paths: None,
async_receive_message_paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice.signature()) },
),
Expand Down Expand Up @@ -1672,7 +1672,7 @@ mod tests {
fallbacks: None,
features: None,
node_id: Some(&recipient_pubkey()),
message_paths: None,
async_receive_message_paths: None,
},
SignatureTlvStreamRef { signature: Some(&invoice.signature()) },
),
Expand Down Expand Up @@ -2445,7 +2445,7 @@ mod tests {
}

#[test]
fn fails_parsing_invoice_with_message_paths() {
fn fails_parsing_invoice_with_async_receive_message_paths() {
let invoice = OfferBuilder::new(recipient_pubkey())
.amount_msats(1000)
.build().unwrap()
Expand All @@ -2466,8 +2466,8 @@ mod tests {
};

let mut tlv_stream = invoice.as_tlv_stream();
let message_paths = vec![blinded_path];
tlv_stream.3.message_paths = Some(&message_paths);
let async_receive_message_paths = vec![blinded_path];
tlv_stream.3.async_receive_message_paths = Some(&async_receive_message_paths);

match Bolt12Invoice::try_from(tlv_stream.to_bytes()) {
Ok(_) => panic!("expected error"),
Expand Down
52 changes: 31 additions & 21 deletions lightning/src/offers/static_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct InvoiceContents {
fallbacks: Option<Vec<FallbackAddress>>,
features: Bolt12InvoiceFeatures,
signing_pubkey: PublicKey,
message_paths: Vec<BlindedPath>,
async_receive_message_paths: Vec<BlindedPath>,
}

/// Builds a [`StaticInvoice`] from an [`Offer`].
Expand All @@ -98,14 +98,17 @@ impl<'a> StaticInvoiceBuilder<'a> {
/// after `created_at`.
pub fn for_offer_using_derived_keys<T: secp256k1::Signing>(
offer: &'a Offer, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
message_paths: Vec<BlindedPath>, created_at: Duration, expanded_key: &ExpandedKey,
secp_ctx: &Secp256k1<T>,
async_receive_message_paths: Vec<BlindedPath>, created_at: Duration,
expanded_key: &ExpandedKey, secp_ctx: &Secp256k1<T>,
) -> Result<Self, Bolt12SemanticError> {
if offer.chains().len() > 1 {
return Err(Bolt12SemanticError::UnexpectedChain);
}

if payment_paths.is_empty() || message_paths.is_empty() || offer.paths().is_empty() {
if payment_paths.is_empty()
|| async_receive_message_paths.is_empty()
|| offer.paths().is_empty()
{
return Err(Bolt12SemanticError::MissingPaths);
}

Expand All @@ -123,8 +126,13 @@ impl<'a> StaticInvoiceBuilder<'a> {
return Err(Bolt12SemanticError::InvalidSigningPubkey);
}

let invoice =
InvoiceContents::new(offer, payment_paths, message_paths, created_at, signing_pubkey);
let invoice = InvoiceContents::new(
offer,
payment_paths,
async_receive_message_paths,
created_at,
signing_pubkey,
);

Ok(Self { offer_bytes: &offer.bytes, invoice, keys })
}
Expand Down Expand Up @@ -230,8 +238,8 @@ macro_rules! invoice_accessors { ($self: ident, $contents: expr) => {

/// 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()
pub fn async_receive_message_paths(&$self) -> &[BlindedPath] {
$contents.async_receive_message_paths()
}

/// The quantity of items supported, from [`Offer::supported_quantity`].
Expand Down Expand Up @@ -327,12 +335,13 @@ impl InvoiceContents {

fn new(
offer: &Offer, payment_paths: Vec<(BlindedPayInfo, BlindedPath)>,
message_paths: Vec<BlindedPath>, created_at: Duration, signing_pubkey: PublicKey,
async_receive_message_paths: Vec<BlindedPath>, created_at: Duration,
signing_pubkey: PublicKey,
) -> Self {
Self {
offer: offer.contents.clone(),
payment_paths,
message_paths,
async_receive_message_paths,
created_at,
relative_expiry: None,
fallbacks: None,
Expand All @@ -352,7 +361,7 @@ impl InvoiceContents {

let invoice = InvoiceTlvStreamRef {
paths: Some(Iterable(self.payment_paths.iter().map(|(_, path)| path))),
message_paths: Some(self.message_paths.as_ref()),
async_receive_message_paths: Some(self.async_receive_message_paths.as_ref()),
blindedpay: Some(Iterable(self.payment_paths.iter().map(|(payinfo, _)| payinfo))),
created_at: Some(self.created_at.as_secs()),
relative_expiry: self.relative_expiry.map(|duration| duration.as_secs() as u32),
Expand Down Expand Up @@ -399,8 +408,8 @@ impl InvoiceContents {
self.offer.paths()
}

fn message_paths(&self) -> &[BlindedPath] {
&self.message_paths[..]
fn async_receive_message_paths(&self) -> &[BlindedPath] {
&self.async_receive_message_paths[..]
}

fn supported_quantity(&self) -> Quantity {
Expand Down Expand Up @@ -510,7 +519,7 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
fallbacks,
features,
node_id,
message_paths,
async_receive_message_paths,
payment_hash,
amount,
},
Expand All @@ -524,7 +533,8 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
}

let payment_paths = construct_payment_paths(blindedpay, paths)?;
let message_paths = message_paths.ok_or(Bolt12SemanticError::MissingPaths)?;
let async_receive_message_paths =
async_receive_message_paths.ok_or(Bolt12SemanticError::MissingPaths)?;

let created_at = match created_at {
None => return Err(Bolt12SemanticError::MissingCreationTime),
Expand All @@ -548,7 +558,7 @@ impl TryFrom<PartialInvoiceTlvStream> for InvoiceContents {
Ok(InvoiceContents {
offer: OfferContents::try_from(offer_tlv_stream)?,
payment_paths,
message_paths,
async_receive_message_paths,
created_at,
relative_expiry,
fallbacks,
Expand Down Expand Up @@ -679,7 +689,7 @@ mod tests {
assert_eq!(invoice.offer_features(), &OfferFeatures::empty());
assert_eq!(invoice.absolute_expiry(), None);
assert_eq!(invoice.request_paths(), &[blinded_path()]);
assert_eq!(invoice.message_paths(), &[blinded_path()]);
assert_eq!(invoice.async_receive_message_paths(), &[blinded_path()]);
assert_eq!(invoice.issuer(), None);
assert_eq!(invoice.supported_quantity(), Quantity::One);
assert_ne!(invoice.signing_pubkey(), recipient_pubkey());
Expand Down Expand Up @@ -726,7 +736,7 @@ mod tests {
fallbacks: None,
features: None,
node_id: Some(&offer_signing_pubkey),
message_paths: Some(&paths),
async_receive_message_paths: Some(&paths),
},
SignatureTlvStreamRef { signature: Some(&invoice.signature()) },
)
Expand Down Expand Up @@ -1055,9 +1065,9 @@ mod tests {
}

// Error if message paths are missing.
let missing_message_paths_invoice = invoice();
let mut tlv_stream = missing_message_paths_invoice.as_tlv_stream();
tlv_stream.1.message_paths = None;
let missing_async_receive_message_paths_invoice = invoice();
let mut tlv_stream = missing_async_receive_message_paths_invoice.as_tlv_stream();
tlv_stream.1.async_receive_message_paths = None;
match StaticInvoice::try_from(tlv_stream_to_bytes(&tlv_stream)) {
Ok(_) => panic!("expected error"),
Err(e) => {
Expand Down

0 comments on commit 1bfd19e

Please sign in to comment.