All files / app/store/keys keys.reducer.ts

65.62% Statements 21/32
50% Branches 2/4
26.66% Functions 4/15
79.16% Lines 19/24

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68  1x             1x                         1x           1x 3x 3x   2x 1x   1x   1x                           1x 1x       1x 1x 1x 1x       1x 1x 1x      
import { RootState } from '..';
import {
  setPasswordSuccess,
  persistLedgerWallet,
  persistMnemonicSafe,
  persistMnemonic,
} from './keys.actions';
import { WalletType } from '@models/wallet-type';
import { createReducer, createSelector } from '@reduxjs/toolkit';
 
export interface KeysState {
  walletType: WalletType;
  mnemonic: string | null;
  decrypting: boolean;
  salt?: string;
  decryptionError?: string;
  encryptedMnemonic?: string;
  stxAddress?: string;
  publicKey?: string;
}
 
const initialState: Readonly<KeysState> = Object.freeze({
  mnemonic: null,
  decrypting: false,
  walletType: 'software',
});
 
export const createKeysReducer = (keys: Partial<KeysState> = {}) =>
  createReducer({ ...initialState, ...keys }, builder =>
    builder
      .addCase(persistMnemonicSafe, (state, action) => {
        if (state.mnemonic !== null) {
          return state;
        }
        return { ...state, mnemonic: action.payload };
      })
      .addCase(persistMnemonic, (state, action) => ({ ...state, mnemonic: action.payload }))
      .addCase(setPasswordSuccess, (state, { payload }) => ({
        ...state,
        ...payload,
        mnemonic: null,
      }))
      .addCase(persistLedgerWallet, (state, { payload }) => ({
        ...state,
        stxAddress: payload.address,
        publicKey: payload.publicKey,
        walletType: 'ledger',
      }))
  );
 
export const selectKeysSlice = (state: RootState) => state.keys;
export const selectDecryptionError = createSelector(
  selectKeysSlice,
  state => state.decryptionError
);
export const selectIsDecrypting = createSelector(selectKeysSlice, state => state.decrypting);
export const selectMnemonic = createSelector(selectKeysSlice, state => state.mnemonic);
export const selectWalletType = createSelector(selectKeysSlice, state => state.walletType);
export const selectEncryptedMnemonic = createSelector(
  selectKeysSlice,
  state => state.encryptedMnemonic
);
export const selectAddress = createSelector(selectKeysSlice, state => state.stxAddress);
export const selectSalt = createSelector(selectKeysSlice, state => state.salt);
export const selectPublicKey = createSelector(selectKeysSlice, state =>
  state.publicKey ? Buffer.from(state.publicKey, 'hex') : null
);