Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat: Encrypt/decrypt using MetaMask #1109

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions wallets/metamask/src/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,30 @@ export class MetaMask {
await this.settingsPage.disableEthSign()
}

async addNewToken() {
if (!this.extensionId) {
throw NO_EXTENSION_ID_ERROR
}

await this.notificationPage.addNewToken(this.extensionId)
}

async providePublicEncryptionKey() {
if (!this.extensionId) {
throw NO_EXTENSION_ID_ERROR
}

await this.notificationPage.providePublicEncryptionKey(this.extensionId)
}

async decrypt() {
if (!this.extensionId) {
throw NO_EXTENSION_ID_ERROR
}

await this.notificationPage.decryptMessage(this.extensionId)
}

/// -------------------------------------------
/// ---------- EXPERIMENTAL FEATURES ----------
/// -------------------------------------------
Expand Down Expand Up @@ -429,12 +453,4 @@ export class MetaMask {
async closeTransactionDetails() {
await this.homePage.closeTransactionDetails()
}

async addNewToken() {
if (!this.extensionId) {
throw NO_EXTENSION_ID_ERROR
}

await this.notificationPage.addNewToken(this.extensionId)
}
}
10 changes: 10 additions & 0 deletions wallets/metamask/src/pages/NotificationPage/actions/encryption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Page } from '@playwright/test'
import Selectors from '../selectors'

export async function providePublicEncryptionKey(notificationPage: Page) {
await notificationPage.locator(Selectors.ActionFooter.confirmActionButton).click()
}

export async function decryptMessage(notificationPage: Page) {
await notificationPage.locator(Selectors.ActionFooter.confirmActionButton).click()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './approvePermission'
export * from './transaction'
export * from './network'
export * from './token'
export * from './encryption'
14 changes: 14 additions & 0 deletions wallets/metamask/src/pages/NotificationPage/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
type GasSetting,
approvePermission,
connectToDapp,
decryptMessage,
network,
providePublicEncryptionKey,
signSimpleMessage,
signStructuredMessage,
token,
Expand Down Expand Up @@ -139,4 +141,16 @@ export class NotificationPage {

await token.addNew(notificationPage)
}

async providePublicEncryptionKey(extensionId: string) {
const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId)

await providePublicEncryptionKey(notificationPage)
}

async decryptMessage(extensionId: string) {
const notificationPage = await getNotificationPageAndWaitForLoad(this.page.context(), extensionId)

await decryptMessage(notificationPage)
}
}
4 changes: 2 additions & 2 deletions wallets/metamask/src/pages/OnboardingPage/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export default {
WalletCreationSuccessPageSelectors,

// 5th Page
PinExtensionPageSelectors
}
PinExtensionPageSelectors,
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export async function getNotificationPageAndWaitForLoad(context: BrowserContext,
let notificationPage = context.pages().find(isNotificationPage)

if (!notificationPage) {
notificationPage = await context.waitForEvent('page', { predicate: isNotificationPage })
notificationPage = await context.waitForEvent('page', {
predicate: isNotificationPage
})
}

// Set pop-up window viewport size to resemble the actual MetaMask pop-up window.
Expand Down
34 changes: 34 additions & 0 deletions wallets/metamask/test/e2e/metamask/encrypt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { testWithMetaMask } from '../testWithMetaMask'

const test = testWithMetaMask

const { expect } = test

test('should provide public encryption key', async ({ page, metamask }) => {
await page.locator('#getEncryptionKeyButton').click()
await metamask.providePublicEncryptionKey()

await expect(page.locator('#encryptionKeyDisplay')).toHaveText('mtrHp1WHZM9rxF2Ilot9Hie5XmQcKCf7oDQ1DpGkTSI=')
})

test('should encrypt and decrypt a message', async ({ page, metamask }) => {
await page.locator('#getEncryptionKeyButton').click()
await metamask.providePublicEncryptionKey()
await expect(page.locator('#encryptionKeyDisplay')).toHaveText('mtrHp1WHZM9rxF2Ilot9Hie5XmQcKCf7oDQ1DpGkTSI=')

// `fill` does not trigger buttons validation, so we use `type` instead
await page.locator('#encryptMessageInput').type('Hello, world')

await page.locator('#encryptButton').click()

const encryptedMessage = await page.locator('#ciphertextDisplay').textContent()

expect(encryptedMessage).toContain('0x7b')

await page.locator('#decryptButton').click()
await metamask.decrypt()

const decryptedMessage = await page.locator('#cleartextDisplay').textContent()

expect(decryptedMessage).toEqual('Hello, world')
})
Loading