arbos/util/
serialization.rs1use std::io::{self, Read, Write};
2
3use alloy_primitives::{Address, B256, U256};
4
5pub fn hash_from_reader<R: Read>(r: &mut R) -> io::Result<B256> {
7 let mut buf = [0u8; 32];
8 r.read_exact(&mut buf)?;
9 Ok(B256::from(buf))
10}
11
12pub fn hash_to_writer<W: Write>(w: &mut W, hash: &B256) -> io::Result<()> {
14 w.write_all(hash.as_slice())
15}
16
17pub fn uint256_from_reader<R: Read>(r: &mut R) -> io::Result<U256> {
19 let hash = hash_from_reader(r)?;
20 Ok(U256::from_be_bytes(hash.0))
21}
22
23pub fn address_from_reader<R: Read>(r: &mut R) -> io::Result<Address> {
25 let mut buf = [0u8; 20];
26 r.read_exact(&mut buf)?;
27 Ok(Address::from(buf))
28}
29
30pub fn address_to_writer<W: Write>(w: &mut W, addr: &Address) -> io::Result<()> {
32 w.write_all(addr.as_slice())
33}
34
35pub fn address_from_256_from_reader<R: Read>(r: &mut R) -> io::Result<Address> {
37 let hash = hash_from_reader(r)?;
38 Ok(Address::from_slice(&hash[12..]))
39}
40
41pub fn address_to_256_to_writer<W: Write>(w: &mut W, addr: &Address) -> io::Result<()> {
43 let mut buf = [0u8; 32];
44 buf[12..].copy_from_slice(addr.as_slice());
45 w.write_all(&buf)
46}
47
48pub fn uint64_from_reader<R: Read>(r: &mut R) -> io::Result<u64> {
50 let mut buf = [0u8; 8];
51 r.read_exact(&mut buf)?;
52 Ok(u64::from_be_bytes(buf))
53}
54
55pub fn uint64_to_writer<W: Write>(w: &mut W, val: u64) -> io::Result<()> {
57 w.write_all(&val.to_be_bytes())
58}
59
60pub fn bytestring_from_reader<R: Read>(r: &mut R, max_bytes_to_read: u64) -> io::Result<Vec<u8>> {
65 let len_u64 = uint64_from_reader(r)?;
66 if len_u64 > max_bytes_to_read {
67 return Err(io::Error::new(
68 io::ErrorKind::InvalidData,
69 format!("byte-string length {len_u64} exceeds max {max_bytes_to_read}"),
70 ));
71 }
72 let len = len_u64 as usize;
73 let mut buf = vec![0u8; len];
74 r.read_exact(&mut buf)?;
75 Ok(buf)
76}
77
78pub fn bytestring_to_writer<W: Write>(w: &mut W, data: &[u8]) -> io::Result<()> {
80 uint64_to_writer(w, data.len() as u64)?;
81 w.write_all(data)
82}
83
84pub fn int_to_hash(val: i64) -> B256 {
86 let mut buf = [0u8; 32];
87 if val >= 0 {
88 buf[24..].copy_from_slice(&(val as u64).to_be_bytes());
89 } else {
90 buf.fill(0xFF);
92 buf[24..].copy_from_slice(&(val as u64).to_be_bytes());
93 }
94 B256::from(buf)
95}
96
97pub fn uint_to_hash(val: u64) -> B256 {
99 let mut buf = [0u8; 32];
100 buf[24..].copy_from_slice(&val.to_be_bytes());
101 B256::from(buf)
102}
103
104pub fn address_to_hash(addr: &Address) -> B256 {
106 let mut buf = [0u8; 32];
107 buf[12..].copy_from_slice(addr.as_slice());
108 B256::from(buf)
109}