Skip to content

Commit 910229f

Browse files
committed
f - check invalid amount
1 parent bc2a01e commit 910229f

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

lightning/src/offers/offer.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use core::ops::{Bound, RangeBounds};
6161
use core::time::Duration;
6262
use io;
6363
use ln::features::OfferFeatures;
64+
use ln::msgs::MAX_VALUE_MSAT;
6465
use onion_message::BlindedPath;
6566
use util::ser::{Writeable, Writer};
6667

@@ -204,6 +205,14 @@ impl OfferBuilder {
204205

205206
/// Builds an [`Offer`] from the builder's settings.
206207
pub fn build(self) -> Result<Offer, ()> {
208+
if let Some(Amount::Currency { .. }) = self.offer.amount {
209+
return Err(());
210+
}
211+
212+
if self.offer.amount_msats() > MAX_VALUE_MSAT {
213+
return Err(());
214+
}
215+
207216
if self.offer.quantity_min() > self.offer.quantity_max() {
208217
return Err(());
209218
}
@@ -332,6 +341,10 @@ impl Offer {
332341
}
333342

334343
impl OfferContents {
344+
pub fn amount_msats(&self) -> u64 {
345+
self.amount.as_ref().map(Amount::as_msats).unwrap_or(0)
346+
}
347+
335348
pub fn quantity_min(&self) -> u64 {
336349
self.quantity_min.unwrap_or(1)
337350
}
@@ -395,6 +408,16 @@ pub enum Amount {
395408
},
396409
}
397410

411+
impl Amount {
412+
/// Returns the amount in millisatoshi.
413+
pub fn as_msats(&self) -> u64 {
414+
match self {
415+
Amount::Currency { .. } => unimplemented!(),
416+
Amount::Bitcoin { amount_msats } => *amount_msats,
417+
}
418+
}
419+
}
420+
398421
/// An ISO 4712 three-letter currency code (e.g., USD).
399422
pub type CurrencyCode = [u8; 3];
400423

@@ -423,6 +446,7 @@ mod tests {
423446
use core::num::NonZeroU64;
424447
use core::time::Duration;
425448
use ln::features::OfferFeatures;
449+
use ln::msgs::MAX_VALUE_MSAT;
426450
use onion_message::{BlindedHop, BlindedPath};
427451
use util::ser::Writeable;
428452

@@ -535,14 +559,16 @@ mod tests {
535559
assert_eq!(tlv_stream.amount, Some(1000));
536560
assert_eq!(tlv_stream.currency, None);
537561

538-
let offer = OfferBuilder::new("foo".into(), pubkey(42))
539-
.amount(currency_amount.clone())
540-
.build()
541-
.unwrap();
542-
let tlv_stream = offer.as_tlv_stream();
543-
assert_eq!(offer.amount(), Some(&currency_amount));
562+
let builder = OfferBuilder::new("foo".into(), pubkey(42))
563+
.amount(currency_amount.clone());
564+
let tlv_stream = builder.offer.as_tlv_stream();
565+
assert_eq!(builder.offer.amount.as_ref(), Some(&currency_amount));
544566
assert_eq!(tlv_stream.amount, Some(10));
545567
assert_eq!(tlv_stream.currency, Some(b"USD"));
568+
match builder.build() {
569+
Ok(_) => panic!("expected error"),
570+
Err(e) => assert_eq!(e, ()),
571+
}
546572

547573
let offer = OfferBuilder::new("foo".into(), pubkey(42))
548574
.amount(currency_amount.clone())
@@ -552,6 +578,12 @@ mod tests {
552578
let tlv_stream = offer.as_tlv_stream();
553579
assert_eq!(tlv_stream.amount, Some(1000));
554580
assert_eq!(tlv_stream.currency, None);
581+
582+
let invalid_amount = Amount::Bitcoin { amount_msats: MAX_VALUE_MSAT + 1 };
583+
match OfferBuilder::new("foo".into(), pubkey(42)).amount(invalid_amount).build() {
584+
Ok(_) => panic!("expected error"),
585+
Err(e) => assert_eq!(e, ()),
586+
}
555587
}
556588

557589
#[test]

0 commit comments

Comments
 (0)