reth_codecs/alloy/signature.rs
1//! Compact implementation for [`Signature`]
2
3use crate::Compact;
4use alloy_primitives::{PrimitiveSignature as Signature, U256};
5
6impl Compact for Signature {
7 fn to_compact<B>(&self, buf: &mut B) -> usize
8 where
9 B: bytes::BufMut + AsMut<[u8]>,
10 {
11 buf.put_slice(&self.r().as_le_bytes());
12 buf.put_slice(&self.s().as_le_bytes());
13 self.v() as usize
14 }
15
16 fn from_compact(mut buf: &[u8], identifier: usize) -> (Self, &[u8]) {
17 use bytes::Buf;
18 assert!(buf.len() >= 64);
19 let r = U256::from_le_slice(&buf[0..32]);
20 let s = U256::from_le_slice(&buf[32..64]);
21 buf.advance(64);
22 (Self::new(r, s, identifier != 0), buf)
23 }
24}