ledger_zcash_builder/
errors.rs

1/*******************************************************************************
2*   (c) 2022-2024 Zondax AG
3*
4*  Licensed under the Apache License, Version 2.0 (the "License");
5*  you may not use this file except in compliance with the License.
6*  You may obtain a copy of the License at
7*
8*      http://www.apache.org/licenses/LICENSE-2.0
9*
10*  Unless required by applicable law or agreed to in writing, software
11*  distributed under the License is distributed on an "AS IS" BASIS,
12*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*  See the License for the specific language governing permissions and
14*  limitations under the License.
15********************************************************************************/
16use std::error;
17use std::fmt;
18
19#[derive(Debug, PartialEq)]
20pub enum Error {
21    AnchorMismatch,
22    BindingSig,
23    ChangeIsNegative,
24    InvalidAddress,
25    InvalidAddressFormat,
26    InvalidAddressHash,
27    InvalidAmount,
28    NoChangeAddress,
29    SpendProof,
30    MissingSpendSig,
31    SpendSig,
32    InvalidSpendSig,
33    NoSpendSig,
34    TransparentSig,
35    Finalization,
36    MinShieldedOutputs,
37    BuilderNoKeys,
38    ReadWriteError,
39    InvalidOVKHashSeed,
40    AlreadyAuthorized,
41    Unauthorized,
42    UnknownAuthorization,
43}
44
45impl fmt::Display for Error {
46    fn fmt(
47        &self,
48        f: &mut fmt::Formatter,
49    ) -> fmt::Result {
50        match self {
51            Error::AnchorMismatch => {
52                write!(f, "Anchor mismatch (anchors for all spends must be equal)")
53            },
54            Error::BindingSig => write!(f, "Failed to create bindingSig"),
55            Error::ChangeIsNegative => write!(f, "Change is negative"),
56            Error::InvalidAddress => write!(f, "Invalid address"),
57            Error::InvalidAmount => write!(f, "Invalid amount"),
58            Error::NoChangeAddress => {
59                write!(f, "No change address specified or discoverable")
60            },
61            Error::SpendProof => {
62                write!(f, "Failed to create Sapling spend proof")
63            },
64            Error::MissingSpendSig => {
65                write!(f, "Missing Sapling spend signature(s)")
66            },
67            Error::SpendSig => {
68                write!(f, "Failed to get Sapling spend signature")
69            },
70            Error::InvalidSpendSig => {
71                write!(f, "Sapling spend signature failed to verify")
72            },
73            Error::NoSpendSig => {
74                write!(f, "No Sapling spend signatures")
75            },
76            Error::InvalidAddressFormat => {
77                write!(f, "Incorrect format of address")
78            },
79            Error::InvalidAddressHash => write!(f, "Incorrect hash of address"),
80            Error::TransparentSig => {
81                write!(f, "Failed to sign transparent inputs")
82            },
83            Error::Finalization => {
84                write!(f, "Failed to build complete transaction")
85            },
86            Error::MinShieldedOutputs => {
87                write!(f, "Not enough shielded outputs for transaction")
88            },
89            Error::BuilderNoKeys => {
90                write!(f, "Builder does not have any keys set")
91            },
92            Error::ReadWriteError => {
93                write!(f, "Error writing/reading bytes to/from vector")
94            },
95            Error::InvalidOVKHashSeed => {
96                write!(f, "Error: either OVK or hash_seed should be some")
97            },
98            Error::AlreadyAuthorized => {
99                write!(f, "Error: operation not available after authorization")
100            },
101            Error::Unauthorized => {
102                write!(f, "Error: operation not available without authorization")
103            },
104            Error::UnknownAuthorization => {
105                write!(f, "Error: authorization status unknown")
106            },
107        }
108    }
109}
110
111impl error::Error for Error {}
112
113impl From<std::io::Error> for Error {
114    fn from(e: std::io::Error) -> Error {
115        Error::ReadWriteError
116    }
117}