referrerpolicy=no-referrer-when-downgrade

sp_core/
hash.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! A fixed hash type.
19
20pub use primitive_types::{H160, H256, H512};
21
22/// Hash conversion. Used to convert between unbound associated hash types in traits,
23/// implemented by the same hash type.
24/// Panics if used to convert between different hash types.
25pub fn convert_hash<H1: Default + AsMut<[u8]>, H2: AsRef<[u8]>>(src: &H2) -> H1 {
26	let mut dest = H1::default();
27	assert_eq!(dest.as_mut().len(), src.as_ref().len());
28	dest.as_mut().copy_from_slice(src.as_ref());
29	dest
30}
31
32#[cfg(test)]
33mod tests {
34	use super::*;
35
36	#[test]
37	fn test_h160() {
38		let tests = vec![
39			(Default::default(), "0x0000000000000000000000000000000000000000"),
40			(H160::from_low_u64_be(2), "0x0000000000000000000000000000000000000002"),
41			(H160::from_low_u64_be(15), "0x000000000000000000000000000000000000000f"),
42			(H160::from_low_u64_be(16), "0x0000000000000000000000000000000000000010"),
43			(H160::from_low_u64_be(1_000), "0x00000000000000000000000000000000000003e8"),
44			(H160::from_low_u64_be(100_000), "0x00000000000000000000000000000000000186a0"),
45			(H160::from_low_u64_be(u64::MAX), "0x000000000000000000000000ffffffffffffffff"),
46		];
47
48		for (number, expected) in tests {
49			assert_eq!(
50				format!("{:?}", expected),
51				serde_json::to_string_pretty(&number).expect("Json pretty print failed")
52			);
53			assert_eq!(number, serde_json::from_str(&format!("{:?}", expected)).unwrap());
54		}
55	}
56
57	#[test]
58	fn test_h256() {
59		let tests = vec![
60			(
61				Default::default(),
62				"0x0000000000000000000000000000000000000000000000000000000000000000",
63			),
64			(
65				H256::from_low_u64_be(2),
66				"0x0000000000000000000000000000000000000000000000000000000000000002",
67			),
68			(
69				H256::from_low_u64_be(15),
70				"0x000000000000000000000000000000000000000000000000000000000000000f",
71			),
72			(
73				H256::from_low_u64_be(16),
74				"0x0000000000000000000000000000000000000000000000000000000000000010",
75			),
76			(
77				H256::from_low_u64_be(1_000),
78				"0x00000000000000000000000000000000000000000000000000000000000003e8",
79			),
80			(
81				H256::from_low_u64_be(100_000),
82				"0x00000000000000000000000000000000000000000000000000000000000186a0",
83			),
84			(
85				H256::from_low_u64_be(u64::MAX),
86				"0x000000000000000000000000000000000000000000000000ffffffffffffffff",
87			),
88		];
89
90		for (number, expected) in tests {
91			assert_eq!(
92				format!("{:?}", expected),
93				serde_json::to_string_pretty(&number).expect("Json pretty print failed")
94			);
95			assert_eq!(number, serde_json::from_str(&format!("{:?}", expected)).unwrap());
96		}
97	}
98
99	#[test]
100	fn test_invalid() {
101		assert!(serde_json::from_str::<H256>(
102			"\"0x000000000000000000000000000000000000000000000000000000000000000\""
103		)
104		.unwrap_err()
105		.is_data());
106		assert!(serde_json::from_str::<H256>(
107			"\"0x000000000000000000000000000000000000000000000000000000000000000g\""
108		)
109		.unwrap_err()
110		.is_data());
111		assert!(serde_json::from_str::<H256>(
112			"\"0x00000000000000000000000000000000000000000000000000000000000000000\""
113		)
114		.unwrap_err()
115		.is_data());
116		assert!(serde_json::from_str::<H256>("\"\"").unwrap_err().is_data());
117		assert!(serde_json::from_str::<H256>("\"0\"").unwrap_err().is_data());
118		assert!(serde_json::from_str::<H256>("\"10\"").unwrap_err().is_data());
119	}
120}