Skip to content

Commit 13c3c06

Browse files
committed
Refactor amount checks and moved currency check
Modified check_amount_msats_for_quantity to trust the user-provided msats amount and moved the currency check to the InvoiceRequest parsing code. Updated fails_creating_or_paying_for_offer_without_connected_peers and fails_creating_invoice_request_without_blinded_reply_path offers tests to use fiat amounts, bypassing insufficient liquidity errors.
1 parent 4e98b8b commit 13c3c06

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

lightning/src/ln/offers_tests.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use crate::offers::invoice::Bolt12Invoice;
5454
use crate::offers::invoice_error::InvoiceError;
5555
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
5656
use crate::offers::parse::Bolt12SemanticError;
57+
use crate::offers::offer::{Amount};
5758
use crate::onion_message::messenger::PeeledOnion;
5859
use crate::onion_message::offers::OffersMessage;
5960
use crate::onion_message::packet::ParsedOnionMessageContents;
@@ -1267,15 +1268,15 @@ fn fails_creating_or_paying_for_offer_without_connected_peers() {
12671268
reconnect_nodes(args);
12681269

12691270
let offer = alice.node
1270-
.create_offer_builder(Some(absolute_expiry)).unwrap()
1271-
.amount_msats(10_000_000)
1272-
.build().unwrap();
1271+
.create_offer_builder(None).unwrap()
1272+
.amount(Amount::Currency {iso4217_code: *b"USD", amount: 6_000})
1273+
.build_unchecked();
12731274

12741275
let payment_id = PaymentId([1; 32]);
12751276

12761277
match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
12771278
Ok(_) => panic!("Expected error"),
1278-
Err(e) => assert_eq!(e, Bolt12SemanticError::InsufficientLiquidity),
1279+
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
12791280
}
12801281

12811282
assert!(nodes[0].node.list_recent_payments().is_empty());
@@ -1431,14 +1432,15 @@ fn fails_creating_invoice_request_without_blinded_reply_path() {
14311432

14321433
let offer = alice.node
14331434
.create_offer_builder(None).unwrap()
1434-
.amount_msats(10_000_000)
1435-
.build().unwrap();
1435+
.amount(Amount::Currency {iso4217_code: *b"USD", amount: 6_000})
1436+
.build_unchecked();
1437+
14361438

14371439
let payment_id = PaymentId([1; 32]);
14381440

14391441
match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
14401442
Ok(_) => panic!("Expected error"),
1441-
Err(e) => assert_eq!(e, Bolt12SemanticError::InsufficientLiquidity),
1443+
Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
14421444
}
14431445

14441446
assert!(nodes[0].node.list_recent_payments().is_empty());

lightning/src/offers/invoice_request.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
7272
use crate::ln::msgs::DecodeError;
7373
use crate::offers::invoice::BlindedPayInfo;
7474
use crate::offers::merkle::{SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, self};
75-
use crate::offers::offer::{Offer, OfferContents, OfferId, OfferTlvStream, OfferTlvStreamRef};
75+
use crate::offers::offer::{Amount, Offer, OfferContents, OfferId, OfferTlvStream, OfferTlvStreamRef};
7676
use crate::offers::parse::{Bolt12ParseError, ParsedMessage, Bolt12SemanticError};
7777
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
7878
use crate::offers::signer::{Metadata, MetadataMaterial};
@@ -1133,6 +1133,14 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
11331133
offer.check_quantity(quantity)?;
11341134
offer.check_amount_msats_for_quantity(amount, quantity)?;
11351135

1136+
let amount_msats = match offer.amount() {
1137+
None => amount,
1138+
Some(Amount::Bitcoin { amount_msats }) => Some(amount_msats),
1139+
Some(Amount::Currency { iso4217_code: _ , amount: _ }) => {
1140+
return Err(Bolt12SemanticError::UnsupportedCurrency)
1141+
},
1142+
};
1143+
11361144
let features = features.unwrap_or_else(InvoiceRequestFeatures::empty);
11371145

11381146
let payer_id = match payer_id {
@@ -1146,7 +1154,7 @@ impl TryFrom<PartialInvoiceRequestTlvStream> for InvoiceRequestContents {
11461154

11471155
Ok(InvoiceRequestContents {
11481156
inner: InvoiceRequestContentsWithoutPayerId {
1149-
payer, offer, chain, amount_msats: amount, features, quantity, payer_note,
1157+
payer, offer, chain, amount_msats, features, quantity, payer_note,
11501158
},
11511159
payer_id,
11521160
})

lightning/src/offers/offer.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ macro_rules! offer_builder_methods { (
309309
/// Sets the [`Offer::amount`].
310310
///
311311
/// Successive calls to this method will override the previous setting.
312-
pub(super) fn amount($($self_mut)* $self: $self_type, amount: Amount) -> $return_type {
312+
pub(crate) fn amount($($self_mut)* $self: $self_type, amount: Amount) -> $return_type {
313313
$self.offer.amount = Some(amount);
314314
$return_value
315315
}
@@ -453,7 +453,7 @@ macro_rules! offer_builder_test_methods { (
453453
}
454454

455455
#[cfg_attr(c_bindings, allow(dead_code))]
456-
pub(super) fn build_unchecked($self: $self_type) -> Offer {
456+
pub(crate) fn build_unchecked($self: $self_type) -> Offer {
457457
$self.build_without_checks()
458458
}
459459
} }
@@ -854,11 +854,7 @@ impl OfferContents {
854854
pub(super) fn check_amount_msats_for_quantity(
855855
&self, amount_msats: Option<u64>, quantity: Option<u64>
856856
) -> Result<(), Bolt12SemanticError> {
857-
let offer_amount_msats = match self.amount {
858-
None => 0,
859-
Some(Amount::Bitcoin { amount_msats }) => amount_msats,
860-
Some(Amount::Currency { .. }) => return Err(Bolt12SemanticError::UnsupportedCurrency),
861-
};
857+
let offer_amount_msats = amount_msats.unwrap_or(0);
862858

863859
if !self.expects_quantity() || quantity.is_some() {
864860
let expected_amount_msats = offer_amount_msats.checked_mul(quantity.unwrap_or(1))

0 commit comments

Comments
 (0)