Skip to content

Commit 0c7fe08

Browse files
fixup! Add OfferId to Bolt12Invoice and tests for offer_id correctness
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 29e5bd6 commit 0c7fe08

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

lightning/src/offers/invoice.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -666,16 +666,14 @@ impl UnsignedBolt12Invoice {
666666
&self.tagged_hash
667667
}
668668

669-
/// Computes the offer ID if this invoice corresponds to an offer.
669+
/// Computes the [`OfferId`] if this invoice corresponds to an [`Offer`].
670670
fn compute_offer_id(&self) -> Option<OfferId> {
671671
match &self.contents {
672672
InvoiceContents::ForOffer { .. } => {
673-
// Extract offer TLV records from the invoice bytes
674-
let offer_tlv_stream = TlvStream::new(&self.bytes).range(OFFER_TYPES);
675-
let experimental_offer_tlv_stream = TlvStream::new(&self.experimental_bytes).range(EXPERIMENTAL_OFFER_TYPES);
676-
let combined_tlv_stream = offer_tlv_stream.chain(experimental_offer_tlv_stream);
677-
let tagged_hash = TaggedHash::from_tlv_stream("LDK Offer ID", combined_tlv_stream);
678-
Some(OfferId(tagged_hash.to_bytes()))
673+
// Create a temporary concatenated array for offer ID computation
674+
let mut combined_bytes = self.bytes.clone();
675+
combined_bytes.extend_from_slice(&self.experimental_bytes);
676+
Some(OfferId::from_invoice_bytes(&combined_bytes))
679677
},
680678
InvoiceContents::ForRefund { .. } => None,
681679
}
@@ -987,7 +985,7 @@ impl Bolt12Invoice {
987985
self.tagged_hash.as_digest().as_ref().clone()
988986
}
989987

990-
/// Returns the offer ID if this invoice corresponds to an offer.
988+
/// Returns the [`OfferId`] if this invoice corresponds to an [`Offer`].
991989
pub fn offer_id(&self) -> Option<OfferId> {
992990
self.offer_id
993991
}
@@ -1647,11 +1645,7 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
16471645
let pubkey = contents.fields().signing_pubkey;
16481646
merkle::verify_signature(&signature, &tagged_hash, pubkey)?;
16491647

1650-
let offer_tlv_stream = TlvStream::new(&bytes).range(OFFER_TYPES);
1651-
let experimental_offer_tlv_stream = TlvStream::new(&bytes).range(EXPERIMENTAL_OFFER_TYPES);
1652-
let combined_tlv_stream = offer_tlv_stream.chain(experimental_offer_tlv_stream);
1653-
let offer_tagged_hash = TaggedHash::from_tlv_stream("LDK Offer ID", combined_tlv_stream);
1654-
let offer_id = OfferId::from_tagged_hash(&offer_tagged_hash);
1648+
let offer_id = OfferId::from_invoice_bytes(&bytes);
16551649
Ok(Bolt12Invoice { bytes, contents, signature, tagged_hash, offer_id: Some(offer_id) })
16561650
}
16571651
}
@@ -3595,23 +3589,19 @@ mod tests {
35953589
let secp_ctx = Secp256k1::new();
35963590
let payment_id = PaymentId([1; 32]);
35973591

3598-
// Create an offer
35993592
let offer = OfferBuilder::new(recipient_pubkey())
36003593
.amount_msats(1000)
36013594
.build()
36023595
.unwrap();
36033596

3604-
// Get the offer ID
36053597
let offer_id = offer.id();
36063598

3607-
// Create an invoice request from the offer
36083599
let invoice_request = offer
36093600
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id)
36103601
.unwrap()
36113602
.build_and_sign()
36123603
.unwrap();
36133604

3614-
// Create an invoice from the invoice request
36153605
let invoice = invoice_request
36163606
.respond_with_no_std(payment_paths(), payment_hash(), now())
36173607
.unwrap()
@@ -3620,19 +3610,16 @@ mod tests {
36203610
.sign(recipient_sign)
36213611
.unwrap();
36223612

3623-
// Verify that the invoice's offer_id matches the offer's id
36243613
assert_eq!(invoice.offer_id(), Some(offer_id));
36253614
}
36263615

36273616
#[test]
36283617
fn refund_invoice_has_no_offer_id() {
3629-
// Create a refund
36303618
let refund = RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000)
36313619
.unwrap()
36323620
.build()
36333621
.unwrap();
36343622

3635-
// Create an invoice from the refund
36363623
let invoice = refund
36373624
.respond_with_no_std(payment_paths(), payment_hash(), recipient_pubkey(), now())
36383625
.unwrap()
@@ -3641,7 +3628,6 @@ mod tests {
36413628
.sign(recipient_sign)
36423629
.unwrap();
36433630

3644-
// Verify that the refund invoice has no offer_id
36453631
assert_eq!(invoice.offer_id(), None);
36463632
}
36473633
}

lightning/src/offers/offer.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ impl OfferId {
134134
Self(tagged_hash.to_bytes())
135135
}
136136

137-
pub (crate) fn from_tagged_hash(tagged_hash: &TaggedHash) -> Self {
137+
/// Computes the [`OfferId`] from a [`Bolt12Invoice`] bytes.
138+
pub(crate) fn from_invoice_bytes(bytes: &[u8]) -> Self {
139+
let offer_tlv_stream = TlvStream::new(bytes).range(OFFER_TYPES);
140+
let experimental_offer_tlv_stream = TlvStream::new(bytes).range(EXPERIMENTAL_OFFER_TYPES);
141+
let combined_tlv_stream = offer_tlv_stream.chain(experimental_offer_tlv_stream);
142+
let tagged_hash = TaggedHash::from_tlv_stream(Self::ID_TAG, combined_tlv_stream);
138143
Self(tagged_hash.to_bytes())
139144
}
140145
}

0 commit comments

Comments
 (0)