From ee8b672def878ef464730f20c2befa50fe1f6bd0 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 16 Sep 2024 15:14:56 +0800 Subject: [PATCH 1/5] fix --- bitcoin/src/address/mod.rs | 12 ++++++++---- bitcoin/src/blockdata/constants.rs | 18 ++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 5e0a4fd5..82d86448 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -39,8 +39,8 @@ use secp256k1::{Secp256k1, Verification, XOnlyPublicKey}; use crate::base58; use crate::blockdata::constants::{ - MAX_SCRIPT_ELEMENT_SIZE, PUBKEY_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_TEST, - SCRIPT_ADDRESS_PREFIX_MAIN, SCRIPT_ADDRESS_PREFIX_TEST, + MAX_SCRIPT_ELEMENT_SIZE, PUBKEY_ADDRESS_PREFIX_MAIN, PUBKEY_ADDRESS_PREFIX_REGTEST, PUBKEY_ADDRESS_PREFIX_TEST, + SCRIPT_ADDRESS_PREFIX_MAIN, SCRIPT_ADDRESS_PREFIX_REGTEST, SCRIPT_ADDRESS_PREFIX_TEST, }; use crate::blockdata::script::witness_program::WitnessProgram; use crate::blockdata::script::witness_version::WitnessVersion; @@ -368,15 +368,19 @@ impl Address { fn fmt_internal(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let p2pkh_prefix = match self.network() { Network::Mainnet => PUBKEY_ADDRESS_PREFIX_MAIN, - Network::Testnet | Network::Devnet | Network::Regtest => PUBKEY_ADDRESS_PREFIX_TEST, + Network::Testnet | Network::Devnet | Network::Devnet => PUBKEY_ADDRESS_PREFIX_TEST, + Network::Regtest => PUBKEY_ADDRESS_PREFIX_REGTEST, }; let p2sh_prefix = match self.network() { Network::Mainnet => SCRIPT_ADDRESS_PREFIX_MAIN, - Network::Testnet | Network::Devnet | Network::Regtest => SCRIPT_ADDRESS_PREFIX_TEST, + Network::Testnet | Network::Devnet | Network::Devnet => SCRIPT_ADDRESS_PREFIX_TEST, + Network::Regtest => SCRIPT_ADDRESS_PREFIX_REGTEST, }; let hrp = match self.network() { Network::Mainnet => hrp::BC, Network::Testnet | Network::Devnet => hrp::TB, + // Network::Mainnet => hrp::DF, + // Network::Testnet | Network::Devnet => hrp::TF, Network::Regtest => hrp::BCRT, }; let encoding = AddressEncoding { payload: self.payload(), p2pkh_prefix, p2sh_prefix, hrp }; diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index 544350c5..d716a88f 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -36,18 +36,28 @@ pub const DIFFCHANGE_TIMESPAN: u32 = 14 * 24 * 3600; 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 -pub const PUBKEY_ADDRESS_PREFIX_MAIN: u8 = 18; // 0x12 (defichain) +pub const PUBKEY_ADDRESS_PREFIX_MAIN: u8 = 18; // 0x12 (defichain mainnet) + /// Mainnet (bitcoin) script address prefix. // pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 5; // 0x05 -pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 90; // 0x5a (defichain) +pub const SCRIPT_ADDRESS_PREFIX_MAIN: u8 = 90; // 0x5a (defichain mainnet) + /// 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) +pub const PUBKEY_ADDRESS_PREFIX_TEST: u8 = 15; // 0xf (defichain testnet, devnet, changi) + /// 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) +pub const SCRIPT_ADDRESS_PREFIX_TEST: u8 = 128; // 0x80 (defichain testnet, devnet changi) + +/// Regtest pubkey address prefix. +pub const PUBKEY_ADDRESS_PREFIX_REGTEST: u8 = 111; // 0x6f (defichain regtest) +/// Regtest script address prefix. +pub const SCRIPT_ADDRESS_PREFIX_REGTEST: u8 = 196; // 0xc4 (defichain regtest) + /// The maximum allowed script size. pub const MAX_SCRIPT_ELEMENT_SIZE: usize = 520; /// How may blocks between halvings. From 4e4b0ab1f73abb720e618ddbbda35600bf8c5c96 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 16 Sep 2024 16:06:48 +0800 Subject: [PATCH 2/5] Hrp::parse --- bitcoin/src/address/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 82d86448..391f9759 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -377,10 +377,8 @@ impl Address { Network::Regtest => SCRIPT_ADDRESS_PREFIX_REGTEST, }; let hrp = match self.network() { - Network::Mainnet => hrp::BC, - Network::Testnet | Network::Devnet => hrp::TB, - // Network::Mainnet => hrp::DF, - // Network::Testnet | Network::Devnet => hrp::TF, + Network::Mainnet => Hrp::parse("df").unwrap(), + Network::Testnet | Network::Devnet => Hrp::parse("tf").unwrap(), Network::Regtest => hrp::BCRT, }; let encoding = AddressEncoding { payload: self.payload(), p2pkh_prefix, p2sh_prefix, hrp }; From ffa8ad6a9733c23346df3a22586bdc0784527890 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 16 Sep 2024 16:52:51 +0800 Subject: [PATCH 3/5] rename Bitcoin -> Mainnet --- bitcoin/src/address/mod.rs | 22 +++++++++++----------- bitcoin/src/bip32.rs | 30 +++++++++++++++--------------- bitcoin/src/crypto/key.rs | 4 ++-- bitcoin/src/psbt/mod.rs | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 391f9759..607fbe17 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -807,7 +807,7 @@ mod tests { use super::*; use crate::crypto::key::PublicKey; - use crate::network::Network::{Bitcoin, Testnet}; + use crate::network::Network::{Mainnet, Testnet}; fn roundtrips(addr: &Address) { assert_eq!( @@ -835,7 +835,7 @@ mod tests { #[test] fn test_p2pkh_address_58() { let addr = Address::new( - Bitcoin, + Mainnet, Payload::PubkeyHash("162c5ea71c0b23f5b9022ef047c4a86470a5b070".parse().unwrap()), ); @@ -851,7 +851,7 @@ mod tests { #[test] fn test_p2pkh_from_key() { let key = "048d5141948c1702e8c95f438815794b87f706a8d4cd2bffad1dc1570971032c9b6042a0431ded2478b5c9cf2d81c124a5e57347a3c63ef0e7716cf54d613ba183".parse::().unwrap(); - let addr = Address::p2pkh(&key, Bitcoin); + let addr = Address::p2pkh(&key, Mainnet); assert_eq!(&addr.to_string(), "1QJVDzdqb1VpbDK7uDeyVXy9mR27CJiyhY"); let key = "03df154ebfcf29d29cc10d5c2565018bce2d9edbab267c31d2caf44a63056cf99f" @@ -866,7 +866,7 @@ mod tests { #[test] fn test_p2sh_address_58() { let addr = Address::new( - Bitcoin, + Mainnet, Payload::ScriptHash("162c5ea71c0b23f5b9022ef047c4a86470a5b070".parse().unwrap()), ); @@ -900,21 +900,21 @@ mod tests { let mut key = "033bc8c83c52df5712229a2f72206d90192366c36428cb0c12b6af98324d97bfbc" .parse::() .unwrap(); - let addr = Address::p2wpkh(&key, Bitcoin).unwrap(); + let addr = Address::p2wpkh(&key, Mainnet).unwrap(); assert_eq!(&addr.to_string(), "bc1qvzvkjn4q3nszqxrv3nraga2r822xjty3ykvkuw"); assert_eq!(addr.address_type(), Some(AddressType::P2wpkh)); roundtrips(&addr); // Test uncompressed pubkey key.compressed = false; - assert_eq!(Address::p2wpkh(&key, Bitcoin), Err(Error::UncompressedPubkey)); + assert_eq!(Address::p2wpkh(&key, Mainnet), Err(Error::UncompressedPubkey)); } #[test] fn test_p2wsh() { // stolen from Bitcoin transaction 5df912fda4becb1c29e928bec8d64d93e9ba8efa9b5b405bd683c86fd2c65667 let script = ScriptBuf::from_hex("52210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae").unwrap(); - let addr = Address::p2wsh(&script, Bitcoin); + let addr = Address::p2wsh(&script, Mainnet); assert_eq!( &addr.to_string(), "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej" @@ -929,21 +929,21 @@ mod tests { let mut key = "026c468be64d22761c30cd2f12cbc7de255d592d7904b1bab07236897cc4c2e766" .parse::() .unwrap(); - let addr = Address::p2shwpkh(&key, Bitcoin).unwrap(); + let addr = Address::p2shwpkh(&key, Mainnet).unwrap(); assert_eq!(&addr.to_string(), "3QBRmWNqqBGme9er7fMkGqtZtp4gjMFxhE"); assert_eq!(addr.address_type(), Some(AddressType::P2sh)); roundtrips(&addr); // Test uncompressed pubkey key.compressed = false; - assert_eq!(Address::p2wpkh(&key, Bitcoin), Err(Error::UncompressedPubkey)); + assert_eq!(Address::p2wpkh(&key, Mainnet), Err(Error::UncompressedPubkey)); } #[test] fn test_p2shwsh() { // stolen from Bitcoin transaction f9ee2be4df05041d0e0a35d7caa3157495ca4f93b233234c9967b6901dacf7a9 let script = ScriptBuf::from_hex("522103e5529d8eaa3d559903adb2e881eb06c86ac2574ffa503c45f4e942e2a693b33e2102e5f10fcdcdbab211e0af6a481f5532536ec61a5fdbf7183770cf8680fe729d8152ae").unwrap(); - let addr = Address::p2shwsh(&script, Bitcoin); + let addr = Address::p2shwsh(&script, Mainnet); assert_eq!(&addr.to_string(), "36EqgNnsWW94SreZgBWc1ANC6wpFZwirHr"); assert_eq!(addr.address_type(), Some(AddressType::P2sh)); roundtrips(&addr); @@ -956,7 +956,7 @@ mod tests { "654f6ea368e0acdfd92976b7c2103a1b26313f430654f6ea368e0acdfd92976b7c2103a1b26313f4" ); let witness_prog = WitnessProgram::new(WitnessVersion::V13, program.to_vec()).unwrap(); - let addr = Address::new(Bitcoin, Payload::WitnessProgram(witness_prog)); + let addr = Address::new(Mainnet, Payload::WitnessProgram(witness_prog)); roundtrips(&addr); } diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index b1593094..95a21dc9 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -876,7 +876,7 @@ mod tests { use super::ChildNumber::{Hardened, Normal}; use super::*; - use crate::network::Network::{self, Bitcoin}; + use crate::network::Network::{self, Mainnet}; #[test] fn test_parse_derivation_path() { @@ -1052,32 +1052,32 @@ mod tests { let seed = hex!("000102030405060708090a0b0c0d0e0f"); // m - test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m".parse().unwrap(), "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi", "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8"); // m/0h - test_path(&secp, Bitcoin, &seed, "m/0h".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0h".parse().unwrap(), "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7", "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw"); // m/0h/1 - test_path(&secp, Bitcoin, &seed, "m/0h/1".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0h/1".parse().unwrap(), "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs", "xpub6ASuArnXKPbfEwhqN6e3mwBcDTgzisQN1wXN9BJcM47sSikHjJf3UFHKkNAWbWMiGj7Wf5uMash7SyYq527Hqck2AxYysAA7xmALppuCkwQ"); // m/0h/1/2h - test_path(&secp, Bitcoin, &seed, "m/0h/1/2h".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0h/1/2h".parse().unwrap(), "xprv9z4pot5VBttmtdRTWfWQmoH1taj2axGVzFqSb8C9xaxKymcFzXBDptWmT7FwuEzG3ryjH4ktypQSAewRiNMjANTtpgP4mLTj34bhnZX7UiM", "xpub6D4BDPcP2GT577Vvch3R8wDkScZWzQzMMUm3PWbmWvVJrZwQY4VUNgqFJPMM3No2dFDFGTsxxpG5uJh7n7epu4trkrX7x7DogT5Uv6fcLW5"); // m/0h/1/2h/2 - test_path(&secp, Bitcoin, &seed, "m/0h/1/2h/2".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0h/1/2h/2".parse().unwrap(), "xprvA2JDeKCSNNZky6uBCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334", "xpub6FHa3pjLCk84BayeJxFW2SP4XRrFd1JYnxeLeU8EqN3vDfZmbqBqaGJAyiLjTAwm6ZLRQUMv1ZACTj37sR62cfN7fe5JnJ7dh8zL4fiyLHV"); // m/0h/1/2h/2/1000000000 - test_path(&secp, Bitcoin, &seed, "m/0h/1/2h/2/1000000000".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0h/1/2h/2/1000000000".parse().unwrap(), "xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76", "xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy"); } @@ -1088,32 +1088,32 @@ mod tests { let seed = hex!("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"); // m - test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m".parse().unwrap(), "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U", "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB"); // m/0 - test_path(&secp, Bitcoin, &seed, "m/0".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0".parse().unwrap(), "xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt", "xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH"); // m/0/2147483647h - test_path(&secp, Bitcoin, &seed, "m/0/2147483647h".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0/2147483647h".parse().unwrap(), "xprv9wSp6B7kry3Vj9m1zSnLvN3xH8RdsPP1Mh7fAaR7aRLcQMKTR2vidYEeEg2mUCTAwCd6vnxVrcjfy2kRgVsFawNzmjuHc2YmYRmagcEPdU9", "xpub6ASAVgeehLbnwdqV6UKMHVzgqAG8Gr6riv3Fxxpj8ksbH9ebxaEyBLZ85ySDhKiLDBrQSARLq1uNRts8RuJiHjaDMBU4Zn9h8LZNnBC5y4a"); // m/0/2147483647h/1 - test_path(&secp, Bitcoin, &seed, "m/0/2147483647h/1".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0/2147483647h/1".parse().unwrap(), "xprv9zFnWC6h2cLgpmSA46vutJzBcfJ8yaJGg8cX1e5StJh45BBciYTRXSd25UEPVuesF9yog62tGAQtHjXajPPdbRCHuWS6T8XA2ECKADdw4Ef", "xpub6DF8uhdarytz3FWdA8TvFSvvAh8dP3283MY7p2V4SeE2wyWmG5mg5EwVvmdMVCQcoNJxGoWaU9DCWh89LojfZ537wTfunKau47EL2dhHKon"); // m/0/2147483647h/1/2147483646h - test_path(&secp, Bitcoin, &seed, "m/0/2147483647h/1/2147483646h".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0/2147483647h/1/2147483646h".parse().unwrap(), "xprvA1RpRA33e1JQ7ifknakTFpgNXPmW2YvmhqLQYMmrj4xJXXWYpDPS3xz7iAxn8L39njGVyuoseXzU6rcxFLJ8HFsTjSyQbLYnMpCqE2VbFWc", "xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL"); // m/0/2147483647h/1/2147483646h/2 - test_path(&secp, Bitcoin, &seed, "m/0/2147483647h/1/2147483646h/2".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0/2147483647h/1/2147483646h/2".parse().unwrap(), "xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j", "xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt"); } @@ -1124,12 +1124,12 @@ mod tests { let seed = hex!("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be"); // m - test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m".parse().unwrap(), "xprv9s21ZrQH143K25QhxbucbDDuQ4naNntJRi4KUfWT7xo4EKsHt2QJDu7KXp1A3u7Bi1j8ph3EGsZ9Xvz9dGuVrtHHs7pXeTzjuxBrCmmhgC6", "xpub661MyMwAqRbcEZVB4dScxMAdx6d4nFc9nvyvH3v4gJL378CSRZiYmhRoP7mBy6gSPSCYk6SzXPTf3ND1cZAceL7SfJ1Z3GC8vBgp2epUt13"); // m/0h - test_path(&secp, Bitcoin, &seed, "m/0h".parse().unwrap(), + test_path(&secp, Mainnet, &seed, "m/0h".parse().unwrap(), "xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L", "xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y"); } diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index c7a287cc..34fb5f46 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -773,7 +773,7 @@ mod tests { use super::*; use crate::address::Address; - use crate::network::Network::{Bitcoin, Testnet}; + use crate::network::Network::{Mainnet, Testnet}; #[test] fn test_key_derivation() { @@ -797,7 +797,7 @@ mod tests { // mainnet uncompressed let sk = PrivateKey::from_wif("5JYkZjmN7PVMjJUfJWfRFwtuXTGB439XV6faajeHPAM9Z2PT2R3").unwrap(); - assert_eq!(sk.network, Bitcoin); + assert_eq!(sk.network, Mainnet); assert!(!sk.compressed); assert_eq!(&sk.to_wif(), "5JYkZjmN7PVMjJUfJWfRFwtuXTGB439XV6faajeHPAM9Z2PT2R3"); diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index 8dc8663e..73625390 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -1153,7 +1153,7 @@ mod tests { let mut hd_keypaths: BTreeMap = Default::default(); - let mut sk: Xpriv = Xpriv::new_master(Bitcoin, &seed).unwrap(); + let mut sk: Xpriv = Xpriv::new_master(Mainnet, &seed).unwrap(); let fprint = sk.fingerprint(secp); From af1f841ff906cae745d947aa488411e49dc14752 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 16 Sep 2024 16:53:03 +0800 Subject: [PATCH 4/5] test --- bitcoin/src/address/mod.rs | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 607fbe17..30ef983c 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -798,6 +798,58 @@ fn segwit_redeem_hash(pubkey_hash: &PubkeyHash) -> crate::hashes::hash160::Hash crate::hashes::hash160::Hash::from_engine(sha_engine) } +#[test] +fn test_defi_addr() { + // mainnet + println!("mainnet"); + { + let legacy = "8PTeJhFbLR6NFdbXbxMyPVbSHnADbvvBFR"; + let legacy = Address::from_str(legacy); + println!("legacy: {:?}", legacy); + + let p2sh = "dQgtBZ3rUe89p5xB37BY52ouchoq9eKeSU"; + let p2sh = Address::from_str(p2sh); + println!("p2sh: {:?}", p2sh); + + let bech32 = "df1q6qvx8rvw4lw6t7dhhvh25s7dm32yh75ufvzdxt"; + let bech32 = Address::from_str(bech32); + println!("bech32: {:?}", bech32); + } + + // testnet + println!("testnet"); + { + let legacy = "73QYLGdMQp7dtsXEfDV5y1APSGDH5AaNpA"; + let legacy = Address::from_str(legacy); + println!("legacy: {:?}", legacy); + + let p2sh = "ttA5usesnEsseDa2UfRz3HqeDtwEmvMEhr"; + let p2sh = Address::from_str(p2sh); + println!("p2sh: {:?}", p2sh); + + let bech32 = "tf1qv73jmh3x9ydnzvwuhk739dq7fpquyun7msuppu"; + let bech32 = Address::from_str(bech32); + println!("bech32: {:?}", bech32); + } + + // regtest + println!("regtest"); + { + let legacy = "mwjqFtKDbf8w8bRCLBxxjfyBfBNJcaBAdm"; + let legacy = Address::from_str(legacy); + println!("legacy: {:?}", legacy); + + let p2sh = "2NFb7jCNEJPz1CyBA6b97mENJPhm6qpfnKc"; + let p2sh = Address::from_str(p2sh); + println!("p2sh: {:?}", p2sh); + + let bech32 = "bcrt1qeq2g82kj99mqfvnwc2g5w0azzd298q0t84tc6s"; + let bech32 = Address::from_str(bech32); + println!("bech32: {:?}", bech32); + } +} + + #[cfg(test)] mod tests { use core::str::FromStr; From 0b1cc0168353cdf11994f7665f46b0ee4d657275 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Mon, 16 Sep 2024 16:55:04 +0800 Subject: [PATCH 5/5] add missing regtest pubkey scriptaddr prefix --- bitcoin/src/address/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 30ef983c..7de8485c 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -783,6 +783,10 @@ impl FromStr for Address { (Network::Testnet, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap())), SCRIPT_ADDRESS_PREFIX_TEST => (Network::Testnet, Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap())), + PUBKEY_ADDRESS_PREFIX_REGTEST => + (Network::Testnet, Payload::PubkeyHash(PubkeyHash::from_slice(&data[1..]).unwrap())), + SCRIPT_ADDRESS_PREFIX_REGTEST => + (Network::Testnet, Payload::ScriptHash(ScriptHash::from_slice(&data[1..]).unwrap())), x => return Err(ParseError::Base58(base58::Error::InvalidAddressVersion(x))), };