From 416478a41db2fd7bc6fcba36a145cd049f7e8198 Mon Sep 17 00:00:00 2001 From: nuanyang233 <528944303@qq.com> Date: Thu, 23 Dec 2021 17:46:58 +0800 Subject: [PATCH 1/4] fix: bugfix for swap page --- .../background-script/EthereumServices/nonce.ts | 10 +++++++++- .../popups/components/NetworkSelector/index.tsx | 8 ++++---- .../src/plugins/Trader/SNSAdaptor/trader/Trader.tsx | 9 +++++---- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/mask/src/extension/background-script/EthereumServices/nonce.ts b/packages/mask/src/extension/background-script/EthereumServices/nonce.ts index 9d29aa0f9fef..174821139e00 100644 --- a/packages/mask/src/extension/background-script/EthereumServices/nonce.ts +++ b/packages/mask/src/extension/background-script/EthereumServices/nonce.ts @@ -1,5 +1,7 @@ +import { ProviderType } from '@masknet/web3-shared-evm' import { EthereumAddress } from 'wallet.ts' import { getTransactionCount } from './network' +import { currentMaskWalletChainIdSettings } from '../../../plugins/Wallet/settings' class NonceManager { constructor(private address: string) {} @@ -28,7 +30,13 @@ class NonceManager { const run = async () => { try { this.lock() - callback(null, await getTransactionCount(this.address)) + callback( + null, + await getTransactionCount(this.address, { + providerType: ProviderType.MaskWallet, + chainId: currentMaskWalletChainIdSettings.value, + }), + ) } catch (error: any) { callback(error) } diff --git a/packages/mask/src/extension/popups/components/NetworkSelector/index.tsx b/packages/mask/src/extension/popups/components/NetworkSelector/index.tsx index b92609c5e5bf..b09ea5451cc3 100644 --- a/packages/mask/src/extension/popups/components/NetworkSelector/index.tsx +++ b/packages/mask/src/extension/popups/components/NetworkSelector/index.tsx @@ -2,10 +2,10 @@ import { memo, useCallback } from 'react' import { Box, MenuItem, Typography } from '@mui/material' import { makeStyles } from '@masknet/theme' import { Flags } from '../../../../../shared' -import { ChainId, ProviderType, useAccount, useChainId, useProviderType } from '@masknet/web3-shared-evm' +import { ChainId, ProviderType, useAccount, useChainId } from '@masknet/web3-shared-evm' import { getRegisteredWeb3Networks, NetworkPluginID, Web3Plugin } from '@masknet/plugin-infra' -import { currentMaskWalletAccountSettings } from '../../../../plugins/Wallet/settings' -import { ChainIcon, useMenu, WalletIcon } from '@masknet/shared' +import { currentMaskWalletAccountSettings, currentProviderSettings } from '../../../../plugins/Wallet/settings' +import { ChainIcon, useMenu, useValueRef, WalletIcon } from '@masknet/shared' import { ArrowDownRound } from '@masknet/icons' import { WalletRPC } from '../../../../plugins/Wallet/messages' @@ -46,7 +46,7 @@ export const NetworkSelector = memo(() => { const networks = getRegisteredWeb3Networks() const account = useAccount() const chainId = useChainId() - const providerType = useProviderType() + const providerType = useValueRef(currentProviderSettings) const onChainChange = useCallback( async (chainId: ChainId) => { if (providerType === ProviderType.MaskWallet) { diff --git a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/Trader.tsx b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/Trader.tsx index 61e377c27180..e09e12894a2a 100644 --- a/packages/mask/src/plugins/Trader/SNSAdaptor/trader/Trader.tsx +++ b/packages/mask/src/plugins/Trader/SNSAdaptor/trader/Trader.tsx @@ -15,11 +15,12 @@ import { useChainId, useChainIdValid, useFungibleTokenBalance, + useProviderType, useTokenConstants, useWallet, } from '@masknet/web3-shared-evm' import { isGreaterThan, isLessThan, multipliedBy } from '@masknet/web3-shared-base' -import { useRemoteControlledDialog, useValueRef } from '@masknet/shared' +import { useRemoteControlledDialog } from '@masknet/shared' import { delay } from '@masknet/shared-base' import type { Coin } from '../../types' import { TokenPanelType, TradeInfo } from '../../types' @@ -36,7 +37,7 @@ import { useTradeCallback } from '../../trader/useTradeCallback' import { isNativeTokenWrapper } from '../../helpers' import { ConfirmDialog } from './ConfirmDialog' import Services from '../../../../extension/service' -import { currentBalancesSettings, currentProviderSettings } from '../../../Wallet/settings' +import { currentBalancesSettings } from '../../../Wallet/settings' import { TargetChainIdContext } from '../../trader/useTargetChainIdContext' import { WalletRPC } from '../../../Wallet/messages' import { PluginTraderMessages } from '../../messages' @@ -44,7 +45,7 @@ import { NetworkType } from '@masknet/public-api' import BigNumber from 'bignumber.js' import { useNativeTokenPrice, useTokenPrice } from '../../../Wallet/hooks/useTokenPrice' import { SettingsDialog } from './SettingsDialog' -import { useAccount } from '@masknet/plugin-infra' +import { useAccount } from '@masknet/web3-shared-evm' const useStyles = makeStyles()(() => { return { @@ -71,7 +72,7 @@ export function Trader(props: TraderProps) { const chainIdValid = useChainIdValid() const { NATIVE_TOKEN_ADDRESS } = useTokenConstants() const currentAccount = useAccount() - const currentProvider = useValueRef(currentProviderSettings) + const currentProvider = useProviderType() const classes = useStylesExtends(useStyles(), props) const { t } = useI18N() const { setTargetChainId } = TargetChainIdContext.useContainer() From 342e97881ab3546a4c66d418a157b4b5c99dd224 Mon Sep 17 00:00:00 2001 From: nuanyang233 <528944303@qq.com> Date: Thu, 23 Dec 2021 17:54:20 +0800 Subject: [PATCH 2/4] chore: add i18n --- packages/mask/shared-ui/locales/en-US.json | 6 +++ .../EthereumServices/nonce.ts | 1 + .../pages/Wallet/ImportWallet/index.tsx | 52 +++++++------------ .../pages/Wallet/WalletRecovery/index.tsx | 2 +- 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/packages/mask/shared-ui/locales/en-US.json b/packages/mask/shared-ui/locales/en-US.json index 01c040ad091b..2e09df082685 100644 --- a/packages/mask/shared-ui/locales/en-US.json +++ b/packages/mask/shared-ui/locales/en-US.json @@ -926,11 +926,17 @@ "popups_wallet_dialog_legacy_wallet_tip": "Detected legacy wallets, please click confirm to restore them all.", "popups_wallet_set_payment_password": "Set the payment password", "popups_wallet_payment_password": "Payment Password", + "popups_wallet_re_payment_password": "Re-enter the payment password", "popups_wallet_set_up_payment_password": "Set up payment password", "popups_wallet_payment_password_tip": "The Payment password must be a combination of 2 categories out of numbers, letters, and special characters with a length of 8-20 characters.", "popups_wallet_go_back": "Go back", "popups_wallet_start_up_tip": "Connect to your wallet, create a new wallet or recover an existing wallet using a seed phrase.", "popups_wallet_name_placeholder": "Enter 1-12 characters", + "popups_wallet_name_mnemonic": "Mnemonic", + "popups_wallet_name_json_file": "Json File", + "popups_wallet_name_private_key": "Private Key", + "popups_wallet_name_mnemonic_placeholder": "Please enter 12 mnemonic words separated by spaces", + "popups_wallet_name_origin_password": "Original Password", "popups_wallet_tab_assets": "Assets", "popups_wallet_tab_activity": "Activity", "popups_wallet_derivation_path": "Derivation path ({{ path }})", diff --git a/packages/mask/src/extension/background-script/EthereumServices/nonce.ts b/packages/mask/src/extension/background-script/EthereumServices/nonce.ts index 174821139e00..f3423c91d28d 100644 --- a/packages/mask/src/extension/background-script/EthereumServices/nonce.ts +++ b/packages/mask/src/extension/background-script/EthereumServices/nonce.ts @@ -32,6 +32,7 @@ class NonceManager { this.lock() callback( null, + // Only mask wallets need to use Nonce await getTransactionCount(this.address, { providerType: ProviderType.MaskWallet, chainId: currentMaskWalletChainIdSettings.value, diff --git a/packages/mask/src/extension/popups/pages/Wallet/ImportWallet/index.tsx b/packages/mask/src/extension/popups/pages/Wallet/ImportWallet/index.tsx index a13dfce03f5a..f648f8488239 100644 --- a/packages/mask/src/extension/popups/pages/Wallet/ImportWallet/index.tsx +++ b/packages/mask/src/extension/popups/pages/Wallet/ImportWallet/index.tsx @@ -1,10 +1,9 @@ import { memo, useMemo, useState } from 'react' import { Tab, Tabs, TextField, Typography } from '@mui/material' -import { makeStyles } from '@masknet/theme' +import { makeStyles, useTabs } from '@masknet/theme' import { Controller, useForm } from 'react-hook-form' import { z as zod } from 'zod' import { zodResolver } from '@hookform/resolvers/zod' -import { getEnumAsArray } from '@dimensiondev/kit' import { LoadingButton, TabContext, TabPanel } from '@mui/lab' import { useHistory } from 'react-router-dom' import { PopupRoutes } from '@masknet/shared-base' @@ -101,18 +100,12 @@ const useStyles = makeStyles()({ }, }) -enum ImportWalletTab { - Mnemonic = 'Mnemonic', - JsonFile = 'Json File', - PrivateKey = 'Private Key', -} - const ImportWallet = memo(() => { const { t } = useI18N() const history = useHistory() const chainId = useChainId() const { classes } = useStyles() - const [currentTab, setCurrentTab] = useState(ImportWalletTab.Mnemonic) + const [currentTab, onChange, tabs] = useTabs('mnemonic', 'json', 'privateKey') const [mnemonic, setMnemonic] = useState('') const [keyStoreContent, setKeyStoreContent] = useState('') const [keyStorePassword, setKeyStorePassword] = useState('') @@ -139,23 +132,23 @@ const ImportWallet = memo(() => { const disabled = useMemo(() => { if (!isValid) return true switch (currentTab) { - case ImportWalletTab.Mnemonic: + case tabs.mnemonic: return !mnemonic - case ImportWalletTab.JsonFile: + case tabs.json: return !keyStoreContent - case ImportWalletTab.PrivateKey: + case tabs.privateKey: return !privateKey default: return true } - }, [currentTab, mnemonic, keyStorePassword, keyStoreContent, privateKey, isValid]) + }, [currentTab, mnemonic, keyStorePassword, keyStoreContent, privateKey, isValid, tabs]) const [{ loading }, onDerivedWallet] = useAsyncFn( async (data: zod.infer) => { if (!disabled) { try { switch (currentTab) { - case ImportWalletTab.Mnemonic: + case tabs.mnemonic: // valid the mnemonic await getDerivableAccounts(mnemonic, 0, 1) const params = query({ name: data.name }) @@ -165,7 +158,7 @@ const ImportWallet = memo(() => { state: { mnemonic }, }) break - case ImportWalletTab.JsonFile: + case tabs.json: const wallet = await WalletRPC.recoverWalletFromKeyStoreJSON( data.name, keyStoreContent, @@ -186,7 +179,7 @@ const ImportWallet = memo(() => { history.replace(PopupRoutes.Wallet) await Services.Helper.removePopupWindow() break - case ImportWalletTab.PrivateKey: + case tabs.privateKey: const privateKeyWallet = await WalletRPC.recoverWalletFromPrivateKey(data.name, privateKey) if (!currentMaskWalletAccountSettings.value) { await WalletRPC.updateMaskAccount({ @@ -215,7 +208,7 @@ const ImportWallet = memo(() => { } } }, - [mnemonic, currentTab, keyStoreContent, keyStorePassword, privateKey, disabled, chainId, history], + [mnemonic, currentTab, keyStoreContent, keyStorePassword, privateKey, disabled, chainId, history, tabs], ) const onSubmit = handleSubmit(onDerivedWallet) @@ -251,18 +244,13 @@ const ImportWallet = memo(() => { classes={{ indicator: classes.indicator }} onChange={(event, tab) => { if (errorMessage) setErrorMessage('') - setCurrentTab(tab) + onChange(event, tab) }}> - {getEnumAsArray(ImportWalletTab).map(({ key, value }) => ( - - ))} + + + - + { }} minRows={4} maxRows={10} - placeholder="Please enter 12 mnemonic words separated by spaces" + placeholder={t('popups_wallet_name_mnemonic_placeholder')} InputProps={{ disableUnderline: true, classes: { root: classes.multilineInput } }} className={classes.multiline} inputProps={{ className: classes.textArea }} /> - + setKeyStoreContent(content)} /> { if (errorMessage) setErrorMessage('') setKeyStorePassword(e.target.value) @@ -291,7 +279,7 @@ const ImportWallet = memo(() => { value={keyStorePassword} /> - + { setPrivateKey(e.target.value) }} rows={4} - placeholder="Private Key" + placeholder={t('popups_wallet_name_private_key')} InputProps={{ disableUnderline: true, classes: { root: classes.multilineInput } }} className={classes.multiline} inputProps={{ className: classes.textArea }} diff --git a/packages/mask/src/extension/popups/pages/Wallet/WalletRecovery/index.tsx b/packages/mask/src/extension/popups/pages/Wallet/WalletRecovery/index.tsx index 8b18ecae324a..4aaad7ec18b9 100644 --- a/packages/mask/src/extension/popups/pages/Wallet/WalletRecovery/index.tsx +++ b/packages/mask/src/extension/popups/pages/Wallet/WalletRecovery/index.tsx @@ -184,7 +184,7 @@ const WalletRecovery = memo(() => { helperText={!isValid ? errors.confirm?.message : ''} type="password" variant="filled" - placeholder="Re-enter the payment password" + placeholder={t('popups_wallet_re_payment_password')} /> )} name="confirm" From 6c15e6f4c4a06e4fe4b54986d84ab31307d80fa3 Mon Sep 17 00:00:00 2001 From: nuanyang233 <528944303@qq.com> Date: Mon, 27 Dec 2021 17:00:49 +0800 Subject: [PATCH 3/4] chore: style --- .../popups/pages/Wallet/ImportWallet/index.tsx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/mask/src/extension/popups/pages/Wallet/ImportWallet/index.tsx b/packages/mask/src/extension/popups/pages/Wallet/ImportWallet/index.tsx index f648f8488239..39ce7e24b1ec 100644 --- a/packages/mask/src/extension/popups/pages/Wallet/ImportWallet/index.tsx +++ b/packages/mask/src/extension/popups/pages/Wallet/ImportWallet/index.tsx @@ -246,9 +246,21 @@ const ImportWallet = memo(() => { if (errorMessage) setErrorMessage('') onChange(event, tab) }}> - - - + + + Date: Mon, 27 Dec 2021 19:53:10 +0800 Subject: [PATCH 4/4] fix: add overrides to the replace request --- .../EthereumServices/request.ts | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/mask/src/extension/background-script/EthereumServices/request.ts b/packages/mask/src/extension/background-script/EthereumServices/request.ts index 7d5cab4417da..397928066fe6 100644 --- a/packages/mask/src/extension/background-script/EthereumServices/request.ts +++ b/packages/mask/src/extension/background-script/EthereumServices/request.ts @@ -173,16 +173,23 @@ export async function replaceRequest(hash: string, payload: JsonRpcPayload, over if (!pid || payload.method !== EthereumMethodType.ETH_SEND_TRANSACTION) return const [config] = payload.params as [TransactionConfig] - return request({ - method: EthereumMethodType.MASK_REPLACE_TRANSACTION, - params: [ - hash, - { - ...config, - ...overrides, - }, - ], - }) + return request( + { + method: EthereumMethodType.MASK_REPLACE_TRANSACTION, + params: [ + hash, + { + ...config, + ...overrides, + }, + ], + }, + { + account: currentMaskWalletAccountSettings.value, + chainId: currentMaskWalletChainIdSettings.value, + providerType: ProviderType.MaskWallet, + }, + ) } export async function cancelRequest(hash: string, payload: JsonRpcPayload, overrides?: TransactionConfig) {