Commit Graph
113 Commits
Author SHA1 Message Date
jwinterm e838565ed7 feat: electrum client (webpki TLS, bech32, checked sats)
BTC/LTC Electrum/Fulcrum client, reworked clean from the chain-audit
playbook (not ported). Every byte from a server is treated as hostile.

Security:
- TLS verified against the webpki root store WITH hostname checking; the
  accept-any-certificate verifier is gone (closes the MITM hole). The
  connector is built once and reused.
- Addresses decoded via the `bech32` crate (full checksum + bech32m +
  witness version) and base58check (checksum); the hand-rolled bech32
  underflow panic is removed. Coin/network enforced (reject ltc-on-btc,
  testnet-on-mainnet) — no byte-indexing of untrusted input.
- Amounts pass a fallible checked f64->sat converter (rejects
  non-finite/negative/>2^53) with checked_add summation; no silent
  saturation to 0 / u64::MAX. estimatefee requires finite, clamps.
- Bounded response read + per-exchange timeout (no OOM, no slowloris).
- JSON-RPC response id correlated with the request.
- Untrusted upstream text is control-stripped + length-capped before it
  reaches a (privately-logged) NodeError.

Single `call` primitive centralizes failover + id + envelope mapping;
public methods (balance/utxos/history/tip/verbose-tx/broadcast/fee) are
thin wrappers. Tip-attribution helpers dropped; total_sent_from now
declines (None) without resolved prevouts instead of mislabeling a
receive-with-change as a send.

20 L2 tests: address->script KATs (P2WPKH/P2WSH/taproot/legacy), network
rejection, panic-safety on short/garbage input, checked-sat bounds,
envelope id correlation, bounded/oversized read, duplex round-trip.
2026-06-22 20:57:08 -04:00
jwinterm 611e02e4de test: L1 integration harness (Postgres-backed, fundless)
- shared build_router() (lib) so main + tests exercise identical wiring
- tests/common: spins the real router + AppState against TEST_DATABASE_URL;
  self-skips when unset (cargo test stays green without a DB). Deterministic,
  non-secret test JWT/peppers; ephemeral generated identities only — no funded
  or sensitive wallet seeds (those stay in a separate gitignored L3 harness).
- tests/db_queries: validates migrations + db layer against real Postgres
  (peppered identity lookups, UNIQUE->409, session create/lookup/revoke,
  key upsert/list/delete, restore-attempt counts).
- tests/auth_flow: HTTP flows through the real router (health, auth gate,
  refresh rotation + old-token revocation, username set+lookup, key register+
  fetch, NIP-05 directory).
- CI: new integration job with an ephemeral postgres:16 service.
- 82 tests total (69 unit + 13 integration), all green against live PG.

Complements the monorepo's client-side suite (it tests the wire format the
client sends; the client tests the wallet side); mirrors its secret-handling
(env-driven, gitignored). Mirrors the L0-L5 tiering from docs/engineering.
2026-06-22 18:54:24 -04:00
jwinterm fb3c547d93 feat: website auth, username + key registration, NIP-05 directory
- api/website.rs: sign-in-with-wallet (dapp connect). challenge bound to origin
  + short TTL; verify is single-use (remove-before-verify, race-safe), expiry-
  checked, signature via Result<(),AppError> verifiers (no oracle), resolves an
  EXISTING user by the proven key's pubkey_hash (never mints identity), mints a
  Platform::Web session.
- api/users.rs: username set/get + by-username lookup (JWT-gated, 409 on taken,
  constant-shape lookup); per-asset key registration bound to the JWT user
  (no spoofable user_id) + public key-directory reads.
- api/nip05.rs: /.well-known/nostr.json directory (username -> nostr_pubkey).
- Wired into api/mod.rs; website+users nested at /api/v1, nip05 at root; all
  paths/schemas registered (openapi.json now 17 endpoints).
- main.rs: periodic GC of expired web challenges (bounds the in-memory store).

