Use consts for address prefix values

Instead of using magic numbers we can define constants for the address
prefix bytes. This makes it easier for future readers of the code to see
what these values are if they don't know them and/or see that they are
correct if they do know them.
This commit is contained in:
Tobin Harding 2021-07-21 11:52:41 +10:00
parent abff973e83
commit adc1543e7c
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 18 additions and 8 deletions

View File

@ -48,6 +48,15 @@ pub const MIN_TRANSACTION_WEIGHT: u32 = 4 * 60;
pub const WITNESS_SCALE_FACTOR: usize = 4; pub const WITNESS_SCALE_FACTOR: usize = 4;
/// The maximum allowed number of signature check operations in a block /// The maximum allowed number of signature check operations in a block
pub const MAX_BLOCK_SIGOPS_COST: i64 = 80_000; pub const MAX_BLOCK_SIGOPS_COST: i64 = 80_000;
/// Mainnet (bitcoin) pubkey address prefix.
pub const PUBKEY_ADDRESS_PREFIX_MAIN: u8 = 0; // 0x00
/// Mainnet (bitcoin) script address prefix.
pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 5; // 0x05
/// Test (tesnet, signet, regtest) pubkey address prefix.
pub const PUBKEY_ADDRESS_PREFIX_TEST: u8 = 111; // 0x6f
/// Test (tesnet, signet, regtest) script address prefix.
pub const SCRIPT_ADDRESS_PREFIX_TEST: u8 = 196; // 0xc4
/// In Bitcoind this is insanely described as ~((u256)0 >> 32) /// In Bitcoind this is insanely described as ~((u256)0 >> 32)
pub fn max_target(_: Network) -> Uint256 { pub fn max_target(_: Network) -> Uint256 {

View File

@ -41,6 +41,7 @@ use bech32;
use hashes::Hash; use hashes::Hash;
use hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash}; use hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash};
use blockdata::script; use blockdata::script;
use blockdata::constants::{PUBKEY_ADDRESS_PREFIX_MAIN, SCRIPT_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_TEST, SCRIPT_ADDRESS_PREFIX_TEST};
use network::constants::Network; use network::constants::Network;
use util::base58; use util::base58;
use util::ecdsa; use util::ecdsa;
@ -374,8 +375,8 @@ impl fmt::Display for Address {
Payload::PubkeyHash(ref hash) => { Payload::PubkeyHash(ref hash) => {
let mut prefixed = [0; 21]; let mut prefixed = [0; 21];
prefixed[0] = match self.network { prefixed[0] = match self.network {
Network::Bitcoin => 0, Network::Bitcoin => PUBKEY_ADDRESS_PREFIX_MAIN,
Network::Testnet | Network::Signet | Network::Regtest => 111, Network::Testnet | Network::Signet | Network::Regtest => PUBKEY_ADDRESS_PREFIX_TEST,
}; };
prefixed[1..].copy_from_slice(&hash[..]); prefixed[1..].copy_from_slice(&hash[..]);
base58::check_encode_slice_to_fmt(fmt, &prefixed[..]) base58::check_encode_slice_to_fmt(fmt, &prefixed[..])
@ -383,8 +384,8 @@ impl fmt::Display for Address {
Payload::ScriptHash(ref hash) => { Payload::ScriptHash(ref hash) => {
let mut prefixed = [0; 21]; let mut prefixed = [0; 21];
prefixed[0] = match self.network { prefixed[0] = match self.network {
Network::Bitcoin => 5, Network::Bitcoin => SCRIPT_ADDRESS_PREFIX_MAIN,
Network::Testnet | Network::Signet | Network::Regtest => 196, Network::Testnet | Network::Signet | Network::Regtest => SCRIPT_ADDRESS_PREFIX_TEST,
}; };
prefixed[1..].copy_from_slice(&hash[..]); prefixed[1..].copy_from_slice(&hash[..]);
base58::check_encode_slice_to_fmt(fmt, &prefixed[..]) base58::check_encode_slice_to_fmt(fmt, &prefixed[..])
@ -498,19 +499,19 @@ impl FromStr for Address {
} }
let (network, payload) = match data[0] { let (network, payload) = match data[0] {
0 => ( PUBKEY_ADDRESS_PREFIX_MAIN => (
Network::Bitcoin, Network::Bitcoin,
Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap()), Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap()),
), ),
5 => ( SCRIPT_ADDRESS_PREFIX_MAIN => (
Network::Bitcoin, Network::Bitcoin,
Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()), Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()),
), ),
111 => ( PUBKEY_ADDRESS_PREFIX_TEST => (
Network::Testnet, Network::Testnet,
Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap()), Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap()),
), ),
196 => ( SCRIPT_ADDRESS_PREFIX_TEST => (
Network::Testnet, Network::Testnet,
Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()), Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()),
), ),