neo3/neo_wallets/wallet/
wallet_error.rs

1use crate::{
2	builder::{BuilderError, TransactionError},
3	crypto::CryptoError,
4	neo_clients::ProviderError,
5};
6use p256::ecdsa;
7use thiserror::Error;
8
9/// Errors that may occur within wallet operations.
10///
11/// This enum encompasses a range of errors that can arise when interacting with
12/// cryptocurrency wallets, including but not limited to account state issues, cryptographic
13/// errors, and IO operations. It is designed to be comprehensive, covering errors
14/// from underlying libraries (such as ECDSA operations, hex encoding/decoding) as well
15/// as wallet-specific errors like missing key pairs or default accounts.
16///
17/// # Variants
18///
19/// - `AccountState`: Represents errors related to an account's state, such as when an account
20///   cannot be found or is in an invalid state for the requested operation.
21/// - `NoKeyPair`: Indicates that a key pair was expected but none was found.
22/// - `EcdsaError`: Wraps errors from the `ecdsa` crate, typically related to signature operations.
23/// - `HexError`: Wraps errors from the `hex` crate, often arising during hex encoding or decoding.
24/// - `IoError`: Wraps standard IO errors that might occur during file operations.
25/// - `NoDefaultAccount`: Signals that a default account was expected but not set.
26/// - `InvalidKeyPair`: General error for when a key pair is invalid or fails validation.
27/// - `CryptoError`: Wraps cryptographic errors, potentially from operations like hashing or encryption.
28/// - `TransactionError`: Encapsulates errors that may occur during transaction creation or processing.
29/// - `BuilderError`: Wraps errors that occur during the construction of complex objects, possibly due to invalid parameters.
30/// - `DecryptionError`: Indicates an error occurred during account decryption.
31/// - `SigningError`: Indicates an error occurred during transaction signing.
32/// - `FileError`: Indicates an error occurred during file operations.
33/// - `ParseError`: Indicates an error occurred during parsing operations.
34/// - `ImportError`: Indicates an error occurred during key import operations.
35/// - `InvalidPassword`: Indicates that an invalid password was provided.
36/// - `NoAccounts`: Indicates that no accounts were found in the wallet.
37/// - `YubiHsmError`: Wraps errors related to YubiHSM operations.
38/// - `ProviderError`: Wraps errors from the RPC provider.
39///
40/// # Examples
41///
42/// Handling a `WalletError` might look like this:
43///
44/// ```
45/// # use neo3::prelude::*;
46/// # fn main() -> Result<(), wallets::WalletError> {
47/// let result = some_wallet_operation();
48///     match result {
49///         Ok(_) => println!("Operation successful"),
50///         Err(wallets::WalletError::NoKeyPair) => println!("Key pair missing"),
51///         Err(e) => println!("An error occurred: {:?}", e),
52///     }
53/// #    Ok(())
54/// # }
55/// # fn some_wallet_operation() -> Result<(), wallets::WalletError> {
56/// #    Err(wallets::WalletError::NoKeyPair)
57/// # }
58/// ```
59///
60/// This approach allows for precise error handling and reporting, facilitating debugging and user feedback.
61#[derive(Error, Debug)]
62pub enum WalletError {
63	/// Error indicating an issue with the account's state, such as being locked or
64	/// insufficient funds. The contained message provides additional detail.
65	#[error("Account state error: {0}")]
66	AccountState(String),
67
68	/// Indicates that no key pair is available for a cryptographic operation, possibly
69	/// because it has not been generated or imported.
70	#[error("No key pair")]
71	NoKeyPair,
72
73	/// Wraps errors from the `ecdsa` crate, related to ECDSA signature operations.
74	/// This could include errors during signature generation or verification.
75	#[error(transparent)]
76	EcdsaError(#[from] ecdsa::Error),
77
78	/// Represents errors encountered during hex encoding or decoding operations,
79	/// such as an invalid hex character or incorrect string length.
80	#[error(transparent)]
81	HexError(#[from] hex::FromHexError),
82
83	/// Encapsulates errors arising from IO operations, like reading from or writing to
84	/// files. This includes file not found, permissions issues, and other file-related errors.
85	#[error(transparent)]
86	IoError(#[from] std::io::Error),
87
88	/// Signifies that the wallet does not have a designated default account, which might
89	/// be required for certain operations or configurations.
90	#[error("No default account")]
91	NoDefaultAccount,
92
93	/// Used when a key pair is found to be invalid, such as when a private key does not
94	/// match the public key, or the key pair cannot be used for signing due to corruption.
95	#[error("Invalid key pair")]
96	SignHashError,
97
98	/// Wraps generic cryptographic errors that might occur during operations such as
99	/// encryption, decryption, hashing, or key generation.
100	#[error(transparent)]
101	CryptoError(#[from] CryptoError),
102
103	/// Covers errors related to the creation, signing, or broadcasting of transactions,
104	/// including invalid transaction formats, insufficient gas, or nonce issues.
105	#[error(transparent)]
106	TransactionError(#[from] TransactionError),
107
108	/// Indicates issues encountered during the construction or configuration of wallet
109	/// components, such as invalid parameters or configurations that cannot be applied.
110	#[error(transparent)]
111	BuilderError(#[from] BuilderError),
112
113	/// Indicates an invalid signature
114	#[error("Invalid signature")]
115	VerifyError,
116
117	/// Errors related to Ledger hardware wallet operations
118	#[error("Ledger error: {0}")]
119	LedgerError(String),
120
121	/// Error indicating no accounts in wallet
122	#[error("No accounts in wallet")]
123	NoAccounts,
124
125	/// Errors related to YubiHSM operations
126	#[error("YubiHSM error: {0}")]
127	YubiHsmError(String),
128
129	/// Errors from the RPC provider
130	#[error(transparent)]
131	ProviderError(#[from] ProviderError),
132
133	/// Errors during account decryption
134	#[error("Decryption error: {0}")]
135	DecryptionError(String),
136
137	/// Errors during transaction signing
138	#[error("Signing error: {0}")]
139	SigningError(String),
140
141	/// Errors during file operations
142	#[error("File error: {0}")]
143	FileError(String),
144
145	/// Errors during parsing operations
146	#[error("Parse error: {0}")]
147	ParseError(String),
148
149	/// Errors during key import operations
150	#[error("Import error: {0}")]
151	ImportError(String),
152
153	/// Invalid password provided
154	#[error("Invalid password")]
155	InvalidPassword,
156
157	/// Errors during deserialization
158	#[error("Deserialization error: {0}")]
159	DeserializationError(String),
160}