From a6d7d542abb13ffd8d8df9bc2586620de6448115 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 28 Sep 2023 10:34:42 +1000 Subject: [PATCH 1/3] bitcoin:: Remove dev dependency serde_derive We do not need this dependency because we can get the serde derives directly from `serde`. diff --git a/bitcoin/Cargo.toml b/bitcoin/Cargo.toml index 3868bd08..db7fb322 100644 --- a/bitcoin/Cargo.toml +++ b/bitcoin/Cargo.toml @@ -53,7 +53,6 @@ actual-serde = { package = "serde", version = "1.0.103", default-features = fals [dev-dependencies] serde_json = "1.0.0" serde_test = "1.0.19" -serde_derive = "1.0.103" bincode = "1.3.1" [target.'cfg(mutate)'.dev-dependencies] --- Cargo-minimal.lock | 1 - Cargo-recent.lock | 1 - bitcoin/Cargo.toml | 1 - bitcoin/src/consensus/serde.rs | 2 +- 4 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Cargo-minimal.lock b/Cargo-minimal.lock index 52268334..3221cff7 100644 --- a/Cargo-minimal.lock +++ b/Cargo-minimal.lock @@ -46,7 +46,6 @@ dependencies = [ "mutagen", "secp256k1", "serde", - "serde_derive", "serde_json", "serde_test", ] diff --git a/Cargo-recent.lock b/Cargo-recent.lock index 27677e11..7f20c93f 100644 --- a/Cargo-recent.lock +++ b/Cargo-recent.lock @@ -45,7 +45,6 @@ dependencies = [ "mutagen", "secp256k1", "serde", - "serde_derive", "serde_json", "serde_test", ] diff --git a/bitcoin/Cargo.toml b/bitcoin/Cargo.toml index 0b9933cc..aa9f6159 100644 --- a/bitcoin/Cargo.toml +++ b/bitcoin/Cargo.toml @@ -53,7 +53,6 @@ actual-serde = { package = "serde", version = "1.0.103", default-features = fals [dev-dependencies] serde_json = "1.0.0" serde_test = "1.0.19" -serde_derive = "1.0.103" bincode = "1.3.1" [target.'cfg(mutate)'.dev-dependencies] diff --git a/bitcoin/src/consensus/serde.rs b/bitcoin/src/consensus/serde.rs index 1b201996..9073a32a 100644 --- a/bitcoin/src/consensus/serde.rs +++ b/bitcoin/src/consensus/serde.rs @@ -465,9 +465,9 @@ impl>> io::Read for IterReader Date: Sat, 4 Nov 2023 08:50:46 +1100 Subject: [PATCH 2/3] Remove whitespace character from string No whitespace needed here, this is likely a typo. --- hashes/src/sha512_256.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashes/src/sha512_256.rs b/hashes/src/sha512_256.rs index 61ce8fe3..a796a0bc 100644 --- a/hashes/src/sha512_256.rs +++ b/hashes/src/sha512_256.rs @@ -16,7 +16,7 @@ use crate::{sha512, FromSliceError}; crate::internal_macros::hash_type! { 256, false, - "Output of the SHA512/256 hash function.\n\nSHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at . ", + "Output of the SHA512/256 hash function.\n\nSHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at .", "crate::util::json_hex_string::len_32" } From 75c490c60f30c080c5b04ed8ce4ac50d221bb268 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 28 Sep 2023 10:55:33 +1000 Subject: [PATCH 3/3] hashes: Remove default features from schemars dep We are trying to get rid of the `serde_derive` dependency from our dependency graph. Stop using default features for the `schemars` dependency which includes `schemars_derive` which depends on `serder_derive`. Manually implement `schemars::JsonSchema` instead of deriving it. --- Cargo-minimal.lock | 24 ------------------------ Cargo-recent.lock | 24 ------------------------ contrib/test.sh | 3 --- hashes/Cargo.toml | 3 +-- hashes/src/hash160.rs | 3 +-- hashes/src/hmac.rs | 13 +++++++++++-- hashes/src/internal_macros.rs | 23 ++++++++++++++++++----- hashes/src/ripemd160.rs | 3 +-- hashes/src/sha1.rs | 3 +-- hashes/src/sha256.rs | 3 +-- hashes/src/sha256d.rs | 3 +-- hashes/src/sha256t.rs | 24 ++++++++++++++++++++++-- hashes/src/sha512.rs | 3 +-- hashes/src/sha512_256.rs | 3 +-- hashes/src/siphash24.rs | 3 +-- hashes/src/util.rs | 24 ------------------------ 16 files changed, 60 insertions(+), 102 deletions(-) diff --git a/Cargo-minimal.lock b/Cargo-minimal.lock index 3221cff7..c8fcbe8f 100644 --- a/Cargo-minimal.lock +++ b/Cargo-minimal.lock @@ -310,23 +310,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc6ab463ae35acccb5cba66c0084c985257b797d288b6050cc2f6ac1b266cb78" dependencies = [ "dyn-clone", - "schemars_derive", "serde", "serde_json", ] -[[package]] -name = "schemars_derive" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "902fdfbcf871ae8f653bddf4b2c05905ddaabc08f69d32a915787e3be0d31356" -dependencies = [ - "proc-macro2", - "quote", - "serde_derive_internals", - "syn", -] - [[package]] name = "secp256k1" version = "0.28.0" @@ -385,17 +372,6 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_derive_internals" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dbab34ca63057a1f15280bdf3c39f2b1eb1b54c17e98360e511637aef7418c6" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "serde_json" version = "1.0.68" diff --git a/Cargo-recent.lock b/Cargo-recent.lock index 7f20c93f..c6af81cf 100644 --- a/Cargo-recent.lock +++ b/Cargo-recent.lock @@ -299,23 +299,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02c613288622e5f0c3fdc5dbd4db1c5fbe752746b1d1a56a0630b78fd00de44f" dependencies = [ "dyn-clone", - "schemars_derive", "serde", "serde_json", ] -[[package]] -name = "schemars_derive" -version = "0.8.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "109da1e6b197438deb6db99952990c7f959572794b80ff93707d55a232545e7c" -dependencies = [ - "proc-macro2", - "quote", - "serde_derive_internals", - "syn", -] - [[package]] name = "secp256k1" version = "0.28.0" @@ -374,17 +361,6 @@ dependencies = [ "syn", ] -[[package]] -name = "serde_derive_internals" -version = "0.26.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "serde_json" version = "1.0.96" diff --git a/contrib/test.sh b/contrib/test.sh index 96ea2d70..51abc82a 100755 --- a/contrib/test.sh +++ b/contrib/test.sh @@ -13,10 +13,7 @@ if cargo --version | grep ${MSRV}; then cargo update -p quote --precise 1.0.30 cargo update -p proc-macro2 --precise 1.0.63 cargo update -p serde_test --precise 1.0.175 - # Have to pin this so we can pin `schemars_derive` cargo update -p schemars --precise 0.8.12 - # schemars_derive 0.8.13 uses edition 2021 - cargo update -p schemars_derive --precise 0.8.12 # byteorder 1.5.0 uses edition 2021 cargo update -p byteorder --precise 1.4.3 diff --git a/hashes/Cargo.toml b/hashes/Cargo.toml index 7c298aa3..1238ab66 100644 --- a/hashes/Cargo.toml +++ b/hashes/Cargo.toml @@ -30,8 +30,7 @@ rustdoc-args = ["--cfg", "docsrs"] hex = { package = "hex-conservative", version = "0.1.1", default-features = false } bitcoin-io = { version = "0.1", default-features = false, optional = true } - -schemars = { version = "0.8.3", optional = true } +schemars = { version = "0.8.3", default-features = false, optional = true } # Only enable this if you explicitly do not want to use "std", otherwise enable "serde-std". serde = { version = "1.0", default-features = false, optional = true } diff --git a/hashes/src/hash160.rs b/hashes/src/hash160.rs index af44f477..41098a8a 100644 --- a/hashes/src/hash160.rs +++ b/hashes/src/hash160.rs @@ -17,8 +17,7 @@ use crate::{ripemd160, sha256, FromSliceError}; crate::internal_macros::hash_type! { 160, false, - "Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))", - "crate::util::json_hex_string::len_20" + "Output of the Bitcoin HASH160 hash function. (RIPEMD160(SHA256))" } type HashEngine = sha256::HashEngine; diff --git a/hashes/src/hmac.rs b/hashes/src/hmac.rs index 32ca96a1..b196f297 100644 --- a/hashes/src/hmac.rs +++ b/hashes/src/hmac.rs @@ -17,11 +17,20 @@ use crate::{FromSliceError, Hash, HashEngine}; /// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] -#[cfg_attr(feature = "schemars", schemars(transparent))] #[repr(transparent)] pub struct Hmac(T); +#[cfg(feature = "schemars")] +impl schemars::JsonSchema for Hmac { + fn is_referenceable() -> bool { ::is_referenceable() } + + fn schema_name() -> std::string::String { ::schema_name() } + + fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + ::json_schema(gen) + } +} + impl str::FromStr for Hmac { type Err = ::Err; fn from_str(s: &str) -> Result { Ok(Hmac(str::FromStr::from_str(s)?)) } diff --git a/hashes/src/internal_macros.rs b/hashes/src/internal_macros.rs index 32a52dbf..bb99febc 100644 --- a/hashes/src/internal_macros.rs +++ b/hashes/src/internal_macros.rs @@ -196,14 +196,11 @@ pub(crate) use hash_trait_impls; /// The `from_engine` free-standing function is still required with this macro. See the doc of /// [`hash_trait_impls`]. macro_rules! hash_type { - ($bits:expr, $reverse:expr, $doc:literal, $schemars:literal) => { + ($bits:expr, $reverse:expr, $doc:literal) => { #[doc = $doc] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - #[cfg_attr(feature = "schemars", derive(crate::schemars::JsonSchema))] #[repr(transparent)] - pub struct Hash( - #[cfg_attr(feature = "schemars", schemars(schema_with = $schemars))] [u8; $bits / 8], - ); + pub struct Hash([u8; $bits / 8]); impl Hash { fn internal_new(arr: [u8; $bits / 8]) -> Self { Hash(arr) } @@ -211,6 +208,22 @@ macro_rules! hash_type { fn internal_engine() -> HashEngine { Default::default() } } + #[cfg(feature = "schemars")] + impl schemars::JsonSchema for Hash { + fn schema_name() -> String { "Hash".to_owned() } + + fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + let len = $bits / 8; + let mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); + schema.string = Some(Box::new(schemars::schema::StringValidation { + max_length: Some(len * 2), + min_length: Some(len * 2), + pattern: Some("[0-9a-fA-F]+".to_owned()), + })); + schema.into() + } + } + crate::internal_macros::hash_trait_impls!($bits, $reverse); }; } diff --git a/hashes/src/ripemd160.rs b/hashes/src/ripemd160.rs index d97b3bba..e0bebe49 100644 --- a/hashes/src/ripemd160.rs +++ b/hashes/src/ripemd160.rs @@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _}; crate::internal_macros::hash_type! { 160, false, - "Output of the RIPEMD160 hash function.", - "crate::util::json_hex_string::len_20" + "Output of the RIPEMD160 hash function." } #[cfg(not(hashes_fuzz))] diff --git a/hashes/src/sha1.rs b/hashes/src/sha1.rs index a638ebc3..79eab8f5 100644 --- a/hashes/src/sha1.rs +++ b/hashes/src/sha1.rs @@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _}; crate::internal_macros::hash_type! { 160, false, - "Output of the SHA1 hash function.", - "crate::util::json_hex_string::len_20" + "Output of the SHA1 hash function." } fn from_engine(mut e: HashEngine) -> Hash { diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index 3a8718d5..4267c66a 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -17,8 +17,7 @@ use crate::{hex, sha256d, FromSliceError, HashEngine as _}; crate::internal_macros::hash_type! { 256, false, - "Output of the SHA256 hash function.", - "crate::util::json_hex_string::len_32" + "Output of the SHA256 hash function." } #[cfg(not(hashes_fuzz))] diff --git a/hashes/src/sha256d.rs b/hashes/src/sha256d.rs index 86cff70a..4fb813bd 100644 --- a/hashes/src/sha256d.rs +++ b/hashes/src/sha256d.rs @@ -12,8 +12,7 @@ use crate::{sha256, FromSliceError}; crate::internal_macros::hash_type! { 256, true, - "Output of the SHA256d hash function.", - "crate::util::json_hex_string::len_32" + "Output of the SHA256d hash function." } type HashEngine = sha256::HashEngine; diff --git a/hashes/src/sha256t.rs b/hashes/src/sha256t.rs index 6b4847a9..774dfdc8 100644 --- a/hashes/src/sha256t.rs +++ b/hashes/src/sha256t.rs @@ -27,7 +27,13 @@ impl schemars::JsonSchema for Hash { fn schema_name() -> String { "Hash".to_owned() } fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { - crate::util::json_hex_string::len_32(gen) + let mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); + schema.string = Some(Box::new(schemars::schema::StringValidation { + max_length: Some(32 * 2), + min_length: Some(32 * 2), + pattern: Some("[0-9a-fA-F]+".to_owned()), + })); + schema.into() } } @@ -169,7 +175,6 @@ mod tests { ]; #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)] - #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] pub struct TestHashTag; impl sha256t::Tag for TestHashTag { @@ -180,6 +185,21 @@ mod tests { } } + #[cfg(feature = "schemars")] + impl schemars::JsonSchema for TestHashTag { + fn schema_name() -> String { "Hash".to_owned() } + + fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { + let mut schema: schemars::schema::SchemaObject = ::json_schema(gen).into(); + schema.string = Some(Box::new(schemars::schema::StringValidation { + max_length: Some(64 * 2), + min_length: Some(64 * 2), + pattern: Some("[0-9a-fA-F]+".to_owned()), + })); + schema.into() + } + } + /// A hash tagged with `$name`. #[cfg(feature = "alloc")] pub type TestHash = sha256t::Hash; diff --git a/hashes/src/sha512.rs b/hashes/src/sha512.rs index f7fff543..1b380b8f 100644 --- a/hashes/src/sha512.rs +++ b/hashes/src/sha512.rs @@ -13,8 +13,7 @@ use crate::{FromSliceError, HashEngine as _}; crate::internal_macros::hash_type! { 512, false, - "Output of the SHA512 hash function.", - "crate::util::json_hex_string::len_64" + "Output of the SHA512 hash function." } #[cfg(not(hashes_fuzz))] diff --git a/hashes/src/sha512_256.rs b/hashes/src/sha512_256.rs index a796a0bc..c0d4529b 100644 --- a/hashes/src/sha512_256.rs +++ b/hashes/src/sha512_256.rs @@ -16,8 +16,7 @@ use crate::{sha512, FromSliceError}; crate::internal_macros::hash_type! { 256, false, - "Output of the SHA512/256 hash function.\n\nSHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at .", - "crate::util::json_hex_string::len_32" + "Output of the SHA512/256 hash function.\n\nSHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at ." } fn from_engine(e: HashEngine) -> Hash { diff --git a/hashes/src/siphash24.rs b/hashes/src/siphash24.rs index 86981afe..c81335de 100644 --- a/hashes/src/siphash24.rs +++ b/hashes/src/siphash24.rs @@ -12,8 +12,7 @@ use crate::{FromSliceError, Hash as _, HashEngine as _}; crate::internal_macros::hash_type! { 64, false, - "Output of the SipHash24 hash function.", - "crate::util::json_hex_string::len_8" + "Output of the SipHash24 hash function." } #[cfg(not(hashes_fuzz))] diff --git a/hashes/src/util.rs b/hashes/src/util.rs index 2a5756ad..393718c9 100644 --- a/hashes/src/util.rs +++ b/hashes/src/util.rs @@ -386,30 +386,6 @@ macro_rules! hash_newtype_known_attrs { ($($ignore:tt)*) => {}; } -#[cfg(feature = "schemars")] -pub mod json_hex_string { - use schemars::gen::SchemaGenerator; - use schemars::schema::{Schema, SchemaObject}; - use schemars::JsonSchema; - macro_rules! define_custom_hex { - ($name:ident, $len:expr) => { - pub fn $name(gen: &mut SchemaGenerator) -> Schema { - let mut schema: SchemaObject = ::json_schema(gen).into(); - schema.string = Some(Box::new(schemars::schema::StringValidation { - max_length: Some($len * 2), - min_length: Some($len * 2), - pattern: Some("[0-9a-fA-F]+".to_owned()), - })); - schema.into() - } - }; - } - define_custom_hex!(len_8, 8); - define_custom_hex!(len_20, 20); - define_custom_hex!(len_32, 32); - define_custom_hex!(len_64, 64); -} - #[cfg(test)] mod test { use crate::{sha256, Hash};