Skip to content

Commit a68da7e

Browse files
offer: add OfferId to Bolt12Invoice
- Add an Option<OfferId> field to Bolt12Invoice to track the originating offer. - Compute the offer_id for invoices created from offers by extracting the offer TLV records and hashing them with the correct tag. - Expose a public offer_id() accessor on invoice. - Add tests to ensure the offer_id in the invoice matches the originating Offer, and that refund invoices have no offer_id. - All existing and new tests pass. This enables linking invoices to their originating offers in a robust and spec-compliant way. Signed-off-by: Vincenzo Palazzo <[email protected]> Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com> Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent d7f7987 commit a68da7e

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

lightning/src/offers/invoice.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ use crate::offers::merkle::{
135135
};
136136
use crate::offers::nonce::Nonce;
137137
use crate::offers::offer::{
138-
Amount, ExperimentalOfferTlvStream, ExperimentalOfferTlvStreamRef, OfferTlvStream,
138+
Amount, ExperimentalOfferTlvStream, ExperimentalOfferTlvStreamRef, OfferId, OfferTlvStream,
139139
OfferTlvStreamRef, Quantity, EXPERIMENTAL_OFFER_TYPES, OFFER_TYPES,
140140
};
141141
use crate::offers::parse::{Bolt12ParseError, Bolt12SemanticError, ParsedMessage};
@@ -686,6 +686,13 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
686686
// Append the experimental bytes after the signature.
687687
$self.bytes.extend_from_slice(&$self.experimental_bytes);
688688

689+
let offer_id = match &self.contents {
690+
InvoiceContents::ForOffer { .. } => {
691+
Some(OfferId::from_valid_bolt12_tlv_stream(&self.bytes))
692+
},
693+
InvoiceContents::ForRefund { .. } => None,
694+
};
695+
689696
Ok(Bolt12Invoice {
690697
#[cfg(not(c_bindings))]
691698
bytes: $self.bytes,
@@ -700,6 +707,7 @@ macro_rules! unsigned_invoice_sign_method { ($self: ident, $self_type: ty $(, $s
700707
tagged_hash: $self.tagged_hash,
701708
#[cfg(c_bindings)]
702709
tagged_hash: $self.tagged_hash.clone(),
710+
offer_id,
703711
})
704712
}
705713
} }
@@ -734,6 +742,7 @@ pub struct Bolt12Invoice {
734742
contents: InvoiceContents,
735743
signature: Signature,
736744
tagged_hash: TaggedHash,
745+
offer_id: Option<OfferId>,
737746
}
738747

739748
/// The contents of an [`Bolt12Invoice`] for responding to either an [`Offer`] or a [`Refund`].
@@ -967,6 +976,13 @@ impl Bolt12Invoice {
967976
self.tagged_hash.as_digest().as_ref().clone()
968977
}
969978

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+
970986
/// Verifies that the invoice was for a request or refund created using the given key by
971987
/// checking the payer metadata from the invoice request.
972988
///
@@ -1622,7 +1638,11 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
16221638
let pubkey = contents.fields().signing_pubkey;
16231639
merkle::verify_signature(&signature, &tagged_hash, pubkey)?;
16241640

1625-
Ok(Bolt12Invoice { bytes, contents, signature, tagged_hash })
1641+
let offer_id = match &contents {
1642+
InvoiceContents::ForOffer { .. } => Some(OfferId::from_valid_bolt12_tlv_stream(&bytes)),
1643+
InvoiceContents::ForRefund { .. } => None,
1644+
};
1645+
Ok(Bolt12Invoice { bytes, contents, signature, tagged_hash, offer_id })
16261646
}
16271647
}
16281648

@@ -3556,4 +3576,49 @@ mod tests {
35563576
),
35573577
}
35583578
}
3579+
3580+
#[test]
3581+
fn invoice_offer_id_matches_offer_id() {
3582+
let expanded_key = ExpandedKey::new([42; 32]);
3583+
let entropy = FixedEntropy {};
3584+
let nonce = Nonce::from_entropy_source(&entropy);
3585+
let secp_ctx = Secp256k1::new();
3586+
let payment_id = PaymentId([1; 32]);
3587+
3588+
let offer = OfferBuilder::new(recipient_pubkey()).amount_msats(1000).build().unwrap();
3589+
3590+
let offer_id = offer.id();
3591+
3592+
let invoice_request = offer
3593+
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id)
3594+
.unwrap()
3595+
.build_and_sign()
3596+
.unwrap();
3597+
3598+
let invoice = invoice_request
3599+
.respond_with_no_std(payment_paths(), payment_hash(), now())
3600+
.unwrap()
3601+
.build()
3602+
.unwrap()
3603+
.sign(recipient_sign)
3604+
.unwrap();
3605+
3606+
assert_eq!(invoice.offer_id(), Some(offer_id));
3607+
}
3608+
3609+
#[test]
3610+
fn refund_invoice_has_no_offer_id() {
3611+
let refund =
3612+
RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000).unwrap().build().unwrap();
3613+
3614+
let invoice = refund
3615+
.respond_with_no_std(payment_paths(), payment_hash(), recipient_pubkey(), now())
3616+
.unwrap()
3617+
.build()
3618+
.unwrap()
3619+
.sign(recipient_sign)
3620+
.unwrap();
3621+
3622+
assert_eq!(invoice.offer_id(), None);
3623+
}
35593624
}

lightning/src/offers/offer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl OfferId {
128128
Self(tagged_hash.to_bytes())
129129
}
130130

131-
fn from_valid_invreq_tlv_stream(bytes: &[u8]) -> Self {
131+
pub(super) fn from_valid_bolt12_tlv_stream(bytes: &[u8]) -> Self {
132132
let tlv_stream = Offer::tlv_stream_iter(bytes);
133133
let tagged_hash = TaggedHash::from_tlv_stream(Self::ID_TAG, tlv_stream);
134134
Self(tagged_hash.to_bytes())
@@ -987,7 +987,7 @@ impl OfferContents {
987987
secp_ctx,
988988
)?;
989989

990-
let offer_id = OfferId::from_valid_invreq_tlv_stream(bytes);
990+
let offer_id = OfferId::from_valid_bolt12_tlv_stream(bytes);
991991

992992
Ok((offer_id, keys))
993993
},

0 commit comments

Comments
 (0)