diff --git a/CHANGELOG.md b/CHANGELOG.md index eaf3fbc3a..ded8307bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,17 +15,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). The dependency switch does not automatically opt types into `DecodeUntrusted`. - **Breaking:** `dashcore_hashes` re-exports the hash primitives (`sha1`, `sha256`, `sha256d`, `sha512`, `sha512_256`, `ripemd160`, `hash160`, `hmac`, - `siphash24`, `cmp`, `hex`) from `bitcoin_hashes` 0.14. The raw types no longer - implement bincode or accept byte and sequence forms in serde, `Error` is - `FromSliceError`, and `forward_hex`, `backward_hex` and the `schemars` feature - are removed + `siphash24`, `cmp`) from `bitcoin_hashes` 0.14. The raw types no longer + implement bincode or accept byte and sequence forms in serde, and `Error` is + `FromSliceError` +- **Breaking:** hex goes through `hex-conservative` 1.x, so `HexToArrayError` + and `HexToBytesError` become `DecodeFixedLengthBytesError` and + `DecodeVariableLengthBytesError` wherever `from_hex`, `FromStr` or a hex + error variant carried them. The raw hash types still report the 0.2 errors, + found under `dashcore_hashes::bitcoin_hashes::hex` - **Breaking:** `BlsPkBytes::from_hex` and `BlsSigBytes::from_hex` report `DecodeFixedLengthBytesError` instead of `hex::FromHexError`, and `dashcore_rpc_json::HexError` wraps `DecodeVariableLengthBytesError` - **Breaking:** hashes honour the format precision, so `{:.8}` prints 8 digits -- **Breaking:** `hash_x11::Hash` loses `forward_hex`, `backward_hex`, - `from_bytes_ref` and `from_bytes_mut`, `hash_x11::Midstate` is replaced by - `[u8; 32]`, and `n_bytes_hashed()` returns the bytes written +- **Breaking:** `hash_x11::Midstate` is replaced by `[u8; 32]`, and + `hash_x11::HashEngine::n_bytes_hashed()` returns the bytes written - **Breaking:** `EcdsaSighashType` comes from `bitcoin-crypto` and gains a `NonStandard` variant, which `from_consensus` returns for non-standard flags and which displays, parses and serializes as `0xNN`. The `from_standard` error @@ -54,12 +57,23 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). single when checking for the legacy `SIGHASH_SINGLE` bug - `transaction_sighash` in `key-wallet-ffi` hashes non-standard flags as given and rejects flags above `0xff` +- Reject 256-bit child numbers in `key-wallet` with an odd number of hex digits ### Removed - **Breaking:** remove BIP38 from `key-wallet` (the `bip38` feature, `Bip38EncryptedKey`, `Bip38Mode`, `encrypt_private_key`, `generate_intermediate_code` and the `Wallet` BIP38 export/import methods) and the unimplemented `bip38_*` functions from `key-wallet-ffi` +- **Breaking:** remove the `hex` re-export from `dashcore_hashes`. Depend on + `hex-conservative` 1.x directly, where `decode_to_array` and `decode_to_vec` + replace `FromHex` +- **Breaking:** remove `forward_hex` and `backward_hex` from the hash types, + and the `schemars` feature from `dashcore_hashes` +- **Breaking:** remove `from_bytes_ref` and `from_bytes_mut` from + `hash_x11::Hash` +- **Breaking:** remove the `hex` module from `dashcore-private`, and the + `btreemap_byte_values`, `btreemap_as_seq`, `btreemap_as_seq_byte_values` and + `hex_bytes` serde helpers from `dashcore::serde_utils` ## 0.44.0 - 2026-07-01 diff --git a/crypto/src/ecdsa.rs b/crypto/src/ecdsa.rs index dcb9deb82..7f8efcd8c 100644 --- a/crypto/src/ecdsa.rs +++ b/crypto/src/ecdsa.rs @@ -12,8 +12,7 @@ use core::str::FromStr; use core::{fmt, iter}; -use hashes::hex::{self, FromHex}; -use internals::hex::display::DisplayHex; +use hex_conservative::DisplayHex; use internals::write_err; use secp256k1; #[cfg(feature = "serde")] @@ -85,8 +84,8 @@ impl Signature { impl fmt::Display for Signature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::LowerHex::fmt(&self.sig.serialize_der().as_hex(), f)?; - fmt::LowerHex::fmt(&[self.hash_ty.to_consensus_u8()].as_hex(), f) + write!(f, "{:x}", self.sig.serialize_der().as_hex())?; + write!(f, "{:x}", [self.hash_ty.to_consensus_u8()].as_hex()) } } @@ -94,7 +93,7 @@ impl FromStr for Signature { type Err = Error; fn from_str(s: &str) -> Result { - let bytes = Vec::from_hex(s)?; + let bytes = hex_conservative::decode_to_vec(s)?; let (sighash_byte, signature) = bytes.split_last().ok_or(Error::EmptySignature)?; Ok(Signature { sig: secp256k1::ecdsa::Signature::from_der(signature)?, @@ -186,14 +185,14 @@ impl fmt::Display for SerializedSignature { impl fmt::LowerHex for SerializedSignature { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::LowerHex::fmt(&(**self).as_hex(), f) + write!(f, "{:x}", (**self).as_hex()) } } impl fmt::UpperHex for SerializedSignature { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::UpperHex::fmt(&(**self).as_hex(), f) + write!(f, "{:X}", (**self).as_hex()) } } @@ -227,7 +226,7 @@ impl<'a> IntoIterator for &'a SerializedSignature { #[non_exhaustive] pub enum Error { /// Hex encoding error - HexEncoding(hex::HexToBytesError), + HexEncoding(hex_conservative::DecodeVariableLengthBytesError), /// Non-standard sighash type NonStandardSighashType(u32), /// Empty Signature @@ -267,8 +266,36 @@ impl From for Error { } } -impl From for Error { - fn from(err: hex::HexToBytesError) -> Self { +impl From for Error { + fn from(err: hex_conservative::DecodeVariableLengthBytesError) -> Self { Error::HexEncoding(err) } } + +#[cfg(test)] +mod tests { + use super::*; + + // A DER signature with `SIGHASH_ALL` appended. + const SIG_HEX: &str = "3045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c01"; + + /// Formatting flags are ignored, as `hex-conservative` 1.x would otherwise + /// pad and prefix each part of the signature separately. + #[test] + fn display_ignores_format_flags() { + let sig = Signature::from_str(SIG_HEX).unwrap(); + for out in [ + format!("{}", sig), + format!("{:#}", sig), + format!("{:>200}", sig), + format!("{:.4}", sig), + ] { + assert_eq!(out, SIG_HEX); + } + + let serialized = sig.serialize(); + assert_eq!(format!("{:x}", serialized), SIG_HEX); + assert_eq!(format!("{:#x}", serialized), SIG_HEX); + assert_eq!(format!("{:X}", serialized), SIG_HEX.to_uppercase()); + } +} diff --git a/crypto/src/key.rs b/crypto/src/key.rs index a01e8c3a3..0b601e3be 100644 --- a/crypto/src/key.rs +++ b/crypto/src/key.rs @@ -16,8 +16,8 @@ use core::str::FromStr; use std::io; use dash_network::Network; -use hashes::hex::FromHex; -use hashes::{hash160, hash_newtype, hex, Hash as _}; +use hashes::{hash160, hash_newtype, Hash as _}; +use hex_conservative::DisplayHex; use internals::write_err; pub use secp256k1::{self, constants, Keypair, Parity, Secp256k1, Verification, XOnlyPublicKey}; #[cfg(feature = "serde")] @@ -40,7 +40,7 @@ pub enum Error { /// The base58 decoded correctly but the payload was the wrong length. InvalidBase58PayloadLength(usize), /// Hex decoding error - Hex(hex::HexToArrayError), + Hex(hex_conservative::DecodeFixedLengthBytesError), /// `PublicKey` hex should be 66 or 130 digits long. InvalidHexLength(usize), /// Something is not supported based on active features @@ -102,8 +102,8 @@ impl From for Error { } #[doc(hidden)] -impl From for Error { - fn from(e: hex::HexToArrayError) -> Self { +impl From for Error { + fn from(e: hex_conservative::DecodeFixedLengthBytesError) -> Self { Error::Hex(e) } } @@ -312,13 +312,7 @@ pub struct SortKey(u8, [u8; 32], [u8; 32]); impl fmt::Display for PublicKey { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - // TODO: fast hex encoding - self.with_serialized(|bytes| { - for ch in bytes { - write!(f, "{:02x}", ch)?; - } - Ok(()) - }) + self.with_serialized(|bytes| write!(f, "{:x}", bytes.as_hex())) } } @@ -326,8 +320,8 @@ impl FromStr for PublicKey { type Err = Error; fn from_str(s: &str) -> Result { match s.len() { - 66 => PublicKey::from_slice(&<[u8; 33]>::from_hex(s)?), - 130 => PublicKey::from_slice(&<[u8; 65]>::from_hex(s)?), + 66 => PublicKey::from_slice(&hex_conservative::decode_to_array::<33>(s)?), + 130 => PublicKey::from_slice(&hex_conservative::decode_to_array::<65>(s)?), len => Err(Error::InvalidHexLength(len)), } } @@ -699,7 +693,6 @@ impl From for TweakedPublicKey { #[cfg(test)] mod tests { - use hashes::hex::FromHex; use super::*; @@ -715,7 +708,7 @@ mod tests { }; let expected1 = SortKey( 2, - <[u8; 32]>::from_hex( + hex_conservative::decode_to_array::<32>( "ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8", ) .unwrap(), @@ -723,11 +716,11 @@ mod tests { ); let expected2 = SortKey( 4, - <[u8; 32]>::from_hex( + hex_conservative::decode_to_array::<32>( "ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8", ) .unwrap(), - <[u8; 32]>::from_hex( + hex_conservative::decode_to_array::<32>( "1794e7f3d5e420641a3bc690067df5541470c966cbca8c694bf39aa16d836918", ) .unwrap(), diff --git a/dash-spv-ffi/src/bin/ffi_cli.rs b/dash-spv-ffi/src/bin/ffi_cli.rs index a139657fe..424455bbd 100644 --- a/dash-spv-ffi/src/bin/ffi_cli.rs +++ b/dash-spv-ffi/src/bin/ffi_cli.rs @@ -140,11 +140,9 @@ extern "C" fn on_transaction_broadcast_result( relayed_by: u32, _user_data: *mut c_void, ) { - let txid_hex = unsafe { &*txid }.iter().rev().fold(String::new(), |mut acc, b| { - use std::fmt::Write; - let _ = write!(acc, "{:02x}", b); - acc - }); + let mut txid_bytes = unsafe { *txid }; + txid_bytes.reverse(); + let txid_hex = txid_bytes.to_lower_hex_string(); match status { FFIBroadcastStatus::Accepted => { println!("[Broadcast] {} accepted ({} peer(s) relayed it back)", txid_hex, relayed_by) diff --git a/dash/benches/transaction.rs b/dash/benches/transaction.rs index 101cfddbf..f1c97042e 100644 --- a/dash/benches/transaction.rs +++ b/dash/benches/transaction.rs @@ -1,7 +1,6 @@ use criterion::{Criterion, black_box, criterion_group, criterion_main}; use dashcore::blockdata::transaction::Transaction; use dashcore::consensus::{Encodable, deserialize}; -use dashcore::hashes::hex::FromHex; use std::io::Write; const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"; @@ -20,7 +19,7 @@ impl Write for EmptyWrite { } fn bench_transaction_size(c: &mut Criterion) { - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex_conservative::decode_to_vec(SOME_TX).unwrap(); let mut tx: Transaction = deserialize(&raw_tx).unwrap(); c.bench_function("transaction_size", |b| { @@ -31,7 +30,7 @@ fn bench_transaction_size(c: &mut Criterion) { } fn bench_transaction_serialize(c: &mut Criterion) { - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex_conservative::decode_to_vec(SOME_TX).unwrap(); let tx: Transaction = deserialize(&raw_tx).unwrap(); c.bench_function("transaction_serialize", |b| { @@ -45,7 +44,7 @@ fn bench_transaction_serialize(c: &mut Criterion) { } fn bench_transaction_serialize_logic(c: &mut Criterion) { - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex_conservative::decode_to_vec(SOME_TX).unwrap(); let tx: Transaction = deserialize(&raw_tx).unwrap(); c.bench_function("transaction_serialize_logic", |b| { @@ -57,7 +56,7 @@ fn bench_transaction_serialize_logic(c: &mut Criterion) { } fn bench_transaction_deserialize(c: &mut Criterion) { - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex_conservative::decode_to_vec(SOME_TX).unwrap(); c.bench_function("transaction_deserialize", |b| { b.iter(|| { diff --git a/dash/src/bip152.rs b/dash/src/bip152.rs index 50cf991e9..58fee511b 100644 --- a/dash/src/bip152.rs +++ b/dash/src/bip152.rs @@ -373,7 +373,6 @@ impl BlockTransactions { #[cfg(test)] mod test { - use hashes::hex::FromHex; use super::*; use crate::blockdata::locktime::absolute; @@ -442,8 +441,8 @@ mod test { #[test] fn test_compact_block_vector() { // Tested with Elements implementation of compact blocks. - let raw_block = Vec::::from_hex("000000206c750a364035aefd5f81508a08769975116d9195312ee4520dceac39e1fdc62c4dc67473b8e354358c1e610afeaff7410858bd45df43e2940f8a62bd3d5e3ac943c2975cffff7f200000000002020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff04016b0101ffffffff020006062a0100000001510000000000000000266a24aa21a9ed4a3d9f3343dafcc0d6f6d4310f2ee5ce273ed34edca6c75db3a73e7f368734200120000000000000000000000000000000000000000000000000000000000000000000000000020000000001021fc20ba2bd745507b8e00679e3b362558f9457db374ca28ffa5243f4c23a4d5f00000000171600147c9dea14ffbcaec4b575e03f05ceb7a81cd3fcbffdffffff915d689be87b43337f42e26033df59807b768223368f189a023d0242d837768900000000171600147c9dea14ffbcaec4b575e03f05ceb7a81cd3fcbffdffffff0200cdf5050000000017a9146803c72d9154a6a20f404bed6d3dcee07986235a8700e1f5050000000017a9144e6a4c7cb5b5562904843bdf816342f4db9f5797870247304402205e9bf6e70eb0e4b495bf483fd8e6e02da64900f290ef8aaa64bb32600d973c450220670896f5d0e5f33473e5f399ab680cc1d25c2d2afd15abd722f04978f28be887012103e4e4d9312b2261af508b367d8ba9be4f01b61d6d6e78bec499845b4f410bcf2702473044022045ac80596a6ac9c8c572f94708709adaf106677221122e08daf8b9741a04f66a022003ccd52a3b78f8fd08058fc04fc0cffa5f4c196c84eae9e37e2a85babe731b57012103e4e4d9312b2261af508b367d8ba9be4f01b61d6d6e78bec499845b4f410bcf276a000000").unwrap(); - let raw_compact = Vec::::from_hex("000000206c750a364035aefd5f81508a08769975116d9195312ee4520dceac39e1fdc62c4dc67473b8e354358c1e610afeaff7410858bd45df43e2940f8a62bd3d5e3ac943c2975cffff7f2000000000a4df3c3744da89fa010a6979e971450100020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff04016b0101ffffffff020006062a0100000001510000000000000000266a24aa21a9ed4a3d9f3343dafcc0d6f6d4310f2ee5ce273ed34edca6c75db3a73e7f368734200120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let raw_block = hex_conservative::decode_to_vec("000000206c750a364035aefd5f81508a08769975116d9195312ee4520dceac39e1fdc62c4dc67473b8e354358c1e610afeaff7410858bd45df43e2940f8a62bd3d5e3ac943c2975cffff7f200000000002020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff04016b0101ffffffff020006062a0100000001510000000000000000266a24aa21a9ed4a3d9f3343dafcc0d6f6d4310f2ee5ce273ed34edca6c75db3a73e7f368734200120000000000000000000000000000000000000000000000000000000000000000000000000020000000001021fc20ba2bd745507b8e00679e3b362558f9457db374ca28ffa5243f4c23a4d5f00000000171600147c9dea14ffbcaec4b575e03f05ceb7a81cd3fcbffdffffff915d689be87b43337f42e26033df59807b768223368f189a023d0242d837768900000000171600147c9dea14ffbcaec4b575e03f05ceb7a81cd3fcbffdffffff0200cdf5050000000017a9146803c72d9154a6a20f404bed6d3dcee07986235a8700e1f5050000000017a9144e6a4c7cb5b5562904843bdf816342f4db9f5797870247304402205e9bf6e70eb0e4b495bf483fd8e6e02da64900f290ef8aaa64bb32600d973c450220670896f5d0e5f33473e5f399ab680cc1d25c2d2afd15abd722f04978f28be887012103e4e4d9312b2261af508b367d8ba9be4f01b61d6d6e78bec499845b4f410bcf2702473044022045ac80596a6ac9c8c572f94708709adaf106677221122e08daf8b9741a04f66a022003ccd52a3b78f8fd08058fc04fc0cffa5f4c196c84eae9e37e2a85babe731b57012103e4e4d9312b2261af508b367d8ba9be4f01b61d6d6e78bec499845b4f410bcf276a000000").unwrap(); + let raw_compact = hex_conservative::decode_to_vec("000000206c750a364035aefd5f81508a08769975116d9195312ee4520dceac39e1fdc62c4dc67473b8e354358c1e610afeaff7410858bd45df43e2940f8a62bd3d5e3ac943c2975cffff7f2000000000a4df3c3744da89fa010a6979e971450100020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff04016b0101ffffffff020006062a0100000001510000000000000000266a24aa21a9ed4a3d9f3343dafcc0d6f6d4310f2ee5ce273ed34edca6c75db3a73e7f368734200120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); let block: Block = deserialize(&raw_block).unwrap(); let nonce = 18053200567810711460; diff --git a/dash/src/blockdata/block.rs b/dash/src/blockdata/block.rs index a60719fd7..7c410fb5c 100644 --- a/dash/src/blockdata/block.rs +++ b/dash/src/blockdata/block.rs @@ -419,7 +419,6 @@ impl From<&Block> for BlockHash { #[cfg(test)] mod tests { - use hashes::hex::FromHex; use super::*; use crate::consensus::encode::{deserialize, serialize}; @@ -440,7 +439,7 @@ mod tests { const BAD_HEX: &str = "0200000035ab154183570282ce9afc0b494c9fc6a3cfea05aa8c1add2ecc56490000000038ba3d78e4500a5a7570dbe61960398add4410d278b21cd9708e6d9743f374d544fc055227f1001c29c1ea3b0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3d09a08601112233445566000427f1001c046a510100522cfabe6d6d0000000000000000000068692066726f6d20706f6f6c7365727665726aac1eeeed88ffffffff0100f2052a010000001976a914912e2b234f941f30b18afbb4fa46171214bf66c888ac00000000"; let bad: Block = deserialize(&hex!(BAD_HEX)).unwrap(); - let push = Vec::::from_hex("a08601112233445566").unwrap(); + let push = hex_conservative::decode_to_vec("a08601112233445566").unwrap(); assert_eq!(bad.bip34_block_height(), Err(super::Bip34Error::UnexpectedPush(push))); } diff --git a/dash/src/blockdata/script/mod.rs b/dash/src/blockdata/script/mod.rs index 93ea53627..9f84f7cef 100644 --- a/dash/src/blockdata/script/mod.rs +++ b/dash/src/blockdata/script/mod.rs @@ -585,8 +585,6 @@ impl<'de> serde::Deserialize<'de> for ScriptBuf { { use core::fmt::Formatter; - use hashes::hex::FromHex; - if deserializer.is_human_readable() { struct Visitor; impl<'de> serde::de::Visitor<'de> for Visitor { @@ -600,7 +598,7 @@ impl<'de> serde::Deserialize<'de> for ScriptBuf { where E: serde::de::Error, { - let v = Vec::from_hex(v).map_err(E::custom)?; + let v = hex_conservative::decode_to_vec(v).map_err(E::custom)?; Ok(ScriptBuf::from(v)) } } @@ -727,9 +725,9 @@ pub fn bytes_to_asm_fmt(script: &[u8], f: &mut dyn fmt::Write) -> fmt::Result { if data_len > 0 { f.write_str(" ")?; if data_len <= iter.len() { - for ch in iter.by_ref().take(data_len) { - write!(f, "{:02x}", ch)?; - } + let (data, rest) = iter.as_slice().split_at(data_len); + write!(f, "{:x}", data.as_hex())?; + iter = rest.iter(); } else { f.write_str("")?; break; diff --git a/dash/src/blockdata/script/owned.rs b/dash/src/blockdata/script/owned.rs index 77544a4e5..7de1d487f 100644 --- a/dash/src/blockdata/script/owned.rs +++ b/dash/src/blockdata/script/owned.rs @@ -6,7 +6,6 @@ use core::ops::Deref; #[cfg(feature = "bincode")] use bincode::{Decode, Encode}; -use hashes::hex; use crate::address::{WitnessProgram, WitnessVersion}; use crate::blockdata::opcodes::all::*; @@ -168,10 +167,8 @@ impl ScriptBuf { } /// Creates a [`ScriptBuf`] from a hex string. - pub fn from_hex(s: &str) -> Result { - use hashes::hex::FromHex; - - let v = Vec::from_hex(s)?; + pub fn from_hex(s: &str) -> Result { + let v = hex_conservative::decode_to_vec(s)?; Ok(ScriptBuf::from_bytes(v)) } diff --git a/dash/src/blockdata/transaction/mod.rs b/dash/src/blockdata/transaction/mod.rs index e2005b81d..9792790ea 100644 --- a/dash/src/blockdata/transaction/mod.rs +++ b/dash/src/blockdata/transaction/mod.rs @@ -77,7 +77,7 @@ impl EncodeSigningDataResult { /// /// ```rust /// # use dashcore::consensus::deserialize; - /// # use dashcore::hashes::{Hash, hex::FromHex}; + /// # use dashcore::hashes::Hash; /// # use dashcore::sighash::{LegacySighash, SighashCache}; /// # use dashcore::Transaction; /// # let mut writer = LegacySighash::engine(); @@ -85,7 +85,7 @@ impl EncodeSigningDataResult { /// # let script_pubkey = dashcore::ScriptBuf::new(); /// # let sighash_u32 = 0u32; /// # const SOME_TX: &'static str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"; - /// # let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + /// # let raw_tx = hex_conservative::decode_to_vec(SOME_TX).unwrap(); /// # let tx: Transaction = deserialize(&raw_tx).unwrap(); /// let cache = SighashCache::new(&tx); /// if cache.legacy_encode_signing_data_to(&mut writer, input_index, &script_pubkey, sighash_u32) @@ -902,7 +902,6 @@ const fn predict_weight_internal( #[cfg(test)] mod tests { - use hashes::hex::FromHex; use super::*; use crate::blockdata::constants::WITNESS_SCALE_FACTOR; @@ -917,7 +916,7 @@ mod tests { let genesis = constants::genesis_block(Network::Mainnet); assert!(genesis.txdata[0].is_coin_base()); - let tx_bytes = Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); + let tx_bytes = hex_conservative::decode_to_vec("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); let tx: Transaction = deserialize(&tx_bytes).unwrap(); assert!(!tx.is_coin_base()); } @@ -1018,13 +1017,13 @@ mod tests { #[test] fn test_transaction_version() { - let tx_bytes = Vec::from_hex("ffff00000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); + let tx_bytes = hex_conservative::decode_to_vec("ffff00000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); let tx: Result = deserialize(&tx_bytes); assert!(tx.is_ok()); let realtx = tx.unwrap(); assert_eq!(realtx.version, 65535); - let tx2_bytes = Vec::from_hex("000000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); + let tx2_bytes = hex_conservative::decode_to_vec("000000000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); let tx2: Result = deserialize(&tx2_bytes); assert!(tx2.is_ok()); let realtx2 = tx2.unwrap(); @@ -1042,7 +1041,7 @@ mod tests { // Pre-DIP-0002 these bytes were ignored by consensus but still part of the // serialized transaction and therefore part of the txid pre-image. Bytes 2..4 // are the `nTxType` field; bytes 0..2 are version=0. - let tx_bytes = Vec::from_hex("00002a000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); + let tx_bytes = hex_conservative::decode_to_vec("00002a000100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); let tx: Transaction = deserialize(&tx_bytes).expect("decoder must accept pre-DIP-0002 raw type bytes"); assert_eq!(tx.version, 0); @@ -1080,7 +1079,7 @@ mod tests { #[test] fn tx_no_input_deserialization() { - let tx_bytes = Vec::from_hex( + let tx_bytes = hex_conservative::decode_to_vec( "010000000001000100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000" ).unwrap(); let tx: Transaction = deserialize(&tx_bytes).expect("deserialize tx"); @@ -1094,7 +1093,7 @@ mod tests { #[test] fn test_ntxid() { - let tx_bytes = Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); + let tx_bytes = hex_conservative::decode_to_vec("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); let mut tx: Transaction = deserialize(&tx_bytes).unwrap(); let old_ntxid = tx.ntxid(); @@ -1179,7 +1178,7 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_txn_encode_decode() { - let tx_bytes = Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); + let tx_bytes = hex_conservative::decode_to_vec("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); let tx: Transaction = deserialize(&tx_bytes).unwrap(); serde_round_trip!(tx); } @@ -1189,7 +1188,7 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_segwit_tx_decode() { - let tx_bytes = Vec::from_hex("010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3603da1b0e00045503bd5704c7dd8a0d0ced13bb5785010800000000000a636b706f6f6c122f4e696e6a61506f6f6c2f5345475749542fffffffff02b4e5a212000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9edf91c46b49eb8a29089980f02ee6b57e7d63d33b18b4fddac2bcd7db2a39837040120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let tx_bytes = hex_conservative::decode_to_vec("010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3603da1b0e00045503bd5704c7dd8a0d0ced13bb5785010800000000000a636b706f6f6c122f4e696e6a61506f6f6c2f5345475749542fffffffff02b4e5a212000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9edf91c46b49eb8a29089980f02ee6b57e7d63d33b18b4fddac2bcd7db2a39837040120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); let tx: Transaction = deserialize(&tx_bytes).unwrap(); assert_eq!(tx.weight(), Weight::from_wu(780)); serde_round_trip!(tx); @@ -1203,19 +1202,17 @@ mod tests { fn test_transaction_verify() { use std::collections::HashMap; - use hashes::hex::FromHex; - use crate::blockdata::script; use crate::blockdata::witness::Witness; // a random recent segwit transaction from blockchain using both old and segwit inputs - let mut spending: Transaction = deserialize(Vec::from_hex("020000000001031cfbc8f54fbfa4a33a30068841371f80dbfe166211242213188428f437445c91000000006a47304402206fbcec8d2d2e740d824d3d36cc345b37d9f65d665a99f5bd5c9e8d42270a03a8022013959632492332200c2908459547bf8dbf97c65ab1a28dec377d6f1d41d3d63e012103d7279dfb90ce17fe139ba60a7c41ddf605b25e1c07a4ddcb9dfef4e7d6710f48feffffff476222484f5e35b3f0e43f65fc76e21d8be7818dd6a989c160b1e5039b7835fc00000000171600140914414d3c94af70ac7e25407b0689e0baa10c77feffffffa83d954a62568bbc99cc644c62eb7383d7c2a2563041a0aeb891a6a4055895570000000017160014795d04cc2d4f31480d9a3710993fbd80d04301dffeffffff06fef72f000000000017a91476fd7035cd26f1a32a5ab979e056713aac25796887a5000f00000000001976a914b8332d502a529571c6af4be66399cd33379071c588ac3fda0500000000001976a914fc1d692f8de10ae33295f090bea5fe49527d975c88ac522e1b00000000001976a914808406b54d1044c429ac54c0e189b0d8061667e088ac6eb68501000000001976a914dfab6085f3a8fb3e6710206a5a959313c5618f4d88acbba20000000000001976a914eb3026552d7e3f3073457d0bee5d4757de48160d88ac0002483045022100bee24b63212939d33d513e767bc79300051f7a0d433c3fcf1e0e3bf03b9eb1d70220588dc45a9ce3a939103b4459ce47500b64e23ab118dfc03c9caa7d6bfc32b9c601210354fd80328da0f9ae6eef2b3a81f74f9a6f66761fadf96f1d1d22b1fd6845876402483045022100e29c7e3a5efc10da6269e5fc20b6a1cb8beb92130cc52c67e46ef40aaa5cac5f0220644dd1b049727d991aece98a105563416e10a5ac4221abac7d16931842d5c322012103960b87412d6e169f30e12106bdf70122aabb9eb61f455518322a18b920a4dfa887d30700") + let mut spending: Transaction = deserialize(hex_conservative::decode_to_vec("020000000001031cfbc8f54fbfa4a33a30068841371f80dbfe166211242213188428f437445c91000000006a47304402206fbcec8d2d2e740d824d3d36cc345b37d9f65d665a99f5bd5c9e8d42270a03a8022013959632492332200c2908459547bf8dbf97c65ab1a28dec377d6f1d41d3d63e012103d7279dfb90ce17fe139ba60a7c41ddf605b25e1c07a4ddcb9dfef4e7d6710f48feffffff476222484f5e35b3f0e43f65fc76e21d8be7818dd6a989c160b1e5039b7835fc00000000171600140914414d3c94af70ac7e25407b0689e0baa10c77feffffffa83d954a62568bbc99cc644c62eb7383d7c2a2563041a0aeb891a6a4055895570000000017160014795d04cc2d4f31480d9a3710993fbd80d04301dffeffffff06fef72f000000000017a91476fd7035cd26f1a32a5ab979e056713aac25796887a5000f00000000001976a914b8332d502a529571c6af4be66399cd33379071c588ac3fda0500000000001976a914fc1d692f8de10ae33295f090bea5fe49527d975c88ac522e1b00000000001976a914808406b54d1044c429ac54c0e189b0d8061667e088ac6eb68501000000001976a914dfab6085f3a8fb3e6710206a5a959313c5618f4d88acbba20000000000001976a914eb3026552d7e3f3073457d0bee5d4757de48160d88ac0002483045022100bee24b63212939d33d513e767bc79300051f7a0d433c3fcf1e0e3bf03b9eb1d70220588dc45a9ce3a939103b4459ce47500b64e23ab118dfc03c9caa7d6bfc32b9c601210354fd80328da0f9ae6eef2b3a81f74f9a6f66761fadf96f1d1d22b1fd6845876402483045022100e29c7e3a5efc10da6269e5fc20b6a1cb8beb92130cc52c67e46ef40aaa5cac5f0220644dd1b049727d991aece98a105563416e10a5ac4221abac7d16931842d5c322012103960b87412d6e169f30e12106bdf70122aabb9eb61f455518322a18b920a4dfa887d30700") .unwrap().as_slice()).unwrap(); - let spent1: Transaction = deserialize(Vec::from_hex("020000000001040aacd2c49f5f3c0968cfa8caf9d5761436d95385252e3abb4de8f5dcf8a582f20000000017160014bcadb2baea98af0d9a902e53a7e9adff43b191e9feffffff96cd3c93cac3db114aafe753122bd7d1afa5aa4155ae04b3256344ecca69d72001000000171600141d9984579ceb5c67ebfbfb47124f056662fe7adbfeffffffc878dd74d3a44072eae6178bb94b9253177db1a5aaa6d068eb0e4db7631762e20000000017160014df2a48cdc53dae1aba7aa71cb1f9de089d75aac3feffffffe49f99275bc8363f5f593f4eec371c51f62c34ff11cc6d8d778787d340d6896c0100000017160014229b3b297a0587e03375ab4174ef56eeb0968735feffffff03360d0f00000000001976a9149f44b06f6ee92ddbc4686f71afe528c09727a5c788ac24281b00000000001976a9140277b4f68ff20307a2a9f9b4487a38b501eb955888ac227c0000000000001976a9148020cd422f55eef8747a9d418f5441030f7c9c7788ac0247304402204aa3bd9682f9a8e101505f6358aacd1749ecf53a62b8370b97d59243b3d6984f02200384ad449870b0e6e89c92505880411285ecd41cf11e7439b973f13bad97e53901210205b392ffcb83124b1c7ce6dd594688198ef600d34500a7f3552d67947bbe392802473044022033dfd8d190a4ae36b9f60999b217c775b96eb10dee3a1ff50fb6a75325719106022005872e4e36d194e49ced2ebcf8bb9d843d842e7b7e0eb042f4028396088d292f012103c9d7cbf369410b090480de2aa15c6c73d91b9ffa7d88b90724614b70be41e98e0247304402207d952de9e59e4684efed069797e3e2d993e9f98ec8a9ccd599de43005fe3f713022076d190cc93d9513fc061b1ba565afac574e02027c9efbfa1d7b71ab8dbb21e0501210313ad44bc030cc6cb111798c2bf3d2139418d751c1e79ec4e837ce360cc03b97a024730440220029e75edb5e9413eb98d684d62a077b17fa5b7cc19349c1e8cc6c4733b7b7452022048d4b9cae594f03741029ff841e35996ef233701c1ea9aa55c301362ea2e2f68012103590657108a72feb8dc1dec022cf6a230bb23dc7aaa52f4032384853b9f8388baf9d20700") + let spent1: Transaction = deserialize(hex_conservative::decode_to_vec("020000000001040aacd2c49f5f3c0968cfa8caf9d5761436d95385252e3abb4de8f5dcf8a582f20000000017160014bcadb2baea98af0d9a902e53a7e9adff43b191e9feffffff96cd3c93cac3db114aafe753122bd7d1afa5aa4155ae04b3256344ecca69d72001000000171600141d9984579ceb5c67ebfbfb47124f056662fe7adbfeffffffc878dd74d3a44072eae6178bb94b9253177db1a5aaa6d068eb0e4db7631762e20000000017160014df2a48cdc53dae1aba7aa71cb1f9de089d75aac3feffffffe49f99275bc8363f5f593f4eec371c51f62c34ff11cc6d8d778787d340d6896c0100000017160014229b3b297a0587e03375ab4174ef56eeb0968735feffffff03360d0f00000000001976a9149f44b06f6ee92ddbc4686f71afe528c09727a5c788ac24281b00000000001976a9140277b4f68ff20307a2a9f9b4487a38b501eb955888ac227c0000000000001976a9148020cd422f55eef8747a9d418f5441030f7c9c7788ac0247304402204aa3bd9682f9a8e101505f6358aacd1749ecf53a62b8370b97d59243b3d6984f02200384ad449870b0e6e89c92505880411285ecd41cf11e7439b973f13bad97e53901210205b392ffcb83124b1c7ce6dd594688198ef600d34500a7f3552d67947bbe392802473044022033dfd8d190a4ae36b9f60999b217c775b96eb10dee3a1ff50fb6a75325719106022005872e4e36d194e49ced2ebcf8bb9d843d842e7b7e0eb042f4028396088d292f012103c9d7cbf369410b090480de2aa15c6c73d91b9ffa7d88b90724614b70be41e98e0247304402207d952de9e59e4684efed069797e3e2d993e9f98ec8a9ccd599de43005fe3f713022076d190cc93d9513fc061b1ba565afac574e02027c9efbfa1d7b71ab8dbb21e0501210313ad44bc030cc6cb111798c2bf3d2139418d751c1e79ec4e837ce360cc03b97a024730440220029e75edb5e9413eb98d684d62a077b17fa5b7cc19349c1e8cc6c4733b7b7452022048d4b9cae594f03741029ff841e35996ef233701c1ea9aa55c301362ea2e2f68012103590657108a72feb8dc1dec022cf6a230bb23dc7aaa52f4032384853b9f8388baf9d20700") .unwrap().as_slice()).unwrap(); - let spent2: Transaction = deserialize(Vec::from_hex("0200000000010166c3d39490dc827a2594c7b17b7d37445e1f4b372179649cd2ce4475e3641bbb0100000017160014e69aa750e9bff1aca1e32e57328b641b611fc817fdffffff01e87c5d010000000017a914f3890da1b99e44cd3d52f7bcea6a1351658ea7be87024830450221009eb97597953dc288de30060ba02d4e91b2bde1af2ecf679c7f5ab5989549aa8002202a98f8c3bd1a5a31c0d72950dd6e2e3870c6c5819a6c3db740e91ebbbc5ef4800121023f3d3b8e74b807e32217dea2c75c8d0bd46b8665b3a2d9b3cb310959de52a09bc9d20700") + let spent2: Transaction = deserialize(hex_conservative::decode_to_vec("0200000000010166c3d39490dc827a2594c7b17b7d37445e1f4b372179649cd2ce4475e3641bbb0100000017160014e69aa750e9bff1aca1e32e57328b641b611fc817fdffffff01e87c5d010000000017a914f3890da1b99e44cd3d52f7bcea6a1351658ea7be87024830450221009eb97597953dc288de30060ba02d4e91b2bde1af2ecf679c7f5ab5989549aa8002202a98f8c3bd1a5a31c0d72950dd6e2e3870c6c5819a6c3db740e91ebbbc5ef4800121023f3d3b8e74b807e32217dea2c75c8d0bd46b8665b3a2d9b3cb310959de52a09bc9d20700") .unwrap().as_slice()).unwrap(); - let spent3: Transaction = deserialize(Vec::from_hex("01000000027a1120a30cef95422638e8dab9dedf720ec614b1b21e451a4957a5969afb869d000000006a47304402200ecc318a829a6cad4aa9db152adbf09b0cd2de36f47b53f5dade3bc7ef086ca702205722cda7404edd6012eedd79b2d6f24c0a0c657df1a442d0a2166614fb164a4701210372f4b97b34e9c408741cd1fc97bcc7ffdda6941213ccfde1cb4075c0f17aab06ffffffffc23b43e5a18e5a66087c0d5e64d58e8e21fcf83ce3f5e4f7ecb902b0e80a7fb6010000006b483045022100f10076a0ea4b4cf8816ed27a1065883efca230933bf2ff81d5db6258691ff75202206b001ef87624e76244377f57f0c84bc5127d0dd3f6e0ef28b276f176badb223a01210309a3a61776afd39de4ed29b622cd399d99ecd942909c36a8696cfd22fc5b5a1affffffff0200127a000000000017a914f895e1dd9b29cb228e9b06a15204e3b57feaf7cc8769311d09000000001976a9144d00da12aaa51849d2583ae64525d4a06cd70fde88ac00000000") + let spent3: Transaction = deserialize(hex_conservative::decode_to_vec("01000000027a1120a30cef95422638e8dab9dedf720ec614b1b21e451a4957a5969afb869d000000006a47304402200ecc318a829a6cad4aa9db152adbf09b0cd2de36f47b53f5dade3bc7ef086ca702205722cda7404edd6012eedd79b2d6f24c0a0c657df1a442d0a2166614fb164a4701210372f4b97b34e9c408741cd1fc97bcc7ffdda6941213ccfde1cb4075c0f17aab06ffffffffc23b43e5a18e5a66087c0d5e64d58e8e21fcf83ce3f5e4f7ecb902b0e80a7fb6010000006b483045022100f10076a0ea4b4cf8816ed27a1065883efca230933bf2ff81d5db6258691ff75202206b001ef87624e76244377f57f0c84bc5127d0dd3f6e0ef28b276f176badb223a01210309a3a61776afd39de4ed29b622cd399d99ecd942909c36a8696cfd22fc5b5a1affffffff0200127a000000000017a914f895e1dd9b29cb228e9b06a15204e3b57feaf7cc8769311d09000000001976a9144d00da12aaa51849d2583ae64525d4a06cd70fde88ac00000000") .unwrap().as_slice()).unwrap(); let mut spent = HashMap::new(); @@ -1279,7 +1276,8 @@ mod tests { special_transaction_payload: None, }; - let pk_data = Vec::from_hex("b8e2d839dd21088b78bebfea3e3e632181197982").unwrap(); + let pk_data = + hex_conservative::decode_to_vec("b8e2d839dd21088b78bebfea3e3e632181197982").unwrap(); let mut pk_array: [u8; 20] = [0; 20]; for (index, kek) in pk_array.iter_mut().enumerate() { diff --git a/dash/src/blockdata/transaction/outpoint.rs b/dash/src/blockdata/transaction/outpoint.rs index ec042542f..9087e58a0 100644 --- a/dash/src/blockdata/transaction/outpoint.rs +++ b/dash/src/blockdata/transaction/outpoint.rs @@ -152,7 +152,7 @@ impl TryInto> for OutPoint { #[non_exhaustive] pub enum ParseOutPointError { /// Error in TXID part. - Txid(hashes::hex::HexToArrayError), + Txid(hex_conservative::DecodeFixedLengthBytesError), /// Error in vout part. Vout(crate::error::ParseIntError), /// Error in general format. diff --git a/dash/src/blockdata/transaction/special_transaction/asset_unlock/qualified_asset_unlock.rs b/dash/src/blockdata/transaction/special_transaction/asset_unlock/qualified_asset_unlock.rs index 937b754fe..40317bddb 100644 --- a/dash/src/blockdata/transaction/special_transaction/asset_unlock/qualified_asset_unlock.rs +++ b/dash/src/blockdata/transaction/special_transaction/asset_unlock/qualified_asset_unlock.rs @@ -141,8 +141,7 @@ mod tests { use core::str::FromStr; use hashes::Hash; - use internals::hex::Case; - use internals::hex::display::DisplayHex; + use hex_conservative::{Case, DisplayHex}; use crate::bls_sig_utils::BLSSignature; use crate::consensus::Encodable; diff --git a/dash/src/blockdata/transaction/special_transaction/coinbase.rs b/dash/src/blockdata/transaction/special_transaction/coinbase.rs index e19379c4d..d6435c8f0 100644 --- a/dash/src/blockdata/transaction/special_transaction/coinbase.rs +++ b/dash/src/blockdata/transaction/special_transaction/coinbase.rs @@ -264,7 +264,7 @@ mod tests { // This is the exact payload from block 1028171 that was causing the issue let payload_hex = "01004bb00f002176daba0c98fecfa0903fa527d118fbb704c497ee6ab817945e68ba9ba8743b"; - let payload_bytes = hex_decode(payload_hex).unwrap(); + let payload_bytes = hex_conservative::decode_to_vec(payload_hex).unwrap(); // Verify payload is 38 bytes (version 1 should be: 2+4+32 = 38 bytes) assert_eq!(payload_bytes.len(), 38); @@ -403,27 +403,4 @@ mod tests { }; assert!(payload.consensus_encode(&mut Vec::new()).is_err()); } - - fn hex_decode(s: &str) -> Result, &'static str> { - if !s.len().is_multiple_of(2) { - return Err("Hex string has odd length"); - } - - let mut bytes = Vec::with_capacity(s.len() / 2); - for chunk in s.as_bytes().chunks(2) { - let high = hex_digit(chunk[0])?; - let low = hex_digit(chunk[1])?; - bytes.push((high << 4) | low); - } - Ok(bytes) - } - - fn hex_digit(digit: u8) -> Result { - match digit { - b'0'..=b'9' => Ok(digit - b'0'), - b'a'..=b'f' => Ok(digit - b'a' + 10), - b'A'..=b'F' => Ok(digit - b'A' + 10), - _ => Err("Invalid hex digit"), - } - } } diff --git a/dash/src/blockdata/transaction/special_transaction/mnhf_signal.rs b/dash/src/blockdata/transaction/special_transaction/mnhf_signal.rs index eccd4d1a8..efe5505e1 100644 --- a/dash/src/blockdata/transaction/special_transaction/mnhf_signal.rs +++ b/dash/src/blockdata/transaction/special_transaction/mnhf_signal.rs @@ -121,7 +121,7 @@ mod tests { // Test the actual failing payload from the error message // extraPayload: "010bdd1ec5c4a8db99beced78f2c16565d31458bbf4771a55f552900000000000000afc931a000054238f952286289448847d86e25c20b6d357bf2845ed286ecdee426ca53a0f06de790c5b3a8c13913c1ad10da511122f9de8cd98c4af693acda58379fe572c2a8b41e7a860b85653306a6a2c1a6e8e3ba47560f17c1d5bf1a4889" let payload_hex = "010bdd1ec5c4a8db99beced78f2c16565d31458bbf4771a55f552900000000000000afc931a000054238f952286289448847d86e25c20b6d357bf2845ed286ecdee426ca53a0f06de790c5b3a8c13913c1ad10da511122f9de8cd98c4af693acda58379fe572c2a8b41e7a860b85653306a6a2c1a6e8e3ba47560f17c1d5bf1a4889"; - let payload_bytes = hex_decode(payload_hex).unwrap(); + let payload_bytes = hex_conservative::decode_to_vec(payload_hex).unwrap(); // Verify payload is 130 bytes assert_eq!(payload_bytes.len(), 130); @@ -152,27 +152,4 @@ mod tests { payload.consensus_encode(&mut encoded).unwrap(); assert_eq!(encoded, payload_bytes); } - - fn hex_decode(s: &str) -> Result, &'static str> { - if !s.len().is_multiple_of(2) { - return Err("Hex string has odd length"); - } - - let mut bytes = Vec::with_capacity(s.len() / 2); - for chunk in s.as_bytes().chunks(2) { - let high = hex_digit(chunk[0])?; - let low = hex_digit(chunk[1])?; - bytes.push((high << 4) | low); - } - Ok(bytes) - } - - fn hex_digit(digit: u8) -> Result { - match digit { - b'0'..=b'9' => Ok(digit - b'0'), - b'a'..=b'f' => Ok(digit - b'a' + 10), - b'A'..=b'F' => Ok(digit - b'A' + 10), - _ => Err("Invalid hex digit"), - } - } } diff --git a/dash/src/blockdata/transaction/special_transaction/provider_registration.rs b/dash/src/blockdata/transaction/special_transaction/provider_registration.rs index 1973cce76..f3c40f5be 100644 --- a/dash/src/blockdata/transaction/special_transaction/provider_registration.rs +++ b/dash/src/blockdata/transaction/special_transaction/provider_registration.rs @@ -39,7 +39,7 @@ use std::net::SocketAddr; #[cfg(feature = "bincode")] use bincode::{Decode, Encode}; use hashes::Hash; -use internals::hex::Case::Lower; +use hex_conservative::Case::Lower; use crate::address::Payload; use crate::blockdata::transaction::special_transaction::SpecialTransactionBasePayloadEncodable; @@ -308,7 +308,6 @@ mod tests { use crate::bls_sig_utils::BLSPublicKey; use crate::consensus::{Encodable, deserialize}; use crate::hash_types::InputsHash; - use crate::hashes::hex::FromHex; use crate::internal_macros::hex; use crate::transaction::special_transaction::provider_registration::{ ProviderMasternodeType, ProviderRegistrationPayload, @@ -523,7 +522,7 @@ mod tests { // This is a test for testnet let network = Network::Testnet; - let expected_transaction_bytes = Vec::from_hex("030001000379efbe95cba05893d09f4ec51a71171a3852b54aa958ae35ce43276f5f8f1002000000006a473044022015df39c80ca8595cc197a0be692e9d158dc53bdbc8c6abca0d30c086f338c037022063becdb4f891436de3d2fb21cbf294e9dcb5c1a04bc0ba621867479e46d048cc0121030de5cb8989b6902d98017ab4d42b9244912006b0a1561c1d1ba0e2f3117a39adffffffff79efbe95cba05893d09f4ec51a71171a3852b54aa958ae35ce43276f5f8f1002010000006a47304402205c1bae23b459081b060de14133a20378243bebc05c8e2ed9acdabf6717ae7f9702204027ba0abbcce9ba5b2cb563cbff0190ba8f80e5f8fd6beb07c2c449f194c9be01210270b0f0b71472736a397975a84927314261be815d423006d1bcbc00cd693c3d81ffffffff9d925d6cd8e3a408f472e872d1c2849bc664efda8c7f68f1b3a3efde221bc474010000006a47304402203fa23ec33f91efa026b34e90b15a1fd64ff03242a6a92985b16a25b590e5bae002202d1429374b60b1180cd8b9bd0b432158524f5624d6c5d2d6db8c637c9961a21e0121024c0b09e261253dc40ed572c2d63d0b6cda89154583d75a5ab5a14fba81d70089ffffffff0200e87648170000001976a9143795a62df2eb953c1d08bc996d4089ee5d67e28b88ac438ca95a020000001976a91470ed8f5b5cfd4791c15b9d8a7f829cb6a98da18c88ac00000000d101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffff010101014e1f3dd03f9ec192b5f275a433bfc90f468ee1a3eb4c157b10706659e25eb362b5d902d809f9160b1688e201ee6e94b40f9b5062d7074683ef05a2d5efb7793c47059c878dfad38a30fafe61575db40f05ab0a08d55119b0aad300001976a9143795a62df2eb953c1d08bc996d4089ee5d67e28b88ac14b33f2231f0df567e0dfb12899c893f5d2d05f6dcc7d9c8c27b68a71191c75400").unwrap(); + let expected_transaction_bytes = hex_conservative::decode_to_vec("030001000379efbe95cba05893d09f4ec51a71171a3852b54aa958ae35ce43276f5f8f1002000000006a473044022015df39c80ca8595cc197a0be692e9d158dc53bdbc8c6abca0d30c086f338c037022063becdb4f891436de3d2fb21cbf294e9dcb5c1a04bc0ba621867479e46d048cc0121030de5cb8989b6902d98017ab4d42b9244912006b0a1561c1d1ba0e2f3117a39adffffffff79efbe95cba05893d09f4ec51a71171a3852b54aa958ae35ce43276f5f8f1002010000006a47304402205c1bae23b459081b060de14133a20378243bebc05c8e2ed9acdabf6717ae7f9702204027ba0abbcce9ba5b2cb563cbff0190ba8f80e5f8fd6beb07c2c449f194c9be01210270b0f0b71472736a397975a84927314261be815d423006d1bcbc00cd693c3d81ffffffff9d925d6cd8e3a408f472e872d1c2849bc664efda8c7f68f1b3a3efde221bc474010000006a47304402203fa23ec33f91efa026b34e90b15a1fd64ff03242a6a92985b16a25b590e5bae002202d1429374b60b1180cd8b9bd0b432158524f5624d6c5d2d6db8c637c9961a21e0121024c0b09e261253dc40ed572c2d63d0b6cda89154583d75a5ab5a14fba81d70089ffffffff0200e87648170000001976a9143795a62df2eb953c1d08bc996d4089ee5d67e28b88ac438ca95a020000001976a91470ed8f5b5cfd4791c15b9d8a7f829cb6a98da18c88ac00000000d101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffff010101014e1f3dd03f9ec192b5f275a433bfc90f468ee1a3eb4c157b10706659e25eb362b5d902d809f9160b1688e201ee6e94b40f9b5062d7074683ef05a2d5efb7793c47059c878dfad38a30fafe61575db40f05ab0a08d55119b0aad300001976a9143795a62df2eb953c1d08bc996d4089ee5d67e28b88ac14b33f2231f0df567e0dfb12899c893f5d2d05f6dcc7d9c8c27b68a71191c75400").unwrap(); let expected_transaction: Transaction = deserialize(expected_transaction_bytes.as_slice()).expect("expected a transaction"); diff --git a/dash/src/blockdata/transaction/txin.rs b/dash/src/blockdata/transaction/txin.rs index b1077d93d..2c86fa525 100644 --- a/dash/src/blockdata/transaction/txin.rs +++ b/dash/src/blockdata/transaction/txin.rs @@ -87,14 +87,13 @@ impl Decodable for TxIn { #[cfg(test)] mod tests { - use hashes::hex::FromHex; use super::*; use crate::consensus::encode::deserialize; #[test] fn test_txin() { - let txin: Result = deserialize(&Vec::from_hex("a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff").unwrap()); + let txin: Result = deserialize(&hex_conservative::decode_to_vec("a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff").unwrap()); assert!(txin.is_ok()); } diff --git a/dash/src/blockdata/witness.rs b/dash/src/blockdata/witness.rs index 6681b4379..5060d7d68 100644 --- a/dash/src/blockdata/witness.rs +++ b/dash/src/blockdata/witness.rs @@ -428,8 +428,7 @@ impl<'de> serde::Deserialize<'de> for Witness { self, mut a: A, ) -> Result { - use hashes::hex::FromHex; - use hashes::hex::HexToBytesError::*; + use hex_conservative::DecodeVariableLengthBytesError::*; use serde::de::{self, Unexpected}; let mut ret = match a.size_hint() { @@ -438,7 +437,7 @@ impl<'de> serde::Deserialize<'de> for Witness { }; while let Some(elem) = a.next_element::()? { - let vec = Vec::::from_hex(&elem).map_err(|e| match e { + let vec = hex_conservative::decode_to_vec(&elem).map_err(|e| match e { InvalidChar(e) => { let b = e.invalid_char(); match core::char::from_u32(b.into()) { diff --git a/dash/src/consensus/encode.rs b/dash/src/consensus/encode.rs index ec2b9bcde..9b4a718da 100644 --- a/dash/src/consensus/encode.rs +++ b/dash/src/consensus/encode.rs @@ -107,7 +107,7 @@ pub enum Error { /// The script type was non standard NonStandardScriptPayout(ScriptBuf), /// Hex error - Hex(hashes::hex::HexToArrayError), + Hex(hex_conservative::DecodeFixedLengthBytesError), /// Address error Address(address::Error), /// Invalid enum value diff --git a/dash/src/consensus/serde.rs b/dash/src/consensus/serde.rs index 0cc35965c..c951cd0bd 100644 --- a/dash/src/consensus/serde.rs +++ b/dash/src/consensus/serde.rs @@ -37,7 +37,7 @@ pub mod hex { use core::fmt; use core::marker::PhantomData; - use internals::hex::BufEncoder; + use hex_conservative::buf_encoder::BufEncoder; /// Marker for upper/lower case type-level flags ("type-level enum"). /// @@ -55,15 +55,15 @@ pub mod hex { mod sealed { pub trait Case { /// Internal detail, don't depend on it!!! - const INTERNAL_CASE: internals::hex::Case; + const INTERNAL_CASE: hex_conservative::Case; } impl Case for super::Lower { - const INTERNAL_CASE: internals::hex::Case = internals::hex::Case::Lower; + const INTERNAL_CASE: hex_conservative::Case = hex_conservative::Case::Lower; } impl Case for super::Upper { - const INTERNAL_CASE: internals::hex::Case = internals::hex::Case::Upper; + const INTERNAL_CASE: hex_conservative::Case = hex_conservative::Case::Upper; } } @@ -72,11 +72,11 @@ pub mod hex { /// Hex byte encoder. // We wrap `BufEncoder` to not leak internal representation. - pub struct Encoder(BufEncoder<[u8; HEX_BUF_SIZE]>, PhantomData); + pub struct Encoder(BufEncoder, PhantomData); impl From> for Encoder { fn from(_: super::Hex) -> Self { - Encoder(BufEncoder::new([0; HEX_BUF_SIZE]), Default::default()) + Encoder(BufEncoder::new(C::INTERNAL_CASE), Default::default()) } } @@ -86,7 +86,7 @@ pub mod hex { if self.0.is_full() { self.flush(writer)?; } - bytes = self.0.put_bytes_min(bytes, C::INTERNAL_CASE); + bytes = self.0.put_bytes_min(bytes); } Ok(()) } @@ -103,20 +103,20 @@ pub mod hex { /// Error returned when a hex string decoder can't be created. #[derive(Debug)] - pub struct DecodeInitError(hashes::hex::OddLengthStringError); + pub struct DecodeInitError(hex_conservative::OddLengthStringError); /// Error returned when a hex string contains invalid characters. #[derive(Debug)] - pub struct DecodeError(hashes::hex::InvalidCharError); + pub struct DecodeError(hex_conservative::InvalidCharError); /// Hex decoder state. pub struct Decoder<'a>( - Box> + 'a>, + Box> + 'a>, ); impl<'a> Decoder<'a> { fn new(s: &'a str) -> Result { - match hashes::hex::HexToBytesIter::new(s) { + match hex_conservative::HexSliceToBytesIter::new(s) { Ok(iter) => Ok(Decoder(Box::new(iter))), Err(error) => Err(DecodeInitError(error)), } diff --git a/dash/src/crypto/sighash.rs b/dash/src/crypto/sighash.rs index 2440d1cf6..8d42b06f7 100644 --- a/dash/src/crypto/sighash.rs +++ b/dash/src/crypto/sighash.rs @@ -1231,7 +1231,8 @@ mod tests { // All our tests use the default `0xFFFFFFFF` codeseparator value let leaf_hash = leaf_hash.map(|lh| (lh, 0xFFFFFFFF)); - let prevouts = if sighash_type.split_anyonecanpay_flag().1 && tx_bytes[0] % 2 == 0 { + let prevouts = if sighash_type.split_anyonecanpay_flag().1 && tx_bytes[0].is_multiple_of(2) + { // for anyonecanpay the `Prevouts::All` variant is good anyway, but sometimes we want to // test other codepaths Prevouts::One(input_index, prevouts[input_index].clone()) diff --git a/dash/src/hash_types.rs b/dash/src/hash_types.rs index b7bbee7e4..afe382fed 100644 --- a/dash/src/hash_types.rs +++ b/dash/src/hash_types.rs @@ -70,18 +70,48 @@ mod newtypes { pub use dashcore_crypto::eddsa::EddsaPkHash; pub use dashcore_crypto::key::{PubkeyHash, WPubkeyHash}; - use core::str::FromStr; use std::cmp::Ordering; #[cfg(feature = "core-block-hash-use-x11")] use hashes::hash_x11; - use hashes::hex::HexToArrayError as Error; use hashes::{Hash, hash_newtype, hash_newtype_no_ord, hash160, sha256, sha256d}; + use hex_conservative::DecodeFixedLengthBytesError as Error; use crate::alloc::string::ToString; use crate::prelude::String; use crate::transaction::special_transaction::quorum_commitment::QuorumEntry; + /// Parses hex in the inner hash's display order. + /// + /// The `from_hex`/`to_hex` pairs below follow the wrapped hash, which is + /// not always the newtype's own `FromStr` order (see `ProTxHash`). The + /// inner hash's `FromStr` would report a `hex-conservative` 0.2 error, so + /// decode here and flip the bytes the way it would. + fn from_inner_hex>(s: &str) -> Result { + let mut bytes: [u8; 32] = hex_conservative::decode_to_array(s)?; + if H::DISPLAY_BACKWARD { + bytes.reverse(); + } + Ok(H::from_byte_array(bytes)) + } + + /// Adds `from_hex`/`to_hex`, both in the wrapped hash's display order. + macro_rules! impl_inner_hex { + ($($newtype:ident($inner:ty)),* $(,)?) => {$( + impl $newtype { + /// Parses hex in the wrapped hash's display order. + pub fn from_hex(s: &str) -> Result { + from_inner_hex::<$inner>(s).map(Self) + } + + /// Formats as hex in the wrapped hash's display order. + pub fn to_hex(&self) -> String { + self.0.to_string() + } + } + )*}; + } + #[cfg(feature = "core-block-hash-use-x11")] hash_newtype! { /// A dash block hash. @@ -237,41 +267,7 @@ mod newtypes { } } - impl Txid { - /// Create a Txid from a string - pub fn from_hex(s: &str) -> Result { - Ok(Self(sha256d::Hash::from_str(s)?)) - } - - /// Convert a Txid to a string - pub fn to_hex(&self) -> String { - self.0.to_string() - } - } - - impl ProTxHash { - /// Create a ProTxHash from a string - pub fn from_hex(s: &str) -> Result { - Ok(Self(sha256d::Hash::from_str(s)?)) - } - - /// Convert a ProTxHash to a string - pub fn to_hex(&self) -> String { - self.0.to_string() - } - } - impl ScoreHash { - /// Create a ScoreHash from a string - pub fn from_hex(s: &str) -> Result { - Ok(Self(sha256::Hash::from_str(s)?)) - } - - /// Convert a ScoreHash to a string - pub fn to_hex(&self) -> String { - self.0.to_string() - } - /// Creates a score based on the optional confirmed hash and the quorum modifier. /// /// # Arguments @@ -296,16 +292,6 @@ mod newtypes { } impl QuorumOrderingHash { - /// Create a ScoreHash from a string - pub fn from_hex(s: &str) -> Result { - Ok(Self(sha256d::Hash::from_str(s)?)) - } - - /// Convert a ScoreHash to a string - pub fn to_hex(&self) -> String { - self.0.to_string() - } - /// Creates an ordering hash based on the quorum and request id. /// /// # Arguments @@ -328,29 +314,7 @@ mod newtypes { } } - impl ConfirmedHash { - /// Create a ConfirmedHash from a string - pub fn from_hex(s: &str) -> Result { - Ok(Self(sha256d::Hash::from_str(s)?)) - } - - /// Convert a ConfirmedHash to a string - pub fn to_hex(&self) -> String { - self.0.to_string() - } - } - impl ConfirmedHashHashedWithProRegTx { - /// Create a ConfirmedHash from a string - pub fn from_hex(s: &str) -> Result { - Ok(Self(sha256::Hash::from_str(s)?)) - } - - /// Convert a ConfirmedHash to a string - pub fn to_hex(&self) -> String { - self.0.to_string() - } - /// Hashes the members pub fn hash_members(pro_tx_hash: &ProTxHash, confirmed_hash: &ConfirmedHash) -> Self { Self::hash(&[pro_tx_hash.to_byte_array(), confirmed_hash.to_byte_array()].concat()) @@ -366,29 +330,16 @@ mod newtypes { } } - impl Sha256dHash { - /// Create a Sha256dHash from a string - pub fn from_hex(s: &str) -> Result { - Ok(Self(sha256d::Hash::from_str(s)?)) - } - - /// Convert a ConfirmedHash to a string - pub fn to_hex(&self) -> String { - self.0.to_string() - } - } - - impl InputsHash { - /// Create an InputsHash from a string - pub fn from_hex(s: &str) -> Result { - Ok(Self(sha256d::Hash::from_str(s)?)) - } - - /// Convert an InputsHash to a string - pub fn to_hex(&self) -> String { - self.0.to_string() - } - } + impl_inner_hex!( + Txid(sha256d::Hash), + ProTxHash(sha256d::Hash), + ScoreHash(sha256::Hash), + QuorumOrderingHash(sha256d::Hash), + ConfirmedHash(sha256d::Hash), + ConfirmedHashHashedWithProRegTx(sha256::Hash), + Sha256dHash(sha256d::Hash), + InputsHash(sha256d::Hash), + ); impl SpecialTransactionPayloadHash { /// Create a SpecialTransactionPayloadHash from a string @@ -405,9 +356,29 @@ mod tests { use super::*; use crate::consensus::{deserialize, serialize}; use dashcore_crypto::eddsa::EddsaPkBytes; + use hashes::{sha256, sha256d}; use serde_derive::{Deserialize, Serialize}; + #[test] + fn from_hex_follows_the_inner_hash() { + let s = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"; + let d = sha256d::Hash::from_str(s).unwrap(); + let single = sha256::Hash::from_str(s).unwrap(); + + assert_eq!(Txid::from_hex(s).unwrap(), Txid::from_raw_hash(d)); + assert_eq!(ProTxHash::from_hex(s).unwrap(), ProTxHash::from_raw_hash(d)); + assert_eq!(ScoreHash::from_hex(s).unwrap(), ScoreHash::from_raw_hash(single)); + + // `ProTxHash` displays forward, so its `FromStr` reads the other way + // round. + assert_ne!(ProTxHash::from_hex(s).unwrap(), ProTxHash::from_str(s).unwrap()); + assert_eq!(ProTxHash::from_hex(s).unwrap().to_hex(), s); + + assert!(Txid::from_hex(&s[2..]).is_err()); + assert!(Txid::from_hex(&s.replace('1', "g")).is_err()); + } + #[test] fn consensus_round_trip() { let mut wire = [0u8; 20]; diff --git a/dash/src/internal_macros.rs b/dash/src/internal_macros.rs index 2c893869d..80189eb5e 100644 --- a/dash/src/internal_macros.rs +++ b/dash/src/internal_macros.rs @@ -49,13 +49,26 @@ pub(crate) use impl_consensus_encoding; #[cfg(test)] pub(crate) use test_macros::*; +/// Formats `$len` bytes as hex in integer style. +/// +/// Right aligned by default and `0x` under `{:#}`, with precision ignored. +/// Formatting through `hex_conservative` directly would pad like a string and +/// change how these types print with a width. +macro_rules! fmt_hex_exact { + ($f:expr, $len:expr, $bytes:expr, $case:expr) => {{ + let mut encoder = hex_conservative::buf_encoder::BufEncoder::<{ $len * 2 }>::new($case); + encoder.put_bytes($bytes); + $f.pad_integral(true, "0x", encoder.as_str()) + }}; +} +pub(crate) use fmt_hex_exact; + /// Implements several traits for byte-based newtypes. /// Implements: /// - core::fmt::LowerHex /// - core::fmt::UpperHex /// - core::fmt::Display /// - core::str::FromStr -/// - hashes::hex::FromHex macro_rules! impl_bytes_newtype { ($t:ident, $len:literal) => { impl $t { @@ -79,15 +92,23 @@ macro_rules! impl_bytes_newtype { impl core::fmt::LowerHex for $t { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - use internals::hex::{Case, display}; - display::fmt_hex_exact!(f, $len, &self.0, Case::Lower) + $crate::internal_macros::fmt_hex_exact!( + f, + $len, + &self.0, + hex_conservative::Case::Lower + ) } } impl core::fmt::UpperHex for $t { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - use internals::hex::{Case, display}; - display::fmt_hex_exact!(f, $len, &self.0, Case::Upper) + $crate::internal_macros::fmt_hex_exact!( + f, + $len, + &self.0, + hex_conservative::Case::Upper + ) } } @@ -103,18 +124,10 @@ macro_rules! impl_bytes_newtype { } } - impl $crate::hashes::hex::FromHex for $t { - type Error = $crate::hashes::hex::HexToArrayError; - - fn from_hex(s: &str) -> Result { - Ok($t(<[u8; $len] as $crate::hashes::hex::FromHex>::from_hex(s)?)) - } - } - impl core::str::FromStr for $t { - type Err = $crate::hashes::hex::HexToArrayError; + type Err = hex_conservative::DecodeFixedLengthBytesError; fn from_str(s: &str) -> Result { - $crate::hashes::hex::FromHex::from_hex(s) + Ok($t(hex_conservative::decode_to_array(s)?)) } } @@ -149,7 +162,7 @@ macro_rules! impl_bytes_newtype { use $crate::serde::de::Unexpected; if let Ok(hex) = core::str::from_utf8(v) { - $crate::hashes::hex::FromHex::from_hex(hex).map_err(E::custom) + hex.parse().map_err(E::custom) } else { return Err(E::invalid_value(Unexpected::Bytes(v), &self)); } @@ -159,7 +172,7 @@ macro_rules! impl_bytes_newtype { where E: $crate::serde::de::Error, { - $crate::hashes::hex::FromHex::from_hex(v).map_err(E::custom) + v.parse().map_err(E::custom) } } @@ -199,6 +212,6 @@ pub(crate) use impl_bytes_newtype; #[cfg(test)] mod test_macros { - macro_rules! hex (($hex:expr) => ( as hashes::hex::FromHex>::from_hex($hex).unwrap())); + macro_rules! hex (($hex:expr) => (hex_conservative::decode_to_vec($hex).unwrap())); pub(crate) use hex; } diff --git a/dash/src/lib.rs b/dash/src/lib.rs index a59ead5a9..622afcf44 100644 --- a/dash/src/lib.rs +++ b/dash/src/lib.rs @@ -157,7 +157,7 @@ pub mod prelude { pub use std::io::sink; - pub use internals::hex::display::DisplayHex; + pub use hex_conservative::DisplayHex; pub type CoreBlockHeight = u32; } diff --git a/dash/src/merkle_tree/block.rs b/dash/src/merkle_tree/block.rs index 264977984..88bab20a6 100644 --- a/dash/src/merkle_tree/block.rs +++ b/dash/src/merkle_tree/block.rs @@ -15,13 +15,12 @@ //! //! ```rust //! use dashcore::hash_types::Txid; -//! use dashcore::hashes::hex::FromHex; //! use dashcore::{Block, MerkleBlock}; //! //! // Get the proof from a dashcored by running in the terminal: //! // $ TXID="5a4ebf66822b0b2d56bd9dc64ece0bc38ee7844a23ff1d7320a88c5fdb2ad3e2" //! // $ dashcore-cli gettxoutproof [\"$TXID\"] -//! let mb_bytes = Vec::from_hex("01000000ba8b9cda965dd8e536670f9ddec10e53aab14b20bacad27b913719\ +//! let mb_bytes = hex_conservative::decode_to_vec("01000000ba8b9cda965dd8e536670f9ddec10e53aab14b20bacad27b913719\ //! 0000000000190760b278fe7b8565fda3b968b918d5fd997f993b23674c0af3b6fde300b38f33a5914ce6ed5b\ //! 1b01e32f570200000002252bf9d75c4f481ebb6278d708257d1f12beb6dd30301d26c623f789b2ba6fc0e2d3\ //! 2adb5f8ca820731dff234a84e78ec30bce4ec69dbd562d0b2b8266bf4e5a0105").unwrap(); @@ -75,11 +74,10 @@ impl MerkleBlock { /// /// ```rust /// use dashcore::hash_types::Txid; - /// use dashcore::hashes::hex::FromHex; /// use dashcore::{Block, MerkleBlock}; /// /// // Block 80000 - /// let block_bytes = Vec::from_hex("01000000ba8b9cda965dd8e536670f9ddec10e53aab14b20bacad2\ + /// let block_bytes = hex_conservative::decode_to_vec("01000000ba8b9cda965dd8e536670f9ddec10e53aab14b20bacad2\ /// 7b9137190000000000190760b278fe7b8565fda3b968b918d5fd997f993b23674c0af3b6fde300b38f33\ /// a5914ce6ed5b1b01e32f5702010000000100000000000000000000000000000000000000000000000000\ /// 00000000000000ffffffff0704e6ed5b1b014effffffff0100f2052a01000000434104b68a50eaa0287e\ @@ -235,7 +233,6 @@ impl PartialMerkleTree { /// /// ```rust /// use dashcore::hash_types::Txid; - /// use dashcore::hashes::hex::FromHex; /// use dashcore::merkle_tree::{MerkleBlock, PartialMerkleTree}; /// /// // Block 80000 @@ -771,8 +768,7 @@ mod tests { /// Returns a real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af) /// with 9 txs. fn get_block_13b8a() -> Block { - use hashes::hex::FromHex; let block_hex = include_str!("../../tests/data/block_13b8a.hex"); - deserialize(&Vec::from_hex(block_hex).unwrap()).unwrap() + deserialize(&hex_conservative::decode_to_vec(block_hex).unwrap()).unwrap() } } diff --git a/dash/src/network/address.rs b/dash/src/network/address.rs index 3d7ef89a7..2986876bc 100644 --- a/dash/src/network/address.rs +++ b/dash/src/network/address.rs @@ -341,8 +341,6 @@ mod test { use core::str::FromStr; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; - use hashes::hex::FromHex; - use super::{AddrV2, AddrV2Message, Address}; use crate::consensus::encode::{deserialize, serialize}; use crate::internal_macros::hex; @@ -446,12 +444,14 @@ mod test { AddrV2::Ipv6(Ipv6Addr::from_str("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b").unwrap()); assert_eq!(serialize(&ip), hex!("02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b")); - let ip = AddrV2::TorV2(FromHex::from_hex("f1f2f3f4f5f6f7f8f9fa").unwrap()); + let ip = AddrV2::TorV2(hex_conservative::decode_to_array("f1f2f3f4f5f6f7f8f9fa").unwrap()); assert_eq!(serialize(&ip), hex!("030af1f2f3f4f5f6f7f8f9fa")); let ip = AddrV2::TorV3( - FromHex::from_hex("53cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88") - .unwrap(), + hex_conservative::decode_to_array( + "53cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88", + ) + .unwrap(), ); assert_eq!( serialize(&ip), @@ -459,8 +459,10 @@ mod test { ); let ip = AddrV2::I2p( - FromHex::from_hex("a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87") - .unwrap(), + hex_conservative::decode_to_array( + "a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87", + ) + .unwrap(), ); assert_eq!( serialize(&ip), @@ -509,7 +511,10 @@ mod test { // Valid TORv2. let ip: AddrV2 = deserialize(&hex!("030af1f2f3f4f5f6f7f8f9fa")).unwrap(); - assert_eq!(ip, AddrV2::TorV2(FromHex::from_hex("f1f2f3f4f5f6f7f8f9fa").unwrap())); + assert_eq!( + ip, + AddrV2::TorV2(hex_conservative::decode_to_array("f1f2f3f4f5f6f7f8f9fa").unwrap()) + ); // Invalid TORv2, with bogus length. assert!(deserialize::(&hex!("030700")).is_err()); @@ -522,7 +527,7 @@ mod test { assert_eq!( ip, AddrV2::TorV3( - FromHex::from_hex( + hex_conservative::decode_to_array( "79bcc625184b05194975c28b66b66b0469f7f6556fb1ac3189a79b40dda32f1f" ) .unwrap() @@ -540,7 +545,7 @@ mod test { assert_eq!( ip, AddrV2::I2p( - FromHex::from_hex( + hex_conservative::decode_to_array( "a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87" ) .unwrap() diff --git a/dash/src/network/message_headers2.rs b/dash/src/network/message_headers2.rs index 156c3df3c..4cba71276 100644 --- a/dash/src/network/message_headers2.rs +++ b/dash/src/network/message_headers2.rs @@ -940,7 +940,6 @@ mod tests { #[test] fn test_headers2_message_capacity_overflow() { use crate::consensus::encode::deserialize; - use crate::hashes::hex::FromHex; use crate::network::message::RawNetworkMessage; let crash_inputs: &[&str] = &[ @@ -949,7 +948,7 @@ mod tests { ]; for hex in crash_inputs { - let data = Vec::from_hex(hex).expect("valid hex"); + let data = hex_conservative::decode_to_vec(hex).expect("valid hex"); let result = deserialize::(&data); assert!(result.is_err(), "should return Err, not panic"); } diff --git a/dash/src/pow.rs b/dash/src/pow.rs index 06456db69..d0af91e81 100644 --- a/dash/src/pow.rs +++ b/dash/src/pow.rs @@ -815,13 +815,13 @@ macro_rules! impl_hex { ($hex:ident, $case:expr) => { impl $hex for U256 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - internals::hex::display::fmt_hex_exact!(f, 32, &self.to_be_bytes(), $case) + crate::internal_macros::fmt_hex_exact!(f, 32, &self.to_be_bytes(), $case) } } }; } -impl_hex!(LowerHex, internals::hex::Case::Lower); -impl_hex!(UpperHex, internals::hex::Case::Upper); +impl_hex!(LowerHex, hex_conservative::Case::Lower); +impl_hex!(UpperHex, hex_conservative::Case::Upper); #[cfg(feature = "serde")] impl crate::serde::Serialize for U256 { @@ -850,8 +850,6 @@ impl crate::serde::Serialize for U256 { #[cfg(feature = "serde")] impl<'de> crate::serde::Deserialize<'de> for U256 { fn deserialize>(d: D) -> Result { - use hashes::hex::FromHex; - use crate::serde::de; if d.is_human_readable() { @@ -872,7 +870,7 @@ impl<'de> crate::serde::Deserialize<'de> for U256 { return Err(de::Error::invalid_length(s.len(), &self)); } - let b = <[u8; 32]>::from_hex(s) + let b = hex_conservative::decode_to_array::<32>(s) .map_err(|_| de::Error::invalid_value(de::Unexpected::Str(s), &self))?; Ok(U256::from_be_bytes(b)) @@ -883,7 +881,7 @@ impl<'de> crate::serde::Deserialize<'de> for U256 { E: de::Error, { if let Ok(hex) = core::str::from_utf8(v) { - let b = <[u8; 32]>::from_hex(hex).map_err(|_| { + let b = hex_conservative::decode_to_array::<32>(hex).map_err(|_| { de::Error::invalid_value(de::Unexpected::Str(hex), &self) })?; @@ -1055,6 +1053,28 @@ mod tests { ); } + /// Width, fill and zero padding behave like an integer, and precision does + /// not cut digits. + #[test] + fn fixed_length_hex_pads_like_an_integer() { + let value = U256::from(0xDEADBEEF_u64); + let digits = format!("{:x}", value); + assert_eq!(digits.len(), 64); + assert_eq!(format!("{:70x}", value), format!(" {}", digits)); + assert_eq!(format!("{:<70x}", value), format!("{} ", digits)); + assert_eq!(format!("{:*^70x}", value), format!("***{}***", digits)); + assert_eq!(format!("{:070x}", value), format!("000000{}", digits)); + assert_eq!(format!("{:#070x}", value), format!("0x0000{}", digits)); + assert_eq!(format!("{:.4x}", value), digits); + + let id: crate::bip152::ShortId = "abcd00000001".parse().unwrap(); + assert_eq!(format!("{}", id), "abcd00000001"); + assert_eq!(format!("{:?}", id), "abcd00000001"); + assert_eq!(format!("{:#X}", id), "0xABCD00000001"); + assert_eq!(format!("{:16}", id), " abcd00000001"); + assert_eq!(format!("{:016x}", id), "0000abcd00000001"); + } + #[test] fn u256_display() { assert_eq!(format!("{}", U256::from(100_u32)), "100",); diff --git a/dash/src/serde_utils.rs b/dash/src/serde_utils.rs index d30de7bda..7bcde48c1 100644 --- a/dash/src/serde_utils.rs +++ b/dash/src/serde_utils.rs @@ -10,300 +10,12 @@ impl<'a> serde::Serialize for SerializeBytesAsHex<'a> { where S: serde::Serializer, { - use internals::hex::display::DisplayHex; + use hex_conservative::DisplayHex; serializer.collect_str(&format_args!("{:x}", self.0.as_hex())) } } -pub mod btreemap_byte_values { - //! Module for serialization of BTreeMaps with hex byte values. - #![allow(missing_docs)] - - // NOTE: This module can be exactly copied to use with HashMap. - - use hashes::hex::FromHex; - use serde; - - use crate::prelude::*; - - pub fn serialize(v: &BTreeMap>, s: S) -> Result - where - S: serde::Serializer, - T: serde::Serialize + core::hash::Hash + Eq + Ord, - { - use serde::ser::SerializeMap; - - // Don't do anything special when not human readable. - if !s.is_human_readable() { - serde::Serialize::serialize(v, s) - } else { - let mut map = s.serialize_map(Some(v.len()))?; - for (key, value) in v.iter() { - map.serialize_entry(key, &super::SerializeBytesAsHex(value))?; - } - map.end() - } - } - - pub fn deserialize<'de, D, T>(d: D) -> Result>, D::Error> - where - D: serde::Deserializer<'de>, - T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord, - { - use core::marker::PhantomData; - - struct Visitor(PhantomData); - impl<'de, T> serde::de::Visitor<'de> for Visitor - where - T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord, - { - type Value = BTreeMap>; - - fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - write!(f, "a map with hexadecimal values") - } - - fn visit_map>( - self, - mut a: A, - ) -> Result { - let mut ret = BTreeMap::new(); - while let Some((key, value)) = a.next_entry()? { - ret.insert(key, FromHex::from_hex(value).map_err(serde::de::Error::custom)?); - } - Ok(ret) - } - } - - // Don't do anything special when not human readable. - if !d.is_human_readable() { - serde::Deserialize::deserialize(d) - } else { - d.deserialize_map(Visitor(PhantomData)) - } - } -} - -pub mod btreemap_as_seq { - //! Module for serialization of BTreeMaps as lists of sequences because - //! serde_json will not serialize hashmaps with non-string keys be default. - #![allow(missing_docs)] - - // NOTE: This module can be exactly copied to use with HashMap. - - use serde; - - use crate::prelude::*; - - pub fn serialize(v: &BTreeMap, s: S) -> Result - where - S: serde::Serializer, - T: serde::Serialize + core::hash::Hash + Eq + Ord, - U: serde::Serialize, - { - use serde::ser::SerializeSeq; - - // Don't do anything special when not human readable. - if !s.is_human_readable() { - serde::Serialize::serialize(v, s) - } else { - let mut seq = s.serialize_seq(Some(v.len()))?; - for pair in v.iter() { - seq.serialize_element(&pair)?; - } - seq.end() - } - } - - pub fn deserialize<'de, D, T, U>(d: D) -> Result, D::Error> - where - D: serde::Deserializer<'de>, - T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord, - U: serde::Deserialize<'de>, - { - use core::marker::PhantomData; - - struct Visitor(PhantomData<(T, U)>); - impl<'de, T, U> serde::de::Visitor<'de> for Visitor - where - T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord, - U: serde::Deserialize<'de>, - { - type Value = BTreeMap; - - fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - write!(f, "a sequence of pairs") - } - - fn visit_seq>( - self, - mut a: A, - ) -> Result { - let mut ret = BTreeMap::new(); - while let Some((key, value)) = a.next_element()? { - ret.insert(key, value); - } - Ok(ret) - } - } - - // Don't do anything special when not human readable. - if !d.is_human_readable() { - serde::Deserialize::deserialize(d) - } else { - d.deserialize_seq(Visitor(PhantomData)) - } - } -} - -pub mod btreemap_as_seq_byte_values { - //! Module for serialization of BTreeMaps as lists of sequences because - //! serde_json will not serialize hashmaps with non-string keys be default. - #![allow(missing_docs)] - - // NOTE: This module can be exactly copied to use with HashMap. - - use serde; - - use crate::prelude::*; - - /// A custom key-value pair type that serialized the bytes as hex. - #[derive(Debug, Deserialize)] - #[serde(crate = "serde")] - struct OwnedPair( - T, - #[serde(deserialize_with = "crate::serde_utils::hex_bytes::deserialize")] Vec, - ); - - /// A custom key-value pair type that serialized the bytes as hex. - #[derive(Debug, Serialize)] - #[serde(crate = "serde")] - struct BorrowedPair<'a, T: 'static>( - &'a T, - #[serde(serialize_with = "crate::serde_utils::hex_bytes::serialize")] &'a [u8], - ); - - pub fn serialize(v: &BTreeMap>, s: S) -> Result - where - S: serde::Serializer, - T: serde::Serialize + core::hash::Hash + Eq + Ord + 'static, - { - use serde::ser::SerializeSeq; - - // Don't do anything special when not human readable. - if !s.is_human_readable() { - serde::Serialize::serialize(v, s) - } else { - let mut seq = s.serialize_seq(Some(v.len()))?; - for (key, value) in v.iter() { - seq.serialize_element(&BorrowedPair(key, value))?; - } - seq.end() - } - } - - pub fn deserialize<'de, D, T>(d: D) -> Result>, D::Error> - where - D: serde::Deserializer<'de>, - T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord, - { - use core::marker::PhantomData; - - struct Visitor(PhantomData); - impl<'de, T> serde::de::Visitor<'de> for Visitor - where - T: serde::Deserialize<'de> + core::hash::Hash + Eq + Ord, - { - type Value = BTreeMap>; - - fn expecting(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - write!(f, "a sequence of pairs") - } - - fn visit_seq>( - self, - mut a: A, - ) -> Result { - let mut ret = BTreeMap::new(); - while let Option::Some(OwnedPair(key, value)) = a.next_element()? { - ret.insert(key, value); - } - Ok(ret) - } - } - - // Don't do anything special when not human readable. - if !d.is_human_readable() { - serde::Deserialize::deserialize(d) - } else { - d.deserialize_seq(Visitor(PhantomData)) - } - } -} - -pub mod hex_bytes { - //! Module for serialization of byte arrays as hex strings. - #![allow(missing_docs)] - - use hashes::hex::FromHex; - use serde; - - pub fn serialize(bytes: &T, s: S) -> Result - where - T: serde::Serialize + AsRef<[u8]>, - S: serde::Serializer, - { - // Don't do anything special when not human readable. - if !s.is_human_readable() { - serde::Serialize::serialize(bytes, s) - } else { - serde::Serialize::serialize(&super::SerializeBytesAsHex(bytes.as_ref()), s) - } - } - - pub fn deserialize<'de, D, B>(d: D) -> Result - where - D: serde::Deserializer<'de>, - B: serde::Deserialize<'de> + FromHex, - { - struct Visitor(core::marker::PhantomData); - - impl<'de, B: FromHex> serde::de::Visitor<'de> for Visitor { - type Value = B; - - fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { - formatter.write_str("an ASCII hex string") - } - - fn visit_bytes(self, v: &[u8]) -> Result - where - E: serde::de::Error, - { - if let Ok(hex) = core::str::from_utf8(v) { - FromHex::from_hex(hex).map_err(E::custom) - } else { - Err(E::invalid_value(serde::de::Unexpected::Bytes(v), &self)) - } - } - - fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, - { - FromHex::from_hex(v).map_err(E::custom) - } - } - - // Don't do anything special when not human readable. - if !d.is_human_readable() { - serde::Deserialize::deserialize(d) - } else { - d.deserialize_str(Visitor(core::marker::PhantomData)) - } - } -} - macro_rules! serde_string_serialize_impl { ($name:ty, $expecting:literal) => { impl $crate::serde::Serialize for $name { diff --git a/dash/src/taproot.rs b/dash/src/taproot.rs index 937a82484..28e35bdfc 100644 --- a/dash/src/taproot.rs +++ b/dash/src/taproot.rs @@ -1765,7 +1765,6 @@ impl TapTweak for UntweakedKeyPair { mod test { use core::str::FromStr; - use hashes::hex::FromHex; use hashes::sha256t::Tag; use hashes::{Hash, HashEngine, sha256}; use secp256k1::XOnlyPublicKey; @@ -1863,7 +1862,8 @@ mod test { let out_pk = TweakedPublicKey::dangerous_assume_tweaked(out_pk); let script = ScriptBuf::from_hex(script_hex).unwrap(); let control_block = - ControlBlock::decode(&Vec::::from_hex(control_block_hex).unwrap()).unwrap(); + ControlBlock::decode(&hex_conservative::decode_to_vec(control_block_hex).unwrap()) + .unwrap(); assert_eq!(control_block_hex, control_block.serialize().to_lower_hex_string()); assert!(control_block.verify_taproot_commitment(out_pk.to_inner(), &script)); } @@ -2249,7 +2249,7 @@ mod test { for (i, script_ver) in leaves.iter().enumerate() { let expected_leaf_hash = leaf_hashes[i].as_str().unwrap(); let expected_ctrl_blk = ControlBlock::decode( - &Vec::::from_hex(ctrl_blks[i].as_str().unwrap()).unwrap(), + &hex_conservative::decode_to_vec(ctrl_blks[i].as_str().unwrap()).unwrap(), ) .unwrap(); diff --git a/deny.toml b/deny.toml index 940ac88cb..9a0d646c2 100644 --- a/deny.toml +++ b/deny.toml @@ -37,6 +37,16 @@ confidence-threshold = 0.8 multiple-versions = "warn" wildcards = "allow" highlight = "all" +deny = [ + # Hex goes through `hex-conservative` 1.x. + { crate = "hex", reason = "use `hex-conservative` 1.x instead" }, + { crate = "hex_lit", reason = "use `hex!` from `hex-conservative` 1.x instead" }, + { crate = "hex-literal", reason = "use `hex!` from `hex-conservative` 1.x instead" }, +] +skip = [ + # Older releases still come in through upstream crates (`bitcoin_hashes` 0.14 via `bip39`), + { crate = "hex-conservative:<1.0.0", reason = "depended on by rust-bitcoin crates" }, +] [sources] unknown-registry = "deny" diff --git a/hashes/Cargo.toml b/hashes/Cargo.toml index ce92041e4..d380e95a6 100644 --- a/hashes/Cargo.toml +++ b/hashes/Cargo.toml @@ -25,6 +25,7 @@ rustdoc-args = ["--cfg", "docsrs"] [dependencies] bincode = { workspace = true, optional = true } bitcoin_hashes = { workspace = true, features = ["std"] } +hex-conservative = { workspace = true } rs-x11-hash = { version = "0.1.8", optional = true } serde = { workspace = true, optional = true } diff --git a/hashes/src/hash_x11.rs b/hashes/src/hash_x11.rs index 533d56e42..301739f2a 100644 --- a/hashes/src/hash_x11.rs +++ b/hashes/src/hash_x11.rs @@ -24,8 +24,7 @@ use core::str; use std::io; use std::vec::Vec; -use crate::hex::FromHex as _; -use crate::{hex, FromSliceError, HashEngine as _}; +use crate::{FromSliceError, HashEngine as _}; /// Output of the X11 hash function. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -38,10 +37,10 @@ crate::bincode_impl!(Hash, 32); crate::borrow_slice_impl!(Hash); impl str::FromStr for Hash { - type Err = hex::HexToArrayError; + type Err = hex_conservative::DecodeFixedLengthBytesError; fn from_str(s: &str) -> Result { - let mut bytes = <[u8; 32]>::from_hex(s)?; + let mut bytes: [u8; 32] = hex_conservative::decode_to_array(s)?; bytes.reverse(); Ok(Hash(bytes)) } diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index 68dfe07ee..b042d257d 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -98,6 +98,9 @@ pub extern crate bitcoin_hashes; #[doc(hidden)] pub mod _export { + pub use hex_conservative; + + pub use crate::util::pad_hex; /// A re-export of core::* pub mod _core { pub use core::*; @@ -108,8 +111,8 @@ pub mod _export { // The tag helpers are upstream's verbatim. Only `sha256t_hash_newtype!` stays // ours, so that tagged hashes route through our `hash_newtype!`. pub use bitcoin_hashes::{ - cmp, hash160, hex, hmac, ripemd160, sha1, sha256, sha256d, sha512, sha512_256, FromSliceError, - Hash, HashEngine, Hmac, HmacEngine, + cmp, hash160, hmac, ripemd160, sha1, sha256, sha256d, sha512, sha512_256, FromSliceError, Hash, + HashEngine, Hmac, HmacEngine, }; pub use bitcoin_hashes::{sha256t_hash_newtype_tag, sha256t_hash_newtype_tag_constructor}; diff --git a/hashes/src/util.rs b/hashes/src/util.rs index c567f3a85..939b0bbe8 100644 --- a/hashes/src/util.rs +++ b/hashes/src/util.rs @@ -12,6 +12,21 @@ // If not, see . // +/// Writes already encoded hex in the style of `hex-conservative` 0.2. +/// +/// Right aligned by default, `0x` under `{:#}`, and precision cuts the digits. +/// `hex-conservative` 1.x pads like a string, so going through it directly +/// would change how hashes print with a width. +#[doc(hidden)] +pub fn pad_hex(f: &mut core::fmt::Formatter, encoded: &str) -> core::fmt::Result { + match f.precision() { + Some(precision) if encoded.len() > precision => { + f.pad_integral(true, "0x", &encoded[..precision]) + } + _ => f.pad_integral(true, "0x", encoded), + } +} + #[macro_export] /// Adds hexadecimal formatting implementation of a trait `$imp` to a given type `$ty`. macro_rules! hex_fmt_impl( @@ -22,24 +37,28 @@ macro_rules! hex_fmt_impl( impl<$($generator: $gent),*> $crate::_export::_core::fmt::LowerHex for $ty<$($generator),*> { #[inline] fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result { + let mut encoder = $crate::_export::hex_conservative::buf_encoder::BufEncoder::<{ $len * 2 }>::new($crate::_export::hex_conservative::Case::Lower); let bytes = $crate::Hash::as_byte_array(self); if $reverse { - $crate::hex::fmt_hex_exact!(f, $len, bytes.iter().rev(), $crate::hex::Case::Lower) + encoder.put_bytes(bytes.iter().rev()); } else { - $crate::hex::fmt_hex_exact!(f, $len, bytes.iter(), $crate::hex::Case::Lower) + encoder.put_bytes(bytes.iter()); } + $crate::_export::pad_hex(f, encoder.as_str()) } } impl<$($generator: $gent),*> $crate::_export::_core::fmt::UpperHex for $ty<$($generator),*> { #[inline] fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result { + let mut encoder = $crate::_export::hex_conservative::buf_encoder::BufEncoder::<{ $len * 2 }>::new($crate::_export::hex_conservative::Case::Upper); let bytes = $crate::Hash::as_byte_array(self); if $reverse { - $crate::hex::fmt_hex_exact!(f, $len, bytes.iter().rev(), $crate::hex::Case::Upper) + encoder.put_bytes(bytes.iter().rev()); } else { - $crate::hex::fmt_hex_exact!(f, $len, bytes.iter(), $crate::hex::Case::Upper) + encoder.put_bytes(bytes.iter()); } + $crate::_export::pad_hex(f, encoder.as_str()) } } @@ -268,11 +287,9 @@ macro_rules! hash_newtype { } impl $crate::_export::_core::str::FromStr for $newtype { - type Err = $crate::hex::HexToArrayError; + type Err = $crate::_export::hex_conservative::DecodeFixedLengthBytesError; fn from_str(s: &str) -> $crate::_export::_core::result::Result<$newtype, Self::Err> { - use $crate::hex::FromHex; - - let mut bytes = <[u8; ::LEN]>::from_hex(s)?; + let mut bytes: [u8; ::LEN] = $crate::_export::hex_conservative::decode_to_array(s)?; if ::DISPLAY_BACKWARD { bytes.reverse(); } @@ -405,11 +422,9 @@ macro_rules! hash_newtype_no_ord { } impl $crate::_export::_core::str::FromStr for $newtype { - type Err = $crate::hex::HexToArrayError; + type Err = $crate::_export::hex_conservative::DecodeFixedLengthBytesError; fn from_str(s: &str) -> $crate::_export::_core::result::Result<$newtype, Self::Err> { - use $crate::hex::FromHex; - - let mut bytes = <[u8; ::LEN]>::from_hex(s)?; + let mut bytes: [u8; ::LEN] = $crate::_export::hex_conservative::decode_to_array(s)?; if ::DISPLAY_BACKWARD { bytes.reverse(); } @@ -557,3 +572,31 @@ macro_rules! hash_newtype_known_attrs { (#[hash_newtype($($unknown:tt)*)]) => { compile_error!(concat!("Unrecognized attribute ", stringify!($($unknown)*))); }; ($($ignore:tt)*) => {}; } + +#[cfg(test)] +mod tests { + use crate::{sha256, sha256d, Hash}; + + crate::hash_newtype! { + /// Displayed backward, like `sha256d`. + struct Backward(sha256d::Hash); + /// Displayed forward, like `sha256`. + struct Forward(sha256::Hash); + } + + #[test] + fn newtype_formatting_matches_hex_conservative_0_2() { + let backward = sha256d::Hash::hash(b"backward"); + let forward = sha256::Hash::hash(b"forward"); + macro_rules! same { + ($($spec:literal),*) => {$( + assert_eq!(format!($spec, Backward(backward)), format!($spec, backward), "{}", $spec); + assert_eq!(format!($spec, Forward(forward)), format!($spec, forward), "{}", $spec); + )*}; + } + same!( + "{}", "{:?}", "{:x}", "{:X}", "{:#}", "{:#x}", "{:#X}", "{:70}", "{:<70}", "{:>70}", + "{:^70}", "{:*^70}", "{:070}", "{:#070x}", "{:.10}", "{:#.10}", "{:20.10}" + ); + } +} diff --git a/internals/src/hex/buf_encoder.rs b/internals/src/hex/buf_encoder.rs deleted file mode 100644 index a51112031..000000000 --- a/internals/src/hex/buf_encoder.rs +++ /dev/null @@ -1,422 +0,0 @@ -//! Implements a buffered encoder. -//! -//! The main type of this module is [`BufEncoder`] which provides buffered hex encoding. Such is -//! faster than the usual `write!(f, "{02x}", b)?` in a for loop because it reduces dynamic -//! dispatch and decreases the number of allocations if a `String` is being created. - -use core::borrow::Borrow; - -pub use out_bytes::OutBytes; - -use super::Case; - -/// Trait for types that can be soundly converted to `OutBytes`. -/// -/// To protect the API from future breakage this sealed trait guards which types can be used with -/// the `Encoder`. Currently it is implemented for byte arrays of various interesting lengths. -/// -/// ## Safety -/// -/// This is not `unsafe` yet but the `as_out_bytes` should always return the same reference if the -/// same reference is supplied. IOW the returned memory address and length should be the same if -/// the input memory address and length are the same. -/// -/// If the trait ever becomes `unsafe` this will be required for soundness. -pub trait AsOutBytes: out_bytes::Sealed { - /// Performs the conversion. - fn as_out_bytes(&self) -> &OutBytes; - - /// Performs the conversion. - fn as_mut_out_bytes(&mut self) -> &mut OutBytes; -} - -/// A buffer with compile-time-known length. -/// -/// This is essentially `Default + AsOutBytes` but supports lengths 1.41 doesn't. -pub trait FixedLenBuf: Sized + AsOutBytes { - /// Creates an uninitialized buffer. - /// - /// The current implementations initialize the buffer with zeroes but it should be treated a - /// uninitialized anyway. - fn uninit() -> Self; -} - -/// Implements `OutBytes` -/// -/// This prevents the rest of the crate from accessing the field of `OutBytes`. -mod out_bytes { - use super::AsOutBytes; - - /// A byte buffer that can only be written-into. - /// - /// You shouldn't concern yourself with this, just call `BufEncoder::new` with your array. - /// - /// This prepares the API for potential future support of `[MaybeUninit]`. We don't want to use - /// `unsafe` until it's proven to be needed but if it does we have an easy, compatible upgrade - /// option. - /// - /// Warning: `repr(transparent)` is an internal implementation detail and **must not** be - /// relied on! - #[repr(transparent)] - pub struct OutBytes([u8]); - - impl OutBytes { - /// Returns the first `len` bytes as initialized. - /// - /// Not `unsafe` because we don't use `unsafe` (yet). - /// - /// ## Panics - /// - /// The method panics if `len` is out of bounds. - #[track_caller] - pub(crate) fn assume_init(&self, len: usize) -> &[u8] { - &self.0[..len] - } - - /// Writes given bytes into the buffer. - /// - /// ## Panics - /// - /// The method panics if pos is out of bounds or `bytes` don't fit into the buffer. - #[track_caller] - pub(crate) fn write(&mut self, pos: usize, bytes: &[u8]) { - self.0[pos..(pos + bytes.len())].copy_from_slice(bytes); - } - - /// Returns the length of the buffer. - pub(crate) fn len(&self) -> usize { - self.0.len() - } - - fn from_bytes(slice: &[u8]) -> &Self { - // SAFETY: copied from std - // conversion of reference to pointer of the same referred type is always sound, - // including in unsized types. - // Thanks to repr(transparent) the types have the same layout making the other - // conversion sound. - // The pointer was just created from a reference that's still alive so dereferencing is - // sound. - unsafe { &*(slice as *const [u8] as *const Self) } - } - - fn from_mut_bytes(slice: &mut [u8]) -> &mut Self { - // SAFETY: copied from std - // conversion of reference to pointer of the same referred type is always sound, - // including in unsized types. - // Thanks to repr(transparent) the types have the same layout making the other - // conversion sound. - // The pointer was just created from a reference that's still alive so dereferencing is - // sound. - unsafe { &mut *(slice as *mut [u8] as *mut Self) } - } - } - - macro_rules! impl_from_array { - ($($len:expr),* $(,)?) => { - $( - impl super::FixedLenBuf for [u8; $len] { - fn uninit() -> Self { - [0u8; $len] - } - } - - impl AsOutBytes for [u8; $len] { - fn as_out_bytes(&self) -> &OutBytes { - OutBytes::from_bytes(self) - } - - fn as_mut_out_bytes(&mut self) -> &mut OutBytes { - OutBytes::from_mut_bytes(self) - } - } - - impl Sealed for [u8; $len] {} - - impl<'a> super::super::display::DisplayHex for &'a [u8; $len / 2] { - type Display = super::super::display::DisplayArray, [u8; $len]>; - fn as_hex(self) -> Self::Display { - super::super::display::DisplayArray::new(self.iter()) - } - - fn hex_reserve_suggestion(self) -> usize { - $len - } - } - )* - } - } - - impl AsOutBytes for &'_ mut T { - fn as_out_bytes(&self) -> &OutBytes { - (**self).as_out_bytes() - } - - fn as_mut_out_bytes(&mut self) -> &mut OutBytes { - (**self).as_mut_out_bytes() - } - } - - impl Sealed for &'_ mut T {} - - impl AsOutBytes for OutBytes { - fn as_out_bytes(&self) -> &OutBytes { - self - } - - fn as_mut_out_bytes(&mut self) -> &mut OutBytes { - self - } - } - - impl Sealed for OutBytes {} - - // As a sanity check we only provide conversions for even, non-empty arrays. - // Weird lengths 66 and 130 are provided for serialized public keys. - impl_from_array!( - 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 40, 48, 64, 66, 96, 128, 130, - 192, 256, 512, 1024, 2048, 4096, 8192 - ); - - /// Prevents outside crates from implementing the trait - pub trait Sealed {} -} - -/// Hex-encodes bytes into the provided buffer. -/// -/// This is an important building block for fast hex-encoding. Because string writing tools -/// provided by `core::fmt` involve dynamic dispatch and don't allow reserving capacity in strings -/// buffering the hex and then formatting it is significantly faster. -pub struct BufEncoder { - buf: T, - pos: usize, -} - -impl BufEncoder { - /// Creates an empty `BufEncoder`. - /// - /// This is usually used with uninitialized (zeroed) byte array allocated on stack. - /// This can only be constructed with an even-length, non-empty array. - #[inline] - pub fn new(buf: T) -> Self { - BufEncoder { - buf, - pos: 0, - } - } - - /// Encodes `byte` as hex in given `case` and appends it to the buffer. - /// - /// ## Panics - /// - /// The method panics if the buffer is full. - #[inline] - #[track_caller] - pub fn put_byte(&mut self, byte: u8, case: Case) { - self.buf.as_mut_out_bytes().write(self.pos, &super::byte_to_hex(byte, case.table())); - self.pos += 2; - } - - /// Encodes `bytes` as hex in given `case` and appends them to the buffer. - /// - /// ## Panics - /// - /// The method panics if the bytes wouldn't fit the buffer. - #[inline] - #[track_caller] - pub fn put_bytes(&mut self, bytes: I, case: Case) - where - I: IntoIterator, - I::Item: Borrow, - { - self.put_bytes_inner(bytes.into_iter(), case) - } - - #[inline] - #[track_caller] - fn put_bytes_inner(&mut self, bytes: I, case: Case) - where - I: Iterator, - I::Item: Borrow, - { - // May give the compiler better optimization opportunity - if let Some(max) = bytes.size_hint().1 { - assert!(max <= self.space_remaining()); - } - for byte in bytes { - self.put_byte(*byte.borrow(), case); - } - } - - /// Encodes as many `bytes` as fit into the buffer as hex and return the remainder. - /// - /// This method works just like `put_bytes` but instead of panicking it returns the unwritten - /// bytes. The method returns an empty slice if all bytes were written - #[must_use = "this may write only part of the input buffer"] - #[inline] - #[track_caller] - pub fn put_bytes_min<'a>(&mut self, bytes: &'a [u8], case: Case) -> &'a [u8] { - let to_write = self.space_remaining().min(bytes.len()); - self.put_bytes(&bytes[..to_write], case); - &bytes[to_write..] - } - - /// Returns true if no more bytes can be written into the buffer. - #[inline] - pub fn is_full(&self) -> bool { - self.pos == self.buf.as_out_bytes().len() - } - - /// Returns the written bytes as a hex `str`. - #[inline] - pub fn as_str(&self) -> &str { - core::str::from_utf8(self.buf.as_out_bytes().assume_init(self.pos)) - .expect("we only write ASCII") - } - - /// Resets the buffer to become empty. - #[inline] - pub fn clear(&mut self) { - self.pos = 0; - } - - /// How many bytes can be written to this buffer. - /// - /// Note that this returns the number of bytes before encoding, not number of hex digits. - #[inline] - pub fn space_remaining(&self) -> usize { - (self.buf.as_out_bytes().len() - self.pos) / 2 - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn empty() { - let mut buf = [0u8; 2]; - let encoder = BufEncoder::new(&mut buf); - assert_eq!(encoder.as_str(), ""); - assert!(!encoder.is_full()); - } - - #[test] - fn single_byte_exact_buf() { - let mut buf = [0u8; 2]; - let mut encoder = BufEncoder::new(&mut buf); - assert_eq!(encoder.space_remaining(), 1); - encoder.put_byte(42, Case::Lower); - assert_eq!(encoder.as_str(), "2a"); - assert_eq!(encoder.space_remaining(), 0); - assert!(encoder.is_full()); - encoder.clear(); - assert_eq!(encoder.space_remaining(), 1); - assert!(!encoder.is_full()); - encoder.put_byte(42, Case::Upper); - assert_eq!(encoder.as_str(), "2A"); - assert_eq!(encoder.space_remaining(), 0); - assert!(encoder.is_full()); - } - - #[test] - fn single_byte_oversized_buf() { - let mut buf = [0u8; 4]; - let mut encoder = BufEncoder::new(&mut buf); - assert_eq!(encoder.space_remaining(), 2); - encoder.put_byte(42, Case::Lower); - assert_eq!(encoder.space_remaining(), 1); - assert_eq!(encoder.as_str(), "2a"); - assert!(!encoder.is_full()); - encoder.clear(); - assert_eq!(encoder.space_remaining(), 2); - encoder.put_byte(42, Case::Upper); - assert_eq!(encoder.as_str(), "2A"); - assert_eq!(encoder.space_remaining(), 1); - assert!(!encoder.is_full()); - } - - #[test] - fn two_bytes() { - let mut buf = [0u8; 4]; - let mut encoder = BufEncoder::new(&mut buf); - encoder.put_byte(42, Case::Lower); - assert_eq!(encoder.space_remaining(), 1); - encoder.put_byte(255, Case::Lower); - assert_eq!(encoder.space_remaining(), 0); - assert_eq!(encoder.as_str(), "2aff"); - assert!(encoder.is_full()); - encoder.clear(); - assert!(!encoder.is_full()); - encoder.put_byte(42, Case::Upper); - encoder.put_byte(255, Case::Upper); - assert_eq!(encoder.as_str(), "2AFF"); - assert!(encoder.is_full()); - } - - #[test] - fn put_bytes_min() { - let mut buf = [0u8; 2]; - let mut encoder = BufEncoder::new(&mut buf); - let remainder = encoder.put_bytes_min(b"", Case::Lower); - assert_eq!(remainder, b""); - assert_eq!(encoder.as_str(), ""); - let remainder = encoder.put_bytes_min(b"*", Case::Lower); - assert_eq!(remainder, b""); - assert_eq!(encoder.as_str(), "2a"); - encoder.clear(); - let remainder = encoder.put_bytes_min(&[42, 255], Case::Lower); - assert_eq!(remainder, &[255]); - assert_eq!(encoder.as_str(), "2a"); - } - - #[test] - fn same_as_fmt() { - use core::fmt::{self, Write}; - - struct Writer { - buf: [u8; 2], - pos: usize, - } - - impl Writer { - fn as_str(&self) -> &str { - core::str::from_utf8(&self.buf[..self.pos]).unwrap() - } - } - - impl Write for Writer { - fn write_str(&mut self, s: &str) -> fmt::Result { - assert!(self.pos <= 2); - if s.len() > 2 - self.pos { - Err(fmt::Error) - } else { - self.buf[self.pos..(self.pos + s.len())].copy_from_slice(s.as_bytes()); - self.pos += s.len(); - Ok(()) - } - } - } - - let mut writer = Writer { - buf: [0u8; 2], - pos: 0, - }; - let mut buf = [0u8; 2]; - let mut encoder = BufEncoder::new(&mut buf); - - for i in 0..=255 { - write!(writer, "{:02x}", i).unwrap(); - encoder.put_byte(i, Case::Lower); - assert_eq!(encoder.as_str(), writer.as_str()); - writer.pos = 0; - encoder.clear(); - } - for i in 0..=255 { - write!(writer, "{:02X}", i).unwrap(); - encoder.put_byte(i, Case::Upper); - assert_eq!(encoder.as_str(), writer.as_str()); - writer.pos = 0; - encoder.clear(); - } - } -} diff --git a/internals/src/hex/display.rs b/internals/src/hex/display.rs deleted file mode 100644 index af1faacf9..000000000 --- a/internals/src/hex/display.rs +++ /dev/null @@ -1,325 +0,0 @@ -//! Helpers for displaying bytes as hex strings. -//! -//! This module provides a trait for displaying things as hex as well as an implementation for -//! `&[u8]`. - -use core::borrow::Borrow; -use core::fmt; - -use super::Case; -use super::buf_encoder::{BufEncoder, OutBytes}; -use crate::hex::buf_encoder::FixedLenBuf; - -/// Extension trait for types that can be displayed as hex. -/// -/// Types that have a single, obvious text representation being hex should **not** implement this -/// trait and simply implement `Display` instead. -/// -/// This trait should be generally implemented for references only. We would prefer to use GAT but -/// that is beyond our MSRV. As a lint we require the `IsRef` trait which is implemented for all -/// references. -pub trait DisplayHex: Copy + sealed::IsRef { - /// The type providing [`fmt::Display`] implementation. - /// - /// This is usually a wrapper type holding a reference to `Self`. - type Display: fmt::LowerHex + fmt::UpperHex; - - /// Display `Self` as a continuous sequence of ASCII hex chars. - fn as_hex(self) -> Self::Display; - - /// Create a lower-hex-encoded string. - /// - /// A shorthand for `to_hex_string(Case::Lower)`, so that `Case` doesn't need to be imported. - /// - /// This may be faster than `.display_hex().to_string()` because it uses `reserve_suggestion`. - fn to_lower_hex_string(self) -> String { - self.to_hex_string(Case::Lower) - } - - /// Create an upper-hex-encoded string. - /// - /// A shorthand for `to_hex_string(Case::Upper)`, so that `Case` doesn't need to be imported. - /// - /// This may be faster than `.display_hex().to_string()` because it uses `reserve_suggestion`. - fn to_upper_hex_string(self) -> String { - self.to_hex_string(Case::Upper) - } - - /// Create a hex-encoded string. - /// - /// This may be faster than `.display_hex().to_string()` because it uses `reserve_suggestion`. - fn to_hex_string(self, case: Case) -> String { - let mut string = String::new(); - self.append_hex_to_string(case, &mut string); - string - } - - /// Appends hex-encoded content to an existing `String`. - /// - /// This may be faster than `write!(string, "{:x}", self.display_hex())` because it uses - /// `reserve_suggestion`. - fn append_hex_to_string(self, case: Case, string: &mut String) { - use fmt::Write; - - string.reserve(self.hex_reserve_suggestion()); - match case { - Case::Lower => write!(string, "{:x}", self.as_hex()), - Case::Upper => write!(string, "{:X}", self.as_hex()), - } - .unwrap_or_else(|_| { - let name = core::any::type_name::(); - // We don't expect `std` to ever be buggy, so the bug is most likely in the `Display` - // impl of `Self::Display`. - panic!("The implementation of Display for {} returned an error when it shouldn't", name) - }) - } - - /// Hints how much bytes to reserve when creating a `String`. - /// - /// Implementors that know the number of produced bytes upfront should override this. - /// Defaults to 0. - /// - // We prefix the name with `hex_` to avoid potential collision with other methods. - fn hex_reserve_suggestion(self) -> usize { - 0 - } -} - -mod sealed { - /// Trait marking a shared reference. - pub trait IsRef: Copy {} - - impl IsRef for &'_ T {} -} - -impl<'a> DisplayHex for &'a [u8] { - type Display = DisplayByteSlice<'a>; - - #[inline] - fn as_hex(self) -> Self::Display { - DisplayByteSlice { - bytes: self, - } - } - - #[inline] - fn hex_reserve_suggestion(self) -> usize { - // Since the string wouldn't fit into address space if this overflows (actually even for - // smaller amounts) it's better to panic right away. It should also give the optimizer - // better opportunities. - self.len().checked_mul(2).expect("the string wouldn't fit into address space") - } -} - -impl<'a> DisplayHex for &'a Vec { - type Display = DisplayByteSlice<'a>; - - #[inline] - fn as_hex(self) -> Self::Display { - DisplayByteSlice { - bytes: self, - } - } - - #[inline] - fn hex_reserve_suggestion(self) -> usize { - // Since the string wouldn't fit into address space if this overflows (actually even for - // smaller amounts) it's better to panic right away. It should also give the optimizer - // better opportunities. - self.len().checked_mul(2).expect("the string wouldn't fit into address space") - } -} - -/// Displays byte slice as hex. -/// -/// Created by [`<&[u8] as DisplayHex>::as_hex`](DisplayHex::as_hex). -pub struct DisplayByteSlice<'a> { - // pub because we want to keep lengths in sync - pub(crate) bytes: &'a [u8], -} - -impl<'a> DisplayByteSlice<'a> { - fn display(&self, f: &mut fmt::Formatter, case: Case) -> fmt::Result { - let mut buf = [0u8; 1024]; - let mut encoder = super::BufEncoder::new(&mut buf); - - let (chunks, remainder) = self.bytes.as_chunks::<512>(); - for chunk in chunks { - encoder.put_bytes(chunk, case); - f.write_str(encoder.as_str())?; - encoder.clear(); - } - encoder.put_bytes(remainder, case); - f.write_str(encoder.as_str()) - } -} - -impl<'a> fmt::LowerHex for DisplayByteSlice<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.display(f, Case::Lower) - } -} - -impl<'a> fmt::UpperHex for DisplayByteSlice<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.display(f, Case::Upper) - } -} - -/// Displays byte array as hex. -/// -/// Created by [`<&[u8; LEN] as DisplayHex>::as_hex`](DisplayHex::as_hex). -pub struct DisplayArray -where - A::Item: Borrow, -{ - array: A, - _buffer_marker: core::marker::PhantomData, -} - -impl DisplayArray -where - A::Item: Borrow, -{ - /// Creates the wrapper. - pub fn new(array: A) -> Self { - DisplayArray { - array, - _buffer_marker: Default::default(), - } - } - - fn display(&self, f: &mut fmt::Formatter, case: Case) -> fmt::Result { - let mut buf = B::uninit(); - let mut encoder = super::BufEncoder::new(&mut buf); - encoder.put_bytes(self.array.clone(), case); - f.pad_integral(true, "0x", encoder.as_str()) - } -} - -impl fmt::LowerHex for DisplayArray -where - A::Item: Borrow, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.display(f, Case::Lower) - } -} - -impl fmt::UpperHex for DisplayArray -where - A::Item: Borrow, -{ - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.display(f, Case::Upper) - } -} - -/// Format known-length array as hex. -/// -/// This supports all formatting options of formatter and may be faster than calling -/// `display_as_hex()` on an arbitrary `&[u8]`. Note that the implementation intentionally keeps -/// leading zeros even when not requested. This is designed to display values such as hashes and -/// keys and removing leading zeros would be confusing. -/// -/// ## Parameters -/// -/// * `$formatter` - a [`fmt::Formatter`]. -/// * `$len` known length of `$bytes`, must be a const expression. -/// * `$bytes` - bytes to be encoded, most likely a reference to an array. -/// * `$case` - value of type [`Case`] determining whether to format as lower or upper case. -/// -/// ## Panics -/// -/// This macro panics if `$len` is not equal to `$bytes.len()`. It also fails to compile if `$len` -/// is more than half of `usize::MAX`. -#[macro_export] -macro_rules! fmt_hex_exact { - ($formatter:expr, $len:expr, $bytes:expr, $case:expr) => {{ - // statically check $len - #[allow(deprecated)] - const _: () = [()][($len > usize::MAX / 2) as usize]; - assert_eq!($bytes.len(), $len); - let mut buf = [0u8; $len * 2]; - let buf = $crate::hex::buf_encoder::AsOutBytes::as_mut_out_bytes(&mut buf); - $crate::hex::display::fmt_hex_exact_fn($formatter, buf, $bytes, $case) - }}; -} -pub use fmt_hex_exact; - -// Implementation detail of `write_hex_exact` macro to de-duplicate the code -#[doc(hidden)] -#[inline] -pub fn fmt_hex_exact_fn( - f: &mut fmt::Formatter, - buf: &mut OutBytes, - bytes: I, - case: Case, -) -> fmt::Result -where - I: IntoIterator, - I::Item: Borrow, -{ - let mut encoder = BufEncoder::new(buf); - encoder.put_bytes(bytes, case); - f.pad_integral(true, "0x", encoder.as_str()) -} - -#[cfg(test)] -mod tests { - use super::*; - - fn check_encoding(bytes: &[u8]) { - use core::fmt::Write; - - let s1 = bytes.to_lower_hex_string(); - let mut s2 = String::with_capacity(bytes.len() * 2); - for b in bytes { - write!(s2, "{:02x}", b).unwrap(); - } - assert_eq!(s1, s2); - } - - #[test] - fn empty() { - check_encoding(b""); - } - - #[test] - fn single() { - check_encoding(b"*"); - } - - #[test] - fn two() { - check_encoding(b"*x"); - } - - #[test] - fn just_below_boundary() { - check_encoding(&[42; 512]); - } - - #[test] - fn just_above_boundary() { - check_encoding(&[42; 513]); - } - - #[test] - fn just_above_double_boundary() { - check_encoding(&[42; 1025]); - } - - #[test] - fn fmt_exact_macro() { - struct Dummy([u8; 32]); - - impl fmt::Display for Dummy { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt_hex_exact!(f, 32, &self.0, Case::Lower) - } - } - - assert_eq!(Dummy([42; 32]).to_string(), "2a".repeat(32)); - } -} diff --git a/internals/src/hex/mod.rs b/internals/src/hex/mod.rs deleted file mode 100644 index 6169e881b..000000000 --- a/internals/src/hex/mod.rs +++ /dev/null @@ -1,49 +0,0 @@ -//! Helpers for encoding bytes as hex strings. - -pub mod buf_encoder; -pub mod display; - -pub use buf_encoder::BufEncoder; - -/// Reexports of extension traits. -pub mod exts { - pub use super::display::DisplayHex; -} - -/// Possible case of hex. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)] -pub enum Case { - /// Produce lower-case chars (`[0-9a-f]`). - /// - /// This is the default. - #[default] - Lower, - - /// Produce upper-case chars (`[0-9A-F]`). - Upper, -} - -impl Case { - /// Returns the encoding table. - /// - /// The returned table may only contain displayable ASCII chars. - #[inline] - #[rustfmt::skip] - pub(crate) fn table(self) -> &'static [u8; 16] { - static LOWER: [u8; 16] = *b"0123456789abcdef"; - static UPPER: [u8; 16] = *b"0123456789ABCDEF"; - - match self { - Case::Lower => &LOWER, - Case::Upper => &UPPER, - } - } -} - -/// Encodes single byte as two ASCII chars using the given table. -/// -/// The function guarantees only returning values from the provided table. -#[inline] -pub(crate) fn byte_to_hex(byte: u8, table: &[u8; 16]) -> [u8; 2] { - [table[usize::from(byte.wrapping_shr(4))], table[usize::from(byte & 0x0F)]] -} diff --git a/internals/src/lib.rs b/internals/src/lib.rs index 8c31cffb1..e99d8c1fb 100644 --- a/internals/src/lib.rs +++ b/internals/src/lib.rs @@ -13,5 +13,4 @@ #![warn(missing_docs)] pub mod error; -pub mod hex; pub mod macros; diff --git a/key-wallet-ffi/src/managed_account.rs b/key-wallet-ffi/src/managed_account.rs index 84f9a99c2..d200bbddf 100644 --- a/key-wallet-ffi/src/managed_account.rs +++ b/key-wallet-ffi/src/managed_account.rs @@ -2771,8 +2771,6 @@ mod tests { /// consumer decoding `tx_data`. #[test] fn test_transaction_record_exposes_typed_proreg_payload() { - use dashcore::hashes::hex::FromHex; - let tx = proreg_transaction(); let record = TransactionRecord::new( tx, @@ -2797,7 +2795,8 @@ mod tests { assert_eq!(service, "54.148.58.128:9999"); assert_eq!(reg.service_port, 9999); let expected_hash = - <[u8; 20]>::from_hex("70993555a01f7e8d6179d6135b5c56809d2d1d36").expect("hash160"); + hex_conservative::decode_to_array::<20>("70993555a01f7e8d6179d6135b5c56809d2d1d36") + .expect("hash160"); assert_eq!(reg.owner_key_hash, expected_hash); assert_eq!(reg.voting_key_hash, expected_hash); diff --git a/key-wallet-ffi/src/special_payload.rs b/key-wallet-ffi/src/special_payload.rs index 44f080715..24f13f4a8 100644 --- a/key-wallet-ffi/src/special_payload.rs +++ b/key-wallet-ffi/src/special_payload.rs @@ -426,7 +426,6 @@ mod tests { use super::*; use dashcore::bls_sig_utils::BLSSignature; use dashcore::hash_types::InputsHash; - use dashcore::hashes::hex::FromHex; use dashcore::Txid; use std::ffi::CStr; @@ -452,7 +451,8 @@ mod tests { assert_eq!(reg.service_ip, expected_ip); let expected_hash = - <[u8; 20]>::from_hex("70993555a01f7e8d6179d6135b5c56809d2d1d36").expect("hash160"); + hex_conservative::decode_to_array::<20>("70993555a01f7e8d6179d6135b5c56809d2d1d36") + .expect("hash160"); assert_eq!(reg.owner_key_hash, expected_hash); assert_eq!(reg.voting_key_hash, expected_hash); assert_eq!(reg.operator_public_key, [0x11; 48]); @@ -466,7 +466,7 @@ mod tests { unsafe { std::slice::from_raw_parts(reg.script_payout, reg.script_payout_len) }; assert_eq!( script, - Vec::::from_hex("76a914fef33f56f709ba6b08d073932f925afedb606d0288ac") + hex_conservative::decode_to_vec("76a914fef33f56f709ba6b08d073932f925afedb606d0288ac") .expect("script hex") .as_slice() ); diff --git a/key-wallet-manager/Cargo.toml b/key-wallet-manager/Cargo.toml index 9bce3a997..614d57afc 100644 --- a/key-wallet-manager/Cargo.toml +++ b/key-wallet-manager/Cargo.toml @@ -37,6 +37,7 @@ async-trait = "0.1" tokio = { version = "1", features = ["macros", "rt", "sync"] } tracing = "0.1" zeroize = { workspace = true } +hex-conservative = { workspace = true } rayon = { workspace = true, optional = true } bincode = { workspace = true, optional = true } serde = { workspace = true, features = ["derive"], optional = true } @@ -45,7 +46,6 @@ serde = { workspace = true, features = ["derive"], optional = true } key-wallet = { path = "../key-wallet", features = ["test-utils", "bincode"] } dashcore = { path = "../dash", features = ["test-utils"] } tokio = { version = "1", features = ["macros", "rt-multi-thread"] } -hex-conservative = { workspace = true } criterion = "0.8.1" [[bench]] diff --git a/key-wallet-manager/src/error.rs b/key-wallet-manager/src/error.rs index e4dbd35e1..1feccb763 100644 --- a/key-wallet-manager/src/error.rs +++ b/key-wallet-manager/src/error.rs @@ -1,6 +1,7 @@ //! Error types for the wallet manager. use crate::WalletId; +use hex_conservative::DisplayHex; use key_wallet::wallet::managed_wallet_info::transaction_builder::BuilderError; /// Wallet manager errors @@ -34,20 +35,8 @@ impl core::fmt::Display for WalletError { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { WalletError::WalletCreation(msg) => write!(f, "Wallet creation failed: {}", msg), - WalletError::WalletNotFound(id) => { - write!(f, "Wallet not found: ")?; - for byte in id.iter() { - write!(f, "{:02x}", byte)?; - } - Ok(()) - } - WalletError::WalletExists(id) => { - write!(f, "Wallet already exists: ")?; - for byte in id.iter() { - write!(f, "{:02x}", byte)?; - } - Ok(()) - } + WalletError::WalletNotFound(id) => write!(f, "Wallet not found: {:x}", id.as_hex()), + WalletError::WalletExists(id) => write!(f, "Wallet already exists: {:x}", id.as_hex()), WalletError::InvalidMnemonic(msg) => write!(f, "Invalid mnemonic: {}", msg), WalletError::AccountCreation(msg) => write!(f, "Account creation failed: {}", msg), WalletError::AccountNotFound(idx) => write!(f, "Account not found: {}", idx), diff --git a/key-wallet-manager/src/process_block.rs b/key-wallet-manager/src/process_block.rs index 742b92eab..f9cf90f12 100644 --- a/key-wallet-manager/src/process_block.rs +++ b/key-wallet-manager/src/process_block.rs @@ -2,11 +2,11 @@ use crate::events::{diff_account_balances, project_derived_addresses, DerivedAdd use crate::wallet_interface::{BlockProcessingResult, MempoolTransactionResult, WalletInterface}; use crate::{WalletEvent, WalletId, WalletManager}; use async_trait::async_trait; -use core::fmt::Write as _; use dashcore::ephemerealdata::chain_lock::ChainLock; use dashcore::ephemerealdata::instant_lock::InstantLock; use dashcore::prelude::CoreBlockHeight; use dashcore::{Address, Block, BlockHash, ScriptBuf, Transaction}; +use hex_conservative::DisplayHex; use key_wallet::account::AccountType; use key_wallet::managed_account::transaction_record::TransactionRecord; use key_wallet::transaction_checking::{BlockInfo, DerivedAddressInfo, TransactionContext}; @@ -494,10 +494,7 @@ impl WalletInterface for WalletM for (wallet_id, info) in &self.wallet_infos { let name = info.name().unwrap_or("unnamed"); - let mut wallet_id_hex = String::with_capacity(wallet_id.len() * 2); - for byte in wallet_id { - let _ = write!(&mut wallet_id_hex, "{:02x}", byte); - } + let wallet_id_hex = wallet_id.to_lower_hex_string(); let script_count = info.monitored_addresses().len(); let summary = format!("{} scripts", script_count); diff --git a/key-wallet/README.md b/key-wallet/README.md index 8e21cc133..97df73c36 100644 --- a/key-wallet/README.md +++ b/key-wallet/README.md @@ -76,7 +76,7 @@ The library is organized into several key modules: ```rust use key_wallet::{Wallet, Mnemonic, Network}; use key_wallet::mnemonic::Language; -use dashcore::hashes::hex::DisplayHex; +use hex_conservative::DisplayHex; // Generate a new mnemonic let mnemonic = Mnemonic::generate(24, Language::English)?; @@ -163,7 +163,7 @@ let pool_1_address = coinjoin_account.derive_address_at_pool(1, 0)?; ```rust #[cfg(feature = "eddsa")] { - use dashcore::hashes::hex::DisplayHex; + use hex_conservative::DisplayHex; use key_wallet::account::EdDSAAccount; // Create identity registration funding account diff --git a/key-wallet/src/bip32.rs b/key-wallet/src/bip32.rs index 18aa9d695..b74365ec5 100644 --- a/key-wallet/src/bip32.rs +++ b/key-wallet/src/bip32.rs @@ -35,6 +35,7 @@ use serde; #[cfg(feature = "bincode")] use bincode_derive::{Decode, Encode}; use dashcore::{base58, Network}; +use hex_conservative::DisplayHex; use zeroize::Zeroize; /// XpubIdentifier as a hash160 result @@ -90,10 +91,7 @@ impl TryFrom<&[u8]> for ChainCode { impl fmt::Display for ChainCode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for &byte in &self.0 { - write!(f, "{:02x}", byte)?; - } - Ok(()) + write!(f, "{:x}", self.0.as_hex()) } } @@ -122,10 +120,7 @@ impl zeroize::Zeroize for Fingerprint { impl fmt::LowerHex for ChainCode { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for &byte in &self.0 { - write!(f, "{:02x}", byte)?; - } - Ok(()) + write!(f, "{:x}", self.0.as_hex()) } } @@ -194,9 +189,7 @@ impl<'de> serde::Deserialize<'de> for ChainCode { use serde::de::Error; let s = String::deserialize(deserializer)?; - let mut bytes = [0u8; 32]; - crate::utils::parse_hex_bytes(&s, &mut bytes).map_err(D::Error::custom)?; - Ok(ChainCode(bytes)) + hex_conservative::decode_to_array(&s).map(ChainCode).map_err(D::Error::custom) } } @@ -244,10 +237,7 @@ impl TryFrom<&[u8]> for Fingerprint { impl fmt::Display for Fingerprint { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for &byte in &self.0 { - write!(f, "{:02x}", byte)?; - } - Ok(()) + write!(f, "{:x}", self.0.as_hex()) } } @@ -261,19 +251,15 @@ impl core::str::FromStr for Fingerprint { type Err = Error; fn from_str(s: &str) -> Result { - let mut bytes = [0u8; 4]; - crate::utils::parse_hex_bytes(s, &mut bytes) - .map_err(|_| Error::InvalidPublicKeyHexLength(s.len()))?; - Ok(Fingerprint(bytes)) + hex_conservative::decode_to_array(s) + .map(Fingerprint) + .map_err(|_| Error::InvalidPublicKeyHexLength(s.len())) } } impl fmt::LowerHex for Fingerprint { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for &byte in &self.0 { - write!(f, "{:02x}", byte)?; - } - Ok(()) + write!(f, "{:x}", self.0.as_hex()) } } @@ -852,10 +838,7 @@ impl fmt::Display for ChildNumber { ChildNumber::Hardened256 { index, } => { - write!(f, "0x")?; - for byte in index { - write!(f, "{:02x}", byte)?; - } + write!(f, "0x{:x}", index.as_hex())?; write!( f, "{}", @@ -869,11 +852,7 @@ impl fmt::Display for ChildNumber { ChildNumber::Normal256 { index, } => { - write!(f, "0x")?; - for byte in index { - write!(f, "{:02x}", byte)?; - } - Ok(()) + write!(f, "0x{:x}", index.as_hex()) } } } @@ -893,33 +872,8 @@ impl FromStr for ChildNumber { if index_str.starts_with("0x") || index_str.starts_with("0X") { // Parse as a 256-bit hex number let hex_str = &index_str[2..]; - // Simple hex decoder - let hex_bytes = hex_str - .as_bytes() - .chunks(2) - .map(|chunk| { - let high = chunk[0]; - let low = chunk.get(1).copied().unwrap_or(b'0'); - let h = match high { - b'0'..=b'9' => high - b'0', - b'a'..=b'f' => high - b'a' + 10, - b'A'..=b'F' => high - b'A' + 10, - _ => return Err(Error::InvalidChildNumberFormat), - }; - let l = match low { - b'0'..=b'9' => low - b'0', - b'a'..=b'f' => low - b'a' + 10, - b'A'..=b'F' => low - b'A' + 10, - _ => return Err(Error::InvalidChildNumberFormat), - }; - Ok((h << 4) | l) - }) - .collect::, Error>>()?; - if hex_bytes.len() != 32 { - return Err(Error::InvalidChildNumberFormat); - } - let mut index_bytes = [0u8; 32]; - index_bytes[32 - hex_bytes.len()..].copy_from_slice(&hex_bytes); + let index_bytes = hex_conservative::decode_to_array::<32>(hex_str) + .map_err(|_| Error::InvalidChildNumberFormat)?; if is_hardened { Ok(ChildNumber::Hardened256 { index: index_bytes, @@ -1296,7 +1250,7 @@ pub enum Error { /// Base58 encoding error Base58(base58::DecodeCheckError), /// Hexadecimal decoding error - Hex(dashcore_hashes::hex::HexToArrayError), + Hex(hex_conservative::DecodeFixedLengthBytesError), /// `PublicKey` hex should be 66 or 130 digits long. InvalidPublicKeyHexLength(usize), /// Something is not supported based on active features @@ -1947,12 +1901,28 @@ impl FromStr for ExtendedPubKey { mod tests { use core::str::FromStr; - use dashcore_hashes::hex::FromHex; - use super::ChildNumber::{Hardened, Normal}; use super::*; use dashcore::Network::{self, Mainnet}; use hex_conservative::DisplayHex; + use test_case::{test_case, test_matrix}; + + const INDEX_256: &str = "00000000000000000000000000000000000000000000000000000000000000ab"; + + #[test_matrix(["0x", "0X"], [INDEX_256.to_owned(), INDEX_256.to_uppercase()], ["", "'"])] + fn child_number_256_round_trips(prefix: &str, digits: String, suffix: &str) { + let parsed: ChildNumber = format!("{prefix}{digits}{suffix}").parse().unwrap(); + assert_eq!(parsed.is_hardened(), !suffix.is_empty()); + assert_eq!(parsed.to_string(), format!("0x{INDEX_256}{suffix}")); + } + + // 63 digits must not parse as if a trailing `0` were appended. + #[test_case(&INDEX_256[1..] ; "odd length")] + #[test_case(&INDEX_256[2..] ; "too short")] + #[test_case("00000000000000000000000000000000000000000000000000000000000000zz" ; "invalid digit")] + fn child_number_256_rejects(digits: &str) { + assert!(format!("0x{digits}").parse::().is_err()); + } #[test] fn test_parse_derivation_path() { @@ -2133,7 +2103,7 @@ mod tests { #[test] fn test_vector_1() { - let seed = Vec::from_hex("000102030405060708090a0b0c0d0e0f").unwrap(); + let seed = hex_conservative::decode_to_vec("000102030405060708090a0b0c0d0e0f").unwrap(); // m test_path( @@ -2192,7 +2162,7 @@ mod tests { #[test] fn test_vector_2() { - let seed = Vec::from_hex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542").unwrap(); + let seed = hex_conservative::decode_to_vec("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542").unwrap(); // m test_path( @@ -2251,7 +2221,7 @@ mod tests { #[test] fn test_vector_3() { - let seed = Vec::from_hex("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be").unwrap(); + let seed = hex_conservative::decode_to_vec("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be").unwrap(); // m test_path( @@ -2356,7 +2326,7 @@ mod tests { #[test] fn test_dashpay_vector_1() { - let seed = Vec::from_hex("b16d3782e714da7c55a397d5f19104cfed7ffa8036ac514509bbb50807f8ac598eeb26f0797bd8cc221a6cbff2168d90a5e9ee025a5bd977977b9eccd97894bb").unwrap(); + let seed = hex_conservative::decode_to_vec("b16d3782e714da7c55a397d5f19104cfed7ffa8036ac514509bbb50807f8ac598eeb26f0797bd8cc221a6cbff2168d90a5e9ee025a5bd977977b9eccd97894bb").unwrap(); // Test Vector 1: Non-hardened / Hardened path example test_path( @@ -2374,7 +2344,7 @@ mod tests { #[test] fn test_dashpay_vector_2() { - let seed = Vec::from_hex("b16d3782e714da7c55a397d5f19104cfed7ffa8036ac514509bbb50807f8ac598eeb26f0797bd8cc221a6cbff2168d90a5e9ee025a5bd977977b9eccd97894bb").unwrap(); + let seed = hex_conservative::decode_to_vec("b16d3782e714da7c55a397d5f19104cfed7ffa8036ac514509bbb50807f8ac598eeb26f0797bd8cc221a6cbff2168d90a5e9ee025a5bd977977b9eccd97894bb").unwrap(); // Test Vector 2: Multiple hardened derivations with final non-hardened index test_path( @@ -2392,7 +2362,7 @@ mod tests { #[test] fn test_dashpay_vector_3() { - let seed = Vec::from_hex("b16d3782e714da7c55a397d5f19104cfed7ffa8036ac514509bbb50807f8ac598eeb26f0797bd8cc221a6cbff2168d90a5e9ee025a5bd977977b9eccd97894bb").unwrap(); + let seed = hex_conservative::decode_to_vec("b16d3782e714da7c55a397d5f19104cfed7ffa8036ac514509bbb50807f8ac598eeb26f0797bd8cc221a6cbff2168d90a5e9ee025a5bd977977b9eccd97894bb").unwrap(); // Test Vector 3: Non-hardened derivation test_path( @@ -2406,7 +2376,7 @@ mod tests { #[test] fn test_dashpay_vector_4() { - let seed = Vec::from_hex("b16d3782e714da7c55a397d5f19104cfed7ffa8036ac514509bbb50807f8ac598eeb26f0797bd8cc221a6cbff2168d90a5e9ee025a5bd977977b9eccd97894bb").unwrap(); + let seed = hex_conservative::decode_to_vec("b16d3782e714da7c55a397d5f19104cfed7ffa8036ac514509bbb50807f8ac598eeb26f0797bd8cc221a6cbff2168d90a5e9ee025a5bd977977b9eccd97894bb").unwrap(); // Test Vector 4: Hardened path with complex indices test_path( diff --git a/key-wallet/src/lib.rs b/key-wallet/src/lib.rs index f71aacfa5..3e049ba64 100644 --- a/key-wallet/src/lib.rs +++ b/key-wallet/src/lib.rs @@ -35,7 +35,6 @@ pub mod mnemonic; pub mod seed; pub mod signer; pub mod transaction_checking; -pub(crate) mod utils; pub mod utxo; pub mod wallet; diff --git a/key-wallet/src/seed.rs b/key-wallet/src/seed.rs index 9bffa4fcc..f2d465226 100644 --- a/key-wallet/src/seed.rs +++ b/key-wallet/src/seed.rs @@ -7,7 +7,7 @@ use crate::error::{Error, Result}; use bincode_derive::{Decode, Encode}; use core::fmt; use core::str::FromStr; -use dashcore_hashes::hex::FromHex; +use hex_conservative::DisplayHex; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; use zeroize::Zeroize; @@ -53,19 +53,14 @@ impl Seed { /// Create a seed from hex string pub fn from_hex(hex_str: &str) -> Result { - let bytes = Vec::::from_hex(hex_str) + let bytes = hex_conservative::decode_to_vec(hex_str) .map_err(|e| Error::InvalidParameter(format!("Invalid hex: {}", e)))?; Self::from_slice(&bytes) } /// Convert to hex string pub fn to_hex(&self) -> String { - use core::fmt::Write; - let mut s = String::new(); - for byte in &self.0 { - write!(&mut s, "{:02x}", byte).unwrap(); - } - s + self.0.to_lower_hex_string() } /// Check if the seed is all zeros (empty/invalid) @@ -118,16 +113,7 @@ impl fmt::Debug for Seed { impl fmt::Display for Seed { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Show first and last 4 bytes in hex - use core::fmt::Write; - let mut start = String::new(); - let mut end = String::new(); - for byte in &self.0[..4] { - write!(&mut start, "{:02x}", byte).unwrap(); - } - for byte in &self.0[60..] { - write!(&mut end, "{:02x}", byte).unwrap(); - } - write!(f, "Seed({}...{})", start, end) + write!(f, "Seed({:x}...{:x})", self.0[..4].as_hex(), self.0[60..].as_hex()) } } diff --git a/key-wallet/src/utils.rs b/key-wallet/src/utils.rs deleted file mode 100644 index 850ff732b..000000000 --- a/key-wallet/src/utils.rs +++ /dev/null @@ -1,59 +0,0 @@ -//! Utility functions for the key-wallet library - -/// Parse a hex character to its numeric value -pub(crate) fn parse_hex_digit(digit: u8) -> Option { - match digit { - b'0'..=b'9' => Some(digit - b'0'), - b'a'..=b'f' => Some(digit - b'a' + 10), - b'A'..=b'F' => Some(digit - b'A' + 10), - _ => None, - } -} - -/// Parse a hex string into bytes -pub(crate) fn parse_hex_bytes(hex_str: &str, output: &mut [u8]) -> Result<(), &'static str> { - if hex_str.len() != output.len() * 2 { - return Err("invalid hex length"); - } - - for (i, chunk) in hex_str.as_bytes().chunks(2).enumerate() { - let high = parse_hex_digit(chunk[0]).ok_or("invalid hex character")?; - let low = parse_hex_digit(chunk[1]).ok_or("invalid hex character")?; - output[i] = (high << 4) | low; - } - Ok(()) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_parse_hex_digit() { - assert_eq!(parse_hex_digit(b'0'), Some(0)); - assert_eq!(parse_hex_digit(b'9'), Some(9)); - assert_eq!(parse_hex_digit(b'a'), Some(10)); - assert_eq!(parse_hex_digit(b'f'), Some(15)); - assert_eq!(parse_hex_digit(b'A'), Some(10)); - assert_eq!(parse_hex_digit(b'F'), Some(15)); - assert_eq!(parse_hex_digit(b'g'), None); - assert_eq!(parse_hex_digit(b'G'), None); - } - - #[test] - fn test_parse_hex_bytes() { - let mut output = [0u8; 4]; - assert!(parse_hex_bytes("deadbeef", &mut output).is_ok()); - assert_eq!(output, [0xde, 0xad, 0xbe, 0xef]); - - let mut output = [0u8; 2]; - assert!(parse_hex_bytes("1234", &mut output).is_ok()); - assert_eq!(output, [0x12, 0x34]); - - // Test error cases - let mut output = [0u8; 2]; - assert!(parse_hex_bytes("123", &mut output).is_err()); // Wrong length - assert!(parse_hex_bytes("12345", &mut output).is_err()); // Wrong length - assert!(parse_hex_bytes("12gg", &mut output).is_err()); // Invalid character - } -} diff --git a/key-wallet/src/wallet/mod.rs b/key-wallet/src/wallet/mod.rs index a174d4cee..926056039 100644 --- a/key-wallet/src/wallet/mod.rs +++ b/key-wallet/src/wallet/mod.rs @@ -24,6 +24,7 @@ use crate::Network; use bincode_derive::{Decode, Encode}; use core::fmt; use dashcore_hashes::{sha256, Hash}; +use hex_conservative::DisplayHex; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; use zeroize::Zeroize; @@ -205,8 +206,7 @@ impl Wallet { impl fmt::Display for Wallet { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Format wallet ID as hex string (first 8 chars) - let id_hex = - self.wallet_id.iter().take(4).map(|b| format!("{:02x}", b)).collect::(); + let id_hex = self.wallet_id[..4].to_lower_hex_string(); let total_accounts: usize = self.accounts.count(); @@ -720,7 +720,7 @@ mod tests { // regardless of the network passed to from_mnemonic. let none = Wallet::compute_wallet_id_from_root_extended_pub_key(&root, None); assert_eq!( - hex_lower(&none), + none.to_lower_hex_string(), "93401f55c5bc17629140344a2098ebdeb204dfdf1576e87605fbc7b655c86f08", "network-independent wallet id digest must remain byte-for-byte stable" ); @@ -728,7 +728,7 @@ mod tests { let mainnet = Wallet::compute_wallet_id_from_root_extended_pub_key(&root, Some(Network::Mainnet)); assert_eq!( - hex_lower(&mainnet), + mainnet.to_lower_hex_string(), "0b91f36de2613a410303e8309b4f92a150738ae018695d2030b33e64ccea7b2e", "network-scoped (mainnet) wallet id digest must remain byte-for-byte stable; \ a change here means DOMAIN_TAG or a discriminant byte shifted" @@ -765,8 +765,4 @@ mod tests { Wallet::new_external_signable(Network::Mainnet, stored_id, AccountCollection::new()); assert_eq!(external.compute_wallet_id(), stored_id); } - - fn hex_lower(bytes: &[u8]) -> String { - bytes.iter().map(|b| format!("{:02x}", b)).collect() - } } diff --git a/rpc-client/Cargo.toml b/rpc-client/Cargo.toml index dd27ebc36..63b062177 100644 --- a/rpc-client/Cargo.toml +++ b/rpc-client/Cargo.toml @@ -20,6 +20,7 @@ path = "src/lib.rs" [dependencies] dashcore-rpc-json = { path = "../rpc-json" } +hex-conservative = { workspace = true } jsonrpc = "0.18.0" tracing = "0.1" diff --git a/rpc-client/src/client.rs b/rpc-client/src/client.rs index a1a56da1c..d9461c099 100644 --- a/rpc-client/src/client.rs +++ b/rpc-client/src/client.rs @@ -26,7 +26,6 @@ use crate::error::*; use crate::json; use crate::queryable; use dashcore::hashes::Hash; -use dashcore::hashes::hex::FromHex; use dashcore::secp256k1::ecdsa::Signature; use dashcore::{ Address, Amount, Block, OutPoint, PrivateKey, ProTxHash, PublicKey, QuorumHash, Transaction, @@ -318,7 +317,7 @@ pub trait RpcApi: Sized { fn get_block(&self, hash: &BlockHash) -> Result { let hex: String = self.call("getblock", &[into_json(hash)?, 0.into()])?; - let bytes: Vec = FromHex::from_hex(&hex)?; + let bytes: Vec = hex_conservative::decode_to_vec(&hex)?; Ok(dashcore::consensus::encode::deserialize(&bytes)?) } @@ -337,7 +336,7 @@ pub trait RpcApi: Sized { fn get_block_header(&self, hash: &BlockHash) -> Result { let hex: String = self.call("getblockheader", &[into_json(hash)?, false.into()])?; - let bytes: Vec = FromHex::from_hex(&hex)?; + let bytes: Vec = hex_conservative::decode_to_vec(&hex)?; Ok(dashcore::consensus::encode::deserialize(&bytes)?) } @@ -440,7 +439,7 @@ pub trait RpcApi: Sized { ) -> Result { let mut args = [into_json(txid)?, into_json(false)?, opt_into_json(block_hash)?]; let hex: String = self.call("getrawtransaction", handle_defaults(&mut args, &[null()]))?; - let bytes: Vec = FromHex::from_hex(&hex)?; + let bytes: Vec = hex_conservative::decode_to_vec(&hex)?; Ok(dashcore::consensus::encode::deserialize(&bytes)?) } @@ -579,7 +578,7 @@ pub trait RpcApi: Sized { ) -> Result> { let mut args = [into_json(txids)?, opt_into_json(block_hash)?]; let hex: String = self.call("gettxoutproof", handle_defaults(&mut args, &[null()]))?; - Ok(FromHex::from_hex(&hex)?) + Ok(hex_conservative::decode_to_vec(&hex)?) } fn import_public_key( @@ -730,7 +729,7 @@ pub trait RpcApi: Sized { locktime: Option, ) -> Result { let hex: String = self.create_raw_transaction_hex(utxos, outs, locktime)?; - let bytes: Vec = FromHex::from_hex(&hex)?; + let bytes: Vec = hex_conservative::decode_to_vec(&hex)?; Ok(dashcore::consensus::encode::deserialize(&bytes)?) } @@ -1756,7 +1755,7 @@ mod tests { fn test_raw_tx() { use dashcore::consensus::encode; let client = Client::new("http://localhost/", Auth::None).unwrap(); - let tx: Transaction = encode::deserialize(&Vec::::from_hex("0200000001586bd02815cf5faabfec986a4e50d25dbee089bd2758621e61c5fab06c334af0000000006b483045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c012103dae30a4d7870cd87b45dd53e6012f71318fdd059c1c2623b8cc73f8af287bb2dfeffffff021dc4260c010000001976a914f602e88b2b5901d8aab15ebe4a97cf92ec6e03b388ac00e1f505000000001976a914687ffeffe8cf4e4c038da46a9b1d37db385a472d88acfd211500").unwrap()).unwrap(); + let tx: Transaction = encode::deserialize(&hex_conservative::decode_to_vec("0200000001586bd02815cf5faabfec986a4e50d25dbee089bd2758621e61c5fab06c334af0000000006b483045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c012103dae30a4d7870cd87b45dd53e6012f71318fdd059c1c2623b8cc73f8af287bb2dfeffffff021dc4260c010000001976a914f602e88b2b5901d8aab15ebe4a97cf92ec6e03b388ac00e1f505000000001976a914687ffeffe8cf4e4c038da46a9b1d37db385a472d88acfd211500").unwrap()).unwrap(); assert!(client.send_raw_transaction(&tx).is_err()); assert!(client.send_raw_transaction(&encode::serialize(&tx)).is_err()); diff --git a/rpc-client/src/error.rs b/rpc-client/src/error.rs index e6768cc5e..3afc5c9a0 100644 --- a/rpc-client/src/error.rs +++ b/rpc-client/src/error.rs @@ -11,7 +11,6 @@ use std::{error, fmt, io}; use crate::dashcore; -use dashcore::hashes::hex; use dashcore::secp256k1; pub type OldVecLen = usize; @@ -21,7 +20,7 @@ pub type FixedSizeLen = usize; #[derive(Debug)] pub enum Error { JsonRpc(jsonrpc::error::Error), - Hex(hex::HexToBytesError), + Hex(hex_conservative::DecodeVariableLengthBytesError), Json(serde_json::error::Error), BitcoinSerialization(dashcore::consensus::encode::Error), Secp256k1(secp256k1::Error), @@ -38,8 +37,8 @@ impl From for Error { } } -impl From for Error { - fn from(e: hex::HexToBytesError) -> Error { +impl From for Error { + fn from(e: hex_conservative::DecodeVariableLengthBytesError) -> Error { Error::Hex(e) } } diff --git a/rpc-client/src/queryable.rs b/rpc-client/src/queryable.rs index e8eb8d5aa..7ad515ed5 100644 --- a/rpc-client/src/queryable.rs +++ b/rpc-client/src/queryable.rs @@ -27,7 +27,7 @@ impl Queryable for dashcore::blockdata::block::Block { fn query(rpc: &C, id: &Self::Id) -> Result { let rpc_name = "getblock"; let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?, 0.into()])?; - let bytes: Vec = dashcore::hashes::hex::FromHex::from_hex(&hex)?; + let bytes: Vec = hex_conservative::decode_to_vec(&hex)?; Ok(dashcore::consensus::encode::deserialize(&bytes)?) } } @@ -38,7 +38,7 @@ impl Queryable for dashcore::blockdata::transaction::Transaction { fn query(rpc: &C, id: &Self::Id) -> Result { let rpc_name = "getrawtransaction"; let hex: String = rpc.call(rpc_name, &[serde_json::to_value(id)?])?; - let bytes: Vec = dashcore::hashes::hex::FromHex::from_hex(&hex)?; + let bytes: Vec = hex_conservative::decode_to_vec(&hex)?; Ok(dashcore::consensus::encode::deserialize(&bytes)?) } } diff --git a/rpc-integration-test/src/main.rs b/rpc-integration-test/src/main.rs index f245deeb4..b7ecda2ef 100644 --- a/rpc-integration-test/src/main.rs +++ b/rpc-integration-test/src/main.rs @@ -26,7 +26,6 @@ use dashcore_rpc::{ SignedAmount, Transaction, TxIn, TxOut, Txid, Witness, consensus::encode::{deserialize, serialize}, hashes::Hash, - hashes::hex::FromHex, secp256k1, }, }; @@ -454,7 +453,7 @@ fn test_get_block(cl: &Client) { let tip = cl.get_best_block_hash().unwrap(); let block = cl.get_block(&tip).unwrap(); let hex = cl.get_block_hex(&tip).unwrap(); - assert_eq!(block, deserialize(&Vec::::from_hex(&hex).unwrap()).unwrap()); + assert_eq!(block, deserialize(&hex_conservative::decode_to_vec(&hex).unwrap()).unwrap()); assert_eq!(hex, serialize(&block).to_lower_hex_string()); let tip = cl.get_best_block_hash().unwrap();