Skip to content

Commit

Permalink
Merge branch 'main' into patch-4
Browse files Browse the repository at this point in the history
  • Loading branch information
Setland34 authored Jan 17, 2025
2 parents b28dad0 + 8ac2116 commit 4232ea4
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 32 deletions.
53 changes: 42 additions & 11 deletions test/e2e/fixture-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ class FixtureBuilder {
}

withPermissionControllerConnectedToTestDapp({
restrictReturnedAccounts = true,
account = '',
useLocalhostHostname = false,
} = {}) {
Expand All @@ -477,7 +476,7 @@ class FixtureBuilder {
id: 'ZaqPEWxyhNCJYACFw93jE',
parentCapability: 'eth_accounts',
invoker: DAPP_URL,
caveats: restrictReturnedAccounts && [
caveats: [
{
type: 'restrictReturnedAccounts',
value: [
Expand All @@ -494,6 +493,42 @@ class FixtureBuilder {
});
}

withPermissionControllerConnectedToTestDappWithChain() {
return this.withPermissionController({
subjects: {
[DAPP_URL]: {
origin: DAPP_URL,
permissions: {
eth_accounts: {
id: 'ZaqPEWxyhNCJYACFw93jE',
parentCapability: 'eth_accounts',
invoker: DAPP_URL,
caveats: [
{
type: 'restrictReturnedAccounts',
value: [DEFAULT_FIXTURE_ACCOUNT.toLowerCase()],
},
],
date: 1664388714636,
},
'endowment:permitted-chains': {
id: 'D7cac0a2e3BD8f349506a',
parentCapability: 'endowment:permitted-chains',
invoker: DAPP_URL,
caveats: [
{
type: 'restrictNetworkSwitching',
value: ['0x539'],
},
],
date: 1664388714637,
},
},
},
},
});
}

withPermissionControllerConnectedToTestDappWithTwoAccounts() {
const subjects = {
[DAPP_URL]: {
Expand Down Expand Up @@ -522,9 +557,7 @@ class FixtureBuilder {
});
}

withPermissionControllerSnapAccountConnectedToTestDapp(
restrictReturnedAccounts = true,
) {
withPermissionControllerSnapAccountConnectedToTestDapp() {
return this.withPermissionController({
subjects: {
[DAPP_URL]: {
Expand All @@ -534,7 +567,7 @@ class FixtureBuilder {
id: 'ZaqPEWxyhNCJYACFw93jE',
parentCapability: 'eth_accounts',
invoker: DAPP_URL,
caveats: restrictReturnedAccounts && [
caveats: [
{
type: 'restrictReturnedAccounts',
value: ['0x09781764c08de8ca82e156bbf156a3ca217c7950'],
Expand All @@ -548,9 +581,7 @@ class FixtureBuilder {
});
}

withPermissionControllerConnectedToTwoTestDapps(
restrictReturnedAccounts = true,
) {
withPermissionControllerConnectedToTwoTestDapps() {
return this.withPermissionController({
subjects: {
[DAPP_URL]: {
Expand All @@ -560,7 +591,7 @@ class FixtureBuilder {
id: 'ZaqPEWxyhNCJYACFw93jE',
parentCapability: 'eth_accounts',
invoker: DAPP_URL,
caveats: restrictReturnedAccounts && [
caveats: [
{
type: 'restrictReturnedAccounts',
value: [
Expand All @@ -580,7 +611,7 @@ class FixtureBuilder {
id: 'AqPEWxyhNCJYACFw93jE4',
parentCapability: 'eth_accounts',
invoker: DAPP_ONE_URL,
caveats: restrictReturnedAccounts && [
caveats: [
{
type: 'restrictReturnedAccounts',
value: [
Expand Down
100 changes: 91 additions & 9 deletions test/e2e/json-rpc/wallet_revokePermissions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
import { strict as assert } from 'assert';
import { ACCOUNT_1, withFixtures } from '../helpers';
import { PermissionConstraint } from '@metamask/permission-controller';
import { withFixtures } from '../helpers';
import FixtureBuilder from '../fixture-builder';
import TestDapp from '../page-objects/pages/test-dapp';
import { loginWithBalanceValidation } from '../page-objects/flows/login.flow';

describe('Revoke Dapp Permissions', function () {
it('should revoke dapp permissions ', async function () {
it('should revoke dapp permissions for "eth_accounts"', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp()
.withPermissionControllerConnectedToTestDappWithChain()
.build(),
title: this.test?.fullTitle(),
},
async ({ driver }) => {
await loginWithBalanceValidation(driver);
const testDapp = new TestDapp(driver);
await testDapp.openTestDappPage();
await testDapp.check_connectedAccounts(ACCOUNT_1);

// wallet_revokePermissions request
const beforeGetPermissionsRequest = JSON.stringify({
jsonrpc: '2.0',
method: 'wallet_getPermissions',
});
const beforeGetPermissionsResult = await driver.executeScript(
`return window.ethereum.request(${beforeGetPermissionsRequest})`,
);
const beforeGetPermissionsNames = beforeGetPermissionsResult.map(
(permission: PermissionConstraint) => permission.parentCapability,
);
assert.deepEqual(beforeGetPermissionsNames, [
'eth_accounts',
'endowment:permitted-chains',
]);

const revokePermissionsRequest = JSON.stringify({
jsonrpc: '2.0',
method: 'wallet_revokePermissions',
Expand All @@ -30,14 +44,82 @@ describe('Revoke Dapp Permissions', function () {
},
],
});
const revokePermissionsResult = await driver.executeScript(
`return window.ethereum.request(${revokePermissionsRequest})`,
);
assert.deepEqual(revokePermissionsResult, null);

const afterGetPermissionsRequest = JSON.stringify({
jsonrpc: '2.0',
method: 'wallet_getPermissions',
});
const afterGetPermissionsResult = await driver.executeScript(
`return window.ethereum.request(${afterGetPermissionsRequest})`,
);
const afterGetPermissionsNames = afterGetPermissionsResult.map(
(permission: PermissionConstraint) => permission.parentCapability,
);
assert.deepEqual(afterGetPermissionsNames, [
'endowment:permitted-chains',
]);
},
);
});

it('should revoke dapp permissions for "endowment:permitted-chains"', async function () {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDappWithChain()
.build(),
title: this.test?.fullTitle(),
},
async ({ driver }) => {
await loginWithBalanceValidation(driver);
const testDapp = new TestDapp(driver);
await testDapp.openTestDappPage();

const beforeGetPermissionsRequest = JSON.stringify({
jsonrpc: '2.0',
method: 'wallet_getPermissions',
});
const beforeGetPermissionsResult = await driver.executeScript(
`return window.ethereum.request(${beforeGetPermissionsRequest})`,
);
const beforeGetPermissionsNames = beforeGetPermissionsResult.map(
(permission: PermissionConstraint) => permission.parentCapability,
);
assert.deepEqual(beforeGetPermissionsNames, [
'eth_accounts',
'endowment:permitted-chains',
]);

const result = await driver.executeScript(
const revokePermissionsRequest = JSON.stringify({
jsonrpc: '2.0',
method: 'wallet_revokePermissions',
params: [
{
'endowment:permitted-chains': {},
},
],
});
const revokePermissionsResult = await driver.executeScript(
`return window.ethereum.request(${revokePermissionsRequest})`,
);
// Response of method call
assert.deepEqual(result, null);
assert.deepEqual(revokePermissionsResult, null);

await testDapp.check_connectedAccounts(ACCOUNT_1, false);
const afterGetPermissionsRequest = JSON.stringify({
jsonrpc: '2.0',
method: 'wallet_getPermissions',
});
const afterGetPermissionsResult = await driver.executeScript(
`return window.ethereum.request(${afterGetPermissionsRequest})`,
);
const afterGetPermissionsNames = afterGetPermissionsResult.map(
(permission: PermissionConstraint) => permission.parentCapability,
);
assert.deepEqual(afterGetPermissionsNames, ['eth_accounts']);
},
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ describe('Snap Account Signatures and Disconnects @no-mmi', function (this: Suit
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp({
restrictReturnedAccounts: false,
})
.build(),
fixtures: new FixtureBuilder().build(),
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
Expand All @@ -49,9 +45,12 @@ describe('Snap Account Signatures and Disconnects @no-mmi', function (this: Suit
await experimentalSettings.check_pageIsLoaded();
await experimentalSettings.toggleRedesignedSignature();

// Open the Test Dapp and signTypedDataV3
// Open the Test Dapp and connect
const testDapp = new TestDapp(driver);
await testDapp.openTestDappPage();
await testDapp.connectAccount({ publicAddress: newPublicKey });

// SignedTypedDataV3 with Test Dapp
await signTypedDataV3WithSnapAccount(driver, newPublicKey, false, true);

// Disconnect from Test Dapp and reconnect to Test Dapp
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/tests/account/snap-account-signatures.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ describe('Snap Account Signatures @no-mmi', function (this: Suite) {
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder()
.withPermissionControllerConnectedToTestDapp({
restrictReturnedAccounts: false,
})
.build(),
fixtures: new FixtureBuilder().build(),
title,
},
async ({ driver }: { driver: Driver }) => {
Expand Down Expand Up @@ -62,8 +58,12 @@ describe('Snap Account Signatures @no-mmi', function (this: Suite) {
await experimentalSettings.check_pageIsLoaded();
await experimentalSettings.toggleRedesignedSignature();

// Connect the SSK account
const testDapp = new TestDapp(driver);
await testDapp.openTestDappPage();
await testDapp.connectAccount({ publicAddress: newPublicKey });

// Run all 5 signature types
await new TestDapp(driver).openTestDappPage();
await personalSignWithSnapAccount(
driver,
newPublicKey,
Expand Down

0 comments on commit 4232ea4

Please sign in to comment.