import logger from '../lib/logger'; import { browser } from '../state/browser'; import click from '../utils/click'; import get from '../utils/get'; import stable from '../utils/stable'; import type from '../utils/type'; import visit from '../utils/visit'; import lockMetaMask from './lockMetaMask'; import showCypress from './showCypress'; import showMetaMask from './showMetaMask'; import unlockMetaMask from './unlockMetaMask'; /** * Reset the MetaMask extension by entering a new mnemonic BIP-39 phrase. * - If MetaMask has not been initialized yet, it goes through the initial onboarding by importing the mnemonic phrase. * - If MetaMask is locked, it will restore the wallet by importing the passphrase. * - If MetaMask is not locked, it will lock MetaMask first. * @param {string} words The new mnemonic phrase. * @param {string} password The new MetaMask password. * @returns {Promise} */ export default async function resetMetaMask(words: string, password: string): Promise { await showMetaMask(); await stable(); // If MetaMask is already unlocked, we need to lock it to be able to import // the wallet from the mnemonic words if (browser.metaMask.url().includes('initialize')) { logger.debug('Initializing MetaMask'); // 1. Welcome page await click('.first-time-flow__button'); // 2. Import an existing wallet using mnemonic words await click('.first-time-flow__button'); await stable(); // 3. Opt-out from MetaMask metrics; we are scripting the extension, and we might generate bad data await click('.metametrics-opt-in [data-testid="page-container-footer-cancel"]'); await stable(); // 4. Fill the import from await type('.first-time-flow__form .first-time-flow__seedphrase input', words); await type('.first-time-flow__form #password', password); await type('.first-time-flow__form #confirm-password', password); await click('.first-time-flow__form .first-time-flow__terms'); await click('.first-time-flow__button'); await stable(); // 5. Finish setup await click('.end-of-flow .first-time-flow__button'); await get('.wallet-overview'); // We can now see the wallets await stable(); // 6. Close popup if present if ((await browser.metaMask.$('.popover-container')) !== null) { await click('.popover-header__button'); await stable(); } // 7. Ensure that we won't be requested to unlock our password on #initialize/unlock page // TODO: investigate about initialize/unlock page await browser.metaMask.waitForTimeout(1000); await lockMetaMask(); await stable(); await browser.metaMask.waitForTimeout(1000); await unlockMetaMask(password); await stable(); await browser.metaMask.waitForTimeout(1000); } else { if (!browser.metaMask.url().includes('unlock')) { logger.debug('MetaMask is unlocked, locking before reset'); await lockMetaMask(); } logger.debug('MetaMask is locked, resetting'); // If MetaMask is locked, it means that it has already been initialized, we // will import the HD wallet using the mnemonic words await visit('/home.html#restore-vault'); // await click('.unlock-page__link--import'); await type('.import-account .import-account__seedphrase', words); await type('.import-account #password', password); await type('.import-account #confirm-password', password); await click('.import-account .first-time-flow__button'); } await showCypress(); return true; }