bitcoin_bech32/
constants.rs

1// Copyright (c) 2017 Clark Moody
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21//! Human-readable constants for various cryptocurrencies
22//!
23//! The authoratative list of Human-readable parts for Bech32 addresses is
24//! maintained in [SLIP-0173](https://github.com/satoshilabs/slips/blob/master/slip-0173.md).
25
26use crate::imports::*;
27
28/// The cryptocurrency to act on
29#[derive(PartialEq, Eq, Debug, Clone, Copy, PartialOrd, Ord, Hash)]
30pub enum Network {
31    /// Bitcoin mainnet
32    Bitcoin,
33    /// Bitcoin testnet
34    Testnet,
35    /// Bitcoin signet,
36    Signet,
37    /// Bitcoin regtest,
38    Regtest,
39    /// Groestlcoin Mainnet
40    Groestlcoin,
41    /// Groestlcoin Testnet,
42    GroestlcoinTestnet,
43    /// Litecoin mainnet
44    Litecoin,
45    /// Litecoin testnet
46    LitecoinTestnet,
47    /// Vertcoin mainnet
48    Vertcoin,
49    /// Vertcoin testnet
50    VertcoinTestnet,
51}
52
53/// Returns the Human-readable part for the given network
54pub fn hrp(network: &Network) -> String {
55    match *network {
56        Network::Bitcoin => "bc".to_string(),
57        Network::Testnet => "tb".to_string(),
58        Network::Signet => "tb".to_string(),
59        Network::Groestlcoin => "grs".to_string(),
60        Network::GroestlcoinTestnet => "tgrs".to_string(),
61        Network::Litecoin => "ltc".to_string(),
62        Network::LitecoinTestnet => "tltc".to_string(),
63        Network::Vertcoin => "vtc".to_string(),
64        Network::VertcoinTestnet => "tvtc".to_string(),
65        Network::Regtest => "bcrt".to_string(),
66    }
67}
68
69/// Classify a Human-readable part as its cryptocurrency
70pub fn classify(hrp: &str) -> Option<Network> {
71    match hrp {
72        "bc" => Some(Network::Bitcoin),
73        "tb" => Some(Network::Testnet),
74        "grs" => Some(Network::Groestlcoin),
75        "tgrs" => Some(Network::GroestlcoinTestnet),
76        "ltc" => Some(Network::Litecoin),
77        "tltc" => Some(Network::LitecoinTestnet),
78        "vtc" => Some(Network::Vertcoin),
79        "tvtc" => Some(Network::VertcoinTestnet),
80        "bcrt" => Some(Network::Regtest),
81        _ => None,
82    }
83}