use alloy_primitives::{Address, B256};
use alloy_sol_types::SolStruct;
use super::Eip3009Payment;
use crate::exact::TransferWithAuthorization;
use crate::signature::{StructuredSignature, StructuredSignatureFormatError};
#[derive(Debug, Clone)]
pub(crate) struct SignedMessage {
pub address: Address,
pub hash: B256,
pub signature: StructuredSignature,
}
impl SignedMessage {
pub(super) fn extract(
payment: &Eip3009Payment,
domain: &alloy_sol_types::Eip712Domain,
) -> Result<Self, StructuredSignatureFormatError> {
let transfer_with_authorization = TransferWithAuthorization {
from: payment.from,
to: payment.to,
value: payment.value,
validAfter: alloy_primitives::U256::from(payment.valid_after.as_secs()),
validBefore: alloy_primitives::U256::from(payment.valid_before.as_secs()),
nonce: payment.nonce,
};
let eip712_hash = transfer_with_authorization.eip712_signing_hash(domain);
let structured_signature: StructuredSignature = StructuredSignature::try_from_bytes(
payment.signature.clone(),
payment.from,
&eip712_hash,
)?;
Ok(Self {
address: payment.from,
hash: eip712_hash,
signature: structured_signature,
})
}
}