1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A fixed hash type.

pub use primitive_types::{H160, H256, H512};

/// Hash conversion. Used to convert between unbound associated hash types in traits,
/// implemented by the same hash type.
/// Panics if used to convert between different hash types.
pub fn convert_hash<H1: Default + AsMut<[u8]>, H2: AsRef<[u8]>>(src: &H2) -> H1 {
	let mut dest = H1::default();
	assert_eq!(dest.as_mut().len(), src.as_ref().len());
	dest.as_mut().copy_from_slice(src.as_ref());
	dest
}

#[cfg(test)]
mod tests {
	use super::*;
	use sp_serializer as ser;

	#[test]
	fn test_h160() {
		let tests = vec![
			(Default::default(), "0x0000000000000000000000000000000000000000"),
			(H160::from_low_u64_be(2), "0x0000000000000000000000000000000000000002"),
			(H160::from_low_u64_be(15), "0x000000000000000000000000000000000000000f"),
			(H160::from_low_u64_be(16), "0x0000000000000000000000000000000000000010"),
			(H160::from_low_u64_be(1_000), "0x00000000000000000000000000000000000003e8"),
			(H160::from_low_u64_be(100_000), "0x00000000000000000000000000000000000186a0"),
			(H160::from_low_u64_be(u64::MAX), "0x000000000000000000000000ffffffffffffffff"),
		];

		for (number, expected) in tests {
			assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number));
			assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
		}
	}

	#[test]
	fn test_h256() {
		let tests = vec![
			(
				Default::default(),
				"0x0000000000000000000000000000000000000000000000000000000000000000",
			),
			(
				H256::from_low_u64_be(2),
				"0x0000000000000000000000000000000000000000000000000000000000000002",
			),
			(
				H256::from_low_u64_be(15),
				"0x000000000000000000000000000000000000000000000000000000000000000f",
			),
			(
				H256::from_low_u64_be(16),
				"0x0000000000000000000000000000000000000000000000000000000000000010",
			),
			(
				H256::from_low_u64_be(1_000),
				"0x00000000000000000000000000000000000000000000000000000000000003e8",
			),
			(
				H256::from_low_u64_be(100_000),
				"0x00000000000000000000000000000000000000000000000000000000000186a0",
			),
			(
				H256::from_low_u64_be(u64::MAX),
				"0x000000000000000000000000000000000000000000000000ffffffffffffffff",
			),
		];

		for (number, expected) in tests {
			assert_eq!(format!("{:?}", expected), ser::to_string_pretty(&number));
			assert_eq!(number, ser::from_str(&format!("{:?}", expected)).unwrap());
		}
	}

	#[test]
	fn test_invalid() {
		assert!(ser::from_str::<H256>(
			"\"0x000000000000000000000000000000000000000000000000000000000000000\""
		)
		.unwrap_err()
		.is_data());
		assert!(ser::from_str::<H256>(
			"\"0x000000000000000000000000000000000000000000000000000000000000000g\""
		)
		.unwrap_err()
		.is_data());
		assert!(ser::from_str::<H256>(
			"\"0x00000000000000000000000000000000000000000000000000000000000000000\""
		)
		.unwrap_err()
		.is_data());
		assert!(ser::from_str::<H256>("\"\"").unwrap_err().is_data());
		assert!(ser::from_str::<H256>("\"0\"").unwrap_err().is_data());
		assert!(ser::from_str::<H256>("\"10\"").unwrap_err().is_data());
	}
}