Skip to content

Commit

Permalink
fix: no confirm button when switch to same chain (#237)
Browse files Browse the repository at this point in the history
* fix: no confirm button when switch to same chain

* fix: can not request approval if user reject previous

* fix: reset requestedApproval when approval finished
  • Loading branch information
vvvvvv1vvvvvv authored Sep 23, 2021
1 parent 3146a83 commit bdf58a8
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@types/chrome": "^0.0.143",
"@types/events": "^3.0.0",
"@types/jest": "^26.0.23",
"@types/koa-compose": "^3.2.5",
"@types/lru-cache": "^5.1.0",
"@types/node": "^14.14.41",
"@types/react": "^17.0.4",
Expand Down
13 changes: 10 additions & 3 deletions src/background/controller/provider/rpcFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const isSignApproval = (type: string) => {
return SIGN_APPROVALS.includes(type);
};

const flow = new PromiseFlow()
const flow = new PromiseFlow();
const flowContext = flow
.use(async (ctx, next) => {
// check method
const {
Expand All @@ -39,6 +40,7 @@ const flow = new PromiseFlow()
const isUnlock = keyringService.memStore.getState().isUnlocked;

if (!isUnlock) {
flow.requestedApproval = true;
await notificationService.requestApproval({ lock: true });
}
}
Expand All @@ -55,6 +57,7 @@ const flow = new PromiseFlow()
} = ctx;
if (!Reflect.getMetadata('SAFE', providerController, mapMethod)) {
if (!permissionService.hasPerssmion(origin)) {
flow.requestedApproval = true;
const { defaultChain } = await notificationService.requestApproval(
{
params: { origin, name, icon },
Expand Down Expand Up @@ -82,6 +85,7 @@ const flow = new PromiseFlow()
Reflect.getMetadata('APPROVAL', providerController, mapMethod) || [];

if (approvalType && (!condition || !condition(ctx.request))) {
flow.requestedApproval = true;
ctx.approvalRes = await notificationService.requestApproval(
{
approvalComponent: approvalType,
Expand Down Expand Up @@ -119,6 +123,7 @@ const flow = new PromiseFlow()
);

if (uiRequestComponent) {
flow.requestedApproval = true;
return await notificationService.requestApproval({
approvalComponent: uiRequestComponent,
requestDefer,
Expand All @@ -133,7 +138,7 @@ const flow = new PromiseFlow()
.callback();

export default (request) => {
return flow({ request }).finally(() => {
return flowContext({ request }).finally(() => {
const isApproval =
Reflect.getMetadata(
'APPROVAL',
Expand All @@ -144,8 +149,10 @@ export default (request) => {
'SAFE',
providerController,
underline2Camelcase(request?.data.method)
);
) ||
flow.requestedApproval;
if (isApproval) {
flow.requestedApproval = false;
// only unlock notification if current flow is an approval flow
notificationService.unLock();
}
Expand Down
3 changes: 2 additions & 1 deletion src/background/utils/promiseFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import compose from 'koa-compose';
export default class PromiseFlow {
private _tasks: ((args: any) => void)[] = [];
_context: any = {};
requestedApproval = false;

use(fn) {
use(fn): PromiseFlow {
if (typeof fn !== 'function') {
throw new Error('promise need function to handle');
}
Expand Down
3 changes: 1 addition & 2 deletions src/ui/views/Approval/components/AddChain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const AddChain = ({ params }: { params: AddChainProps }) => {
const showChain = supportChains.find((chain) => chain.hex === chainId);

const enableChains = wallet.getEnableChains();
const { chain: defaultChain } = wallet.getConnectedSite(session.origin)!;

let title;
let content;
Expand All @@ -44,7 +43,7 @@ const AddChain = ({ params }: { params: AddChainProps }) => {
title = t('Enable a Chain');
content = t('enableChainContent');
confirmBtnText = t('Enable');
} else if (CHAINS[defaultChain].hex !== chainId) {
} else {
title = t('Switch a Chain');
content = (
<Trans i18nKey="switchChainDesc" values={{ name: showChain?.name }} />
Expand Down
115 changes: 115 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,13 @@
"@svgr/plugin-svgo" "^5.5.0"
loader-utils "^2.0.0"

"@types/accepts@*":
version "1.3.5"
resolved "https://registry.yarnpkg.com/@types/accepts/-/accepts-1.3.5.tgz#c34bec115cfc746e04fe5a059df4ce7e7b391575"
integrity sha512-jOdnI/3qTpHABjM5cx1Hc0sKsPoYCp+DP/GJRGtDlPd7fiV9oXGGIcjW/ZOxLIvjGz8MA+uMZI9metHlgqbgwQ==
dependencies:
"@types/node" "*"

"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7":
version "7.1.14"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.14.tgz#faaeefc4185ec71c389f4501ee5ec84b170cc402"
Expand Down Expand Up @@ -2398,6 +2405,14 @@
dependencies:
"@types/node" "*"

"@types/body-parser@*":
version "1.19.1"
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.1.tgz#0c0174c42a7d017b818303d4b5d969cb0b75929c"
integrity sha512-a6bTJ21vFOGIkwM0kzh9Yr89ziVxq4vYH2fQ6N8AeipEzai/cFK6aGMArIkUeIdRIgpwQa+2bXiLuUJCpSf2Cg==
dependencies:
"@types/connect" "*"
"@types/node" "*"

"@types/chrome@^0.0.143":
version "0.0.143"
resolved "https://registry.yarnpkg.com/@types/chrome/-/chrome-0.0.143.tgz#0722fd6c76a73003fa220d869d2dbc8afd581810"
Expand All @@ -2406,6 +2421,28 @@
"@types/filesystem" "*"
"@types/har-format" "*"

"@types/connect@*":
version "3.4.35"
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.35.tgz#5fcf6ae445e4021d1fc2219a4873cc73a3bb2ad1"
integrity sha512-cdeYyv4KWoEgpBISTxWvqYsVy444DOqehiF3fM3ne10AmJ62RSyNkUnxMJXHQWRQQX2eR94m5y1IZyDwBjV9FQ==
dependencies:
"@types/node" "*"

"@types/content-disposition@*":
version "0.5.4"
resolved "https://registry.yarnpkg.com/@types/content-disposition/-/content-disposition-0.5.4.tgz#de48cf01c79c9f1560bcfd8ae43217ab028657f8"
integrity sha512-0mPF08jn9zYI0n0Q/Pnz7C4kThdSt+6LD4amsrYDDpgBfrVWa3TcCOxKX1zkGgYniGagRv8heN2cbh+CAn+uuQ==

"@types/cookies@*":
version "0.7.7"
resolved "https://registry.yarnpkg.com/@types/cookies/-/cookies-0.7.7.tgz#7a92453d1d16389c05a5301eef566f34946cfd81"
integrity sha512-h7BcvPUogWbKCzBR2lY4oqaZbO3jXZksexYJVFvkrFeLgbZjQkU4x8pRq6eg2MHXQhY0McQdqmmsxRWlVAHooA==
dependencies:
"@types/connect" "*"
"@types/express" "*"
"@types/keygrip" "*"
"@types/node" "*"

"@types/eslint-scope@^3.7.0":
version "3.7.0"
resolved "https://registry.yarnpkg.com/@types/eslint-scope/-/eslint-scope-3.7.0.tgz#4792816e31119ebd506902a482caec4951fabd86"
Expand Down Expand Up @@ -2445,6 +2482,25 @@
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7"
integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g==

"@types/express-serve-static-core@^4.17.18":
version "4.17.24"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.24.tgz#ea41f93bf7e0d59cd5a76665068ed6aab6815c07"
integrity sha512-3UJuW+Qxhzwjq3xhwXm2onQcFHn76frIYVbTu+kn24LFxI+dEhdfISDFovPB8VpEgW8oQCTpRuCe+0zJxB7NEA==
dependencies:
"@types/node" "*"
"@types/qs" "*"
"@types/range-parser" "*"

"@types/express@*":
version "4.17.13"
resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.13.tgz#a76e2995728999bab51a33fabce1d705a3709034"
integrity sha512-6bSZTPaTIACxn48l50SR+axgrqm6qXFIxrdAKaG6PaJk3+zuUr35hBlgT7vOmJcum+OEaIBLtHV/qloEAFITeA==
dependencies:
"@types/body-parser" "*"
"@types/express-serve-static-core" "^4.17.18"
"@types/qs" "*"
"@types/serve-static" "*"

"@types/filesystem@*":
version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/filesystem/-/filesystem-0.0.30.tgz#a7373a2edf34d13e298baf7ee1101f738b2efb7e"
Expand Down Expand Up @@ -2487,6 +2543,16 @@
resolved "https://registry.yarnpkg.com/@types/html-minifier-terser/-/html-minifier-terser-5.1.1.tgz#3c9ee980f1a10d6021ae6632ca3e79ca2ec4fb50"
integrity sha512-giAlZwstKbmvMk1OO7WXSj4OZ0keXAcl2TQq4LWHiiPH2ByaH7WeUzng+Qej8UPxxv+8lRTuouo0iaNDBuzIBA==

"@types/http-assert@*":
version "1.5.3"
resolved "https://registry.yarnpkg.com/@types/http-assert/-/http-assert-1.5.3.tgz#ef8e3d1a8d46c387f04ab0f2e8ab8cb0c5078661"
integrity sha512-FyAOrDuQmBi8/or3ns4rwPno7/9tJTijVW6aQQjK02+kOQ8zmoNg2XJtAuQhvQcy1ASJq38wirX5//9J1EqoUA==

"@types/http-errors@*":
version "1.8.1"
resolved "https://registry.yarnpkg.com/@types/http-errors/-/http-errors-1.8.1.tgz#e81ad28a60bee0328c6d2384e029aec626f1ae67"
integrity sha512-e+2rjEwK6KDaNOm5Aa9wNGgyS9oSZU/4pfSMMPYNOfjvFI0WVXm29+ITRFr6aKDvvKo7uU1jV68MW4ScsfDi7Q==

"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz#4ba8ddb720221f432e443bd5f9117fd22cfd4762"
Expand Down Expand Up @@ -2529,6 +2595,32 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/keygrip@*":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@types/keygrip/-/keygrip-1.0.2.tgz#513abfd256d7ad0bf1ee1873606317b33b1b2a72"
integrity sha512-GJhpTepz2udxGexqos8wgaBx4I/zWIDPh/KOGEwAqtuGDkOUJu5eFvwmdBX4AmB8Odsr+9pHCQqiAqDL/yKMKw==

"@types/koa-compose@*", "@types/koa-compose@^3.2.5":
version "3.2.5"
resolved "https://registry.yarnpkg.com/@types/koa-compose/-/koa-compose-3.2.5.tgz#85eb2e80ac50be95f37ccf8c407c09bbe3468e9d"
integrity sha512-B8nG/OoE1ORZqCkBVsup/AKcvjdgoHnfi4pZMn5UwAPCbhk/96xyv284eBYW8JlQbQ7zDmnpFr68I/40mFoIBQ==
dependencies:
"@types/koa" "*"

"@types/koa@*":
version "2.13.4"
resolved "https://registry.yarnpkg.com/@types/koa/-/koa-2.13.4.tgz#10620b3f24a8027ef5cbae88b393d1b31205726b"
integrity sha512-dfHYMfU+z/vKtQB7NUrthdAEiSvnLebvBjwHtfFmpZmB7em2N3WVQdHgnFq+xvyVgxW5jKDmjWfLD3lw4g4uTw==
dependencies:
"@types/accepts" "*"
"@types/content-disposition" "*"
"@types/cookies" "*"
"@types/http-assert" "*"
"@types/http-errors" "*"
"@types/keygrip" "*"
"@types/koa-compose" "*"
"@types/node" "*"

"@types/lodash@^4.14.172":
version "4.14.172"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.172.tgz#aad774c28e7bfd7a67de25408e03ee5a8c3d028a"
Expand All @@ -2539,6 +2631,11 @@
resolved "https://registry.yarnpkg.com/@types/lru-cache/-/lru-cache-5.1.0.tgz#57f228f2b80c046b4a1bd5cac031f81f207f4f03"
integrity sha512-RaE0B+14ToE4l6UqdarKPnXwVDuigfFv+5j9Dze/Nqr23yyuqdNvzcZi3xB+3Agvi5R4EOgAksfv3lXX4vBt9w==

"@types/mime@^1":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
integrity sha512-YATxVxgRqNH6nHEIsvg6k2Boc1JHI9ZbH5iWFFv/MTkchz3b1ieGDa5T0a9RznNdI0KhVbdbWSN+KWWrQZRxTw==

"@types/minimatch@*":
version "3.0.4"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.4.tgz#f0ec25dbf2f0e4b18647313ac031134ca5b24b21"
Expand Down Expand Up @@ -2591,6 +2688,16 @@
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.4.tgz#15925414e0ad2cd765bfef58842f7e26a7accb24"
integrity sha512-1HcDas8SEj4z1Wc696tH56G8OlRaH/sqZOynNNB+HF0WOeXPaxTtbYzJY2oEfiUxjSKjhCKr+MvR7dCHcEelug==

"@types/qs@*":
version "6.9.7"
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.7.tgz#63bb7d067db107cc1e457c303bc25d511febf6cb"
integrity sha512-FGa1F62FT09qcrueBA6qYTrJPVDzah9a+493+o2PCXsesWHIn27G98TsSMs3WPNbZIEj4+VJf6saSFpvD+3Zsw==

"@types/range-parser@*":
version "1.2.4"
resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.4.tgz#cd667bcfdd025213aafb7ca5915a932590acdcdc"
integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw==

"@types/react-dom@^17.0.3":
version "17.0.3"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-17.0.3.tgz#7fdf37b8af9d6d40127137865bb3fff8871d7ee1"
Expand Down Expand Up @@ -2636,6 +2743,14 @@
dependencies:
"@types/node" "*"

"@types/serve-static@*":
version "1.13.10"
resolved "https://registry.yarnpkg.com/@types/serve-static/-/serve-static-1.13.10.tgz#f5e0ce8797d2d7cc5ebeda48a52c96c4fa47a8d9"
integrity sha512-nCkHGI4w7ZgAdNkrEu0bv+4xNV/XDqW+DydknebMOQwkpDGx8G+HTlj7R7ABI8i8nKxVw0wtKPi1D+lPOkh4YQ==
dependencies:
"@types/mime" "^1"
"@types/node" "*"

"@types/stack-utils@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
Expand Down

0 comments on commit bdf58a8

Please sign in to comment.