Skip to main content

bsv_primitives/
error.rs

1/// Unified error type for all primitives operations.
2///
3/// Covers errors from hashing, EC operations, encryption, encoding, and key management.
4#[derive(Debug, thiserror::Error)]
5pub enum PrimitivesError {
6    /// Invalid private key data.
7    #[error("invalid private key: {0}")]
8    InvalidPrivateKey(String),
9
10    /// Invalid public key data.
11    #[error("invalid public key: {0}")]
12    InvalidPublicKey(String),
13
14    /// Invalid signature data.
15    #[error("invalid signature: {0}")]
16    InvalidSignature(String),
17
18    /// Malformed WIF-encoded key.
19    #[error("invalid WIF format: {0}")]
20    InvalidWif(String),
21
22    /// Base58Check checksum did not match.
23    #[error("checksum mismatch")]
24    ChecksumMismatch,
25
26    /// EC point is not on the secp256k1 curve.
27    #[error("point not on curve")]
28    PointNotOnCurve,
29
30    /// Key length does not match the expected size.
31    #[error("invalid key length: expected {expected}, got {got}")]
32    InvalidKeyLength {
33        /// Expected length in bytes.
34        expected: usize,
35        /// Actual length in bytes.
36        got: usize,
37    },
38
39    /// AES-GCM encryption failed.
40    #[error("encryption error: {0}")]
41    EncryptionError(String),
42
43    /// AES-GCM decryption or authentication failed.
44    #[error("decryption error: {0}")]
45    DecryptionError(String),
46
47    /// Invalid hexadecimal string.
48    #[error("invalid hex: {0}")]
49    InvalidHex(String),
50
51    /// Invalid hash value.
52    #[error("invalid hash: {0}")]
53    InvalidHash(String),
54
55    /// Invalid Base58 encoding.
56    #[error("invalid base58: {0}")]
57    InvalidBase58(String),
58
59    /// Not enough Shamir shares to reconstruct the secret.
60    #[error("insufficient shares for recovery: need {threshold}, got {got}")]
61    InsufficientShares {
62        /// Minimum shares required.
63        threshold: usize,
64        /// Shares actually provided.
65        got: usize,
66    },
67
68    /// Shamir threshold value is invalid.
69    #[error("invalid threshold: {0}")]
70    InvalidThreshold(String),
71
72    /// Duplicate Shamir share index.
73    #[error("duplicate share detected")]
74    DuplicateShare,
75
76    /// Variable-length integer exceeds maximum size.
77    #[error("varint too large")]
78    VarIntTooLarge,
79
80    /// Unexpected end of input data.
81    #[error("unexpected end of data")]
82    UnexpectedEof,
83
84    /// Hex decoding error.
85    #[error("hex decode error: {0}")]
86    HexDecode(#[from] hex::FromHexError),
87
88    /// Elliptic curve error (from k256).
89    #[error("elliptic curve error: {0}")]
90    EllipticCurve(#[from] k256::elliptic_curve::Error),
91
92    /// ECDSA signature error (from k256/signature).
93    #[error("ecdsa error: {0}")]
94    Ecdsa(#[from] k256::ecdsa::signature::Error),
95
96    /// AEAD (AES-GCM) error.
97    #[error("aead error")]
98    Aead,
99
100    /// Catch-all error.
101    #[error("{0}")]
102    Other(String),
103}
104
105impl From<aes_gcm::Error> for PrimitivesError {
106    fn from(_: aes_gcm::Error) -> Self {
107        PrimitivesError::Aead
108    }
109}