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.
This commit is contained in:
Tobin C. Harding 2023-09-28 10:55:33 +10:00
parent 1105876423
commit 75c490c60f
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
16 changed files with 60 additions and 102 deletions

View File

@ -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"

View File

@ -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"

View File

@ -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

View File

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

View File

@ -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;

View File

@ -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: Hash>(T);
#[cfg(feature = "schemars")]
impl<T: Hash + schemars::JsonSchema> schemars::JsonSchema for Hmac<T> {
fn is_referenceable() -> bool { <T as schemars::JsonSchema>::is_referenceable() }
fn schema_name() -> std::string::String { <T as schemars::JsonSchema>::schema_name() }
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
<T as schemars::JsonSchema>::json_schema(gen)
}
}
impl<T: Hash + str::FromStr> str::FromStr for Hmac<T> {
type Err = <T as str::FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> { Ok(Hmac(str::FromStr::from_str(s)?)) }

View File

@ -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 = <String>::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);
};
}

View File

@ -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))]

View File

@ -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 {

View File

@ -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))]

View File

@ -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;

View File

@ -27,7 +27,13 @@ impl<T: Tag> schemars::JsonSchema for Hash<T> {
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 = <String>::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 = <String>::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<TestHashTag>;

View File

@ -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))]

View File

@ -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 <https://eprint.iacr.org/2010/548.pdf>.",
"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 <https://eprint.iacr.org/2010/548.pdf>."
}
fn from_engine(e: HashEngine) -> Hash {

View File

@ -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))]

View File

@ -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 = <String>::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};