From 376db4684b012e89fae8ef16535359f92223698f Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Tue, 5 Mar 2024 21:45:12 -0700 Subject: [PATCH] Fix incorrect zip321 amount parsing. --- zcash_client_backend/CHANGELOG.md | 5 +++++ zcash_client_backend/src/zip321.rs | 8 ++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/zcash_client_backend/CHANGELOG.md b/zcash_client_backend/CHANGELOG.md index cb89963662..87b13bab85 100644 --- a/zcash_client_backend/CHANGELOG.md +++ b/zcash_client_backend/CHANGELOG.md @@ -64,6 +64,11 @@ and this library adheres to Rust's notion of - `zcash_client_backend::PoolType::is_receiver`: use `zcash_keys::Address::has_receiver` instead. +### Fixed +- This release fixes an error in amount parsing in `zip321` that previously + allowed amounts having a decimal point but no decimal value to be parsed + as valid. + ## [0.11.0] - 2024-03-01 ### Added diff --git a/zcash_client_backend/src/zip321.rs b/zcash_client_backend/src/zip321.rs index 871faad192..bd020bb3e1 100644 --- a/zcash_client_backend/src/zip321.rs +++ b/zcash_client_backend/src/zip321.rs @@ -458,7 +458,7 @@ mod parse { use nom::{ bytes::complete::{tag, take_till}, character::complete::{alpha1, char, digit0, digit1, one_of}, - combinator::{map_opt, map_res, opt, recognize}, + combinator::{all_consuming, map_opt, map_res, opt, recognize}, sequence::{preceded, separated_pair, tuple}, AsChar, IResult, InputTakeAtPosition, }; @@ -642,13 +642,13 @@ mod parse { /// Parses a value in decimal ZEC. pub fn parse_amount(input: &str) -> IResult<&str, NonNegativeAmount> { map_res( - tuple(( + all_consuming(tuple(( digit1, opt(preceded( char('.'), map_opt(digit1, |s: &str| if s.len() > 8 { None } else { Some(s) }), )), - )), + ))), |(whole_s, decimal_s): (&str, Option<&str>)| { let coins: u64 = whole_s .to_string() @@ -667,7 +667,7 @@ mod parse { .and_then(|coin_zats| coin_zats.checked_add(zats)) .ok_or(BalanceError::Overflow) .and_then(NonNegativeAmount::from_u64) - .map_err(|_| format!("Not a valid amount: {}.{:0>8} ZEC", coins, zats)) + .map_err(|_| format!("Not a valid amount: {} ZEC", input)) }, )(input) }