lamarge 0.5.1

Client for the LaMarge API: sign-in, takes, transfers, withdrawals and market making, each request checked before it is signed.
Documentation
use serde_json::{Value, json};

use super::*;
use crate::{
    error::VerificationKind,
    testing::{MAKER, NOW, OTHER, OWNER, at, edited, fixtures, replaced},
};

fn step(value: &Value) -> SetupStep {
    fixtures::model(value)
}

fn check_at(
    value: &Value,
    venue: &str,
    environment: EscrowEnvironment,
    now: i64,
) -> Result<Verified, VerificationError> {
    let owners = fixtures::lighter_owners();
    check_setup_step(
        &step(value),
        &venue.parse().unwrap(),
        OWNER,
        Some(42),
        &owners,
        environment,
        at(now),
    )
}

fn check(value: &Value, venue: &str) -> Result<Verified, VerificationError> {
    check_at(value, venue, EscrowEnvironment::Production, 0)
}

/// `consents.test.ts` "a setup step asks the one kind of signature it is signed with".
#[test]
fn a_setup_step_asks_the_one_kind_of_signature_it_is_signed_with() {
    assert!(check(&fixtures::permit_step(), "hyperliquid").is_ok());
    assert!(check(&fixtures::approval_step(), "aster").is_ok());
    assert!(check(&fixtures::registration_step(), "lighter").is_ok());
    for (base, step) in [
        (fixtures::permit_step(), "agent"),
        (fixtures::approval_step(), "permit"),
        (fixtures::registration_step(), "permit"),
    ] {
        let other = edited(base, |s| s["step"] = json!(step));
        assert_eq!(check(&other, "aster").unwrap_err().detail, details::SETUP_KIND);
    }
    let unknown = edited(fixtures::permit_step(), |s| s["step"] = json!("other"));
    assert_eq!(check(&unknown, "hyperliquid").unwrap_err().kind, VerificationKind::Unsupported);
    // An agent belongs to Aster, a key to Lighter.
    assert_eq!(
        check(&fixtures::approval_step(), "lighter").unwrap_err().detail,
        details::SETUP_STEP
    );
    assert_eq!(
        check(&fixtures::registration_step(), "aster").unwrap_err().detail,
        details::SETUP_STEP
    );
    let signer = edited(fixtures::permit_step(), |s| s["to_sign"]["signer"] = json!(OTHER));
    assert_eq!(check(&signer, "hyperliquid").unwrap_err().detail, details::TO_SIGN_SIGNER);
    let missing = edited(fixtures::permit_step(), |s| s["to_sign"] = Value::Null);
    assert_eq!(check(&missing, "hyperliquid").unwrap_err().detail, details::TO_SIGN);
    let untyped = edited(fixtures::permit_step(), |s| {
        fixtures::object(&mut s["to_sign"]).remove("typed_data");
    });
    assert_eq!(check(&untyped, "hyperliquid").unwrap_err().detail, details::TYPED_DATA);
    let unmessaged = edited(fixtures::registration_step(), |s| {
        fixtures::object(&mut s["to_sign"]).remove("message");
    });
    assert_eq!(check(&unmessaged, "lighter").unwrap_err().detail, details::MESSAGE);
}

/// `intents.test.ts` "only the unlimited permit for the pinned escrow is signed"; `wallet.test.ts`
/// "a wallet permit cannot authorize another spender".
#[test]
fn only_the_unlimited_permit_to_the_environments_escrow_is_signed() {
    let verified = check(&fixtures::permit_step(), "hyperliquid").unwrap();
    // The vector's deadline is ten minutes ahead: signable until 30 seconds before it.
    assert_eq!(verified.not_after(), Some(NOW + 570));
    let message = |name: &str, value: Value| {
        edited(fixtures::permit_step(), |s| {
            s["to_sign"]["typed_data"]["message"][name] = value.clone();
        })
    };
    let cases = [
        (message("value", json!("10000000")), details::VALUE),
        (message("spender", json!(OTHER)), details::SPENDER),
        (message("spender", json!("0x948e65c40442d935d4ee990bdbd01bdd825c4873")), details::SPENDER),
        (message("owner", json!(OTHER)), details::OWNER),
        (message("deadline", json!((NOW + 631).to_string())), details::DEADLINE),
        (message("deadline", json!(NOW.to_string())), details::DEADLINE),
    ];
    for (index, (value, detail)) in cases.iter().enumerate() {
        assert_eq!(check(value, "hyperliquid").unwrap_err().detail, *detail, "case {index}");
    }
    assert!(check(&message("deadline", json!((NOW + 630).to_string())), "hyperliquid").is_ok());
    // Another token's domain, or the permit asked on another chain.
    let domain = edited(fixtures::permit_step(), |s| {
        s["to_sign"]["typed_data"]["domain"]["version"] = json!("1");
    });
    assert_eq!(check(&domain, "hyperliquid").unwrap_err().detail, details::DOMAIN);
    assert_eq!(check(&fixtures::permit_step(), "polymarket").unwrap_err().detail, details::DOMAIN);
    assert_eq!(
        check(&fixtures::permit_step(), "lighter").unwrap_err().kind,
        VerificationKind::Unsupported
    );
    // Development permits development's escrow only.
    let development = |raw: &Value| check_at(raw, "hyperliquid", EscrowEnvironment::Development, 0);
    assert_eq!(development(&fixtures::permit_step()).unwrap_err().detail, details::SPENDER);
    assert!(
        development(&message("spender", json!("0x948e65c40442d935d4ee990bdbd01bdd825c4873")))
            .is_ok()
    );
    // The wallet permit is the same permit.
    let wallet = |value: &Value| {
        let permit: WalletPermit = fixtures::model(value);
        check_wallet_permit(
            &permit,
            &VenueId::Hyperliquid,
            OWNER,
            EscrowEnvironment::Production,
            fixtures::now(),
        )
    };
    assert!(wallet(&fixtures::wallet_permit()).is_ok());
    let spender = edited(fixtures::wallet_permit(), |p| {
        p["to_sign"]["typed_data"]["message"]["spender"] = json!(MAKER);
    });
    assert_eq!(wallet(&spender).unwrap_err().detail, details::SPENDER);
    let signer = edited(fixtures::wallet_permit(), |p| p["to_sign"]["signer"] = json!(OTHER));
    assert_eq!(wallet(&signer).unwrap_err().detail, details::TO_SIGN_SIGNER);
    let kind =
        edited(fixtures::wallet_permit(), |p| p["to_sign"]["kind"] = json!("aster_approval"));
    assert_eq!(wallet(&kind).unwrap_err().detail, details::SETUP_KIND);
}

