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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use sp_std::vec::Vec;

/// Mainnet Genesis Hash 0x4a587bf17a404e3572747add7aab7bbe56e805a5479c6c436f07f36fcc8d3ae1
pub const MAINNET_GENESIS_HASH: &[u8] = &[
	74u8, 88, 123, 241, 122, 64, 78, 53, 114, 116, 122, 221, 122, 171, 123, 190, 86, 232, 5, 165,
	71, 156, 108, 67, 111, 7, 243, 111, 204, 141, 58, 225,
];

/// Frequency Testnet on Paseo Genesis Hash 0x203c6838fc78ea3660a2f298a58d859519c72a5efdc0f194abd6f0d5ce1838e0
pub const TESTNET_ON_PASEO_GENESIS_HASH: &[u8] = &[
	32, 60, 104, 56, 252, 120, 234, 54, 96, 162, 242, 152, 165, 141, 133, 149, 25, 199, 42, 94,
	253, 192, 241, 148, 171, 214, 240, 213, 206, 24, 56, 224,
];

/// An enum that shows the detected chain type
#[derive(Debug, Clone, PartialEq)]
pub enum DetectedChainType {
	/// An unknown chain, it can be a local or development chain
	Unknown,
	/// Frequency Mainnet
	FrequencyMainNet,
	/// Frequency Paseo Testnet
	FrequencyPaseoTestNet,
}

/// Finds the chain type by genesis hash
pub fn get_chain_type_by_genesis_hash(genesis_hash: &[u8]) -> DetectedChainType {
	match genesis_hash {
		MAINNET_GENESIS_HASH => DetectedChainType::FrequencyMainNet,
		TESTNET_ON_PASEO_GENESIS_HASH => DetectedChainType::FrequencyPaseoTestNet,
		_ => DetectedChainType::Unknown,
	}
}
/// Handle serializing and deserializing from `Vec<u8>` to hexadecimal
#[cfg(feature = "std")]
pub mod as_hex {
	use serde::{Deserializer, Serializer};

	/// Serializes a `Vec<u8>` into a hexadecimal string
	pub fn serialize<S>(bytes: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		impl_serde::serialize::serialize(bytes.as_slice(), serializer)
	}

	/// Deserializes a hexadecimal string into a `Vec<u8>`
	pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<u8>, D::Error> {
		impl_serde::serialize::deserialize(deserializer)
	}
}

/// Handle serializing and deserializing from `Option<Vec<u8>>` to hexadecimal
#[cfg(feature = "std")]
pub mod as_hex_option {
	use serde::{Deserializer, Serializer};

	/// Serializes a `Vec<u8>` into a hexadecimal string
	pub fn serialize<S>(bytes: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		match bytes {
			Some(bytes) => impl_serde::serialize::serialize(bytes.as_slice(), serializer),
			None => serializer.serialize_none(),
		}
	}

	/// Deserializes a hexadecimal string into a `Vec<u8>`
	pub fn deserialize<'de, D: Deserializer<'de>>(
		deserializer: D,
	) -> Result<Option<Vec<u8>>, D::Error>
	where
		D: Deserializer<'de>,
	{
		impl_serde::serialize::deserialize(deserializer).map(|r| Some(r))
	}
}
/// Handle serializing and deserializing from `Vec<u8>` to a UTF-8 string
#[cfg(feature = "std")]
pub mod as_string {
	use super::*;
	use serde::{ser::Error, Deserialize, Deserializer, Serialize, Serializer};

	/// Serializes a `Vec<u8>` into a UTF-8 string
	pub fn serialize<S: Serializer>(bytes: &Vec<u8>, serializer: S) -> Result<S::Ok, S::Error> {
		std::str::from_utf8(bytes)
			.map_err(|e| S::Error::custom(format!("Debug buffer contains invalid UTF8: {}", e)))?
			.serialize(serializer)
	}

	/// Serializes a UTF-8 string into a `Vec<u8>`
	pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<u8>, D::Error> {
		Ok(String::deserialize(deserializer)?.into_bytes())
	}
}

/// Handle serializing and deserializing from `Option<Vec<u8>>` to a UTF-8 string
#[cfg(feature = "std")]
pub mod as_string_option {
	use super::*;
	use serde::{ser::Error, Deserialize, Deserializer, Serialize, Serializer};

	/// Serializes a `Option<Vec<u8>>` into a UTF-8 string if Ok()
	pub fn serialize<S: Serializer>(
		bytes: &Option<Vec<u8>>,
		serializer: S,
	) -> Result<S::Ok, S::Error> {
		match bytes {
			Some(bytes) => std::str::from_utf8(bytes)
				.map_err(|e| {
					S::Error::custom(format!("Debug buffer contains invalid UTF8: {}", e))
				})?
				.serialize(serializer),
			None => serializer.serialize_none(),
		}
	}

	/// Deserializes a UTF-8 string into a `Option<Vec<u8>>`
	pub fn deserialize<'de, D: Deserializer<'de>>(
		deserializer: D,
	) -> Result<Option<Vec<u8>>, D::Error> {
		let bytes = String::deserialize(deserializer)?.into_bytes();
		Ok(match bytes.len() {
			0 => None,
			_ => Some(bytes),
		})
	}
}

const PREFIX: &'static str = "<Bytes>";
const POSTFIX: &'static str = "</Bytes>";

/// Wraps `PREFIX` and `POSTFIX` around a `Vec<u8>`
/// Returns `PREFIX` ++ `data` ++ `POSTFIX`
pub fn wrap_binary_data(data: Vec<u8>) -> Vec<u8> {
	let mut encapsuled = PREFIX.as_bytes().to_vec();
	encapsuled.append(&mut data.clone());
	encapsuled.append(&mut POSTFIX.as_bytes().to_vec());
	encapsuled
}

