# Sister-ledger interlock (`rs-peer-ledger/1`)

Two append-only ledgers under common ownership that witness each other. Each side verifies the other's whole chain,
seals the other's verified head into its own ledger, and cosigns the other's blocks as an affiliated witness. A rewrite
of either history then has to rewrite both ledgers and both trails of Bitcoin anchors, in public. It is not
independent witnessing, and both sides say so on every surface. What a reader sees is served at
`https://www.rightshold.com/v1/ledger/peers`.

## The ledger each side exposes

Every participant runs a ledger with these public, key-less routes (the same ones any outside witness uses):

- `GET /v1/ledger` answers `{head: {height, block_hash, ...} | null, height, ...}`. A ledger with no sealed block
  answers exactly `head: null` and `height: -1`.
- `GET /v1/ledger/export?from_height=N&limit=L` answers `{blocks: [{block, entries}]}` in height order, at most 100
  blocks a page. `block` is the header `{v, height, prev_hash, merkle_root, first_seq, last_seq, entry_count, at, key_id}`
  with `block_hash`, `signature` and `public_key_pem`; each entry is `{seq, at, type, subject, payload, payload_hash,
  leaf_hash}`.
- `GET /v1/ledger/entries?subject=<domain>&type=peer.affiliated&limit=1` answers `{entries: [...]}`.
- `GET /v1/ledger/witnesses` answers the witness registry with `key_path`, the path at which this ledger expects a
  witness to publish its key.
- `POST /v1/ledger/witnesses {"domain"}` registers a witness: the ledger fetches `https://<domain><key_path>` and
  records the Ed25519 public key it finds there.
- `POST /v1/ledger/blocks/{height}/cosign {"domain", "block_hash", "signature"}` stores a registered witness's
  signature over a block header.

Peers are addressed by an `https` base URL with no path, query or port. Requests go to public addresses only, follow no
redirects and are bounded in time and size. A ledger never treats its own registrable domain as a peer.

## Verifying a peer's blocks

The follower reads the peer's export from the block after the last head it sealed (from height 0 on first contact),
in pages of 50, and checks every block exactly as it checks its own chain:

- `height` is one more than the previous block's, and `prev_hash` is the previous `block_hash` (64 zeros at height 0);
- `first_seq` continues the previous block's `last_seq`, the entries are numbered without a gap, and `entry_count`
  and `last_seq` agree with them;
- each `payload_hash` is SHA-256 of the canonical JSON of `payload` (keys sorted at every level, no white space);
- each `leaf_hash` is SHA-256 of `"leaf:" + canonical({seq, at, type, subject, payload_hash})`;
- `merkle_root` is the tree of the leaf hashes, a node being SHA-256 of `"node:" + left + right`, an odd node carried
  up unchanged;
- `block_hash` is SHA-256 of `"block:" + canonical(header)`;
- `key_id` is `key_` followed by the first 16 hex characters of SHA-256 of `public_key_pem`, and `signature` is a valid
  Ed25519 signature by that key over `canonical(header)`.

Before reading on, the follower asks the peer again for the block at the height it recorded: the peer must still serve
that very hash.

## What each side seals in its own ledger

All three entry types have the peer's registrable domain as `subject` and carry `spec: "rs-peer-ledger/1"`,
`peer_domain` and `relation: "common_ownership"`.

- **`peer.affiliated`**, once per peer, at first contact, never withdrawn. It also carries `statement` and
  `statement_sha256`, the SHA-256 of this exact text:

  > This ledger and the peer ledger named in this entry are under common ownership. Each verifies the other's chain,
  > records the other's head in its own ledger and cosigns the other's blocks as an affiliated witness. An affiliated
  > witness never counts toward independent finality, and this record cannot be withdrawn.

- **`peer.checkpoint`** `{height, block_hash, key_id, last_seq, verified_from_height}`: the peer's verified head. Sealed
  on first contact, then at most once per checkpoint interval (an hour unless the operator sets another) and only
  when the head has moved on. Because the follower's own ledger
  is itself anchored in Bitcoin and witnessed, the peer's head is now inside the follower's proofs.
- **`peer.conflict`** `{kind, height, recorded_hash, observed_hash, problem}`: evidence that the peer's chain stopped
  extending what was recorded. Sealed once per kind, height and observed hash.

## Conflict and rollback rules

Against the last head it sealed (height H, hash X), the follower seals a conflict when:

| The peer | kind |
|---|---|
| reports a head below H | `rollback` |
| answers, well formed, that it has no sealed block at all (`head: null`, `height: -1`) | `rollback` (an emptied peer) |
| no longer serves block H | `rollback` |
| reports or serves a different hash at H | `fork` |
| serves a block whose `prev_hash` does not link to the block before | `fork` |
| serves a block that fails any other check above | `invalid` |

A conflict is permanent: the follower stops following that peer, keeps the head it recorded as the record, and a peer
cannot erase the evidence by repairing its chain. An answer that cannot be read (an error, a timeout, a body without a
`head` field) is not evidence: it is retried on the next pass and never sealed.

## The handshake, the witness key and cosigning

