Manual
Almnck.
Price: 50ⓐ

Membership Core Identity Loop — Implementation Design

Implementation plan for the first phase of the membership system: identity generation, visit tracking, and balance display.

Parent design: 2026-01-16-membership-system-design.md

Scope

This phase implements: - Identity JS library (keypair generation, localStorage, signing) - FastAPI microservice (/register, /visit, /me) - Name generator with regional whimsy - Meta-right dropdown UI showing name + balance + streak

Not in scope: Payments, encrypted subscriber content, profile page, recovery flows.

Architecture

┌─────────────────────────────────────────────────────────────┐
│                     Static Site (Django)                     │
│  ┌─────────────────────────────────────────────────────────┐│
│  │  <script src="/static/js/identity.js">                  ││
│  │  Meta-right dropdown (Django template)                  ││
│  └─────────────────────────────────────────────────────────┘│
└──────────────────────────┬──────────────────────────────────┘
                           │ fetch()
                           ▼
┌─────────────────────────────────────────────────────────────┐
│                  FastAPI Microservice                        │
│  api/                                                        │
│  ├── main.py              (FastAPI app, routes)             │
│  ├── models.py            (SQLModel/Pydantic schemas)       │
│  ├── db.py                (SQLite via SQLModel)             │
│  ├── auth.py              (signature verification)          │
│  └── names.py             (generator, imports from assets)  │
│                                                              │
│  Database: api/data/members.db (gitignored)                 │
└─────────────────────────────────────────────────────────────┘

assets/names/                (name data files)
├── titles.txt
├── firstnames/              (regional lists)
│   ├── generic.txt
│   ├── japan.txt
│   ├── russia.txt
│   └── ...
├── surnames.txt             (or Markov training data)
└── locations/               (regional lists)
    ├── generic.txt
    ├── japan.txt
    └── ...

Data Models

Member (SQLite via SQLModel)

class Member(SQLModel, table=True):
    pubkey: str = Field(primary_key=True)  # age1...
    name: str                               # "Captain Dmitri Winklebottom of the Volga Basin"
    balance: int = 0                        # almanickels, can go negative
    created_at: datetime
    last_visit: date | None = None
    streak: int = 0

Visit (for heatmap data)

