From adc1543e7c4d0f7708d8e21ced205c58a7e62077 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Wed, 21 Jul 2021 11:52:41 +1000 Subject: [PATCH] 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. --- src/blockdata/constants.rs | 9 +++++++++ src/util/address.rs | 17 +++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/blockdata/constants.rs b/src/blockdata/constants.rs index e8c54946..fe432304 100644 --- a/src/blockdata/constants.rs +++ b/src/blockdata/constants.rs @@ -48,6 +48,15 @@ pub const MIN_TRANSACTION_WEIGHT: u32 = 4 * 60; pub const WITNESS_SCALE_FACTOR: usize = 4; /// The maximum allowed number of signature check operations in a block 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) pub fn max_target(_: Network) -> Uint256 { diff --git a/src/util/address.rs b/src/util/address.rs index 58777a42..2030415d 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -41,6 +41,7 @@ use bech32; use hashes::Hash; use hash_types::{PubkeyHash, WPubkeyHash, ScriptHash, WScriptHash}; 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 util::base58; use util::ecdsa; @@ -374,8 +375,8 @@ impl fmt::Display for Address { Payload::PubkeyHash(ref hash) => { let mut prefixed = [0; 21]; prefixed[0] = match self.network { - Network::Bitcoin => 0, - Network::Testnet | Network::Signet | Network::Regtest => 111, + Network::Bitcoin => PUBKEY_ADDRESS_PREFIX_MAIN, + Network::Testnet | Network::Signet | Network::Regtest => PUBKEY_ADDRESS_PREFIX_TEST, }; prefixed[1..].copy_from_slice(&hash[..]); base58::check_encode_slice_to_fmt(fmt, &prefixed[..]) @@ -383,8 +384,8 @@ impl fmt::Display for Address { Payload::ScriptHash(ref hash) => { let mut prefixed = [0; 21]; prefixed[0] = match self.network { - Network::Bitcoin => 5, - Network::Testnet | Network::Signet | Network::Regtest => 196, + Network::Bitcoin => SCRIPT_ADDRESS_PREFIX_MAIN, + Network::Testnet | Network::Signet | Network::Regtest => SCRIPT_ADDRESS_PREFIX_TEST, }; prefixed[1..].copy_from_slice(&hash[..]); base58::check_encode_slice_to_fmt(fmt, &prefixed[..]) @@ -498,19 +499,19 @@ impl FromStr for Address { } let (network, payload) = match data[0] { - 0 => ( + PUBKEY_ADDRESS_PREFIX_MAIN => ( Network::Bitcoin, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap()), ), - 5 => ( + SCRIPT_ADDRESS_PREFIX_MAIN => ( Network::Bitcoin, Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()), ), - 111 => ( + PUBKEY_ADDRESS_PREFIX_TEST => ( Network::Testnet, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap()), ), - 196 => ( + SCRIPT_ADDRESS_PREFIX_TEST => ( Network::Testnet, Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap()), ),