caelum0x f40b54c983 feat(near-spend-guard): close the revolving-credit demo gap with top_up_allowance
Adds the SpendGuard entrypoint flagged in HACKATHON.md as a known limit:
an agent with a still-ACTIVE spend key can now draw, repay, top up, and
draw again on the SAME key, not just at the ExposureBook ledger level.

- contracts-near/spend_guard/src/lib.rs: new admin-only top_up_allowance,
  re-reserving freed headroom (capped by the agent's own policy-approved
  credit line) and re-minting the agent's existing Function Call Access
  Key via spend_key::retop_spend_key.
- src/spend_key.rs: retop_spend_key — a real NEAR protocol constraint
  found and fixed live against a running nearcore sandbox: AddKey
  unconditionally rejects an already-existing public key
  (AddKeyAlreadyExists), so topping up means delete_key + re-add in the
  SAME receipt, not a second AddKey call. Also fixed a clippy
  doc_lazy_continuation nit in this file's module docs.
- src/exposure.rs: replaced two manual saturating-subtract patterns with
  u128::saturating_sub (clippy implicit_saturating_sub), and added a unit
  test proving repay-then-reserve-again reaches the full cap a second
  time.
- tests/sandbox.rs: extended the real near-workspaces sandbox integration
  test (genuinely running local nearcore node) to prove the full loop
  live: draw -> repay -> top_up_allowance -> a real spend_to_seller call
  that succeeds again on the SAME original key -> dispute spike -> freeze
  -> revoked-key rejection.
- lib/x402/near_spend_guard_client.ts: repayCreditLine + topUpAllowance
  client methods; scripts/near_spend_guard_full_demo.ts now exercises the
  same real repay/top-up/draw-again loop against the LIVE deployed public
  Testnet contract (spendguard-024e8dd6f7.testnet), not just the sandbox.
- scripts/near_spend_guard_redeploy.ts: new script to upgrade the
  already-deployed contract's wasm in place (DeployContract replaces code
  only, never storage) — used to push this change to the live Testnet
  contract without losing its existing agent/exposure state.

Also fixes a real, judge-relevant finding hit while running the above:
NEAR's own legacy public RPC (rpc.testnet.near.org) now rejects most
calls with -429 "WARNING! THIS ENDPOINT IS DEPRECATED!". Every default
RPC URL in this repo (near_networks.ts, near_spend_guard_client.ts,
near_spend_guard_deploy.ts, docs/near_x402.md's example) now defaults to
FastNear's free public RPC instead; CRED402_NEAR_RPC_URL still overrides.
Updated the one test that asserted the old default.

18/18 Rust unit tests green, cargo clippy -D warnings clean, the sandbox
integration test passes against a real local nearcore node, the live
Testnet bonus loop passes against the real public contract, and the full
TS suite is 450/450 green with a clean typecheck.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-05 19:33:05 +03:00
..
2026-06-16 02:50:52 +03:00
2026-06-16 02:50:52 +03:00
2026-06-16 02:50:52 +03:00
2026-06-16 02:50:52 +03:00