Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit c7882c0

Browse files
authored
Merge pull request #117 from JeremyRubin/schemars
Reintroduce Schemars with MSRV compatible deps
2 parents 5518293 + 133e97d commit c7882c0

File tree

16 files changed

+285
-7
lines changed

16 files changed

+285
-7
lines changed

.github/workflows/rust.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ jobs:
7878
- name: Running cargo
7979
env:
8080
DO_FEATURE_MATRIX: true
81+
DO_SCHEMARS_TESTS: ${{matrix.rust != '1.29.0'}}
8182
run: ./contrib/test.sh
8283

8384
Embedded:

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ unstable = [] # for benchmarking
2222

2323
[dependencies]
2424
serde = { version = "1.0", default-features = false, optional = true }
25+
schemars = { version = "0.8.0", optional = true }
2526

2627
[dev-dependencies]
2728
serde_test = "1.0"

contrib/test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ if [ "$DO_FEATURE_MATRIX" = true ]; then
3636
cargo test --all --features="serde-std"
3737
fi
3838

39+
if [ "$DO_SCHEMARS_TESTS" = true ]; then
40+
(cd extended_tests/schemars && cargo test)
41+
fi
42+
3943
# Docs
4044
if [ "$DO_DOCS" = true ]; then
4145
cargo doc --all --features="$FEATURES"

