diff --git a/src/lib/krux.ts b/src/lib/krux.ts index 8d2b57a..42a75b9 100644 --- a/src/lib/krux.ts +++ b/src/lib/krux.ts @@ -152,3 +152,19 @@ export async function decryptFromKrux(params: { kefData: string; passphrase: str const mnemonic = new TextDecoder().decode(decrypted); return { mnemonic, label, version, iterations }; } + +export function bytesToHex(bytes: Uint8Array): string { + return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('').toUpperCase(); +} + +export async function encryptToKrux(params: { mnemonic: string; passphrase: string; label?: string; iterations?: number; version?: number; }): Promise<{ kefHex: string; label: string; version: number; iterations: number }> { + const { mnemonic, passphrase, label = "Seed Backup", iterations = 200000, version = 21 } = params; + if (!passphrase) throw new Error("Passphrase is required for Krux encryption"); + + const mnemonicBytes = new TextEncoder().encode(mnemonic); + // For encryption, we encode the string label to get the salt bytes + const cipher = new KruxCipher(passphrase, new TextEncoder().encode(label), iterations); + const payload = await cipher.encrypt(mnemonicBytes, version); + const kef = wrap(label, version, iterations, payload); + return { kefHex: bytesToHex(kef), label, version, iterations }; +}