Skip to content

Commit

Permalink
Fix incorrect zip321 amount parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttycom committed Mar 6, 2024
1 parent 6b4942f commit 376db46
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 5 additions & 0 deletions zcash_client_backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions zcash_client_backend/src/zip321.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down

0 comments on commit 376db46

Please sign in to comment.