Move magic bytes to nested private modules for t-addrs

This commit is contained in:
Deirdre Connolly 2020-03-17 18:44:46 -04:00 committed by Deirdre Connolly
parent 1e71793357
commit 01afa09575
1 changed files with 22 additions and 8 deletions

View File

@ -16,6 +16,20 @@ use crate::{
Network, Network,
}; };
/// Magic numbers used to identify what networks Transparent Addresses
/// are associated with.
mod magics {
pub mod p2sh {
pub const MAINNET: [u8; 2] = [0x1C, 0xBD];
pub const TESTNET: [u8; 2] = [0x1C, 0xBA];
}
pub mod p2pkh {
pub const MAINNET: [u8; 2] = [0x1C, 0xB8];
pub const TESTNET: [u8; 2] = [0x1D, 0x25];
}
}
/// Transparent Zcash Addresses /// Transparent Zcash Addresses
/// ///
/// In Bitcoin a single byte is used for the version field identifying /// In Bitcoin a single byte is used for the version field identifying
@ -102,9 +116,9 @@ impl ZcashSerialize for TransparentAddress {
// Dev network doesn't have a recommendation so we // Dev network doesn't have a recommendation so we
// default to testnet bytes if it's not mainnet. // default to testnet bytes if it's not mainnet.
if *network == Network::Mainnet { if *network == Network::Mainnet {
writer.write_all(&[0x1C, 0xBD][..])? writer.write_all(&magics::p2sh::MAINNET[..])?
} else { } else {
writer.write_all(&[0x1C, 0xBA][..])? writer.write_all(&magics::p2sh::TESTNET[..])?
} }
writer.write_all(script_hash)? writer.write_all(script_hash)?
} }
@ -115,9 +129,9 @@ impl ZcashSerialize for TransparentAddress {
// Dev network doesn't have a recommendation so we // Dev network doesn't have a recommendation so we
// default to testnet bytes if it's not mainnet. // default to testnet bytes if it's not mainnet.
if *network == Network::Mainnet { if *network == Network::Mainnet {
writer.write_all(&[0x1C, 0xB8][..])? writer.write_all(&magics::p2pkh::MAINNET[..])?
} else { } else {
writer.write_all(&[0x1D, 0x25][..])? writer.write_all(&magics::p2pkh::TESTNET[..])?
} }
writer.write_all(pub_key_hash)? writer.write_all(pub_key_hash)?
} }
@ -136,19 +150,19 @@ impl ZcashDeserialize for TransparentAddress {
reader.read_exact(&mut hash_bytes)?; reader.read_exact(&mut hash_bytes)?;
match version_bytes { match version_bytes {
[0x1c, 0xbd] => Ok(TransparentAddress::PayToScriptHash { magics::p2sh::MAINNET => Ok(TransparentAddress::PayToScriptHash {
network: Network::Mainnet, network: Network::Mainnet,
script_hash: hash_bytes, script_hash: hash_bytes,
}), }),
[0x1c, 0xba] => Ok(TransparentAddress::PayToScriptHash { magics::p2sh::TESTNET => Ok(TransparentAddress::PayToScriptHash {
network: Network::Testnet, network: Network::Testnet,
script_hash: hash_bytes, script_hash: hash_bytes,
}), }),
[0x1c, 0xb8] => Ok(TransparentAddress::PayToPublicKeyHash { magics::p2pkh::MAINNET => Ok(TransparentAddress::PayToPublicKeyHash {
network: Network::Mainnet, network: Network::Mainnet,
pub_key_hash: hash_bytes, pub_key_hash: hash_bytes,
}), }),
[0x1d, 0x25] => Ok(TransparentAddress::PayToPublicKeyHash { magics::p2pkh::TESTNET => Ok(TransparentAddress::PayToPublicKeyHash {
network: Network::Testnet, network: Network::Testnet,
pub_key_hash: hash_bytes, pub_key_hash: hash_bytes,
}), }),