From 089a1e452dc1a99338122903205ae4f64d48d824 Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Sat, 3 Dec 2022 20:57:18 +0100 Subject: [PATCH] Replace `Vec::from_hex` with `hex!` This makes the code less noisy and is a preparation for changing it to `const`-based literal. Because of the preparation, places that used variables to store the hex string were changed to constants. There are still some instances of `Vec::from_hex` left - where they won't be changeable to `const` and where `hex!` is unavailable (integration tests). These may be dealt with later. See also #1189 --- bitcoin/src/address.rs | 2 +- bitcoin/src/base58.rs | 6 +-- bitcoin/src/bip158.rs | 45 ++++++++-------- bitcoin/src/bip32.rs | 8 +-- bitcoin/src/blockdata/block.rs | 29 +++++----- bitcoin/src/blockdata/constants.rs | 7 +-- bitcoin/src/blockdata/script.rs | 28 +++++----- bitcoin/src/blockdata/transaction.rs | 65 +++++++++++----------- bitcoin/src/blockdata/witness.rs | 32 ++++++----- bitcoin/src/internal_macros.rs | 2 +- bitcoin/src/merkle_tree/block.rs | 11 ++-- bitcoin/src/network/address.rs | 69 ++++++++++++------------ bitcoin/src/network/message.rs | 18 +++---- bitcoin/src/network/message_blockdata.rs | 12 ++--- bitcoin/src/network/message_network.rs | 7 +-- bitcoin/src/psbt/mod.rs | 28 +++++----- bitcoin/src/sighash.rs | 23 ++++---- 17 files changed, 195 insertions(+), 197 deletions(-) diff --git a/bitcoin/src/address.rs b/bitcoin/src/address.rs index 796b31b8..9caa972f 100644 --- a/bitcoin/src/address.rs +++ b/bitcoin/src/address.rs @@ -885,7 +885,7 @@ mod tests { use super::*; use crate::crypto::key::PublicKey; - use crate::hashes::hex::{FromHex, ToHex}; + use crate::hashes::hex::ToHex; use crate::internal_macros::{hex, hex_into, hex_script}; use crate::network::constants::Network::{Bitcoin, Testnet}; diff --git a/bitcoin/src/base58.rs b/bitcoin/src/base58.rs index 5e1658ea..9a46b039 100644 --- a/bitcoin/src/base58.rs +++ b/bitcoin/src/base58.rs @@ -273,7 +273,7 @@ impl std::error::Error for Error { #[cfg(test)] mod tests { use super::*; - use crate::hashes::hex::FromHex; + use crate::internal_macros::hex; #[test] fn test_base58_encode() { @@ -300,7 +300,7 @@ mod tests { assert_eq!(&res, exp); // Addresses - let addr = Vec::from_hex("00f8917303bfa8ef24f292e8fa1419b20460ba064d").unwrap(); + let addr = hex!("00f8917303bfa8ef24f292e8fa1419b20460ba064d"); assert_eq!(&encode_check(&addr[..]), "1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH"); } @@ -319,7 +319,7 @@ mod tests { // Addresses assert_eq!( decode_check("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH").ok(), - Some(Vec::from_hex("00f8917303bfa8ef24f292e8fa1419b20460ba064d").unwrap()) + Some(hex!("00f8917303bfa8ef24f292e8fa1419b20460ba064d")) ); // Non Base58 char. assert_eq!(decode("ยข").unwrap_err(), Error::BadByte(194)); diff --git a/bitcoin/src/bip158.rs b/bitcoin/src/bip158.rs index d487f7bb..34dbc244 100644 --- a/bitcoin/src/bip158.rs +++ b/bitcoin/src/bip158.rs @@ -563,6 +563,7 @@ mod test { use crate::hash_types::BlockHash; use crate::hashes::hex::FromHex; use crate::ScriptBuf; + use crate::internal_macros::hex; #[test] fn test_blockfilters() { @@ -573,12 +574,12 @@ mod test { for t in testdata.iter().skip(1) { let block_hash = BlockHash::from_hex(t.get(1).unwrap().as_str().unwrap()).unwrap(); let block: Block = - deserialize(&Vec::from_hex(t.get(2).unwrap().as_str().unwrap()).unwrap()).unwrap(); + deserialize(&hex!(t.get(2).unwrap().as_str().unwrap())).unwrap(); assert_eq!(block.block_hash(), block_hash); let scripts = t.get(3).unwrap().as_array().unwrap(); let previous_filter_header = FilterHeader::from_hex(t.get(4).unwrap().as_str().unwrap()).unwrap(); - let filter_content = Vec::from_hex(t.get(5).unwrap().as_str().unwrap()).unwrap(); + let filter_content = hex!(t.get(5).unwrap().as_str().unwrap()); let filter_header = FilterHeader::from_hex(t.get(6).unwrap().as_str().unwrap()).unwrap(); @@ -588,7 +589,7 @@ mod test { for input in tx.input.iter() { txmap.insert( input.previous_output, - ScriptBuf::from(Vec::from_hex(si.next().unwrap().as_str().unwrap()).unwrap()), + ScriptBuf::from(hex!(si.next().unwrap().as_str().unwrap())), ); } } @@ -635,22 +636,22 @@ mod test { fn test_filter() { let mut patterns = HashSet::new(); - patterns.insert(Vec::from_hex("000000").unwrap()); - patterns.insert(Vec::from_hex("111111").unwrap()); - patterns.insert(Vec::from_hex("222222").unwrap()); - patterns.insert(Vec::from_hex("333333").unwrap()); - patterns.insert(Vec::from_hex("444444").unwrap()); - patterns.insert(Vec::from_hex("555555").unwrap()); - patterns.insert(Vec::from_hex("666666").unwrap()); - patterns.insert(Vec::from_hex("777777").unwrap()); - patterns.insert(Vec::from_hex("888888").unwrap()); - patterns.insert(Vec::from_hex("999999").unwrap()); - patterns.insert(Vec::from_hex("aaaaaa").unwrap()); - patterns.insert(Vec::from_hex("bbbbbb").unwrap()); - patterns.insert(Vec::from_hex("cccccc").unwrap()); - patterns.insert(Vec::from_hex("dddddd").unwrap()); - patterns.insert(Vec::from_hex("eeeeee").unwrap()); - patterns.insert(Vec::from_hex("ffffff").unwrap()); + patterns.insert(hex!("000000")); + patterns.insert(hex!("111111")); + patterns.insert(hex!("222222")); + patterns.insert(hex!("333333")); + patterns.insert(hex!("444444")); + patterns.insert(hex!("555555")); + patterns.insert(hex!("666666")); + patterns.insert(hex!("777777")); + patterns.insert(hex!("888888")); + patterns.insert(hex!("999999")); + patterns.insert(hex!("aaaaaa")); + patterns.insert(hex!("bbbbbb")); + patterns.insert(hex!("cccccc")); + patterns.insert(hex!("dddddd")); + patterns.insert(hex!("eeeeee")); + patterns.insert(hex!("ffffff")); let mut out = Vec::new(); { @@ -664,14 +665,14 @@ mod test { let bytes = out; { - let query = vec![Vec::from_hex("abcdef").unwrap(), Vec::from_hex("eeeeee").unwrap()]; + let query = vec![hex!("abcdef"), hex!("eeeeee")]; let reader = GcsFilterReader::new(0, 0, M, P); assert!(reader .match_any(&mut bytes.as_slice(), &mut query.iter().map(|v| v.as_slice())) .unwrap()); } { - let query = vec![Vec::from_hex("abcdef").unwrap(), Vec::from_hex("123456").unwrap()]; + let query = vec![hex!("abcdef"), hex!("123456")]; let reader = GcsFilterReader::new(0, 0, M, P); assert!(!reader .match_any(&mut bytes.as_slice(), &mut query.iter().map(|v| v.as_slice())) @@ -693,7 +694,7 @@ mod test { for p in &patterns { query.push(p.clone()); } - query.push(Vec::from_hex("abcdef").unwrap()); + query.push(hex!("abcdef")); assert!(!reader .match_all(&mut bytes.as_slice(), &mut query.iter().map(|v| v.as_slice())) .unwrap()); diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index 777eae77..bb1cb928 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -840,7 +840,7 @@ mod tests { use super::ChildNumber::{Hardened, Normal}; use super::*; - use crate::hashes::hex::FromHex; + use crate::internal_macros::hex; use crate::network::constants::Network::{self, Bitcoin}; #[test] @@ -1014,7 +1014,7 @@ mod tests { #[test] fn test_vector_1() { let secp = Secp256k1::new(); - let seed = Vec::from_hex("000102030405060708090a0b0c0d0e0f").unwrap(); + let seed = hex!("000102030405060708090a0b0c0d0e0f"); // m test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(), @@ -1050,7 +1050,7 @@ mod tests { #[test] fn test_vector_2() { let secp = Secp256k1::new(); - let seed = Vec::from_hex("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542").unwrap(); + let seed = hex!("fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"); // m test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(), @@ -1086,7 +1086,7 @@ mod tests { #[test] fn test_vector_3() { let secp = Secp256k1::new(); - let seed = Vec::from_hex("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be").unwrap(); + let seed = hex!("4b381541583be4423346c643850da4b320e46a87ae3d2a4e6da11eba819cd4acba45d239319ac14f863b8d5ab5a0d0c64d2e8a1e7d1457df2e5a3c51c73235be"); // m test_path(&secp, Bitcoin, &seed, "m".parse().unwrap(), diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 277a7ea9..120b6896 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -418,12 +418,13 @@ mod tests { use crate::hashes::hex::FromHex; use crate::consensus::encode::{deserialize, serialize}; + use crate::internal_macros::hex; #[test] fn test_coinbase_and_bip34() { // testnet block 100,000 - let block_hex = "0200000035ab154183570282ce9afc0b494c9fc6a3cfea05aa8c1add2ecc56490000000038ba3d78e4500a5a7570dbe61960398add4410d278b21cd9708e6d9743f374d544fc055227f1001c29c1ea3b0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3703a08601000427f1001c046a510100522cfabe6d6d0000000000000000000068692066726f6d20706f6f6c7365727665726aac1eeeed88ffffffff0100f2052a010000001976a914912e2b234f941f30b18afbb4fa46171214bf66c888ac00000000"; - let block: Block = deserialize(&Vec::::from_hex(block_hex).unwrap()).unwrap(); + const BLOCK_HEX: &str = "0200000035ab154183570282ce9afc0b494c9fc6a3cfea05aa8c1add2ecc56490000000038ba3d78e4500a5a7570dbe61960398add4410d278b21cd9708e6d9743f374d544fc055227f1001c29c1ea3b0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3703a08601000427f1001c046a510100522cfabe6d6d0000000000000000000068692066726f6d20706f6f6c7365727665726aac1eeeed88ffffffff0100f2052a010000001976a914912e2b234f941f30b18afbb4fa46171214bf66c888ac00000000"; + let block: Block = deserialize(&hex!(BLOCK_HEX)).unwrap(); let cb_txid = "d574f343976d8e70d91cb278d21044dd8a396019e6db70755a0a50e4783dba38"; assert_eq!(block.coinbase().unwrap().txid().to_string(), cb_txid); @@ -432,8 +433,8 @@ mod tests { // block with 9-byte bip34 push - let bad_hex = "0200000035ab154183570282ce9afc0b494c9fc6a3cfea05aa8c1add2ecc56490000000038ba3d78e4500a5a7570dbe61960398add4410d278b21cd9708e6d9743f374d544fc055227f1001c29c1ea3b0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3d09a08601112233445566000427f1001c046a510100522cfabe6d6d0000000000000000000068692066726f6d20706f6f6c7365727665726aac1eeeed88ffffffff0100f2052a010000001976a914912e2b234f941f30b18afbb4fa46171214bf66c888ac00000000"; - let bad: Block = deserialize(&Vec::::from_hex(bad_hex).unwrap()).unwrap(); + const BAD_HEX: &str = "0200000035ab154183570282ce9afc0b494c9fc6a3cfea05aa8c1add2ecc56490000000038ba3d78e4500a5a7570dbe61960398add4410d278b21cd9708e6d9743f374d544fc055227f1001c29c1ea3b0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff3d09a08601112233445566000427f1001c046a510100522cfabe6d6d0000000000000000000068692066726f6d20706f6f6c7365727665726aac1eeeed88ffffffff0100f2052a010000001976a914912e2b234f941f30b18afbb4fa46171214bf66c888ac00000000"; + let bad: Block = deserialize(&hex!(BAD_HEX)).unwrap(); let push = Vec::::from_hex("a08601112233445566").unwrap(); assert_eq!(bad.bip34_block_height(), Err(super::Bip34Error::UnexpectedPush(push))); @@ -442,11 +443,11 @@ mod tests { #[test] fn block_test() { // Mainnet block 00000000b0c5a240b2a61d2e75692224efd4cbecdf6eaf4cc2cf477ca7c270e7 - let some_block = Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac00000000").unwrap(); - let cutoff_block = Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac").unwrap(); + let some_block = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac00000000"); + let cutoff_block = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0804ffff001d026e04ffffffff0100f2052a0100000043410446ef0102d1ec5240f0d061a4246c1bdef63fc3dbab7733052fbbf0ecd8f41fc26bf049ebb4f9527f374280259e7cfa99c48b0e3f39c51347a19a5819651503a5ac00000000010000000321f75f3139a013f50f315b23b0c9a2b6eac31e2bec98e5891c924664889942260000000049483045022100cb2c6b346a978ab8c61b18b5e9397755cbd17d6eb2fe0083ef32e067fa6c785a02206ce44e613f31d9a6b0517e46f3db1576e9812cc98d159bfdaf759a5014081b5c01ffffffff79cda0945903627c3da1f85fc95d0b8ee3e76ae0cfdc9a65d09744b1f8fc85430000000049483045022047957cdd957cfd0becd642f6b84d82f49b6cb4c51a91f49246908af7c3cfdf4a022100e96b46621f1bffcf5ea5982f88cef651e9354f5791602369bf5a82a6cd61a62501fffffffffe09f5fe3ffbf5ee97a54eb5e5069e9da6b4856ee86fc52938c2f979b0f38e82000000004847304402204165be9a4cbab8049e1af9723b96199bfd3e85f44c6b4c0177e3962686b26073022028f638da23fc003760861ad481ead4099312c60030d4cb57820ce4d33812a5ce01ffffffff01009d966b01000000434104ea1feff861b51fe3f5f8a3b12d0f4712db80e919548a80839fc47c6a21e66d957e9c5d8cd108c7a2d2324bad71f9904ac0ae7336507d785b17a2c115e427a32fac"); - let prevhash = Vec::from_hex("4ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000").unwrap(); - let merkle = Vec::from_hex("bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914c").unwrap(); + let prevhash = hex!("4ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000"); + let merkle = hex!("bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914c"); let work = Work::from(0x100010001_u128); let decode: Result = deserialize(&some_block); @@ -484,8 +485,8 @@ mod tests { let decode: Result = deserialize(&segwit_block); - let prevhash = Vec::from_hex("2aa2f2ca794ccbd40c16e2f3333f6b8b683f9e7179b2c4d74906000000000000").unwrap(); - let merkle = Vec::from_hex("10bc26e70a2f672ad420a6153dd0c28b40a6002c55531bfc99bf8994a8e8f67e").unwrap(); + let prevhash = hex!("2aa2f2ca794ccbd40c16e2f3333f6b8b683f9e7179b2c4d74906000000000000"); + let merkle = hex!("10bc26e70a2f672ad420a6153dd0c28b40a6002c55531bfc99bf8994a8e8f67e"); let work = Work::from(0x257c3becdacc64_u64); assert!(decode.is_ok()); @@ -513,13 +514,13 @@ mod tests { #[test] fn block_version_test() { - let block = Vec::from_hex("ffffff7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let block = hex!("ffffff7f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); let decode: Result = deserialize(&block); assert!(decode.is_ok()); let real_decode = decode.unwrap(); assert_eq!(real_decode.header.version, Version(2147483647)); - let block2 = Vec::from_hex("000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let block2 = hex!("000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); let decode2: Result = deserialize(&block2); assert!(decode2.is_ok()); let real_decode2 = decode2.unwrap(); @@ -528,7 +529,7 @@ mod tests { #[test] fn validate_pow_test() { - let some_header = Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b").unwrap(); + let some_header = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b"); let some_header: Header = deserialize(&some_header).expect("Can't deserialize correct block header"); assert_eq!(some_header.validate_pow(some_header.target()).unwrap(), some_header.block_hash()); @@ -549,7 +550,7 @@ mod tests { #[test] fn compact_roundrtip_test() { - let some_header = Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b").unwrap(); + let some_header = hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b"); let header: Header = deserialize(&some_header).expect("Can't deserialize correct block header"); diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index 70b304d2..b5e18bbf 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -192,10 +192,11 @@ impl ChainHash { #[cfg(test)] mod test { use super::*; - use crate::hashes::hex::{ToHex, FromHex}; + use crate::hashes::hex::ToHex; use crate::network::constants::Network; use crate::consensus::encode::serialize; use crate::blockdata::locktime::absolute; + use crate::internal_macros::hex; #[test] fn bitcoin_genesis_first_transaction() { @@ -206,12 +207,12 @@ mod test { assert_eq!(gen.input[0].previous_output.txid, Hash::all_zeros()); assert_eq!(gen.input[0].previous_output.vout, 0xFFFFFFFF); assert_eq!(serialize(&gen.input[0].script_sig), - Vec::from_hex("4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73").unwrap()); + hex!("4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73")); assert_eq!(gen.input[0].sequence, Sequence::MAX); assert_eq!(gen.output.len(), 1); assert_eq!(serialize(&gen.output[0].script_pubkey), - Vec::from_hex("434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac").unwrap()); + hex!("434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac")); assert_eq!(gen.output[0].value, 50 * COIN_VALUE); assert_eq!(gen.lock_time, absolute::LockTime::ZERO); diff --git a/bitcoin/src/blockdata/script.rs b/bitcoin/src/blockdata/script.rs index 6be61312..979b4b3d 100644 --- a/bitcoin/src/blockdata/script.rs +++ b/bitcoin/src/blockdata/script.rs @@ -2015,7 +2015,7 @@ mod test { use crate::blockdata::opcodes; use crate::crypto::key::PublicKey; use crate::psbt::serialize::Serialize; - use crate::internal_macros::hex_script; + use crate::internal_macros::{hex, hex_script}; #[test] fn script() { @@ -2041,12 +2041,12 @@ mod test { script = script.push_slice("NRA4VR".as_bytes()); comp.extend([6u8, 78, 82, 65, 52, 86, 82].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]); // keys - let keystr = "21032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af"; - let key = PublicKey::from_str(&keystr[2..]).unwrap(); - script = script.push_key(&key); comp.extend(Vec::from_hex(keystr).unwrap().iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]); - let keystr = "41042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133"; - let key = PublicKey::from_str(&keystr[2..]).unwrap(); - script = script.push_key(&key); comp.extend(Vec::from_hex(keystr).unwrap().iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]); + const KEYSTR1: &str = "21032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af"; + let key = PublicKey::from_str(&KEYSTR1[2..]).unwrap(); + script = script.push_key(&key); comp.extend_from_slice(&hex!(KEYSTR1)); assert_eq!(script.as_bytes(), &comp[..]); + const KEYSTR2: &str = "41042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133"; + let key = PublicKey::from_str(&KEYSTR2[2..]).unwrap(); + script = script.push_key(&key); comp.extend_from_slice(&hex!(KEYSTR2)); assert_eq!(script.as_bytes(), &comp[..]); // opcodes script = script.push_opcode(OP_CHECKSIG); comp.push(0xACu8); assert_eq!(script.as_bytes(), &comp[..]); @@ -2193,10 +2193,10 @@ mod test { // Notice the "20" which prepends the keystr. That 20 is hexidecimal for "32". The Builder automatically adds the 32 opcode // to our script in order to give a heads up to the script compiler that it should add the next 32 bytes to the stack. // From: https://github.com/bitcoin-core/btcdeb/blob/e8c2750c4a4702768c52d15640ed03bf744d2601/doc/tapscript-example.md?plain=1#L43 - let keystr = "209997a497d964fc1a62885b05a51166a65a90df00492c8d7cf61d6accf54803be"; - let x_only_key = XOnlyPublicKey::from_str(&keystr[2..]).unwrap(); + const KEYSTR: &str = "209997a497d964fc1a62885b05a51166a65a90df00492c8d7cf61d6accf54803be"; + let x_only_key = XOnlyPublicKey::from_str(&KEYSTR[2..]).unwrap(); let script = Builder::new().push_x_only_key(&x_only_key); - assert_eq!(script.into_bytes(), Vec::from_hex(keystr).unwrap()); + assert_eq!(script.into_bytes(), hex!(KEYSTR)); } #[test] @@ -2204,7 +2204,7 @@ mod test { // from txid 3bb5e6434c11fb93f64574af5d116736510717f2c595eb45b52c28e31622dfff which was in my mempool when I wrote the test let script = Builder::new().push_opcode(OP_DUP) .push_opcode(OP_HASH160) - .push_slice(&Vec::from_hex("16e1ae70ff0fa102905d4af297f6912bda6cce19").unwrap()) + .push_slice(&hex!("16e1ae70ff0fa102905d4af297f6912bda6cce19")) .push_opcode(OP_EQUALVERIFY) .push_opcode(OP_CHECKSIG) .into_script(); @@ -2317,7 +2317,7 @@ mod test { #[test] fn script_serialize() { - let hex_script = Vec::from_hex("6c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52").unwrap(); + let hex_script = hex!("6c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52"); let script: Result = deserialize(&hex_script); assert!(script.is_ok()); assert_eq!(serialize(&script.unwrap()), hex_script); @@ -2553,8 +2553,8 @@ mod test { #[cfg(feature = "bitcoinconsensus")] fn test_bitcoinconsensus () { // a random segwit transaction from the blockchain using native segwit - let spent = Builder::from(Vec::from_hex("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d").unwrap()).into_script(); - let spending = Vec::from_hex("010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000").unwrap(); + let spent = Builder::from(hex!("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d")).into_script(); + let spending = hex!("010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000"); spent.verify(0, crate::Amount::from_sat(18393430), spending.as_slice()).unwrap(); } diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index f41c2243..7815e038 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -1140,6 +1140,7 @@ mod tests { use crate::sighash::EcdsaSighashType; use crate::hashes::hex::FromHex; + use crate::internal_macros::hex; use crate::hash_types::*; @@ -1148,7 +1149,7 @@ mod tests { #[test] fn encode_to_unsized_writer() { let mut buf = [0u8; 1024]; - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex!(SOME_TX); let tx: Transaction = Decodable::consensus_decode(&mut raw_tx.as_slice()).unwrap(); let size = tx.consensus_encode(&mut &mut buf[..]).unwrap(); @@ -1191,7 +1192,7 @@ mod tests { #[test] fn test_txin() { - let txin: Result = deserialize(&Vec::from_hex("a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff").unwrap()); + let txin: Result = deserialize(&hex!("a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff")); assert!(txin.is_ok()); } @@ -1212,14 +1213,14 @@ mod tests { let genesis = constants::genesis_block(Network::Bitcoin); assert! (genesis.txdata[0].is_coin_base()); - let tx_bytes = Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); + let tx_bytes = hex!("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"); let tx: Transaction = deserialize(&tx_bytes).unwrap(); assert!(!tx.is_coin_base()); } #[test] fn test_nonsegwit_transaction() { - let tx_bytes = Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); + let tx_bytes = hex!("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"); let tx: Result = deserialize(&tx_bytes); assert!(tx.is_ok()); let realtx = tx.unwrap(); @@ -1247,13 +1248,13 @@ mod tests { #[test] fn test_segwit_transaction() { - let tx_bytes = Vec::from_hex( + let tx_bytes = hex!( "02000000000101595895ea20179de87052b4046dfe6fd515860505d6511a9004cf12a1f93cac7c01000000\ 00ffffffff01deb807000000000017a9140f3444e271620c736808aa7b33e370bd87cb5a078702483045022\ 100fb60dad8df4af2841adc0346638c16d0b8035f5e3f3753b88db122e70c79f9370220756e6633b17fd271\ 0e626347d28d60b0a2d6cbb41de51740644b9fb3ba7751040121028fa937ca8cba2197a37c007176ed89410\ 55d3bcb8627d085e94553e62f057dcc00000000" - ).unwrap(); + ); let tx: Result = deserialize(&tx_bytes); assert!(tx.is_ok()); let realtx = tx.unwrap(); @@ -1313,13 +1314,13 @@ mod tests { #[test] fn test_transaction_version() { - let tx_bytes = Vec::from_hex("ffffff7f0100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); + let tx_bytes = hex!("ffffff7f0100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000"); let tx: Result = deserialize(&tx_bytes); assert!(tx.is_ok()); let realtx = tx.unwrap(); assert_eq!(realtx.version, 2147483647); - let tx2_bytes = Vec::from_hex("000000800100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap(); + let tx2_bytes = hex!("000000800100000000000000000000000000000000000000000000000000000000000000000000000000ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000"); let tx2: Result = deserialize(&tx2_bytes); assert!(tx2.is_ok()); let realtx2 = tx2.unwrap(); @@ -1328,9 +1329,9 @@ mod tests { #[test] fn tx_no_input_deserialization() { - let tx_bytes = Vec::from_hex( + let tx_bytes = hex!( "010000000001000100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000" - ).unwrap(); + ); let tx: Transaction = deserialize(&tx_bytes).expect("deserialize tx"); assert_eq!(tx.input.len(), 0); @@ -1342,7 +1343,7 @@ mod tests { #[test] fn test_ntxid() { - let tx_bytes = Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); + let tx_bytes = hex!("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"); let mut tx: Transaction = deserialize(&tx_bytes).unwrap(); let old_ntxid = tx.ntxid(); @@ -1358,7 +1359,7 @@ mod tests { #[test] fn test_txid() { // segwit tx from Liquid integration tests, txid/hash from Core decoderawtransaction - let tx_bytes = Vec::from_hex( + let tx_bytes = hex!( "01000000000102ff34f95a672bb6a4f6ff4a7e90fa8c7b3be7e70ffc39bc99be3bda67942e836c00000000\ 23220020cde476664d3fa347b8d54ef3aee33dcb686a65ced2b5207cbf4ec5eda6b9b46e4f414d4c934ad8\ 1d330314e888888e3bd22c7dde8aac2ca9227b30d7c40093248af7812201000000232200200af6f6a071a6\ @@ -1387,7 +1388,7 @@ mod tests { ab0281d17cfca1cc035966d335f474932d5e6c5422210355fbb768ce3ce39360277345dbb5f376e706459e\ 5a2b5e0e09a535e61690647021023222ceec58b94bd25925dd9743dae6b928737491bd940fc5dd7c6f5d5f\ 2adc1e53ae00000000" - ).unwrap(); + ); let tx: Transaction = deserialize(&tx_bytes).unwrap(); assert_eq!(format!("{:x}", tx.wtxid()), "d6ac4a5e61657c4c604dcde855a1db74ec6b3e54f32695d72c5e11c7761ea1b4"); @@ -1395,14 +1396,14 @@ mod tests { assert_eq!(tx.weight(), 2718); // non-segwit tx from my mempool - let tx_bytes = Vec::from_hex( + let tx_bytes = hex!( "01000000010c7196428403d8b0c88fcb3ee8d64f56f55c8973c9ab7dd106bb4f3527f5888d000000006a47\ 30440220503a696f55f2c00eee2ac5e65b17767cd88ed04866b5637d3c1d5d996a70656d02202c9aff698f\ 343abb6d176704beda63fcdec503133ea4f6a5216b7f925fa9910c0121024d89b5a13d6521388969209df2\ 7a8469bd565aff10e8d42cef931fad5121bfb8ffffffff02b825b404000000001976a914ef79e7ee9fff98\ bcfd08473d2b76b02a48f8c69088ac0000000000000000296a273236303039343836393731373233313237\ 3633313032313332353630353838373931323132373000000000" - ).unwrap(); + ); let tx: Transaction = deserialize(&tx_bytes).unwrap(); assert_eq!(format!("{:x}", tx.wtxid()), "971ed48a62c143bbd9c87f4bafa2ef213cfa106c6e140f111931d0be307468dd"); @@ -1412,7 +1413,7 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_txn_encode_decode() { - let tx_bytes = Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); + let tx_bytes = hex!("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"); let tx: Transaction = deserialize(&tx_bytes).unwrap(); serde_round_trip!(tx); } @@ -1422,7 +1423,7 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_segwit_tx_decode() { - let tx_bytes = Vec::from_hex("010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3603da1b0e00045503bd5704c7dd8a0d0ced13bb5785010800000000000a636b706f6f6c122f4e696e6a61506f6f6c2f5345475749542fffffffff02b4e5a212000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9edf91c46b49eb8a29089980f02ee6b57e7d63d33b18b4fddac2bcd7db2a39837040120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let tx_bytes = hex!("010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3603da1b0e00045503bd5704c7dd8a0d0ced13bb5785010800000000000a636b706f6f6c122f4e696e6a61506f6f6c2f5345475749542fffffffff02b4e5a212000000001976a914876fbb82ec05caa6af7a3b5e5a983aae6c6cc6d688ac0000000000000000266a24aa21a9edf91c46b49eb8a29089980f02ee6b57e7d63d33b18b4fddac2bcd7db2a39837040120000000000000000000000000000000000000000000000000000000000000000000000000"); let tx: Transaction = deserialize(&tx_bytes).unwrap(); assert_eq!(tx.weight(), 780); serde_round_trip!(tx); @@ -1464,27 +1465,25 @@ mod tests { #[test] fn test_huge_witness() { - use crate::hashes::hex::FromHex; - deserialize::(&Vec::from_hex(include_str!("../../tests/data/huge_witness.hex").trim()).unwrap()).unwrap(); + deserialize::(&hex!(include_str!("../../tests/data/huge_witness.hex").trim())).unwrap(); } #[test] #[cfg(feature="bitcoinconsensus")] fn test_transaction_verify () { use std::collections::HashMap; - use crate::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") - .unwrap().as_slice()).unwrap(); - let spent1: Transaction = deserialize(Vec::from_hex("020000000001040aacd2c49f5f3c0968cfa8caf9d5761436d95385252e3abb4de8f5dcf8a582f20000000017160014bcadb2baea98af0d9a902e53a7e9adff43b191e9feffffff96cd3c93cac3db114aafe753122bd7d1afa5aa4155ae04b3256344ecca69d72001000000171600141d9984579ceb5c67ebfbfb47124f056662fe7adbfeffffffc878dd74d3a44072eae6178bb94b9253177db1a5aaa6d068eb0e4db7631762e20000000017160014df2a48cdc53dae1aba7aa71cb1f9de089d75aac3feffffffe49f99275bc8363f5f593f4eec371c51f62c34ff11cc6d8d778787d340d6896c0100000017160014229b3b297a0587e03375ab4174ef56eeb0968735feffffff03360d0f00000000001976a9149f44b06f6ee92ddbc4686f71afe528c09727a5c788ac24281b00000000001976a9140277b4f68ff20307a2a9f9b4487a38b501eb955888ac227c0000000000001976a9148020cd422f55eef8747a9d418f5441030f7c9c7788ac0247304402204aa3bd9682f9a8e101505f6358aacd1749ecf53a62b8370b97d59243b3d6984f02200384ad449870b0e6e89c92505880411285ecd41cf11e7439b973f13bad97e53901210205b392ffcb83124b1c7ce6dd594688198ef600d34500a7f3552d67947bbe392802473044022033dfd8d190a4ae36b9f60999b217c775b96eb10dee3a1ff50fb6a75325719106022005872e4e36d194e49ced2ebcf8bb9d843d842e7b7e0eb042f4028396088d292f012103c9d7cbf369410b090480de2aa15c6c73d91b9ffa7d88b90724614b70be41e98e0247304402207d952de9e59e4684efed069797e3e2d993e9f98ec8a9ccd599de43005fe3f713022076d190cc93d9513fc061b1ba565afac574e02027c9efbfa1d7b71ab8dbb21e0501210313ad44bc030cc6cb111798c2bf3d2139418d751c1e79ec4e837ce360cc03b97a024730440220029e75edb5e9413eb98d684d62a077b17fa5b7cc19349c1e8cc6c4733b7b7452022048d4b9cae594f03741029ff841e35996ef233701c1ea9aa55c301362ea2e2f68012103590657108a72feb8dc1dec022cf6a230bb23dc7aaa52f4032384853b9f8388baf9d20700") - .unwrap().as_slice()).unwrap(); - let spent2: Transaction = deserialize(Vec::from_hex("0200000000010166c3d39490dc827a2594c7b17b7d37445e1f4b372179649cd2ce4475e3641bbb0100000017160014e69aa750e9bff1aca1e32e57328b641b611fc817fdffffff01e87c5d010000000017a914f3890da1b99e44cd3d52f7bcea6a1351658ea7be87024830450221009eb97597953dc288de30060ba02d4e91b2bde1af2ecf679c7f5ab5989549aa8002202a98f8c3bd1a5a31c0d72950dd6e2e3870c6c5819a6c3db740e91ebbbc5ef4800121023f3d3b8e74b807e32217dea2c75c8d0bd46b8665b3a2d9b3cb310959de52a09bc9d20700") - .unwrap().as_slice()).unwrap(); - let spent3: Transaction = deserialize(Vec::from_hex("01000000027a1120a30cef95422638e8dab9dedf720ec614b1b21e451a4957a5969afb869d000000006a47304402200ecc318a829a6cad4aa9db152adbf09b0cd2de36f47b53f5dade3bc7ef086ca702205722cda7404edd6012eedd79b2d6f24c0a0c657df1a442d0a2166614fb164a4701210372f4b97b34e9c408741cd1fc97bcc7ffdda6941213ccfde1cb4075c0f17aab06ffffffffc23b43e5a18e5a66087c0d5e64d58e8e21fcf83ce3f5e4f7ecb902b0e80a7fb6010000006b483045022100f10076a0ea4b4cf8816ed27a1065883efca230933bf2ff81d5db6258691ff75202206b001ef87624e76244377f57f0c84bc5127d0dd3f6e0ef28b276f176badb223a01210309a3a61776afd39de4ed29b622cd399d99ecd942909c36a8696cfd22fc5b5a1affffffff0200127a000000000017a914f895e1dd9b29cb228e9b06a15204e3b57feaf7cc8769311d09000000001976a9144d00da12aaa51849d2583ae64525d4a06cd70fde88ac00000000") - .unwrap().as_slice()).unwrap(); + let mut spending: Transaction = deserialize(hex!("020000000001031cfbc8f54fbfa4a33a30068841371f80dbfe166211242213188428f437445c91000000006a47304402206fbcec8d2d2e740d824d3d36cc345b37d9f65d665a99f5bd5c9e8d42270a03a8022013959632492332200c2908459547bf8dbf97c65ab1a28dec377d6f1d41d3d63e012103d7279dfb90ce17fe139ba60a7c41ddf605b25e1c07a4ddcb9dfef4e7d6710f48feffffff476222484f5e35b3f0e43f65fc76e21d8be7818dd6a989c160b1e5039b7835fc00000000171600140914414d3c94af70ac7e25407b0689e0baa10c77feffffffa83d954a62568bbc99cc644c62eb7383d7c2a2563041a0aeb891a6a4055895570000000017160014795d04cc2d4f31480d9a3710993fbd80d04301dffeffffff06fef72f000000000017a91476fd7035cd26f1a32a5ab979e056713aac25796887a5000f00000000001976a914b8332d502a529571c6af4be66399cd33379071c588ac3fda0500000000001976a914fc1d692f8de10ae33295f090bea5fe49527d975c88ac522e1b00000000001976a914808406b54d1044c429ac54c0e189b0d8061667e088ac6eb68501000000001976a914dfab6085f3a8fb3e6710206a5a959313c5618f4d88acbba20000000000001976a914eb3026552d7e3f3073457d0bee5d4757de48160d88ac0002483045022100bee24b63212939d33d513e767bc79300051f7a0d433c3fcf1e0e3bf03b9eb1d70220588dc45a9ce3a939103b4459ce47500b64e23ab118dfc03c9caa7d6bfc32b9c601210354fd80328da0f9ae6eef2b3a81f74f9a6f66761fadf96f1d1d22b1fd6845876402483045022100e29c7e3a5efc10da6269e5fc20b6a1cb8beb92130cc52c67e46ef40aaa5cac5f0220644dd1b049727d991aece98a105563416e10a5ac4221abac7d16931842d5c322012103960b87412d6e169f30e12106bdf70122aabb9eb61f455518322a18b920a4dfa887d30700") + .as_slice()).unwrap(); + let spent1: Transaction = deserialize(hex!("020000000001040aacd2c49f5f3c0968cfa8caf9d5761436d95385252e3abb4de8f5dcf8a582f20000000017160014bcadb2baea98af0d9a902e53a7e9adff43b191e9feffffff96cd3c93cac3db114aafe753122bd7d1afa5aa4155ae04b3256344ecca69d72001000000171600141d9984579ceb5c67ebfbfb47124f056662fe7adbfeffffffc878dd74d3a44072eae6178bb94b9253177db1a5aaa6d068eb0e4db7631762e20000000017160014df2a48cdc53dae1aba7aa71cb1f9de089d75aac3feffffffe49f99275bc8363f5f593f4eec371c51f62c34ff11cc6d8d778787d340d6896c0100000017160014229b3b297a0587e03375ab4174ef56eeb0968735feffffff03360d0f00000000001976a9149f44b06f6ee92ddbc4686f71afe528c09727a5c788ac24281b00000000001976a9140277b4f68ff20307a2a9f9b4487a38b501eb955888ac227c0000000000001976a9148020cd422f55eef8747a9d418f5441030f7c9c7788ac0247304402204aa3bd9682f9a8e101505f6358aacd1749ecf53a62b8370b97d59243b3d6984f02200384ad449870b0e6e89c92505880411285ecd41cf11e7439b973f13bad97e53901210205b392ffcb83124b1c7ce6dd594688198ef600d34500a7f3552d67947bbe392802473044022033dfd8d190a4ae36b9f60999b217c775b96eb10dee3a1ff50fb6a75325719106022005872e4e36d194e49ced2ebcf8bb9d843d842e7b7e0eb042f4028396088d292f012103c9d7cbf369410b090480de2aa15c6c73d91b9ffa7d88b90724614b70be41e98e0247304402207d952de9e59e4684efed069797e3e2d993e9f98ec8a9ccd599de43005fe3f713022076d190cc93d9513fc061b1ba565afac574e02027c9efbfa1d7b71ab8dbb21e0501210313ad44bc030cc6cb111798c2bf3d2139418d751c1e79ec4e837ce360cc03b97a024730440220029e75edb5e9413eb98d684d62a077b17fa5b7cc19349c1e8cc6c4733b7b7452022048d4b9cae594f03741029ff841e35996ef233701c1ea9aa55c301362ea2e2f68012103590657108a72feb8dc1dec022cf6a230bb23dc7aaa52f4032384853b9f8388baf9d20700") + .as_slice()).unwrap(); + let spent2: Transaction = deserialize(hex!("0200000000010166c3d39490dc827a2594c7b17b7d37445e1f4b372179649cd2ce4475e3641bbb0100000017160014e69aa750e9bff1aca1e32e57328b641b611fc817fdffffff01e87c5d010000000017a914f3890da1b99e44cd3d52f7bcea6a1351658ea7be87024830450221009eb97597953dc288de30060ba02d4e91b2bde1af2ecf679c7f5ab5989549aa8002202a98f8c3bd1a5a31c0d72950dd6e2e3870c6c5819a6c3db740e91ebbbc5ef4800121023f3d3b8e74b807e32217dea2c75c8d0bd46b8665b3a2d9b3cb310959de52a09bc9d20700") + .as_slice()).unwrap(); + let spent3: Transaction = deserialize(hex!("01000000027a1120a30cef95422638e8dab9dedf720ec614b1b21e451a4957a5969afb869d000000006a47304402200ecc318a829a6cad4aa9db152adbf09b0cd2de36f47b53f5dade3bc7ef086ca702205722cda7404edd6012eedd79b2d6f24c0a0c657df1a442d0a2166614fb164a4701210372f4b97b34e9c408741cd1fc97bcc7ffdda6941213ccfde1cb4075c0f17aab06ffffffffc23b43e5a18e5a66087c0d5e64d58e8e21fcf83ce3f5e4f7ecb902b0e80a7fb6010000006b483045022100f10076a0ea4b4cf8816ed27a1065883efca230933bf2ff81d5db6258691ff75202206b001ef87624e76244377f57f0c84bc5127d0dd3f6e0ef28b276f176badb223a01210309a3a61776afd39de4ed29b622cd399d99ecd942909c36a8696cfd22fc5b5a1affffffff0200127a000000000017a914f895e1dd9b29cb228e9b06a15204e3b57feaf7cc8769311d09000000001976a9144d00da12aaa51849d2583ae64525d4a06cd70fde88ac00000000") + .as_slice()).unwrap(); let mut spent = HashMap::new(); spent.insert(spent1.txid(), spent1); @@ -1617,14 +1616,14 @@ mod benches { use super::Transaction; use crate::EmptyWrite; use crate::consensus::{deserialize, Encodable}; - use crate::hashes::hex::FromHex; + use crate::internal_macros::hex; use test::{black_box, Bencher}; const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"; #[bench] pub fn bench_transaction_size(bh: &mut Bencher) { - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex!(SOME_TX); let mut tx: Transaction = deserialize(&raw_tx).unwrap(); @@ -1635,7 +1634,7 @@ mod benches { #[bench] pub fn bench_transaction_serialize(bh: &mut Bencher) { - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex!(SOME_TX); let tx: Transaction = deserialize(&raw_tx).unwrap(); let mut data = Vec::with_capacity(raw_tx.len()); @@ -1649,7 +1648,7 @@ mod benches { #[bench] pub fn bench_transaction_serialize_logic(bh: &mut Bencher) { - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex!(SOME_TX); let tx: Transaction = deserialize(&raw_tx).unwrap(); bh.iter(|| { @@ -1660,7 +1659,7 @@ mod benches { #[bench] pub fn bench_transaction_deserialize(bh: &mut Bencher) { - let raw_tx = Vec::from_hex(SOME_TX).unwrap(); + let raw_tx = hex!(SOME_TX); bh.iter(|| { let tx: Transaction = deserialize(&raw_tx).unwrap(); diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs index cc99e242..afe1f1d6 100644 --- a/bitcoin/src/blockdata/witness.rs +++ b/bitcoin/src/blockdata/witness.rs @@ -491,7 +491,8 @@ mod test { use super::*; use crate::consensus::{deserialize, serialize}; - use crate::hashes::hex::{FromHex, ToHex}; + use crate::hashes::hex::ToHex; + use crate::internal_macros::hex; use crate::Transaction; use crate::secp256k1::ecdsa; @@ -577,22 +578,21 @@ mod test { fn test_push_ecdsa_sig() { // The very first signature in block 734,958 let sig_bytes = - Vec::from_hex("304402207c800d698f4b0298c5aac830b822f011bb02df41eb114ade9a6702f364d5e39c0220366900d2a60cab903e77ef7dd415d46509b1f78ac78906e3296f495aa1b1b541"); - let sig = ecdsa::Signature::from_der(&sig_bytes.unwrap()).unwrap(); + hex!("304402207c800d698f4b0298c5aac830b822f011bb02df41eb114ade9a6702f364d5e39c0220366900d2a60cab903e77ef7dd415d46509b1f78ac78906e3296f495aa1b1b541"); + let sig = ecdsa::Signature::from_der(&sig_bytes).unwrap(); let mut witness = Witness::default(); witness.push_bitcoin_signature(&sig.serialize_der(), EcdsaSighashType::All); - let expected_witness = vec![Vec::from_hex( + let expected_witness = vec![hex!( "304402207c800d698f4b0298c5aac830b822f011bb02df41eb114ade9a6702f364d5e39c0220366900d2a60cab903e77ef7dd415d46509b1f78ac78906e3296f495aa1b1b54101") - .unwrap()]; + ]; assert_eq!(witness.to_vec(), expected_witness); } #[test] fn test_witness() { let w0 = - Vec::from_hex("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105") - .unwrap(); - let w1 = Vec::from_hex("000000").unwrap(); + hex!("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105"); + let w1 = hex!("000000"); let witness_vec = vec![w0.clone(), w1.clone()]; let witness_serialized: Vec = serialize(&witness_vec); let witness = Witness { @@ -619,10 +619,10 @@ mod test { #[test] fn test_get_tapscript() { - let tapscript = Vec::from_hex("deadbeef").unwrap(); - let control_block = Vec::from_hex("02").unwrap(); + let tapscript = hex!("deadbeef"); + let control_block = hex!("02"); // annex starting with 0x50 causes the branching logic. - let annex = Vec::from_hex("50").unwrap(); + let annex = hex!("50"); let witness_vec = vec![tapscript.clone(), control_block.clone()]; let witness_vec_annex = vec![tapscript.clone(), control_block, annex]; @@ -648,8 +648,8 @@ mod test { #[test] fn test_tx() { - let s = "02000000000102b44f26b275b8ad7b81146ba3dbecd081f9c1ea0dc05b97516f56045cfcd3df030100000000ffffffff1cb4749ae827c0b75f3d0a31e63efc8c71b47b5e3634a4c698cd53661cab09170100000000ffffffff020b3a0500000000001976a9143ea74de92762212c96f4dd66c4d72a4deb20b75788ac630500000000000016001493a8dfd1f0b6a600ab01df52b138cda0b82bb7080248304502210084622878c94f4c356ce49c8e33a063ec90f6ee9c0208540888cfab056cd1fca9022014e8dbfdfa46d318c6887afd92dcfa54510e057565e091d64d2ee3a66488f82c0121026e181ffb98ebfe5a64c983073398ea4bcd1548e7b971b4c175346a25a1c12e950247304402203ef00489a0d549114977df2820fab02df75bebb374f5eee9e615107121658cfa02204751f2d1784f8e841bff6d3bcf2396af2f1a5537c0e4397224873fbd3bfbe9cf012102ae6aa498ce2dd204e9180e71b4fb1260fe3d1a95c8025b34e56a9adf5f278af200000000"; - let tx_bytes = Vec::from_hex(s).unwrap(); + const S: &str = "02000000000102b44f26b275b8ad7b81146ba3dbecd081f9c1ea0dc05b97516f56045cfcd3df030100000000ffffffff1cb4749ae827c0b75f3d0a31e63efc8c71b47b5e3634a4c698cd53661cab09170100000000ffffffff020b3a0500000000001976a9143ea74de92762212c96f4dd66c4d72a4deb20b75788ac630500000000000016001493a8dfd1f0b6a600ab01df52b138cda0b82bb7080248304502210084622878c94f4c356ce49c8e33a063ec90f6ee9c0208540888cfab056cd1fca9022014e8dbfdfa46d318c6887afd92dcfa54510e057565e091d64d2ee3a66488f82c0121026e181ffb98ebfe5a64c983073398ea4bcd1548e7b971b4c175346a25a1c12e950247304402203ef00489a0d549114977df2820fab02df75bebb374f5eee9e615107121658cfa02204751f2d1784f8e841bff6d3bcf2396af2f1a5537c0e4397224873fbd3bfbe9cf012102ae6aa498ce2dd204e9180e71b4fb1260fe3d1a95c8025b34e56a9adf5f278af200000000"; + let tx_bytes = hex!(S); let tx: Transaction = deserialize(&tx_bytes).unwrap(); let expected_wit = ["304502210084622878c94f4c356ce49c8e33a063ec90f6ee9c0208540888cfab056cd1fca9022014e8dbfdfa46d318c6887afd92dcfa54510e057565e091d64d2ee3a66488f82c01", "026e181ffb98ebfe5a64c983073398ea4bcd1548e7b971b4c175346a25a1c12e95"]; @@ -670,12 +670,10 @@ mod test { #[test] fn fuzz_cases() { - let s = "26ff0000000000c94ce592cf7a4cbb68eb00ce374300000057cd0000000000000026"; - let bytes = Vec::from_hex(s).unwrap(); + let bytes = hex!("26ff0000000000c94ce592cf7a4cbb68eb00ce374300000057cd0000000000000026"); assert!(deserialize::(&bytes).is_err()); // OversizedVectorAllocation - let s = "24000000ffffffffffffffffffffffff"; - let bytes = Vec::from_hex(s).unwrap(); + let bytes = hex!("24000000ffffffffffffffffffffffff"); assert!(deserialize::(&bytes).is_err()); // OversizedVectorAllocation } diff --git a/bitcoin/src/internal_macros.rs b/bitcoin/src/internal_macros.rs index ffa5f7a0..2ae036e7 100644 --- a/bitcoin/src/internal_macros.rs +++ b/bitcoin/src/internal_macros.rs @@ -74,7 +74,7 @@ mod test_macros { } } - macro_rules! hex (($hex:literal) => (Vec::from_hex($hex).unwrap())); + macro_rules! hex (($hex:expr) => ( as hashes::hex::FromHex>::from_hex($hex).unwrap())); pub(crate) use hex; macro_rules! hex_into { diff --git a/bitcoin/src/merkle_tree/block.rs b/bitcoin/src/merkle_tree/block.rs index 685ef962..f7d9067f 100644 --- a/bitcoin/src/merkle_tree/block.rs +++ b/bitcoin/src/merkle_tree/block.rs @@ -509,6 +509,7 @@ mod tests { use crate::hashes::hex::{FromHex, ToHex}; #[cfg(feature = "rand-std")] use crate::hashes::Hash; + use crate::internal_macros::hex; use crate::{Block, Txid}; /// accepts `pmt_test_$num` @@ -640,7 +641,7 @@ mod tests { fn merkleblock_serialization() { // Got it by running the rpc call // `gettxoutproof '["220ebc64e21abece964927322cba69180ed853bb187fbc6923bac7d010b9d87a"]'` - let mb_hex = + const MB_HEX: &str = "0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c\ 9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf930900000005fac\ 7708a6e81b2a986dea60db2663840ed141130848162eb1bd1dee54f309a1b2ee1e12587e497ada70d9bd10d\ @@ -648,14 +649,14 @@ mod tests { ebe1ae264bc0e2289189ff0316cdc10511da71da757e553cada9f3b5b1434f3923673adb57d83caac392c38\ af156d6fc30b55fad4112df2b95531e68114e9ad10011e72f7b7cfdb025700"; - let mb: MerkleBlock = deserialize(&Vec::from_hex(mb_hex).unwrap()).unwrap(); + let mb: MerkleBlock = deserialize(&hex!(MB_HEX)).unwrap(); assert_eq!(get_block_13b8a().block_hash(), mb.header.block_hash()); assert_eq!( mb.header.merkle_root, mb.txn.extract_matches(&mut vec![], &mut vec![]).unwrap() ); // Serialize again and check that it matches the original bytes - assert_eq!(mb_hex, serialize(&mb).to_hex().as_str()); + assert_eq!(MB_HEX, serialize(&mb).to_hex().as_str()); } /// Create a CMerkleBlock using a list of txids which will be found in the @@ -737,7 +738,7 @@ mod tests { /// Returns a real block (0000000000013b8ab2cd513b0261a14096412195a72a0c4827d229dcc7e0f7af) /// with 9 txs. fn get_block_13b8a() -> Block { - let block_hex = + const BLOCK_HEX: &str = "0100000090f0a9f110702f808219ebea1173056042a714bad51b916cb6800000000000005275289558f51c\ 9966699404ae2294730c3c9f9bda53523ce50e9b95e558da2fdb261b4d4c86041b1ab1bf930901000000010\ 000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0146\ @@ -809,6 +810,6 @@ mod tests { 058b800a098fc1740ce3012e8fc8a00c96af966ffffffff02c0e1e400000000001976a9144134e75a6fcb60\ 42034aab5e18570cf1f844f54788ac404b4c00000000001976a9142b6ba7c9d796b75eef7942fc9288edd37\ c32f5c388ac00000000"; - deserialize(&Vec::from_hex(block_hex).unwrap()).unwrap() + deserialize(&hex!(BLOCK_HEX)).unwrap() } } diff --git a/bitcoin/src/network/address.rs b/bitcoin/src/network/address.rs index 2015970d..e87d0a5f 100644 --- a/bitcoin/src/network/address.rs +++ b/bitcoin/src/network/address.rs @@ -310,6 +310,7 @@ mod test { use super::{AddrV2, AddrV2Message, Address}; use crate::consensus::encode::{deserialize, serialize}; use crate::hashes::hex::FromHex; + use crate::internal_macros::hex; use crate::network::constants::ServiceFlags; #[test] @@ -401,14 +402,14 @@ mod test { // Taken from https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/test/net_tests.cpp#L348 let ip = AddrV2::Ipv4(Ipv4Addr::new(1, 2, 3, 4)); - assert_eq!(serialize(&ip), Vec::from_hex("010401020304").unwrap()); + assert_eq!(serialize(&ip), hex!("010401020304")); let ip = AddrV2::Ipv6(Ipv6Addr::from_str("1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b").unwrap()); - assert_eq!(serialize(&ip), Vec::from_hex("02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b").unwrap()); + assert_eq!(serialize(&ip), hex!("02101a1b2a2b3a3b4a4b5a5b6a6b7a7b8a8b")); let ip = AddrV2::TorV2(FromHex::from_hex("f1f2f3f4f5f6f7f8f9fa").unwrap()); - assert_eq!(serialize(&ip), Vec::from_hex("030af1f2f3f4f5f6f7f8f9fa").unwrap()); + assert_eq!(serialize(&ip), hex!("030af1f2f3f4f5f6f7f8f9fa")); let ip = AddrV2::TorV3( FromHex::from_hex("53cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88") @@ -416,8 +417,7 @@ mod test { ); assert_eq!( serialize(&ip), - Vec::from_hex("042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88") - .unwrap() + hex!("042053cd5648488c4707914182655b7664034e09e66f7e8cbf1084e654eb56c5bd88") ); let ip = AddrV2::I2p( @@ -426,15 +426,14 @@ mod test { ); assert_eq!( serialize(&ip), - Vec::from_hex("0520a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87") - .unwrap() + hex!("0520a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87") ); let ip = AddrV2::Cjdns(Ipv6Addr::from_str("fc00:1:2:3:4:5:6:7").unwrap()); - assert_eq!(serialize(&ip), Vec::from_hex("0610fc000001000200030004000500060007").unwrap()); + assert_eq!(serialize(&ip), hex!("0610fc000001000200030004000500060007")); - let ip = AddrV2::Unknown(170, Vec::from_hex("01020304").unwrap()); - assert_eq!(serialize(&ip), Vec::from_hex("aa0401020304").unwrap()); + let ip = AddrV2::Unknown(170, hex!("01020304")); + assert_eq!(serialize(&ip), hex!("aa0401020304")); } #[test] @@ -442,52 +441,52 @@ mod test { // Taken from https://github.com/bitcoin/bitcoin/blob/12a1c3ad1a43634d2a98717e49e3f02c4acea2fe/src/test/net_tests.cpp#L386 // Valid IPv4. - let ip: AddrV2 = deserialize(&Vec::from_hex("010401020304").unwrap()).unwrap(); + let ip: AddrV2 = deserialize(&hex!("010401020304")).unwrap(); assert_eq!(ip, AddrV2::Ipv4(Ipv4Addr::new(1, 2, 3, 4))); // Invalid IPv4, valid length but address itself is shorter. - deserialize::(&Vec::from_hex("01040102").unwrap()).unwrap_err(); + deserialize::(&hex!("01040102")).unwrap_err(); // Invalid IPv4, with bogus length. - assert!(deserialize::(&Vec::from_hex("010501020304").unwrap()).is_err()); + assert!(deserialize::(&hex!("010501020304")).is_err()); // Invalid IPv4, with extreme length. - assert!(deserialize::(&Vec::from_hex("01fd010201020304").unwrap()).is_err()); + assert!(deserialize::(&hex!("01fd010201020304")).is_err()); // Valid IPv6. let ip: AddrV2 = - deserialize(&Vec::from_hex("02100102030405060708090a0b0c0d0e0f10").unwrap()).unwrap(); + deserialize(&hex!("02100102030405060708090a0b0c0d0e0f10")).unwrap(); assert_eq!( ip, AddrV2::Ipv6(Ipv6Addr::from_str("102:304:506:708:90a:b0c:d0e:f10").unwrap()) ); // Invalid IPv6, with bogus length. - assert!(deserialize::(&Vec::from_hex("020400").unwrap()).is_err()); + assert!(deserialize::(&hex!("020400")).is_err()); // Invalid IPv6, contains embedded IPv4. assert!(deserialize::( - &Vec::from_hex("021000000000000000000000ffff01020304").unwrap() + &hex!("021000000000000000000000ffff01020304") ) .is_err()); // Invalid IPv6, contains embedded TORv2. assert!(deserialize::( - &Vec::from_hex("0210fd87d87eeb430102030405060708090a").unwrap() + &hex!("0210fd87d87eeb430102030405060708090a") ) .is_err()); // Valid TORv2. - let ip: AddrV2 = deserialize(&Vec::from_hex("030af1f2f3f4f5f6f7f8f9fa").unwrap()).unwrap(); + let ip: AddrV2 = deserialize(&hex!("030af1f2f3f4f5f6f7f8f9fa")).unwrap(); assert_eq!(ip, AddrV2::TorV2(FromHex::from_hex("f1f2f3f4f5f6f7f8f9fa").unwrap())); // Invalid TORv2, with bogus length. - assert!(deserialize::(&Vec::from_hex("030700").unwrap()).is_err()); + assert!(deserialize::(&hex!("030700")).is_err()); // Valid TORv3. let ip: AddrV2 = deserialize( - &Vec::from_hex("042079bcc625184b05194975c28b66b66b0469f7f6556fb1ac3189a79b40dda32f1f") - .unwrap(), + &hex!("042079bcc625184b05194975c28b66b66b0469f7f6556fb1ac3189a79b40dda32f1f") + , ) .unwrap(); assert_eq!( @@ -501,12 +500,12 @@ mod test { ); // Invalid TORv3, with bogus length. - assert!(deserialize::(&Vec::from_hex("040000").unwrap()).is_err()); + assert!(deserialize::(&hex!("040000")).is_err()); // Valid I2P. let ip: AddrV2 = deserialize( - &Vec::from_hex("0520a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87") - .unwrap(), + &hex!("0520a2894dabaec08c0051a481a6dac88b64f98232ae42d4b6fd2fa81952dfe36a87") + , ) .unwrap(); assert_eq!( @@ -520,39 +519,39 @@ mod test { ); // Invalid I2P, with bogus length. - assert!(deserialize::(&Vec::from_hex("050300").unwrap()).is_err()); + assert!(deserialize::(&hex!("050300")).is_err()); // Valid CJDNS. let ip: AddrV2 = - deserialize(&Vec::from_hex("0610fc000001000200030004000500060007").unwrap()).unwrap(); + deserialize(&hex!("0610fc000001000200030004000500060007")).unwrap(); assert_eq!(ip, AddrV2::Cjdns(Ipv6Addr::from_str("fc00:1:2:3:4:5:6:7").unwrap())); // Invalid CJDNS, incorrect marker assert!(deserialize::( - &Vec::from_hex("0610fd000001000200030004000500060007").unwrap() + &hex!("0610fd000001000200030004000500060007") ) .is_err()); // Invalid CJDNS, with bogus length. - assert!(deserialize::(&Vec::from_hex("060100").unwrap()).is_err()); + assert!(deserialize::(&hex!("060100")).is_err()); // Unknown, with extreme length. assert!( - deserialize::(&Vec::from_hex("aafe0000000201020304050607").unwrap()).is_err() + deserialize::(&hex!("aafe0000000201020304050607")).is_err() ); // Unknown, with reasonable length. - let ip: AddrV2 = deserialize(&Vec::from_hex("aa0401020304").unwrap()).unwrap(); - assert_eq!(ip, AddrV2::Unknown(170, Vec::from_hex("01020304").unwrap())); + let ip: AddrV2 = deserialize(&hex!("aa0401020304")).unwrap(); + assert_eq!(ip, AddrV2::Unknown(170, hex!("01020304"))); // Unknown, with zero length. - let ip: AddrV2 = deserialize(&Vec::from_hex("aa00").unwrap()).unwrap(); + let ip: AddrV2 = deserialize(&hex!("aa00")).unwrap(); assert_eq!(ip, AddrV2::Unknown(170, vec![])); } #[test] fn addrv2message_test() { - let raw = Vec::from_hex("0261bc6649019902abab208d79627683fd4804010409090909208d").unwrap(); + let raw = hex!("0261bc6649019902abab208d79627683fd4804010409090909208d"); let addresses: Vec = deserialize(&raw).unwrap(); assert_eq!( @@ -562,7 +561,7 @@ mod test { services: ServiceFlags::NETWORK, time: 0x4966bc61, port: 8333, - addr: AddrV2::Unknown(153, Vec::from_hex("abab").unwrap()) + addr: AddrV2::Unknown(153, hex!("abab")) }, AddrV2Message { services: ServiceFlags::NETWORK_LIMITED diff --git a/bitcoin/src/network/message.rs b/bitcoin/src/network/message.rs index 6426f529..2ef20d1d 100644 --- a/bitcoin/src/network/message.rs +++ b/bitcoin/src/network/message.rs @@ -519,9 +519,9 @@ mod test { use crate::blockdata::script::ScriptBuf; use crate::blockdata::transaction::Transaction; use crate::consensus::encode::{deserialize, deserialize_partial, serialize}; - use crate::hashes::hex::FromHex; use crate::hashes::sha256d::Hash; use crate::hashes::Hash as HashTrait; + use crate::internal_macros::hex; use crate::network::address::{AddrV2, AddrV2Message, Address}; use crate::network::constants::{Magic, Network, ServiceFlags}; use crate::network::message_blockdata::{GetBlocksMessage, GetHeadersMessage, Inventory}; @@ -536,17 +536,17 @@ mod test { #[test] fn full_round_ser_der_raw_network_message_test() { // TODO: Impl Rand traits here to easily generate random values. - let version_msg: VersionMessage = deserialize(&Vec::from_hex("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001").unwrap()).unwrap(); - let tx: Transaction = deserialize(&Vec::from_hex("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap()).unwrap(); + let version_msg: VersionMessage = deserialize(&hex!("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001")).unwrap(); + let tx: Transaction = deserialize(&hex!("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000")).unwrap(); let block: Block = deserialize(&include_bytes!("../../tests/data/testnet_block_000000000000045e0b1660b6445b5e5c5ab63c9a4f956be7e1e69be04fa4497b.raw")[..]).unwrap(); - let header: block::Header = deserialize(&Vec::from_hex("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b").unwrap()).unwrap(); + let header: block::Header = deserialize(&hex!("010000004ddccd549d28f385ab457e98d1b11ce80bfea2c5ab93015ade4973e400000000bf4473e53794beae34e64fccc471dace6ae544180816f89591894e0f417a914cd74d6e49ffff001d323b3a7b")).unwrap(); let script: ScriptBuf = deserialize( - &Vec::from_hex("1976a91431a420903c05a0a7de2de40c9f02ebedbacdc17288ac").unwrap(), + &hex!("1976a91431a420903c05a0a7de2de40c9f02ebedbacdc17288ac"), ) .unwrap(); - let merkle_block: MerkleBlock = deserialize(&Vec::from_hex("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101").unwrap()).unwrap(); - let cmptblock = deserialize(&Vec::from_hex("00000030d923ad36ff2d955abab07f8a0a6e813bc6e066b973e780c5e36674cad5d1cd1f6e265f2a17a0d35cbe701fe9d06e2c6324cfe135f6233e8b767bfa3fb4479b71115dc562ffff7f2006000000000000000000000000010002000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0302ee00ffffffff0100f9029500000000015100000000").unwrap()).unwrap(); - let blocktxn = deserialize(&Vec::from_hex("2e93c0cff39ff605020072d96bc3a8d20b8447e294d08092351c8583e08d9b5a01020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0402dc0000ffffffff0200f90295000000001976a9142b4569203694fc997e13f2c0a1383b9e16c77a0d88ac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap()).unwrap(); + let merkle_block: MerkleBlock = deserialize(&hex!("0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f19630101")).unwrap(); + let cmptblock = deserialize(&hex!("00000030d923ad36ff2d955abab07f8a0a6e813bc6e066b973e780c5e36674cad5d1cd1f6e265f2a17a0d35cbe701fe9d06e2c6324cfe135f6233e8b767bfa3fb4479b71115dc562ffff7f2006000000000000000000000000010002000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0302ee00ffffffff0100f9029500000000015100000000")).unwrap(); + let blocktxn = deserialize(&hex!("2e93c0cff39ff605020072d96bc3a8d20b8447e294d08092351c8583e08d9b5a01020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff0402dc0000ffffffff0200f90295000000001976a9142b4569203694fc997e13f2c0a1383b9e16c77a0d88ac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000")).unwrap(); let msgs = vec![ NetworkMessage::Version(version_msg), @@ -576,7 +576,7 @@ mod test { NetworkMessage::Pong(23), NetworkMessage::MerkleBlock(merkle_block), NetworkMessage::FilterLoad(FilterLoad { - filter: Vec::from_hex("03614e9b050000000000000001").unwrap(), + filter: hex!("03614e9b050000000000000001"), hash_funcs: 1, tweak: 2, flags: BloomFlags::All, diff --git a/bitcoin/src/network/message_blockdata.rs b/bitcoin/src/network/message_blockdata.rs index 8521616d..d15762ef 100644 --- a/bitcoin/src/network/message_blockdata.rs +++ b/bitcoin/src/network/message_blockdata.rs @@ -129,15 +129,14 @@ impl_consensus_encoding!(GetHeadersMessage, version, locator_hashes, stop_hash); mod tests { use super::{GetBlocksMessage, GetHeadersMessage, Vec}; use crate::consensus::encode::{deserialize, serialize}; - use crate::hashes::hex::FromHex; use crate::hashes::Hash; + use crate::internal_macros::hex; #[test] fn getblocks_message_test() { - let from_sat = Vec::from_hex("72110100014a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b0000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let from_sat = hex!("72110100014a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b0000000000000000000000000000000000000000000000000000000000000000"); let genhash = - Vec::from_hex("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b") - .unwrap(); + hex!("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); let decode: Result = deserialize(&from_sat); assert!(decode.is_ok()); @@ -152,10 +151,9 @@ mod tests { #[test] fn getheaders_message_test() { - let from_sat = Vec::from_hex("72110100014a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b0000000000000000000000000000000000000000000000000000000000000000").unwrap(); + let from_sat = hex!("72110100014a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b0000000000000000000000000000000000000000000000000000000000000000"); let genhash = - Vec::from_hex("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b") - .unwrap(); + hex!("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"); let decode: Result = deserialize(&from_sat); assert!(decode.is_ok()); diff --git a/bitcoin/src/network/message_network.rs b/bitcoin/src/network/message_network.rs index b8ac228e..904dc1de 100644 --- a/bitcoin/src/network/message_network.rs +++ b/bitcoin/src/network/message_network.rs @@ -145,12 +145,13 @@ mod tests { use crate::consensus::encode::{deserialize, serialize}; use crate::hashes::hex::FromHex; use crate::hashes::sha256d::Hash; + use crate::internal_macros::hex; use crate::network::constants::ServiceFlags; #[test] fn version_message_test() { // This message is from my satoshi node, morning of May 27 2014 - let from_sat = Vec::from_hex("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001").unwrap(); + let from_sat = hex!("721101000100000000000000e6e0845300000000010000000000000000000000000000000000ffff0000000000000100000000000000fd87d87eeb4364f22cf54dca59412db7208d47d920cffce83ee8102f5361746f7368693a302e392e39392f2c9f040001"); let decode: Result = deserialize(&from_sat); assert!(decode.is_ok()); @@ -169,8 +170,8 @@ mod tests { #[test] fn reject_message_test() { - let reject_tx_conflict = Vec::from_hex("027478121474786e2d6d656d706f6f6c2d636f6e666c69637405df54d3860b3c41806a3546ab48279300affacf4b88591b229141dcf2f47004").unwrap(); - let reject_tx_nonfinal = Vec::from_hex("02747840096e6f6e2d66696e616c259bbe6c83db8bbdfca7ca303b19413dc245d9f2371b344ede5f8b1339a5460b").unwrap(); + let reject_tx_conflict = hex!("027478121474786e2d6d656d706f6f6c2d636f6e666c69637405df54d3860b3c41806a3546ab48279300affacf4b88591b229141dcf2f47004"); + let reject_tx_nonfinal = hex!("02747840096e6f6e2d66696e616c259bbe6c83db8bbdfca7ca303b19413dc245d9f2371b344ede5f8b1339a5460b"); let decode_result_conflict: Result = deserialize(&reject_tx_conflict); let decode_result_nonfinal: Result = deserialize(&reject_tx_nonfinal); diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index 0ea02a92..ba85ef4e 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -897,7 +897,7 @@ mod tests { use crate::bip32::{ChildNumber, ExtendedPrivKey, ExtendedPubKey, KeySource}; use crate::psbt::map::{Output, Input}; use crate::psbt::raw; - use crate::internal_macros::hex_script; + use crate::internal_macros::{hex, hex_script}; use std::collections::BTreeMap; use crate::blockdata::witness::Witness; @@ -935,7 +935,7 @@ mod tests { #[test] fn serialize_then_deserialize_output() { let secp = &Secp256k1::new(); - let seed = Vec::from_hex("000102030405060708090a0b0c0d0e0f").unwrap(); + let seed = hex!("000102030405060708090a0b0c0d0e0f"); let mut hd_keypaths: BTreeMap = Default::default(); @@ -1058,7 +1058,7 @@ mod tests { }, script_sig: hex_script!("160014be18d152a9b012039daf3da7de4f53349eecb985"), sequence: Sequence::MAX, - witness: Witness::from_slice(&[Vec::from_hex("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105").unwrap()]), + witness: Witness::from_slice(&[hex!("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105")]), }], output: vec![ TxOut { @@ -1154,7 +1154,7 @@ mod tests { use crate::sighash::EcdsaSighashType; use std::collections::BTreeMap; use crate::blockdata::witness::Witness; - use crate::internal_macros::hex_script; + use crate::internal_macros::{hex, hex_script}; #[test] #[should_panic(expected = "InvalidMagic")] @@ -1279,8 +1279,8 @@ mod tests { script_sig: hex_script!("160014be18d152a9b012039daf3da7de4f53349eecb985"), sequence: Sequence::MAX, witness: Witness::from_slice(&[ - Vec::from_hex("304402202712be22e0270f394f568311dc7ca9a68970b8025fdd3b240229f07f8a5f3a240220018b38d7dcd314e734c9276bd6fb40f673325bc4baa144c800d2f2f02db2765c01").unwrap(), - Vec::from_hex("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105").unwrap(), + hex!("304402202712be22e0270f394f568311dc7ca9a68970b8025fdd3b240229f07f8a5f3a240220018b38d7dcd314e734c9276bd6fb40f673325bc4baa144c800d2f2f02db2765c01"), + hex!("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105"), ]), }, TxIn { @@ -1293,8 +1293,8 @@ mod tests { script_sig: hex_script!("160014fe3e9ef1a745e974d902c4355943abcb34bd5353"), sequence: Sequence::MAX, witness: Witness::from_slice(&[ - Vec::from_hex("3045022100d12b852d85dcd961d2f5f4ab660654df6eedcc794c0c33ce5cc309ffb5fce58d022067338a8e0e1725c197fb1a88af59f51e44e4255b20167c8684031c05d1f2592a01").unwrap(), - Vec::from_hex("0223b72beef0965d10be0778efecd61fcac6f79a4ea169393380734464f84f2ab3").unwrap(), + hex!("3045022100d12b852d85dcd961d2f5f4ab660654df6eedcc794c0c33ce5cc309ffb5fce58d022067338a8e0e1725c197fb1a88af59f51e44e4255b20167c8684031c05d1f2592a01"), + hex!("0223b72beef0965d10be0778efecd61fcac6f79a4ea169393380734464f84f2ab3"), ]), }], output: vec![ @@ -1440,9 +1440,9 @@ mod tests { let mut unknown: BTreeMap> = BTreeMap::new(); let key: raw::Key = raw::Key { type_value: 0x0fu8, - key: Vec::from_hex("010203040506070809").unwrap(), + key: hex!("010203040506070809"), }; - let value: Vec = Vec::from_hex("0102030405060708090a0b0c0d0e0f").unwrap(); + let value: Vec = hex!("0102030405060708090a0b0c0d0e0f"); unknown.insert(key, value); @@ -1591,8 +1591,8 @@ mod tests { script_sig: hex_script!("160014be18d152a9b012039daf3da7de4f53349eecb985"), sequence: Sequence::MAX, witness: Witness::from_slice(&[ - Vec::from_hex("304402202712be22e0270f394f568311dc7ca9a68970b8025fdd3b240229f07f8a5f3a240220018b38d7dcd314e734c9276bd6fb40f673325bc4baa144c800d2f2f02db2765c01").unwrap(), - Vec::from_hex("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105").unwrap(), + hex!("304402202712be22e0270f394f568311dc7ca9a68970b8025fdd3b240229f07f8a5f3a240220018b38d7dcd314e734c9276bd6fb40f673325bc4baa144c800d2f2f02db2765c01"), + hex!("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105"), ]), }, TxIn { @@ -1605,8 +1605,8 @@ mod tests { script_sig: hex_script!("160014fe3e9ef1a745e974d902c4355943abcb34bd5353"), sequence: Sequence::MAX, witness: Witness::from_slice(&[ - Vec::from_hex("3045022100d12b852d85dcd961d2f5f4ab660654df6eedcc794c0c33ce5cc309ffb5fce58d022067338a8e0e1725c197fb1a88af59f51e44e4255b20167c8684031c05d1f2592a01").unwrap(), - Vec::from_hex("0223b72beef0965d10be0778efecd61fcac6f79a4ea169393380734464f84f2ab3").unwrap(), + hex!("3045022100d12b852d85dcd961d2f5f4ab660654df6eedcc794c0c33ce5cc309ffb5fce58d022067338a8e0e1725c197fb1a88af59f51e44e4255b20167c8684031c05d1f2592a01"), + hex!("0223b72beef0965d10be0778efecd61fcac6f79a4ea169393380734464f84f2ab3"), ]), }], output: vec![ diff --git a/bitcoin/src/sighash.rs b/bitcoin/src/sighash.rs index 6433e6b8..a201a7ee 100644 --- a/bitcoin/src/sighash.rs +++ b/bitcoin/src/sighash.rs @@ -1056,7 +1056,7 @@ mod tests { use crate::hash_types::Sighash; use crate::hashes::hex::FromHex; use crate::hashes::{Hash, HashEngine}; - use crate::internal_macros::{hex_from_slice, hex_script}; + use crate::internal_macros::{hex, hex_from_slice, hex_script}; use crate::network::constants::Network; use crate::taproot::{TapLeafHash, TapSighashHash}; @@ -1126,10 +1126,9 @@ mod tests { #[test] fn test_tap_sighash_hash() { - let bytes = Vec::from_hex("00011b96877db45ffa23b307e9f0ac87b80ef9a80b4c5f0db3fbe734422453e83cc5576f3d542c5d4898fb2b696c15d43332534a7c1d1255fda38993545882df92c3e353ff6d36fbfadc4d168452afd8467f02fe53d71714fcea5dfe2ea759bd00185c4cb02bc76d42620393ca358a1a713f4997f9fc222911890afb3fe56c6a19b202df7bffdcfad08003821294279043746631b00e2dc5e52a111e213bbfe6ef09a19428d418dab0d50000000000").unwrap(); + let bytes = hex!("00011b96877db45ffa23b307e9f0ac87b80ef9a80b4c5f0db3fbe734422453e83cc5576f3d542c5d4898fb2b696c15d43332534a7c1d1255fda38993545882df92c3e353ff6d36fbfadc4d168452afd8467f02fe53d71714fcea5dfe2ea759bd00185c4cb02bc76d42620393ca358a1a713f4997f9fc222911890afb3fe56c6a19b202df7bffdcfad08003821294279043746631b00e2dc5e52a111e213bbfe6ef09a19428d418dab0d50000000000"); let expected = - Vec::from_hex("04e808aad07a40b3767a1442fead79af6ef7e7c9316d82dec409bb31e77699b0") - .unwrap(); + hex!("04e808aad07a40b3767a1442fead79af6ef7e7c9316d82dec409bb31e77699b0"); let mut enc = TapSighashHash::engine(); enc.input(&bytes); let hash = TapSighashHash::from_engine(enc); @@ -1607,12 +1606,12 @@ mod tests { #[test] fn bip143_p2wpkh() { let tx = deserialize::( - &Vec::from_hex( + &hex!( "0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f000000\ 0000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a01000000\ 00ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093\ - 510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000", - ).unwrap()[..], + 510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000" + ), ).unwrap(); let witness_script = @@ -1646,11 +1645,11 @@ mod tests { #[test] fn bip143_p2wpkh_nested_in_p2sh() { let tx = deserialize::( - &Vec::from_hex( + &hex!( "0100000001db6b1b20aa0fd7b23880be2ecbd4a98130974cf4748fb66092ac4d3ceb1a5477010000\ 0000feffffff02b8b4eb0b000000001976a914a457b684d7f0d539a46a45bbc043f35b59d0d96388ac00\ - 08af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000", - ).unwrap()[..], + 08af2f000000001976a914fd270b1ee6abcaea97fea7ad0402e8bd8ad6d77c88ac92040000" + ), ).unwrap(); let witness_script = @@ -1684,10 +1683,10 @@ mod tests { #[test] fn bip143_p2wsh_nested_in_p2sh() { let tx = deserialize::( - &Vec::from_hex( + &hex!( "010000000136641869ca081e70f394c6948e8af409e18b619df2ed74aa106c1ca29787b96e0100000000\ ffffffff0200e9a435000000001976a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688acc0832f\ - 05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000").unwrap()[..], + 05000000001976a9147480a33f950689af511e6e84c138dbbd3c3ee41588ac00000000"), ).unwrap(); let witness_script = hex_script!(