#[cfg(test)]
mod tests {
	use super::*;
	use impl_serde::serialize::from_hex;
	use parity_scale_codec::{Decode, Encode};
	use scale_info::TypeInfo;
	use serde::{Deserialize, Serialize};

	#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
	#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
	struct TestAsHex {
		#[cfg_attr(feature = "std", serde(with = "as_hex",))]
		pub data: Vec<u8>,
	}

	#[test]
	fn as_hex_can_serialize() {
		let test_data = TestAsHex { data: vec![1, 2, 3, 4] };
		let result = serde_json::to_string(&test_data);
		assert!(result.is_ok());
		assert_eq!("{\"data\":\"0x01020304\"}", result.unwrap());
	}

	#[test]
	fn as_hex_can_deserialize() {
		let result: Result<TestAsHex, serde_json::Error> =
			serde_json::from_str("{\"data\":\"0x01020304\"}");
		assert!(result.is_ok());
		assert_eq!(vec![1, 2, 3, 4], result.unwrap().data);
	}

	#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
	#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
	struct TestAsHexOption {
		#[cfg_attr(
			feature = "std",
			serde(with = "as_hex_option", skip_serializing_if = "Option::is_none", default)
		)]
		pub data: Option<Vec<u8>>,
	}

	#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
	#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
	struct TestAsHexOptionNull {
		#[cfg_attr(feature = "std", serde(with = "as_hex_option", default))]
		pub data: Option<Vec<u8>>,
	}

	#[test]
	fn as_hex_option_can_serialize() {
		let test_data = TestAsHexOption { data: Some(vec![1, 2, 3, 4]) };
		let result = serde_json::to_string(&test_data);
		assert!(result.is_ok());
		assert_eq!("{\"data\":\"0x01020304\"}", result.unwrap());
	}

	#[test]
	fn as_hex_option_can_deserialize() {
		let result: Result<TestAsHexOption, serde_json::Error> =
			serde_json::from_str("{\"data\":\"0x01020304\"}");
		assert!(result.is_ok());
		assert_eq!(Some(vec![1, 2, 3, 4]), result.unwrap().data);
	}

	#[test]
	fn as_hex_option_can_serialize_nothing_with_skip() {
		let test_data = TestAsHexOption { data: None };
		let result = serde_json::to_string(&test_data);
		assert!(result.is_ok());
		assert_eq!("{}", result.unwrap());
	}

	#[test]
	fn as_hex_option_can_serialize_nothing_as_null() {
		let test_data = TestAsHexOptionNull { data: None };
		let result = serde_json::to_string(&test_data);
		assert!(result.is_ok());
		assert_eq!("{\"data\":null}", result.unwrap());
	}

	#[test]
	fn as_hex_option_can_deserialize_nothing() {
		let result: Result<TestAsHexOption, serde_json::Error> = serde_json::from_str("{}");
		assert!(result.is_ok());
		assert_eq!(None, result.unwrap().data);
	}

	#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
	#[derive(Default, Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
	struct TestAsString {
		#[cfg_attr(feature = "std", serde(with = "as_string"))]
		pub data: Vec<u8>,
	}

	#[test]
	fn as_string_can_serialize() {
		let test_data = TestAsString {
			data: vec![
				0xe8, 0x95, 0x99, 0x49, 0xdd, 0x9d, 0xcd, 0x99, 0xe0, 0xbc, 0x8d, 0x4c, 0xd0, 0xbc,
			],
		};
		let result = serde_json::to_string(&test_data);
		assert!(result.is_ok());
		assert_eq!("{\"data\":\"蕙Iݝ͙།Lм\"}", result.unwrap());
	}

	#[test]
	fn as_string_can_deserialize() {
		let result: Result<TestAsString, serde_json::Error> =
			serde_json::from_str("{\"data\":\"蕙Iݝ͙།Lм\"}");
		assert!(result.is_ok());
		assert_eq!(
			vec![
				0xe8, 0x95, 0x99, 0x49, 0xdd, 0x9d, 0xcd, 0x99, 0xe0, 0xbc, 0x8d, 0x4c, 0xd0, 0xbc
			],
			result.unwrap().data
		);
	}

	#[test]
	fn as_string_errors_for_bad_utf8_vec() {
		let test_data = TestAsString { data: vec![0xc3, 0x28] };
		let result = serde_json::to_string(&test_data);
		assert!(result.is_err());
	}

	#[test]
	fn as_string_errors_for_bad_utf8_str() {
		let result: Result<TestAsString, serde_json::Error> =
			serde_json::from_str("{\"data\":\"\\xa0\\xa1\"}");
		assert!(result.is_err());
	}

	#[test]
	fn get_chain_type_by_genesis_hash_with_mainnet_genesis_should_get_mainnet() {
		// arrange
		let known_genesis =
			from_hex("4a587bf17a404e3572747add7aab7bbe56e805a5479c6c436f07f36fcc8d3ae1").unwrap();

		// act
		let detected = get_chain_type_by_genesis_hash(&known_genesis);

		// assert
		assert_eq!(detected, DetectedChainType::FrequencyMainNet);
	}

	#[test]
	fn get_chain_type_by_genesis_hash_with_paseo_genesis_should_get_paseo() {
		// arrange
		let known_genesis =
			from_hex("203c6838fc78ea3660a2f298a58d859519c72a5efdc0f194abd6f0d5ce1838e0").unwrap();

		// act
		let detected = get_chain_type_by_genesis_hash(&known_genesis);

		// assert
		assert_eq!(detected, DetectedChainType::FrequencyPaseoTestNet);
	}
}