Skip to content

Commit 6da192b

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 50d21b7 commit 6da192b

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 8 additions & 3 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,13 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
11581158
return Err(Bolt12SemanticError::MissingAmount);
11591159
}
11601160

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

11641169
let features = features.unwrap_or_else(InvoiceRequestFeatures::empty);
11651170

lightning/src/offers/offer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
861860
Some(Amount::Bitcoin { amount_msats }) => amount_msats,
862-
Some(Amount::Currency { .. }) => return Err(Bolt12SemanticError::UnsupportedCurrency),
861+
_ => 0,
863862
};
864863

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

0 commit comments

Comments
 (0)