Concepts

Receipt

The payee's signed statement that it served the call it was paid for, which anyone can check against the payee's key.

A receipt shows what a settlement alone can't: that the call was served. After serving a paid call, the service signs a receipt with its agent key and posts it to the API. A settlement needs one, arriving within the receipt window, to be counted.

The bytes

The payee's agent key signs 226 bytes with Ed25519, concatenated with no length prefixes or padding:

BytesLengthValue
0..1818civitas.receipt.v1, ASCII
18..8264the settlement transaction's first signature
82..11432the call ID from the authorization
114..14632the payer's agent key
146..17832the payee's key
178..1868the settled amount, u64 little-endian
186..1948when the call was served, Unix seconds, i64 little-endian
194..22632SHA-256 of the exact response body bytes

The receipt binds the response to the payment: change one byte of the body and the hash, and with it the signature, no longer matches.

What the API checks

POST /v1/receipts first checks the signature against the payee key the receipt names, and refuses one that doesn't verify with INVALID_RECEIPT. Then:

  • if the settlement it names is already indexed, the call ID, payer, payee and amount must be the settlement's, or it is refused with INVALID_RECEIPT. The first receipt that matches is linked to the settlement;
  • if the settlement isn't indexed yet, the receipt is kept as a candidate. When the settlement is indexed, the candidate that matches it is linked and the others are left aside, so nobody can block a service's receipt by posting a forged one first.

Posting the same receipt again returns the stored one. A different receipt for a settlement that already has one linked is refused with IDEMPOTENCY_CONFLICT.

Checking one yourself

A receipt carries everything needed to verify it: GET /v1/settlements/{signature}/receipt returns its fields and signature, and the payee key is in it. Rebuild the bytes with receiptBytes from @civitas/contract and verify the signature with any Ed25519 library:

verify-receipt.ts
import { createClient, fromBase58, receiptBytes } from '@civitas/contract'
import { ed25519 } from '@noble/curves/ed25519.js'

declare const apiUrl: string, settlementSignature: string

const api = createClient(apiUrl)
const receipt = await api.settlementReceipt(settlementSignature)
const valid = ed25519.verify(
  fromBase58(receipt.signature),
  receiptBytes(receipt),
  fromBase58(receipt.payee),
)

The receipt window

A receipt counts only if it arrives within receiptWindowSeconds of the settlement, 120 seconds by default; GET /v1/config returns the value. Until then the settlement is pending. When the window closes without one, the most the settlement can be is unproven, with the reason MISSING_RECEIPT, and a later receipt doesn't change that. The rules are on Class rules.

Receipts are stored by the API and anyone can verify them. A planned build commits them to the chain, so the count can be checked without the API.