use alloy::{primitives::Signature, sol_types::SolStruct};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignedMessage<M> {
pub message: M,
pub signature: Signature,
}
impl<M> SignedMessage<M> {
pub fn signature_bytes(&self) -> SignatureBytes {
SignatureBytes(self.signature.as_bytes())
}
pub fn message_hash<MSol>(&self) -> MessageHash
where
M: ToSolStruct<MSol>,
MSol: SolStruct,
{
MessageHash(*self.message.to_sol_struct().eip712_hash_struct())
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SignatureBytes([u8; 65]);
impl SignatureBytes {
pub fn as_bytes(&self) -> [u8; 65] {
self.0
}
}
impl std::ops::Deref for SignatureBytes {
type Target = [u8; 65];
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MessageHash([u8; 32]);
impl MessageHash {
pub fn as_bytes(&self) -> [u8; 32] {
self.0
}
}
impl std::ops::Deref for MessageHash {
type Target = [u8; 32];
fn deref(&self) -> &Self::Target {
&self.0
}
}
pub trait ToSolStruct<T: SolStruct> {
fn to_sol_struct(&self) -> T;
}
impl<T> ToSolStruct<T> for T
where
T: SolStruct + Clone,
{
fn to_sol_struct(&self) -> T {
self.clone()
}
}