Skip to main content

tronz_primitives/
error.rs

1//! Error types for `tronz-primitives`.
2
3/// Errors produced when parsing or validating a TRON [`Address`](crate::Address).
4#[derive(thiserror::Error, Debug)]
5#[non_exhaustive]
6pub enum AddressError {
7    /// The raw address did not start with the mainnet prefix byte `0x41`.
8    #[error("invalid prefix byte: expected 0x41, got 0x{0:02x}")]
9    BadPrefix(u8),
10
11    /// The decoded byte slice was not the expected length.
12    #[error("bad length: expected {expected} bytes, got {got}")]
13    BadLength {
14        /// Number of bytes that were expected.
15        expected: usize,
16        /// Number of bytes that were actually provided.
17        got: usize,
18    },
19
20    /// base58check decoding failed (bad checksum or invalid characters).
21    #[error("base58 decode failed: {0}")]
22    Base58(#[from] bs58::decode::Error),
23
24    /// hex decoding failed.
25    #[error("hex decode failed: {0}")]
26    Hex(#[from] hex::FromHexError),
27}
28
29/// Errors produced when constructing a [`Trx`](crate::Trx) amount.
30#[derive(thiserror::Error, Debug)]
31#[non_exhaustive]
32pub enum AmountError {
33    /// A negative value was supplied where only non-negative amounts are valid.
34    #[error("negative amount is invalid: {0} sun")]
35    Negative(i64),
36
37    /// Converting a floating-point TRX value overflowed the `i64` sun range.
38    #[error("amount out of range: {0} TRX cannot be represented in sun")]
39    OutOfRange(f64),
40}
41
42/// Errors produced when constructing a [`RecoverableSignature`](crate::RecoverableSignature).
43#[derive(thiserror::Error, Debug)]
44#[non_exhaustive]
45pub enum SignatureError {
46    /// The signature byte slice was not exactly 65 bytes (`r || s || v`).
47    #[error("bad signature length: expected 65 bytes, got {0}")]
48    BadLength(usize),
49
50    /// The recovery id byte was not `0` or `1` (after normalising `27`/`28`).
51    #[error("invalid recovery id: {0}")]
52    BadRecoveryId(u8),
53
54    /// The underlying ECDSA library rejected the signature scalars.
55    #[error("ecdsa error: {0}")]
56    Ecdsa(#[from] k256::ecdsa::Error),
57}