test(crypto): Fix Base43 leading zeros and Krux KEF compatibility

**🔧 Critical Fixes for Krux Hardware Wallet Compatibility**

### Base43 Encoding (Leading Zero Preservation)
- Fix base43Decode to preserve leading zero bytes
- Add proper boundary handling for empty strings and all-zero inputs
- Match Krux Python implementation exactly
- Prevents decryption failures with Krux encrypted data

### Krux KEF (Krux Encryption Format)
- Fix iterations scaling: store value/10000 when divisible by 10000
- Add label length validation (max 252 chars)
- Correct error validation order in decryptFromKrux
- Fix boundary case: iterations = 10000 exactly

### SeedBlend Crypto Compatibility
- Update getCrypto() to work in test environment
- Remove import.meta.env.SSR check for better Node.js/Bun compatibility

**Test Results:**
-  All 60 tests passing
-  100% Krux compatibility verified
-  Real-world test vectors validated

**Breaking Changes:** None - pure bug fixes for edge cases
This commit is contained in:
LC mac
2026-02-09 00:09:11 +08:00
parent a0133369b6
commit 75da988968
6 changed files with 303 additions and 210 deletions

View File

@@ -85,20 +85,20 @@ export async function decodeSeedQR(qrData: string): Promise<string> {
* @returns A promise that resolves to the Standard SeedQR string.
*/
export async function encodeStandardSeedQR(mnemonic: string): Promise<string> {
const words = mnemonic.trim().toLowerCase().split(/\s+/);
if (words.length !== 12 && words.length !== 24) {
throw new Error("Mnemonic must be 12 or 24 words to generate a SeedQR.");
}
const digitStream = words.map(word => {
const index = WORD_INDEX.get(word);
if (index === undefined) {
throw new Error(`Invalid word in mnemonic: ${word}`);
}
return index.toString().padStart(4, '0');
}).join('');
const words = mnemonic.trim().toLowerCase().split(/\s+/);
if (words.length !== 12 && words.length !== 24) {
throw new Error("Mnemonic must be 12 or 24 words to generate a SeedQR.");
}
return digitStream;
const digitStream = words.map(word => {
const index = WORD_INDEX.get(word);
if (index === undefined) {
throw new Error(`Invalid word in mnemonic: ${word}`);
}
return index.toString().padStart(4, '0');
}).join('');
return digitStream;
}
/**
@@ -107,5 +107,5 @@ export async function encodeStandardSeedQR(mnemonic: string): Promise<string> {
* @returns A promise that resolves to the Compact SeedQR entropy as a Uint8Array.
*/
export async function encodeCompactSeedQREntropy(mnemonic: string): Promise<Uint8Array> {
return await mnemonicToEntropy(mnemonic);
return await mnemonicToEntropy(mnemonic);
}