lamarge 0.9.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
//! Escrow typed-data and signing-domain checks.

use alloy_sol_types::{Eip712Domain, SolStruct};
use serde_json::Value;

use super::details::{self, differs, unsupported};
use crate::{
    deployments,
    error::VerificationError,
    protocol::eip712::{self, Parsed, QuoteSet, Schema},
    types::{EscrowEnvironment, VenueId},
};

/// The domain `venue`'s escrow requests are signed in: `environment`'s escrow on the venue's
/// chain, or the signing domain of a venue settled inside itself. Never another environment's
/// escrow, a retired proxy, or an address the API supplies.
pub(crate) fn signing_domain(
    venue: &VenueId,
    environment: EscrowEnvironment,
) -> Result<Eip712Domain, VerificationError> {
    let chain = deployments::chain(venue).ok_or(unsupported(details::VENUE))?;
    let contract = deployments::contract(venue, environment).ok_or(unsupported(details::VENUE))?;
    Ok(eip712::lamarge_domain(chain.id, contract))
}

/// Reads `raw` as an escrow request of schema `T` on `venue`, signed in its pinned domain
/// ([`signing_domain`]); a venue settled inside itself signs quote sets only. Returns the parsed
/// request and the pinned domain to sign it in.
pub(crate) fn check_escrow_typed<T: Schema>(
    raw: &Value,
    venue: &VenueId,
    environment: EscrowEnvironment,
) -> Result<(Parsed<T>, Eip712Domain), VerificationError> {
    if deployments::is_venue_settled(venue) && T::PRIMARY != QuoteSet::NAME {
        return Err(differs(details::PRIMARY_TYPE));
    }
    let domain = signing_domain(venue, environment)?;
    Ok((eip712::parse_in::<T>(raw, &domain)?, domain))
}

#[cfg(test)]
mod tests;