anychain_core/
public_key.rs

1use {
2    crate::{
3        address::{Address, AddressError},
4        format::Format,
5        no_std::{
6            fmt::{Debug, Display},
7            FromStr, String,
8        },
9    },
10    thiserror::Error,
11};
12
13/// Generic public key.
14pub trait PublicKey: Clone + Debug + Display + FromStr + Send + Sync + 'static + Sized {
15    type SecretKey;
16    type Address: Address;
17    type Format: Format;
18
19    /// Returns a public key given an secp256k1 secret key.
20    fn from_secret_key(secret_key: &Self::SecretKey) -> Self;
21
22    /// Returns an address corresponding to this public key.
23    fn to_address(&self, format: &Self::Format) -> Result<Self::Address, AddressError>;
24}
25
26#[derive(Debug, Error)]
27pub enum PublicKeyError {
28    #[error("{0}: {1}")]
29    Crate(&'static str, String),
30
31    #[error("invalid byte length: {0}")]
32    InvalidByteLength(usize),
33
34    #[error("invalid character length: {0}")]
35    InvalidCharacterLength(usize),
36
37    #[error("invalid public key prefix: {0}")]
38    InvalidPrefix(String),
39
40    #[error("no public spending key found")]
41    NoSpendingKey,
42
43    #[error("no public viewing key found")]
44    NoViewingKey,
45}
46
47impl From<crate::no_std::io::Error> for PublicKeyError {
48    fn from(error: crate::no_std::io::Error) -> Self {
49        PublicKeyError::Crate("crate::no_std::io", format!("{:?}", error))
50    }
51}
52
53impl From<base58::FromBase58Error> for PublicKeyError {
54    fn from(error: base58::FromBase58Error) -> Self {
55        PublicKeyError::Crate("base58", format!("{:?}", error))
56    }
57}
58
59impl From<bech32::Error> for PublicKeyError {
60    fn from(error: bech32::Error) -> Self {
61        PublicKeyError::Crate("bech32", format!("{:?}", error))
62    }
63}
64
65impl From<hex::FromHexError> for PublicKeyError {
66    fn from(error: hex::FromHexError) -> Self {
67        PublicKeyError::Crate("hex", format!("{:?}", error))
68    }
69}
70
71// impl From<libsecp256k1::Error> for PublicKeyError {
72//     fn from(error: libsecp256k1::Error) -> Self {
73//         PublicKeyError::Crate("libsecp256k1", format!("{:?}", error))
74//     }
75// }