Skip to main content

uts_core/codec/imp/
alloy.rs

1use crate::codec::{Decode, DecodeError, Decoder, Encode, EncodeError, Encoder};
2use alloy_chains::Chain;
3use alloy_primitives::{Address, ChainId, FixedBytes};
4
5impl<const N: usize> Encode for FixedBytes<N> {
6    fn encode(&self, encoder: &mut impl Encoder) -> Result<(), EncodeError> {
7        encoder.write_all(self)
8    }
9}
10
11impl<const N: usize> Decode for FixedBytes<N> {
12    fn decode(decoder: &mut impl Decoder) -> Result<Self, DecodeError> {
13        let mut buf = [0u8; N];
14        decoder.read_exact(&mut buf)?;
15        Ok(Self::new(buf))
16    }
17}
18
19impl Encode for Address {
20    fn encode(&self, encoder: &mut impl Encoder) -> Result<(), EncodeError> {
21        encoder.write_all(self.0)
22    }
23}
24
25impl Decode for Address {
26    fn decode(decoder: &mut impl Decoder) -> Result<Self, DecodeError> {
27        let inner: FixedBytes<20> = decoder.decode()?;
28        Ok(Self::from(inner))
29    }
30}
31
32impl Encode for Chain {
33    fn encode(&self, encoder: &mut impl Encoder) -> Result<(), EncodeError> {
34        self.id().encode(encoder)
35    }
36}
37
38impl Decode for Chain {
39    fn decode(decoder: &mut impl Decoder) -> Result<Self, DecodeError> {
40        let id: ChainId = decoder.decode()?;
41        Ok(Chain::from_id(id))
42    }
43}