Skip to main content

SCHEMA

Constant SCHEMA 

Source
const SCHEMA: &str = "
    CREATE TABLE IF NOT EXISTS wan_meta (
        key   TEXT PRIMARY KEY,
        value INTEGER NOT NULL
    );
    -- One row, ever. `singleton` pins it so a concurrent first mint
    -- collides instead of creating a second instance.
    CREATE TABLE IF NOT EXISTS wan_instance (
        singleton   INTEGER PRIMARY KEY CHECK (singleton = 1),
        public_key  TEXT    NOT NULL,
        private_key TEXT    NOT NULL,
        instance_id TEXT    NOT NULL,
        host_hint   TEXT    NOT NULL,
        created_at  INTEGER NOT NULL
    );
    CREATE TABLE IF NOT EXISTS wan_agent_keys (
        agent_id     TEXT    PRIMARY KEY,
        public_key   TEXT    NOT NULL,
        private_key  TEXT    NOT NULL,
        created_at   INTEGER NOT NULL,
        -- 1 when the key was carried over from objects.db's
        -- db_agent_wan_keys rather than minted here.
        imported     INTEGER NOT NULL DEFAULT 0,
        -- D1b: the fingerprint of the certificate confirmed published to the
        -- cloud directory, and when. Superseded by wan_agent_key_publications
        -- (v3), which records WHICH directory; no longer read or written here.
        published_fp TEXT,
        published_at INTEGER
    );
    -- v3: which key fingerprint each directory is confirmed to hold. A
    -- directory is a relay plus the account it was published under
    -- (`wan_publish::directory_id`). A sign-in to another relay or account
    -- simply has no row yet, so the key is pending there — no reset needed,
    -- whichever process or channel of this file did the publishing.
    CREATE TABLE IF NOT EXISTS wan_agent_key_publications (
        agent_id     TEXT    NOT NULL,
        directory    TEXT    NOT NULL,
        key_fp       TEXT    NOT NULL,
        published_at INTEGER NOT NULL,
        PRIMARY KEY (agent_id, directory)
    );
    -- v2 (D2, receiver side) --
    -- Directory records fetched for verification. Self-certifying and
    -- content-addressed by the key fingerprint, so a cached record never
    -- expires; re-checked against its chain on every use.
    CREATE TABLE IF NOT EXISTS wan_peer_records (
        instance_id TEXT    NOT NULL,
        agent_id    TEXT    NOT NULL,
        channel     TEXT    NOT NULL,
        key_fp      TEXT    NOT NULL,
        record      TEXT    NOT NULL,
        fetched_at  INTEGER NOT NULL,
        PRIMARY KEY (instance_id, agent_id, channel, key_fp)
    );
    -- Every verified instance, recorded on first sight (§2.6). approved_at is
    -- written only by the host-gated approval window, which ships disabled;
    -- revoked_at is sticky.
    CREATE TABLE IF NOT EXISTS wan_known_instances (
        instance_id           TEXT    PRIMARY KEY,
        host_hint             TEXT    NOT NULL,
        first_seen_at         INTEGER NOT NULL,
        approved_at           INTEGER,
        revoked_at            INTEGER,
        revocation_checked_at INTEGER
    );
    -- (instance, agent, msgid) already delivered (§2.5). Written after
    -- successful local delivery; pruned once the message could no longer
    -- pass freshness.
    CREATE TABLE IF NOT EXISTS wan_seen_sigs (
        instance_id TEXT    NOT NULL,
        agent_id    TEXT    NOT NULL,
        msg_id      TEXT    NOT NULL,
        expires_at  INTEGER NOT NULL,
        PRIMARY KEY (instance_id, agent_id, msg_id)
    );
";