Skip to content

Commit

Permalink
Merge pull request #6 from DeFiCh/canonbrother/defichain-network
Browse files Browse the repository at this point in the history
Fix: rename bitcoin enum and prefix
  • Loading branch information
Jouzo authored Jun 5, 2024
2 parents d00a34d + 93c6333 commit c68ce40
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 131 deletions.
2 changes: 1 addition & 1 deletion bitcoin/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::Mainnet).unwrap();
hprintln!("Address: {}", address).unwrap();

assert_eq!(address.to_string(), "bc1qpx9t9pzzl4qsydmhyt6ctrxxjd4ep549np9993".to_string());
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/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 = bitcoin::Network::Bitcoin;
let network = bitcoin::Network::Mainnet;
println!("Network: {:?}", network);

let seed = Vec::from_hex(seed_hex).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/examples/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
);

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/examples/sign-tx-segwit-v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn senders_keys<C: Signing>(secp: &Secp256k1<C>) -> (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")
}

Expand Down
2 changes: 1 addition & 1 deletion bitcoin/examples/sign-tx-taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn senders_keys<C: Signing>(secp: &Secp256k1<C>) -> 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")
}

Expand Down
72 changes: 36 additions & 36 deletions bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//! # }
//! ```
//!
Expand Down Expand Up @@ -220,15 +220,15 @@ struct AddressInner {
///
/// // variant 1
/// let address: Address<NetworkUnchecked> = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse().unwrap();
/// let address: Address<NetworkChecked> = address.require_network(Network::Bitcoin).unwrap();
/// let address: Address<NetworkChecked> = 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<NetworkChecked> = "32iVBEu4dxkUQk9dJbZUiBiQdmypcEyJRf".parse::<Address<_>>()
/// .unwrap().require_network(Network::Bitcoin).unwrap();
/// .unwrap().require_network(Network::Mainnet).unwrap();
/// ```
///
/// ### Formatting addresses
Expand Down Expand Up @@ -367,16 +367,16 @@ impl<V: NetworkValidation> Address<V> {
/// 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 };
Expand Down Expand Up @@ -648,12 +648,12 @@ impl Address<NetworkUnchecked> {
/// let address: Address<NetworkUnchecked> = "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<NetworkUnchecked> = "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 {
Expand All @@ -664,9 +664,9 @@ impl Address<NetworkUnchecked> {

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,
}
}

Expand Down Expand Up @@ -749,8 +749,8 @@ impl FromStr for Address<NetworkUnchecked> {
// 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
"df" | "DF" => Some(Network::Mainnet),
"tf" | "TF" => Some(Network::Testnet), // this may also be devnet
"bcrt" | "BCRT" => Some(Network::Regtest),
_ => None,
};
Expand All @@ -774,9 +774,9 @@ impl FromStr for Address<NetworkUnchecked> {

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 =>
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -1132,9 +1132,9 @@ mod tests {
.collect::<Vec<_>>();

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 {
Expand Down Expand Up @@ -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"
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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")
);

Expand All @@ -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")
);

Expand All @@ -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)))
);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
14 changes: 7 additions & 7 deletions bitcoin/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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[..]);
Expand Down Expand Up @@ -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 {
Expand All @@ -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[..]);
Expand Down Expand Up @@ -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 },
Expand Down
2 changes: 1 addition & 1 deletion bitcoin/src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading

0 comments on commit c68ce40

Please sign in to comment.