Skip to content

Commit

Permalink
Feat/confirm signature and encrypt (#1189)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
matstyler authored Jul 29, 2024
1 parent 2e22752 commit ae7c111
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
39 changes: 39 additions & 0 deletions wallets/metamask/src/cypress/configureSynpress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
})

Expand Down
14 changes: 14 additions & 0 deletions wallets/metamask/src/cypress/support/synpressCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ declare global {
renameAccount(currentAccountName: string, newAccountName: string): Chainable<void>

switchNetwork(networkName: string, isTestnet?: boolean): Chainable<void>
providePublicEncryptionKey(): Chainable<void>
decrypt(): Chainable<void>
confirmSignature(): Chainable<void>
}
}
}
Expand All @@ -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)
})
Expand All @@ -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')
})
}
89 changes: 89 additions & 0 deletions wallets/metamask/test/cypress/confirmSignature.cy.ts
Original file line number Diff line number Diff line change
@@ -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'
// )
// })
26 changes: 26 additions & 0 deletions wallets/metamask/test/cypress/encrypt.cy.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit ae7c111

Please sign in to comment.