rusk_wallet/
error.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use std::io;
8use std::str::Utf8Error;
9
10use inquire::InquireError;
11use rand::Error as RngError;
12
13use crate::gql::GraphQLError;
14
15/// Errors returned by this library
16#[derive(Debug, thiserror::Error)]
17pub enum Error {
18    /// Command not available in offline mode
19    #[error("This command cannot be performed while offline")]
20    Offline,
21    /// Unauthorized access to this address
22    #[error("Unauthorized access to this address")]
23    Unauthorized,
24    /// Rusk error
25    #[error("Rusk error occurred: {0}")]
26    Rusk(String),
27    /// Filesystem errors
28    #[error(transparent)]
29    IO(#[from] io::Error),
30    /// JSON serialization errors
31    #[error(transparent)]
32    Json(#[from] serde_json::Error),
33    /// Bytes encoding errors
34    #[error("A serialization error occurred: {0:?}")]
35    Bytes(dusk_bytes::Error),
36    /// Base58 errors
37    #[error(transparent)]
38    Base58(#[from] bs58::decode::Error),
39    /// Rkyv errors
40    #[error("A serialization error occurred.")]
41    Rkyv,
42    /// Error creating HTTP client
43    #[error("Cannot create HTTP client")]
44    HttpClient,
45    /// Reqwest errors
46    #[error("A request error occurred: {0}")]
47    Reqwest(#[from] reqwest::Error),
48    /// Utf8 errors
49    #[error("Utf8 error: {0:?}")]
50    Utf8(Utf8Error),
51    /// Random number generator errors
52    #[error(transparent)]
53    Rng(#[from] RngError),
54    /// Not enough balance to perform transaction
55    #[error("Insufficient balance to perform this operation")]
56    NotEnoughBalance,
57    /// Amount to transfer/stake cannot be zero
58    #[error("Amount to transfer/stake cannot be zero")]
59    AmountIsZero,
60    /// Note combination for the given value is impossible given the maximum
61    /// amount of inputs in a transaction
62    #[error("Impossible notes' combination for the given value is")]
63    NoteCombinationProblem,
64    /// The note wasn't found in the note-tree of the transfer-contract
65    #[error("Note wasn't found in transfer-contract")]
66    NoteNotFound,
67    /// The note couldn't be decrypted with the provided ViewKey
68    #[error("Note couldn't be decrypted with the provided ViewKey")]
69    WrongViewKey,
70    /// Not enough gas to perform this transaction
71    #[error("Not enough gas to perform this transaction")]
72    NotEnoughGas,
73    /// A stake does not exist for this key
74    #[error("A stake does not exist for this key")]
75    NotStaked,
76    /// No reward available for this key
77    #[error("No reward available for this key")]
78    NoReward,
79    /// Invalid address
80    #[error("Invalid address")]
81    BadAddress,
82    /// Address does not belong to this wallet
83    #[error("Address does not belong to this wallet")]
84    AddressNotOwned,
85    /// No menu item selected
86    #[error("No menu item selected")]
87    NoMenuItemSelected,
88    /// Mnemonic phrase is not valid
89    #[error("Invalid mnemonic phrase")]
90    InvalidMnemonicPhrase,
91    /// Path provided is not a directory
92    #[error("Path provided is not a directory")]
93    NotDirectory,
94    /// Cannot get the path to the $HOME directory
95    #[error("OS not supported")]
96    OsNotSupported,
97    /// Wallet file content is not valid
98    #[error("Wallet file content is not valid")]
99    WalletFileCorrupted,
100    /// File version not recognized
101    #[error("File version {0}.{1} not recognized")]
102    UnknownFileVersion(u8, u8),
103    /// A wallet file with this name already exists
104    #[error("A wallet file with this name already exists")]
105    WalletFileExists,
106    /// Wallet file is missing
107    #[error("Wallet file is missing")]
108    WalletFileMissing,
109    /// Wrong wallet password
110    #[error("Invalid password")]
111    BlockMode(#[from] block_modes::BlockModeError),
112    /// Reached the maximum number of attempts
113    #[error("Reached the maximum number of attempts")]
114    AttemptsExhausted,
115    /// Status callback needs to be set before connecting
116    #[error("Status callback needs to be set before connecting")]
117    StatusWalletConnected,
118    /// Transaction error
119    #[error("Transaction error: {0}")]
120    Transaction(String),
121    /// Rocksdb cache database error
122    #[error("Rocks cache database error: {0}")]
123    RocksDB(rocksdb::Error),
124    /// Provided Network not found
125    #[error(
126        "Network not found, check config.toml, specify network with -n flag"
127    )]
128    NetworkNotFound,
129    /// The cache database couldn't find column family required
130    #[error("Cache database corrupted")]
131    CacheDatabaseCorrupted,
132    /// Prover errors from dusk-core
133    #[error("Prover Error: {0}")]
134    ProverError(String),
135    /// Memo provided is too large
136    #[error("Memo too large {0}")]
137    MemoTooLarge(usize),
138    /// Expected BLS Key
139    #[error("Expected BLS Public Key")]
140    ExpectedBlsPublicKey,
141    /// Expected Phoenix public key
142    #[error("Expected Phoenix public Key")]
143    ExpectedPhoenixPublicKey,
144    /// Addresses use different transaction models
145    #[error("Addresses use different transaction models")]
146    DifferentTransactionModels,
147    /// Invalid contract id provided
148    #[error("Invalid contractID provided")]
149    InvalidContractId,
150    /// Contract file location not found
151    #[error("Invalid WASM contract path provided")]
152    InvalidWasmContractPath,
153    /// Invalid environment variable value
154    #[error("Invalid environment variable value {0}")]
155    InvalidEnvVar(String),
156    /// Conversion error
157    #[error("Conversion error: {0}")]
158    Conversion(String),
159    /// GraphQL error
160    #[error("GraphQL error: {0}")]
161    GraphQLError(GraphQLError),
162    /// Inquire error
163    #[error("Inquire error: {0}")]
164    InquireError(String),
165    /// Error while querying archival node
166    #[error("Archive node query error: {0}")]
167    ArchiveJsonError(String),
168}
169
170impl From<dusk_bytes::Error> for Error {
171    fn from(e: dusk_bytes::Error) -> Self {
172        Self::Bytes(e)
173    }
174}
175
176impl From<block_modes::InvalidKeyIvLength> for Error {
177    fn from(_: block_modes::InvalidKeyIvLength) -> Self {
178        Self::WalletFileCorrupted
179    }
180}
181
182impl From<dusk_core::Error> for Error {
183    fn from(e: dusk_core::Error) -> Self {
184        use dusk_core::Error::*;
185
186        match e {
187            InsufficientBalance => Self::NotEnoughBalance,
188            Replay => Self::Transaction("Replay".to_string()),
189            PhoenixOwnership => Self::AddressNotOwned,
190            PhoenixCircuit(s) | PhoenixProver(s) => Self::ProverError(s),
191            InvalidData => Self::Bytes(dusk_bytes::Error::InvalidData),
192            BadLength(found, expected) => {
193                Self::Bytes(dusk_bytes::Error::BadLength { found, expected })
194            }
195            InvalidChar(ch, index) => {
196                Self::Bytes(dusk_bytes::Error::InvalidChar { ch, index })
197            }
198            Rkyv(_) => Self::Rkyv,
199            MemoTooLarge(m) => Self::MemoTooLarge(m),
200        }
201    }
202}
203
204impl From<rocksdb::Error> for Error {
205    fn from(e: rocksdb::Error) -> Self {
206        Self::RocksDB(e)
207    }
208}
209
210impl From<GraphQLError> for Error {
211    fn from(e: GraphQLError) -> Self {
212        Self::GraphQLError(e)
213    }
214}
215
216impl From<InquireError> for Error {
217    fn from(e: InquireError) -> Self {
218        Self::InquireError(e.to_string())
219    }
220}