1 import { CryptoProxy } from '@proton/crypto';
2 import { computeKeyPassword, generateKeySalt } from '@proton/srp';
4 import { getHasMigratedAddressKeys } from '../../lib/keys';
5 import { isPrivate } from '../../lib/user/helpers';
6 import type { APP_NAMES } from '../constants';
7 import { APPS } from '../constants';
8 import { MNEMONIC_STATUS } from '../interfaces';
9 import type { Address, Api, DecryptedKey, User } from '../interfaces';
10 import { srpGetVerify } from '../srp';
11 import { generateMnemonicBase64RandomBytes, generateMnemonicFromBase64RandomBytes } from './bip39Wrapper';
13 export interface MnemonicData {
19 export const generateMnemonicWithSalt = async () => {
20 const salt = generateKeySalt();
21 const randomBytes = generateMnemonicBase64RandomBytes();
22 const mnemonic = await generateMnemonicFromBase64RandomBytes(randomBytes);
31 interface GenerateMnemonicPayloadParams {
34 userKeys: DecryptedKey[];
39 export const generateMnemonicPayload = async ({
45 }: GenerateMnemonicPayloadParams) => {
46 const hashedPassphrase = await computeKeyPassword(randomBytes, salt);
47 const reEncryptedKeys = await Promise.all(
48 userKeys.map(async ({ ID, privateKey }) => {
49 const PrivateKey = await CryptoProxy.exportPrivateKey({
51 passphrase: hashedPassphrase,
60 const { Auth } = await srpGetVerify({
64 password: randomBytes,
69 MnemonicUserKeys: reEncryptedKeys,
75 export const getIsMnemonicAvailable = ({
84 const hasMigratedKeys = getHasMigratedAddressKeys(addresses);
85 const isNonPrivateUser = !isPrivate(user);
86 return hasMigratedKeys && !isNonPrivateUser && app !== APPS.PROTONVPN_SETTINGS;
89 export const getCanReactiveMnemonic = (user: User) => {
91 user.MnemonicStatus === MNEMONIC_STATUS.PROMPT ||
92 user.MnemonicStatus === MNEMONIC_STATUS.ENABLED ||
93 user.MnemonicStatus === MNEMONIC_STATUS.OUTDATED