caelum0x af5d96ce91 feat(near-spend-guard): add SpendGuard contract — policy, exposure cap, capped spend key, circuit breaker
New near-sdk-rs contract (contracts-near/spend_guard/, separate workspace,
untouched contracts/Cargo.toml) for the NEAR Hackathon 2026 Agent Economy /
AI Security & Defense tracks:

- policy.rs: policyV1 ported from contracts/risk_policy_manager/src/lib.rs
  (Casper/Odra), fixing a real unit bug caught before shipping — porting
  Casper's CSPR motes divisor (1e9) unchanged against yoctoNEAR (1e24)
  would silently saturate every agent's stake multiplier at its ceiling,
  not floor it. Numerically cross-checked against test/risk_policy.test.ts's
  policyV1 fixture (credit_line matches once units align).
- exposure.rs: ExposureBook, a fresh Rust port of
  lib/ledger/contracts/global_exposure_manager.ts's GlobalExposureManager
  (reserve/activate/release/decrease/freeze), no prior Rust crate existed.
- spend_key.rs / lib.rs::spend_to_seller: mints a real Function Call Access
  Key (allowance = approved credit line) scoped to this contract's own
  spend_to_seller method. Documents a real NEAR protocol constraint found
  while building this and confirmed live against a local nearcore sandbox:
  FC keys can never attach a deposit (InvalidAccessKeyError::
  DepositWithFunctionCall), so the yoctoNEAR cap is enforced by
  ExposureBook while the FC key's allowance caps real, protocol-level
  prepaid-gas call budget, and scope/receiver/method restriction.
  Also documents why the live key is minted on the contract's own account
  rather than the agent's identity sub-account: NEAR only allows
  AddKey/DeleteKey as same-account self-actions outside of an account's own
  creation batch.
- scam_detection.rs: dispute-spike circuit breaker — freezes ExposureBook
  and deletes the live spend key, independent of remaining budget.
- tests/sandbox.rs: a real near-workspaces sandbox test against a live
  local nearcore node (not mocked) driving the full loop — deploy, policy,
  38 real capped spends, a genuine nearcore NotEnoughAllowance rejection,
  a dispute spike, freeze, and a genuine post-freeze AccessKeyNotFound
  rejection. `cargo test` (14 unit tests + this) is all green.

Pre-existing x402-over-NEAR payment rail (near_gateway.ts, near_finality.ts,
near_rpc.ts, etc., commit c80dda9) is untouched — this is new, separate
hackathon-window work.
2026-11-03 04:50:51 +03:00

30 lines
673 B
TOML

[package]
name = "spend_guard"
version = "0.1.0"
edition = "2021"
description = "NEAR Hackathon 2026 — SpendGuard: turns an agent's verified on-chain x402 revenue into a capped, protocol-enforced spending permission via NEAR Function Call Access Keys."
license = "Apache-2.0"
publish = false
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
near-sdk = "5"
borsh = "1"
[dev-dependencies]
near-sdk = { version = "5", features = ["unit-testing"] }
near-workspaces = "0.24"
tokio = { version = "1", features = ["full"] }
serde_json = "1"
anyhow = "1"
[profile.release]
codegen-units = 1
opt-level = "z"
lto = true
debug = false
panic = "abort"
overflow-checks = true