anychain_bitcoin/network/
mod.rs

1use crate::format::BitcoinFormat;
2use anychain_core::no_std::*;
3use anychain_core::{AddressError, Network};
4
5pub mod bitcoin;
6pub use self::bitcoin::*;
7
8pub mod bitcoin_testnet;
9pub use self::bitcoin_testnet::*;
10
11pub mod bitcoincash;
12pub use self::bitcoincash::*;
13
14pub mod bitcoincash_testnet;
15pub use self::bitcoincash_testnet::*;
16
17pub mod litecoin;
18pub use self::litecoin::*;
19
20pub mod litecoin_testnet;
21pub use self::litecoin_testnet::*;
22
23pub mod dogecoin;
24pub use self::dogecoin::*;
25
26pub mod dogecoin_testnet;
27pub use self::dogecoin_testnet::*;
28
29/// The interface for a Bitcoin network.
30pub trait BitcoinNetwork: Network {
31    /// Returns the address prefix of the given network.
32    fn to_address_prefix(format: BitcoinFormat) -> Result<Prefix, AddressError>;
33
34    /// Returns the network of the given address prefix.
35    fn from_address_prefix(prefix: Prefix) -> Result<Self, AddressError>;
36}
37
38#[derive(Clone)]
39pub enum Prefix {
40    // address prefix of utxo compatible blockchains
41    AddressPrefix(String),
42    // version byte prepended to the hash160
43    Version(u8),
44}
45
46impl Prefix {
47    pub fn version(self) -> u8 {
48        if let Self::Version(version) = self {
49            version
50        } else {
51            panic!("Attempt to get version byte from an AddressPrefix");
52        }
53    }
54
55    pub fn prefix(self) -> String {
56        if let Self::AddressPrefix(prefix) = self {
57            prefix
58        } else {
59            panic!("Attempt to get prefix from a version");
60        }
61    }
62
63    pub fn from_version(version: u8) -> Self {
64        Self::Version(version)
65    }
66
67    pub fn from_prefix(prefix: &str) -> Self {
68        Self::AddressPrefix(prefix.to_string())
69    }
70}