1use std::{io, sync::Arc};
4use thiserror::Error;
5use zcash_protocol::value::BalanceError;
6
7#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
11pub enum RandError {
12 #[error("failed to generate a secure stream of random bytes")]
14 FillBytes,
15}
16
17#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
19pub enum NoteCommitmentError {
20 #[error("Randomness generation failure")]
22 InsufficientRandomness(#[from] RandError),
23 #[error("failed to generate a sapling::NoteCommitment from a diversifier")]
25 InvalidDiversifier,
26}
27
28#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
31pub enum KeyError {
32 #[error("Randomness generation failure")]
34 InsufficientRandomness(#[from] RandError),
35}
36
37#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
40pub enum AddressError {
41 #[error("Randomness generation failure")]
43 InsufficientRandomness(#[from] RandError),
44 #[error("Randomness did not hash into the Jubjub group for producing a new diversifier")]
46 DiversifierGenerationFailure,
47}
48
49#[derive(Clone, Error, Debug)]
51pub enum Error {
52 #[error("invalid consensus branch id")]
54 InvalidConsensusBranchId,
55
56 #[error(transparent)]
58 Io(#[from] Arc<io::Error>),
59
60 #[error("the transaction is missing a network upgrade")]
62 MissingNetworkUpgrade,
63
64 #[error(transparent)]
66 Amount(#[from] BalanceError),
67
68 #[error("Zebra's type could not be converted to its librustzcash equivalent: {0}")]
70 Conversion(String),
71}
72
73impl From<io::Error> for Error {
76 fn from(value: io::Error) -> Self {
77 Arc::new(value).into()
78 }
79}
80
81impl PartialEq for Error {
84 fn eq(&self, other: &Self) -> bool {
85 match self {
86 Error::InvalidConsensusBranchId => matches!(other, Error::InvalidConsensusBranchId),
87 Error::Io(e) => {
88 if let Error::Io(o) = other {
89 e.to_string() == o.to_string()
92 } else {
93 false
94 }
95 }
96 Error::MissingNetworkUpgrade => matches!(other, Error::MissingNetworkUpgrade),
97 Error::Amount(e) => matches!(other, Error::Amount(o) if e == o),
98 Error::Conversion(e) => matches!(other, Error::Conversion(o) if e == o),
99 }
100 }
101}
102
103impl Eq for Error {}