From a89fbdef5aeeb7b09994c67cc8791939f0151f85 Mon Sep 17 00:00:00 2001 From: Thibaut Sardan <33178835+Tbaut@users.noreply.github.com> Date: Mon, 21 Sep 2020 16:23:14 +0200 Subject: [PATCH] Show warning and genesis override when importing JSON with different genesis hash (#3732) * warning and genesis override * Update packages/page-accounts/src/modals/Import.tsx Co-authored-by: Jaco Greeff Co-authored-by: Jaco Greeff --- packages/page-accounts/src/modals/Import.tsx | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/page-accounts/src/modals/Import.tsx b/packages/page-accounts/src/modals/Import.tsx index 168ed787d9..46ec56bf50 100644 --- a/packages/page-accounts/src/modals/Import.tsx +++ b/packages/page-accounts/src/modals/Import.tsx @@ -5,8 +5,9 @@ import { KeyringPair$Json } from '@polkadot/keyring/types'; import { ActionStatus } from '@polkadot/react-components/Status/types'; import { ModalProps } from '../types'; -import React, { useCallback, useState } from 'react'; +import React, { useCallback, useMemo, useState } from 'react'; import { AddressRow, Button, InputAddress, InputFile, Modal, Password } from '@polkadot/react-components'; +import { useApi } from '@polkadot/react-hooks'; import { hexToU8a, isHex, isObject, u8aToString } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; import keyring from '@polkadot/ui-keyring'; @@ -63,9 +64,12 @@ function parseFile (file: Uint8Array): FileState { function Import ({ className = '', onClose, onStatusChange }: Props): React.ReactElement { const { t } = useTranslation(); + const { api } = useApi(); const [isBusy, setIsBusy] = useState(false); const [{ address, isFileValid, json }, setFile] = useState({ address: null, isFileValid: false, json: null }); const [{ isPassValid, password }, setPass] = useState({ isPassValid: false, password: '' }); + const apiGenesisHash = useMemo(() => api.genesisHash.toHex(), [api]); + const differentGenesis = useMemo(() => json?.meta.genesisHash && json.meta.genesisHash !== apiGenesisHash, [apiGenesisHash, json]); const _onChangeFile = useCallback( (file: Uint8Array) => setFile(parseFile(file)), @@ -88,7 +92,11 @@ function Import ({ className = '', onClose, onStatusChange }: Props): React.Reac const status: Partial = { action: 'restore' }; try { - const pair = keyring.restoreAccount(json, password); + const pair = keyring.restoreAccount({ + ...json, + // forcing the use of current genesis hash + meta: { ...json.meta, genesisHash: apiGenesisHash } + }, password); const { address } = pair; status.status = pair ? 'success' : 'error'; @@ -112,7 +120,7 @@ function Import ({ className = '', onClose, onStatusChange }: Props): React.Reac } }, 0); }, - [json, onClose, onStatusChange, password, t] + [apiGenesisHash, json, onClose, onStatusChange, password, t] ); return ( @@ -164,6 +172,11 @@ function Import ({ className = '', onClose, onStatusChange }: Props): React.Reac

{t('The password previously used to encrypt this account.')}

+ { differentGenesis && +
+

{t('The network from which this account was originally generated is different than the network you are currently connected to. Once imported ensure you toggle the "allow on any network" option for the account to keep it visible on the current network.')}

+
+ }