1use crate::enc::XKeyEncoder;
2
3#[cfg(feature = "mainnet")]
5pub type Encoder = crate::enc::MainnetEncoder;
6
7#[cfg(feature = "testnet")]
9pub type Encoder = crate::enc::TestnetEncoder;
10
11impl std::str::FromStr for crate::xkeys::XPriv {
12 type Err = crate::Bip32Error;
13
14 fn from_str(s: &str) -> Result<Self, Self::Err> {
15 Encoder::xpriv_from_base58(s)
16 }
17}
18
19impl std::str::FromStr for crate::xkeys::XPub {
20 type Err = crate::Bip32Error;
21
22 fn from_str(s: &str) -> Result<Self, Self::Err> {
23 Encoder::xpub_from_base58(s)
24 }
25}
26
27impl serde::Serialize for crate::xkeys::XPub {
28 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
29 where
30 S: serde::Serializer,
31 {
32 let encoded =
33 Encoder::xpub_to_base58(self).map_err(|e| serde::ser::Error::custom(e.to_string()))?;
34 serializer.serialize_str(&encoded)
35 }
36}
37
38impl<'de> serde::Deserialize<'de> for crate::xkeys::XPub {
39 fn deserialize<D>(deserializer: D) -> Result<crate::xkeys::XPub, D::Error>
40 where
41 D: serde::Deserializer<'de>,
42 {
43 let s: &str = serde::Deserialize::deserialize(deserializer)?;
44 Encoder::xpub_from_base58(s).map_err(|e| serde::de::Error::custom(e.to_string()))
45 }
46}
47
48impl serde::Serialize for crate::xkeys::XPriv {
49 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
50 where
51 S: serde::Serializer,
52 {
53 let encoded =
54 Encoder::xpriv_to_base58(self).map_err(|e| serde::ser::Error::custom(e.to_string()))?;
55 serializer.serialize_str(&encoded)
56 }
57}
58
59impl<'de> serde::Deserialize<'de> for crate::xkeys::XPriv {
60 fn deserialize<D>(deserializer: D) -> Result<crate::xkeys::XPriv, D::Error>
61 where
62 D: serde::Deserializer<'de>,
63 {
64 let s: &str = serde::Deserialize::deserialize(deserializer)?;
65 Encoder::xpriv_from_base58(s).map_err(|e| serde::de::Error::custom(e.to_string()))
66 }
67}