> For the complete documentation index, see [llms.txt](https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/security/architecture.md).

# Architecture

A high-level view of how Black Sheep protects funds.

```
┌──────────────┐         ┌────────────────────────┐         ┌────────────────┐
│  Your app    │ HTTPS   │  Black Sheep API       │ RPC     │  Solana        │
│  (LLM/bot)   │ ──────▶ │  ┌──────────────────┐  │ ──────▶ │  mainnet/devnet│
│              │ Bearer  │  │ Policy engine    │  │         │                │
└──────────────┘ key     │  └──────────────────┘  │         └────────────────┘
                         │  ┌──────────────────┐  │
                         │  │ Signer (in-mem)  │  │
                         │  └──────────────────┘  │
                         │  ┌──────────────────┐  │
                         │  │ Encrypted secret │◀─┼── Vault key (HSM-backed)
                         │  │ store (DB)       │  │
                         │  └──────────────────┘  │
                         └────────────────────────┘
```

## Layers

### 1. Authentication

* API keys are random 256-bit tokens hashed at rest.
* Dashboard auth uses Supabase Auth (httpOnly cookies, refresh rotation).
* SIWS supported for wallet-native sign-in.

### 2. Authorization

* All API routes are user-scoped via Postgres Row Level Security.
* A given API key only ever sees its own account's data — enforced at the database, not just the application layer.

### 3. Policy engine

* Runs server-side inside a transaction.
* Evaluates daily caps, per-tx caps, allowlists, and approval thresholds before any signing happens.
* The LLM can't influence the rules — they live in the database, not in the request.

### 4. Key custody

* Agent secret keys are generated server-side with `tweetnacl`.
* Encrypted with `xchacha20-poly1305` using a per-environment vault key.
* Decrypted only in-memory at signing time. Never logged. Never returned via API.

### 5. Transport

* TLS 1.2+ enforced.
* HSTS on the public domain.

### 6. Audit

* Every policy decision, approval, and signature is logged with user, IP, timestamp, and metadata.
* Audit log is append-only at the database level.

## What we do **not** do

* We don't auto-sweep wallets to a treasury.
* We don't take fees or skim transactions.
* We don't expose secret keys via any API surface.
* We don't allow API keys to read other accounts' data.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://black-sheep-finance.gitbook.io/black-sheep-finance-docs/security/architecture.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
