Last active
June 14, 2022 07:43
-
-
Save m-cakir/e19e0f8c8dfb60727463bcc7791ec0ba to your computer and use it in GitHub Desktop.
MetaMask + Puppeteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dappeteer = require('@chainsafe/dappeteer'); | |
const puppeteer = require('puppeteer'); | |
const SEED = 'hello world its me again'; | |
const PWD = '123456Git'; | |
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | |
const retry = async (fn, count = 3) => { | |
try { | |
return await fn(); | |
} catch (error) { | |
if (count <= 0) { | |
throw error; | |
} | |
return await retry(fn, --count); | |
} | |
} | |
(async () => { | |
const browser = await dappeteer.launch(puppeteer, { metamaskVersion: 'v10.8.1', headless: false, defaultViewport: null }); | |
const metamask = await dappeteer.setupMetamask(browser, { seed: SEED, password: PWD }); | |
const metamaskPage = metamask.page; | |
// wait ::: until account options menu shows up | |
await retry( | |
() => metamaskPage.waitForSelector('button.menu-bar__account-options', { visible: true }) | |
); | |
// click ::: account options menu | |
await retry( | |
() => metamaskPage.click('button.menu-bar__account-options') | |
); | |
// wait ::: until popover menu shows up | |
await retry( | |
() => metamaskPage.waitForSelector('div#popover-content', { visible: true }) | |
); | |
// click ::: account details button | |
await metamaskPage.click('button[data-testid="account-options-menu__account-details"]'); | |
// wait ::: until account details modal shows up | |
await retry( | |
() => metamaskPage.waitForSelector('div.account-details-modal.account-modal', { visible: true }) | |
); | |
// read ::: account address | |
const address = await metamaskPage.$eval('div.qr-code__address', el => el.textContent); | |
// click ::: export pkey button | |
await metamaskPage.click('button.account-details-modal__button:last-child'); | |
// wait ::: until confirmation modal shows up | |
await retry( | |
() => metamaskPage.waitForSelector('input[type=password].export-private-key-modal__password-input', { visible: true }) | |
); | |
// type ::: password input | |
await metamaskPage.type('input[type=password].export-private-key-modal__password-input', PWD); | |
// click ::: confirm button | |
await metamaskPage.click('button.export-private-key-modal__button:last-child'); | |
// wait ::: until seed phrases shows up | |
await retry( | |
() => metamaskPage.waitForSelector('textarea.readonly-input__input.export-private-key-modal__password-display-textarea', { visible: true }) | |
); | |
// read ::: account pkey | |
const pkey = await metamaskPage.$eval('textarea.readonly-input__input.export-private-key-modal__password-display-textarea', el => el.value); | |
// click ::: close button | |
await metamaskPage.click('button.export-private-key-modal__button'); | |
console.log(`account/wallet address: ${address}, pkey: ${pkey}`); | |
await browser.close(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dappeteer = require('@chainsafe/dappeteer'); | |
const puppeteer = require('puppeteer'); | |
const SEED = 'hello world its me again'; | |
const PWD = '123456Git'; | |
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | |
const retry = async (fn, count = 3) => { | |
try { | |
return await fn(); | |
} catch (error) { | |
if (count <= 0) { | |
throw error; | |
} | |
return await retry(fn, --count); | |
} | |
} | |
(async () => { | |
const browser = await dappeteer.launch(puppeteer, { metamaskVersion: 'v10.8.1', headless: false, defaultViewport: null }); | |
const metamask = await dappeteer.setupMetamask(browser, { seed: SEED, password: PWD }); | |
const metamaskPage = metamask.page; | |
// navigate ::: metamask create account page | |
await metamaskPage.goto(metamaskPage.url().endsWith('#new-account') ? metamaskPage.url() : `${metamaskPage.url()}new-account`, { waitUntil: 'networkidle2' }); | |
// wait ::: until create button shows | |
await retry( | |
() => metamaskPage.waitForSelector('button.new-account-create-form__button:last-child', { visible: true }) | |
); | |
// click ::: create button | |
await metamaskPage.click('button.new-account-create-form__button:last-child'); | |
// wait ::: until main page shows | |
await retry( | |
() => metamaskPage.waitForSelector('#app-content .main-container', { visible: true }) | |
); | |
await browser.close(); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm install -s puppeteer | |
$ npm install -s @chainsafe/dappeteer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment