cfx_addr/
consts.rs

1// Copyright 2021 Conflux Foundation. All rights reserved.
2// Conflux is free software and distributed under GNU General Public License.
3// See http://www.gnu.org/licenses/
4//
5// Modification based on https://github.com/hlb8122/rust-bitcoincash-addr in MIT License.
6// A copy of the original license is included in LICENSE.rust-bitcoincash-addr.
7
8pub const CHARSET_SIZE: usize = 32;
9
10pub const RESERVED_BITS_MASK: u8 = 0xf8;
11
12// Because we use a different CHARSET than BCH, it's OK that we disregard all of
13// the BITCOIN type bits.
14//
15// // pub const TYPE_MASK: u8 = 0x78;
16// // pub const TYPE_BITCOIN_P2PKH: u8 = 0x00;
17// // pub const TYPE_BITCOIN_P2SH: u8 = 0x08;
18//
19// In Conflux we have so far only one type of account key format. So we try to
20// use the 4 type bits differently. In the future we may use them in some
21// special transaction scenarios. e.g. A payment code, an address linked to
22// off-chain or cross-chain mechanism.
23
24pub const SIZE_MASK: u8 = 0x07;
25pub const SIZE_160: u8 = 0x00;
26
27// In Conflux we only have 160 bits hash size, however we keep these unused
28// sizes for unit test and compatibility.
29pub const SIZE_192: u8 = 0x01;
30pub const SIZE_224: u8 = 0x02;
31pub const SIZE_256: u8 = 0x03;
32pub const SIZE_320: u8 = 0x04;
33pub const SIZE_384: u8 = 0x05;
34pub const SIZE_448: u8 = 0x06;
35pub const SIZE_512: u8 = 0x07;
36
37pub const BASE32_CHARS: &str = "abcdefghijklmnopqrstuvwxyz0123456789";
38pub const EXCLUDE_CHARS: [char; 4] = ['o', 'i', 'l', 'q'];
39
40// network prefix
41pub const MAINNET_PREFIX: &str = "cfx";
42pub const TESTNET_PREFIX: &str = "cfxtest";
43pub const NETWORK_ID_PREFIX: &str = "net";
44
45// address types
46pub const ADDRESS_TYPE_BUILTIN: &'static str = "builtin";
47pub const ADDRESS_TYPE_CONTRACT: &'static str = "contract";
48pub const ADDRESS_TYPE_NULL: &'static str = "null";
49pub const ADDRESS_TYPE_UNKNOWN: &'static str = "unknown";
50pub const ADDRESS_TYPE_USER: &'static str = "user";
51
52// These two network_ids are reserved.
53pub const RESERVED_NETWORK_IDS: [u64; 2] = [1, 1029];
54
55#[cfg(not(feature = "std"))]
56use alloc::{format, string::String, vec::Vec};
57use lazy_static::lazy_static;
58
59lazy_static! {
60    // Regular expression for application to match string. This regex isn't strict,
61    // because our SDK will.
62    // "(?i)[:=_-0123456789abcdefghijklmnopqrstuvwxyz]*"
63    pub static ref REGEXP: String = format!{"(?i)[:=_-{}]*", BASE32_CHARS};
64
65    // For encoding.
66    pub static ref CHARSET: Vec<u8> =
67        // Remove EXCLUDE_CHARS from charset.
68        BASE32_CHARS.replace(&EXCLUDE_CHARS[..], "").into_bytes();
69
70    // For decoding.
71    pub static ref CHAR_INDEX: [Option<u8>; 128] = (|| {
72        let mut index = [None; 128];
73        assert_eq!(CHARSET.len(), CHARSET_SIZE);
74        for i in 0..CHARSET_SIZE {
75            let c = CHARSET[i] as usize;
76            index[c] = Some(i as u8);
77            // Support uppercase as well.
78            let u = (c as u8 as char).to_ascii_uppercase() as u8 as usize;
79            if u != c {
80                index[u] = Some(i as u8);
81            }
82        }
83        return index;
84    }) ();
85}