-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(metamask): Add support for importing private keys
- Loading branch information
1 parent
2683d0e
commit c06af63
Showing
4 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
61 changes: 61 additions & 0 deletions
61
wallets/metamask/test/e2e/metamask/importWalletFromPrivateKey.spec.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { testWithSynpress } from 'fixtures' | ||
import { MetaMask, unlockForFixture } from '../../../src' | ||
|
||
import basicSetup from '../wallet-setup/basic.setup' | ||
|
||
const test = testWithSynpress(basicSetup, unlockForFixture) | ||
|
||
const { expect } = test | ||
|
||
test.use({ | ||
permissions: ['clipboard-read'] | ||
}) | ||
|
||
test('should import a new wallet from private key', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
if (await metamaskPage.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton).isVisible()) { | ||
await metamaskPage.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton).click() | ||
} | ||
|
||
await metamask.importWalletFromPrivateKey('ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a') | ||
|
||
await metamaskPage.locator(metamask.homePage.selectors.account).click() | ||
|
||
const accountAddressInClipboard = await metamaskPage.evaluate('navigator.clipboard.readText()') | ||
expect(accountAddressInClipboard).toContain('0xa2ce797cA71d0EaE1be5a7EffD27Fd6C38126801') | ||
}) | ||
|
||
test('should throw an error if trying to import private key for the 2nd time', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
if (await metamaskPage.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton).isVisible()) { | ||
await metamaskPage.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton).click() | ||
} | ||
|
||
const privateKey = 'ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a' | ||
|
||
await metamask.importWalletFromPrivateKey(privateKey) | ||
|
||
await metamaskPage.locator(metamask.homePage.selectors.account).click() | ||
|
||
const importWalletPromise = metamask.importWalletFromPrivateKey(privateKey) | ||
|
||
await expect(importWalletPromise).rejects.toThrowError( | ||
'[ImportWalletFromPrivateKey] Importing failed due to error: The account you are trying to import is a duplicate' | ||
) | ||
}) | ||
|
||
test('should throw an error if the private key is invalid', async ({ context, metamaskPage }) => { | ||
const metamask = new MetaMask(context, metamaskPage, basicSetup.walletPassword) | ||
|
||
if (await metamaskPage.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton).isVisible()) { | ||
await metamaskPage.locator(metamask.homePage.selectors.recoveryPhraseReminder.gotItButton).click() | ||
} | ||
|
||
const importWalletPromise = metamask.importWalletFromPrivateKey('0xdeadbeef') | ||
|
||
await expect(importWalletPromise).rejects.toThrowError( | ||
'[ImportWalletFromPrivateKey] Importing failed due to error: Expected private key to be an Uint8Array with length 32' | ||
) | ||
}) |