4
1
mirror of https://github.com/Qortal/qortal-ui.git synced 2025-02-11 17:55:51 +00:00
qortal-ui/crypto/api/decryptStoredWallet.js

32 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

import Base58 from './deps/Base58'
import { kdf } from './kdf'
import { AES_CBC, HmacSha512 } from 'asmcrypto.js'
import { get, registerTranslateConfig } from '../../core/translate'
2023-06-27 16:21:06 +02:00
registerTranslateConfig({
loader: lang => fetch(`/language/${lang}.json`).then(res => res.json())
2023-06-27 16:21:06 +02:00
})
2021-12-25 14:39:47 +01:00
export const decryptStoredWallet = async (password, wallet, statusFn = () => { }) => {
2023-06-27 16:21:06 +02:00
const sfn1 = get("login.lp12")
statusFn(sfn1)
const encryptedSeedBytes = Base58.decode(wallet.encryptedSeed)
const iv = Base58.decode(wallet.iv)
const salt = Base58.decode(wallet.salt)
2023-06-27 16:21:06 +02:00
const sfn2 = get("login.lp13")
statusFn(sfn2)
const key = await kdf(password, salt, statusFn)
const encryptionKey = key.slice(0, 32)
const macKey = key.slice(32, 63)
2023-06-27 16:21:06 +02:00
const sfn3 = get("login.lp14")
statusFn(sfn3)
const mac = new HmacSha512(macKey).process(encryptedSeedBytes).finish().result
2023-06-27 16:21:06 +02:00
const sfn4 = get("login.lp15")
if (Base58.encode(mac) !== wallet.mac) {
2023-06-27 16:21:06 +02:00
throw new Error(sfn4)
}
2023-06-27 16:21:06 +02:00
const sfn5 = get("login.lp16")
statusFn(sfn5)
2024-03-29 09:00:10 +01:00
return AES_CBC.decrypt(encryptedSeedBytes, encryptionKey, false, iv)
2021-12-25 14:39:47 +01:00
}