class Visit(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    pubkey: str = Field(foreign_key="member.pubkey")
    visited_on: date
    page_type: str = "article"

API Endpoints

Base URL: http://localhost:8000 (dev), configurable for prod.

POST /register

Register a new member with generated name.

Request:

{"pubkey": "age1..."}

Response:

{"name": "Captain Dmitri Winklebottom of the Volga Basin", "balance": 0, "streak": 0}

Behavior: - Generate whimsical name (server-side, can use IP for regional flavor) - Create Member record with balance=0 - Return profile data

POST /visit

Record a visit, debit if new day.

Request:

{"pubkey": "age1...", "signature": "...", "timestamp": "2026-01-16T12:00:00Z"}

Response:

{"balance": -50, "streak": 5, "debited": true}

Behavior: 1. Verify signature (signed timestamp, ±5 min tolerance) 2. Check if already visited today 3. If new day: debit 50 almanickels, update streak, record Visit 4. If same day: no debit, return current state 5. Streak logic: consecutive days = increment; gap = reset to 1

GET /me

Fetch current profile.

Query params: ?pubkey=age1...&sig=...&ts=...

Response:

{"name": "Captain Dmitri Winklebottom of the Volga Basin", "balance": -50, "streak": 5}

Signature Verification

Client signs the ISO timestamp with their age private key. Server verifies:

  1. Timestamp within ±5 minutes of server time
  2. Signature valid for pubkey + timestamp

Using age for signatures is unconventional (it’s primarily encryption), so we’ll use the X25519 key to derive an Ed25519 signing key, or use a simple HMAC-style challenge. Implementation note: May need to research typage’s signing capabilities or use a separate Ed25519 keypair.

Alternative: Use the age keypair only for identity, derive a signing keypair from it deterministically.

Identity JS Library

Location: src/almnck/static/js/identity.js

Public API

const Identity = {
  // Lifecycle
  async init()           // Load from localStorage or generate new
  async register()       // POST /register, store name

  // Crypto
  getPublicKey()         // Returns age1... string
  async sign(message)    // Signs with private key

  // API helpers
  async visit()          // POST /visit, returns {balance, streak}
  async getMe()          // GET /me, returns profile

  // State
  getName()              // Cached display name
  getBalance()           // Cached balance
  getStreak()            // Cached streak
  isInitialized()        // True if identity exists
}

localStorage Keys

  • almanack:identity — age secret key (AGE-SECRET-KEY-1…)
  • almanack:profile — cached {name, balance, streak}

Page Load Flow

  1. Identity.init() — load keypair from localStorage, or generate new
  2. If new → Identity.register() to get name
  3. Identity.visit() — record visit, refresh balance
  4. UI updates from Identity.getName(), getBalance(), getStreak()

Error Handling

  • API unreachable: Use cached profile, show “Jubilee day!” (no debit)
  • Invalid signature: Re-register (keypair may have been corrupted)

Name Generator

Format: [Title] [Regional Firstname] [Procedural Surname] of [Regional Location]

Data Files (assets/names/)

titles.txt:

Sir
Dame
Lord
Lady
Captain
Admiral
Professor
Doctor
Mx.
Viscount
Baron
Alderman
Magistrate
Rev.

firstnames/generic.txt (fallback):

Archibald
Beatrice
Cornelius
Dorothea
...

firstnames/japan.txt:

Yuki
Haruki
Sakura
Kenji
...

locations/generic.txt:

the Western Reaches
the Northern Counties
the Coastal Provinces
...

Surname Generation

Markov chain or similar for whimsical surnames: - Winklebottom, Bramblethwaite, Crumplestitch, Thistlewood, Skipansicoal

Training data or hand-curated syllable combinations.

Region Detection

Coarse IP geolocation → region code → appropriate firstname/location lists. Falls back to “generic” (Victorian British flavor).

UI: Meta-Right Dropdown

HTML (Django template)

<div class="meta-right">
  <a href="/chronology/">Chronology</a> · <a href="/index/">Index</a>
  <div class="identity-dropdown" id="identity-widget">
    <button class="identity-trigger">
      <span class="greeting">Hello, <span id="member-name">stranger</span>!</span>
      <span class="dropdown-arrow">▼</span>
    </button>
    <div class="dropdown-menu" hidden>
      <div class="balance-row">
        <span class="balance-icon">⚖️</span>
        <span id="member-balance">0</span> almanickels
      </div>
      <div class="streak-row">
        <span class="streak-icon">🔥</span>
        <span id="member-streak">0</span>-day streak
      </div>
      <hr>
      <a href="/profile/">Profile</a>
      <a href="/topup/">Top up</a>
      <hr>
      <a href="/recover/" class="recover-link">Not <span id="member-name-short">you</span>? Recover →</a>
    </div>
  </div>
</div>

Behavior

  • Click trigger → toggle dropdown visibility
  • Click outside → close dropdown
  • On page load → Identity.init() → populate name/balance/streak

Styling

Minimal CSS additions to existing stylesheet. Dropdown absolute-positioned below trigger, matches site aesthetic.

Development Workflow

Makefile Targets

serve-api:
    cd api && uvicorn main:app --reload --port 8000

test-api:
    cd api && pytest

Configuration

Environment variable ALMANACK_API_URL: - Dev: http://localhost:8000 - Prod: https://api.almanack.xyz (future)

Identity.js reads this from a <meta> tag or global config.

File Checklist

New files to create:

api/
├── main.py
├── models.py
├── db.py
├── auth.py
├── names.py
├── requirements.txt
└── data/
    └── .gitkeep

assets/names/
├── titles.txt
├── surnames.txt (or syllables.txt for Markov)
├── firstnames/
│   ├── generic.txt
│   ├── japan.txt
│   ├── russia.txt
│   ├── ireland.txt
│   └── american-south.txt
└── locations/
    ├── generic.txt
    ├── japan.txt
    ├── russia.txt
    ├── ireland.txt
    └── american-south.txt

src/almnck/static/js/
└── identity.js

src/almnck/templates/ (modify existing)
└── base.html (or equivalent with masthead)

Open Questions

  1. Signing with age keys: typage may not expose signing. May need Ed25519 keypair derived from age key, or separate keypair.
  2. CORS: FastAPI needs CORS middleware for cross-origin requests from static site.
  3. IP geolocation: Use a lightweight service/database, or skip regional names for MVP?

Migration Path

Once stable, migrate FastAPI → Cloudflare Workers: - SQLModel → D1 or Durable Object SQLite - Same API contract - Update ALMANACK_API_URL in prod