From caf1f126d0569670712e26fa2f6ac3127605cd91 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 28 Feb 2026 15:24:48 -0800 Subject: hash pins --- index.ts | 2 ++ lib/migrate.ts | 28 ++++++++++++++++++++++++++++ routes/users.ts | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/index.ts b/index.ts index 15762f26..d40bae55 100644 --- a/index.ts +++ b/index.ts @@ -8,6 +8,7 @@ import { fixBolt12, listenForLightning, replay } from "$lib/lightning"; import { startHealthCheck } from "$lib/health"; import { getLocations } from "$lib/locations"; import { + hashPins, migrateAccounts, migrateAutowithdraw, migrateBalancesToTB, @@ -49,6 +50,7 @@ try { migrateToMicrosats().then((n) => n && console.log(`Migrated ${n} users to microsats`)); migrateAutowithdraw().then((n) => n && console.log(`Migrated autowithdraw to ${n} accounts`)); migrateFundsToTB().then((n) => n && console.log(`Migrated ${n} funds to TB`)); + hashPins().then((n) => n && console.log(`Hashed ${n} user PINs`)); } catch (e) { console.log(e); } diff --git a/lib/migrate.ts b/lib/migrate.ts index 8ea37a06..f2320390 100644 --- a/lib/migrate.ts +++ b/lib/migrate.ts @@ -206,4 +206,32 @@ export const migrateFundsToTB = async () => { return count; }; +export const hashPins = async () => { + const migrated = await db.get("pins:hashed"); + if (migrated) return 0; + + let count = 0; + + for await (const k of scan("user:*")) { + try { + const raw = await db.get(k); + if (!raw || !(raw as string).startsWith("{")) continue; + + const user = JSON.parse(raw as string); + if (!user.pin || user.pin.length !== 6) continue; + + const hash = new Bun.CryptoHasher("sha256").update(user.pin).digest("hex"); + user.pin = hash; + await s(k as string, user); + count++; + } catch (e) { + warn("pin hash migration error", k, e.message); + } + } + + await db.set("pins:hashed", Date.now().toString()); + l(`Hashed ${count} user PINs`); + return count; +}; + export default migrate; diff --git a/routes/users.ts b/routes/users.ts index 1d0f9bcb..4d07a288 100644 --- a/routes/users.ts +++ b/routes/users.ts @@ -272,7 +272,7 @@ export default { } if (user.pin && !(pin === user.pin)) fail("Pin required"); - if (typeof newpin !== "undefined" && newpin.length === 6) user.pin = newpin; + if (typeof newpin !== "undefined" && (newpin.length === 6 || newpin.length === 64)) user.pin = newpin; if (user.pin === "delete") user.pin = undefined; if (username) { -- cgit v1.2.3