anya_core/bitcoin/
error.rs

1// [AIR-3][AIS-3][BPC-3][RES-3] Removed unused import: std::error::Error
2// Migrated from OPSource to anya-core
3// This file was automatically migrated as part of the Rust-only implementation
4// Original file: C:\Users\bmokoka\Downloads\OPSource\src\bitcoin\error.rs
5// Bitcoin Error Handling Module
6// Implements comprehensive error types and handling for Bitcoin operations
7//
8// [AIR-3][AIS-3][AIT-2][AIM-2][AIP-2][BPC-3][RES-3]
9// This module provides structured error types with comprehensive coverage
10// for all Bitcoin-related operations with good resilience characteristics.
11// Complete implementation as per official Bitcoin Improvement Proposals (BIPs) standards
12
13use bitcoin::key::FromSliceError;
14use bitcoin::secp256k1;
15use bitcoin::{
16    sighash::P2wpkhError, sighash::TaprootError, taproot::SigFromSliceError,
17    taproot::TaprootBuilder, taproot::TaprootBuilderError,
18};
19use hex::FromHexError;
20use thiserror::Error;
21
22/// Bitcoin operation errors
23#[derive(Debug, Error, Clone)]
24pub enum BitcoinError {
25    #[error("Failed to sign transaction")]
26    SigningError,
27
28    #[error("Failed to create Taproot output: {0}")]
29    TaprootError(String),
30
31    #[error("Failed to convert signature")]
32    SignatureConversionError,
33
34    #[error("Invalid sighash")]
35    InvalidSighash,
36
37    #[error("Invalid public key: {0}")]
38    InvalidPublicKey(String),
39
40    #[error("Invalid private key")]
41    InvalidPrivateKey,
42
43    #[error("Invalid script: {0}")]
44    InvalidScript(String),
45
46    #[error("Invalid address: {0}")]
47    InvalidAddress(String),
48
49    #[error("Insufficient funds")]
50    InsufficientFunds,
51
52    #[error("Transaction not found")]
53    TransactionNotFound,
54
55    #[error("Block not found")]
56    BlockNotFound,
57
58    #[error("Network error: {0}")]
59    NetworkError(String),
60
61    #[error("Wallet error: {0}")]
62    Wallet(String),
63
64    #[error("Lightning error: {0}")]
65    Lightning(String),
66
67    #[error("DLC error: {0}")]
68    DLC(String),
69
70    #[error("Secp256k1 error: {0}")]
71    Secp256k1Error(String),
72
73    #[error("Other error: {0}")]
74    Other(String),
75
76    #[error("Asset already issued")]
77    AssetAlreadyIssued,
78
79    #[error("Taproot builder error: {0}")]
80    TaprootBuilderError(TaprootBuilderError),
81
82    #[error("Invalid secret key")]
83    InvalidSecretKey,
84
85    #[error("Invalid witness")]
86    InvalidWitness,
87
88    #[error("Hex decoding error")]
89    HexDecodingError,
90
91    #[error("Key conversion error")]
92    KeyConversionError,
93
94    #[error("IO error: {0}")]
95    IOError(String),
96
97    #[error("Key error: {0}")]
98    KeyError(String),
99
100    #[error("Invalid transaction: {0}")]
101    InvalidTransaction(String),
102
103    #[error("Validation error: {0}")]
104    ValidationError(String),
105
106    #[error("Invalid signature: {0}")]
107    InvalidSignature(String),
108
109    #[error("Encoding error: {0}")]
110    EncodingError(String),
111
112    #[error("Script error: {0}")]
113    ScriptError(String),
114
115    #[error("Invalid oracle signature")]
116    InvalidOracleSignature,
117
118    #[error("P2WPKH error: {0}")]
119    P2wpkhError(String),
120
121    #[error("Invalid contract: {0}")]
122    InvalidContract(String),
123
124    #[error("Wallet not found: {0}")]
125    WalletNotFound(String),
126
127    #[error("Invalid wallet: {0}")]
128    InvalidWallet(String),
129
130    #[error("Transaction error: {0}")]
131    TransactionError(String),
132
133    // [AIR-3][AIS-3][BPC-3][RES-3] Invalid configuration error
134    // This follows official Bitcoin Improvement Proposals (BIPs) standards for configuration validation
135    #[error("Invalid configuration: {0}")]
136    InvalidConfiguration(String),
137
138    #[error("Storage error: {0}")]
139    StorageError(String),
140
141    #[error("Serialization error: {0}")]
142    SerializationError(String),
143
144    #[error("Authentication error: {0}")]
145    AuthenticationError(String),
146
147    #[error("Internal error: {0}")]
148    InternalError(String),
149
150    // [AIR-3][AIS-3][BPC-3][RES-3] Configuration error handling
151    // This follows official Bitcoin Improvement Proposals (BIPs) standards for error handling
152    #[error("Configuration error: {0}")]
153    ConfigError(String),
154
155    #[error("Key derivation error: {0}")]
156    KeyDerivation(String),
157}
158
159impl From<TaprootBuilderError> for BitcoinError {
160    fn from(err: TaprootBuilderError) -> Self {
161        BitcoinError::TaprootBuilderError(err)
162    }
163}
164
165impl From<secp256k1::Error> for BitcoinError {
166    fn from(error: secp256k1::Error) -> Self {
167        BitcoinError::Secp256k1Error(error.to_string())
168    }
169}
170
171impl From<TaprootBuilder> for BitcoinError {
172    fn from(_: TaprootBuilder) -> Self {
173        BitcoinError::TaprootError("Taproot builder error".to_string())
174    }
175}
176
177impl From<SigFromSliceError> for BitcoinError {
178    fn from(_: SigFromSliceError) -> Self {
179        BitcoinError::SignatureConversionError
180    }
181}
182
183impl From<FromHexError> for BitcoinError {
184    fn from(_: FromHexError) -> Self {
185        BitcoinError::HexDecodingError
186    }
187}
188
189impl From<FromSliceError> for BitcoinError {
190    fn from(_: FromSliceError) -> Self {
191        BitcoinError::KeyConversionError
192    }
193}
194
195// Implementation for TaprootError
196impl From<TaprootError> for BitcoinError {
197    fn from(error: TaprootError) -> Self {
198        BitcoinError::TaprootError(error.to_string())
199    }
200}
201
202impl From<P2wpkhError> for BitcoinError {
203    fn from(err: P2wpkhError) -> Self {
204        BitcoinError::P2wpkhError(err.to_string())
205    }
206}
207
208/// Result type for Bitcoin operations
209pub type BitcoinResult<T> = Result<T, BitcoinError>;