System Architecture
End-to-end view of how Bitcoin, the relayer, the Writz v1 contracts (bitcoin-spv, writz-lend) and the browser interact. The privacy edition’s contracts are drawn as a separate group: writz-lend never calls them.

- Bitcoin - P2WSH locks collateral; two spending paths (cooperative co-sign or CLTV timelock)
- Relayer - submits Bitcoin headers to
bitcoin-spv, serves SPV proofs, and co-signs the release PSBT after a full repayment - Soroban contracts -
bitcoin-spvverifies Bitcoin transactions;writz-lendholds plaintext positions, the USDC pool and liquidation, priced by the Reflector oracle - Browser - the Bitcoin wallet signs the key-control proof and the release; the Stellar wallet signs Soroban transactions
Deposit Flow
Step-by-step path from connecting a Bitcoin wallet to having an active position on Stellar.
- Connect Bitcoin wallet (Xverse) and Stellar wallet (Freighter)
- Derive unique P2WSH address from protocol key + user key + timelock height
- Send BTC to that address on-chain
- Relayer serves the SPV proof (block hash + Merkle path + raw transaction)
- The Bitcoin wallet signs the key-control message (BIP-137) for this contract, depositor and txid
writz-lend.deposit()rebuilds the P2WSH, callsbitcoin-spv.verify_transaction, checks the timelock window and the signature, consumes the txid and creates the position
Borrow & Repay Flow
Howwritz-lend handles a borrow or a repayment.

- Borrow: only the depositor; interest accrues first; the post-borrow ratio at the Reflector price must stay at or above 150%, within pool liquidity and the 50,000 USDC total cap
- Repay: partial or full; a full repayment closes the position and emits
repay_full
BTC Release Flow (Path A)
Cooperative co-signed release after full repayment.
- The relayer’s repay watcher reacts to
repay_full, builds the PSBT and co-signs it with the protocol key - The half-signed PSBT is published on
writz-lend(publish_release_psbt) and can be read withget_release_psbteven if Writz is down - Final witness:
[user_sig, protocol_sig, 0x01, redeemScript]
Soroban Contract Interactions
How the Writz v1 testnet contracts call each other.
Privacy edition (in progress): ZK Circuits
These circuits belong to the separate, opt-in privacy edition, in progress and on testnet only. They are not part of Writz v1 and are not used by writz-lend. See Privacy edition (in progress).
The main Groth16 circuits (Circom / BN254), their private inputs, public inputs, and public outputs.

Privacy edition (in progress): Commitment State Machine
Privacy edition only. Writz Lend positions on writz-lend have no commitments: they are plaintext, keyed by the Bitcoin txid, with the statuses Active, Closed and Liquidated.
Complete lifecycle of a privacy edition position from pending to BTC release (or liquidation).

Regenerating Diagrams
docs/diagrams/output/*.svg and *.png