From ae7c111cb815dab21a31a0c62141574bcff7ef43 Mon Sep 17 00:00:00 2001 From: Mat <63294765+matstyler@users.noreply.github.com> Date: Mon, 29 Jul 2024 16:04:01 +0200 Subject: [PATCH] Feat/confirm signature and encrypt (#1189) * feat: MetaMask setup in Cypress part 1 * feat: wallet setup is working as expected * fix: gitignore cleanup * fix: clean imports * fix: export from metamask package * fix: dependencies issues * fix: cleanup * fix: test scripts * fix: format * fix: renamed configureBeforeSynpress.ts to configureSynpress.ts * fix: improve metamask/cypress stability * fix: format * fix: cleanup * feat: Implemented getPlaywrightMetamask and addNewAccount * fix: cleanup * feat: Implemented switchNetwork and switchAccount for Cypress * feat: Added renameAccount for Cypress * feat: Cypress API expanded by confirmSignature and encrypt * fix: cypress config cleanup --- .../metamask/src/cypress/configureSynpress.ts | 39 ++++++++ .../src/cypress/support/synpressCommands.ts | 14 +++ .../test/cypress/confirmSignature.cy.ts | 89 +++++++++++++++++++ wallets/metamask/test/cypress/encrypt.cy.ts | 26 ++++++ 4 files changed, 168 insertions(+) create mode 100644 wallets/metamask/test/cypress/confirmSignature.cy.ts create mode 100644 wallets/metamask/test/cypress/encrypt.cy.ts diff --git a/wallets/metamask/src/cypress/configureSynpress.ts b/wallets/metamask/src/cypress/configureSynpress.ts index d90b036b9..6c5ec83da 100644 --- a/wallets/metamask/src/cypress/configureSynpress.ts +++ b/wallets/metamask/src/cypress/configureSynpress.ts @@ -144,6 +144,45 @@ export default function configureSynpress(on: Cypress.PluginEvents, config: Cypr .catch(() => { return false }) + }, + + async providePublicEncryptionKey() { + const metamask = getPlaywrightMetamask(context, metamaskExtensionPage, metamaskExtensionId) + + return await metamask + .providePublicEncryptionKey() + .then(() => { + return true + }) + .catch(() => { + return false + }) + }, + + async decrypt() { + const metamask = getPlaywrightMetamask(context, metamaskExtensionPage, metamaskExtensionId) + + return await metamask + .decrypt() + .then(() => { + return true + }) + .catch(() => { + return false + }) + }, + + async confirmSignature() { + const metamask = getPlaywrightMetamask(context, metamaskExtensionPage, metamaskExtensionId) + + return await metamask + .confirmSignature() + .then(() => { + return true + }) + .catch(() => { + return false + }) } }) diff --git a/wallets/metamask/src/cypress/support/synpressCommands.ts b/wallets/metamask/src/cypress/support/synpressCommands.ts index 0c4154d3e..000c60d1c 100644 --- a/wallets/metamask/src/cypress/support/synpressCommands.ts +++ b/wallets/metamask/src/cypress/support/synpressCommands.ts @@ -22,6 +22,9 @@ declare global { renameAccount(currentAccountName: string, newAccountName: string): Chainable switchNetwork(networkName: string, isTestnet?: boolean): Chainable + providePublicEncryptionKey(): Chainable + decrypt(): Chainable + confirmSignature(): Chainable } } } @@ -37,6 +40,7 @@ export default function synpressCommands() { Cypress.Commands.add('connectToDapp', () => { return cy.task('connectToDapp') }) + Cypress.Commands.add('addNewAccount', (accountName: string) => { return cy.task('addNewAccount', accountName) }) @@ -46,7 +50,17 @@ export default function synpressCommands() { Cypress.Commands.add('renameAccount', (currentAccountName: string, newAccountName: string) => { return cy.task('renameAccount', { currentAccountName, newAccountName }) }) + Cypress.Commands.add('switchNetwork', (networkName: string, isTestnet = false) => { return cy.task('switchNetwork', { networkName, isTestnet }) }) + Cypress.Commands.add('providePublicEncryptionKey', () => { + return cy.task('providePublicEncryptionKey') + }) + Cypress.Commands.add('decrypt', () => { + return cy.task('decrypt') + }) + Cypress.Commands.add('confirmSignature', () => { + return cy.task('confirmSignature') + }) } diff --git a/wallets/metamask/test/cypress/confirmSignature.cy.ts b/wallets/metamask/test/cypress/confirmSignature.cy.ts new file mode 100644 index 000000000..cfbc81727 --- /dev/null +++ b/wallets/metamask/test/cypress/confirmSignature.cy.ts @@ -0,0 +1,89 @@ +before(() => { + cy.get('#connectButton').click() + cy.connectToDapp() +}) + +it('should confirm `personal_sign`', () => { + cy.get('#personalSign').click() + + cy.confirmSignature() + + cy.get('#personalSignResult').should( + 'have.text', + '0xf95b3efc808585303e20573e960993cde30c7f5a0f1c25cfab0379d5a14311d17898199814c8ebe66ec80b2b11690f840bde539f862ff4f04468d2a40f15178a1b' + ) + + cy.get('#personalSignVerify').click() + + cy.get('#personalSignVerifySigUtilResult').should('have.text', '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266') + cy.get('#personalSignVerifyECRecoverResult').should('have.text', '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266') +}) + +it('should confirm `eth_signTypedData`', () => { + cy.get('#signTypedData').click() + + cy.confirmSignature() + + cy.get('#signTypedDataResult').should( + 'have.text', + '0xd75eece0d337f4e425f87bd112c849561956afe4f154cdd07d1d4cba7a979b481ba6ceede5c0eb9daa66bec4eea6e7ecfee5496274ef2a93b69abd97531519b21c' + ) + + cy.get('#signTypedDataVerify').click() + + cy.get('#signTypedDataVerifyResult').should('have.text', '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266') +}) + +it('should confirm `eth_signTypedData_v3`', () => { + cy.get('#signTypedDataV3').click() + + cy.confirmSignature() + + cy.get('#signTypedDataV3Result').should( + 'have.text', + '0x6ea8bb309a3401225701f3565e32519f94a0ea91a5910ce9229fe488e773584c0390416a2190d9560219dab757ecca2029e63fa9d1c2aebf676cc25b9f03126a1b' + ) + + cy.get('#signTypedDataV3Verify').click() + + cy.get('#signTypedDataV3VerifyResult').should('have.text', '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266') +}) + +it('should confirm `eth_signTypedData_v4`', () => { + cy.get('#signTypedDataV4').click() + + cy.confirmSignature() + + cy.get('#signTypedDataV4Result').should( + 'have.text', + '0x1cf422c4a319c19ecb89c960e7c296810278fa2bef256c7e9419b285c8216c547b3371fa1ec3987ce08561d3ed779845393d8d3e4311376d0bc0846f37d1b2821c' + ) + + cy.get('#signTypedDataV4Verify').click() + + cy.get('#signTypedDataV4VerifyResult').should('have.text', '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266') +}) + +// TODO: `unsafe_enableEthSign` needs to be implemented +// it('should confirm `eth_sign`', () => { +// await metamask.unsafe_enableEthSign() +// +// cy.get('#ethSign').click() +// +// await metamask.confirmSignatureWithRisk() +// +// await expect(page.locator('#ethSignResult')).toContainText( +// '0xbfefd81020331aa2869403ba11711f082506b9c9313c29a212975067123ca222536ba40b17d8847356cc4ee448fb088231db98632e745e469f7e3d142e4256541b' +// ) +// }) +// +// it('should not be permitted to confirm `eth_sign`', () => { +// await metamask.unsafe_enableEthSign() +// await metamask.disableEthSign() +// +// cy.get('#ethSign').click() +// +// await expect(page.locator('#ethSign')).toContainText( +// 'Error: eth_sign has been disabled. You must enable it in the advanced settings' +// ) +// }) diff --git a/wallets/metamask/test/cypress/encrypt.cy.ts b/wallets/metamask/test/cypress/encrypt.cy.ts new file mode 100644 index 000000000..7eaab4479 --- /dev/null +++ b/wallets/metamask/test/cypress/encrypt.cy.ts @@ -0,0 +1,26 @@ +it('should provide public encryption key', () => { + cy.get('#getEncryptionKeyButton').click() + cy.providePublicEncryptionKey().then(() => { + cy.get('#encryptionKeyDisplay').should('have.text', '4Yhh35o6z4dK2m5Hua4GltZ7IU9DdJPjLaspm6VDwHY=') + }) +}) + +it('should encrypt and decrypt a message', () => { + cy.get('#getEncryptionKeyButton').click() + cy.providePublicEncryptionKey().then(() => { + cy.get('#encryptionKeyDisplay').should('have.text', '4Yhh35o6z4dK2m5Hua4GltZ7IU9DdJPjLaspm6VDwHY=') + }) + + // `fill` does not trigger buttons validation, so we use `type` instead + cy.get('#encryptMessageInput').type('Hello, world') + + cy.get('#encryptButton').click() + + cy.get('#ciphertextDisplay').contains('0x7b') + + cy.get('#decryptButton').click() + + cy.decrypt().then(() => { + cy.get('#cleartextDisplay').contains('Hello, world') + }) +})