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 NoteError {
20 #[error("Randomness generation failure")]
22 InsufficientRandomness(#[from] RandError),
23 #[error("failed to generate an Orchard note's rho.")]
25 InvalidRho,
26}
27
28#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
30pub enum NoteCommitmentError {
31 #[error("Randomness generation failure")]
33 InsufficientRandomness(#[from] RandError),
34 #[error("failed to generate a sapling::NoteCommitment from a diversifier")]
36 InvalidDiversifier,
37}
38
39#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
42pub enum KeyError {
43 #[error("Randomness generation failure")]
45 InsufficientRandomness(#[from] RandError),
46}
47
48#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
51pub enum AddressError {
52 #[error("Randomness generation failure")]
54 InsufficientRandomness(#[from] RandError),
55 #[error("Randomness did not hash into the Jubjub group for producing a new diversifier")]
57 DiversifierGenerationFailure,
58}
59
60#[derive(Clone, Error, Debug)]
62pub enum Error {
63 #[error("invalid consensus branch id")]
65 InvalidConsensusBranchId,
66
67 #[error(transparent)]
69 Io(#[from] Arc<io::Error>),
70
71 #[error("the transaction is missing a network upgrade")]
73 MissingNetworkUpgrade,
74
75 #[error(transparent)]
77 Amount(#[from] BalanceError),
78
79 #[error("Zebra's type could not be converted to its librustzcash equivalent: {0}")]
81 Conversion(String),
82}
83
84impl From<io::Error> for Error {
87 fn from(value: io::Error) -> Self {
88 Arc::new(value).into()
89 }
90}
91
92impl PartialEq for Error {
95 fn eq(&self, other: &Self) -> bool {
96 match self {
97 Error::InvalidConsensusBranchId => matches!(other, Error::InvalidConsensusBranchId),
98 Error::Io(e) => {
99 if let Error::Io(o) = other {
100 e.to_string() == o.to_string()
103 } else {
104 false
105 }
106 }
107 Error::MissingNetworkUpgrade => matches!(other, Error::MissingNetworkUpgrade),
108 Error::Amount(e) => matches!(other, Error::Amount(o) if e == o),
109 Error::Conversion(e) => matches!(other, Error::Conversion(o) if e == o),
110 }
111 }
112}
113
114impl Eq for Error {}