1. Each side cosigns with a dedicated Ed25519 **peer-witness key**, never its issuer key, so a compromise of one signs
   nothing for the other.
2. Each side cosigns the other only once the other's own ledger records it as common ownership: a `peer.affiliated`
   entry whose `subject` is the cosigner's registrable domain, found in the verified export or through
   `GET /v1/ledger/entries`. A cosignature is therefore labelled affiliated on the receiving side from its first
   appearance.
3. **Key path convention.** The cosigner reads the peer's advertised `key_path` from `GET /v1/ledger/witnesses` and
   serves its peer-witness public key (PEM) at that path on its own domain. Only a path of the form
   `/.well-known/<name>-witness.pem` (`<name>` lowercase letters, digits and dashes) is served, only paths learned from
   a configured peer, and nothing is served there when no peer is configured. Two peers running the same
   implementation advertise the same path, so each serves its peer-witness key at the path it also uses for its own
   witness registry; a witness on a ledger's own domain is always affiliated.
4. The cosigner registers (`POST /v1/ledger/witnesses {"domain"}`) and again at least every 7 days (the receiving side
   accepts a cosignature only from a key it verified within the last 30 days), then posts
   `POST /v1/ledger/blocks/{height}/cosign {"domain", "block_hash", "signature"}` for the verified head, where
   `signature` is base64 Ed25519 over `canonical(header)`, the bytes the issuer signs. Each header commits to every
   earlier block. On `404` (not registered) or `409` (key needs verifying again) it registers once and posts again.
5. The cosigner keeps, per peer, the peer's own answer: the last height and time at which the peer accepted the
   cosignature (`200` or `201`), and the last refusal with its HTTP status and a reason from a fixed list (the peer's
   error code when it is one of `block_not_found`, `witness_not_found`, `witness_key_stale`, `block_hash_mismatch`,
   `invalid_signature`, `bad_signature`, `invalid_domain`, `rate_limited`, `witness_key_unreachable`,
   `invalid_witness_key`, `witness_key_limit`; otherwise `registration_refused`, `unreachable` or `other`). The peer's
   free text is never kept.

## How each side classifies the other's witness

A witness on a sister's registrable domain is `affiliated` with basis `common_ownership`, permanently: from the first
request on a fresh database, before any sync has sealed `peer.affiliated`, whether or not a sync ever succeeds. A key
that appears on any affiliated domain is affiliated on all of them. Such a witness cannot declare itself unaffiliated
(`409 witness_affiliated`, and nothing reaches the ledger), and it never counts toward independent finality or toward
the count of confirmed-unaffiliated witnesses. Public cosignature views name witnesses by label (`W-001`) and key
fingerprint, never by domain.

## What a reader of `GET /v1/ledger/peers` is shown

```json
{
  "spec": "rs-peer-ledger/1",
  "protocol_document": "/specs/rs-peer-ledger-1.md",
  "sisters": [{
    "ledger": "<registrable domain>",
    "url": "https://<peer host>",
    "relation": "common_ownership",
    "disclosure": "… under common ownership …",
    "we_witness_them": {
      "common_ownership_sealed": { "seq": 0, "at": "…" },
      "verified_head": { "height": 0, "block_hash": "…", "key_id": "…", "sealed_at": "…", "seq": 0 },
      "conflicts": 0,
      "our_cosignature": {
        "last_accepted": { "height": 0, "at": "…" },
        "last_refused": { "height": 0, "at": "…", "http_status": 422, "reason": "bad_signature" },
        "rests_on": "The peer's own answer to this ledger's cosign request …"
      }
    },
    "they_witness_us": { "witnesses": 0, "relationship": "affiliated", "cosignatures": 0, "last_cosigned_height": 0, "last_cosigned_at": "…" },
    "counts_toward_finality": false
  }],
  "finality": "Only confirmed-unaffiliated outside witnesses count toward finality. …",
  "check_it": "…"
}
```

`we_witness_them` is what this ledger has verified and sealed of the sister's chain, and whether the sister accepted
this ledger's cosignature. `our_cosignature` rests on the sister's own answer and nothing else: the cosignatures
themselves are on the sister's ledger, at `<peer>/v1/ledger/blocks/{height}/witnesses`. `null` means nothing has been
recorded yet. `they_witness_us` counts the sister's cosignatures stored here. The same object is the MCP tool
`ledger_witnesses` with `{"peers": true}`. Every seal is on the public ledger: `GET /v1/ledger/entries?type=peer.`.

## What the interlock proves, and what it does not

It proves that each ledger's history, as it stood when the other last verified it, is fixed in two ledgers and two
trails of Bitcoin anchors: rewriting or emptying one ledger after that point becomes a two-ledger event, and the other
ledger seals the evidence. It defends against a single compromised server, database or key.

It does not prove independence. The two ledgers are under common ownership, so the same party could in principle
change both; that is why a sister is always affiliated, never counts toward finality, and only confirmed-unaffiliated
outside witnesses do. It says nothing about blocks sealed after the last verified head, and a cosignature the sister
reports as accepted is the sister's statement until you read it on the sister's own ledger.
