Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: add Network::BitcoinMainnet and Network::BitcoinTestnet #10

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -370,16 +371,22 @@ impl<V: NetworkValidation> Address<V> {
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 };

Expand Down Expand Up @@ -669,6 +676,7 @@ impl Address<NetworkUnchecked> {
(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,
}
}

Expand Down Expand Up @@ -754,6 +762,8 @@ impl FromStr for Address<NetworkUnchecked> {
"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 {
Expand Down Expand Up @@ -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);
}
}


Expand Down
2 changes: 2 additions & 0 deletions bitcoin/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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[..]);
Expand Down Expand Up @@ -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[..]);
Expand Down
26 changes: 15 additions & 11 deletions bitcoin/src/blockdata/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/consensus/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions bitcoin/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions bitcoin/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub enum Network {
Devnet,
/// Defichain's regtest network.
Regtest,
/// Mainnet Bitcoin.
BitcoinMainnet,
/// Bitcoin's testnet network.
BitcoinTestnet,
}

impl Network {
Expand Down Expand Up @@ -92,6 +96,8 @@ impl Network {
Network::Testnet => "test",
Network::Devnet => "signet",
Network::Regtest => "regtest",
Network::BitcoinMainnet => "bitcoin main",
Network::BitcoinTestnet => "bitcoin test",
}
}

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl From<Network> for Magic {
Network::Mainnet => Magic::MAINNET,
Network::Testnet => Magic::TESTNET,
Network::Devnet => Magic::DEVNET,
Network::Regtest => Magic::REGTEST,
_ => Magic::REGTEST,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading