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

Making permission and approval controller methods idempotent #15848

Merged
merged 96 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
3e59ba6
Making permission and approval controller methods idempotent
jpuri Sep 14, 2022
a1dc291
Adding unit test coverage
jpuri Sep 15, 2022
9868acc
fix
jpuri Sep 15, 2022
f6f79ba
Merge branch 'develop' into permission_approval_method_idempotent
jpuri Sep 15, 2022
6a09689
Merge branch 'develop' of github.com:MetaMask/metamask-extension into…
jpuri Sep 16, 2022
92ed8fe
fix build
jpuri Sep 16, 2022
4448926
Merge branch 'permission_approval_method_idempotent' of github.com:Me…
jpuri Sep 16, 2022
a884b52
fix
jpuri Sep 16, 2022
1c417a5
Upgrade controller
jpuri Sep 16, 2022
b397a1a
Merge branch 'develop' into permission_approval_method_idempotent
jpuri Sep 16, 2022
9b67e11
Upgrade controller
jpuri Sep 16, 2022
286c4e2
Merge branch 'permission_approval_method_idempotent' of github.com:Me…
jpuri Sep 16, 2022
b8e15db
Upgrade controller
jpuri Sep 16, 2022
af037f0
Merge branch 'develop' into permission_approval_method_idempotent
jpuri Sep 16, 2022
1297f81
Merge branch 'develop' of github.com:MetaMask/metamask-extension into…
jpuri Sep 19, 2022
7031cb0
Merge branch 'permission_approval_method_idempotent' of github.com:Me…
jpuri Sep 19, 2022
8c967d1
Merge branch 'develop' of github.com:MetaMask/metamask-extension into…
jpuri Sep 21, 2022
6ead266
mocking
jpuri Sep 22, 2022
46beb32
Merge branch 'develop' into permission_approval_method_idempotent
jpuri Sep 22, 2022
9bc960d
mocking
jpuri Sep 22, 2022
71e302d
Merge branch 'permission_approval_method_idempotent' of github.com:Me…
jpuri Sep 22, 2022
b294c6d
merge
jpuri Sep 22, 2022
0e8c7ba
Adding mocking
jpuri Sep 23, 2022
f56b905
merge
jpuri Sep 29, 2022
877b413
Updating controller
jpuri Sep 29, 2022
08556bf
Merge branch 'develop' of github.com:MetaMask/metamask-extension into…
jpuri Sep 29, 2022
3bee90e
fix
jpuri Sep 29, 2022
cda51f2
fix
jpuri Sep 29, 2022
7218309
fix
jpuri Sep 29, 2022
fbfdd94
Updating controller dependency
jpuri Sep 29, 2022
93bfb92
Merge branch 'develop' into controller_update
jpuri Oct 3, 2022
ee790eb
fix
jpuri Oct 3, 2022
3bbec0e
fix
jpuri Oct 3, 2022
4142af2
fix
jpuri Oct 3, 2022
961de1b
fix
jpuri Oct 3, 2022
6b060e1
Merge branch 'develop' into controller_update
jpuri Oct 5, 2022
6c102de
Lavamoat auto
seaona Oct 5, 2022
12f3958
Update URLs for phishing detection testcase
seaona Oct 5, 2022
e08c0ce
Merge branch 'develop' into controller_update
jpuri Oct 5, 2022
d8e625f
Merge branch 'develop' into controller_update
jpuri Oct 5, 2022
470bcfa
Making permission and approval controller methods idempotent
jpuri Sep 14, 2022
64ac099
Adding unit test coverage
jpuri Sep 15, 2022
5f66099
fix
jpuri Sep 15, 2022
1e3100f
fix build
jpuri Sep 16, 2022
8ac248d
fix
jpuri Sep 16, 2022
c6e2e1e
Upgrade controller
jpuri Sep 16, 2022
8fee76e
Upgrade controller
jpuri Sep 16, 2022
f7179e4
mocking
jpuri Sep 22, 2022
d384d5f
mocking
jpuri Sep 22, 2022
14dc92f
Adding mocking
jpuri Sep 23, 2022
d1e5e0b
Updating controller
jpuri Sep 29, 2022
134147e
fix
jpuri Sep 29, 2022
b61a1ce
fix
jpuri Sep 29, 2022
d09e7c1
fix
jpuri Sep 29, 2022
58c7cd2
fix
jpuri Oct 5, 2022
3398a09
Merge branch 'permission_approval_method_idempotent' of github.com:Me…
jpuri Oct 5, 2022
6dba681
fix
jpuri Oct 5, 2022
ba62042
Merge branch 'develop' into controller_update
jpuri Oct 6, 2022
c253c92
Merge branch 'develop' into controller_update
jpuri Oct 7, 2022
e1dfaf8
Merge branch 'develop' into controller_update
jpuri Oct 10, 2022
7a5e9de
Merge branch 'develop' into controller_update
jpuri Oct 11, 2022
fc0e7dc
fixes
jpuri Oct 11, 2022
e9c7880
Merge branch 'controller_update' of github.com:MetaMask/metamask-exte…
jpuri Oct 11, 2022
f66606f
Merge branch 'controller_update' into permission_approval_method_idem…
jpuri Oct 11, 2022
14d7752
Updating controller dependency
jpuri Sep 29, 2022
a50f17a
fix
jpuri Oct 3, 2022
710116c
fix
jpuri Oct 3, 2022
9b178b9
fix
jpuri Oct 3, 2022
4b4f9ba
fix
jpuri Oct 3, 2022
2c466bc
fixes
jpuri Oct 11, 2022
bb59d25
Lavamoat auto
seaona Oct 5, 2022
53d2729
Update URLs for phishing detection testcase
seaona Oct 5, 2022
6091fa6
update lavamoat files
adonesky1 Oct 11, 2022
94fd9a0
call phishingController.test synchronously again
adonesky1 Oct 11, 2022
8efffd2
bump @metamask/controllers to v32.0.1
adonesky1 Oct 12, 2022
f409c23
lint
adonesky1 Oct 12, 2022
6c4dc47
update policy files
adonesky1 Oct 12, 2022
8a61d6c
bump controllers version again
adonesky1 Oct 13, 2022
02f4cc6
modify update phishing list strategy
adonesky1 Oct 14, 2022
23c58f5
revert back to use isOutOfDate, but without blocking substream
adonesky1 Oct 14, 2022
a097715
possible way to fix e2e tests?
adonesky1 Oct 18, 2022
7cefd59
Merge branch 'develop' into controller_update
jpuri Oct 21, 2022
f841761
merge
jpuri Oct 21, 2022
3c9aef8
merge
jpuri Oct 21, 2022
ede0d94
fi
jpuri Oct 21, 2022
988f0ab
fix
jpuri Oct 21, 2022
8f57930
merge
jpuri Oct 25, 2022
f88ee7d
Merge branch 'develop' into permission_approval_method_idempotent
jpuri Oct 26, 2022
ef7baf2
fix
jpuri Oct 26, 2022
fbb1e4e
Merge branch 'permission_approval_method_idempotent' of github.com:Me…
jpuri Oct 26, 2022
bfd83fd
Merge branch 'develop' into permission_approval_method_idempotent
jpuri Oct 26, 2022
516dbc5
fix
jpuri Oct 26, 2022
31df963
Merge branch 'permission_approval_method_idempotent' of github.com:Me…
jpuri Oct 26, 2022
0161493
fix build
jpuri Oct 27, 2022
57b9d7d
fix build
jpuri Oct 27, 2022
911116c
fix build
jpuri Oct 27, 2022
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
130 changes: 130 additions & 0 deletions app/scripts/metamask-controller.actions.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { strict as assert } from 'assert';
import sinon from 'sinon';
import proxyquire from 'proxyquire';

import {
ApprovalRequestNotFoundError,
PermissionsRequestNotFoundError,
} from '@metamask/controllers';
import { ORIGIN_METAMASK } from '../../shared/constants/app';

const Ganache = require('../../test/e2e/ganache');
Expand Down Expand Up @@ -238,4 +243,129 @@ describe('MetaMaskController', function () {
assert.deepEqual(transaction1, transaction2);
});
});

describe('#removePermissionsFor', function () {
it('should not propagate PermissionsRequestNotFoundError', function () {
const error = new PermissionsRequestNotFoundError('123');
metamaskController.permissionController = {
revokePermissions: () => {
throw error;
},
};
// Line below will not throw error, in case it throws this test case will fail.
metamaskController.removePermissionsFor({ subject: 'test_subject' });
});

it('should propagate Error other than PermissionsRequestNotFoundError', function () {
const error = new Error();
metamaskController.permissionController = {
revokePermissions: () => {
throw error;
},
};
assert.throws(() => {
metamaskController.removePermissionsFor({ subject: 'test_subject' });
}, error);
});
});

describe('#rejectPermissionsRequest', function () {
it('should not propagate PermissionsRequestNotFoundError', function () {
const error = new PermissionsRequestNotFoundError('123');
metamaskController.permissionController = {
rejectPermissionsRequest: () => {
throw error;
},
};
// Line below will not throw error, in case it throws this test case will fail.
metamaskController.rejectPermissionsRequest('DUMMY_ID');
});

it('should propagate Error other than PermissionsRequestNotFoundError', function () {
const error = new Error();
metamaskController.permissionController = {
rejectPermissionsRequest: () => {
throw error;
},
};
assert.throws(() => {
metamaskController.rejectPermissionsRequest('DUMMY_ID');
}, error);
});
});

describe('#acceptPermissionsRequest', function () {
it('should not propagate PermissionsRequestNotFoundError', function () {
const error = new PermissionsRequestNotFoundError('123');
metamaskController.permissionController = {
acceptPermissionsRequest: () => {
throw error;
},
};
// Line below will not throw error, in case it throws this test case will fail.
metamaskController.acceptPermissionsRequest('DUMMY_ID');
});

it('should propagate Error other than PermissionsRequestNotFoundError', function () {
const error = new Error();
metamaskController.permissionController = {
acceptPermissionsRequest: () => {
throw error;
},
};
assert.throws(() => {
metamaskController.acceptPermissionsRequest('DUMMY_ID');
}, error);
});
});

