Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
**/node_modules
**/.env
**/.env.*
**/*keypair*.json
**/*.key
.git
evidence
docs
scripts
**/test
**/examples
30 changes: 30 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/base"
schedule:
interval: weekly
open-pull-requests-limit: 5
groups:
x402-and-solana-evm:
patterns: ["@x402/*", "@solana/*", "viem"]
- package-ecosystem: npm
directory: "/base/market"
schedule:
interval: weekly
open-pull-requests-limit: 5
groups:
x402-and-solana-evm:
patterns: ["@x402/*", "@solana/*", "viem"]
- package-ecosystem: npm
directory: "/base/demo"
schedule:
interval: weekly
open-pull-requests-limit: 5
groups:
x402-and-solana-evm:
patterns: ["@x402/*", "@solana/*", "viem"]
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
32 changes: 32 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CodeQL

on:
push:
branches: [main]
pull_request:
schedule:
- cron: "17 6 * * 1"
workflow_dispatch:

permissions:
contents: read

jobs:
analyze:
# Code scanning needs Advanced Security on a private repo; runs once the repo is public.
if: ${{ !github.event.repository.private }}
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
actions: read
contents: read
security-events: write
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: github/codeql-action/init@915412ff23e8b7659517e607487dbeae018f2934 # v4.39.5
with:
languages: javascript-typescript
queries: security-extended
- uses: github/codeql-action/analyze@915412ff23e8b7659517e607487dbeae018f2934 # v4.39.5
with:
category: "/language:javascript-typescript"
68 changes: 68 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: tests

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
base:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 24
- name: Install
run: |
(cd base && npm ci --no-audit --no-fund)
(cd base/demo && npm ci --no-audit --no-fund)
(cd base/market && npm ci --no-audit --no-fund)
- name: Syntax
run: for f in $(git ls-files "base/*.mjs"); do node --check "$f"; done
- name: Receipt verifier core (7 tests, no network)
working-directory: base
run: npm run test:verify
- name: Pre-sign policy (19 tests, no network)
working-directory: base
run: npm run test:policy
- name: Library (pay, charge, receipts, decisions; 14 tests, one reads the public Base Sepolia RPC)
working-directory: base
run: npm run test:sdk
- name: x402 market (4 tests, no network)
working-directory: base/market
run: npm test
- name: Verifier against a real third-party Base mainnet payment (10 cases, public RPC)
working-directory: base
run: npm run test:live
- name: Package (pack, install in an empty project, import every subpath, run the bin)
working-directory: base
run: |
npm pack --pack-destination "$RUNNER_TEMP"
mkdir "$RUNNER_TEMP/consumer" && cd "$RUNNER_TEMP/consumer"
npm init -y >/dev/null
npm install --no-audit --no-fund "$RUNNER_TEMP"/nirium-base-*.tgz
node --input-type=module -e "for (const p of ['nirium-base','nirium-base/policy','nirium-base/policy/x402-fetch','nirium-base/policy/rules','nirium-base/receipt','nirium-base/verify','nirium-base/decision']) await import(p); console.log('imports ok')"
npx --no-install nirium-verify-evm "$GITHUB_WORKSPACE/base/fixtures/payai-ok.json" --no-sig | tail -1 | grep -q "RESULT: VALID"
if tar -tzf "$RUNNER_TEMP"/nirium-base-*.tgz | grep -E -i "test|example|evidence|fixture|market|demo"; then echo "unexpected files in the package"; exit 1; fi

