Skip to content

About

Agent invoice payments on NEAR. A vault pays one signed bill per service week, even after a worker crash and retry.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Countersign

Countersign in 30 seconds

Countersign is a safety layer between an AI agent and a business's money. The agent can only ask to pay an invoice; a NEAR smart-contract vault decides whether money moves. On the signed-invoice path, it checks the supplier's signature and rejects altered invoices, unapproved payees, duplicate bills, over-budget payments, and repeat execution after a crash.

What's deployed and what runs on your machine

  • Deployed: The vault is a Rust smart contract on NEAR testnet at countersign-vault-7d8cc5331e.testnet. Testnet is NEAR's public testing network; its payment receipts are public.
  • On your machine: The agent, desk website, Ask Dave assistant, and MCP server are self-hosted. The payment host keeps signing keys there, so each business runs its own copy, like a wallet. By default, they use a disposable local NEAR chain, so trying them needs no real money.

Three ways to try it

  1. No install: Open the recorded Demo café to watch a replay without making payments.
  2. Website: After the local setup, run COUNTERSIGN_PORT=4417 npm run dev and open http://127.0.0.1:4417/#demo for a one-click café run: suppliers send signed bills, and each bill shows PAID or STOPPED with its reason. To pay your own signed bills, set up an agent vault with npm run agent:desk -- init (plus a policy and a pinned supplier key, see Agent payments in the desk), then use http://127.0.0.1:4417/#agent-payments.
  3. Terminal: After local setup, run the signed demo from the repo root. It creates a disposable local vault, pins a supplier key, and writes signed-invoice.json (ID SIGNED-001) in the printed Disposable verification records directory. It imports and pays that bill once, then stops the chain. Set DEMO_DIR to the printed directory to inspect the same bill; the CLI restarts its local chain. Repeating import and run does not pay it again.
npm run agent:signed:demo
DEMO_DIR=/absolute/path/printed/by/the/demo
COUNTERSIGN_AGENT_DIR="$DEMO_DIR" npm run agent:desk -- import "$DEMO_DIR/signed-invoice.json"
COUNTERSIGN_AGENT_DIR="$DEMO_DIR" npm run agent:desk -- run SIGNED-001
COUNTERSIGN_AGENT_DIR="$DEMO_DIR" npm run agent:desk -- ask --check "$DEMO_DIR/signed-invoice.json"
COUNTERSIGN_AGENT_DIR="$DEMO_DIR" npm run agent:desk -- ask --facts

ask --facts prints the vault's verified records and needs no key. A plain-English question (ask "How is my agent doing?") needs NEAR_AI_API_KEY and NEAR_AI_MODEL set for NEAR AI Cloud.

The demo sets a 0.1 NEAR per-bill cap and a 0.15 NEAR rolling budget for 60 minutes. For a new bill in your own vault, see merchant-signed invoices. To connect any MCP-compatible agent (one that can use Model Context Protocol tools), see MCP setup.

The parts

  • Vault contract + signed invoices: The Rust vault checks each bill's supplier signature, payee, limits, and payment history.
  • Agent: The optional NEAR AI Cloud adapter has one payment tool: request_payment for an invoice ID.
  • Desk website: A local page for importing bills, requesting payment, and reading receipts.
  • Ask Dave: A read-only assistant whose financial claims are checked against the vault's records.
  • MCP server: A local tool server that lets an MCP-compatible agent request payment by invoice ID.

Two signed bills, one payment

  1. A merchant signs two different invoices for 0.04 NEAR each under the same weekly agreement. The agent submits both.
  2. The vault settles one bill and blocks the other. We kill the payment worker after the transfer, while its last saved state is still claimed.
  3. Fresh workers retry both invoices. The merchant has received 0.04 local test NEAR in total. The retries add 0.00.

Recorded weekly payment. The merchant receives 0.04 local test NEAR and both retries add zero.

90-second narrated replay of the terminal run (MP4) · Attack results and recorded balances · Original demo post · X: @Counter_sign_

Try it from an agent. Connect a local MCP client to a disposable café vault, request both signed bills by ID, and inspect the vault receipt. MCP setup and crash test

Quick start

Try the demo café

Complete the local setup, then start the local app with COUNTERSIGN_PORT=4417 npm run dev and open Demo café. Leave COUNTERSIGN_NETWORK unset (or set it to local).

  1. Open the café to create a separate agent vault, three suppliers with pinned signing keys and payouts, a 0.5 NEAR payment cap, a 1.2 NEAR daily budget (rolling 24 hours), and a weekly Milk Co service. Rules expire after 120 minutes.
  2. Receive today's invoices, then Pay all. Signed bills arrive in shuffled order; the existing vault rules decide which payments settle. Fixture proposals are the default, with no model calls.
  3. Review Supplier ledger: each supplier's live balance increase, paid invoice count, and signed on-chain receipts. The scam payee's live balance increase remains zero. Initial account funding is excluded. Agent payments shows the payment outcomes and reasons; Rejected at intake shows the tampered signature rejection.
  4. Ask Dave why opens the floating panel with “How is my agent doing?”. Without a model key, Dave shows the existing no-key message.

Receive again for fresh invoice IDs. Top up vault (+5 test NEAR) adds exactly 5 to the vault's available funds. It does not raise the daily budget (rolling 24 hours): once that is spent, the café says so and later bills stay blocked until earlier payments are 24 hours old, or you reset. Reset café stops the old local chain and moves the entire agent directory to work/agent-vault.bak-<timestamp>-<suffix> before starting fresh; it never deletes the old records. If an agent vault already exists, use reset to preserve it before setup. Keys remain in ignored work/ files and never reach the browser. Café routes and navigation are disabled on other networks.