fn approval(fields: &[(&str, String)]) -> Value {
    fixtures::approval_step_of(&fixtures::approval_message(fields))
}

fn with(name: &str, value: &str) -> Vec<(&'static str, String)> {
    replaced(fixtures::approval_fields(), name, value)
}

/// `aster.test.ts` "an agent approval is LaMarge's, for this address, only moving margin to
/// Spot, for a year".
#[test]
fn an_agent_approval_is_the_environments_for_this_address_for_a_year() {
    let verified = check(&fixtures::approval_step(), "aster").unwrap();
    assert_eq!(verified.not_after(), Some(NOW + 60));
    let without_ips: Vec<_> = fixtures::approval_fields().into_iter().take(9).collect();
    assert!(check(&approval(&without_ips), "aster").is_ok());
    let nonce = fixtures::approval_fields()[1].1.parse::<u64>().unwrap();
    let expired = (nonce / 1000 + APPROVAL_MILLIS).to_string();
    let cases = [
        (with("canWithdraw", "true"), details::AGENT_PERMISSIONS),
        (with("canPerpTrade", "false"), details::AGENT_PERMISSIONS),
        (with("canSpotTrade", "true"), details::AGENT_PERMISSIONS),
        (with("ipWhitelist", ""), details::AGENT_PERMISSIONS),
        (with("signatureChainId", "42161"), details::AGENT_PERMISSIONS),
        (with("user", &format!("{OTHER:#x}")), details::AGENT_USER),
        (with("agentName", "Other"), details::AGENT),
        (with("agentName", "@aster-desktop"), details::AGENT),
        (with("agentName", "LaMargeProd"), details::AGENT),
        (with("agentName", "LaMargeWithdrawal"), details::AGENT),
        // The development agent, approved in production, would revoke development's.
        (with("agentName", "LaMargeDev"), details::AGENT),
        (with("agentAddress", &format!("{OWNER:#x}")), details::AGENT),
        (with("agentAddress", "0x1234"), details::AGENT),
        (
            with("expired", &(expired.parse::<u64>().unwrap() + 1).to_string()),
            details::AGENT_EXPIRY,
        ),
        (with("nonce", &(nonce + 61_000_000).to_string()), details::AGENT_NONCE),
    ];
    for (index, (fields, detail)) in cases.iter().enumerate() {
        assert_eq!(check(&approval(fields), "aster").unwrap_err().detail, *detail, "case {index}");
    }
    let mut extra = fixtures::approval_fields();
    extra.push(("agentCode", "ref".to_owned()));
    assert_eq!(check(&approval(&extra), "aster").unwrap_err().detail, details::AGENT_APPROVAL);
    let mut reordered = fixtures::approval_fields();
    reordered.swap(0, 1);
    assert_eq!(check(&approval(&reordered), "aster").unwrap_err().detail, details::AGENT_APPROVAL);
    // The nonce signed is the one prepared, and within a minute.
    let reference =
        edited(fixtures::approval_step(), |s| s["to_sign"]["ref"] = json!("1790000000000001"));
    assert_eq!(check(&reference, "aster").unwrap_err().detail, details::AGENT_NONCE);
    let late = check_at(&fixtures::approval_step(), "aster", EscrowEnvironment::Production, 61);
    assert_eq!(late.unwrap_err().kind, VerificationKind::Stale);
    let elsewhere = edited(fixtures::approval_step(), |s| {
        s["to_sign"]["typed_data"]["domain"]["chainId"] = json!(42161);
    });
    assert_eq!(check(&elsewhere, "aster").unwrap_err().detail, details::DOMAIN);
    // Development approves its own agent only.
    let development = |value: &Value| check_at(value, "aster", EscrowEnvironment::Development, 0);
    assert!(development(&approval(&with("agentName", "LaMargeDev"))).is_ok());
    assert_eq!(development(&fixtures::approval_step()).unwrap_err().detail, details::AGENT);
}

