Skip to content

Commit 1916fc4

Browse files
committed
Refactor amount checks and fix currency check
Modified check_amount_msats_for_quantity to trust the user-provided msats amount and moved/fixed the currency check to the InvoiceRequest parsing code. We check if the user attempts to use an Amount:Currency and return an error if so.
1 parent 72912ce commit 1916fc4

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

lightning/src/ln/offers_tests.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use crate::offers::invoice_error::InvoiceError;
5555
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
5656
use crate::offers::parse::Bolt12SemanticError;
5757
use crate::onion_message::messenger::{Destination, PeeledOnion};
58+
use crate::offers::offer::Amount;
5859
use crate::onion_message::offers::OffersMessage;
5960
use crate::onion_message::packet::ParsedOnionMessageContents;
6061
use crate::routing::gossip::{NodeAlias, NodeId};
@@ -1607,9 +1608,9 @@ fn fails_creating_or_paying_for_offer_without_connected_peers() {
16071608
reconnect_nodes(args);
16081609

16091610
let offer = alice.node
1610-
.create_offer_builder(Some(absolute_expiry)).unwrap()
1611-
.amount_msats(10_000_000)
1612-
.build().unwrap();
1611+
.create_offer_builder(None).unwrap()
1612+
.amount(Amount::Currency {iso4217_code: *b"USD", amount: 6_000})
1613+
.build_unchecked();
16131614

16141615
let payment_id = PaymentId([1; 32]);
16151616

@@ -1771,8 +1772,9 @@ fn fails_creating_invoice_request_without_blinded_reply_path() {
17711772

17721773
let offer = alice.node
17731774
.create_offer_builder(None).unwrap()
1774-
.amount_msats(10_000_000)
1775-
.build().unwrap();
1775+
.amount(Amount::Currency {iso4217_code: *b"USD", amount: 6_000})
1776+
.build_unchecked();
1777+
17761778

17771779
let payment_id = PaymentId([1; 32]);
17781780

lightning/src/offers/invoice_request.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use crate::ln::msgs::DecodeError;
7171
use crate::offers::invoice::BlindedPayInfo;
7272
use crate::offers::merkle::{SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, self};
7373
use crate::offers::nonce::Nonce;
74-
use crate::offers::offer::{Offer, OfferContents, OfferId, OfferTlvStream, OfferTlvStreamRef};
74+
use crate::offers::offer::{Amount, Offer, OfferContents, OfferId, OfferTlvStream, OfferTlvStreamRef};
7575
use crate::offers::parse::{Bolt12ParseError, ParsedMessage, Bolt12SemanticError};
7676
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
7777
use crate::offers::signer::{Metadata, MetadataMaterial};
@@ -1158,8 +1158,12 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
11581158
return Err(Bolt12SemanticError::MissingAmount);
11591159
}
11601160

1161-
offer.check_quantity(quantity)?;
1161+
match offer.amount() {
1162+
Some(Amount::Currency {..}) => return Err(Bolt12SemanticError::UnsupportedCurrency),
1163+
_ => { },
1164+
};
11621165
offer.check_amount_msats_for_quantity(amount, quantity)?;
1166+
offer.check_quantity(quantity)?;
11631167

11641168
let features = features.unwrap_or_else(InvoiceRequestFeatures::empty);
11651169

lightning/src/offers/offer.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ macro_rules! offer_builder_methods { (
310310
/// Sets the [`Offer::amount`].
311311
///
312312
/// Successive calls to this method will override the previous setting.
313-
pub(super) fn amount($($self_mut)* $self: $self_type, amount: Amount) -> $return_type {
313+
pub(crate) fn amount($($self_mut)* $self: $self_type, amount: Amount) -> $return_type {
314314
$self.offer.amount = Some(amount);
315315
$return_value
316316
}
@@ -460,7 +460,7 @@ macro_rules! offer_builder_test_methods { (
460460
}
461461

462462
#[cfg_attr(c_bindings, allow(dead_code))]
463-
pub(super) fn build_unchecked($self: $self_type) -> Offer {
463+
pub(crate) fn build_unchecked($self: $self_type) -> Offer {
464464
$self.build_without_checks()
465465
}
466466
} }
@@ -857,9 +857,8 @@ impl OfferContents {
857857
&self, amount_msats: Option<u64>, quantity: Option<u64>
858858
) -> Result<(), Bolt12SemanticError> {
859859
let offer_amount_msats = match self.amount {
860-
None => 0,
861-
Some(Amount::Bitcoin { amount_msats }) => amount_msats,
862-
Some(Amount::Currency { .. }) => return Err(Bolt12SemanticError::UnsupportedCurrency),
860+
Some(Amount::Bitcoin {amount_msats}) => amount_msats,
861+
_ => 0,
863862
};
864863

865864
if !self.expects_quantity() || quantity.is_some() {

0 commit comments

Comments
 (0)