sophon_types/
hash.rs

1use crate::{U64, U128, U256, U512};
2
3#[cfg(feature = "serialize")]
4use serde::{Serialize, Serializer, Deserialize, Deserializer};
5
6#[cfg(feature = "serialize")]
7use sophon_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				sophon_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				sophon_types_serialize::deserialize_check_len(deserializer, sophon_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::zero();
35				value.to_big_endian(ret.as_bytes_mut());
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::zero();
43				value.to_big_endian(ret.as_bytes_mut());
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_fixed_hash!{ pub struct H32(4); }
72construct_fixed_hash!{ pub struct H64(8); }
73construct_fixed_hash!{ pub struct H128(16); }
74construct_fixed_hash!{ pub struct H160(20); }
75construct_fixed_hash!{ pub struct H256(32); }
76construct_fixed_hash!{ pub struct H264(33); }
77construct_fixed_hash!{ pub struct H512(64); }
78construct_fixed_hash!{ pub struct H520(65); }
79
80impl_uint_conversions!(H64, U64);
81impl_uint_conversions!(H128, U128);
82impl_uint_conversions!(H256, U256);
83impl_uint_conversions!(H512, U512);
84
85impl From<H160> for H256 {
86	fn from(value: H160) -> H256 {
87		let mut ret = H256::zero();
88		ret.0[12..32].copy_from_slice(value.as_bytes());
89		ret
90	}
91}
92
93impl<'a> From<&'a H160> for H256 {
94	fn from(value: &'a H160) -> H256 {
95		let mut ret = H256::zero();
96		ret.0[12..32].copy_from_slice(value.as_bytes());
97		ret
98	}
99}
100
101impl From<u64> for H160 {
102	fn from(val: u64) -> Self {
103		H160::from_low_u64_be(val)
104	}
105}
106
107impl From<u64> for H256 {
108	fn from(val: u64) -> Self {
109		H256::from_low_u64_be(val)
110	}
111}
112
113#[cfg(test)]
114mod tests {
115	use super::{H160, H256};
116	use serde_json as ser;
117
118	#[test]
119	fn test_serialize_h160() {
120		let tests = vec![
121			(H160::from(0), "0x0000000000000000000000000000000000000000"),
122			(H160::from(2), "0x0000000000000000000000000000000000000002"),
123			(H160::from(15), "0x000000000000000000000000000000000000000f"),
124			(H160::from(16), "0x0000000000000000000000000000000000000010"),
125			(H160::from(1_000), "0x00000000000000000000000000000000000003e8"),
126			(H160::from(100_000), "0x00000000000000000000000000000000000186a0"),
127			(H160::from(u64::max_value()), "0x000000000000000000000000ffffffffffffffff"),
128		];
129
130		for (number, expected) in tests {
131			assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
132			assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
133		}
134	}
135
136	#[test]
137	fn test_serialize_h256() {
138		let tests = vec![
139			(H256::from(0), "0x0000000000000000000000000000000000000000000000000000000000000000"),
140			(H256::from(2), "0x0000000000000000000000000000000000000000000000000000000000000002"),
141			(H256::from(15), "0x000000000000000000000000000000000000000000000000000000000000000f"),
142			(H256::from(16), "0x0000000000000000000000000000000000000000000000000000000000000010"),
143			(H256::from(1_000), "0x00000000000000000000000000000000000000000000000000000000000003e8"),
144			(H256::from(100_000), "0x00000000000000000000000000000000000000000000000000000000000186a0"),
145			(H256::from(u64::max_value()), "0x000000000000000000000000000000000000000000000000ffffffffffffffff"),
146		];
147
148		for (number, expected) in tests {
149			assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number).unwrap());
150			assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
151		}
152	}
153
154	#[test]
155	fn test_serialize_invalid() {
156		assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
157		assert!(ser::from_str::<H256>("\"0x000000000000000000000000000000000000000000000000000000000000000g\"").unwrap_err().is_data());
158		assert!(ser::from_str::<H256>("\"0x00000000000000000000000000000000000000000000000000000000000000000\"").unwrap_err().is_data());
159		assert!(ser::from_str::<H256>("\"\"").unwrap_err().is_data());
160		assert!(ser::from_str::<H256>("\"0\"").unwrap_err().is_data());
161		assert!(ser::from_str::<H256>("\"10\"").unwrap_err().is_data());
162	}
163}