From c5caf3bff28f389abbc6323897d90ac894588fd6 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Fri, 15 Apr 2022 14:54:10 +0800 Subject: [PATCH 1/3] fix: personalSign --- .../popups/pages/Wallet/VerifyWallet/index.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/mask/src/extension/popups/pages/Wallet/VerifyWallet/index.tsx b/packages/mask/src/extension/popups/pages/Wallet/VerifyWallet/index.tsx index 36fd0399b416..29e85eb3b1ae 100644 --- a/packages/mask/src/extension/popups/pages/Wallet/VerifyWallet/index.tsx +++ b/packages/mask/src/extension/popups/pages/Wallet/VerifyWallet/index.tsx @@ -1,16 +1,17 @@ +import { memo, useState } from 'react' +import { useLocation } from 'react-use' +import { useNavigate } from 'react-router-dom' import { NextIDAction, NextIDPayload, NextIDPlatform, PopupRoutes } from '@masknet/shared-base' import { makeStyles } from '@masknet/theme' import { NextIDProof } from '@masknet/web3-providers' -import { memo, useState } from 'react' +import { ChainId, isSameAddress, NetworkType, ProviderType } from '@masknet/web3-shared-evm' +import type { Web3Plugin } from '@masknet/plugin-infra/src/web3-types' import { SignSteps, Steps } from '../../../../../components/shared/VerifyWallet/Steps' import Services from '../../../../service' import { PersonaContext } from '../../Personas/hooks/usePersonaContext' import { useTitle } from '../../../hook/useTitle' import { useI18N } from '../../../../../utils' import { useQueryIsBound } from '../../../hook/useQueryIsBound' -import { useNavigate } from 'react-router-dom' -import { useLocation } from 'react-use' -import { isSameAddress } from '@masknet/web3-shared-evm' const useStyles = makeStyles()((theme) => ({ container: { @@ -32,7 +33,7 @@ const VerifyWallet = memo(() => { const navigate = useNavigate() useTitle(t('popups_add_wallet')) const location = useLocation() - const wallet = location.state.usr + const wallet: Web3Plugin.ConnectionResult = location.state.usr const bounds = useQueryIsBound(wallet.account) if (bounds && bounds.length > 0 && !isBound) { @@ -72,7 +73,10 @@ const VerifyWallet = memo(() => { const walletSign = async () => { if (!payload) throw new Error('payload error') try { - const walletSig = await Services.Ethereum.personalSign(payload.signPayload, wallet.account) + const walletSig = await Services.Ethereum.personalSign(payload.signPayload, wallet.account, '', { + chainId: wallet.chainId, + providerType: wallet.providerType, + }) if (!walletSig) throw new Error('Wallet sign failed') await NextIDProof.bindProof( payload.uuid, From 4388eaeb2384b1c37145ca5b29e6c1eac6243c34 Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Fri, 15 Apr 2022 15:06:16 +0800 Subject: [PATCH 2/3] refactor: replace NetworkType with ChainId --- .../background-script/EthereumServices/provider.ts | 6 +++--- .../EthereumServices/providers/MaskWallet.ts | 6 +++--- .../plugins/Wallet/SNSAdaptor/ConnectWalletDialog/index.tsx | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/mask/src/extension/background-script/EthereumServices/provider.ts b/packages/mask/src/extension/background-script/EthereumServices/provider.ts index 32d8cd321bcd..9e35700ecfb8 100644 --- a/packages/mask/src/extension/background-script/EthereumServices/provider.ts +++ b/packages/mask/src/extension/background-script/EthereumServices/provider.ts @@ -1,6 +1,6 @@ import { first } from 'lodash-unified' import { defer } from '@dimensiondev/kit' -import type { ChainId, NetworkType, ProviderType } from '@masknet/web3-shared-evm' +import type { ChainId, ProviderType } from '@masknet/web3-shared-evm' import * as MaskWallet from './providers/MaskWallet' import * as MetaMask from './providers/MetaMask' import * as WalletConnect from './providers/WalletConnect' @@ -50,8 +50,8 @@ export async function cancelWalletConnect() { } // #endregion -export async function connectMaskWallet(networkType: NetworkType) { - const { accounts, chainId } = await MaskWallet.requestAccounts(networkType) +export async function connectMaskWallet(expectedChainId: ChainId) { + const { accounts, chainId } = await MaskWallet.requestAccounts(expectedChainId) return { account: first(accounts), chainId, diff --git a/packages/mask/src/extension/background-script/EthereumServices/providers/MaskWallet.ts b/packages/mask/src/extension/background-script/EthereumServices/providers/MaskWallet.ts index 4a3c2f369097..ffa32df949ab 100644 --- a/packages/mask/src/extension/background-script/EthereumServices/providers/MaskWallet.ts +++ b/packages/mask/src/extension/background-script/EthereumServices/providers/MaskWallet.ts @@ -1,7 +1,7 @@ import MaskWallet from 'web3' import type { HttpProvider } from 'web3-core' import { PopupRoutes } from '@masknet/shared-base' -import { ChainId, getChainIdFromNetworkType, getChainRPC, NetworkType, ProviderType } from '@masknet/web3-shared-evm' +import { ChainId, getChainRPC, ProviderType } from '@masknet/web3-shared-evm' import { currentChainIdSettings } from '../../../../plugins/Wallet/settings' import { getWallets, selectAccountPrepare } from '../../../../plugins/Wallet/services' import { openPopupWindow } from '../../../../../background/services/helper' @@ -66,7 +66,7 @@ export function createWeb3({ } // #endregion -export async function requestAccounts(networkType: NetworkType) { +export async function requestAccounts(chainId: ChainId) { const wallets = await getWallets(ProviderType.MaskWallet) return new Promise<{ chainId: ChainId @@ -80,7 +80,7 @@ export async function requestAccounts(networkType: NetworkType) { }) }) await openPopupWindow(wallets.length > 0 ? PopupRoutes.SelectWallet : undefined, { - chainId: getChainIdFromNetworkType(networkType), + chainId, }) } catch { reject(new Error('Failed to connect to Mask Network.')) diff --git a/packages/mask/src/plugins/Wallet/SNSAdaptor/ConnectWalletDialog/index.tsx b/packages/mask/src/plugins/Wallet/SNSAdaptor/ConnectWalletDialog/index.tsx index a8d61e61bad3..77c380ef3940 100644 --- a/packages/mask/src/plugins/Wallet/SNSAdaptor/ConnectWalletDialog/index.tsx +++ b/packages/mask/src/plugins/Wallet/SNSAdaptor/ConnectWalletDialog/index.tsx @@ -68,7 +68,7 @@ export function ConnectWalletDialog(props: ConnectWalletDialogProps) { switch (providerType) { case ProviderType.MaskWallet: - ;({ account, chainId } = await Services.Ethereum.connectMaskWallet(networkType)) + ;({ account, chainId } = await Services.Ethereum.connectMaskWallet(expectedChainId)) break case ProviderType.MetaMask: ;({ account, chainId } = await Services.Ethereum.connectMetaMask()) From 5be6afa156ecffcb6b0f26ea03755fe44e91305d Mon Sep 17 00:00:00 2001 From: guanbinrui Date: Fri, 15 Apr 2022 17:52:51 +0800 Subject: [PATCH 3/3] fix: popup connect wallet --- .../src/initialization/Dashboard.tsx | 2 +- .../shared/VerifyWallet/CurrentWalletBox.tsx | 55 ++----------------- .../src/components/shared/WalletStatusBox.tsx | 38 ------------- .../EthereumServices/providers/MaskWallet.ts | 6 +- .../pages/Wallet/SelectWallet/index.tsx | 35 ++++++++---- .../SNSAdaptor/ConnectWalletDialog/index.tsx | 34 +++++++++++- .../WalletConnectQRCodeDialog/index.tsx | 7 +++ .../plugins/Wallet/hooks/useSelectAccount.ts | 25 +++++++++ .../defaults/inject/PageInspector.tsx | 11 +++- 9 files changed, 106 insertions(+), 107 deletions(-) create mode 100644 packages/mask/src/plugins/Wallet/hooks/useSelectAccount.ts diff --git a/packages/dashboard/src/initialization/Dashboard.tsx b/packages/dashboard/src/initialization/Dashboard.tsx index 5715cdc26333..84236295ed53 100644 --- a/packages/dashboard/src/initialization/Dashboard.tsx +++ b/packages/dashboard/src/initialization/Dashboard.tsx @@ -60,8 +60,8 @@ export default function DashboardRoot() { + - diff --git a/packages/mask/src/components/shared/VerifyWallet/CurrentWalletBox.tsx b/packages/mask/src/components/shared/VerifyWallet/CurrentWalletBox.tsx index be797ea147ec..0b0b53a37120 100644 --- a/packages/mask/src/components/shared/VerifyWallet/CurrentWalletBox.tsx +++ b/packages/mask/src/components/shared/VerifyWallet/CurrentWalletBox.tsx @@ -1,10 +1,8 @@ -import { useCallback } from 'react' import { ExternalLink } from 'react-feather' import classNames from 'classnames' import { ProviderType } from '@masknet/web3-shared-evm' import { Button, Link, Typography } from '@mui/material' import { makeStyles } from '@masknet/theme' -import { useRemoteControlledDialog } from '@masknet/shared-base-ui' import { useAccount, useWeb3State, @@ -16,10 +14,7 @@ import { useWallet, } from '@masknet/plugin-infra/web3' import { FormattedAddress, WalletIcon } from '@masknet/shared' -import { WalletMessages } from '../../../plugins/Wallet/messages' import { useI18N } from '../../../utils' -import Services from '../../../extension/service' -import { ActionButtonPromise } from '../../../extension/options-page/DashboardComponents/ActionButton' const useStyles = makeStyles()((theme) => ({ currentAccount: { @@ -109,34 +104,6 @@ export function CurrentWalletBox(props: CurrentWalletBox) { const { value: domain } = useReverseAddress(account) - // #region change provider - const { openDialog: openSelectProviderDialog } = useRemoteControlledDialog( - WalletMessages.events.selectProviderDialogUpdated, - ) - // #endregion - - // #region walletconnect - const { setDialog: setWalletConnectDialog } = useRemoteControlledDialog( - WalletMessages.events.walletConnectQRCodeDialogUpdated, - ) - // #endregion - - const onDisconnect = useCallback(async () => { - switch (providerType) { - case ProviderType.WalletConnect: - setWalletConnectDialog({ - open: true, - uri: await Services.Ethereum.createConnectionURI(), - }) - break - case ProviderType.Fortmatic: - await Services.Ethereum.disconnectFortmatic(chainId) - break - } - }, [chainId, providerType, setWalletConnectDialog]) - - const onChange = props.changeWallet - return account ? (
{!props.disableChange && (
- {providerType === ProviderType.WalletConnect || providerType === ProviderType.Fortmatic ? ( - } - failIcon={<>} - /> - ) : null}
@@ -207,7 +160,11 @@ export function CurrentWalletBox(props: CurrentWalletBox) {
) : (
-
diff --git a/packages/mask/src/components/shared/WalletStatusBox.tsx b/packages/mask/src/components/shared/WalletStatusBox.tsx index f75f919884b7..cdb3d9f92c4e 100644 --- a/packages/mask/src/components/shared/WalletStatusBox.tsx +++ b/packages/mask/src/components/shared/WalletStatusBox.tsx @@ -1,4 +1,3 @@ -import { useCallback } from 'react' import { useCopyToClipboard } from 'react-use' import { Copy, ExternalLink } from 'react-feather' import classNames from 'classnames' @@ -20,8 +19,6 @@ import { import { FormattedAddress, useSnackbarCallback, WalletIcon } from '@masknet/shared' import { WalletMessages } from '../../plugins/Wallet/messages' import { useI18N } from '../../utils' -import Services from '../../extension/service' -import { ActionButtonPromise } from '../../extension/options-page/DashboardComponents/ActionButton' const useStyles = makeStyles<{ isDashboard: boolean }>()((theme, { isDashboard }) => ({ content: { @@ -130,26 +127,6 @@ export function WalletStatusBox(props: WalletStatusBox) { ) // #endregion - // #region walletconnect - const { setDialog: setWalletConnectDialog } = useRemoteControlledDialog( - WalletMessages.events.walletConnectQRCodeDialogUpdated, - ) - // #endregion - - const onDisconnect = useCallback(async () => { - switch (providerType) { - case ProviderType.WalletConnect: - setWalletConnectDialog({ - open: true, - uri: await Services.Ethereum.createConnectionURI(), - }) - break - case ProviderType.Fortmatic: - await Services.Ethereum.disconnectFortmatic(chainId) - break - } - }, [chainId, providerType, setWalletConnectDialog]) - return account ? (
{!props.disableChange && (
- {providerType === ProviderType.WalletConnect || providerType === ProviderType.Fortmatic ? ( - } - failIcon={<>} - /> - ) : null}