Skip to content

Commit

Permalink
Support dapp interactions with locked wallets (MystenLabs#5161)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan-Mysten authored Oct 12, 2022
1 parent 2aad701 commit 29dd9ba
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
9 changes: 6 additions & 3 deletions apps/wallet/src/background/Permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ class Permissions {
this.permissionReply = this._permissionResponses.pipe(
mergeWith(
Tabs.onRemoved.pipe(
filter((aTab) =>
Permissions.isPermissionUiUrl(aTab.url || '')
)
filter((aTab) => {
return (
Permissions.isPermissionUiUrl(aTab.url || '') &&
!aTab.nextUrl?.includes('/locked')
);
})
)
),
concatMap((data) =>
Expand Down
2 changes: 2 additions & 0 deletions apps/wallet/src/background/Tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const onWindowFocusChanged = fromEventPattern<number>(
type TabInfo = {
id: number;
url: string | null;
nextUrl?: string;
closed?: boolean;
};

Expand Down Expand Up @@ -89,6 +90,7 @@ class Tabs {
this._onRemoved.next({
id,
url: currentTab.url,
nextUrl: url,
closed: false,
});
}
Expand Down
17 changes: 14 additions & 3 deletions apps/wallet/src/ui/app/wallet/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

import { useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';

import { useAppSelector } from '_hooks';

export function useLockedGuard(requiredLockedStatus: boolean) {
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const { pathname, search, state } = useLocation();
const { isInitialized, isLocked } = useAppSelector(
({ account: { isInitialized, isLocked } }) => ({
Expand All @@ -18,15 +19,25 @@ export function useLockedGuard(requiredLockedStatus: boolean) {
const loading = isInitialized === null || isLocked === null;
const guardAct =
!loading && isInitialized && requiredLockedStatus !== isLocked;
const nextUrl = searchParams.get('url') || '/';
useEffect(() => {
if (guardAct) {
navigate(
requiredLockedStatus
? '/'
? nextUrl
: `/locked?url=${encodeURIComponent(pathname + search)}`,
{ replace: true, state }
);
}
}, [guardAct, navigate, requiredLockedStatus, pathname, search, state]);
}, [
guardAct,
navigate,
requiredLockedStatus,
pathname,
search,
state,
nextUrl,
]);

return loading || guardAct;
}
6 changes: 1 addition & 5 deletions apps/wallet/src/ui/app/wallet/locked-page/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { Field, Form, Formik } from 'formik';
import { Link, useNavigate, useSearchParams } from 'react-router-dom';
import { Link } from 'react-router-dom';
import * as Yup from 'yup';

import Alert from '_app/components/alert';
Expand All @@ -28,10 +28,7 @@ export default function LockedPage() {
const initGuardLoading = useInitializedGuard(true);
const lockedGuardLoading = useLockedGuard(true);
const guardsLoading = initGuardLoading || lockedGuardLoading;
const [searchParams] = useSearchParams();
const navigate = useNavigate();
const dispatch = useAppDispatch();
const nextUrl = searchParams.get('url') || '/';
return (
<Loading loading={guardsLoading}>
<PageLayout limitToPopUpSize={true}>
Expand All @@ -54,7 +51,6 @@ export default function LockedPage() {
await dispatch(
unlockWallet({ password })
).unwrap();
navigate(nextUrl);
} catch (e) {
setFieldError(
'password',
Expand Down

0 comments on commit 29dd9ba

Please sign in to comment.