extended_tests/schemars/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "schemars"
3+
version = "0.1.0"
4+
authors = ["Jeremy Rubin <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies.bitcoin_hashes]
10+
path = "../.."
11+
features = ['schemars', 'serde']
12+
[dependencies]
13+
jsonschema-valid = "^0.4.0"
14+
serde = { version = "1.0", default-features = false}
15+
schemars = { version = "0.8.0"}
16+
serde_test = "1.0"
17+
serde_json = "1.0"

extended_tests/schemars/src/main.rs

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
fn main() {}
2+
#[cfg(test)]
3+
mod tests {
4+
use bitcoin_hashes::*;
5+
#[test]
6+
fn it_works() {}
7+
8+
#[test]
9+
fn hash160() {
10+
static HASH_BYTES: [u8; 20] = [
11+
0x13, 0x20, 0x72, 0xdf, 0x69, 0x09, 0x33, 0x83, 0x5e, 0xb8, 0xb6, 0xad, 0x0b, 0x77,
12+
0xe7, 0xb6, 0xf1, 0x4a, 0xca, 0xd7,
13+
];
14+
15+
let hash = hash160::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
16+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
17+
let s = schemars::schema_for!(hash160::Hash);
18+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
19+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
20+
.unwrap()
21+
.validate(&js)
22+
.is_ok());
23+
}
24+
25+
#[test]
26+
fn HMAC_SHA512() {
27+
static HASH_BYTES: [u8; 64] = [
28+
0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21, 0x11, 0x3c, 0x52, 0xff, 0x18, 0x2a,
29+
0x1b, 0x8e, 0x0a, 0x19, 0x57, 0x54, 0xaa, 0x52, 0x7f, 0xcd, 0x00, 0xa4, 0x11, 0x62,
30+
0x0b, 0x46, 0xf2, 0x0f, 0xff, 0xfb, 0x80, 0x88, 0xcc, 0xf8, 0x54, 0x97, 0x12, 0x1a,
31+
0xd4, 0x49, 0x9e, 0x08, 0x45, 0xb8, 0x76, 0xf6, 0xdd, 0x66, 0x40, 0x08, 0x8a, 0x2f,
32+
0x0b, 0x2d, 0x8a, 0x60, 0x0b, 0xdf, 0x4c, 0x0c,
33+
];
34+
35+
let hash = Hmac::<sha512::Hash>::from_slice(&HASH_BYTES).expect("right number of bytes");
36+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
37+
let s = schemars::schema_for!(Hmac::<sha512::Hash>);
38+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
39+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
40+
.unwrap()
41+
.validate(&js)
42+
.is_ok());
43+
}
44+
45+
#[test]
46+
fn ripemd160() {
47+
static HASH_BYTES: [u8; 20] = [
48+
0x13, 0x20, 0x72, 0xdf, 0x69, 0x09, 0x33, 0x83, 0x5e, 0xb8, 0xb6, 0xad, 0x0b, 0x77,
49+
0xe7, 0xb6, 0xf1, 0x4a, 0xca, 0xd7,
50+
];
51+
52+
let hash = ripemd160::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
53+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
54+
let s = schemars::schema_for!(ripemd160::Hash);
55+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
56+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
57+
.unwrap()
58+
.validate(&js)
59+
.is_ok());
60+
}
61+
62+
#[test]
63+
fn sha1() {
64+
static HASH_BYTES: [u8; 20] = [
65+
0x13, 0x20, 0x72, 0xdf, 0x69, 0x09, 0x33, 0x83, 0x5e, 0xb8, 0xb6, 0xad, 0x0b, 0x77,
66+
0xe7, 0xb6, 0xf1, 0x4a, 0xca, 0xd7,
67+
];
68+
69+
let hash = sha1::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
70+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
71+
let s = schemars::schema_for!(sha1::Hash);
72+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
73+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
74+
.unwrap()
75+
.validate(&js)
76+
.is_ok());
77+
}
78+
79+
#[test]
80+
fn sha256d() {
81+
static HASH_BYTES: [u8; 32] = [
82+
0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, 0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6,
83+
0x3d, 0x97, 0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2, 0xb7, 0x65, 0x44, 0x8c,
84+
0x86, 0x35, 0xfb, 0x6c,
85+
];
86+
87+
let hash = sha256d::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
88+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
89+
let s = schemars::schema_for!(sha256d::Hash);
90+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
91+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
92+
.unwrap()
93+
.validate(&js)
94+
.is_ok());
95+
}
96+
97+
#[test]
98+
fn sha256() {
99+
static HASH_BYTES: [u8; 32] = [
100+
0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, 0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6,
101+
0x3d, 0x97, 0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2, 0xb7, 0x65, 0x44, 0x8c,
102+
0x86, 0x35, 0xfb, 0x6c,
103+
];
104+
105+
let hash = sha256::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
106+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
107+
let s = schemars::schema_for!(sha256::Hash);
108+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
109+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
110+
.unwrap()
111+
.validate(&js)
112+
.is_ok());
113+
}
114+
115+
#[test]
116+
fn TestHash() {
117+
const TEST_MIDSTATE: [u8; 32] = [
118+
156, 224, 228, 230, 124, 17, 108, 57, 56, 179, 202, 242, 195, 15, 80, 137, 211, 243,
119+
147, 108, 71, 99, 110, 96, 125, 179, 62, 234, 221, 198, 240, 201,
120+
];
121+
122+
#[derive(
123+
Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash, schemars::JsonSchema,
124+
)]
125+
pub struct TestHashTag;
126+
127+
impl sha256t::Tag for TestHashTag {
128+
fn engine() -> sha256::HashEngine {
129+
// The TapRoot TapLeaf midstate.
130+
let midstate = sha256::Midstate::from_inner(TEST_MIDSTATE);
131+
sha256::HashEngine::from_midstate(midstate, 64)
132+
}
133+
}
134+
135+
/// A hash tagged with `$name`.
136+
pub type TestHash = sha256t::Hash<TestHashTag>;
137+
138+
sha256t_hash_newtype!(
139+
NewTypeHash,
140+
NewTypeTag,
141+
TEST_MIDSTATE,
142+
64,
143+
doc = "test hash",
144+
true
145+
);
146+
static HASH_BYTES: [u8; 32] = [
147+
0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, 0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6,
148+
0x3d, 0x97, 0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2, 0xb7, 0x65, 0x44, 0x8c,
149+
0x86, 0x35, 0xfb, 0x6c,
150+
];
151+
152+
let hash = TestHash::from_slice(&HASH_BYTES).expect("right number of bytes");
153+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
154+
let s = schemars::schema_for!(TestHash);
155+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
156+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
157+
.unwrap()
158+
.validate(&js)
159+
.is_ok());
160+
}
161+
#[test]
162+
fn sha512() {
163+
static HASH_BYTES: [u8; 64] = [
164+
0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21, 0x11, 0x3c, 0x52, 0xff, 0x18, 0x2a,
165+
0x1b, 0x8e, 0x0a, 0x19, 0x57, 0x54, 0xaa, 0x52, 0x7f, 0xcd, 0x00, 0xa4, 0x11, 0x62,
166+
0x0b, 0x46, 0xf2, 0x0f, 0xff, 0xfb, 0x80, 0x88, 0xcc, 0xf8, 0x54, 0x97, 0x12, 0x1a,
167+
0xd4, 0x49, 0x9e, 0x08, 0x45, 0xb8, 0x76, 0xf6, 0xdd, 0x66, 0x40, 0x08, 0x8a, 0x2f,
168+
0x0b, 0x2d, 0x8a, 0x60, 0x0b, 0xdf, 0x4c, 0x0c,
169+
];
170+
171+
let hash = sha512::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
172+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
173+
let s = schemars::schema_for!(sha512::Hash);
174+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
175+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
176+
.unwrap()
177+
.validate(&js)
178+
.is_ok());
179+
}
180+
181+
#[test]
182+
fn siphash24() {
183+
static HASH_BYTES: [u8; 8] = [0x8b, 0x41, 0xe1, 0xb7, 0x8a, 0xd1, 0x15, 0x21];
184+
185+
let hash = siphash24::Hash::from_slice(&HASH_BYTES).expect("right number of bytes");
186+
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
187+
let s = schemars::schema_for!(siphash24::Hash);
188+
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
189+
assert!(jsonschema_valid::Config::from_schema(&schema, None)
190+
.unwrap()
191+
.validate(&js)
192+
.is_ok());
193+
}
194+
}

