4
1
mirror of https://github.com/Qortal/qortal-ui.git synced 2025-02-12 10:15:50 +00:00
qortal-ui/qortal-ui-crypto/api/decryptStoredWallet.js

24 lines
923 B
JavaScript
Raw Normal View History

2021-12-25 14:39:47 +01:00
import Base58 from './deps/Base58.js'
import { kdf } from './kdf.js'
import { HmacSha512, AES_CBC } from 'asmcrypto.js'
export const decryptStoredWallet = async (password, wallet, statusFn = () => { }) => {
statusFn('Decoding saved data')
const encryptedSeedBytes = Base58.decode(wallet.encryptedSeed)
const iv = Base58.decode(wallet.iv)
const salt = Base58.decode(wallet.salt)
statusFn('Generating decryption key')
const key = await kdf(password, salt, statusFn)
const encryptionKey = key.slice(0, 32)
const macKey = key.slice(32, 63)
statusFn('Checking key')
const mac = new HmacSha512(macKey).process(encryptedSeedBytes).finish().result
if (Base58.encode(mac) !== wallet.mac) {
throw new Error('Incorrect password')
}
statusFn('Decrypting')
const decryptedBytes = AES_CBC.decrypt(encryptedSeedBytes, encryptionKey, false, iv)
return decryptedBytes
}