mirror of
https://github.com/block/buzz.git
synced 2026-12-01 05:32:24 +00:00
Frontend half of PLANS/NIP49_LOCAL_BACKUP_PLAN.md Rev 3 (D3): - BackupStep: encrypted by default. The default path never invokes get_nsec — the webview only ever sees the finished ncryptsec1 blob returned by create_ncryptsec_backup. The raw key path survives behind an explicit 'Show raw key instead' click with its previous loading/error/skip semantics. - EncryptedBackupCreator (shared by onboarding + settings): generated 6-word passphrase default with cannot-be-recovered copy, 'choose my own' = min 12 chars + confirmation, create -> masked display + copy + 'Save a copy…' via the native dialog. - encryptedBackup.ts: pure reducer/validation model, unit-tested without a DOM; keyImportInput.ts: HRP classification + submit gating. - NsecMaskedDisplay: kind='nsec'|'ncryptsec' drives labels/aria/testids; existing nsec call sites unchanged. - NostrKeyImportForm: ncryptsec1 paste switches to encrypted mode (passphrase field, no npub preview possible), decrypt errors surface; raw nsec flow untouched. import_identity plumbs the optional password through OnboardingFlow/MachineOnboardingFlow/KeyringLockedScreen. - ProfileSettingsCard: 'Encrypted backup' row alongside the raw reveal. - ncryptsecSourceScan.test.mjs: TS-side source-allowlist tripwire mirroring the Rust scan (defense-in-depth per plan D4). - e2e: mock bridge learns the three backup commands + ncryptsec import; onboarding-backup.spec.ts covers the encrypted happy path (asserting get_nsec is never called), custom-passphrase validation, raw fallback, and error/retry; onboarding.spec.ts adds encrypted-import happy path including a wrong-passphrase rejection. Co-authored-by: Tyler Longwell <tlongwell@block.xyz> Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
/**
|
|
* Pure classification + submit gating for the key-import form, unit-testable
|
|
* without a DOM.
|
|
*
|
|
* `ncryptsec1…` is a NIP-49 encrypted backup: no npub preview is possible
|
|
* (the pubkey is inside the encrypted payload) and a passphrase is required.
|
|
* Full validation happens in Rust at decrypt time; here we only classify by
|
|
* HRP shape so the form can switch modes while the user is mid-paste.
|
|
*/
|
|
|
|
import { nsecToNpub } from "@/shared/lib/nostrUtils";
|
|
|
|
export type KeyImportKind = "nsec" | "ncryptsec" | "unknown";
|
|
|
|
/** Bech32 charset after the `ncryptsec1` HRP; anything else can't decode. */
|
|
const NCRYPTSEC_SHAPE = /^ncryptsec1[02-9ac-hj-np-z]{10,}$/;
|
|
|
|
export function classifyKeyImportInput(input: string): KeyImportKind {
|
|
const trimmed = input.trim();
|
|
if (trimmed.startsWith("ncryptsec1")) return "ncryptsec";
|
|
if (trimmed.startsWith("nsec1")) return "nsec";
|
|
return "unknown";
|
|
}
|
|
|
|
/** Structurally plausible encrypted backup (real validation is in Rust). */
|
|
export function isPlausibleNcryptsec(input: string): boolean {
|
|
return NCRYPTSEC_SHAPE.test(input.trim());
|
|
}
|
|
|
|
/**
|
|
* Whether the import form's submit should be enabled.
|
|
* nsec: must derive an npub. ncryptsec: plausible blob + non-empty passphrase.
|
|
*/
|
|
export function keyImportSubmitEnabled(
|
|
input: string,
|
|
passphrase: string,
|
|
): boolean {
|
|
const kind = classifyKeyImportInput(input);
|
|
if (kind === "ncryptsec") {
|
|
return isPlausibleNcryptsec(input) && passphrase.length > 0;
|
|
}
|
|
return nsecToNpub(input) !== null;
|
|
}
|