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},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ProofDeployment {
pub chain_id: u64,
pub address: Address,
pub environment: Option<EscrowEnvironment>,
}
const fn retired(chain_id: u64, address: Address) -> ProofDeployment {
ProofDeployment { chain_id, address, environment: None }
}
pub const RETIRED_V5_DEPLOYMENTS: [ProofDeployment; 4] = [
retired(56, address!("3100b7b49c0f9631c6af25877b5a4e13a47afad6")),
retired(999, address!("ea5ba9e1c0f5dde572bc08f3af6a3e8b9db73734")),
retired(137, address!("ea5ba9e1c0f5dde572bc08f3af6a3e8b9db73734")),
retired(56, address!("ea5ba9e1c0f5dde572bc08f3af6a3e8b9db73734")),
];
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
}
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;