All files / app/hooks use-decrypt-wallet.ts

0% Statements 0/24
0% Branches 0/3
0% Functions 0/4
0% Lines 0/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                                                               
import { RootState } from '@store/index';
import { decryptSoftwareWallet, selectEncryptedMnemonic, selectSalt } from '@store/keys';
import { delay } from '@utils/delay';
import { safeAwait } from '@utils/safe-await';
import { useCallback, useState } from 'react';
import { useSelector } from 'react-redux';
 
const shortDelayToGiveAnimationsTime = async () => delay(100);
 
export function useDecryptWallet() {
  const [isDecrypting, setIsDecrypting] = useState(false);
 
  const { encryptedMnemonic, salt } = useSelector((state: RootState) => ({
    salt: selectSalt(state),
    encryptedMnemonic: selectEncryptedMnemonic(state),
  }));
 
  const decryptWallet = useCallback(
    async (password: string) => {
      Iif (!encryptedMnemonic) throw new Error('`encryptedMnemonic` undefined');
      Iif (!salt) throw new Error('`salt` undefined');
      setIsDecrypting(true);
      await shortDelayToGiveAnimationsTime();
      const [error, decryptedSoftwareWallet] = await safeAwait(
        decryptSoftwareWallet({
          ciphertextMnemonic: encryptedMnemonic,
          salt,
          password,
        })
      );
      setIsDecrypting(false);
      Iif (error) throw error;
      return decryptedSoftwareWallet;
    },
    [encryptedMnemonic, salt]
  );
 
  return { decryptWallet, isDecrypting };
}