From bf89a0dbdb8fe1557da0732663dbdc90f5913627 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 3 Jun 2024 21:58:19 +0800 Subject: [PATCH 1/2] network enum --- bitcoin/embedded/src/main.rs | 2 +- bitcoin/examples/bip32.rs | 2 +- bitcoin/examples/handshake.rs | 4 +- bitcoin/examples/sign-tx-segwit-v0.rs | 2 +- bitcoin/examples/sign-tx-taproot.rs | 2 +- bitcoin/src/address/mod.rs | 72 +++++++++---------- bitcoin/src/bip32.rs | 14 ++-- bitcoin/src/blockdata/block.rs | 2 +- bitcoin/src/blockdata/constants.rs | 24 +++---- bitcoin/src/blockdata/transaction.rs | 4 +- bitcoin/src/consensus/params.rs | 8 +-- bitcoin/src/crypto/key.rs | 6 +- bitcoin/src/network.rs | 70 +++++++++--------- bitcoin/src/p2p/message.rs | 14 ++-- bitcoin/src/p2p/mod.rs | 16 ++--- bitcoin/src/pow.rs | 4 +- bitcoin/src/psbt/mod.rs | 2 +- bitcoin/src/sign_message.rs | 8 +-- bitcoin/src/taproot.rs | 2 +- bitcoin/tests/serde.rs | 2 +- .../bitcoin/deserialize_script.rs | 2 +- 21 files changed, 131 insertions(+), 131 deletions(-) diff --git a/bitcoin/embedded/src/main.rs b/bitcoin/embedded/src/main.rs index 8adbcf4b..912e3076 100644 --- a/bitcoin/embedded/src/main.rs +++ b/bitcoin/embedded/src/main.rs @@ -44,7 +44,7 @@ fn main() -> ! { // Derive address let pubkey = pk.public_key(&secp); - let address = Address::p2wpkh(&pubkey, Network::Bitcoin).unwrap(); + let address = Address::p2wpkh(&pubkey, Network::Mainnet).unwrap(); hprintln!("Address: {}", address).unwrap(); assert_eq!(address.to_string(), "bc1qpx9t9pzzl4qsydmhyt6ctrxxjd4ep549np9993".to_string()); diff --git a/bitcoin/examples/bip32.rs b/bitcoin/examples/bip32.rs index dc38d5b2..60fce9fa 100644 --- a/bitcoin/examples/bip32.rs +++ b/bitcoin/examples/bip32.rs @@ -28,7 +28,7 @@ fn main() { println!("Seed: {}", seed_hex); // default network as mainnet - let network = bitcoin::Network::Bitcoin; + let network = bitcoin::Network::Mainnet; println!("Network: {:?}", network); let seed = Vec::from_hex(seed_hex).unwrap(); diff --git a/bitcoin/examples/handshake.rs b/bitcoin/examples/handshake.rs index 9a3bb462..df275ae0 100644 --- a/bitcoin/examples/handshake.rs +++ b/bitcoin/examples/handshake.rs @@ -29,7 +29,7 @@ fn main() { let version_message = build_version_message(address); let first_message = - message::RawNetworkMessage::new(bitcoin::Network::Bitcoin.magic(), version_message); + message::RawNetworkMessage::new(bitcoin::Network::Mainnet.magic(), version_message); if let Ok(mut stream) = TcpStream::connect(address) { // Send the message @@ -47,7 +47,7 @@ fn main() { println!("Received version message: {:?}", reply.payload()); let second_message = message::RawNetworkMessage::new( - bitcoin::Network::Bitcoin.magic(), + bitcoin::Network::Mainnet.magic(), message::NetworkMessage::Verack, ); diff --git a/bitcoin/examples/sign-tx-segwit-v0.rs b/bitcoin/examples/sign-tx-segwit-v0.rs index ef7f969c..eb5f9cd2 100644 --- a/bitcoin/examples/sign-tx-segwit-v0.rs +++ b/bitcoin/examples/sign-tx-segwit-v0.rs @@ -99,7 +99,7 @@ fn senders_keys(secp: &Secp256k1) -> (SecretKey, WPubkeyHash) { fn receivers_address() -> Address { Address::from_str("bc1q7cyrfmck2ffu2ud3rn5l5a8yv6f0chkp0zpemf") .expect("a valid address") - .require_network(Network::Bitcoin) + .require_network(Network::Mainnet) .expect("valid address for mainnet") } diff --git a/bitcoin/examples/sign-tx-taproot.rs b/bitcoin/examples/sign-tx-taproot.rs index c9025df2..a9ed7914 100644 --- a/bitcoin/examples/sign-tx-taproot.rs +++ b/bitcoin/examples/sign-tx-taproot.rs @@ -101,7 +101,7 @@ fn senders_keys(secp: &Secp256k1) -> Keypair { fn receivers_address() -> Address { Address::from_str("bc1p0dq0tzg2r780hldthn5mrznmpxsxc0jux5f20fwj0z3wqxxk6fpqm7q0va") .expect("a valid address") - .require_network(Network::Bitcoin) + .require_network(Network::Mainnet) .expect("valid address for mainnet") } diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 07115408..f674603e 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -16,7 +16,7 @@ //! let public_key = PublicKey::new(s.generate_keypair(&mut rand::thread_rng()).1); //! //! // Generate pay-to-pubkey-hash address. -//! let address = Address::p2pkh(&public_key, Network::Bitcoin); +//! let address = Address::p2pkh(&public_key, Network::Mainnet); //! # } //! ``` //! @@ -220,15 +220,15 @@ struct AddressInner { /// /// // variant 1 /// let address: Address = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse().unwrap(); -/// let address: Address = address.require_network(Network::Bitcoin).unwrap(); +/// let address: Address = address.require_network(Network::Mainnet).unwrap(); /// /// // variant 2 /// let address: Address = Address::from_str("32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf").unwrap() -/// .require_network(Network::Bitcoin).unwrap(); +/// .require_network(Network::Mainnet).unwrap(); /// /// // variant 3 /// let address: Address = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse::>() -/// .unwrap().require_network(Network::Bitcoin).unwrap(); +/// .unwrap().require_network(Network::Mainnet).unwrap(); /// ``` /// /// ### Formatting addresses @@ -367,16 +367,16 @@ impl Address { /// Format the address for the usage by `Debug` and `Display` implementations. fn fmt_internal(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let p2pkh_prefix = match self.network() { - Network::Bitcoin => PUBKEY_ADDRESS_PREFIX_MAIN, - Network::Testnet | Network::Signet | Network::Regtest => PUBKEY_ADDRESS_PREFIX_TEST, + Network::Mainnet => PUBKEY_ADDRESS_PREFIX_MAIN, + Network::Testnet | Network::Devnet | Network::Regtest => PUBKEY_ADDRESS_PREFIX_TEST, }; let p2sh_prefix = match self.network() { - Network::Bitcoin => SCRIPT_ADDRESS_PREFIX_MAIN, - Network::Testnet | Network::Signet | Network::Regtest => SCRIPT_ADDRESS_PREFIX_TEST, + Network::Mainnet => SCRIPT_ADDRESS_PREFIX_MAIN, + Network::Testnet | Network::Devnet | Network::Regtest => SCRIPT_ADDRESS_PREFIX_TEST, }; let hrp = match self.network() { - Network::Bitcoin => hrp::BC, - Network::Testnet | Network::Signet => hrp::TB, + Network::Mainnet => hrp::BC, + Network::Testnet | Network::Devnet => hrp::TB, Network::Regtest => hrp::BCRT, }; let encoding = AddressEncoding { payload: self.payload(), p2pkh_prefix, p2sh_prefix, hrp }; @@ -648,12 +648,12 @@ impl Address { /// let address: Address = "2N83imGV3gPwBzKJQvWJ7cRUY2SpUyU6A5e".parse().unwrap(); /// assert!(address.is_valid_for_network(Network::Testnet)); /// assert!(address.is_valid_for_network(Network::Regtest)); - /// assert!(address.is_valid_for_network(Network::Signet)); + /// assert!(address.is_valid_for_network(Network::Devnet)); /// - /// assert_eq!(address.is_valid_for_network(Network::Bitcoin), false); + /// assert_eq!(address.is_valid_for_network(Network::Mainnet), false); /// /// let address: Address = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse().unwrap(); - /// assert!(address.is_valid_for_network(Network::Bitcoin)); + /// assert!(address.is_valid_for_network(Network::Mainnet)); /// assert_eq!(address.is_valid_for_network(Network::Testnet), false); /// ``` pub fn is_valid_for_network(&self, network: Network) -> bool { @@ -664,9 +664,9 @@ impl Address { match (self.network(), network) { (a, b) if *a == b => true, - (Network::Bitcoin, _) | (_, Network::Bitcoin) => false, + (Network::Mainnet, _) | (_, Network::Mainnet) => false, (Network::Regtest, _) | (_, Network::Regtest) if !is_legacy => false, - (Network::Testnet, _) | (Network::Regtest, _) | (Network::Signet, _) => true, + (Network::Testnet, _) | (Network::Regtest, _) | (Network::Devnet, _) => true, } } @@ -749,8 +749,8 @@ impl FromStr for Address { // try bech32 let bech32_network = match find_bech32_prefix(s) { // note that upper or lowercase is allowed but NOT mixed case - "bc" | "BC" => Some(Network::Bitcoin), - "tb" | "TB" => Some(Network::Testnet), // this may also be signet + "bc" | "BC" => Some(Network::Mainnet), + "tb" | "TB" => Some(Network::Testnet), // this may also be devnet "bcrt" | "BCRT" => Some(Network::Regtest), _ => None, }; @@ -774,9 +774,9 @@ impl FromStr for Address { let (network, payload) = match data[0] { PUBKEY_ADDRESS_PREFIX_MAIN => - (Network::Bitcoin, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap())), + (Network::Mainnet, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap())), SCRIPT_ADDRESS_PREFIX_MAIN => - (Network::Bitcoin, Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap())), + (Network::Mainnet, Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap())), PUBKEY_ADDRESS_PREFIX_TEST => (Network::Testnet, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap())), SCRIPT_ADDRESS_PREFIX_TEST => @@ -1005,7 +1005,7 @@ mod tests { for (address, expected_type) in &addresses { let addr = Address::from_str(address) .unwrap() - .require_network(Network::Bitcoin) + .require_network(Network::Mainnet) .expect("mainnet"); assert_eq!(&addr.address_type(), expected_type); } @@ -1098,7 +1098,7 @@ mod tests { ["132F25rTsvBdp9JzLLBHP5mvGY66i1xdiM", "33iFwdLuRpW1uK1RTRqsoi8rR4NpDzk66k"].iter() { let addr = - Address::from_str(el).unwrap().require_network(Network::Bitcoin).expect("mainnet"); + Address::from_str(el).unwrap().require_network(Network::Mainnet).expect("mainnet"); assert_eq!(addr.to_qr_uri(), format!("bitcoin:{}", el)); } @@ -1132,9 +1132,9 @@ mod tests { .collect::>(); const LEGACY_EQUIVALENCE_CLASSES: &[&[Network]] = - &[&[Network::Bitcoin], &[Network::Testnet, Network::Regtest, Network::Signet]]; + &[&[Network::Mainnet], &[Network::Testnet, Network::Regtest, Network::Devnet]]; const SEGWIT_EQUIVALENCE_CLASSES: &[&[Network]] = - &[&[Network::Bitcoin], &[Network::Regtest], &[Network::Testnet, Network::Signet]]; + &[&[Network::Mainnet], &[Network::Regtest], &[Network::Testnet, Network::Devnet]]; fn test_addr_type(payloads: &[Payload], equivalence_classes: &[&[Network]]) { for pl in payloads { @@ -1172,7 +1172,7 @@ mod tests { ) .unwrap(); let secp = Secp256k1::verification_only(); - let address = Address::p2tr(&secp, internal_key, None, Network::Bitcoin); + let address = Address::p2tr(&secp, internal_key, None, Network::Mainnet); assert_eq!( address.to_string(), "bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr" @@ -1186,7 +1186,7 @@ mod tests { let address_string = "bc1qhvd6suvqzjcu9pxjhrwhtrlj85ny3n2mqql5w4"; let address = Address::from_str(address_string) .expect("address") - .require_network(Network::Bitcoin) + .require_network(Network::Mainnet) .expect("mainnet"); let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b"; @@ -1207,7 +1207,7 @@ mod tests { let address_string = "3EZQk4F8GURH5sqVMLTFisD17yNeKa7Dfs"; let address = Address::from_str(address_string) .expect("address") - .require_network(Network::Bitcoin) + .require_network(Network::Mainnet) .expect("mainnet"); let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b"; @@ -1228,7 +1228,7 @@ mod tests { let address_string = "1J4LVanjHMu3JkXbVrahNuQCTGCRRgfWWx"; let address = Address::from_str(address_string) .expect("address") - .require_network(Network::Bitcoin) + .require_network(Network::Mainnet) .expect("mainnet"); let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b"; @@ -1271,13 +1271,13 @@ mod tests { let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey"); let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner); let tweaked_pubkey = TweakedPublicKey::dangerous_assume_tweaked(xonly_pubkey); - let address = Address::p2tr_tweaked(tweaked_pubkey, Network::Bitcoin); + let address = Address::p2tr_tweaked(tweaked_pubkey, Network::Mainnet); assert_eq!( address, Address::from_str("bc1pgllnmtxs0g058qz7c6qgaqq4qknwrqj9z7rqn9e2dzhmcfmhlu4sfadf5e") .expect("address") - .require_network(Network::Bitcoin) + .require_network(Network::Mainnet) .expect("mainnet") ); @@ -1297,13 +1297,13 @@ mod tests { let pubkey = PublicKey::from_str(pubkey_string).expect("pubkey"); let xonly_pubkey = XOnlyPublicKey::from(pubkey.inner); let tweaked_pubkey = TweakedPublicKey::dangerous_assume_tweaked(xonly_pubkey); - let address = Address::p2tr_tweaked(tweaked_pubkey, Network::Bitcoin); + let address = Address::p2tr_tweaked(tweaked_pubkey, Network::Mainnet); assert_eq!( address, Address::from_str("bc1pgllnmtxs0g058qz7c6qgaqq4qknwrqj9z7rqn9e2dzhmcfmhlu4sfadf5e") .expect("address") - .require_network(Network::Bitcoin) + .require_network(Network::Mainnet) .expect("mainnet") ); @@ -1324,10 +1324,10 @@ mod tests { ScriptBuf::from_hex("001161458e330389cd0437ee9fe3641d70cc18").unwrap(); let expected = Err(Error::UnrecognizedScript); - assert_eq!(Address::from_script(&bad_p2wpkh, Network::Bitcoin), expected); - assert_eq!(Address::from_script(&bad_p2wsh, Network::Bitcoin), expected); + assert_eq!(Address::from_script(&bad_p2wpkh, Network::Mainnet), expected); + assert_eq!(Address::from_script(&bad_p2wsh, Network::Mainnet), expected); assert_eq!( - Address::from_script(&invalid_segwitv0_script, Network::Bitcoin), + Address::from_script(&invalid_segwitv0_script, Network::Mainnet), Err(Error::WitnessProgram(witness_program::Error::InvalidSegwitV0Length(17))) ); } @@ -1358,10 +1358,10 @@ mod tests { "bc1pgllnmtxs0g058qz7c6qgaqq4qknwrqj9z7rqn9e2dzhmcfmhlu4sfadf5e", ]; for addr in &addresses { - let addr = Address::from_str(addr).unwrap().require_network(Network::Bitcoin).unwrap(); + let addr = Address::from_str(addr).unwrap().require_network(Network::Mainnet).unwrap(); for another in &addresses { let another = - Address::from_str(another).unwrap().require_network(Network::Bitcoin).unwrap(); + Address::from_str(another).unwrap().require_network(Network::Mainnet).unwrap(); assert_eq!(addr.matches_script_pubkey(&another.script_pubkey()), addr == another); } } diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index 06b0cf98..b1593094 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -637,7 +637,7 @@ impl Xpriv { } let network = if data.starts_with(&VERSION_BYTES_MAINNET_PRIVATE) { - Network::Bitcoin + Network::Mainnet } else if data.starts_with(&VERSION_BYTES_TESTNETS_PRIVATE) { Network::Testnet } else { @@ -663,8 +663,8 @@ impl Xpriv { pub fn encode(&self) -> [u8; 78] { let mut ret = [0; 78]; ret[0..4].copy_from_slice(&match self.network { - Network::Bitcoin => VERSION_BYTES_MAINNET_PRIVATE, - Network::Testnet | Network::Signet | Network::Regtest => VERSION_BYTES_TESTNETS_PRIVATE, + Network::Mainnet => VERSION_BYTES_MAINNET_PRIVATE, + Network::Testnet | Network::Devnet | Network::Regtest => VERSION_BYTES_TESTNETS_PRIVATE, }); ret[4] = self.depth; ret[5..9].copy_from_slice(&self.parent_fingerprint[..]); @@ -769,7 +769,7 @@ impl Xpub { } let network = if data.starts_with(&VERSION_BYTES_MAINNET_PUBLIC) { - Network::Bitcoin + Network::Mainnet } else if data.starts_with(&VERSION_BYTES_TESTNETS_PUBLIC) { Network::Testnet } else { @@ -795,8 +795,8 @@ impl Xpub { pub fn encode(&self) -> [u8; 78] { let mut ret = [0; 78]; ret[0..4].copy_from_slice(&match self.network { - Network::Bitcoin => VERSION_BYTES_MAINNET_PUBLIC, - Network::Testnet | Network::Signet | Network::Regtest => VERSION_BYTES_TESTNETS_PUBLIC, + Network::Mainnet => VERSION_BYTES_MAINNET_PUBLIC, + Network::Testnet | Network::Devnet | Network::Regtest => VERSION_BYTES_TESTNETS_PUBLIC, }); ret[4] = self.depth; ret[5..9].copy_from_slice(&self.parent_fingerprint[..]); @@ -1192,7 +1192,7 @@ mod tests { } let xpriv = Xpriv { - network: Network::Bitcoin, + network: Network::Mainnet, depth: 0, parent_fingerprint: Default::default(), child_number: ChildNumber::Normal { index: 0 }, diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 365816b4..60cabf2f 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -505,7 +505,7 @@ mod tests { #[test] fn block_test() { - let network = Network::Bitcoin; + let network = Network::Mainnet; // Mainnet block 00000000b0c5a240b2a61d2e75692224efd4cbecdf6eaf4cc2cf477ca7c270e7 let some_block = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac00000000"); let cutoff_block = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac"); diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index 713e43c5..978dc1c2 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -96,7 +96,7 @@ pub fn genesis_block(network: Network) -> Block { let hash: sha256d::Hash = txdata[0].txid().into(); let merkle_root = hash.into(); match network { - Network::Bitcoin => Block { + Network::Mainnet => Block { header: block::Header { version: block::Version::ONE, prev_blockhash: Hash::all_zeros(), @@ -124,7 +124,7 @@ pub fn genesis_block(network: Network) -> Block { }, txdata, }, - Network::Signet => Block { + Network::Devnet => Block { header: block::Header { version: block::Version::ONE, prev_blockhash: Hash::all_zeros(), @@ -164,7 +164,7 @@ impl_bytes_newtype!(ChainHash, 32); impl ChainHash { // Mainnet value can be verified at https://github.com/lightning/bolts/blob/master/00-introduction.md /// `ChainHash` for mainnet bitcoin. - pub const BITCOIN: Self = Self([ + pub const MAINNET: Self = Self([ 111, 226, 140, 10, 182, 241, 179, 114, 193, 166, 162, 70, 174, 99, 247, 79, 147, 30, 131, 101, 225, 90, 8, 156, 104, 214, 25, 0, 0, 0, 0, 0, ]); @@ -174,7 +174,7 @@ impl ChainHash { 32, 132, 233, 14, 173, 1, 234, 51, 9, 0, 0, 0, 0, ]); /// `ChainHash` for signet bitcoin. - pub const SIGNET: Self = Self([ + pub const DEVNET: Self = Self([ 246, 30, 238, 59, 99, 163, 128, 164, 119, 160, 99, 175, 50, 178, 187, 201, 124, 159, 249, 240, 31, 44, 66, 37, 233, 115, 152, 129, 8, 0, 0, 0, ]); @@ -189,7 +189,7 @@ impl ChainHash { /// See [BOLT 0](https://github.com/lightning/bolts/blob/ffeece3dab1c52efdb9b53ae476539320fa44938/00-introduction.md#chain_hash) /// for specification. pub const fn using_genesis_block(network: Network) -> Self { - let hashes = [Self::BITCOIN, Self::TESTNET, Self::SIGNET, Self::REGTEST]; + let hashes = [Self::MAINNET, Self::TESTNET, Self::DEVNET, Self::REGTEST]; hashes[network as usize] } @@ -237,7 +237,7 @@ mod test { #[test] fn bitcoin_genesis_full_block() { - let gen = genesis_block(Network::Bitcoin); + let gen = genesis_block(Network::Mainnet); assert_eq!(gen.header.version, block::Version::ONE); assert_eq!(gen.header.prev_blockhash, Hash::all_zeros()); @@ -273,7 +273,7 @@ mod test { #[test] fn signet_genesis_full_block() { - let gen = genesis_block(Network::Signet); + let gen = genesis_block(Network::Devnet); assert_eq!(gen.header.version, block::Version::ONE); assert_eq!(gen.header.prev_blockhash, Hash::all_zeros()); assert_eq!( @@ -307,9 +307,9 @@ mod test { #[allow(unreachable_patterns)] // This is specifically trying to catch later added variants. match network { - Network::Bitcoin => {}, + Network::Mainnet => {}, Network::Testnet => {}, - Network::Signet => {}, + Network::Devnet => {}, Network::Regtest => {}, _ => panic!("Update ChainHash::using_genesis_block and chain_hash_genesis_block with new variants"), } @@ -327,16 +327,16 @@ mod test { } chain_hash_genesis_block! { - mainnet_chain_hash_genesis_block, Network::Bitcoin; + mainnet_chain_hash_genesis_block, Network::Mainnet; testnet_chain_hash_genesis_block, Network::Testnet; - signet_chain_hash_genesis_block, Network::Signet; + signet_chain_hash_genesis_block, Network::Devnet; regtest_chain_hash_genesis_block, Network::Regtest; } // Test vector taken from: https://github.com/lightning/bolts/blob/master/00-introduction.md #[test] fn mainnet_chain_hash_test_vector() { - let got = ChainHash::using_genesis_block(Network::Bitcoin).to_string(); + let got = ChainHash::using_genesis_block(Network::Mainnet).to_string(); let want = "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000"; assert_eq!(got, want); } diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 841777ff..71590580 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -80,7 +80,7 @@ impl OutPoint { /// use bitcoin::constants::genesis_block; /// use bitcoin::Network; /// - /// let block = genesis_block(Network::Bitcoin); + /// let block = genesis_block(Network::Mainnet); /// let tx = &block.txdata[0]; /// /// // Coinbase transactions don't have any previous output. @@ -1625,7 +1625,7 @@ mod tests { use crate::blockdata::constants; use crate::network::Network; - let genesis = constants::genesis_block(Network::Bitcoin); + let genesis = constants::genesis_block(Network::Mainnet); assert!(genesis.txdata[0].is_coinbase()); let tx_bytes = hex!("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"); let tx: Transaction = deserialize(&tx_bytes).unwrap(); diff --git a/bitcoin/src/consensus/params.rs b/bitcoin/src/consensus/params.rs index f181ab55..bc6b8437 100644 --- a/bitcoin/src/consensus/params.rs +++ b/bitcoin/src/consensus/params.rs @@ -52,8 +52,8 @@ impl Params { /// Creates parameters set for the given network. pub const fn new(network: Network) -> Self { match network { - Network::Bitcoin => Params { - network: Network::Bitcoin, + Network::Mainnet => Params { + network: Network::Mainnet, bip16_time: 1333238400, // Apr 1 2012 bip34_height: 227931, // 000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8 bip65_height: 388381, // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0 @@ -80,8 +80,8 @@ impl Params { allow_min_difficulty_blocks: true, no_pow_retargeting: false, }, - Network::Signet => Params { - network: Network::Signet, + Network::Devnet => Params { + network: Network::Devnet, bip16_time: 1333238400, // Apr 1 2012 bip34_height: 1, bip65_height: 1, diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index dc2f7cbb..c7a287cc 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -332,8 +332,8 @@ impl PrivateKey { pub fn fmt_wif(&self, fmt: &mut dyn fmt::Write) -> fmt::Result { let mut ret = [0; 34]; ret[0] = match self.network { - Network::Bitcoin => 128, - Network::Testnet | Network::Signet | Network::Regtest => 239, + Network::Mainnet => 128, + Network::Testnet | Network::Devnet | Network::Regtest => 239, }; ret[1..33].copy_from_slice(&self.inner[..]); let privkey = if self.compressed { @@ -366,7 +366,7 @@ impl PrivateKey { }; let network = match data[0] { - 128 => Network::Bitcoin, + 128 => Network::Mainnet, 239 => Network::Testnet, x => { return Err(Error::Base58(base58::Error::InvalidAddressVersion(x))); diff --git a/bitcoin/src/network.rs b/bitcoin/src/network.rs index 32102715..667b16de 100644 --- a/bitcoin/src/network.rs +++ b/bitcoin/src/network.rs @@ -12,7 +12,7 @@ //! use bitcoin::Network; //! use bitcoin::consensus::encode::serialize; //! -//! let network = Network::Bitcoin; +//! let network = Network::Mainnet; //! let bytes = serialize(&network.magic()); //! //! assert_eq!(&bytes[..], &[0xF9, 0xBE, 0xB4, 0xD9]); @@ -38,13 +38,13 @@ use crate::prelude::{String, ToOwned}; #[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] #[non_exhaustive] pub enum Network { - /// Mainnet Bitcoin. - Bitcoin, - /// Bitcoin's testnet network. + /// Mainnet Defichain. + Mainnet, + /// Defichain's testnet network. Testnet, - /// Bitcoin's signet network. - Signet, - /// Bitcoin's regtest network. + /// Defichain's signet network. + Devnet, + /// Defichain's regtest network. Regtest, } @@ -58,7 +58,7 @@ impl Network { /// use bitcoin::Network; /// use std::convert::TryFrom; /// - /// assert_eq!(Ok(Network::Bitcoin), Network::try_from(Magic::from_bytes([0xF9, 0xBE, 0xB4, 0xD9]))); + /// assert_eq!(Ok(Network::Mainnet), Network::try_from(Magic::from_bytes([0xF9, 0xBE, 0xB4, 0xD9]))); /// assert_eq!(None, Network::from_magic(Magic::from_bytes([0xFF, 0xFF, 0xFF, 0xFF]))); /// ``` pub fn from_magic(magic: Magic) -> Option { Network::try_from(magic).ok() } @@ -72,7 +72,7 @@ impl Network { /// use bitcoin::p2p::Magic; /// use bitcoin::Network; /// - /// let network = Network::Bitcoin; + /// let network = Network::Mainnet; /// assert_eq!(network.magic(), Magic::from_bytes([0xF9, 0xBE, 0xB4, 0xD9])); /// ``` pub fn magic(self) -> Magic { Magic::from(self) } @@ -88,9 +88,9 @@ impl Network { /// ``` pub fn to_core_arg(self) -> &'static str { match self { - Network::Bitcoin => "main", + Network::Mainnet => "main", Network::Testnet => "test", - Network::Signet => "signet", + Network::Devnet => "signet", Network::Regtest => "regtest", } } @@ -108,9 +108,9 @@ impl Network { use Network::*; let network = match core_arg { - "main" => Bitcoin, + "main" => Mainnet, "test" => Testnet, - "signet" => Signet, + "devnet" => Devnet, "regtest" => Regtest, _ => return Err(ParseNetworkError(core_arg.to_owned())), }; @@ -125,8 +125,8 @@ impl Network { /// use bitcoin::Network; /// use bitcoin::blockdata::constants::ChainHash; /// - /// let network = Network::Bitcoin; - /// assert_eq!(network.chain_hash(), ChainHash::BITCOIN); + /// let network = Network::Mainnet; + /// assert_eq!(network.chain_hash(), ChainHash::MAINNET); /// ``` pub fn chain_hash(self) -> ChainHash { ChainHash::using_genesis_block(self) } @@ -139,7 +139,7 @@ impl Network { /// use bitcoin::blockdata::constants::ChainHash; /// use std::convert::TryFrom; /// - /// assert_eq!(Ok(Network::Bitcoin), Network::try_from(ChainHash::BITCOIN)); + /// assert_eq!(Ok(Network::Mainnet), Network::try_from(ChainHash::MAINNET)); /// ``` pub fn from_chain_hash(chain_hash: ChainHash) -> Option { Network::try_from(chain_hash).ok() @@ -148,9 +148,9 @@ impl Network { /// Returns the associated network parameters. pub const fn params(self) -> &'static Params { const PARAMS: [Params; 4] = [ - Params::new(Network::Bitcoin), + Params::new(Network::Mainnet), Params::new(Network::Testnet), - Params::new(Network::Signet), + Params::new(Network::Devnet), Params::new(Network::Regtest), ]; &PARAMS[self as usize] @@ -227,9 +227,9 @@ impl FromStr for Network { use Network::*; let network = match s { - "bitcoin" => Bitcoin, + "mainnet" => Mainnet, "testnet" => Testnet, - "signet" => Signet, + "devnet" => Devnet, "regtest" => Regtest, _ => return Err(ParseNetworkError(s.to_owned())), }; @@ -273,9 +273,9 @@ impl TryFrom for Network { fn try_from(chain_hash: ChainHash) -> Result { match chain_hash { // Note: any new network entries must be matched against here. - ChainHash::BITCOIN => Ok(Network::Bitcoin), + ChainHash::MAINNET => Ok(Network::Mainnet), ChainHash::TESTNET => Ok(Network::Testnet), - ChainHash::SIGNET => Ok(Network::Signet), + ChainHash::DEVNET => Ok(Network::Devnet), ChainHash::REGTEST => Ok(Network::Regtest), _ => Err(UnknownChainHashError(chain_hash)), } @@ -290,28 +290,28 @@ mod tests { #[test] fn serialize_test() { - assert_eq!(serialize(&Network::Bitcoin.magic()), &[0xf9, 0xbe, 0xb4, 0xd9]); + assert_eq!(serialize(&Network::Mainnet.magic()), &[0xf9, 0xbe, 0xb4, 0xd9]); assert_eq!(serialize(&Network::Testnet.magic()), &[0x0b, 0x11, 0x09, 0x07]); - assert_eq!(serialize(&Network::Signet.magic()), &[0x0a, 0x03, 0xcf, 0x40]); + assert_eq!(serialize(&Network::Devnet.magic()), &[0x0a, 0x03, 0xcf, 0x40]); assert_eq!(serialize(&Network::Regtest.magic()), &[0xfa, 0xbf, 0xb5, 0xda]); - assert_eq!(deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(), Some(Network::Bitcoin.magic())); + assert_eq!(deserialize(&[0xf9, 0xbe, 0xb4, 0xd9]).ok(), Some(Network::Mainnet.magic())); assert_eq!(deserialize(&[0x0b, 0x11, 0x09, 0x07]).ok(), Some(Network::Testnet.magic())); - assert_eq!(deserialize(&[0x0a, 0x03, 0xcf, 0x40]).ok(), Some(Network::Signet.magic())); + assert_eq!(deserialize(&[0x0a, 0x03, 0xcf, 0x40]).ok(), Some(Network::Devnet.magic())); assert_eq!(deserialize(&[0xfa, 0xbf, 0xb5, 0xda]).ok(), Some(Network::Regtest.magic())); } #[test] fn string_test() { - assert_eq!(Network::Bitcoin.to_string(), "bitcoin"); + assert_eq!(Network::Mainnet.to_string(), "bitcoin"); assert_eq!(Network::Testnet.to_string(), "testnet"); assert_eq!(Network::Regtest.to_string(), "regtest"); - assert_eq!(Network::Signet.to_string(), "signet"); + assert_eq!(Network::Devnet.to_string(), "signet"); - assert_eq!("bitcoin".parse::().unwrap(), Network::Bitcoin); + assert_eq!("bitcoin".parse::().unwrap(), Network::Mainnet); assert_eq!("testnet".parse::().unwrap(), Network::Testnet); assert_eq!("regtest".parse::().unwrap(), Network::Regtest); - assert_eq!("signet".parse::().unwrap(), Network::Signet); + assert_eq!("signet".parse::().unwrap(), Network::Devnet); assert!("fakenet".parse::().is_err()); } @@ -360,9 +360,9 @@ mod tests { fn serde_roundtrip() { use Network::*; let tests = vec![ - (Bitcoin, "bitcoin"), + (Mainnet, "mainnet"), (Testnet, "testnet"), - (Signet, "signet"), + (Devnet, "devnet"), (Regtest, "regtest"), ]; @@ -381,10 +381,10 @@ mod tests { #[test] fn from_to_core_arg() { let expected_pairs = [ - (Network::Bitcoin, "main"), + (Network::Mainnet, "main"), (Network::Testnet, "test"), (Network::Regtest, "regtest"), - (Network::Signet, "signet"), + (Network::Devnet, "signet"), ]; for (net, core_arg) in &expected_pairs { @@ -404,7 +404,7 @@ mod tests { } serde_test::assert_tokens( - &T { network: Network::Bitcoin }, + &T { network: Network::Mainnet }, &[ serde_test::Token::Struct { name: "T", len: 1 }, serde_test::Token::Str("network"), diff --git a/bitcoin/src/p2p/message.rs b/bitcoin/src/p2p/message.rs index 457957f3..9e4eb4f8 100644 --- a/bitcoin/src/p2p/message.rs +++ b/bitcoin/src/p2p/message.rs @@ -705,7 +705,7 @@ mod test { #[test] #[rustfmt::skip] fn serialize_verack_test() { - assert_eq!(serialize(&RawNetworkMessage::new(Magic::from(Network::Bitcoin), NetworkMessage::Verack)), + assert_eq!(serialize(&RawNetworkMessage::new(Magic::from(Network::Mainnet), NetworkMessage::Verack)), vec![0xf9, 0xbe, 0xb4, 0xd9, 0x76, 0x65, 0x72, 0x61, 0x63, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]); @@ -714,7 +714,7 @@ mod test { #[test] #[rustfmt::skip] fn serialize_ping_test() { - assert_eq!(serialize(&RawNetworkMessage::new(Magic::from(Network::Bitcoin), NetworkMessage::Ping(100))), + assert_eq!(serialize(&RawNetworkMessage::new(Magic::from(Network::Mainnet), NetworkMessage::Ping(100))), vec![0xf9, 0xbe, 0xb4, 0xd9, 0x70, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x24, 0x67, 0xf1, 0x1d, @@ -724,7 +724,7 @@ mod test { #[test] #[rustfmt::skip] fn serialize_mempool_test() { - assert_eq!(serialize(&RawNetworkMessage::new(Magic::from(Network::Bitcoin), NetworkMessage::MemPool)), + assert_eq!(serialize(&RawNetworkMessage::new(Magic::from(Network::Mainnet), NetworkMessage::MemPool)), vec![0xf9, 0xbe, 0xb4, 0xd9, 0x6d, 0x65, 0x6d, 0x70, 0x6f, 0x6f, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]); @@ -733,7 +733,7 @@ mod test { #[test] #[rustfmt::skip] fn serialize_getaddr_test() { - assert_eq!(serialize(&RawNetworkMessage::new(Magic::from(Network::Bitcoin), NetworkMessage::GetAddr)), + assert_eq!(serialize(&RawNetworkMessage::new(Magic::from(Network::Mainnet), NetworkMessage::GetAddr)), vec![0xf9, 0xbe, 0xb4, 0xd9, 0x67, 0x65, 0x74, 0x61, 0x64, 0x64, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2]); @@ -748,7 +748,7 @@ mod test { 0x00, 0x00, 0x00, 0x00, 0x5d, 0xf6, 0xe0, 0xe2 ]); let preimage = - RawNetworkMessage::new(Magic::from(Network::Bitcoin), NetworkMessage::GetAddr); + RawNetworkMessage::new(Magic::from(Network::Mainnet), NetworkMessage::GetAddr); assert!(msg.is_ok()); let msg: RawNetworkMessage = msg.unwrap(); assert_eq!(preimage.magic, msg.magic); @@ -779,7 +779,7 @@ mod test { assert!(msg.is_ok()); let msg = msg.unwrap(); - assert_eq!(msg.magic, Magic::from(Network::Bitcoin)); + assert_eq!(msg.magic, Magic::from(Network::Mainnet)); if let NetworkMessage::Version(version_msg) = msg.payload { assert_eq!(version_msg.version, 70015); assert_eq!( @@ -825,7 +825,7 @@ mod test { let (msg, consumed) = msg.unwrap(); assert_eq!(consumed, data.to_vec().len() - 2); - assert_eq!(msg.magic, Magic::from(Network::Bitcoin)); + assert_eq!(msg.magic, Magic::from(Network::Mainnet)); if let NetworkMessage::Version(version_msg) = msg.payload { assert_eq!(version_msg.version, 70015); assert_eq!( diff --git a/bitcoin/src/p2p/mod.rs b/bitcoin/src/p2p/mod.rs index a8ec758c..77617d45 100644 --- a/bitcoin/src/p2p/mod.rs +++ b/bitcoin/src/p2p/mod.rs @@ -207,11 +207,11 @@ pub struct Magic([u8; 4]); impl Magic { /// Bitcoin mainnet network magic bytes. - pub const BITCOIN: Self = Self([0xF9, 0xBE, 0xB4, 0xD9]); + pub const MAINNET: Self = Self([0xF9, 0xBE, 0xB4, 0xD9]); /// Bitcoin testnet network magic bytes. pub const TESTNET: Self = Self([0x0B, 0x11, 0x09, 0x07]); /// Bitcoin signet network magic bytes. - pub const SIGNET: Self = Self([0x0A, 0x03, 0xCF, 0x40]); + pub const DEVNET: Self = Self([0x0A, 0x03, 0xCF, 0x40]); /// Bitcoin regtest network magic bytes. pub const REGTEST: Self = Self([0xFA, 0xBF, 0xB5, 0xDA]); @@ -237,9 +237,9 @@ impl From for Magic { fn from(network: Network) -> Magic { match network { // Note: new network entries must explicitly be matched in `try_from` below. - Network::Bitcoin => Magic::BITCOIN, + Network::Mainnet => Magic::MAINNET, Network::Testnet => Magic::TESTNET, - Network::Signet => Magic::SIGNET, + Network::Devnet => Magic::DEVNET, Network::Regtest => Magic::REGTEST, } } @@ -251,9 +251,9 @@ impl TryFrom for Network { fn try_from(magic: Magic) -> Result { match magic { // Note: any new network entries must be matched against here. - Magic::BITCOIN => Ok(Network::Bitcoin), + Magic::MAINNET => Ok(Network::Mainnet), Magic::TESTNET => Ok(Network::Testnet), - Magic::SIGNET => Ok(Network::Signet), + Magic::DEVNET => Ok(Network::Devnet), Magic::REGTEST => Ok(Network::Regtest), _ => Err(UnknownMagicError(magic)), } @@ -410,10 +410,10 @@ mod tests { #[test] fn magic_from_str() { let known_network_magic_strs = [ - ("f9beb4d9", Network::Bitcoin), + ("f9beb4d9", Network::Mainnet), ("0b110907", Network::Testnet), ("fabfb5da", Network::Regtest), - ("0a03cf40", Network::Signet), + ("0a03cf40", Network::Devnet), ]; for (magic_str, network) in &known_network_magic_strs { diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index 9387bb4f..e19d1969 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -226,9 +226,9 @@ impl Target { #[cfg_attr(all(test, mutate), mutate)] pub fn difficulty(&self, network: Network) -> u128 { let max = match network { - Network::Bitcoin => Target::MAX_ATTAINABLE_MAINNET, + Network::Mainnet => Target::MAX_ATTAINABLE_MAINNET, Network::Testnet => Target::MAX_ATTAINABLE_TESTNET, - Network::Signet => Target::MAX_ATTAINABLE_SIGNET, + Network::Devnet => Target::MAX_ATTAINABLE_SIGNET, Network::Regtest => Target::MAX_ATTAINABLE_REGTEST, }; let d = max.0 / self.0; diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index 870384f3..8dc8663e 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -1014,7 +1014,7 @@ mod tests { use crate::blockdata::script::ScriptBuf; use crate::blockdata::transaction::{self, OutPoint, Sequence, Transaction, TxIn, TxOut}; use crate::blockdata::witness::Witness; - use crate::network::Network::Bitcoin; + use crate::network::Network::Mainnet; use crate::psbt::map::{Input, Output}; use crate::psbt::raw; use crate::psbt::serialize::{Deserialize, Serialize}; diff --git a/bitcoin/src/sign_message.rs b/bitcoin/src/sign_message.rs index d846946a..a83fd35c 100644 --- a/bitcoin/src/sign_message.rs +++ b/bitcoin/src/sign_message.rs @@ -242,14 +242,14 @@ mod tests { assert!(pubkey.compressed); assert_eq!(pubkey.inner, secp256k1::PublicKey::from_secret_key(&secp, &privkey)); - let p2pkh = Address::p2pkh(&pubkey, Network::Bitcoin); + let p2pkh = Address::p2pkh(&pubkey, Network::Mainnet); assert_eq!(signature2.is_signed_by_address(&secp, &p2pkh, msg_hash), Ok(true)); - let p2wpkh = Address::p2wpkh(&pubkey, Network::Bitcoin).unwrap(); + let p2wpkh = Address::p2wpkh(&pubkey, Network::Mainnet).unwrap(); assert_eq!( signature2.is_signed_by_address(&secp, &p2wpkh, msg_hash), Err(MessageSignatureError::UnsupportedAddressType(AddressType::P2wpkh)) ); - let p2shwpkh = Address::p2shwpkh(&pubkey, Network::Bitcoin).unwrap(); + let p2shwpkh = Address::p2shwpkh(&pubkey, Network::Mainnet).unwrap(); assert_eq!( signature2.is_signed_by_address(&secp, &p2shwpkh, msg_hash), Err(MessageSignatureError::UnsupportedAddressType(AddressType::P2sh)) @@ -280,7 +280,7 @@ mod tests { PublicKey::from_slice(&BASE64_STANDARD.decode(pubkey_base64).expect("base64 string")) .expect("pubkey slice"); - let p2pkh = Address::p2pkh(&pubkey, Network::Bitcoin); + let p2pkh = Address::p2pkh(&pubkey, Network::Mainnet); assert_eq!(signature.is_signed_by_address(&secp, &p2pkh, msg_hash), Ok(false)); } } diff --git a/bitcoin/src/taproot.rs b/bitcoin/src/taproot.rs index d8877143..113e701d 100644 --- a/bitcoin/src/taproot.rs +++ b/bitcoin/src/taproot.rs @@ -1962,7 +1962,7 @@ mod test { let tweak = TapTweakHash::from_key_and_tweak(internal_key, merkle_root); let (output_key, _parity) = internal_key.tap_tweak(secp, merkle_root); - let addr = Address::p2tr(secp, internal_key, merkle_root, Network::Bitcoin); + let addr = Address::p2tr(secp, internal_key, merkle_root, Network::Mainnet); let spk = addr.script_pubkey(); assert_eq!(expected_output_key, output_key.to_inner()); diff --git a/bitcoin/tests/serde.rs b/bitcoin/tests/serde.rs index c62bb382..706f1e43 100644 --- a/bitcoin/tests/serde.rs +++ b/bitcoin/tests/serde.rs @@ -146,7 +146,7 @@ fn serde_regression_witness() { fn serde_regression_address() { let s = include_str!("data/serde/public_key_hex"); let pk = PublicKey::from_str(s.trim()).unwrap(); - let addr = Address::p2pkh(&pk, Network::Bitcoin); + let addr = Address::p2pkh(&pk, Network::Mainnet); let got = serialize(&addr).unwrap(); let want = include_bytes!("data/serde/address_bincode") as &[_]; diff --git a/fuzz/fuzz_targets/bitcoin/deserialize_script.rs b/fuzz/fuzz_targets/bitcoin/deserialize_script.rs index a32f01f0..e85d81a5 100644 --- a/fuzz/fuzz_targets/bitcoin/deserialize_script.rs +++ b/fuzz/fuzz_targets/bitcoin/deserialize_script.rs @@ -38,7 +38,7 @@ fn do_test(data: &[u8]) { assert_eq!(data, &encode::serialize(&script)[..]); // Check if valid address and if that address roundtrips. - if let Ok(addr) = Address::from_script(&script, Network::Bitcoin) { + if let Ok(addr) = Address::from_script(&script, Network::Mainnet) { assert_eq!(addr.script_pubkey(), script); } } From 93c63338d55de3d26e9fd79a60ca88256f60d988 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 3 Jun 2024 21:58:40 +0800 Subject: [PATCH 2/2] bech32 prefix --- bitcoin/src/address/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index f674603e..5e0a4fd5 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -749,8 +749,8 @@ impl FromStr for Address { // try bech32 let bech32_network = match find_bech32_prefix(s) { // note that upper or lowercase is allowed but NOT mixed case - "bc" | "BC" => Some(Network::Mainnet), - "tb" | "TB" => Some(Network::Testnet), // this may also be devnet + "df" | "DF" => Some(Network::Mainnet), + "tf" | "TF" => Some(Network::Testnet), // this may also be devnet "bcrt" | "BCRT" => Some(Network::Regtest), _ => None, };