alloy_consensus/
encodable_signature.rsuse alloy_primitives::{Parity, SignatureError, U256};
pub trait EncodableSignature: Sized {
fn from_rs_and_parity<P: TryInto<Parity, Error = E>, E: Into<SignatureError>>(
r: U256,
s: U256,
parity: P,
) -> Result<Self, SignatureError>;
fn r(&self) -> U256;
fn s(&self) -> U256;
fn v(&self) -> Parity;
fn with_parity<T: Into<Parity>>(self, parity: T) -> Self;
#[inline]
fn with_chain_id(self, chain_id: u64) -> Self
where
Self: Copy,
{
self.with_parity(self.v().with_chain_id(chain_id))
}
fn with_parity_bool(self) -> Self
where
Self: Copy,
{
self.with_parity(self.v().to_parity_bool())
}
fn decode_rlp_vrs(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> {
use alloy_rlp::Decodable;
let parity: Parity = Decodable::decode(buf)?;
let r = Decodable::decode(buf)?;
let s = Decodable::decode(buf)?;
Self::from_rs_and_parity(r, s, parity)
.map_err(|_| alloy_rlp::Error::Custom("attempted to decode invalid field element"))
}
fn rlp_rs_len(&self) -> usize {
alloy_rlp::Encodable::length(&self.r()) + alloy_rlp::Encodable::length(&self.s())
}
fn rlp_vrs_len(&self) -> usize {
self.rlp_rs_len() + alloy_rlp::Encodable::length(&self.v())
}
fn write_rlp_rs(&self, out: &mut dyn alloy_rlp::BufMut) {
alloy_rlp::Encodable::encode(&self.r(), out);
alloy_rlp::Encodable::encode(&self.s(), out);
}
fn write_rlp_v(&self, out: &mut dyn alloy_rlp::BufMut) {
alloy_rlp::Encodable::encode(&self.v(), out);
}
fn write_rlp_vrs(&self, out: &mut dyn alloy_rlp::BufMut) {
self.write_rlp_v(out);
self.write_rlp_rs(out);
}
}
impl EncodableSignature for alloy_primitives::Signature {
fn from_rs_and_parity<P: TryInto<Parity, Error = E>, E: Into<SignatureError>>(
r: U256,
s: U256,
parity: P,
) -> Result<Self, SignatureError> {
Self::from_rs_and_parity(r, s, parity)
}
fn r(&self) -> U256 {
self.r()
}
fn s(&self) -> U256 {
self.s()
}
fn v(&self) -> Parity {
self.v()
}
fn with_parity<T: Into<Parity>>(self, parity: T) -> Self {
self.with_parity(parity)
}
}