Skip to main content

bsv_rs/
error.rs

1//! Error types for the BSV SDK.
2
3use thiserror::Error;
4
5/// Main error type for the BSV SDK.
6#[derive(Error, Debug, Clone, PartialEq, Eq)]
7pub enum Error {
8    // ===================
9    // Primitives errors
10    // ===================
11    /// Invalid key length for cryptographic operations.
12    #[error("invalid key length: expected {expected}, got {actual}")]
13    InvalidKeyLength { expected: usize, actual: usize },
14
15    /// Invalid data length for cryptographic operations.
16    #[error("invalid data length: expected {expected}, got {actual}")]
17    InvalidDataLength { expected: usize, actual: usize },
18
19    /// Invalid hex string.
20    #[error("invalid hex string: {0}")]
21    InvalidHex(String),
22
23    /// Invalid base58 string.
24    #[error("invalid base58 string: {0}")]
25    InvalidBase58(String),
26
27    /// Invalid base64 string.
28    #[error("invalid base64 string: {0}")]
29    InvalidBase64(String),
30
31    /// Cryptographic operation failed.
32    #[error("cryptographic operation failed: {0}")]
33    CryptoError(String),
34
35    /// Invalid signature.
36    #[error("invalid signature: {0}")]
37    InvalidSignature(String),
38
39    /// Invalid public key.
40    #[error("invalid public key: {0}")]
41    InvalidPublicKey(String),
42
43    /// Invalid private key.
44    #[error("invalid private key: {0}")]
45    InvalidPrivateKey(String),
46
47    /// Point at infinity (invalid for most operations).
48    #[error("point at infinity")]
49    PointAtInfinity,
50
51    /// Decryption failed.
52    #[error("decryption failed")]
53    DecryptionFailed,
54
55    /// Invalid nonce.
56    #[error("invalid nonce: {0}")]
57    InvalidNonce(String),
58
59    /// Invalid tag (for authenticated encryption).
60    #[error("invalid authentication tag")]
61    InvalidTag,
62
63    /// Invalid UTF-8 sequence.
64    #[error("invalid UTF-8 sequence: {0}")]
65    InvalidUtf8(String),
66
67    /// Reader underflow (not enough bytes to read).
68    #[error("reader underflow: need {needed} bytes, only {available} available")]
69    ReaderUnderflow { needed: usize, available: usize },
70
71    /// Invalid checksum.
72    #[error("invalid checksum")]
73    InvalidChecksum,
74
75    // ===================
76    // Script errors (Phase S1+)
77    // ===================
78    /// Script parse error.
79    #[cfg(feature = "script")]
80    #[error("script parse error: {0}")]
81    ScriptParseError(String),
82
83    /// Script execution error.
84    #[cfg(feature = "script")]
85    #[error("script execution error: {0}")]
86    ScriptExecutionError(String),
87
88    /// Invalid opcode.
89    #[cfg(feature = "script")]
90    #[error("invalid opcode: 0x{0:02x}")]
91    InvalidOpcode(u8),
92
93    /// Disabled opcode.
94    #[cfg(feature = "script")]
95    #[error("disabled opcode: 0x{0:02x}")]
96    DisabledOpcode(u8),
97
98    /// Stack underflow.
99    #[cfg(feature = "script")]
100    #[error("stack underflow")]
101    StackUnderflow,
102
103    /// Stack overflow.
104    #[cfg(feature = "script")]
105    #[error("stack overflow")]
106    StackOverflow,
107
108    /// BIP-276 encoding/decoding error.
109    #[cfg(feature = "script")]
110    #[error("BIP-276 error: {0}")]
111    Bip276Error(String),
112
113    /// Invalid address.
114    #[cfg(feature = "script")]
115    #[error("invalid address: {0}")]
116    InvalidAddress(String),
117
118    /// Invalid address length.
119    #[cfg(feature = "script")]
120    #[error("invalid address length for '{0}'")]
121    InvalidAddressLength(String),
122
123    /// Unsupported address type.
124    #[cfg(feature = "script")]
125    #[error("address not supported {0}")]
126    UnsupportedAddress(String),
127
128    // ===================
129    // Transaction errors
130    // ===================
131    /// Transaction error.
132    #[cfg(feature = "transaction")]
133    #[error("transaction error: {0}")]
134    TransactionError(String),
135
136    /// MerklePath error.
137    #[cfg(feature = "transaction")]
138    #[error("merkle path error: {0}")]
139    MerklePathError(String),
140
141    /// BEEF format error.
142    #[cfg(feature = "transaction")]
143    #[error("BEEF error: {0}")]
144    BeefError(String),
145
146    /// Fee model error.
147    #[cfg(feature = "transaction")]
148    #[error("fee model error: {0}")]
149    FeeModelError(String),
150
151    // ===================
152    // Wallet errors
153    // ===================
154    /// General wallet error.
155    #[cfg(feature = "wallet")]
156    #[error("wallet error: {0}")]
157    WalletError(String),
158
159    /// Key derivation error.
160    #[cfg(feature = "wallet")]
161    #[error("key derivation error: {0}")]
162    KeyDerivationError(String),
163
164    /// Protocol validation error.
165    #[cfg(feature = "wallet")]
166    #[error("protocol validation error: {0}")]
167    ProtocolValidationError(String),
168
169    /// Invalid counterparty.
170    #[cfg(feature = "wallet")]
171    #[error("invalid counterparty: {0}")]
172    InvalidCounterparty(String),
173
174    // ===================
175    // Messages errors
176    // ===================
177    /// Message version mismatch.
178    #[cfg(feature = "messages")]
179    #[error("message version mismatch: expected {expected}, got {actual}")]
180    MessageVersionMismatch { expected: String, actual: String },
181
182    /// General message error.
183    #[cfg(feature = "messages")]
184    #[error("message error: {0}")]
185    MessageError(String),
186
187    /// Message recipient mismatch.
188    #[cfg(feature = "messages")]
189    #[error("message recipient mismatch: expected {expected}, got {actual}")]
190    MessageRecipientMismatch { expected: String, actual: String },
191
192    // ===================
193    // Compat errors
194    // ===================
195    /// Invalid mnemonic phrase.
196    #[cfg(feature = "compat")]
197    #[error("invalid mnemonic: {0}")]
198    InvalidMnemonic(String),
199
200    /// Invalid entropy length for mnemonic generation.
201    #[cfg(feature = "compat")]
202    #[error("invalid entropy length: expected {expected}, got {actual}")]
203    InvalidEntropyLength { expected: String, actual: usize },
204
205    /// Invalid word in mnemonic phrase.
206    #[cfg(feature = "compat")]
207    #[error("invalid word in mnemonic: {0}")]
208    InvalidMnemonicWord(String),
209
210    /// Invalid extended key.
211    #[cfg(feature = "compat")]
212    #[error("invalid extended key: {0}")]
213    InvalidExtendedKey(String),
214
215    /// Cannot derive hardened child from public key.
216    #[cfg(feature = "compat")]
217    #[error("cannot derive hardened child from public key")]
218    HardenedFromPublic,
219
220    /// Invalid derivation path.
221    #[cfg(feature = "compat")]
222    #[error("invalid derivation path: {0}")]
223    InvalidDerivationPath(String),
224
225    /// ECIES decryption failed.
226    #[cfg(feature = "compat")]
227    #[error("ECIES decryption failed: {0}")]
228    EciesDecryptionFailed(String),
229
230    /// ECIES HMAC verification failed.
231    #[cfg(feature = "compat")]
232    #[error("ECIES HMAC verification failed")]
233    EciesHmacMismatch,
234
235    // ===================
236    // Auth errors
237    // ===================
238    /// General authentication error.
239    #[cfg(feature = "auth")]
240    #[error("authentication error: {0}")]
241    AuthError(String),
242
243    /// Session not found.
244    #[cfg(feature = "auth")]
245    #[error("session not found: {0}")]
246    SessionNotFound(String),
247
248    /// Certificate validation failed.
249    #[cfg(feature = "auth")]
250    #[error("certificate validation failed: {0}")]
251    CertificateValidationError(String),
252
253    /// Transport error.
254    #[cfg(feature = "auth")]
255    #[error("transport error: {0}")]
256    TransportError(String),
257
258    // ===================
259    // Overlay errors
260    // ===================
261    /// General overlay error.
262    #[cfg(feature = "overlay")]
263    #[error("overlay error: {0}")]
264    OverlayError(String),
265
266    /// No hosts found for service.
267    #[cfg(feature = "overlay")]
268    #[error("no hosts found for service: {0}")]
269    NoHostsFound(String),
270
271    /// Broadcast to overlay failed.
272    #[cfg(feature = "overlay")]
273    #[error("overlay broadcast failed: {0}")]
274    OverlayBroadcastFailed(String),
275
276    // ===================
277    // Registry errors
278    // ===================
279    /// General registry error.
280    #[cfg(feature = "registry")]
281    #[error("registry error: {0}")]
282    RegistryError(String),
283
284    /// Definition not found in registry.
285    #[cfg(feature = "registry")]
286    #[error("definition not found: {0}")]
287    DefinitionNotFound(String),
288
289    /// Invalid definition data.
290    #[cfg(feature = "registry")]
291    #[error("invalid definition data: {0}")]
292    InvalidDefinitionData(String),
293
294    // ===================
295    // KVStore errors
296    // ===================
297    /// General kvstore error.
298    #[cfg(feature = "kvstore")]
299    #[error("kvstore error: {0}")]
300    KvStoreError(String),
301
302    /// Key not found in kvstore.
303    #[cfg(feature = "kvstore")]
304    #[error("kvstore key not found: {0}")]
305    KvStoreKeyNotFound(String),
306
307    /// Corrupted kvstore state.
308    #[cfg(feature = "kvstore")]
309    #[error("corrupted kvstore state: {0}")]
310    KvStoreCorruptedState(String),
311
312    /// Empty context (protocol_id) provided to kvstore.
313    #[cfg(feature = "kvstore")]
314    #[error("context cannot be empty")]
315    KvStoreEmptyContext,
316
317    /// Invalid key provided to kvstore (empty).
318    #[cfg(feature = "kvstore")]
319    #[error("invalid key")]
320    KvStoreInvalidKey,
321
322    /// Invalid value provided to kvstore (empty).
323    #[cfg(feature = "kvstore")]
324    #[error("invalid value")]
325    KvStoreInvalidValue,
326
327    // ===================
328    // Identity errors
329    // ===================
330    /// General identity error.
331    #[cfg(feature = "identity")]
332    #[error("identity error: {0}")]
333    IdentityError(String),
334
335    /// Identity not found.
336    #[cfg(feature = "identity")]
337    #[error("identity not found: {0}")]
338    IdentityNotFound(String),
339
340    /// Contact not found.
341    #[cfg(feature = "identity")]
342    #[error("contact not found: {0}")]
343    ContactNotFound(String),
344}
345
346/// Result type alias for BSV SDK operations.
347pub type Result<T> = std::result::Result<T, Error>;