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

40.81% Statements 20/49
0% Branches 0/1
0% Functions 0/8
42.55% Lines 20/47

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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122  1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x             1x         1x   1x           1x                 1x                                   1x   1x                       1x                                                                 1x                                
import { RootState } from '..';
import { encryptMnemonic, decryptMnemonic } from '../../crypto/key-encryption';
import { generateSalt } from '../../crypto/key-generation';
import { selectMnemonic } from './keys.reducer';
import { TRANSACTION_VERSION, MNEMONIC_ENTROPY } from '@constants/index';
import routes from '@constants/routes.json';
import { createAction, Dispatch } from '@reduxjs/toolkit';
import { HDKey } from '@scure/bip32';
import * as bip39 from '@scure/bip39';
import { generateSecretKey, DerivationType, getStxAddress } from '@stacks/wallet-sdk';
import { deriveAccount } from '@stacks/wallet-sdk/dist/derive';
import {
  persistSalt,
  persistEncryptedMnemonic,
  persistStxAddress,
  persistWalletType,
  persistPublicKey,
} from '@utils/disk-store';
import { push } from 'connected-react-router';
import { useHistory } from 'react-router';
 
type History = ReturnType<typeof useHistory>;
 
export const persistMnemonicSafe = createAction<string>('keys/save-mnemonic-safe');
 
export const persistMnemonic = createAction<string>('keys/save-mnemonic');
 
interface PersistLedgerWalletAction {
  address: string;
  publicKey: string;
}
export const persistLedgerWallet = createAction<PersistLedgerWalletAction>(
  'keys/persist-ledger-wallet'
);
 
interface SetLedgerAddress {
  address: string;
  publicKey: string;
  onSuccess: () => void;
}
export function setLedgerWallet({ address, publicKey, onSuccess }: SetLedgerAddress) {
  return async (dispatch: Dispatch) => {
    await Promise.all([
      persistStxAddress(address),
      persistPublicKey(publicKey),
      persistWalletType('ledger'),
    ]);
    dispatch(persistLedgerWallet({ address, publicKey }));
    onSuccess();
  };
}
 
interface SetPasswordSuccess {
  salt: string;
  encryptedMnemonic: string;
  stxAddress: string;
}
 
export const setPasswordSuccess = createAction<SetPasswordSuccess>('keys/set-password-success');
 
export function onboardingMnemonicGenerationStep({ stepDelayMs }: { stepDelayMs: number }) {
  return (dispatch: Dispatch) => {
    const plaintextMnemonic = generateSecretKey(MNEMONIC_ENTROPY);
    dispatch(persistMnemonicSafe(plaintextMnemonic));
    setTimeout(() => dispatch(push(routes.SECRET_KEY)), stepDelayMs);
  };
}
 
interface SetSoftwareWallet {
  password: string;
  history: History;
}
export function setSoftwareWallet({ password, history }: SetSoftwareWallet) {
  return async (dispatch: Dispatch, getState: () => RootState) => {
    const mnemonic = selectMnemonic(getState());
    const salt = generateSalt();
    const { derivedKeyHash } = await main.deriveKey({ pass: password, salt });
 
    Iif (!mnemonic) return;
 
    const encryptedMnemonic = await encryptMnemonic({ derivedKeyHash, mnemonic });
    const seed = await bip39.mnemonicToSeed(mnemonic);
    const rootNode = HDKey.fromMasterSeed(seed);
    const account = deriveAccount({
      rootNode,
      index: 0,
      salt,
      stxDerivationType: DerivationType.Wallet,
    });
    const address = getStxAddress({ account, transactionVersion: TRANSACTION_VERSION });
    await Promise.all([
      persistStxAddress(address),
      persistSalt(salt),
      persistEncryptedMnemonic(encryptedMnemonic),
    ]);
    dispatch(setPasswordSuccess({ salt, encryptedMnemonic, stxAddress: address }));
    history.push(routes.HOME);
  };
}
 
interface DecryptSoftwareWalletArgs {
  password: string;
  salt: string;
  ciphertextMnemonic: string;
}
export async function decryptSoftwareWallet(args: DecryptSoftwareWalletArgs) {
  const { password, salt, ciphertextMnemonic } = args;
  const { derivedKeyHash } = await main.deriveKey({ pass: password, salt });
  const plaintextMnemonic = await decryptMnemonic({
    encryptedMnemonic: ciphertextMnemonic,
    derivedKeyHash,
  });
  const seed = await bip39.mnemonicToSeed(plaintextMnemonic);
  const rootNode = HDKey.fromMasterSeed(seed);
  return deriveAccount({ rootNode, index: 0, salt, stxDerivationType: DerivationType.Wallet });
  // as {
  //   childKey: BIP32Interface;
  //   address: string;
  //   privateKey: string;
  // };
}