# Data Persistence

KayakNet persists all data locally to survive restarts and provide history.

## Data Directory

Default location: `./data/`

```
data/
├── identity.key          # Node identity (Ed25519 keypair)
├── peers.json            # Known peer addresses
├── config.json           # Runtime configuration
├── dht/
│   └── records.json      # DHT records
├── chat/
│   ├── rooms/
│   │   └── #general.json # Room messages
│   └── dms/
│       └── <node_id>.json # Direct messages
├── market/
│   ├── listings.json     # All listings
│   └── my_listings.json  # Your listings
├── escrow/
│   ├── orders.json       # Order records
│   └── wallets.json      # Crypto wallet data
├── names/
│   └── domains.json      # .kyk registrations
└── cache/
    └── ...               # Temporary cache
```

## Identity Persistence

### identity.key

```json
{
  "private_key": "<base64 encoded Ed25519 private key>",
  "public_key": "<base64 encoded public key>",
  "node_id": "<hex encoded node ID>",
  "created_at": "2026-01-15T10:00:00Z"
}
```

**Important:**

* Back this up!
* Losing it = new identity
* Never share with anyone

## Chat Persistence

### Room Messages

`data/chat/rooms/#general.json`:

```json
{
  "room": "#general",
  "messages": [
    {
      "id": "msg_abc123",
      "sender_id": "node_123...",
      "sender_name": "alice",
      "content": "Hello!",
      "timestamp": 1234567890,
      "signature": "..."
    }
  ],
  "last_sync": "2026-01-15T10:00:00Z"
}
```

### Direct Messages

`data/chat/dms/<recipient_id>.json`:

```json
{
  "peer_id": "node_456...",
  "peer_name": "bob",
  "messages": [
    {
      "id": "dm_xyz789",
      "from_me": true,
      "content": "Private message",
      "timestamp": 1234567890,
      "read": true
    }
  ]
}
```

## Marketplace Persistence

### Listings

`data/market/listings.json`:

```json
{
  "listings": [
    {
      "id": "listing_abc",
      "title": "Product",
      "price": 99.99,
      "seller_id": "node_123...",
      "created_at": "2026-01-15T10:00:00Z",
      "synced_at": "2026-01-15T10:05:00Z"
    }
  ]
}
```

## Escrow Persistence

### Orders

`data/escrow/orders.json`:

```json
{
  "orders": [
    {
      "id": "esc_123",
      "order_id": "ord_456",
      "listing_id": "listing_abc",
      "buyer_id": "node_buyer...",
      "seller_id": "node_seller...",
      "amount": 0.5,
      "currency": "XMR",
      "state": "funded",
      "payment_address": "...",
      "created_at": "2026-01-15T10:00:00Z"
    }
  ]
}
```

## Domain Persistence

### Registered Domains

`data/names/domains.json`:

```json
{
  "domains": [
    {
      "name": "mysite.kyk",
      "target": "node_id:abc123...",
      "owner": "<public_key>",
      "registered_at": "2026-01-15T10:00:00Z",
      "signature": "..."
    }
  ]
}
```

## Sync Mechanism

### On Startup

1. Load local data from files
2. Connect to network
3. Request updates from peers
4. Merge with local data
5. Save merged state

### During Runtime

* Changes saved immediately
* Background sync every 5 minutes
* Conflict resolution by timestamp

### Conflict Resolution

* Newer timestamp wins
* For listings: Seller's version wins
* For domains: First registration wins

## Backup & Recovery

### Creating Backup

```bash
# Backup entire data directory
tar -czf kayaknet-backup.tar.gz ./data/

# Or just identity
cp data/identity.key identity-backup.key
```

### Restoring Backup

```bash
# Full restore
tar -xzf kayaknet-backup.tar.gz

# Identity only
cp identity-backup.key data/identity.key
```

### What to Backup

| File                     | Priority | Notes               |
| ------------------------ | -------- | ------------------- |
| identity.key             | Critical | Cannot recover      |
| names/domains.json       | High     | Your domains        |
| escrow/orders.json       | High     | Transaction records |
| chat/                    | Medium   | Message history     |
| market/my\_listings.json | Medium   | Your listings       |
| peers.json               | Low      | Rebuilds quickly    |

## Storage Limits

### Default Limits

| Data Type              | Limit  |
| ---------------------- | ------ |
| Chat messages per room | 10,000 |
| DM conversations       | 1,000  |
| Listings cache         | 5,000  |
| DHT records            | 10,000 |

### Cleanup

Old data is automatically cleaned:

* Expired DHT records: Deleted on expiry
* Old messages: Keep last N
* Failed escrows: Archive after 30 days

## Custom Data Directory

```bash
./kayakd --data-dir /custom/path/data
```

Or environment variable:

```bash
export KAYAKNET_DATA_DIR=/custom/path/data
```

## Database Format

Currently using JSON files for simplicity.

Future plans:

* SQLite for large datasets
* LevelDB for DHT
* Encrypted storage option
