diff --git a/zcash_primitives/src/transaction/builder.rs b/zcash_primitives/src/transaction/builder.rs index 5c0e643..a2ea4af 100644 --- a/zcash_primitives/src/transaction/builder.rs +++ b/zcash_primitives/src/transaction/builder.rs @@ -79,7 +79,7 @@ impl SaplingOutput { Some(g_d) => g_d, None => return Err(Error(ErrorKind::InvalidAddress)), }; - if value.0 < 0 { + if value.is_negative() { return Err(Error(ErrorKind::InvalidAmount)); } @@ -288,7 +288,7 @@ impl Builder { to: &TransparentAddress, value: Amount, ) -> Result<(), Error> { - if value.0 < 0 { + if value.is_negative() { return Err(Error(ErrorKind::InvalidAmount)); } diff --git a/zcash_primitives/src/transaction/components/amount.rs b/zcash_primitives/src/transaction/components/amount.rs index 593e98c..eedfe2a 100644 --- a/zcash_primitives/src/transaction/components/amount.rs +++ b/zcash_primitives/src/transaction/components/amount.rs @@ -8,6 +8,11 @@ const MAX_MONEY: i64 = 21_000_000 * COIN; pub struct Amount(pub i64); impl Amount { + /// Returns a zero-valued Amount. + pub const fn zero() -> Self { + Amount(0) + } + // Read an Amount from a signed 64-bit little-endian integer. pub fn read_i64(mut reader: R, allow_negative: bool) -> io::Result { let amount = reader.read_i64::()?; @@ -39,6 +44,18 @@ impl Amount { )) } } + + /// Returns `true` if `self` is positive and `false` if the number is zero or + /// negative. + pub const fn is_positive(self) -> bool { + self.0.is_positive() + } + + /// Returns `true` if `self` is negative and `false` if the number is zero or + /// positive. + pub const fn is_negative(self) -> bool { + self.0.is_negative() + } } #[cfg(test)] diff --git a/zcash_primitives/src/transaction/mod.rs b/zcash_primitives/src/transaction/mod.rs index 19f4396..d8024eb 100644 --- a/zcash_primitives/src/transaction/mod.rs +++ b/zcash_primitives/src/transaction/mod.rs @@ -112,7 +112,7 @@ impl TransactionData { vout: vec![], lock_time: 0, expiry_height: 0, - value_balance: Amount(0), + value_balance: Amount::zero(), shielded_spends: vec![], shielded_outputs: vec![], joinsplits: vec![], @@ -190,7 +190,7 @@ impl Transaction { let so = Vector::read(&mut reader, OutputDescription::read)?; (vb, ss, so) } else { - (Amount(0), vec![], vec![]) + (Amount::zero(), vec![], vec![]) }; let (joinsplits, joinsplit_pubkey, joinsplit_sig) = if version >= 2 {