# Decision ledger

The decision ledger is your account's append-only, hash-chained record of every authorize and verify, with signed receipts you can hand to anyone.

The decision ledger is your account's append-only record of every authorize and verify: the token, the facts as evaluated, the policy version and rule, and the result. Each record is hash-chained to the one before it, so you can prove later that a person authorized an action and that the record wasn't edited.

## When to use it

- Answer "who authorized this, and under which rule?" for an audit or a dispute.
- Hand a counterparty a signed receipt they can check without trusting you or Provenance.
- Find every denied action for an agent session, a token or a reason.

## How it works

1. **Every call writes a record.** Authorize and verify each append one, whatever the result.
2. **Records are chained.** Each has a `record_hash` over its contents and the `prev_hash` of the record before it, per account. Changing any record breaks every hash after it.
3. **Anyone can check.** [`GET /v1/mandate/assessments/verify_chain`](https://provenanceauth.com/docs/api/decisions.md#get-v1-mandate-assessments-verify-chain) recomputes the chain, a receipt verifies offline, and the [transparency log](https://provenanceauth.com/docs/concepts/transparency-log.md) publishes each account's chain head.

1. **Authorize or verify**: facts, rule, result
2. **Ledger record**: record_hash + prev_hash
3. **Receipt**: ES256, verifiable offline

There's no endpoint to edit or delete a record. Deleting an account removes personal fields but keeps the chain intact.

## Key fields

- `id` (string): The decision's ID, prefixed `dec_`.
- `kind` (string): `authorize` or `verify`.
- `result` (string): `valid`, `denied` or `pending`.
- `reason` (string | null): The [reason code](https://provenanceauth.com/docs/platform/reason-codes.md) when the result isn't `valid`.
- `facts` (object | null): The facts the policy evaluated, such as the amount and action type.
- `matched_rule_id` (string | null): The policy rule that decided it.
- `policy_version` (integer | null): The policy version in force; `null` for the default policy.
- `policy` (object | null): The rule that decided, read back as the same block every assessment carries: its current ID and name, its action, and each condition against the facts this record kept. `matched_rule_id` and `policy_version` stay on the record as written, because they are part of what `record_hash` covers.
- `jti` (string): The intent token's ID.
- `agent_session_id` (string): The agent session the token was minted for.
- `record_hash` (string): SHA-256 over this record's canonical contents.
- `prev_hash` (string): The previous record's `record_hash`.

Records also carry the registered agent, grant, approver or trace when one was involved. A verify an [`auto_approve` clause](https://provenanceauth.com/docs/mandate/guides/automate-approvals.md) approved carries `auto_approval` (the rule, the clause, its label and the policy version), and `GET /v1/mandate/assessments?automatic=true` lists those.

## Find a decision and get its receipt

Make a decision to look at: authorize a payment, then verify a changed payload so it's denied. Export the sandbox account key first:

**curl**

```bash
export PROVENANCE_API=https://sandbox-api.provenanceauth.com
export PROVENANCE_KEY=prv_test_acct_P9xN2vQ7bT4mW8sYd3cFjLh6gAe1kR5

curl -s $PROVENANCE_API/v1/mandate/intents \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "action_description": "Pay invoice INV-2041: $1,850.00 to Brindlecote Supplies",
    "action_payload": { "amount": 1850, "payee": "acct_brindlecote_supplies" },
    "agent_session_id": "sess_ledger_1",
    "device_id": "dev_00000000000000000000000000000001"
  }' > authorization.json
export INTENT_TOKEN=$(jq -r .intent_token authorization.json)

curl $PROVENANCE_API/v1/mandate/assessments \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "intent_token": "'"$INTENT_TOKEN"'",
    "action_description": "Pay invoice INV-2041: $1,850.00 to Brindlecote Supplies",
    "action_payload": { "amount": 18500, "payee": "acct_attacker" }
  }'
```

**Node**

```ts
import { Provenance } from "@provenanceauth/sdk";

const prov = new Provenance({ apiKey: process.env.PROVENANCE_KEY!, baseUrl: process.env.PROVENANCE_API });
const action = { actionDescription: "Pay invoice INV-2041: $1,850.00 to Brindlecote Supplies" };
const auth = await prov.mandate.intents.create({
  ...action,
  actionPayload: { amount: 1850, payee: "acct_brindlecote_supplies" },
  agentSessionId: "sess_ledger_1",
  deviceId: "dev_00000000000000000000000000000001",
});
await prov.mandate.assessments.create({ intentToken: auth.intentToken, ...action, actionPayload: { amount: 18500, payee: "acct_attacker" } });
```

**Python**

```python
import os
from provenanceauth import Provenance

prov = Provenance(os.environ["PROVENANCE_KEY"], base_url=os.environ["PROVENANCE_API"])
description = "Pay invoice INV-2041: $1,850.00 to Brindlecote Supplies"
auth = prov.mandate.intents.create(
    action_description=description,
    action_payload={"amount": 1850, "payee": "acct_brindlecote_supplies"},
    agent_session_id="sess_ledger_1",
    device_id="dev_00000000000000000000000000000001",
)
prov.mandate.assessments.create(intent_token=auth["intent_token"], action_description=description, action_payload={"amount": 18500, "payee": "acct_attacker"})
```

```json
{
  "object": "mandate_assessment",
  "valid": false,
  "reason": "payload_hash_mismatch"
}
```

List the session's decisions with [`GET /v1/mandate/assessments`](https://provenanceauth.com/docs/api/decisions.md#get-v1-mandate-assessments), newest first. You can also filter by token, result, reason or a time range:

**curl**

```bash
curl -s "$PROVENANCE_API/v1/mandate/assessments?session=sess_ledger_1&limit=1" \
  -H "Authorization: Bearer $PROVENANCE_KEY" | tee decisions.json
export DECISION_ID=$(jq -r '.data[0].id' decisions.json)
```

**Node**

```ts
const { data } = await prov.mandate.assessments.list({ session: "sess_ledger_1", limit: 1 });
const decision = data[0]!;
```

**Python**

```python
decision = prov.mandate.assessments.list(session="sess_ledger_1", limit=1)["data"][0]
```

The response, trimmed:

```json
{
  "object": "list",
  "data": [
    {
      "id": "mas_01M5Q36E142MXMY0ADRNRAFW7P",
      "object": "mandate_assessment",
      "kind": "verify",
      "agent_session_id": "sess_ledger_1",
      "device_id": "dev_00000000000000000000000000000001",
      "result": "denied",
      "reason": "payload_hash_mismatch",
      "matched_rule_id": null,
      "record_hash": "ffd41ae065bf4eca4098739b361faa47e132fdee91d97eecfd29c4dcdef64bd8",
      "prev_hash": "eeefb28780ff95619231fb234ec2986f5693888219f753e5835f92782a7b65b4"
    }
  ],
  "has_more": true
}
```

Get its receipt with [`GET /v1/mandate/assessments/{id}/receipt`](https://provenanceauth.com/docs/api/decisions.md#get-v1-mandate-assessments-id-receipt). The response is a `receipt` object: `claims` is the signed content, decoded, and `token` is an ES256 JWS signed with the same keys as intent tokens, so anyone can [verify it offline](https://provenanceauth.com/docs/authorize/guides/verify-tokens.md) or paste it into the console's [Verify a proof](https://provenanceauth.com/docs/integrate/verify-a-proof.md) page. Receipts are rebuilt from the record each time, never stored; add `?format=pdf` for a one-page PDF.

**curl**

```bash
curl $PROVENANCE_API/v1/mandate/assessments/$DECISION_ID/receipt \
  -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
const { claims, token, verifyUrl } = await prov.mandate.assessments.receipt(decision.id);
```

**Python**

```python
receipt = prov.mandate.assessments.receipt(decision["id"])
```

The response, trimmed:

```json
{
  "object": "receipt",
  "id": "rcpt_5b0d2b1f8e7c4a6d9e3f1a2b3c4d5e6f",
  "claims": {
    "receipt_id": "rcpt_5b0d2b1f8e7c4a6d9e3f1a2b3c4d5e6f",
    "decision_id": "mas_01M5Q36E142MXMY0ADRNRAFW7P",
    "kind": "verify",
    "agent_session_id": "sess_ledger_1",
    "result": "denied",
    "reason": "payload_hash_mismatch",
    "biometric_bound": false,
    "record_hash": "ffd41ae065bf4eca4098739b361faa47e132fdee91d97eecfd29c4dcdef64bd8"
  },
  "token": "eyJhbGciOiJFUzI1NiIsInR5cCI6InByb3ZlbmFuY2UtcmVjZWlwdCtqd3QifQ.eyJkZWNpc2lvbl9pZCI6ImRlY18wMU01UTM2RTE0Mk1YTVkwQURSTlJBRlc3UCJ9.Yl1jB3gTWRUDUfgfLtDY2wZvFVtA3_VoFfUqHrfZDr4-Oiqn5kPCpoqeOwiPbqzSv0OFVUB1pCe5zz4fXH1RWQ",
  "verify_url": "https://…/developers/verify?receipt=eyJhbGciOiJFUzI1NiIsInR5cCI6InByb3ZlbmFuY2UtcmVjZWlwdCtqd3QifQ…"
}
```

## Check the chain

[`GET /v1/mandate/assessments/verify_chain`](https://provenanceauth.com/docs/api/decisions.md#get-v1-mandate-assessments-verify-chain) recomputes every hash in the account's chain, or in a `created_gte` and `created_lte` range, and reports the first break:

**curl**

```bash
curl $PROVENANCE_API/v1/mandate/assessments/verify_chain \
  -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
const chain = await prov.mandate.assessments.verifyChain();
```

**Python**

```python
chain = prov.mandate.assessments.verify_chain()
```

```json
{ "object": "chain_verification", "valid": true }
```

A broken chain returns `valid: false` with `broken_at` and a `reason`. The console's **Decisions** page has the same check and a filterable view of every record.

**Limitations:** only account keys write to the ledger; decisions made with a carrier key aren't chained here.

## Related

- [Verify tokens offline](https://provenanceauth.com/docs/authorize/guides/verify-tokens.md)
- [Transparency log](https://provenanceauth.com/docs/concepts/transparency-log.md)
- [Policies](https://provenanceauth.com/docs/concepts/policies.md)
