1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::enc::XKeyEncoder;

/// The default encoder, selected by feature flag
#[cfg(feature = "mainnet")]
pub type Encoder = crate::enc::MainnetEncoder;

/// The default encoder, selected by feature flag
#[cfg(feature = "testnet")]
pub type Encoder = crate::enc::TestnetEncoder;

impl std::str::FromStr for crate::xkeys::XPriv {
    type Err = crate::Bip32Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Encoder::xpriv_from_base58(s)
    }
}

impl std::str::FromStr for crate::xkeys::XPub {
    type Err = crate::Bip32Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Encoder::xpub_from_base58(s)
    }
}

impl serde::Serialize for crate::xkeys::XPub {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let encoded =
            Encoder::xpub_to_base58(self).map_err(|e| serde::ser::Error::custom(e.to_string()))?;
        serializer.serialize_str(&encoded)
    }
}

impl<'de> serde::Deserialize<'de> for crate::xkeys::XPub {
    fn deserialize<D>(deserializer: D) -> Result<crate::xkeys::XPub, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s: &str = serde::Deserialize::deserialize(deserializer)?;
        Encoder::xpub_from_base58(s).map_err(|e| serde::de::Error::custom(e.to_string()))
    }
}

impl serde::Serialize for crate::xkeys::XPriv {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let encoded =
            Encoder::xpriv_to_base58(self).map_err(|e| serde::ser::Error::custom(e.to_string()))?;
        serializer.serialize_str(&encoded)
    }
}

impl<'de> serde::Deserialize<'de> for crate::xkeys::XPriv {
    fn deserialize<D>(deserializer: D) -> Result<crate::xkeys::XPriv, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s: &str = serde::Deserialize::deserialize(deserializer)?;
        Encoder::xpriv_from_base58(s).map_err(|e| serde::de::Error::custom(e.to_string()))
    }
}