1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Provide serialization functions for various types and formats.
18

            
19
use ethereum_types::{H256, U256};
20
use serde::{
21
	ser::{Error, SerializeSeq},
22
	Serializer,
23
};
24
use sp_runtime::traits::UniqueSaturatedInto;
25

            
26
pub fn seq_h256_serialize<S>(data: &Option<Vec<H256>>, serializer: S) -> Result<S::Ok, S::Error>
27
where
28
	S: Serializer,
29
{
30
	if let Some(vec) = data {
31
		let mut seq = serializer.serialize_seq(Some(vec.len()))?;
32
		for hash in vec {
33
			seq.serialize_element(&format!("{:x}", hash))?;
34
		}
35
		seq.end()
36
	} else {
37
		let seq = serializer.serialize_seq(Some(0))?;
38
		seq.end()
39
	}
40
}
41

            
42
pub fn bytes_0x_serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
43
where
44
	S: Serializer,
45
{
46
	serializer.serialize_str(&format!("0x{}", hex::encode(bytes)))
47
}
48

            
49
pub fn option_bytes_0x_serialize<S>(
50
	bytes: &Option<Vec<u8>>,
51
	serializer: S,
52
) -> Result<S::Ok, S::Error>
53
where
54
	S: Serializer,
55
{
56
	if let Some(bytes) = bytes.as_ref() {
57
		return serializer.serialize_str(&format!("0x{}", hex::encode(&bytes[..])));
58
	}
59
	Err(S::Error::custom("String serialize error."))
60
}
61

            
62
pub fn opcode_serialize<S>(opcode: &[u8], serializer: S) -> Result<S::Ok, S::Error>
63
where
64
	S: Serializer,
65
{
66
	let d = std::str::from_utf8(opcode)
67
		.map_err(|_| S::Error::custom("Opcode serialize error."))?
68
		.to_uppercase();
69
	serializer.serialize_str(&d)
70
}
71

            
72
pub fn string_serialize<S>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error>
73
where
74
	S: Serializer,
75
{
76
	let d = std::str::from_utf8(value)
77
		.map_err(|_| S::Error::custom("String serialize error."))?
78
		.to_string();
79
	serializer.serialize_str(&d)
80
}
81

            
82
pub fn option_string_serialize<S>(value: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
83
where
84
	S: Serializer,
85
{
86
	if let Some(value) = value.as_ref() {
87
		let d = std::str::from_utf8(&value[..])
88
			.map_err(|_| S::Error::custom("String serialize error."))?
89
			.to_string();
90
		return serializer.serialize_str(&d);
91
	}
92
	Err(S::Error::custom("String serialize error."))
93
}
94

            
95
pub fn u256_serialize<S>(data: &U256, serializer: S) -> Result<S::Ok, S::Error>
96
where
97
	S: Serializer,
98
{
99
	serializer.serialize_u64(UniqueSaturatedInto::<u64>::unique_saturated_into(*data))
100
}
101

            
102
pub fn h256_serialize<S>(data: &H256, serializer: S) -> Result<S::Ok, S::Error>
103
where
104
	S: Serializer,
105
{
106
	serializer.serialize_str(&format!("{:x}", data))
107
}
108

            
109
pub fn h256_0x_serialize<S>(data: &H256, serializer: S) -> Result<S::Ok, S::Error>
110
where
111
	S: Serializer,
112
{
113
	serializer.serialize_str(&format!("0x{:x}", data))
114
}