anychain_bitcoin/network/
dogecoin.rs

1use crate::{BitcoinFormat, BitcoinNetwork, Prefix};
2use anychain_core::no_std::*;
3use anychain_core::{AddressError, Network, NetworkError};
4
5use core::{fmt, str::FromStr};
6use serde::Serialize;
7
8#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
9pub struct Dogecoin;
10
11impl Network for Dogecoin {
12    const NAME: &'static str = "dogecoin";
13}
14
15impl BitcoinNetwork for Dogecoin {
16    /// Returns the address prefix of the given network.
17    fn to_address_prefix(format: BitcoinFormat) -> Result<Prefix, AddressError> {
18        match format {
19            BitcoinFormat::P2PKH => Ok(Prefix::Version(0x1e)),
20            BitcoinFormat::P2WSH => Ok(Prefix::Version(0x00)),
21            BitcoinFormat::P2SH_P2WPKH => Ok(Prefix::Version(0x16)),
22            f => Err(AddressError::Message(format!(
23                "{} does not support address format {}",
24                Self::NAME,
25                f,
26            ))),
27        }
28    }
29
30    /// Returns the network of the given address prefix.
31    fn from_address_prefix(prefix: Prefix) -> Result<Self, AddressError> {
32        match prefix {
33            Prefix::Version(version) => match version {
34                0x1e | 0x16 => Ok(Self),
35                _ => Err(AddressError::Message(format!(
36                    "Invalid version byte {:#0x} for {} network",
37                    version,
38                    Self::NAME,
39                ))),
40            },
41            _ => Err(AddressError::Message(format!(
42                "{} does not support address format Bech32 or CashAddr",
43                Self::NAME,
44            ))),
45        }
46    }
47}
48
49impl FromStr for Dogecoin {
50    type Err = NetworkError;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        match s {
54            Self::NAME => Ok(Self),
55            _ => Err(NetworkError::InvalidNetwork(s.into())),
56        }
57    }
58}
59
60impl fmt::Display for Dogecoin {
61    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62        write!(f, "{}", Self::NAME)
63    }
64}