Skip to content

Commit 123e16f

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

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

lightning/src/offers/invoice.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -666,17 +666,10 @@ 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 {
672-
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()))
679-
},
672+
InvoiceContents::ForOffer { .. } => Some(OfferId::from_invoice_bytes(&self.bytes)),
680673
InvoiceContents::ForRefund { .. } => None,
681674
}
682675
}
@@ -987,7 +980,7 @@ impl Bolt12Invoice {
987980
self.tagged_hash.as_digest().as_ref().clone()
988981
}
989982

990-
/// Returns the offer ID if this invoice corresponds to an offer.
983+
/// Returns the [`OfferId`] if this invoice corresponds to an [`Offer`].
991984
pub fn offer_id(&self) -> Option<OfferId> {
992985
self.offer_id
993986
}
@@ -1647,11 +1640,7 @@ impl TryFrom<ParsedMessage<FullInvoiceTlvStream>> for Bolt12Invoice {
16471640
let pubkey = contents.fields().signing_pubkey;
16481641
merkle::verify_signature(&signature, &tagged_hash, pubkey)?;
16491642

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);
1643+
let offer_id = OfferId::from_invoice_bytes(&bytes);
16551644
Ok(Bolt12Invoice { bytes, contents, signature, tagged_hash, offer_id: Some(offer_id) })
16561645
}
16571646
}
@@ -3595,23 +3584,19 @@ mod tests {
35953584
let secp_ctx = Secp256k1::new();
35963585
let payment_id = PaymentId([1; 32]);
35973586

3598-
// Create an offer
35993587
let offer = OfferBuilder::new(recipient_pubkey())
36003588
.amount_msats(1000)
36013589
.build()
36023590
.unwrap();
36033591

3604-
// Get the offer ID
36053592
let offer_id = offer.id();
36063593

3607-
// Create an invoice request from the offer
36083594
let invoice_request = offer
36093595
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id)
36103596
.unwrap()
36113597
.build_and_sign()
36123598
.unwrap();
36133599

3614-
// Create an invoice from the invoice request
36153600
let invoice = invoice_request
36163601
.respond_with_no_std(payment_paths(), payment_hash(), now())
36173602
.unwrap()
@@ -3620,19 +3605,16 @@ mod tests {
36203605
.sign(recipient_sign)
36213606
.unwrap();
36223607

3623-
// Verify that the invoice's offer_id matches the offer's id
36243608
assert_eq!(invoice.offer_id(), Some(offer_id));
36253609
}
36263610

36273611
#[test]
36283612
fn refund_invoice_has_no_offer_id() {
3629-
// Create a refund
36303613
let refund = RefundBuilder::new(vec![1; 32], payer_pubkey(), 1000)
36313614
.unwrap()
36323615
.build()
36333616
.unwrap();
36343617

3635-
// Create an invoice from the refund
36363618
let invoice = refund
36373619
.respond_with_no_std(payment_paths(), payment_hash(), recipient_pubkey(), now())
36383620
.unwrap()
@@ -3641,7 +3623,6 @@ mod tests {
36413623
.sign(recipient_sign)
36423624
.unwrap();
36433625

3644-
// Verify that the refund invoice has no offer_id
36453626
assert_eq!(invoice.offer_id(), None);
36463627
}
36473628
}

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)