Adversarially reviewed (register_key authz + website identity-binding both
hold; no critical/high). 69 tests green. Deferred LOW: per-IP rate-limit on the
website routes lands with the router-assembly unit.
2026-06-22 18:13:47 -04:00
jwinterm be23042c0c feat: core auth handlers (register, restore, pow, refresh, logout, me, nostr)
api/middleware.rs: Bearer access-token extraction; client_ip() trusts XFF only
behind config.trusted_proxies (fail-closed against spoofing).
api/auth.rs: extension_register (PoW-gated; derivation-rotation requires a
signature from the on-file key, no fingerprint hijack), check_restore (per-IP +
per-fingerprint rate limits, enumeration/timing-resistant), pow_challenge,
refresh_token (re-checks active session + revoke-then-issue rotation, race-safe),
logout, get_me, nostr_login (login-grade NIP-98, never creates a user, bound to
config public_api_url), nostr_link (JWT + state-change verify_signed_action,
fail-closed nonce stub) + DTOs + routes().
Wired into api/mod.rs + nested at /api/v1 in main.rs; all 8 paths registered in
ApiDoc so openapi.json covers the auth surface (was vacuous).

Drafted+hardened via draft->adversarial-review->revise workflow; independently
re-reviewed (no critical/high). 58 tests, all gates green.

Known LOW (tracked): concurrent first-registration of the same new pubkey_hash
can 500 instead of idempotent re-read; client-retryable.
2026-06-22 17:47:20 -04:00
jwinterm 99cfc49cb2 feat: AppState + server wiring (config, pool, migrations, session manager)
- AppState (lib.rs): Config + Database + SessionManager + in-memory web-challenge
  store; injected into handlers via State<Arc<AppState>>
- main.rs: fail-closed Config::from_env, pg pool, run embedded migrations on
  start, build state, serve /health (auth/wallet routes added as handlers land)
- Cargo.toml: sqlx 'migrate' feature for embedded migrations
2026-06-22 14:36:51 -04:00
jwinterm 3caa678dea feat: db query layer (sessions, keys, audit, grin relay, login, restore)
Six entity query modules on Database, all explicit-column (no SELECT *), legacy
methods dropped:
- sessions: create + active-lookup by (already-peppered) refresh_token_hash,
  revoke/revoke-all (race-aware bool), touch, cleanup-expired
- user_keys: upsert (ON CONFLICT user_id+asset+key_type), list, get-by-asset
- audit: log_action + recent-by-user/action
- grin_slatepacks: relay create/fetch/pending/respond/finalize/cancel/expire
- login_events: salted IP hash, retention sweep, erasure purge (LoginStats only)
- restore_attempts: peppered fingerprint + salted IP (never raw), rate-limit
  counts, 7-day sweep

migrations/...02: login_events + restore_attempts tables (keyed-hash columns,
covering indexes for the aggregations). Shared unique_violation_as + hash_ip
helpers on Database.
2026-06-22 14:32:17 -04:00
jwinterm e01f894c82 chore: remove legacy platform name from session test (de-legacy soft=0) 2026-06-22 14:11:09 -04:00
jwinterm 5d70b80aa9 feat: db layer foundation + Nostr-native user queries
- core/crypto/pepper.rs: domain-separated HMAC peppering of identity values at
  rest (kills the seed-existence oracle: stored pubkey_hash/seed_fingerprint are
  non-reproducible from a candidate seed without the server pepper)
- infra/db/mod.rs: Database wrapper holding the pool + peppers (no Debug derive);
  peppering applied at the data boundary so it cannot drift between call sites
- infra/db/users.rs: Nostr-native query set (create/get/update, by pubkey_hash
  /seed_fingerprint [peppered], by username/nostr_pubkey, get-or-create with
  NULL-only backfill, username + nostr_pubkey UNIQUE -> 409). Explicit columns
  (no SELECT *). Dropped all telegram/discord methods.
