fix(blender): improve QR content detection

Fixes a bug where plain text mnemonics and Krux QR codes were being misidentified as encrypted SeedPGP frames.

- The `detectEncryptionMode` function has been rewritten to be stricter and now correctly identifies 'text' as a type.
- It no longer defaults to 'pgp', which was causing plain text to be treated as an encrypted format.
- The `EncryptionMode` type in `types.ts` has been updated to include 'text'.
- This resolves issues where the UI would incorrectly ask for a password for plain text mnemonics and use the wrong decryption logic for Krux QRs.
This commit is contained in:
LC mac
2026-02-04 13:10:12 +08:00
parent c2aeb4ce83
commit 48e8acbe32
2 changed files with 15 additions and 21 deletions

View File

@@ -308,29 +308,23 @@ export async function decryptFromSeed(params: DecryptionParams): Promise<SeedPgp
*/
export function detectEncryptionMode(text: string): EncryptionMode {
const trimmed = text.trim();
// Check for SEEDPGP1 format
// 1. Check for SEEDPGP1 format (most specific)
if (trimmed.startsWith('SEEDPGP1:')) {
return 'pgp';
}
// Check for hex format (Krux KEF)
const cleaned = trimmed.replace(/\s/g, '').replace(/^KEF:/i, '');
if (/^[0-9a-fA-F]+$/.test(cleaned) && cleaned.length % 2 === 0) {
// Try to parse as KEF to confirm
try {
const bytes = hexToBytes(cleaned);
if (bytes.length >= 5) {
const lenId = bytes[0];
if (lenId > 0 && lenId <= 252 && 1 + lenId + 4 <= bytes.length) {
return 'krux';
}
}
} catch {
// Not valid KEF, fall through
}
// 2. Check for Krux KEF format (hex, optional KEF: prefix)
const cleanedHex = trimmed.replace(/\s/g, '').replace(/^KEF:/i, '');
if (/^[0-9a-fA-F]+$/.test(cleanedHex) && cleanedHex.length > 10) { // check for hex and minimum length
return 'krux';
}
// 3. If it contains spaces, it's almost certainly a plain text mnemonic
if (trimmed.includes(' ')) {
return 'text';
}
// Default to PGP for backward compatibility
return 'pgp';
// 4. Default to text for anything else. The component validation will handle it.
return 'text';
}

View File

@@ -20,7 +20,7 @@ export type KruxEncryptionParams = {
version?: number;
};
export type EncryptionMode = 'pgp' | 'krux';
export type EncryptionMode = 'pgp' | 'krux' | 'text';
export type EncryptionParams = {
plaintext: SeedPgpPlaintext | string;