/// `aster.test.ts` "an agent approval that lists addresses lists a few, narrow, public ones".
#[test]
fn listed_addresses_are_few_narrow_and_public() {
    let eight = ["203.0.113.7"; 8].join(",");
    for ips in [
        "203.0.113.7",
        "203.0.113.7,198.51.100.0/24",
        "1.2.3.4 10.0.0.0/24",
        "2001:db8::1",
        "2001:db8:1:2::/64",
        "2001:db8:0:0:0:0:0:1/128",
        &eight,
    ] {
        assert!(narrow_addresses(ips), "{ips}");
        assert!(check(&approval(&with("ipWhitelist", ips)), "aster").is_ok(), "{ips}");
    }
    let nine = ["203.0.113.7"; 9].join(",");
    for ips in [
        "0.0.0.0/0",
        "203.0.113.0/23",
        "0.1.2.3",
        "224.0.0.1",
        "255.255.255.255",
        "::/0",
        "::1",
        "::ffff:1.2.3.4",
        "fe80::1",
        "2001:db8::/63",
        "2001:db8::1%eth0",
        "1.2.3",
        "203.0.113.7/024",
        "203.0.113.7/24/1",
        &nine,
        "2001:db8::1::2",
        "4001::1",
        "01.2.3.4",
        "1.2.3.4,",
        "2001:db8:0:0:0:0:0:0:1",
    ] {
        assert!(!narrow_addresses(ips), "{ips}");
    }
    let wide = approval(&with("ipWhitelist", "0.0.0.0/0"));
    assert_eq!(check(&wide, "aster").unwrap_err().detail, details::AGENT_ADDRESSES);
}

/// `account.test.ts` "Lighter's key is registered by the owner's signature of Lighter's own
/// message", "… at LaMarge's development index, never at another slot of the account, nor at
/// the other environment's", "a Lighter account that Lighter says is someone else's gets no
/// key registration".
#[test]
fn a_lighter_key_is_registered_by_lighters_own_message_on_the_owners_account() {
    let verified = check(&fixtures::registration_step(), "lighter").unwrap();
    let message = lighter::registration_message(42, 254, &fixtures::LIGHTER_KEY, 9);
    assert_eq!(verified.digest(), alloy_primitives::eip191_hash_message(message.as_bytes()));
    assert_eq!(verified.not_after(), None);
    let registering = |account: u64, key: u8, nonce: u64, reference: &str| {
        let message = lighter::registration_message(account, key, &fixtures::LIGHTER_KEY, nonce);
        edited(fixtures::registration_step(), |s| {
            s["to_sign"]["message"] = json!(message);
            s["to_sign"]["ref"] = json!(reference);
        })
    };
    for (value, detail) in [
        (registering(42, 253, 9, "9"), details::LIGHTER_REGISTRATION),
        (registering(42, 4, 9, "9"), details::LIGHTER_REGISTRATION),
        (registering(43, 254, 9, "9"), details::LIGHTER_REGISTRATION),
        (registering(42, 254, 9, "10"), details::LIGHTER_REGISTRATION),
        (registering(42, 254, 9, "+9"), details::LIGHTER_REGISTRATION),
    ] {
        assert_eq!(check(&value, "lighter").unwrap_err().detail, detail);
    }
    let upper = edited(fixtures::registration_step(), |s| {
        let message = s["to_sign"]["message"].as_str().unwrap().replace("0707", "0A07");
        s["to_sign"]["message"] = json!(message);
    });
    assert_eq!(check(&upper, "lighter").unwrap_err().detail, details::LIGHTER_REGISTRATION);
    let development =
        check_at(&registering(42, 253, 9, "9"), "lighter", EscrowEnvironment::Development, 0);
    assert!(development.is_ok());
    // Lighter's word on the account, never LaMarge's record of the link.
    let someone = |owners: &LighterOwners| {
        check_setup_step(
            &step(&fixtures::registration_step()),
            &VenueId::Lighter,
            OWNER,
            Some(42),
            owners,
            EscrowEnvironment::Production,
            fixtures::now(),
        )
    };
    assert_eq!(
        someone(&LighterOwners::from([(42, MAKER)])).unwrap_err().detail,
        details::LIGHTER_OWNER
    );
    assert_eq!(someone(&LighterOwners::new()).unwrap_err().detail, details::LIGHTER_OWNER);
    let unlinked = check_setup_step(
        &step(&fixtures::registration_step()),
        &VenueId::Lighter,
        OWNER,
        None,
        &fixtures::lighter_owners(),
        EscrowEnvironment::Production,
        fixtures::now(),
    );
    let error = unlinked.unwrap_err();
    assert_eq!((error.kind, error.detail), (VerificationKind::Changed, details::LINKED_ACCOUNT));
}