src/hash160.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ use Error;
2828

2929
/// Output of the Bitcoin HASH160 hash function
3030
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
31+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
3132
#[repr(transparent)]
32-
pub struct Hash([u8; 20]);
33+
pub struct Hash(
34+
#[cfg_attr(feature = "schemars", schemars(schema_with="crate::util::json_hex_string::len_20"))]
35+
[u8; 20]
36+
);
37+
38+
3339

3440
hex_fmt_impl!(Debug, Hash);
3541
hex_fmt_impl!(Display, Hash);

src/hmac.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ use Error;
2929

3030
/// A hash computed from a RFC 2104 HMAC. Parameterized by the underlying hash function.
3131
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
32+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
33+
#[cfg_attr(feature = "schemars", schemars(transparent))]
3234
#[repr(transparent)]
3335
pub struct Hmac<T: HashTrait>(T);
3436

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#[cfg(feature="serde")] pub extern crate serde;
4242
#[cfg(all(test,feature="serde"))] extern crate serde_test;
4343

44+
#[cfg(feature = "schemars")] extern crate schemars;
45+
4446
#[macro_use] mod util;
4547
#[macro_use] pub mod serde_macros;
4648
#[cfg(any(test, feature = "std"))] mod std_impls;

src/ripemd160.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ impl EngineTrait for HashEngine {
7676

7777
/// Output of the RIPEMD160 hash function
7878
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
79+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7980
#[repr(transparent)]
80-
pub struct Hash([u8; 20]);
81+
pub struct Hash(
82+
#[cfg_attr(feature = "schemars", schemars(schema_with="util::json_hex_string::len_20"))]
83+
[u8; 20]
84+
);
8185

8286
hex_fmt_impl!(Debug, Hash);
8387
hex_fmt_impl!(Display, Hash);

src/sha1.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ impl EngineTrait for HashEngine {
7171

7272
/// Output of the SHA1 hash function
7373
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
74+
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
7475
#[repr(transparent)]
75-
pub struct Hash([u8; 20]);
76+
pub struct Hash(
77+
#[cfg_attr(feature = "schemars", schemars(schema_with="util::json_hex_string::len_20"))]
78+
[u8; 20]
79+
);
7680

7781
hex_fmt_impl!(Debug, Hash);
7882
hex_fmt_impl!(Display, Hash);

0 commit comments

Comments
 (0)