wagyu_model/
extended_private_key.rs

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