src/utils/wallet.js
/**
 * Decrypt wallet from given encrypted text and password. It defaults to values from local storage.
 * Throws error in case decryption fails. Calling code should catch errors and display message to the user.
 * @param [encryptedWallet]
 * @param [password]
 */
Decrypt wallet from given encrypted text and password. It defaults to values from local storage.
Throws error in case decryption fails. Calling code should catch errors and display message to the user.
@param [encryptedWallet]
@param [password]
Decrypt wallet from given encrypted text and password . It defaults to values from local storage . Throws error in case decryption fails . Calling code should catch errors and display message to the user . @param [ encryptedWallet ] @param [ password ]
function decryptWalletData(encryptedWallet = null, password = null) {
    if (encryptedWallet === null || encryptedWallet === undefined) {
        encryptedWallet = localStorage.getItem('encrypted_wallet');
    }
    if (password === null || password === undefined) {
        password = localStorage.getItem('password');
    }
    if (!encryptedWallet) {
        throw new Error(`Empty wallet data`);
    }
    const algorithm = 'aes-256-ctr';
    const decryptedWallet = decrypt(encryptedWallet, algorithm, password);
    let parsedWallet;
    try {
        parsedWallet = JSON.parse(decryptedWallet);
    }
    catch (e) {
        throw new Error(`Invalid password or corrupted wallet data`);
    }
    if (!parsedWallet || parsedWallet['version'] !== '1') {
        throw new Error(`Invalid wallet format (expected v1)`);
    }
    return parsedWallet;
}
function decryptWalletData ( encryptedWallet = null , password = null ) { if ( encryptedWallet === null || encryptedWallet === undefined ) { encryptedWallet = localStorage . getItem ( 'encrypted_wallet' ) ; } if ( password === null || password === undefined ) { password = localStorage . getItem ( 'password' ) ; } if ( ! encryptedWallet ) { throw new Error ( ` ` ) ; } const algorithm = 'aes-256-ctr' ; const decryptedWallet = decrypt ( encryptedWallet , algorithm , password ) ; let parsedWallet ; try { parsedWallet = JSON . parse ( decryptedWallet ) ; } catch ( e ) { throw new Error ( ` ` ) ; } if ( ! parsedWallet || parsedWallet [ 'version' ] !== '1' ) { throw new Error ( ` ` ) ; } return parsedWallet ; }
Decrypt wallet from given encrypted text and password.
Decrypt wallet from given encrypted text and password .
// This means we got an invalid JSON. Wrong password or corrupted file (no way to know?) // We got correct decrypt, but wallet is in some unsupported format