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

Support new network mode (Prod and Dev) #46

Merged
merged 5 commits into from
Jun 13, 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
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/tapyrus/deserialize_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::Prod) {
assert_eq!(addr.script_pubkey(), script);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tapyrus/embedded/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::Prod).unwrap();
hprintln!("Address: {}", address).unwrap();

assert_eq!(address.to_string(), "bc1qpx9t9pzzl4qsydmhyt6ctrxxjd4ep549np9993".to_string());
Expand Down
2 changes: 1 addition & 1 deletion tapyrus/examples/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() {
println!("Seed: {}", seed_hex);

// default network as mainnet
let network = tapyrus::Network::Bitcoin;
let network = tapyrus::Network::Prod;
println!("Network: {:?}", network);

let seed = Vec::from_hex(seed_hex).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions tapyrus/examples/ecdsa-psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const RECEIVE_ADDRESS: &str = "bcrt1qcmnpjjjw78yhyjrxtql6lk7pzpujs3h244p7ae"; //
const OUTPUT_AMOUNT_BTC: &str = "1 BTC";
const CHANGE_AMOUNT_BTC: &str = "48.99999 BTC"; // 1000 sat transaction fee.

const NETWORK: Network = Network::Regtest;
const NETWORK: Network = Network::Dev;

fn main() -> Result<()> {
let secp = Secp256k1::new();
Expand Down Expand Up @@ -170,7 +170,7 @@ impl WatchOnly {

/// Creates the PSBT, in BIP174 parlance this is the 'Creater'.
fn create_psbt<C: Verification>(&self, secp: &Secp256k1<C>) -> Result<Psbt> {
let to_address = Address::from_str(RECEIVE_ADDRESS)?.require_network(Network::Regtest)?;
let to_address = Address::from_str(RECEIVE_ADDRESS)?.require_network(Network::Dev)?;
let to_amount = Amount::from_str(OUTPUT_AMOUNT_BTC)?;

let (_, change_address, _) = self.change_address(secp)?;
Expand Down
8 changes: 5 additions & 3 deletions tapyrus/examples/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use std::time::{SystemTime, UNIX_EPOCH};
use std::{env, process};

use tapyrus::consensus::{encode, Decodable};
use tapyrus::p2p::{self, address, message, message_network};
use tapyrus::p2p::{self, address, message, message_network, Magic};
use tapyrus::secp256k1;
use tapyrus::secp256k1::rand::Rng;
use tapyrus::network::NetworkId;

fn main() {
// This example establishes a connection to a Bitcoin node, sends the intial
Expand All @@ -28,8 +29,9 @@ fn main() {

let version_message = build_version_message(address);

let magic = Magic::from_bytes(NetworkId::from(1).magic().to_be_bytes());
let first_message =
message::RawNetworkMessage::new(tapyrus::Network::Bitcoin.magic(), version_message);
message::RawNetworkMessage::new(magic , version_message);

if let Ok(mut stream) = TcpStream::connect(address) {
// Send the message
Expand All @@ -47,7 +49,7 @@ fn main() {
println!("Received version message: {:?}", reply.payload());

let second_message = message::RawNetworkMessage::new(
tapyrus::Network::Bitcoin.magic(),
magic,
message::NetworkMessage::Verack,
);

Expand Down
4 changes: 2 additions & 2 deletions tapyrus/examples/taproot-psbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Just some addresses for outputs from our wallets. Not really important.
let to_address =
Address::from_str("bcrt1p0p3rvwww0v9znrclp00uneq8ytre9kj922v8fxhnezm3mgsmn9usdxaefc")?
.require_network(Network::Regtest)?;
.require_network(Network::Dev)?;
let change_address =
Address::from_str("bcrt1pz449kexzydh2kaypatup5ultru3ej284t6eguhnkn6wkhswt0l7q3a7j76")?
.require_network(Network::Regtest)?;
.require_network(Network::Dev)?;
let amount_to_send_in_sats = Amount::ONE_BTC;
let change_amount = UTXO_1
.amount_in_sats
Expand Down
236 changes: 114 additions & 122 deletions tapyrus/src/address/mod.rs

Large diffs are not rendered by default.

72 changes: 36 additions & 36 deletions tapyrus/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ use crate::io::Write;
use crate::network::Network;
use crate::prelude::*;

/// Version bytes for extended public keys on the Bitcoin network.
const VERSION_BYTES_MAINNET_PUBLIC: [u8; 4] = [0x04, 0x88, 0xB2, 0x1E];
/// Version bytes for extended private keys on the Bitcoin network.
const VERSION_BYTES_MAINNET_PRIVATE: [u8; 4] = [0x04, 0x88, 0xAD, 0xE4];
/// Version bytes for extended public keys on any of the testnet networks.
const VERSION_BYTES_TESTNETS_PUBLIC: [u8; 4] = [0x04, 0x35, 0x87, 0xCF];
/// Version bytes for extended private keys on any of the testnet networks.
const VERSION_BYTES_TESTNETS_PRIVATE: [u8; 4] = [0x04, 0x35, 0x83, 0x94];
/// Version bytes for extended public keys on the Tapyrus network.
const VERSION_BYTES_PROD_PUBLIC: [u8; 4] = [0x04, 0x88, 0xB2, 0x1E];
/// Version bytes for extended private keys on the Tapyrus network.
const VERSION_BYTES_PROD_PRIVATE: [u8; 4] = [0x04, 0x88, 0xAD, 0xE4];
/// Version bytes for extended public keys on any of the dev networks.
const VERSION_BYTES_DEV_PUBLIC: [u8; 4] = [0x04, 0x35, 0x87, 0xCF];
/// Version bytes for extended private keys on any of the dev networks.
const VERSION_BYTES_DEV_PRIVATE: [u8; 4] = [0x04, 0x35, 0x83, 0x94];

/// The old name for xpub, extended public key.
#[deprecated(since = "0.31.0", note = "use xpub instead")]
Expand Down Expand Up @@ -651,10 +651,10 @@ impl Xpriv {
return Err(Error::WrongExtendedKeyLength(data.len()));
}

let network = if data.starts_with(&VERSION_BYTES_MAINNET_PRIVATE) {
Network::Bitcoin
} else if data.starts_with(&VERSION_BYTES_TESTNETS_PRIVATE) {
Network::Testnet
let network = if data.starts_with(&VERSION_BYTES_PROD_PRIVATE) {
Network::Prod
} else if data.starts_with(&VERSION_BYTES_DEV_PRIVATE) {
Network::Dev
} else {
let (b0, b1, b2, b3) = (data[0], data[1], data[2], data[3]);
return Err(Error::UnknownVersion([b0, b1, b2, b3]));
Expand All @@ -678,8 +678,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 | Network::Paradium => VERSION_BYTES_MAINNET_PRIVATE,
Network::Testnet | Network::Signet | Network::Regtest => VERSION_BYTES_TESTNETS_PRIVATE,
Network::Prod => VERSION_BYTES_PROD_PRIVATE,
Network::Dev => VERSION_BYTES_DEV_PRIVATE,
});
ret[4] = self.depth;
ret[5..9].copy_from_slice(&self.parent_fingerprint[..]);
Expand Down Expand Up @@ -783,10 +783,10 @@ impl Xpub {
return Err(Error::WrongExtendedKeyLength(data.len()));
}

let network = if data.starts_with(&VERSION_BYTES_MAINNET_PUBLIC) {
Network::Bitcoin
} else if data.starts_with(&VERSION_BYTES_TESTNETS_PUBLIC) {
Network::Testnet
let network = if data.starts_with(&VERSION_BYTES_PROD_PUBLIC) {
Network::Prod
} else if data.starts_with(&VERSION_BYTES_DEV_PUBLIC) {
Network::Dev
} else {
let (b0, b1, b2, b3) = (data[0], data[1], data[2], data[3]);
return Err(Error::UnknownVersion([b0, b1, b2, b3]));
Expand All @@ -810,8 +810,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 | Network::Paradium => VERSION_BYTES_MAINNET_PUBLIC,
Network::Testnet | Network::Signet | Network::Regtest => VERSION_BYTES_TESTNETS_PUBLIC,
Network::Prod => VERSION_BYTES_PROD_PUBLIC,
Network::Dev => VERSION_BYTES_DEV_PUBLIC,
});
ret[4] = self.depth;
ret[5..9].copy_from_slice(&self.parent_fingerprint[..]);
Expand Down Expand Up @@ -891,7 +891,7 @@ mod tests {

use super::ChildNumber::{Hardened, Normal};
use super::*;
use crate::network::Network::{self, Bitcoin};
use crate::network::Network::{self, Prod};

#[test]
fn test_parse_derivation_path() {
Expand Down Expand Up @@ -1067,32 +1067,32 @@ mod tests {
let seed = hex!("000102030405060708090a0b0c0d0e0f");

// m
test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(),
test_path(&secp, Prod, &seed, "m".parse().unwrap(),
"xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
"xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8");

// m/0h
test_path(&secp, Bitcoin, &seed, "m/0h".parse().unwrap(),
test_path(&secp, Prod, &seed, "m/0h".parse().unwrap(),
"xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7",
"xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw");

// m/0h/1
test_path(&secp, Bitcoin, &seed, "m/0h/1".parse().unwrap(),
test_path(&secp, Prod, &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, Prod, &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, Prod, &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, Prod, &seed, "m/0h/1/2h/2/1000000000".parse().unwrap(),
"xprvA41z7zogVVwxVSgdKUHDy1SKmdb533PjDz7J6N6mV6uS3ze1ai8FHa8kmHScGpWmj4WggLyQjgPie1rFSruoUihUZREPSL39UNdE3BBDu76",
"xpub6H1LXWLaKsWFhvm6RVpEL9P4KfRZSW7abD2ttkWP3SSQvnyA8FSVqNTEcYFgJS2UaFcxupHiYkro49S8yGasTvXEYBVPamhGW6cFJodrTHy");
}
Expand All @@ -1103,32 +1103,32 @@ mod tests {
let seed = hex!("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542");

// m
test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(),
test_path(&secp, Prod, &seed, "m".parse().unwrap(),
"xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U",
"xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB");

// m/0
test_path(&secp, Bitcoin, &seed, "m/0".parse().unwrap(),
test_path(&secp, Prod, &seed, "m/0".parse().unwrap(),
"xprv9vHkqa6EV4sPZHYqZznhT2NPtPCjKuDKGY38FBWLvgaDx45zo9WQRUT3dKYnjwih2yJD9mkrocEZXo1ex8G81dwSM1fwqWpWkeS3v86pgKt",
"xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH");

// m/0/2147483647h
test_path(&secp, Bitcoin, &seed, "m/0/2147483647h".parse().unwrap(),
test_path(&secp, Prod, &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, Prod, &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, Prod, &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, Prod, &seed, "m/0/2147483647h/1/2147483646h/2".parse().unwrap(),
"xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j",
"xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt");
}
Expand All @@ -1139,12 +1139,12 @@ mod tests {
let seed = hex!("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be");

// m
test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(),
test_path(&secp, Prod, &seed, "m".parse().unwrap(),
"xprv9s21ZrQH143K25QhxbucbDDuQ4naNntJRi4KUfWT7xo4EKsHt2QJDu7KXp1A3u7Bi1j8ph3EGsZ9Xvz9dGuVrtHHs7pXeTzjuxBrCmmhgC6",
"xpub661MyMwAqRbcEZVB4dScxMAdx6d4nFc9nvyvH3v4gJL378CSRZiYmhRoP7mBy6gSPSCYk6SzXPTf3ND1cZAceL7SfJ1Z3GC8vBgp2epUt13");

// m/0h
test_path(&secp, Bitcoin, &seed, "m/0h".parse().unwrap(),
test_path(&secp, Prod, &seed, "m/0h".parse().unwrap(),
"xprv9uPDJpEQgRQfDcW7BkF7eTya6RPxXeJCqCJGHuCJ4GiRVLzkTXBAJMu2qaMWPrS7AANYqdq6vcBcBUdJCVVFceUvJFjaPdGZ2y9WACViL4L",
"xpub68NZiKmJWnxxS6aaHmn81bvJeTESw724CRDs6HbuccFQN9Ku14VQrADWgqbhhTHBaohPX4CjNLf9fq9MYo6oDaPPLPxSb7gwQN3ih19Zm4Y");
}
Expand Down Expand Up @@ -1207,7 +1207,7 @@ mod tests {
}

let xpriv = Xpriv {
network: Network::Bitcoin,
network: Network::Prod,
depth: 0,
parent_fingerprint: Default::default(),
child_number: ChildNumber::Normal { index: 0 },
Expand Down
7 changes: 4 additions & 3 deletions tapyrus/src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ use crate::blockdata::transaction::Transaction;
use crate::consensus::encode::serialize_hex;
use crate::consensus::{encode, serialize, Decodable, Encodable};
use crate::crypto::key::PublicKey;
use crate::crypto;
use crate::crypto::schnorr::Signature;
pub use crate::hash_types::BlockHash;
use crate::hash_types::{BlockSigHash, TxMerkleNode, WitnessCommitment, WitnessMerkleNode, Wtxid};
use crate::internal_macros::impl_consensus_encoding;
use crate::prelude::*;
use crate::{crypto, io, merkle_tree, VarInt};
use crate::{io, merkle_tree, VarInt};

/// Bitcoin block header.
///
Expand Down Expand Up @@ -214,6 +215,7 @@ impl HeaderWithoutProof {
xfield: header.xfield.clone(),
}
}

}

impl_consensus_encoding!(
Expand Down Expand Up @@ -640,9 +642,8 @@ impl std::error::Error for ValidationError {

#[cfg(test)]
mod tests {
use std::str::FromStr;

use hex::{test_hex_unwrap as hex, FromHex};
use core::str::FromStr;

use super::*;
use crate::consensus::encode::{deserialize, serialize};
Expand Down
Loading
Loading