Skip to content

Commit

Permalink
wallet-ext: disconnect dapp popup
Browse files Browse the repository at this point in the history
  • Loading branch information
pchrysochoidis committed Sep 19, 2022
1 parent b6b6daf commit d41c6ef
Show file tree
Hide file tree
Showing 12 changed files with 1,376 additions and 165 deletions.
1,235 changes: 1,098 additions & 137 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
},
"dependencies": {
"@mysten/sui.js": "workspace:*",
"@popperjs/core": "^2.11.6",
"@reduxjs/toolkit": "^1.8.3",
"@scure/bip32": "^1.1.0",
"bip39-light": "^1.0.7",
Expand All @@ -89,6 +90,7 @@
"react-dom": "^18.2.0",
"react-intl": "^6.0.5",
"react-number-format": "^4.9.3",
"react-popper": "^2.3.0",
"react-redux": "^8.0.2",
"react-router-dom": "^6.3.0",
"react-textarea-autosize": "^8.3.4",
Expand Down
10 changes: 10 additions & 0 deletions wallet/src/background/Permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ class Permissions {
);
}

public async delete(origin: string) {
const allPermissions = await this.getPermissions();
if (origin in allPermissions) {
delete allPermissions[origin];
await Browser.storage.local.set({
[PERMISSIONS_STORAGE_KEY]: allPermissions,
});
}
}

private async createPermissionRequest(
origin: string,
permissionTypes: readonly PermissionType[],
Expand Down
4 changes: 4 additions & 0 deletions wallet/src/background/connections/UiConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { isTransactionRequestResponse } from '_payloads/transactions/ui/Transact
import Permissions from '_src/background/Permissions';
import Tabs from '_src/background/Tabs';
import Transactions from '_src/background/Transactions';
import { isDisconnectApp } from '_src/shared/messaging/messages/payloads/permissions/DisconnectApp';

import type { Message } from '_messages';
import type { PortChannelName } from '_messaging/PortChannelName';
Expand Down Expand Up @@ -68,6 +69,9 @@ export class UiConnection extends Connection {
Object.values(await Transactions.getTransactionRequests()),
id
);
} else if (isDisconnectApp(payload)) {
await Permissions.delete(payload.origin);
this.send(createMessage({ type: 'done' }, id));
}
}

Expand Down
4 changes: 3 additions & 1 deletion wallet/src/shared/messaging/messages/payloads/BasePayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export type PayloadType =
| 'get-transaction-requests'
| 'get-transaction-requests-response'
| 'transaction-request-response'
| 'update-active-origin';
| 'update-active-origin'
| 'disconnect-app'
| 'done';

export interface BasePayload {
type: PayloadType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { isBasePayload } from '_payloads';

import type { BasePayload, Payload } from '_payloads';

export interface DisconnectApp extends BasePayload {
type: 'disconnect-app';
origin: string;
}

export function isDisconnectApp(payload: Payload): payload is DisconnectApp {
return isBasePayload(payload) && payload.type === 'disconnect-app';
}
9 changes: 9 additions & 0 deletions wallet/src/ui/app/background-client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
GetPermissionRequests,
PermissionResponse,
} from '_payloads/permissions';
import type { DisconnectApp } from '_payloads/permissions/DisconnectApp';
import type { GetTransactionRequests } from '_payloads/transactions/ui/GetTransactionRequests';
import type { TransactionRequestResponse } from '_payloads/transactions/ui/TransactionRequestResponse';
import type { AppDispatch } from '_store';
Expand Down Expand Up @@ -94,6 +95,14 @@ export class BackgroundClient {
);
}

public async disconnectApp(origin: string) {
await lastValueFrom(
this.sendMessage(
createMessage<DisconnectApp>({ type: 'disconnect-app', origin })
).pipe(take(1))
);
}

private handleIncomingMessage(msg: Message) {
if (!this._initialized || !this._dispatch) {
throw new Error(
Expand Down
10 changes: 5 additions & 5 deletions wallet/src/ui/app/hooks/useOnClickOutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ const useOnClickOutside = <T extends HTMLElement = HTMLElement>(
useEffect(() => {
const listener = (event: Event) => {
const el = ref?.current;
if (!el || el.contains((event?.target as Node) || null)) {
if (!el || el.contains(event?.target as Node)) {
return;
}

handler(event); // Call the handler only if the click is outside of the element passed.
};

document.addEventListener('mousedown', listener);
document.addEventListener('touchstart', listener);
document.addEventListener('click', listener, true);
document.addEventListener('touchstart', listener, true);

return () => {
document.removeEventListener('mousedown', listener);
document.removeEventListener('touchstart', listener);
document.removeEventListener('click', listener, true);
document.removeEventListener('touchstart', listener, true);
};
}, [ref, handler]); // Reload only if ref or handler changes
};
Expand Down
79 changes: 78 additions & 1 deletion wallet/src/ui/app/shared/dapp-status/DappStatus.module.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@use '_values/colors';
@use '_utils';

.wrapper {
display: contents;
}

.container {
display: flex;
flex-flow: row;
Expand Down Expand Up @@ -30,7 +34,7 @@
}

&:active,
&.acive {
&.active {
color: colors.$sui-blue;
border: none;
}
Expand All @@ -56,3 +60,76 @@
margin-left: 6px;
font-size: 11px;
}

.popup {
position: absolute;
background: colors.$gray-100;
border-radius: 6px;
padding: 10px 12px;
z-index: 999;
max-width: 55%;
}

.popup-content {
display: flex;
flex-flow: column nowrap;
align-items: center;
}

.popup-arrow {
position: absolute;
z-index: -1;
top: -5px;

&::before {
content: '';
background: colors.$gray-100;
transform: rotate(45deg);
height: 12px;
width: 12px;
display: block;
}
}

.origin-container {
display: flex;
flex-flow: row nowrap;
align-items: center;
}

.favicon {
width: 22px;
height: 22px;
border-radius: 6px;
background-color: colors.$gray-85;
margin-right: 8px;
padding: 2px;
}

.origin-text {
@include utils.overflow-ellipsis;
@include utils.typography('Primary/SubtitleSmall-M');

color: colors.$success;
}

.origin-url {
@include utils.typography('Primary/BodySmall-SB');
}

.divider {
height: 1px;
background-color: colors.$gray-90;
margin: 8px 0;
align-self: stretch;
}

.disconnect {
background: none;
outline: none;
border: none;
color: colors.$issue;
cursor: pointer;

@include utils.typography('Primary/BodySmall-M');
}
18 changes: 18 additions & 0 deletions wallet/src/ui/app/shared/dapp-status/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

import { createAsyncThunk } from '@reduxjs/toolkit';

import type { AppThunkConfig } from '_redux/store/thunk-extras';

export const appDisconnect = createAsyncThunk<
void,
{ origin: string },
AppThunkConfig
>(
'dapp-status-app-disconnect',
async ({ origin }, { extra: { background } }) => {
await background.disconnectApp(origin);
await background.sendGetPermissionRequests();
}
);
Loading

0 comments on commit d41c6ef

Please sign in to comment.