Skip to content

Commit

Permalink
✨ feat(metamask): Add support for eth_signTypedData (#991)
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception authored Nov 11, 2023
1 parent b3e5b31 commit f6005c5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
4 changes: 2 additions & 2 deletions wallets/metamask/src/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export class MetaMask {
if (!this.extensionId) {
throw NO_EXTENSION_ID_ERROR
}
await this.notificationPage.signPersonalMessage(this.extensionId)
await this.notificationPage.signSimpleMessage(this.extensionId)
}

async rejectSignature() {
if (!this.extensionId) {
throw NO_EXTENSION_ID_ERROR
}
await this.notificationPage.rejectPersonalMessage(this.extensionId)
await this.notificationPage.rejectSimpleMessage(this.extensionId)
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './connectToDapp'
export * from './personalSign'
export * from './signSimpleMessage'
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import type { BrowserContext } from '@playwright/test'
import { getNotificationPage } from '../../../utils/getNotificationPage'
import Selectors from '../selectors'

const signPersonalMessage = async (context: BrowserContext, extensionId: string) => {
const signMessage = async (context: BrowserContext, extensionId: string) => {
const notificationPage = await getNotificationPage(context, extensionId)

await notificationPage.locator(Selectors.SignaturePage.signButton).click()
}

const rejectPersonalMessage = async (context: BrowserContext, extensionId: string) => {
const rejectMessage = async (context: BrowserContext, extensionId: string) => {
const notificationPage = await getNotificationPage(context, extensionId)

await notificationPage.locator(Selectors.SignaturePage.rejectButton).click()
}

export const personalSign = {
sign: signPersonalMessage,
reject: rejectPersonalMessage
// Used for:
// - `personal_sign`
// - `eth_signTypedData`
export const signSimpleMessage = {
sign: signMessage,
reject: rejectMessage
}
10 changes: 5 additions & 5 deletions wallets/metamask/src/pages/NotificationPage/page.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Page } from '@playwright/test'
import { connectToDapp, personalSign } from './actions'
import { connectToDapp, signSimpleMessage } from './actions'

export class NotificationPage {
readonly page: Page
Expand All @@ -12,11 +12,11 @@ export class NotificationPage {
await connectToDapp(this.page.context(), extensionId)
}

async signPersonalMessage(extensionId: string) {
await personalSign.sign(this.page.context(), extensionId)
async signSimpleMessage(extensionId: string) {
await signSimpleMessage.sign(this.page.context(), extensionId)
}

async rejectPersonalMessage(extensionId: string) {
await personalSign.reject(this.page.context(), extensionId)
async rejectSimpleMessage(extensionId: string) {
await signSimpleMessage.reject(this.page.context(), extensionId)
}
}
18 changes: 18 additions & 0 deletions wallets/metamask/test/e2e/metamask/confirmSignature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,21 @@ test('should confirm `personal_sign`', async ({ context, metamaskPage, page, ext
'0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
)
})

test('should confirm `eth_signTypedData`', async ({ context, metamaskPage, page, extensionId }) => {
const metamask = new MetaMask(context, metamaskPage, connectedSetup.walletPassword, extensionId)

await page.goto('https://metamask.github.io/test-dapp/')

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

await metamask.confirmSignature()

await expect(page.locator('#signTypedDataResult')).toHaveText(
'0xd75eece0d337f4e425f87bd112c849561956afe4f154cdd07d1d4cba7a979b481ba6ceede5c0eb9daa66bec4eea6e7ecfee5496274ef2a93b69abd97531519b21c'
)

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

await expect(page.locator('#signTypedDataVerifyResult')).toHaveText('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266')
})
14 changes: 14 additions & 0 deletions wallets/metamask/test/e2e/metamask/rejectSignature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ test('should reject `personal_sign`', async ({ context, metamaskPage, page, exte
)
await expect(page.locator('#personalSignResult')).toHaveText('')
})

test('should reject `eth_signTypedData`', async ({ context, metamaskPage, page, extensionId }) => {
const metamask = new MetaMask(context, metamaskPage, connectedSetup.walletPassword, extensionId)

await page.goto('https://metamask.github.io/test-dapp/')

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

await metamask.rejectSignature()

await expect(page.locator('#signTypedDataResult')).toHaveText(
'Error: MetaMask Message Signature: User denied message signature.'
)
})

0 comments on commit f6005c5

Please sign in to comment.