Skip to content

Commit 2da0ac8

Browse files
fixup! offer: add OfferId to Bolt12Invoice
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent a68da7e commit 2da0ac8

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

lightning/src/offers/invoice.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ pub struct UnsignedBolt12Invoice {
595595
experimental_bytes: Vec<u8>,
596596
contents: InvoiceContents,
597597
tagged_hash: TaggedHash,
598+
offer_id: Option<OfferId>,
598599
}
599600

600601
/// A function for signing an [`UnsignedBolt12Invoice`].
@@ -658,7 +659,11 @@ impl UnsignedBolt12Invoice {
658659
let tlv_stream = TlvStream::new(&bytes).chain(TlvStream::new(&experimental_bytes));
659660
let tagged_hash = TaggedHash::from_tlv_stream(SIGNATURE_TAG, tlv_stream);
660661

661-
Self { bytes, experimental_bytes, contents, tagged_hash }
662+
let offer_id = match &contents {
663+
InvoiceContents::ForOffer { .. } => Some(OfferId::from_valid_bolt12_tlv_stream(&bytes)),
664+
InvoiceContents::ForRefund { .. } => None,
665+
};
666+
Self { bytes, experimental_bytes, contents, tagged_hash, offer_id }
662667
}
663668

664669
/// Returns the [`TaggedHash`] of the invoice to sign.
@@ -686,9 +691,9 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
686691
// Append the experimental bytes after the signature.
687692
$self.bytes.extend_from_slice(&$self.experimental_bytes);
688693

689-
let offer_id = match &self.contents {
694+
let offer_id = match &$self.contents {
690695
InvoiceContents::ForOffer { .. } => {
691-
Some(OfferId::from_valid_bolt12_tlv_stream(&self.bytes))
696+
Some(OfferId::from_valid_bolt12_tlv_stream(&$self.bytes))
692697
},
693698
InvoiceContents::ForRefund { .. } => None,
694699
};
@@ -976,13 +981,6 @@ impl Bolt12Invoice {
976981
self.tagged_hash.as_digest().as_ref().clone()
977982
}
978983

979-
/// Returns the [`OfferId`] if this invoice corresponds to an [`Offer`].
980-
///
981-
/// [`Offer`]: crate::offers::offer::Offer
982-
pub fn offer_id(&self) -> Option<OfferId> {
983-
self.offer_id
984-
}
985-
986984
/// Verifies that the invoice was for a request or refund created using the given key by
987985
/// checking the payer metadata from the invoice request.
988986
///
@@ -1448,7 +1446,12 @@ impl TryFrom<Vec<u8>> for UnsignedBolt12Invoice {
14481446
.map_or(0, |last_record| last_record.end);
14491447
let experimental_bytes = bytes.split_off(offset);
14501448

1451-
Ok(UnsignedBolt12Invoice { bytes, experimental_bytes, contents, tagged_hash })
1449+
let offer_id = match &contents {
1450+
InvoiceContents::ForOffer { .. } => Some(OfferId::from_valid_bolt12_tlv_stream(&bytes)),
1451+
InvoiceContents::ForRefund { .. } => None,
1452+
};
1453+
1454+
Ok(UnsignedBolt12Invoice { bytes, experimental_bytes, contents, tagged_hash, offer_id })
14521455
}
14531456
}
14541457

lightning/src/offers/invoice_macros.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ macro_rules! invoice_accessors_common { ($self: ident, $contents: expr, $invoice
141141
pub fn invoice_features(&$self) -> &Bolt12InvoiceFeatures {
142142
$contents.features()
143143
}
144+
145+
/// Returns the [`OfferId`] if this invoice corresponds to an [`Offer`].
146+
///
147+
/// [`Offer`]: crate::offers::offer::Offer
148+
pub fn offer_id(&$self) -> Option<OfferId> {
149+
$self.offer_id
150+
}
144151
} }
145152

146153
pub(super) use invoice_accessors_common;

lightning/src/offers/static_invoice.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::offers::merkle::{
2929
use crate::offers::nonce::Nonce;
3030
use crate::offers::offer::{
3131
Amount, ExperimentalOfferTlvStream, ExperimentalOfferTlvStreamRef, Offer, OfferContents,
32-
OfferTlvStream, OfferTlvStreamRef, Quantity, EXPERIMENTAL_OFFER_TYPES, OFFER_TYPES,
32+
OfferId, OfferTlvStream, OfferTlvStreamRef, Quantity, EXPERIMENTAL_OFFER_TYPES, OFFER_TYPES,
3333
};
3434
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
3535
use crate::types::features::{Bolt12InvoiceFeatures, OfferFeatures};
@@ -70,6 +70,7 @@ pub struct StaticInvoice {
7070
bytes: Vec<u8>,
7171
contents: InvoiceContents,
7272
signature: Signature,
73+
offer_id: Option<OfferId>,
7374
}
7475

7576
impl PartialEq for StaticInvoice {
@@ -198,6 +199,7 @@ pub struct UnsignedStaticInvoice {
198199
experimental_bytes: Vec<u8>,
199200
contents: InvoiceContents,
200201
tagged_hash: TaggedHash,
202+
offer_id: Option<OfferId>,
201203
}
202204

203205
macro_rules! invoice_accessors { ($self: ident, $contents: expr) => {
@@ -330,7 +332,9 @@ impl UnsignedStaticInvoice {
330332
let tlv_stream = TlvStream::new(&bytes).chain(TlvStream::new(&experimental_bytes));
331333
let tagged_hash = TaggedHash::from_tlv_stream(SIGNATURE_TAG, tlv_stream);
332334

333-
Self { bytes, experimental_bytes, contents, tagged_hash }
335+
// FIXME: we can have a static invoice for a Refund? if yes this should be optional
336+
let offer_id = OfferId::from_valid_bolt12_tlv_stream(&bytes);
337+
Self { bytes, experimental_bytes, contents, tagged_hash, offer_id: Some(offer_id) }
334338
}
335339

336340
/// Signs the [`TaggedHash`] of the invoice using the given function.
@@ -347,7 +351,13 @@ impl UnsignedStaticInvoice {
347351
// Append the experimental bytes after the signature.
348352
self.bytes.extend_from_slice(&self.experimental_bytes);
349353

350-
Ok(StaticInvoice { bytes: self.bytes, contents: self.contents, signature })
354+
let offer_id = OfferId::from_valid_bolt12_tlv_stream(&self.bytes);
355+
Ok(StaticInvoice {
356+
bytes: self.bytes,
357+
contents: self.contents,
358+
signature,
359+
offer_id: Some(offer_id),
360+
})
351361
}
352362

353363
invoice_accessors_common!(self, self.contents, UnsignedStaticInvoice);
@@ -627,7 +637,8 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for StaticInvoice {
627637
let pubkey = contents.signing_pubkey;
628638
merkle::verify_signature(&signature, &tagged_hash, pubkey)?;
629639

630-
Ok(StaticInvoice { bytes, contents, signature })
640+
let offer_id = OfferId::from_valid_bolt12_tlv_stream(&bytes);
641+
Ok(StaticInvoice { bytes, contents, signature, offer_id: Some(offer_id) })
631642
}
632643
}
633644

0 commit comments

Comments
 (0)