anychain_bitcoin/
format.rs1use crate::Prefix;
2use anychain_core::no_std::*;
3use anychain_core::{AddressError, Format};
4
5use core::fmt;
6use core::str::FromStr;
7use serde::Serialize;
8
9#[derive(Serialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11#[allow(non_camel_case_types)]
12pub enum BitcoinFormat {
13 P2PKH,
15 P2WSH,
19 P2SH_P2WPKH,
21 Bech32,
23 CashAddr,
25}
26
27impl Format for BitcoinFormat {}
28
29impl BitcoinFormat {
30 pub fn from_address_prefix(prefix: Prefix) -> Result<Self, AddressError> {
32 match prefix {
33 Prefix::AddressPrefix(prefix) => match prefix.as_str() {
34 "bc" | "tb" | "ltc" | "tltc" => Ok(Self::Bech32),
35 "bitcoincash" | "bchtest" => Ok(Self::CashAddr),
36 _ => Err(AddressError::Message(format!(
37 "Unrecognized address prefix {}",
38 prefix,
39 ))),
40 },
41 Prefix::Version(version) => match version {
42 0x00 | 0x6f | 0x1e | 0x71 | 0x30 => Ok(Self::P2PKH),
43 0x05 | 0xc4 | 0x16 | 0x32 | 0x3a => Ok(Self::P2SH_P2WPKH),
44 _ => Err(AddressError::Message(format!(
45 "Unrecognized version byte {}",
46 version,
47 ))),
48 },
49 }
50 }
51}
52
53impl fmt::Display for BitcoinFormat {
54 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55 match self {
56 BitcoinFormat::P2PKH => write!(f, "p2pkh"),
57 BitcoinFormat::P2WSH => write!(f, "p2wsh"),
58 BitcoinFormat::P2SH_P2WPKH => write!(f, "p2sh_p2wpkh"),
59 BitcoinFormat::Bech32 => write!(f, "bech32"),
60 BitcoinFormat::CashAddr => write!(f, "cash_addr"),
61 }
62 }
63}
64
65impl FromStr for BitcoinFormat {
66 type Err = AddressError;
67
68 fn from_str(format: &str) -> Result<Self, AddressError> {
69 match format {
70 "p2pkh" => Ok(BitcoinFormat::P2PKH),
71 "p2sh_p2wpkh" => Ok(BitcoinFormat::P2SH_P2WPKH),
72 "p2wsh" => Ok(BitcoinFormat::P2WSH),
73 "bech32" => Ok(BitcoinFormat::Bech32),
74 "cash_addr" => Ok(BitcoinFormat::CashAddr),
75 _ => Err(AddressError::Message(format!(
76 "Unrecognized bitcoin address format {}",
77 format,
78 ))),
79 }
80 }
81}