> ## Documentation Index
> Fetch the complete documentation index at: https://writz.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Architecture Diagrams

> Visual reference for the full Writz Protocol — from Bitcoin locking to ZK proofs on Soroban.

## System Architecture

End-to-end view of how Bitcoin, the SPV Relayer, the four Soroban contracts, and the browser interact.

<img src="https://mintcdn.com/writz/85VI7PIyaucI0WRN/docs/diagrams/output/01-system-architecture.png?fit=max&auto=format&n=85VI7PIyaucI0WRN&q=85&s=6504b26c3c43f9bba339b6913bfe64e4" alt="Writz System Architecture" width="2031" height="1800" data-path="docs/diagrams/output/01-system-architecture.png" />

**Key layers:**

* **Bitcoin** — P2WSH locks collateral; two spending paths (cooperative co-sign or CLTV timelock)
* **SPV Relayer** — watches Bitcoin, produces Merkle inclusion proofs on demand
* **Soroban contracts** — `bitcoin-spv`, `zk-verifier`, `commitment-tree`, `private-lend` verify proofs and manage positions
* **Browser** — generates all ZK proofs locally; private witness values never leave the device

***

## Deposit Flow

Step-by-step path from connecting a Bitcoin wallet to having an active position on Stellar.

<img src="https://mintcdn.com/writz/85VI7PIyaucI0WRN/docs/diagrams/output/02-deposit-flow.png?fit=max&auto=format&n=85VI7PIyaucI0WRN&q=85&s=946056ac099d1e806eb5be7a344b66ae" alt="Deposit Flow" width="2700" height="242" data-path="docs/diagrams/output/02-deposit-flow.png" />

1. Connect Bitcoin wallet (Xverse) and Stellar wallet (Freighter)
2. Derive unique P2WSH address from protocol key + user key + timelock height
3. Send BTC to that address on-chain
4. Relayer produces SPV proof (block headers + Merkle path)
5. Browser generates Groth16 ZK proof binding commitment to the Bitcoin txid
6. `commitment-tree.deposit()` verifies SPV proof + ZK proof on Soroban
7. Admin calls `insert_commitment()` — position becomes active

***

## Borrow & Repay Flow

How the protocol updates the Merkle commitment on every borrow or repay, preserving privacy throughout.

<img src="https://mintcdn.com/writz/85VI7PIyaucI0WRN/docs/diagrams/output/03-borrow-repay-flow.png?fit=max&auto=format&n=85VI7PIyaucI0WRN&q=85&s=bef36387611dd96988de0ebd543ee767" alt="Borrow and Repay Flow" width="1672" height="1650" data-path="docs/diagrams/output/03-borrow-repay-flow.png" />

* **Borrow:** `delta = amount_stroops`, `is_borrow = 1`
* **Repay:** `delta = (FIELD_PRIME − amount) mod FIELD_PRIME`, `is_borrow = 0`
* The nonce rotates on every action; the commitment `Poseidon(collateral, debt, secret, nonce)` is replaced in the tree

***

## BTC Release Flow (Path A)

Cooperative co-signed release after full repayment.

<img src="https://mintcdn.com/writz/85VI7PIyaucI0WRN/docs/diagrams/output/04-btc-release-flow.png?fit=max&auto=format&n=85VI7PIyaucI0WRN&q=85&s=101ba1426316986353828a3d6f3e88f2" alt="BTC Release Flow" width="2700" height="573" data-path="docs/diagrams/output/04-btc-release-flow.png" />

* The server (`/api/cosign`) verifies the commitment is no longer pending before signing
* `PROTOCOL_SIGNING_KEY` is server-only — never exposed to the browser
* Final witness: `[user_sig, protocol_sig, 0x01, redeemScript]`

***

## ZK Circuits

All three Groth16 circuits (Circom / BN254), their private inputs, public inputs, and public outputs.

<img src="https://mintcdn.com/writz/85VI7PIyaucI0WRN/docs/diagrams/output/05-zk-circuits.png?fit=max&auto=format&n=85VI7PIyaucI0WRN&q=85&s=bc6bc07e97038eef113a1380af2c55db" alt="ZK Circuits" width="2700" height="1340" data-path="docs/diagrams/output/05-zk-circuits.png" />

| Circuit               | Purpose                                                             |
| --------------------- | ------------------------------------------------------------------- |
| `deposit.circom`      | Binds a Poseidon commitment to a Bitcoin txid                       |
| `borrow_repay.circom` | Proves Merkle inclusion + valid collateral ratio after state change |
| `liquidation.circom`  | Proves position is under-collateralized                             |

***

## Commitment State Machine

Complete lifecycle of a position from pending to BTC release (or liquidation).

<img src="https://mintcdn.com/writz/85VI7PIyaucI0WRN/docs/diagrams/output/06-commitment-state-machine.png?fit=max&auto=format&n=85VI7PIyaucI0WRN&q=85&s=1f33a5f09d2f984ff30a4b10f3d33323" alt="Commitment State Machine" width="2400" height="408" data-path="docs/diagrams/output/06-commitment-state-machine.png" />

| State               | Description                                             |
| ------------------- | ------------------------------------------------------- |
| **Pending**         | `deposit()` verified, waiting for `insert_commitment()` |
| **Active · debt=0** | Commitment in Merkle tree, no outstanding debt          |
| **Active · debt>0** | USDC borrowed; nonce rotates on every partial repay     |
| **Closed**          | Full repayment done; old nullifier spent                |
| **Released**        | Path A PSBT finalized; BTC returned on Bitcoin          |
| **Liquidated**      | Ratio below 120%; keeper repaid debt                    |

***

## Soroban Contract Interactions

How the four live testnet contracts call each other.

<img src="https://mintcdn.com/writz/85VI7PIyaucI0WRN/docs/diagrams/output/07-contract-interactions.png?fit=max&auto=format&n=85VI7PIyaucI0WRN&q=85&s=28b2b8d4722388090398d4f8cb2f64e1" alt="Contract Interactions" width="2100" height="1031" data-path="docs/diagrams/output/07-contract-interactions.png" />

| Contract          | Role                                                      |
| ----------------- | --------------------------------------------------------- |
| `bitcoin-spv`     | Verifies Bitcoin SPV proofs (block headers + Merkle path) |
| `zk-verifier`     | Groth16 verification via Protocol X-Ray host functions    |
| `commitment-tree` | Merkle tree of commitments; orchestrates all flows        |
| `private-lend`    | USDC liquidity pool; lender deposits + borrower drawdowns |

***

## Regenerating Diagrams

```bash theme={null}
pip3 install graphviz
python3 scripts/diagrams/render-all.py
```

Output: `docs/diagrams/output/*.svg` and `*.png`
