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>
67 lines
3.0 KiB
TypeScript
67 lines
3.0 KiB
TypeScript
/**
|
|
* NEAR network identifiers for the NEAR Hackathon (Nov 1-14, 2026,
|
|
* nearhackathon.com) "Agent Economy" / "AI Security & Defense" tracks.
|
|
*
|
|
* x402's generic `Network` type is `${string}:${string}` (see
|
|
* `@x402/core/types`), so Cred402 identifies NEAR the same way the ecosystem
|
|
* identifies every other non-EVM chain it has no dedicated `@x402/*` package
|
|
* for yet: a simple `near:<network>` pair rather than a CAIP-2 `eip155:`-style
|
|
* id (NEAR has no CAIP-2 namespace registered at the time of writing).
|
|
*
|
|
* Unlike Algorand (GoPlausible facilitator) and QIE (requires an operator to
|
|
* configure a third-party facilitator), the NEAR route needs no facilitator
|
|
* at all: `near_gateway.ts` verifies settlement itself, directly against the
|
|
* configured NEAR RPC endpoint (`near_finality.ts`), the same way a
|
|
* self-hosted x402 facilitator would.
|
|
*/
|
|
|
|
export const NEAR_TESTNET_NETWORK = "near:testnet" as const;
|
|
export const NEAR_MAINNET_NETWORK = "near:mainnet" as const;
|
|
|
|
export type NearNetworkId = typeof NEAR_TESTNET_NETWORK | typeof NEAR_MAINNET_NETWORK;
|
|
export type NearNetworkName = "testnet" | "mainnet";
|
|
|
|
// `rpc.testnet.near.org` / `rpc.mainnet.near.org` are NEAR's own legacy
|
|
// public endpoints; found live (2026-11-05) to now reject most calls with
|
|
// `-429 "WARNING! THIS ENDPOINT IS DEPRECATED!"`, pointing callers at
|
|
// FastNear's free public RPC instead. Defaulting to FastNear here means a
|
|
// judge running this with no `CRED402_NEAR_RPC_URL` override does not hit
|
|
// that same rate limit. `CRED402_NEAR_RPC_URL` still overrides either way.
|
|
export const DEFAULT_NEAR_RPC_URL: Record<NearNetworkName, string> = {
|
|
testnet: "https://rpc.testnet.fastnear.com",
|
|
mainnet: "https://rpc.mainnet.fastnear.com",
|
|
};
|
|
|
|
const MAINNET_ALIASES: readonly string[] = ["mainnet", NEAR_MAINNET_NETWORK];
|
|
const TESTNET_ALIASES: readonly string[] = ["testnet", NEAR_TESTNET_NETWORK];
|
|
|
|
/** Resolve a configured network string, accepting the friendly name or the
|
|
* `near:<network>` id. Returns undefined to fail closed. */
|
|
export function nearNetworkNameFor(raw: string): NearNetworkName | undefined {
|
|
if (MAINNET_ALIASES.includes(raw)) return "mainnet";
|
|
if (TESTNET_ALIASES.includes(raw)) return "testnet";
|
|
return undefined;
|
|
}
|
|
|
|
export function nearNetworkIdFor(name: NearNetworkName): NearNetworkId {
|
|
return name === "mainnet" ? NEAR_MAINNET_NETWORK : NEAR_TESTNET_NETWORK;
|
|
}
|
|
|
|
/** NEAR account ids: 2-64 chars, lowercase alphanumeric plus `. _ -`, no
|
|
* leading/trailing separator, no consecutive separators. Implicit (hex
|
|
* public-key) accounts are a 64-character lowercase hex string, which this
|
|
* pattern also accepts. */
|
|
const NEAR_ACCOUNT_ID = /^(?=.{2,64}$)[a-z0-9]+(?:[._-][a-z0-9]+)*$/;
|
|
|
|
export function isValidNearAccountId(value: string): boolean {
|
|
return NEAR_ACCOUNT_ID.test(value);
|
|
}
|
|
|
|
/** NEAR transaction / block hashes are base58-encoded 32-byte digests
|
|
* (43-44 base58 characters). */
|
|
export const NEAR_HASH = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
|
|
export function isValidNearHash(value: string): boolean {
|
|
return NEAR_HASH.test(value);
|
|
}
|