lamarge 0.7.0

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
//! Escrow proofs: the agreements a quote set, a funding or a payment was signed under, checked
//! against every v5 deployment that may hold one, retired proxies included.
//!
//! A retired proxy is trusted for the proofs it already holds, never for a new signature: the
//! flows sign only for the active escrows.

use alloy_primitives::{Address, address};
use serde_json::Value;

use crate::{
    deployments,
    error::VerificationError,
    protocol::{
        checks::details::{self, deployment, differs},
        eip712::{self, FundOffer, PayTake, QuoteSet},
    },
    types::{EscrowEnvironment, VenueId},
};

/// A reviewed v5 escrow proxy a proof may name, retired ones included: never an implementation or
/// an address the API supplies. [`EscrowDeployment`](super::EscrowDeployment) lists only the
/// active ones, those new signatures go to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ProofDeployment {
    /// Its chain.
    pub chain_id: u64,
    /// The proxy.
    pub address: Address,
    /// The environment of an active proxy; `None` for a retired one.
    pub environment: Option<EscrowEnvironment>,
}

const fn retired(chain_id: u64, address: Address) -> ProofDeployment {
    ProofDeployment { chain_id, address, environment: None }
}

/// Retired v5 proxies: trusted for existing proofs, never new signatures.
pub const RETIRED_V5_DEPLOYMENTS: [ProofDeployment; 4] = [
    retired(56, address!("3100b7b49c0f9631c6af25877b5a4e13a47afad6")),
    retired(999, address!("ea5ba9e1c0f5dde572bc08f3af6a3e8b9db73734")),
    retired(137, address!("ea5ba9e1c0f5dde572bc08f3af6a3e8b9db73734")),
    retired(56, address!("ea5ba9e1c0f5dde572bc08f3af6a3e8b9db73734")),
];

/// Every proxy a proof may name: the active ones of each environment, then the retired ones.
pub const ESCROW_PROOF_DEPLOYMENTS: [ProofDeployment; 10] = proof_deployments();

const fn proof_deployments() -> [ProofDeployment; 10] {
    let mut out = [retired(0, Address::ZERO); 10];
    let mut i = 0;
    while i < deployments::ESCROWS.len() {
        let active = deployments::ESCROWS[i];
        out[i] = ProofDeployment {
            chain_id: active.chain_id,
            address: active.address,
            environment: Some(active.environment),
        };
        i += 1;
    }
    let mut j = 0;
    while j < RETIRED_V5_DEPLOYMENTS.len() {
        out[i + j] = RETIRED_V5_DEPLOYMENTS[j];
        j += 1;
    }
    out
}

/// Checks `typed`, a quote set, a funding or a payment on `venue`, as a proof: exact EIP-712
/// field types, LaMarge's domain on `venue`'s chain, and a contract among
/// [`ESCROW_PROOF_DEPLOYMENTS`]. Returns that deployment.
pub fn check_escrow_proof_typed(
    typed: &Value,
    venue: &VenueId,
) -> Result<ProofDeployment, VerificationError> {
    let primary = typed.get("primaryType").and_then(Value::as_str);
    if !matches!(primary, Some("QuoteSet" | "FundOffer" | "PayTake")) {
        return Err(differs(details::PRIMARY_TYPE));
    }
    let chain = deployments::chain(venue).ok_or(differs(details::VENUE))?;
    let escrow = typed
        .pointer("/domain/verifyingContract")
        .and_then(Value::as_str)
        .and_then(eip712::address_text)
        .ok_or(differs(details::DOMAIN))?;
    let found = ESCROW_PROOF_DEPLOYMENTS
        .into_iter()
        .find(|known| known.chain_id == chain.id && known.address == escrow)
        .ok_or(deployment(details::ESCROW))?;
    let domain = eip712::lamarge_domain(chain.id, escrow);
    match primary {
        Some("QuoteSet") => eip712::parse_in::<QuoteSet>(typed, &domain).map(|_| ()),
        Some("FundOffer") => eip712::parse_in::<FundOffer>(typed, &domain).map(|_| ()),
        _ => eip712::parse_in::<PayTake>(typed, &domain).map(|_| ()),
    }?;
    Ok(found)
}

#[cfg(test)]
mod tests;