Skip to main content

bsv/wallet/
error.rs

1//! Error types for the wallet module.
2
3use thiserror::Error;
4
5/// Unified error type for all wallet operations.
6#[derive(Debug, Error)]
7pub enum WalletError {
8    /// Protocol-level error with a numeric code and message.
9    #[error("protocol error (code {code}): {message}")]
10    Protocol { code: u8, message: String },
11
12    /// Invalid parameter supplied to a wallet operation.
13    #[error("invalid parameter: {0}")]
14    InvalidParameter(String),
15
16    /// Requested functionality is not yet implemented.
17    #[error("not implemented: {0}")]
18    NotImplemented(String),
19
20    /// Internal error wrapping lower-level failures.
21    #[error("internal error: {0}")]
22    Internal(String),
23
24    /// Insufficient funds for the requested action.
25    /// Matches TS SDK `WERR_INSUFFICIENT_FUNDS`.
26    #[error("insufficient funds: {0}")]
27    InsufficientFunds(String),
28
29    /// User must review and approve pending actions.
30    /// Matches TS SDK `WERR_REVIEW_ACTIONS`.
31    #[error("review actions: {0}")]
32    ReviewActions(String),
33
34    /// HMAC verification failed.
35    /// Matches TS SDK behavior which throws on invalid HMAC rather than returning false.
36    #[error("HMAC is not valid")]
37    InvalidHmac,
38
39    /// Signature verification failed.
40    /// Matches TS SDK behavior which throws on invalid signature rather than returning false.
41    #[error("signature is not valid")]
42    InvalidSignature,
43}
44
45impl From<crate::primitives::PrimitivesError> for WalletError {
46    fn from(e: crate::primitives::PrimitivesError) -> Self {
47        WalletError::Internal(e.to_string())
48    }
49}
50
51impl From<crate::script::ScriptError> for WalletError {
52    fn from(e: crate::script::ScriptError) -> Self {
53        WalletError::Internal(e.to_string())
54    }
55}
56
57impl From<crate::transaction::TransactionError> for WalletError {
58    fn from(e: crate::transaction::TransactionError) -> Self {
59        WalletError::Internal(e.to_string())
60    }
61}
62
63impl From<std::io::Error> for WalletError {
64    fn from(e: std::io::Error) -> Self {
65        WalletError::Internal(e.to_string())
66    }
67}