From 1b0988833a34bd45ec945c165dc01677e23c00f2 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Sat, 7 Jan 2023 16:39:11 +0100 Subject: [PATCH] Remove `ToHex` The `ToHex` trait was replaced by either simple `Display`/`LowerHex` where appropriate or `DisplayHex` from `bitcoin_internals` which is faster. This change replaces the usages and removes the trait. --- bitcoin/src/address.rs | 3 +- bitcoin/src/blockdata/constants.rs | 17 ++-- bitcoin/src/blockdata/script.rs | 70 ++++++------- bitcoin/src/blockdata/witness.rs | 19 ++-- bitcoin/src/consensus/encode.rs | 6 +- bitcoin/src/crypto/ecdsa.rs | 5 +- bitcoin/src/crypto/key.rs | 8 +- bitcoin/src/internal_macros.rs | 18 ++-- bitcoin/src/merkle_tree/block.rs | 5 +- bitcoin/src/pow.rs | 24 ++--- bitcoin/src/psbt/raw.rs | 5 +- bitcoin/src/psbt/serialize.rs | 3 +- bitcoin/src/serde_utils.rs | 22 +++- bitcoin/src/sighash.rs | 4 +- bitcoin/src/sign_message.rs | 3 +- bitcoin/src/taproot.rs | 23 +++-- hashes/Cargo.toml | 4 +- hashes/embedded/src/main.rs | 4 +- hashes/src/hash160.rs | 4 +- hashes/src/hex.rs | 158 ++--------------------------- hashes/src/ripemd160.rs | 4 +- hashes/src/sha1.rs | 4 +- hashes/src/sha256.rs | 4 +- hashes/src/sha256d.rs | 4 +- hashes/src/sha256t.rs | 6 +- hashes/src/sha512.rs | 4 +- hashes/src/sha512_256.rs | 4 +- internals/src/hex/display.rs | 17 ++++ 28 files changed, 171 insertions(+), 281 deletions(-) diff --git a/bitcoin/src/address.rs b/bitcoin/src/address.rs index 9caa972f..b1d1460d 100644 --- a/bitcoin/src/address.rs +++ b/bitcoin/src/address.rs @@ -885,7 +885,6 @@ mod tests { use super::*; use crate::crypto::key::PublicKey; - use crate::hashes::hex::ToHex; use crate::internal_macros::{hex, hex_into, hex_script}; use crate::network::constants::Network::{Bitcoin, Testnet}; @@ -1078,7 +1077,7 @@ mod tests { ]; for vector in &valid_vectors { let addr: Address = vector.0.parse().unwrap(); - assert_eq!(&addr.script_pubkey().as_bytes().to_hex(), vector.1); + assert_eq!(&addr.script_pubkey().to_hex_string(), vector.1); roundtrips(&addr); } diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index 3eeb9e03..4e7b8e39 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -192,7 +192,6 @@ impl ChainHash { #[cfg(test)] mod test { use super::*; - use crate::hashes::hex::ToHex; use crate::network::constants::Network; use crate::consensus::encode::serialize; use crate::blockdata::locktime::absolute; @@ -216,7 +215,7 @@ mod test { assert_eq!(gen.output[0].value, 50 * COIN_VALUE); assert_eq!(gen.lock_time, absolute::LockTime::ZERO); - assert_eq!(gen.wtxid().to_hex(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + assert_eq!(gen.wtxid().to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); } #[test] @@ -225,12 +224,12 @@ mod test { assert_eq!(gen.header.version, block::Version::ONE); assert_eq!(gen.header.prev_blockhash, Hash::all_zeros()); - assert_eq!(gen.header.merkle_root.to_hex(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + assert_eq!(gen.header.merkle_root.to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); assert_eq!(gen.header.time, 1231006505); assert_eq!(gen.header.bits, CompactTarget::from_consensus(0x1d00ffff)); assert_eq!(gen.header.nonce, 2083236893); - assert_eq!(gen.header.block_hash().to_hex(), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"); + assert_eq!(gen.header.block_hash().to_string(), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"); } #[test] @@ -238,11 +237,11 @@ mod test { let gen = genesis_block(Network::Testnet); assert_eq!(gen.header.version, block::Version::ONE); assert_eq!(gen.header.prev_blockhash, Hash::all_zeros()); - assert_eq!(gen.header.merkle_root.to_hex(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + assert_eq!(gen.header.merkle_root.to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); assert_eq!(gen.header.time, 1296688602); assert_eq!(gen.header.bits, CompactTarget::from_consensus(0x1d00ffff)); assert_eq!(gen.header.nonce, 414098458); - assert_eq!(gen.header.block_hash().to_hex(), "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"); + assert_eq!(gen.header.block_hash().to_string(), "000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"); } #[test] @@ -250,11 +249,11 @@ mod test { let gen = genesis_block(Network::Signet); assert_eq!(gen.header.version, block::Version::ONE); assert_eq!(gen.header.prev_blockhash, Hash::all_zeros()); - assert_eq!(gen.header.merkle_root.to_hex(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); + assert_eq!(gen.header.merkle_root.to_string(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); assert_eq!(gen.header.time, 1598918400); assert_eq!(gen.header.bits, CompactTarget::from_consensus(0x1e0377ae)); assert_eq!(gen.header.nonce, 52613770); - assert_eq!(gen.header.block_hash().to_hex(), "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6"); + assert_eq!(gen.header.block_hash().to_string(), "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6"); } // The *_chain_hash tests are sanity/regression tests, they verify that the const byte array @@ -304,7 +303,7 @@ mod test { // Test vector taken from: https://github.com/lightning/bolts/blob/master/00-introduction.md #[test] fn mainnet_chain_hash_test_vector() { - let got = ChainHash::using_genesis_block(Network::Bitcoin).to_hex(); + let got = ChainHash::using_genesis_block(Network::Bitcoin).to_string(); let want = "6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000"; assert_eq!(got, want); } diff --git a/bitcoin/src/blockdata/script.rs b/bitcoin/src/blockdata/script.rs index 454d0c35..360ed744 100644 --- a/bitcoin/src/blockdata/script.rs +++ b/bitcoin/src/blockdata/script.rs @@ -53,6 +53,7 @@ use crate::prelude::*; use alloc::rc::Rc; use alloc::sync::Arc; use bitcoin_internals::debug_from_display; +use bitcoin_internals::hex::display::DisplayHex; use crate::io; use core::cmp::Ordering; use core::convert::TryFrom; @@ -61,7 +62,6 @@ use core::{fmt, default::Default}; use core::ops::{Deref, DerefMut, Index, Range, RangeFull, RangeFrom, RangeTo, RangeInclusive, RangeToInclusive}; #[cfg(feature = "rust_v_1_53")] use core::ops::Bound; -use bitcoin_hashes::hex::ToHex; #[cfg(feature = "serde")] use serde; @@ -485,6 +485,15 @@ impl Script { buf } + /// Formats the script as lower-case hex. + /// + /// This is a more convenient and performant way to write `format!("{:x}", script)`. + /// For better performance you should generally prefer displaying the script but if `String` is + /// required (this is common in tests) this method is can be used. + pub fn to_hex_string(&self) -> String { + self.as_bytes().to_lower_hex_string() + } + /// Returns the first opcode of the script (if there is any). pub fn first_opcode(&self) -> Option { self.as_bytes().first().copied().map(From::from) @@ -553,13 +562,6 @@ impl<'a> From<&'a Script> for Rc