forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* allows the user to accept/reject a dapp connection * using mock permissions for now and background client
- Loading branch information
1 parent
36d3c32
commit ec299ae
Showing
14 changed files
with
390 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
wallet/src/shared/messaging/messages/payloads/permissions/Permission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { PermissionType } from './PermissionType'; | ||
import type { SuiAddress } from '@mysten/sui.js'; | ||
|
||
export interface Permission { | ||
id: string; | ||
origin: string; | ||
favIcon: string | undefined; | ||
accounts: SuiAddress[]; | ||
allowed: boolean | null; | ||
permissions: PermissionType[]; | ||
createdDate: string; | ||
responseDate: string | null; | ||
} |
4 changes: 4 additions & 0 deletions
4
wallet/src/shared/messaging/messages/payloads/permissions/PermissionType.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export type PermissionType = 'viewAccount'; |
5 changes: 5 additions & 0 deletions
5
wallet/src/shared/messaging/messages/payloads/permissions/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export * from './PermissionType'; | ||
export * from './Permission'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { setPermissions } from '_redux/slices/permissions'; | ||
|
||
import type { SuiAddress } from '@mysten/sui.js'; | ||
import type { AppDispatch } from '_store'; | ||
|
||
export class BackgroundClient { | ||
private _dispatch: AppDispatch | null = null; | ||
private _initialized = false; | ||
|
||
public async init(dispatch: AppDispatch) { | ||
if (this._initialized) { | ||
throw new Error('[BackgroundClient] already initialized'); | ||
} | ||
this._initialized = true; | ||
this._dispatch = dispatch; | ||
// TODO: implement | ||
return this.sendGetPermissionRequests().then(() => undefined); | ||
} | ||
|
||
public sendPermissionResponse( | ||
id: string, | ||
accounts: SuiAddress[], | ||
allowed: boolean, | ||
responseDate: string | ||
) { | ||
// TODO: implement | ||
} | ||
|
||
public async sendGetPermissionRequests() { | ||
// TODO: remove mock and implement | ||
const id = /connect\/(.+)/.exec(window.location.hash)?.[1]; | ||
if (this._dispatch && id) { | ||
this._dispatch( | ||
setPermissions([ | ||
{ | ||
id, | ||
accounts: [], | ||
allowed: null, | ||
createdDate: new Date().toISOString(), | ||
favIcon: 'https://www.google.com/favicon.ico', | ||
origin: 'https://www.google.com', | ||
permissions: ['viewAccount'], | ||
responseDate: null, | ||
}, | ||
]) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
wallet/src/ui/app/pages/site-connect/SiteConnectPage.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
.container { | ||
display: flex; | ||
flex-flow: column nowrap; | ||
padding: 15px; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.title { | ||
margin-bottom: 30px; | ||
} | ||
|
||
.origin-container { | ||
display: flex; | ||
flex-flow: column nowrap; | ||
align-items: center; | ||
padding: 8px; | ||
border: 1px solid #b2bbc3; | ||
border-radius: 4px; | ||
align-self: stretch; | ||
} | ||
|
||
.fav-icon { | ||
width: 40px; | ||
border-radius: 4px; | ||
margin-bottom: 8px; | ||
} | ||
|
||
.origin { | ||
font-size: 14px; | ||
font-weight: 500; | ||
} | ||
|
||
.label { | ||
align-self: flex-start; | ||
font-weight: 700; | ||
margin-top: 16px; | ||
margin-bottom: 2px; | ||
letter-spacing: 0.6px; | ||
} | ||
|
||
.permission { | ||
font-weight: 500; | ||
font-style: italic; | ||
padding: 3px 6px; | ||
background-color: #c7c7c7; | ||
border-radius: 4px; | ||
display: inline-block; | ||
} | ||
|
||
.actions { | ||
display: flex; | ||
flex-flow: row nowrap; | ||
margin-top: 35px; | ||
align-self: stretch; | ||
align-items: center; | ||
justify-content: space-between; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { useCallback, useEffect, useMemo, useState } from 'react'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
import AccountAddress from '_components/account-address'; | ||
import Loading from '_components/loading'; | ||
import { useAppDispatch, useAppSelector, useInitializedGuard } from '_hooks'; | ||
import { | ||
permissionsSelectors, | ||
respondToPermissionRequest, | ||
} from '_redux/slices/permissions'; | ||
|
||
import type { PermissionType } from '_messages/payloads/permissions'; | ||
import type { RootState } from '_redux/RootReducer'; | ||
import type { MouseEventHandler } from 'react'; | ||
|
||
import st from './SiteConnectPage.module.scss'; | ||
|
||
const permissionTypeToTxt: Record<PermissionType, string> = { | ||
viewAccount: 'View Account', | ||
}; | ||
|
||
function SiteConnectPage() { | ||
const { requestID } = useParams(); | ||
const guardLoading = useInitializedGuard(true); | ||
const permissionsInitialized = useAppSelector( | ||
({ permissions }) => permissions.initialized | ||
); | ||
const loading = guardLoading || !permissionsInitialized; | ||
const permissionSelector = useMemo( | ||
() => (state: RootState) => | ||
requestID | ||
? permissionsSelectors.selectById(state, requestID) | ||
: null, | ||
[requestID] | ||
); | ||
const dispatch = useAppDispatch(); | ||
const permissionRequest = useAppSelector(permissionSelector); | ||
const activeAccount = useAppSelector(({ account }) => account.address); | ||
const [submitting, setSubmitting] = useState(false); | ||
const handleOnResponse = useCallback<MouseEventHandler<HTMLButtonElement>>( | ||
(e) => { | ||
const allowed = e.currentTarget.dataset.allow === 'true'; | ||
if (requestID && activeAccount) { | ||
setSubmitting(true); | ||
dispatch( | ||
respondToPermissionRequest({ | ||
id: requestID, | ||
accounts: allowed ? [activeAccount] : [], | ||
allowed, | ||
}) | ||
); | ||
} | ||
}, | ||
[dispatch, requestID, activeAccount] | ||
); | ||
useEffect(() => { | ||
if ( | ||
!loading && | ||
(!permissionRequest || permissionRequest.responseDate) | ||
) { | ||
window.close(); | ||
} | ||
}, [loading, permissionRequest]); | ||
|
||
return ( | ||
<Loading loading={loading}> | ||
{permissionRequest ? ( | ||
<div className={st.container}> | ||
<h2 className={st.title}>Connect to Sui wallet</h2> | ||
<label className={st.label}>Site</label> | ||
<div className={st.originContainer}> | ||
{permissionRequest.favIcon ? ( | ||
<img | ||
className={st.favIcon} | ||
src={permissionRequest.favIcon} | ||
alt="Site favicon" | ||
/> | ||
) : null} | ||
<span className={st.origin}> | ||
{permissionRequest.origin} | ||
</span> | ||
</div> | ||
<label className={st.label}>Account</label> | ||
<AccountAddress showLink={false} /> | ||
<label className={st.label}>Permissions</label> | ||
<div className={st.permissionsContainer}> | ||
{permissionRequest.permissions.map((aPermission) => ( | ||
<span className={st.permission} key={aPermission}> | ||
{permissionTypeToTxt[aPermission]} | ||
</span> | ||
))} | ||
</div> | ||
<div className={st.actions}> | ||
<button | ||
type="button" | ||
data-allow="false" | ||
onClick={handleOnResponse} | ||
className="btn link" | ||
disabled={submitting} | ||
> | ||
Cancel | ||
</button> | ||
<button | ||
type="button" | ||
className="btn" | ||
data-allow="true" | ||
onClick={handleOnResponse} | ||
disabled={submitting} | ||
> | ||
Connect | ||
</button> | ||
</div> | ||
</div> | ||
) : null} | ||
</Loading> | ||
); | ||
} | ||
|
||
export default SiteConnectPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.