cred402/docs/qie_x402.md
caelum0x 9b615993fa feat: add QIE readiness + release-check scripts to match Algorand parity
Mirrors scripts/algorand_x402_ready.ts and algorand_x402_release_check.ts
for QIE's EVM-based x402 integration: x402:qie:ready fetches live
payer/receiver ERC-20 balances over the configured QIE RPC and reports
readiness; x402:qie:release-check adds the public deployment-status gate
and writes a SHA-256-sealed evidence artifact (same hash-seal mechanism
as the Algorand artifact, no private key). Both fail closed with clear
errors (no guessed RPC URL or contract address) when required env vars
are unset. Closes the "no combined release-check / readiness script yet"
gap tracked in docs/qie_x402.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-19 13:46:48 +03:00

170 lines
8.5 KiB
Markdown

# QIE x402 credit-score endpoint
Cred402's QIE integration (QIE Blockchain Hackathon 3.0, "AI & Autonomous
Agents" track — `hackathon.qie.digital`) sells the same resource as the
Algorand integration, priced on QIE instead:
```text
GET /v1/x402/qie/credit-score/:agentId
```
The endpoint returns the policy score, probability of default, risk band,
eligibility, reason codes, and verified x402 revenue — it uses the same
oracle and risk engine as the rest of Cred402 (`state.x402CreditScore`); the
QIE route is a payment and distribution layer, not a forked scoring model.
## Why this integration is small
QIE is an EVM-compatible Layer-1 (Cosmos SDK consensus + Ethereum JSON-RPC
`eth_*` compatibility — the hackathon's own builder quickstart says to point
an existing Hardhat/Foundry workflow at the QIE RPC endpoint). Cred402
therefore reuses the exact same chain-agnostic `@x402/core` resource-server
primitives the Algorand integration uses (`x402ResourceServer`,
`x402HTTPResourceServer`, `HTTPFacilitatorClient`) and only swaps the
registered scheme: `ExactEvmScheme` from `@x402/evm` instead of
`ExactAvmScheme` from `@x402/avm`. No bespoke signing, encoding, or ABI code
was written — see [`lib/x402/qie_gateway.ts`](../lib/x402/qie_gateway.ts).
## What research could not confirm (and how the code fails closed instead)
- **QUSDC's ERC-20 contract address.** QIE's stablecoin, QUSDC, is documented
at `docs.stable.qie.digital` as a 1:1 USDC-backed receipt token, but its
contract address, decimals, and testnet availability were not confirmed.
`CRED402_QIE_ASSET_ADDRESS` is therefore a **required** env var with no
default — the route returns `503` until an operator supplies a real,
verified token contract. It never guesses an address.
- **A confirmed x402 EVM facilitator for QIE.** Unlike Algorand's GoPlausible
facilitator, no default/public x402 facilitator for QIE is confirmed to
exist. `CRED402_QIE_FACILITATOR_URL` is therefore also **required** with no
default. Point it at a self-hosted facilitator (built from `@x402/evm`'s
`FacilitatorEvmSigner` + `ExactEvmScheme`) or a third-party one that
verifies/settles the `exact` scheme on `eip155:1983` (testnet) /
`eip155:1990` (mainnet).
- **A public Mainnet RPC endpoint.** Only a testnet RPC
(`https://rpc1testnet.qie.digital/`) was independently confirmed by
research. `CRED402_QIE_RPC_URL` has a testnet default but is **required**
with no default on Mainnet.
Without all three configured, `GET /v1/x402/qie/credit-score/:agentId`
returns `503` and never advertises a placeholder receiver, a guessed token,
or an unverified facilitator as if it were real.
## Configuration
```bash
export CRED402_QIE_PAY_TO=0xYOUR_EVM_ADDRESS
export CRED402_QIE_NETWORK=testnet
export CRED402_QIE_RPC_URL=https://rpc1testnet.qie.digital/
export CRED402_QIE_ASSET_ADDRESS=0xYOUR_VERIFIED_TOKEN_CONTRACT # QUSDC once confirmed
export CRED402_QIE_ASSET_SYMBOL=QUSDC
export CRED402_QIE_ASSET_DECIMALS=6
export CRED402_QIE_PRICE_BASE_UNITS=10000 # 0.01 token at 6 decimals
export CRED402_QIE_FACILITATOR_URL=https://your-facilitator.example.com
export CRED402_DATA_DIR=/var/lib/cred402
npm run start
```
See [`.env.example`](../.env.example) for every variable and its defaults.
## Inspect the unpaid challenge
```bash
curl -i "http://localhost:4021/v1/x402/qie/credit-score/YOUR_AGENT_ID"
```
For a strict, payment-free preflight check:
```bash
export CRED402_QIE_EXPECTED_PAY_TO="$CRED402_QIE_PAY_TO"
export CRED402_QIE_EXPECTED_ASSET_ADDRESS="$CRED402_QIE_ASSET_ADDRESS"
npm run x402:qie:check -- \
"http://localhost:4021/v1/x402/qie/credit-score/YOUR_AGENT_ID"
```
The preflight (`lib/x402/qie_client.ts`) rejects anything other than exactly
one x402 v2 `exact` option on the selected QIE network, the configured asset
contract and receiver, a hard price ceiling, and a matching resource URL. It
never signs or submits a transaction.
## Settlement and independent finality
The route uses a durable exact-once attempt ledger
(`lib/x402/qie_payment_attempt_store.ts`, same role as
`AlgorandPaymentAttemptStore`): it stores a digest, not the raw
`PAYMENT-SIGNATURE`, and replays the same paid response after safe retries or
restarts. Facilitator success produces a provisional settlement; Cred402
independently re-reads the transaction receipt from the configured QIE RPC
(`lib/x402/qie_finality.ts`) and confirms an ERC-20 `Transfer` log moved at
least the expected amount to the configured receiver, with a configurable
minimum confirmation depth, before calling the payment `confirmed`. This
plays the same role Algorand's independent Indexer check plays, since no
Indexer-equivalent REST API for QIE was confirmed by research — Cred402 reads
its own RPC directly instead of trusting the facilitator's report alone.
`GET /v1/x402/qie/payments/:attemptId` exposes only coarse lifecycle,
transaction, and confirmation-count data — never the paid report, payer
identity, or raw signature.
## Status probe
`GET /v1/x402/qie/status` is free, secret-free, and never initializes the
facilitator — it mirrors `/v1/x402/algorand/status`.
## Mainnet release checklist
1. Confirm the real QUSDC ERC-20 contract address on QIE Mainnet (chain 1990)
and set `CRED402_QIE_ASSET_ADDRESS` to it.
2. Stand up or confirm a production x402 EVM facilitator for QIE and set
`CRED402_QIE_FACILITATOR_URL` to its HTTPS endpoint.
3. Set `CRED402_QIE_RPC_URL` to a trustworthy QIE Mainnet RPC endpoint and
confirm `eth_chainId` returns `0x7c6` (1990).
4. Mount a persistent volume and set `CRED402_DATA_DIR` to that mount.
5. Set `CRED402_ENV=mainnet`, `CRED402_QIE_NETWORK=mainnet`, and
`CRED402_PUBLIC_URL` to the public HTTPS origin.
6. Set `CRED402_QIE_MAINNET_RELEASE_ACK` exactly to
`I_ACKNOWLEDGE_REAL_QUSDC_MAINNET_PAYMENTS`.
7. Make one real external Mainnet payment and confirm the `Transfer` log and
confirmation depth reconcile via `GET /v1/x402/qie/payments/:attemptId`.
8. Record the public endpoint, transaction hash, repository, and the
hackathon's required fields (GitHub repo link, live demo URL, deployed
contract address, track = "AI & Autonomous Agents") ahead of the
2026-11-30 submission deadline.
## Known gaps vs. the Algorand integration (tracked, not hidden)
- ~~No Casper anchoring of QIE settlements yet~~ **Built.** `api/qie_receipt_anchor.ts`
mirrors `algorand_receipt_anchor.ts`: `state.anchorQieCreditScoreSettlement`
turns a settled QIE payment into the same Casper-rooted Universal Receipt
Envelope every other satellite chain uses (`qie:<payer>` identity,
`qie-erc20:<contract>` asset), anchored provisional until
`reconcileQiePayment`'s RPC finality check confirms it, then finalized or
challenged. The credit-score response carries `external_receipt_id` /
`external_receipt_url` / `casper_anchor_status`; the proof is public at
`GET /v1/x402/external-receipts/:id` (the same endpoint Algorand's receipts
use — receipt ids are a shared content hash, not chain-specific). Covered
by `test/qie_receipt_anchor.test.ts` (6 tests, against a real `Ledger`).
- ~~No combined release-check / readiness script yet~~ **Built.**
`scripts/qie_x402_ready.ts` (`x402:qie:ready`) and
`scripts/qie_x402_release_check.ts` (`x402:qie:release-check`) mirror
`algorand_x402_ready.ts` / `algorand_x402_release_check.ts` exactly: they
fetch the unpaid challenge, then fetch live payer/receiver balances over
the configured QIE RPC (`eth_chainId`, `eth_blockNumber`, ERC-20
`balanceOf`) via `lib/x402/qie_readiness.ts`, and `release-check` additionally
writes a SHA-256-sealed evidence artifact via `lib/x402/qie_release.ts`
(same "signed" = hash-sealed, no private key, mechanism as the Algorand
artifact). Both fail closed with a clear error — no guessed RPC URL or
contract address — if `CRED402_QIE_CLIENT_ADDRESS`,
`CRED402_QIE_EXPECTED_PAY_TO`, `CRED402_QIE_EXPECTED_ASSET_ADDRESS`, or (on
Mainnet) `CRED402_QIE_RPC_URL` are unset. Unlike Algorand's checks, there is
no ALGO-style minimum-balance/opt-in/fee-buffer check: QIE's EVM "exact"
scheme has no account-reserve equivalent, and the facilitator — not the
payer — pays settlement gas, so that check is not invented. Covered by
`test/qie_x402_ready.test.ts` (mocked RPC, no live network needed to pass).
- No Bazaar-style discovery listing (QIE has no confirmed equivalent catalog
yet, so none was wired in to avoid overclaiming discoverability that
doesn't exist).
Do not create synthetic/self-payment volume. One genuine end-to-end payment
against a real facilitator and a real QUSDC (or interim test ERC-20) contract
is more useful than fabricated traffic.