describe('#resolvePendingApproval', function () {
it('should not propagate ApprovalRequestNotFoundError', function () {
const error = new ApprovalRequestNotFoundError('123');
metamaskController.approvalController = {
accept: () => {
throw error;
},
};
// Line below will not throw error, in case it throws this test case will fail.
metamaskController.resolvePendingApproval('DUMMY_ID', 'DUMMY_VALUE');
});

it('should propagate Error other than ApprovalRequestNotFoundError', function () {
const error = new Error();
metamaskController.approvalController = {
accept: () => {
throw error;
},
};
assert.throws(() => {
metamaskController.resolvePendingApproval('DUMMY_ID', 'DUMMY_VALUE');
}, error);
});
});

describe('#rejectPendingApproval', function () {
it('should not propagate ApprovalRequestNotFoundError', function () {
const error = new ApprovalRequestNotFoundError('123');
metamaskController.approvalController = {
reject: () => {
throw error;
},
};
// Line below will not throw error, in case it throws this test case will fail.
metamaskController.rejectPendingApproval('DUMMY_ID', 'DUMMY_VALUE');
});

it('should propagate Error other than ApprovalRequestNotFoundError', function () {
const error = new Error();
metamaskController.approvalController = {
reject: () => {
throw error;
},
};
assert.throws(() => {
metamaskController.rejectPendingApproval('DUMMY_ID', 'DUMMY_VALUE');
}, error);
});
});
});
76 changes: 58 additions & 18 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ import {
CollectibleDetectionController,
PermissionController,
SubjectMetadataController,
PermissionsRequestNotFoundError,
ApprovalRequestNotFoundError,
///: BEGIN:ONLY_INCLUDE_IN(flask)
RateLimitController,
NotificationController,
///: END:ONLY_INCLUDE_IN
} from '@metamask/controllers';
import * as TEST from '@metamask/controllers';
import SmartTransactionsController from '@metamask/smart-transactions-controller';
///: BEGIN:ONLY_INCLUDE_IN(flask)
import {
Expand Down Expand Up @@ -1837,16 +1840,9 @@ export default class MetamaskController extends EventEmitter {
initializeThreeBox: this.initializeThreeBox.bind(this),

// permissions
removePermissionsFor:
permissionController.revokePermissions.bind(permissionController),
approvePermissionsRequest:
permissionController.acceptPermissionsRequest.bind(
permissionController,
),
rejectPermissionsRequest:
permissionController.rejectPermissionsRequest.bind(
permissionController,
),
removePermissionsFor: this.removePermissionsFor,
approvePermissionsRequest: this.approvePermissionsRequest,
rejectPermissionsRequest: this.rejectPermissionsRequest,
...getPermissionBackgroundApiMethods(permissionController),

///: BEGIN:ONLY_INCLUDE_IN(flask)
Expand Down Expand Up @@ -1960,14 +1956,8 @@ export default class MetamaskController extends EventEmitter {
),

// approval controller
resolvePendingApproval:
approvalController.accept.bind(approvalController),
rejectPendingApproval: async (id, error) => {
approvalController.reject(
id,
new EthereumRpcError(error.code, error.message, error.data),
);
},
resolvePendingApproval: this.resolvePendingApproval,
rejectPendingApproval: this.rejectPendingApproval,

// Notifications
updateViewedNotifications: announcementController.updateViewed.bind(
Expand Down Expand Up @@ -4361,4 +4351,54 @@ export default class MetamaskController extends EventEmitter {

return this.keyringController.setLocked();
}

removePermissionsFor = (subjects) => {
try {
this.permissionController.revokePermissions(subjects);
} catch (exp) {
if (!(exp instanceof PermissionsRequestNotFoundError)) {
throw exp;
}
}
};

rejectPermissionsRequest = (requestId) => {
try {
this.permissionController.rejectPermissionsRequest(requestId);
} catch (exp) {
if (!(exp instanceof PermissionsRequestNotFoundError)) {
throw exp;
}
}
};

acceptPermissionsRequest = (request) => {
try {
this.permissionController.acceptPermissionsRequest(request);
} catch (exp) {
if (!(exp instanceof PermissionsRequestNotFoundError)) {
throw exp;
}
}
};

resolvePendingApproval = (id, value) => {
try {
this.approvalController.accept(id, value);
} catch (exp) {
if (!(exp instanceof ApprovalRequestNotFoundError)) {
throw exp;
}
}
};

rejectPendingApproval = (id, value) => {
try {
this.approvalController.reject(id, value);
digiwand marked this conversation as resolved.
Show resolved Hide resolved
} catch (exp) {
if (!(exp instanceof ApprovalRequestNotFoundError)) {
throw exp;
}
}
};
}