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    /// A varint using more bytes than its value requires. Width includes the marker.
72    #[error("non-canonical varint: {value} encoded in {width} bytes")]
73    NonCanonicalVarInt { width: u8, value: u64 },
74
75    /// A node CompactSize above `MAX_COMPACT_SIZE`.
76    #[error("compact size too large: {0}")]
77    CompactSizeTooLarge(u64),
78
79    /// Invalid checksum.
80    #[error("invalid checksum")]
81    InvalidChecksum,
82
83    // ===================
84    // Script errors (Phase S1+)
85    // ===================
86    /// Script parse error.
87    #[cfg(feature = "script")]
88    #[error("script parse error: {0}")]
89    ScriptParseError(String),
90
91    /// Script execution error.
92    #[cfg(feature = "script")]
93    #[error("script execution error: {0}")]
94    ScriptExecutionError(String),
95
96    /// Invalid opcode.
97    #[cfg(feature = "script")]
98    #[error("invalid opcode: 0x{0:02x}")]
99    InvalidOpcode(u8),
100
101    /// Disabled opcode.
102    #[cfg(feature = "script")]
103    #[error("disabled opcode: 0x{0:02x}")]
104    DisabledOpcode(u8),
105
106    /// Stack underflow.
107    #[cfg(feature = "script")]
108    #[error("stack underflow")]
109    StackUnderflow,
110
111    /// Stack overflow.
112    #[cfg(feature = "script")]
113    #[error("stack overflow")]
114    StackOverflow,
115
116    /// BIP-276 encoding/decoding error.
117    #[cfg(feature = "script")]
118    #[error("BIP-276 error: {0}")]
119    Bip276Error(String),
120
121    /// Invalid address.
122    #[cfg(feature = "script")]
123    #[error("invalid address: {0}")]
124    InvalidAddress(String),
125
126    /// Invalid address length.
127    #[cfg(feature = "script")]
128    #[error("invalid address length for '{0}'")]
129    InvalidAddressLength(String),
130
131    /// Unsupported address type.
132    #[cfg(feature = "script")]
133    #[error("address not supported {0}")]
134    UnsupportedAddress(String),
135
136    // ===================
137    // Transaction errors
138    // ===================
139    /// Transaction error.
140    #[cfg(feature = "transaction")]
141    #[error("transaction error: {0}")]
142    TransactionError(String),
143
144    /// MerklePath error.
145    #[cfg(feature = "transaction")]
146    #[error("merkle path error: {0}")]
147    MerklePathError(String),
148
149    /// BEEF format error.
150    #[cfg(feature = "transaction")]
151    #[error("BEEF error: {0}")]
152    BeefError(String),
153
154    /// Fee model error.
155    #[cfg(feature = "transaction")]
156    #[error("fee model error: {0}")]
157    FeeModelError(String),
158
159    // ===================
160    // Wallet errors
161    // ===================
162    /// General wallet error.
163    #[cfg(feature = "wallet")]
164    #[error("wallet error: {0}")]
165    WalletError(String),
166
167    /// Key derivation error.
168    #[cfg(feature = "wallet")]
169    #[error("key derivation error: {0}")]
170    KeyDerivationError(String),
171
172    /// Protocol validation error.
173    #[cfg(feature = "wallet")]
174    #[error("protocol validation error: {0}")]
175    ProtocolValidationError(String),
176
177    /// Invalid counterparty.
178    #[cfg(feature = "wallet")]
179    #[error("invalid counterparty: {0}")]
180    InvalidCounterparty(String),
181
182    // ===================
183    // Messages errors
184    // ===================
185    /// Message version mismatch.
186    #[cfg(feature = "messages")]
187    #[error("message version mismatch: expected {expected}, got {actual}")]
188    MessageVersionMismatch { expected: String, actual: String },
189
190    /// General message error.
191    #[cfg(feature = "messages")]
192    #[error("message error: {0}")]
193    MessageError(String),
194
195    /// Message recipient mismatch.
196    #[cfg(feature = "messages")]
197    #[error("message recipient mismatch: expected {expected}, got {actual}")]
198    MessageRecipientMismatch { expected: String, actual: String },
199
200    // ===================
201    // Compat errors
202    // ===================
203    /// Invalid mnemonic phrase.
204    #[cfg(feature = "compat")]
205    #[error("invalid mnemonic: {0}")]
206    InvalidMnemonic(String),
207
208    /// Invalid entropy length for mnemonic generation.
209    #[cfg(feature = "compat")]
210    #[error("invalid entropy length: expected {expected}, got {actual}")]
211    InvalidEntropyLength { expected: String, actual: usize },
212
213    /// Invalid word in mnemonic phrase.
214    #[cfg(feature = "compat")]
215    #[error("invalid word in mnemonic: {0}")]
216    InvalidMnemonicWord(String),
217
218    /// Invalid extended key.
219    #[cfg(feature = "compat")]
220    #[error("invalid extended key: {0}")]
221    InvalidExtendedKey(String),
222
223    /// Cannot derive hardened child from public key.
224    #[cfg(feature = "compat")]
225    #[error("cannot derive hardened child from public key")]
226    HardenedFromPublic,
227
228    /// Invalid derivation path.
229    #[cfg(feature = "compat")]
230    #[error("invalid derivation path: {0}")]
231    InvalidDerivationPath(String),
232
233    /// ECIES decryption failed.
234    #[cfg(feature = "compat")]
235    #[error("ECIES decryption failed: {0}")]
236    EciesDecryptionFailed(String),
237
238    /// ECIES HMAC verification failed.
239    #[cfg(feature = "compat")]
240    #[error("ECIES HMAC verification failed")]
241    EciesHmacMismatch,
242
243    // ===================
244    // Auth errors
245    // ===================
246    /// General authentication error.
247    #[cfg(feature = "auth")]
248    #[error("authentication error: {0}")]
249    AuthError(String),
250
251    /// Session not found.
252    #[cfg(feature = "auth")]
253    #[error("session not found: {0}")]
254    SessionNotFound(String),
255
256    /// Certificate validation failed.
257    #[cfg(feature = "auth")]
258    #[error("certificate validation failed: {0}")]
259    CertificateValidationError(String),
260
261    /// Transport error.
262    #[cfg(feature = "auth")]
263    #[error("transport error: {0}")]
264    TransportError(String),
265
266    // ===================
267    // Overlay errors
268    // ===================
269    /// General overlay error.
270    #[cfg(feature = "overlay")]
271    #[error("overlay error: {0}")]
272    OverlayError(String),
273
274    /// No hosts found for service.
275    #[cfg(feature = "overlay")]
276    #[error("no hosts found for service: {0}")]
277    NoHostsFound(String),
278
279    /// Broadcast to overlay failed.
280    #[cfg(feature = "overlay")]
281    #[error("overlay broadcast failed: {0}")]
282    OverlayBroadcastFailed(String),
283
284    // ===================
285    // Registry errors
286    // ===================
287    /// General registry error.
288    #[cfg(feature = "registry")]
289    #[error("registry error: {0}")]
290    RegistryError(String),
291
292    /// Definition not found in registry.
293    #[cfg(feature = "registry")]
294    #[error("definition not found: {0}")]
295    DefinitionNotFound(String),
296
297    /// Invalid definition data.
298    #[cfg(feature = "registry")]
299    #[error("invalid definition data: {0}")]
300    InvalidDefinitionData(String),
301
302    // ===================
303    // KVStore errors
304    // ===================
305    /// General kvstore error.
306    #[cfg(feature = "kvstore")]
307    #[error("kvstore error: {0}")]
308    KvStoreError(String),
309
310    /// Key not found in kvstore.
311    #[cfg(feature = "kvstore")]
312    #[error("kvstore key not found: {0}")]
313    KvStoreKeyNotFound(String),
314
315    /// Corrupted kvstore state.
316    #[cfg(feature = "kvstore")]
317    #[error("corrupted kvstore state: {0}")]
318    KvStoreCorruptedState(String),
319
320    /// Empty context (protocol_id) provided to kvstore.
321    #[cfg(feature = "kvstore")]
322    #[error("context cannot be empty")]
323    KvStoreEmptyContext,
324
325    /// Invalid key provided to kvstore (empty).
326    #[cfg(feature = "kvstore")]
327    #[error("invalid key")]
328    KvStoreInvalidKey,
329
330    /// Invalid value provided to kvstore (empty).
331    #[cfg(feature = "kvstore")]
332    #[error("invalid value")]
333    KvStoreInvalidValue,
334
335    // ===================
336    // Identity errors
337    // ===================
338    /// General identity error.
339    #[cfg(feature = "identity")]
340    #[error("identity error: {0}")]
341    IdentityError(String),
342
343    /// Identity not found.
344    #[cfg(feature = "identity")]
345    #[error("identity not found: {0}")]
346    IdentityNotFound(String),
347
348    /// Contact not found.
349    #[cfg(feature = "identity")]
350    #[error("contact not found: {0}")]
351    ContactNotFound(String),
352}
353
354/// Result type alias for BSV SDK operations.
355pub type Result<T> = std::result::Result<T, Error>;