feat(registration): composable gate framework + invite codes (gate #1)

First of the operator-configurable registration gates (PoW-priced restore and
pay-to-register follow). A NEW wallet must present a valid invite code when
REGISTRATION_REQUIRE_INVITE is on; returning wallets and self-hosting bypass it,
and the active gates are advertised via /capabilities so the wallet shapes
onboarding.

- migration + db: invite_codes table storing only sha256(code) (128-bit codes,
  no pepper needed). Redemption is an atomic single-statement claim
  (UPDATE ... WHERE used_at IS NULL RETURNING) — single-use even under concurrent
  submission (L1 test fires 8 concurrent claims; exactly one wins).
- core::invite: generate (base58, 128-bit) + sha256 hash.
- enforce_invite in extension_register, mirroring enforce_pow (returning bypass;
  ONE literal error for not-found/used/expired — never an oracle).
- RegistrationConfig.require_invite + RegistrationCapability in /capabilities.
- smirk-admin mint-invite --count/--label (prints raw codes once; stores hashes).
- L1 tests (single-use + concurrency race), .env.example, OpenAPI (drift clean).
This commit is contained in:
jwinterm
2026-06-30 17:14:53 -04:00
parent 1e08119c5e
commit 610819bb5d
13 changed files with 371 additions and 1 deletions
+33
View File
@@ -26,6 +26,7 @@ use rand::RngCore;
use uuid::Uuid;
use zeroize::Zeroize;
use smirk_backend_core::core::invite::{generate_invite_code, hash_invite_code};
use smirk_backend_core::infra::db::{AddKeyOutcome, Database, RevokeKeyOutcome};
use smirk_backend_core::models::db::{NewAdminAudit, NewAdminKey};
@@ -43,6 +44,7 @@ COMMANDS:
revoke-key --id <uuid>
replace-key --old <uuid> --pubkey <64hex> [--revoke-all]
create-admin-wallet --out <path>
mint-invite [--count <n>] [--label <s>] (registration invite codes)
doctor
";
@@ -93,6 +95,7 @@ async fn run(args: &[String]) -> Result<(), String> {
"revoke-key" => revoke_key(&db, &secret, rest).await,
"replace-key" => replace_key(&db, &secret, rest).await,
"create-admin-wallet" => create_admin_wallet(&db, &secret, rest).await,
"mint-invite" => mint_invite(&db, rest).await,
"doctor" => doctor(&db, &secret).await,
"-h" | "--help" | "help" => {
print!("{USAGE}");
@@ -367,6 +370,36 @@ async fn create_admin_wallet(db: &Database, secret: &str, args: &[String]) -> Re
Ok(())
}
/// Mint single-use registration invite codes. Generates random 128-bit codes,
/// stores only their sha256 hashes, and prints the RAW codes to stdout once for
/// distribution — they are not recoverable afterward (only the hash is kept).
async fn mint_invite(db: &Database, args: &[String]) -> Result<(), String> {
let count: u32 = match flag(args, "--count") {
Some(s) => s
.parse()
.map_err(|_| "--count must be a number".to_string())?,
None => 1,
}
.clamp(1, 1000);
let label = flag(args, "--label");
eprintln!(
"Minting {count} single-use invite code(s){}:",
label
.as_deref()
.map(|l| format!(" [{l}]"))
.unwrap_or_default()
);
for _ in 0..count {
let code = generate_invite_code();
db.insert_invite_code(&hash_invite_code(&code), label.as_deref())
.await
.map_err(|e| format!("insert invite: {e}"))?;
println!("{code}");
}
eprintln!("\nDistribute now — only the hash is stored; raw codes are not recoverable.");
Ok(())
}
async fn doctor(db: &Database, secret: &str) -> Result<(), String> {
println!("database: {}", health(db).await);
let live = db