s_types/
hash.rs

1use {U64, U128, U256, U512};
2
3#[cfg(feature="serialize")]
4use serde::{Serialize, Serializer, Deserialize, Deserializer};
5
6#[cfg(feature="serialize")]
7use s_types_serialize;
8
9macro_rules! impl_serde {
10	($name: ident, $len: expr) => {
11		#[cfg(feature="serialize")]
12		impl Serialize for $name {
13			fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
14				let mut slice = [0u8; 2 + 2 * $len];
15				s_types_serialize::serialize(&mut slice, &self.0, serializer)
16			}
17		}
18
19		#[cfg(feature="serialize")]
20		impl<'de> Deserialize<'de> for $name {
21			fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
22				let mut bytes = [0u8; $len];
23				s_types_serialize::deserialize_check_len(deserializer, s_types_serialize::ExpectedLen::Exact(&mut bytes))?;
24				Ok($name(bytes))
25			}
26		}
27	}
28}
29
30macro_rules! impl_uint_conversions {
31	($hash: ident, $uint: ident) => {
32		impl From<$uint> for $hash {
33			fn from(value: $uint) -> Self {
34				let mut ret = $hash::new();
35				value.to_big_endian(&mut ret);
36				ret
37			}
38		}
39
40		impl<'a> From<&'a $uint> for $hash {
41			fn from(value: &'a $uint) -> Self {
42				let mut ret = $hash::new();
43				value.to_big_endian(&mut ret);
44				ret
45			}
46		}
47
48		impl From<$hash> for $uint {
49			fn from(value: $hash) -> Self {
50				Self::from(&value)
51			}
52		}
53
54		impl<'a> From<&'a $hash> for $uint {
55			fn from(value: &'a $hash) -> Self {
56				Self::from(value.as_ref() as &[u8])
57			}
58		}
59	}
60}
61
62impl_serde!(H32, 4);
63impl_serde!(H64, 8);
64impl_serde!(H128, 16);
65impl_serde!(H160, 20);
66impl_serde!(H256, 32);
67impl_serde!(H264, 33);
68impl_serde!(H512, 64);
69impl_serde!(H520, 65);
70
71construct_hash!(H32, 4);
72construct_hash!(H64, 8);
73construct_hash!(H128, 16);
74construct_hash!(H160, 20);
75construct_hash!(H256, 32);
76construct_hash!(H264, 33);
77construct_hash!(H512, 64);
78construct_hash!(H520, 65);
79
80impl_uint_conversions!(H64, U64);
81impl_uint_conversions!(H128, U128);
82impl_uint_conversions!(H256, U256);
83impl_uint_conversions!(H512, U512);
84
85#[deprecated]
86impl From<H256> for H160 {
87	fn from(value: H256) -> H160 {
88		let mut ret = H160::new();
89		ret.0.copy_from_slice(&value[12..32]);
90		ret
91	}
92}
93
94#[deprecated]
95impl From<H256> for H64 {
96	fn from(value: H256) -> H64 {
97		let mut ret = H64::new();
98		ret.0.copy_from_slice(&value[20..28]);
99		ret
100	}
101}
102
103impl From<H160> for H256 {
104	fn from(value: H160) -> H256 {
105		let mut ret = H256::new();
106		ret.0[12..32].copy_from_slice(&value);
107		ret
108	}
109}
110
111impl<'a> From<&'a H160> for H256 {
112	fn from(value: &'a H160) -> H256 {
113		let mut ret = H256::new();
114		ret.0[12..32].copy_from_slice(value);
115		ret
116	}
117}
118
119#[cfg(test)]
120mod tests {
121	use super::{H160, H256};
122	use serde_json as ser;
123
124	#[test]
125	fn test_serialize_h160() {
126		let tests = vec![
127			(H160::from(0), "0x0000000000000000000000000000000000000000"),
128			(H160::from(2), "0x0000000000000000000000000000000000000002"),
129			(H160::from(15), "0x000000000000000000000000000000000000000f"),
130			(H160::from(16), "0x0000000000000000000000000000000000000010"),
131			(H160::from(1_000), "0x00000000000000000000000000000000000003e8"),
132			(H160::from(100_000), "0x00000000000000000000000000000000000186a0"),
133			(H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"),
134		];
135
136		for (number, expected) in tests {
137			assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
138			assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
139		}
140	}
141
142	#[test]
143	fn test_serialize_h256() {
144		let tests = vec![
145			(H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"),
146			(H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"),
147			(H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"),
148			(H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"),
149			(H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"),
150			(H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"),
151			(H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"),
152		];
153
154		for (number, expected) in tests {
155			assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
156			assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
157		}
158	}
159
160	#[test]
161	fn test_serialize_invalid() {
162		assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
163		assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000g\"").unwrap_err().is_data());
164		assert!(ser::from_str::<H256>("\"0x00000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
165		assert!(ser::from_str::<H256>("\"\"").unwrap_err().is_data());
166		assert!(ser::from_str::<H256>("\"0\"").unwrap_err().is_data());
167		assert!(ser::from_str::<H256>("\"10\"").unwrap_err().is_data());
168	}
169}