diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 7de8485c..372f019b 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -45,6 +45,7 @@ use crate::blockdata::constants::{ use crate::blockdata::script::witness_program::WitnessProgram; use crate::blockdata::script::witness_version::WitnessVersion; use crate::blockdata::script::{self, Script, ScriptBuf, ScriptHash}; +use crate::constants::{PUBKEY_ADDRESS_PREFIX_BITCOIN_MAIN, PUBKEY_ADDRESS_PREFIX_BITCOIN_TEST, SCRIPT_ADDRESS_PREFIX_BITCOIN_MAIN, SCRIPT_ADDRESS_PREFIX_BITCOIN_TEST}; use crate::crypto::key::{PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey}; use crate::network::Network; use crate::prelude::*; @@ -370,16 +371,22 @@ impl Address { Network::Mainnet => PUBKEY_ADDRESS_PREFIX_MAIN, Network::Testnet | Network::Devnet | Network::Devnet => PUBKEY_ADDRESS_PREFIX_TEST, Network::Regtest => PUBKEY_ADDRESS_PREFIX_REGTEST, + Network::BitcoinMainnet => PUBKEY_ADDRESS_PREFIX_BITCOIN_MAIN, + Network::BitcoinTestnet => PUBKEY_ADDRESS_PREFIX_BITCOIN_TEST, }; let p2sh_prefix = match self.network() { Network::Mainnet => SCRIPT_ADDRESS_PREFIX_MAIN, Network::Testnet | Network::Devnet | Network::Devnet => SCRIPT_ADDRESS_PREFIX_TEST, Network::Regtest => SCRIPT_ADDRESS_PREFIX_REGTEST, + Network::BitcoinMainnet => SCRIPT_ADDRESS_PREFIX_BITCOIN_MAIN, + Network::BitcoinTestnet => SCRIPT_ADDRESS_PREFIX_BITCOIN_TEST, }; let hrp = match self.network() { Network::Mainnet => Hrp::parse("df").unwrap(), Network::Testnet | Network::Devnet => Hrp::parse("tf").unwrap(), Network::Regtest => hrp::BCRT, + Network::BitcoinMainnet => Hrp::parse("bc").unwrap(), + Network::BitcoinTestnet => Hrp::parse("tb").unwrap(), }; let encoding = AddressEncoding { payload: self.payload(), p2pkh_prefix, p2sh_prefix, hrp }; @@ -669,6 +676,7 @@ impl Address { (Network::Mainnet, _) | (_, Network::Mainnet) => false, (Network::Regtest, _) | (_, Network::Regtest) if !is_legacy => false, (Network::Testnet, _) | (Network::Regtest, _) | (Network::Devnet, _) => true, + (Network::BitcoinMainnet, _) | (Network::BitcoinTestnet, _) => false, } } @@ -754,6 +762,8 @@ impl FromStr for Address { "df" | "DF" => Some(Network::Mainnet), "tf" | "TF" => Some(Network::Testnet), // this may also be devnet "bcrt" | "BCRT" => Some(Network::Regtest), + "bc" | "BC" => Some(Network::BitcoinMainnet), + "tb" | "TB" => Some(Network::BitcoinTestnet), _ => None, }; if let Some(network) = bech32_network { @@ -851,6 +861,18 @@ fn test_defi_addr() { let bech32 = Address::from_str(bech32); println!("bech32: {:?}", bech32); } + + // bitcoin bech32 + println!("bitcoin bech32"); + { + let bech32 = "bc1qq4mg7tgh7qqkk4eqhdyct89mqegyzut06ksf0h"; + let bech32 = Address::from_str(bech32); + println!("mainnet bitcoin bech32: {:?}", bech32); + + let bech32 = "tb1qndnfnxvjupgyfut6skz8mdj6zcmgu9r65rv7lr"; + let bech32 = Address::from_str(bech32); + println!("testnet bitcoin bech32: {:?}", bech32); + } } diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index 95a21dc9..bdb27813 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -665,6 +665,7 @@ impl Xpriv { ret[0..4].copy_from_slice(&match self.network { Network::Mainnet => VERSION_BYTES_MAINNET_PRIVATE, Network::Testnet | Network::Devnet | Network::Regtest => VERSION_BYTES_TESTNETS_PRIVATE, + _ => VERSION_BYTES_TESTNETS_PRIVATE, }); ret[4] = self.depth; ret[5..9].copy_from_slice(&self.parent_fingerprint[..]); @@ -797,6 +798,7 @@ impl Xpub { ret[0..4].copy_from_slice(&match self.network { Network::Mainnet => VERSION_BYTES_MAINNET_PUBLIC, Network::Testnet | Network::Devnet | Network::Regtest => VERSION_BYTES_TESTNETS_PUBLIC, + _ => VERSION_BYTES_TESTNETS_PUBLIC, }); ret[4] = self.depth; ret[5..9].copy_from_slice(&self.parent_fingerprint[..]); diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index d716a88f..57c085b3 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -38,20 +38,24 @@ pub const WITNESS_SCALE_FACTOR: usize = 4; pub const MAX_BLOCK_SIGOPS_COST: i64 = 80_000; /// Mainnet (bitcoin) pubkey address prefix. -// pub const PUBKEY_ADDRESS_PREFIX_MAIN: u8 = 0; // 0x00 -pub const PUBKEY_ADDRESS_PREFIX_MAIN: u8 = 18; // 0x12 (defichain mainnet) +pub const PUBKEY_ADDRESS_PREFIX_BITCOIN_MAIN: u8 = 0; // 0x00 +/// Mainnet (defichain) pubkey address prefix. +pub const PUBKEY_ADDRESS_PREFIX_MAIN: u8 = 18; // 0x12 /// Mainnet (bitcoin) script address prefix. -// pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 5; // 0x05 -pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 90; // 0x5a (defichain mainnet) +pub const SCRIPT_ADDRESS_PREFIX_BITCOIN_MAIN: u8 = 5; // 0x05 +/// Mainnet (defichain) script address prefix. +pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 90; // 0x5a -/// Test (tesnet, signet, regtest) pubkey address prefix. -// pub const PUBKEY_ADDRESS_PREFIX_TEST: u8 = 111; // 0x6f -pub const PUBKEY_ADDRESS_PREFIX_TEST: u8 = 15; // 0xf (defichain testnet, devnet, changi) +/// Test Bitcoin (tesnet, signet, regtest) pubkey address prefix. +pub const PUBKEY_ADDRESS_PREFIX_BITCOIN_TEST: u8 = 111; // 0x6f +/// Test Defichain (tesnet, devnet, changi) pubkey address prefix. +pub const PUBKEY_ADDRESS_PREFIX_TEST: u8 = 15; // 0xf -/// Test (tesnet, signet, regtest) script address prefix. -// pub const SCRIPT_ADDRESS_PREFIX_TEST: u8 = 196; // 0xc4 -pub const SCRIPT_ADDRESS_PREFIX_TEST: u8 = 128; // 0x80 (defichain testnet, devnet changi) +/// Test Bitcoin (tesnet, signet, regtest) script address prefix. +pub const SCRIPT_ADDRESS_PREFIX_BITCOIN_TEST: u8 = 196; // 0xc4 +/// Test Defichain (tesnet, devnet, changi) script address prefix. +pub const SCRIPT_ADDRESS_PREFIX_TEST: u8 = 128; // 0x80 /// Regtest pubkey address prefix. pub const PUBKEY_ADDRESS_PREFIX_REGTEST: u8 = 111; // 0x6f (defichain regtest) @@ -152,7 +156,7 @@ pub fn genesis_block(network: Network) -> Block { }, txdata, }, - Network::Regtest => Block { + _ => Block { header: block::Header { version: block::Version::ONE, prev_blockhash: Hash::all_zeros(), diff --git a/bitcoin/src/consensus/params.rs b/bitcoin/src/consensus/params.rs index bc6b8437..c2d12924 100644 --- a/bitcoin/src/consensus/params.rs +++ b/bitcoin/src/consensus/params.rs @@ -94,7 +94,7 @@ impl Params { allow_min_difficulty_blocks: false, no_pow_retargeting: false, }, - Network::Regtest => Params { + _ => Params { network: Network::Regtest, bip16_time: 1333238400, // Apr 1 2012 bip34_height: 100000000, // not activated on regtest diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 34fb5f46..49909d2a 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -334,6 +334,7 @@ impl PrivateKey { ret[0] = match self.network { Network::Mainnet => 128, Network::Testnet | Network::Devnet | Network::Regtest => 239, + _ => 239, }; ret[1..33].copy_from_slice(&self.inner[..]); let privkey = if self.compressed { diff --git a/bitcoin/src/network.rs b/bitcoin/src/network.rs index 667b16de..b6f6699a 100644 --- a/bitcoin/src/network.rs +++ b/bitcoin/src/network.rs @@ -46,6 +46,10 @@ pub enum Network { Devnet, /// Defichain's regtest network. Regtest, + /// Mainnet Bitcoin. + BitcoinMainnet, + /// Bitcoin's testnet network. + BitcoinTestnet, } impl Network { @@ -92,6 +96,8 @@ impl Network { Network::Testnet => "test", Network::Devnet => "signet", Network::Regtest => "regtest", + Network::BitcoinMainnet => "bitcoin main", + Network::BitcoinTestnet => "bitcoin test", } } diff --git a/bitcoin/src/p2p/mod.rs b/bitcoin/src/p2p/mod.rs index 77617d45..b44d0442 100644 --- a/bitcoin/src/p2p/mod.rs +++ b/bitcoin/src/p2p/mod.rs @@ -240,7 +240,7 @@ impl From for Magic { Network::Mainnet => Magic::MAINNET, Network::Testnet => Magic::TESTNET, Network::Devnet => Magic::DEVNET, - Network::Regtest => Magic::REGTEST, + _ => Magic::REGTEST, } } } diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index e19d1969..fd06d0b3 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -229,7 +229,7 @@ impl Target { Network::Mainnet => Target::MAX_ATTAINABLE_MAINNET, Network::Testnet => Target::MAX_ATTAINABLE_TESTNET, Network::Devnet => Target::MAX_ATTAINABLE_SIGNET, - Network::Regtest => Target::MAX_ATTAINABLE_REGTEST, + _ => Target::MAX_ATTAINABLE_REGTEST, }; let d = max.0 / self.0; d.saturating_to_u128()