Receipts show contract state, attempt IDs, and signed payload hashes; the shared CLI transport does not retain transaction hashes. Supplier identities use the existing merchant IDs (Bean-Co, Milk-Co, Pastry-Co) in Agent payments and Ask Dave, with friendly names on the café cards.

Run it on GitHub. Open the Race two signed bills, crash the worker and retry step in a verification run. It builds two contract revisions, starts a fresh local NEAR chain and checks the merchant’s balance. To run it yourself without installing tools, fork the repo and choose Actions → Local payment verification → Run workflow. A successful run publishes weekly-payment-result with the measured balances. Verification runs

Run it on your computer. Follow the local setup in a full clone, then run this from the repo root.

npm run test:upgrade:weekly

Look for PASS crash and PASS weekly. Either bill can win. The merchant balance must increase by 0.04 once, with 0.00 extra on retry. The command prints the path to your own result.json.

If a supplier corrects a bill before reservation, the owner can transfer that week's claim to the exact signed replacement. Run npm run test:weekly:correction after the local build to race a 0.03 correction against the original 0.04 bill and crash the paying worker in each order. Owner commands

What runs

The demo executes native NEAR transfers on a disposable local chain. It uses fixed agent proposals so anyone can reproduce it without a model key. The optional NEAR AI Cloud adapter receives structured invoice fields and can propose only request_payment({invoice_id}). The vault checks the merchant signature, payout, spending limits and service week, and records settlement from the transfer callback.

Hackathon tracks · AI Security & Defense (primary), Agent Economy (secondary). Built during NEAR HACKATHON with Rust NEAR SDK, TypeScript, Node SQLite, near-workspaces and AI-assisted development. Official tracks

Local payment desk, contract and operator guide

The local desk opens on Agent payments at http://127.0.0.1:4417/#agent-payments (with the app started on port 4417 as above) on every network. Choose Demo café from the sidebar to try the demo. In Agent payments, import a supplier's signed bill, request payment and read the receipt (needs a local agent vault with a policy and a pinned supplier key). The page calls the same functions as agent:desk import, run and resume and adds no payment logic. The manual payment desk and dry-run 1Click lab are under Tools. See Agent payments in the desk.

Ask Dave also runs in the desk: open http://127.0.0.1:4417/#ask-dave (needs a local agent vault: npm run agent:desk -- init). Its questions read work/agent-vault (or COUNTERSIGN_AGENT_DIR), while the manual payment desk uses work/local-vault (or COUNTERSIGN_SANDBOX_DIR). The page and each answer identify the source account. Standalone account checks show their selected network separately; questions always read the local agent vault.

Known limitations

  • A merchant in the policy without a pinned signing key can be paid without a supplier signature. The payment still goes only to the pinned payee, within the per-payment cap and the daily budget.
  • Renewing the policy invalidates policy approvals that have not been dispatched; in-flight payments keep their holds and settlement tracking. The owner can cancel eligible old approvals, but cancelled invoice and supplier bill IDs stay used: the same signed bill cannot simply be submitted again.
  • Outside weekly services, a supplier who signs the same real bill under two different bill IDs can be paid twice.
  • The public testnet vault's receipts are historical. Its policy expired on 10 November 2026.
  • The machine running the agent host holds the owner and executor keys and is trusted.

Public testnet vault

The opt-in testnet guide covers atomic deployment and the café verification below. The app, agent desk and local tests continue to use the local sandbox.

Install the isolated tooling with npm ci --prefix testnet --ignore-scripts; run its offline checks with npm --prefix testnet test. Live commands use COUNTERSIGN_NETWORK=testnet npm --prefix testnet run vault -- <command> or COUNTERSIGN_NETWORK=testnet npm --prefix testnet run cafe, as detailed in the guide.

Live on testnet

Dave’s café ran on 10 November 2026 using fixture proposals and zero live model calls. The owner funded 2 test NEAR and set the policy: 0.5 per payment, 0.8 rolling daily budget, 0.01 minimum, four-hour expiry. The executor paid signed bills without per-invoice owner approval. All rejections below came from submitted contract transactions.

Outcome Transaction / NearBlocks Exact result
Signed bill under policy C5ok2A2DsLrkoJ7D89dQddmg9Y95qn9w7m78VcE1k5Ag Settled; merchant +0.4 NEAR once; replay +0.
Tampered payee UMJQ9TZzhuAZbvtYyRjKbSi5HzKhN6gzhMyAuzAHUSD Smart contract panicked: invalid merchant signature
Same bill, new invoice ID JMJyVdSeSyi6kyq48zBcHesF1diquLk7GDrVaQXtVrU Smart contract panicked: merchant bill already used
Daily budget exceeded BD2wAqJz62Yb9ZfGA23KHdQ7pzyoj9749J5hDKWWmWy Smart contract panicked: rolling daily budget exceeded
Kill after reserve; restart and resume C1AY2bgfKehYPsKyN6rRSkpwKDjpyE6BkDyd9YFKMswG Settled; merchant +0.3 NEAR once; replay +0.

For the crash case, we confirmed reserve, killed the worker with SIGKILL, and resumed the same durable attempt in a fresh process. Merchant credit totaled 0.7 NEAR, with zero extra on replay and 1.3 NEAR remaining in the vault budget. After the original RPC’s interruption, we switched to FastNEAR and renewed only the expired policy deadline, preserving its limits and 0.4 NEAR usage. Full public receipts, balance snapshots and crash evidence · Reproduce the café run.

About

Agent invoice payments on NEAR. A vault pays one signed bill per service week, even after a worker crash and retry.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages