-
Notifications
You must be signed in to change notification settings - Fork 431
Treat offer_amount of 0 as equivalent to None #4324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -405,6 +405,10 @@ macro_rules! offer_builder_methods { ( | |
| if amount_msats > MAX_VALUE_MSAT { | ||
| return Err(Bolt12SemanticError::InvalidAmount); | ||
| } | ||
| // An amount of 0 is equivalent to not setting an amount, so default to None. | ||
| if amount_msats == 0 { | ||
| $self.offer.amount = None; | ||
| } | ||
| }, | ||
| Some(Amount::Currency { .. }) => return Err(Bolt12SemanticError::UnsupportedCurrency), | ||
| None => {}, | ||
|
|
@@ -1309,8 +1313,12 @@ impl TryFrom<FullOfferTlvStream> for OfferContents { | |
| (None, Some(amount_msats)) if amount_msats > MAX_VALUE_MSAT => { | ||
| return Err(Bolt12SemanticError::InvalidAmount); | ||
| }, | ||
| // An amount of 0 is equivalent to not setting an amount, so default to None. | ||
| (None, Some(0)) => None, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm... I'd rather we not use the same representation for two different serializations. It may still work because when creating the |
||
| (None, Some(amount_msats)) => Some(Amount::Bitcoin { amount_msats }), | ||
| (Some(_), None) => return Err(Bolt12SemanticError::MissingAmount), | ||
| // An amount of 0 with a currency is equivalent to not setting an amount. | ||
| (Some(_), Some(0)) => None, | ||
|
Comment on lines
+1320
to
+1321
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This prevents the currency code check. Also, I don't think it's useful to drop information from the |
||
| (Some(currency_bytes), Some(amount)) => { | ||
| let iso4217_code = CurrencyCode::new(currency_bytes) | ||
| .map_err(|_| Bolt12SemanticError::InvalidCurrencyCode)?; | ||
|
|
@@ -1702,6 +1710,21 @@ mod tests { | |
| Ok(_) => panic!("expected error"), | ||
| Err(e) => assert_eq!(e, Bolt12SemanticError::InvalidAmount), | ||
| } | ||
|
|
||
| // An amount of 0 is equivalent to not setting an amount and should default to None. | ||
| let offer = OfferBuilder::new(pubkey(42)).amount_msats(0).build().unwrap(); | ||
| assert_eq!(offer.amount(), None); | ||
| assert_eq!(offer.as_tlv_stream().0.amount, None); | ||
|
|
||
| // Verify roundtrip: offer with amount=0 serializes and parses back with amount=None. | ||
| let serialized = offer.to_string(); | ||
| match serialized.parse::<Offer>() { | ||
| Ok(reparsed) => { | ||
| assert_eq!(reparsed.amount(), None); | ||
| assert_eq!(reparsed.as_tlv_stream().0.amount, None); | ||
| }, | ||
| Err(e) => panic!("error parsing offer: {:?}", e), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -1974,6 +1997,36 @@ mod tests { | |
| Bolt12ParseError::InvalidSemantics(Bolt12SemanticError::InvalidCurrencyCode) | ||
| ), | ||
| } | ||
|
|
||
| // An amount of 0 is equivalent to not setting an amount and should default to None. | ||
| let mut tlv_stream = offer.as_tlv_stream(); | ||
| tlv_stream.0.amount = Some(0); | ||
| tlv_stream.0.currency = None; | ||
|
|
||
| let mut encoded_offer = Vec::new(); | ||
| tlv_stream.write(&mut encoded_offer).unwrap(); | ||
|
|
||
| match Offer::try_from(encoded_offer) { | ||
| Ok(offer) => { | ||
| assert_eq!(offer.amount(), None); | ||
| }, | ||
| Err(e) => panic!("error parsing offer: {:?}", e), | ||
| } | ||
|
|
||
| // An amount of 0 with a currency is also equivalent to not setting an amount. | ||
| let mut tlv_stream = offer.as_tlv_stream(); | ||
| tlv_stream.0.amount = Some(0); | ||
| tlv_stream.0.currency = Some(b"USD"); | ||
|
|
||
| let mut encoded_offer = Vec::new(); | ||
| tlv_stream.write(&mut encoded_offer).unwrap(); | ||
|
|
||
| match Offer::try_from(encoded_offer) { | ||
| Ok(offer) => { | ||
| assert_eq!(offer.amount(), None); | ||
| }, | ||
| Err(e) => panic!("error parsing offer: {:?}", e), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm somewhat unsure about this change. It could hide an error on the user's end. It might be better to fail in this case instead of allowing them to accept any amount.