2026-06-22 14:09:21 -04:00
jwinterm e182972599 feat: session/JWT + PoW gate (config-driven, strict)
core/session.rs:
- strict JWT Validation: HS256 pinned, exp/sub/aud required, leeway 0
- distinct audiences for access vs refresh so neither is accepted as the other
- Platform -> Extension|Web|Nostr (dropped telegram/discord + platform_user_id)
- refresh-token hash is HMAC-peppered (DB compromise is useless without pepper)
- verify_* return literal error messages (no jsonwebtoken Display oracle)

core/pow.rs: ALTCHA gate driven by config::PowConfig (feature-gated, real HMAC
key required, no fallback); issue/verify/required_for + replay/expiry tests.
2026-06-22 13:57:28 -04:00
jwinterm 9ececb03af feat: NIP-98 auth + wallet-signature verification (hardened)
core/crypto/nip98.rs:
- verify_nip98 (login-grade): binds url+method+freshness
- verify_signed_action (state-change): adds single-use nonce + purpose +
  canonical request-descriptor hash + optional target-id/instance-id, each an
  exactly-one tag, constant-time compared, all checked on the signature-
  authenticated event (id recomputed + schnorr-verified before any tag trusted)
- hardening from adversarial review: length-guard pubkey/id/sig before k256
  construction (no panic on short input), saturating freshness window (no
  overflow wrap); regressions for all three

core/crypto/signatures.rs: BIP-137 ECDSA + Ed25519 verification returning
Result<(),AppError> (Ok only on valid; malformed->400, mismatch->401) — closes
the Ok(false) fail-open.

Cargo.toml: overflow-checks=true in release (defense-in-depth for auth paths).
2026-06-22 13:47:20 -04:00
jwinterm 416fa53c17 feat: Nostr-native data models + initial schema
models/db.rs: User keyed by pubkey_hash/nostr_pubkey/username (pubkey_hash and
seed_fingerprint peppered at rest); Wallet, Session (platform=extension|web|nostr),
UserKey, GrinSlatepack, AuditLog. Dropped all telegram/discord/social-identity
columns and every tip/social/swap struct (swaps land as their own feature unit).

migrations/20260622000001_initial_schema: consolidated clean schema; enum types;
UNIQUE on identity columns; FK ON DELETE (CASCADE/SET NULL) for self-service
erasure; indexes on every lookup column. PostgreSQL 13+.
2026-06-22 07:10:55 -04:00
jwinterm f958aaf0f7 feat: config (fail-closed) + error handling
config.rs (keystone, audited):
- single env reader; validate() FAILS CLOSED on weak/placeholder/missing secrets
  (JWT_SECRET >=32 + non-placeholder, required peppers, PUBLIC_API_URL required
  + url-validated + https-in-prod when Nostr identity on, PoW/admin secrets when
  those features enabled, per-enabled-chain infra secrets hard-fail in prod)
- Nostr-native + feature-flagged; NO telegram/discord/trocador; no dev-fallback keys
- typed sections: auth, identity, secrets(peppers), trusted_proxies, features,
  chains, pow, admin, landing, retention; deployment_mode single|fleet
- secret-bearing structs do not derive Debug; 7 fail-closed validation tests

error.rs: CWE-209 LEAKY/SAFE split (log-private + generic public), error_code
mapping, 5 regression tests.
2026-06-22 07:00:09 -04:00
jwinterm 143f00701b chore: initial foundation — server skeleton, autodocs, CI
- minimal Axum server exposing GET /health
- OpenAPI contract generated from handlers (gen_openapi -> openapi.json),
  drift-gated in CI so the spec can never silently diverge from the code
- CI: secret-scan + gitleaks, openapi drift gate, fmt/clippy/build/test/doc-test, cargo-deny
- Cargo.lock tracked for reproducible builds; publish=false; pinned toolchain
- README, SECURITY.md, deny.toml, .env.example
2026-06-22 06:18:57 -04:00