market-image:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Build the market image (same Dockerfile Fly uses)
run: docker build -f base/market/Dockerfile -t nirium-market .
- name: Run it with the testnet config of fly.market.toml; /health must be 200 and the paid route 402
run: |
docker run -d --name market -p 8080:8080 -e NIRIUM_NETWORK=eip155:84532 -e NIRIUM_EVM_PAYTO=0xC7484059c45Cf89dDc487C836A6f821Dc5882051 nirium-market
for i in $(seq 1 30); do curl -sf http://localhost:8080/health && break; sleep 1; done
code=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/v1/market)
info=$(curl -s http://localhost:8080/v1/market/info)
docker logs market | tail -20
echo "paid route: $code"; echo "info: $info"
test "$code" = 402
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules/
out/
.env
.env.*
*.key
*keypair*.json
keys/
settle.json
binding.json
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# nirium-base

[![tests](https://github.com/nirium-protocol/nirium-base/actions/workflows/tests.yml/badge.svg)](https://github.com/nirium-protocol/nirium-base/actions/workflows/tests.yml)
[![CodeQL](https://github.com/nirium-protocol/nirium-base/actions/workflows/codeql.yml/badge.svg)](https://github.com/nirium-protocol/nirium-base/actions/workflows/codeql.yml)
[![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)

Pay and charge with x402 on Base, refuse a payment before it is signed, and keep a receipt that
says what the payment was for.

The chain already shows who paid whom, how much, and when. A Nirium receipt adds what the chain
does not keep: which order a payment was for and who declared it, signed by the payer and bound
to one specific USDC `Transfer` log. Anyone can check it against Base with a public RPC and
nothing else from us.

This is [Nirium](https://nirium.xyz) on Base. Nirium started on Stellar; this repo brings the
same receipt format and the same pre-sign policy contract to Base and Base Sepolia. The code is
the EVM code of [`nirium-avalanche`](https://github.com/nirium-protocol/nirium-avalanche) with
Base's network table.

## Status

| Piece | Base Sepolia | Base mainnet |
|---|---|---|
| Receipt verifier (`base/verify-evm-receipt.mjs`) | yes, our selftest receipt | yes, our selftest receipt; fixtures from a third-party payment |
| Pre-sign policy (`base/policy/`) | offline tests | not yet |
| Pay and charge library (`base/sdk/`) | yes (selftest, 1 Dec 2026) | yes (selftest, 1 Dec 2026) |
| x402 market (`base/market/`) | offline tests | not yet |

A row says "yes" only once a settled payment's hash is in [`evidence/`](evidence/README.md).
Every payment there is made by the team to test the flow, labeled `selftest`, and is not usage.
Nothing here has had an external audit.

## One Base detail

The USDC contracts do not share an EIP-712 domain name: Base Sepolia USDC signs as `USDC`,
Base mainnet USDC as `USD Coin` (both version `2`). The name lives in each row of
[`base/sdk/networks.mjs`](base/sdk/networks.mjs); with the wrong one every signature is invalid.

## What is here

| Path | What it does |
|---|---|
| [`docs/payment-binding-v1.md`](docs/payment-binding-v1.md) | The receipt format and what a verifier checks |
| [`docs/decision-receipt-v1.md`](docs/decision-receipt-v1.md) | Decision receipts: a payer-signed record of what the pre-sign policy decided, including payments it refused (DENY, WAIT), which leave nothing on-chain |
| [`base/verify-evm-core.mjs`](base/verify-evm-core.mjs) | Checks a receipt against a USDC `Transfer` log (tx hash + block logIndex), the EIP-3009 `AuthorizationUsed` nonce and the payer's signature. The CLI, `verifyReceipt` and a browser run this file |
| [`base/verify-evm-receipt.mjs`](base/verify-evm-receipt.mjs) | The command line for it |
| [`base/sdk/`](base/sdk/README.md) | The library: `x402Serve` to charge, `createX402Fetch` to pay (with an optional policy), `bindReceipt` and `verifyReceipt` |
| [`base/policy/`](base/policy/README.md) | Pre-sign policy: DENY, WAIT, a timeout and every malformed answer sign nothing |
| [`base/market/`](base/market/README.md) | x402 market: Base network conditions (fees, block fill, block time) for $0.01 per query |
| [`base/demo/`](base/demo/README.md) | Reference x402 `exact` payment with PayAI as facilitator, then the receipt |
| [`scripts/`](scripts) | Base Sepolia key setup |
| [`evidence/`](evidence/README.md) | The team's test payments, each with its receipt and explorer link |

## Quick start

```js
import { x402Serve, createX402Fetch, settlementOf, bindReceipt, createRulesPolicy } from "nirium-base";

app.use(x402Serve({ payTo: "0x...", network: "eip155:84532", routes: { "GET /data": "$0.01" } }));

const pay = createX402Fetch({ signer: account, network: "eip155:84532", maxAmountPerPayment: "10000",
policy: createRulesPolicy({ maxAmount: "10000" }) });
const res = await pay("https://seller.example/data");
const receipt = await bindReceipt({ tx: settlementOf(res).transaction, network: "eip155:84532", ref: "order-42", signer: account });
```

`x402Serve` runs on your server: Nirium runs nothing in the middle and receives nothing.

## Tests

| Suite | Command | Network |
|---|---|---|
| Verifier core (hash against `node:crypto`, EIP-191, the full check on recorded RPC answers, altered fields) | `cd base && npm ci && npm run test:verify` | none |
| Verifier, 10 cases against a real third-party Base mainnet payment | `cd base && npm ci && bash run-tests.sh` | public Base RPC |
| Pre-sign policy, 19 tests | `cd base && npm ci && npm run test:policy` | none |
| Library | `cd base && npm ci && npm run test:sdk` | one test reads the public Base Sepolia RPC |
| Market | `cd base && npm ci && cd market && npm ci && npm test` | none |

CI runs all of them on every pull request. The verifier fixtures point at a third-party payment
and are labeled as such: they test the verifier, they are not usage of this project.

## Rules this code follows

- Non-custodial: every key belongs to whoever pays or receives. Nirium never holds one.
- Mainnet payments need an explicit opt-in on the client (`NIRIUM_ALLOW_MAINNET=1` or
`allowMainnet: true`).
- Settlement is done by PayAI, an independent facilitator. Nirium runs no facilitator.
- Receipts are tamper-evident, not tamper-proof: an edited receipt no longer matches its hash or
its signature. A receipt does not prove that goods or a service were delivered.
- Keys never go in this repo. The key script writes outside it and refuses to run inside it.

## Security

See [SECURITY.md](SECURITY.md). Please report vulnerabilities privately, not in a public issue.

## License

[Apache-2.0](LICENSE)
27 changes: 27 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Security policy

## Reporting a vulnerability

Please do not open a public issue for a security problem. Report it privately through GitHub:
the **Security** tab of this repository, then **Report a vulnerability**. That opens a private
advisory that only the maintainers can see.

Include what you found, how to reproduce it, and what an attacker could do with it. If you have
a fix, describe it in the report; we will set up a private branch for it.

We aim to acknowledge a report within 3 business days and to tell you what we plan to do within
10. There is no bug bounty for this repository.

## Scope

In scope: the code in this repository, in particular anything that could make a client sign a
payment its policy refused, make a verifier accept a receipt that does not match the chain, or
make a payment go somewhere other than the requested `payTo`.

Out of scope: the x402 packages, the PayAI facilitator and the public RPC endpoints this code
talks to. Please report those to their own maintainers.

## Keys

No key belongs in this repository. If you ever see a private key, keypair file or seed phrase in
a commit, report it the same private way so it can be revoked.
Loading