wagyu_model/
extended_public_key.rs

1use crate::address::{Address, AddressError};
2use crate::derivation_path::{DerivationPath, DerivationPathError};
3use crate::extended_private_key::ExtendedPrivateKey;
4use crate::format::Format;
5use crate::network::NetworkError;
6use crate::public_key::{PublicKey, PublicKeyError};
7
8use std::{
9    fmt::{Debug, Display},
10    str::FromStr,
11};
12
13/// The interface for a generic extended public key.
14pub trait ExtendedPublicKey: Clone + Debug + Display + FromStr + Send + Sync + 'static + Eq + Sized {
15    type Address: Address;
16    type DerivationPath: DerivationPath;
17    type ExtendedPrivateKey: ExtendedPrivateKey;
18    type Format: Format;
19    type PublicKey: PublicKey;
20
21    /// Returns the extended public key of the corresponding extended private key.
22    fn from_extended_private_key(extended_private_key: &Self::ExtendedPrivateKey) -> Self;
23
24    /// Returns the extended public key for the given derivation path.
25    fn derive(&self, path: &Self::DerivationPath) -> Result<Self, ExtendedPublicKeyError>;
26
27    /// Returns the public key of the corresponding extended public key.
28    fn to_public_key(&self) -> Self::PublicKey;
29
30    /// Returns the address of the corresponding extended public key.
31    fn to_address(&self, format: &Self::Format) -> Result<Self::Address, AddressError>;
32}
33
34#[derive(Debug, Fail)]
35pub enum ExtendedPublicKeyError {
36    #[fail(display = "{}: {}", _0, _1)]
37    Crate(&'static str, String),
38
39    #[fail(display = "{}", _0)]
40    DerivationPathError(DerivationPathError),
41
42    #[fail(display = "invalid byte length: {}", _0)]
43    InvalidByteLength(usize),
44
45    #[fail(
46        display = "invalid extended private key checksum: {{ expected: {:?}, found: {:?} }}",
47        _0, _1
48    )]
49    InvalidChecksum(String, String),
50
51    #[fail(display = "invalid child number: {{ expected: {:?}, found: {:?} }}", _0, _1)]
52    InvalidChildNumber(u32, u32),
53
54    #[fail(display = "invalid version bytes: {:?}", _0)]
55    InvalidVersionBytes(Vec<u8>),
56
57    #[fail(display = "maximum child depth reached: {}", _0)]
58    MaximumChildDepthReached(u8),
59
60    #[fail(display = "{}", _0)]
61    Message(String),
62
63    #[fail(display = "{}", _0)]
64    NetworkError(NetworkError),
65
66    #[fail(display = "{}", _0)]
67    PublicKeyError(PublicKeyError),
68
69    #[fail(display = "unsupported format: {}", _0)]
70    UnsupportedFormat(String),
71}
72
73impl From<DerivationPathError> for ExtendedPublicKeyError {
74    fn from(error: DerivationPathError) -> Self {
75        ExtendedPublicKeyError::DerivationPathError(error)
76    }
77}
78
79impl From<NetworkError> for ExtendedPublicKeyError {
80    fn from(error: NetworkError) -> Self {
81        ExtendedPublicKeyError::NetworkError(error)
82    }
83}
84
85impl From<PublicKeyError> for ExtendedPublicKeyError {
86    fn from(error: PublicKeyError) -> Self {
87        ExtendedPublicKeyError::PublicKeyError(error)
88    }
89}
90
91impl From<base58::FromBase58Error> for ExtendedPublicKeyError {
92    fn from(error: base58::FromBase58Error) -> Self {
93        ExtendedPublicKeyError::Crate("base58", format!("{:?}", error))
94    }
95}
96
97impl From<bech32::Error> for ExtendedPublicKeyError {
98    fn from(error: bech32::Error) -> Self {
99        ExtendedPublicKeyError::Crate("bech32", format!("{:?}", error))
100    }
101}
102
103impl From<crypto_mac::InvalidKeyLength> for ExtendedPublicKeyError {
104    fn from(error: crypto_mac::InvalidKeyLength) -> Self {
105        ExtendedPublicKeyError::Crate("crypto-mac", format!("{:?}", error))
106    }
107}
108
109impl From<secp256k1::Error> for ExtendedPublicKeyError {
110    fn from(error: secp256k1::Error) -> Self {
111        ExtendedPublicKeyError::Crate("secp256k1", format!("{:?}", error))
112    }
113}
114
115impl From<std::array::TryFromSliceError> for ExtendedPublicKeyError {
116    fn from(error: std::array::TryFromSliceError) -> Self {
117        ExtendedPublicKeyError::Crate("std::array", format!("{:?}", error))
118    }
119}
120
121impl From<std::io::Error> for ExtendedPublicKeyError {
122    fn from(error: std::io::Error) -> Self {
123        ExtendedPublicKeyError::Crate("std::io", format!("{:?}", error))
124    }
125}
126
127impl From<std::num::ParseIntError> for ExtendedPublicKeyError {
128    fn from(error: std::num::ParseIntError) -> Self {
129        ExtendedPublicKeyError::Crate("std::num", format!("{:?}", error))
130    }
131}