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