From 6f1a30ab23980b51ef2d6ab2aaf7a0d0038c4d37 Mon Sep 17 00:00:00 2001 From: Antoine Estienne Date: Tue, 20 Jul 2021 12:12:17 +0200 Subject: [PATCH] Add type check for json address import (#5841) * add type check for json address i;port * update type and check isEthereum * Update packages/page-accounts/src/modals/Import.tsx Co-authored-by: Jaco * add types * Update packages/apps-config/package.json Co-authored-by: Jaco * fix moonbeam types bundle version Co-authored-by: Jaco --- packages/apps-config/package.json | 2 +- packages/page-accounts/src/modals/Import.tsx | 18 +++++++++++++----- yarn.lock | 17 +++++++++-------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/packages/apps-config/package.json b/packages/apps-config/package.json index 2d26c1268c..9a060488be 100644 --- a/packages/apps-config/package.json +++ b/packages/apps-config/package.json @@ -25,7 +25,7 @@ "@sora-substrate/type-definitions": "1.3.34", "@subsocial/types": "0.5.9-dev.2", "@zeitgeistpm/type-defs": "0.2.0", - "moonbeam-types-bundle": "1.1.25", + "moonbeam-types-bundle": "1.1.26", "pontem-types-bundle": "1.0.5", "rxjs": "^7.2.0" }, diff --git a/packages/page-accounts/src/modals/Import.tsx b/packages/page-accounts/src/modals/Import.tsx index 4dbc809c41..4b6df8bb47 100644 --- a/packages/page-accounts/src/modals/Import.tsx +++ b/packages/page-accounts/src/modals/Import.tsx @@ -1,6 +1,7 @@ // Copyright 2017-2021 @polkadot/app-accounts authors & contributors // SPDX-License-Identifier: Apache-2.0 +import type { Dispatch, SetStateAction } from 'react'; import type { KeyringPair, KeyringPair$Json } from '@polkadot/keyring/types'; import type { ActionStatus } from '@polkadot/react-components/Status/types'; import type { ModalProps } from '../types'; @@ -28,11 +29,16 @@ interface PassState { const acceptedFormats = ['application/json', 'text/plain'].join(', '); -function parseFile (file: Uint8Array, genesisHash?: string | null): KeyringPair | null { +function parseFile (file: Uint8Array, setWarning: Dispatch>, isEthereum: boolean, genesisHash?: string | null): KeyringPair | null { try { - return keyring.createFromJson(JSON.parse(u8aToString(file)) as KeyringPair$Json, { genesisHash }); + const pair = keyring.createFromJson(JSON.parse(u8aToString(file)) as KeyringPair$Json, { genesisHash }); + + if (isEthereum && pair.type !== 'ethereum') { throw new Error('JSON File does not contain an ethereum type key pair'); } + + return pair; } catch (error) { console.error(error); + setWarning((error as Error).message ? (error as Error).message : (error as Error).toString()); } return null; @@ -40,16 +46,17 @@ function parseFile (file: Uint8Array, genesisHash?: string | null): KeyringPair function Import ({ className = '', onClose, onStatusChange }: Props): React.ReactElement { const { t } = useTranslation(); - const { api, isDevelopment } = useApi(); + const { api, isDevelopment, isEthereum } = useApi(); const [isBusy, setIsBusy] = useState(false); const [pair, setPair] = useState(null); + const [warning, setWarning] = useState(null); const [{ isPassValid, password }, setPass] = useState({ isPassValid: false, password: '' }); const apiGenesisHash = useMemo(() => isDevelopment ? null : api.genesisHash.toHex(), [api, isDevelopment]); const differentGenesis = useMemo(() => pair?.meta.genesisHash && pair.meta.genesisHash !== apiGenesisHash, [apiGenesisHash, pair]); const _onChangeFile = useCallback( - (file: Uint8Array) => setPair(parseFile(file, apiGenesisHash)), - [apiGenesisHash] + (file: Uint8Array) => setPair(parseFile(file, setWarning, isEthereum, apiGenesisHash)), + [apiGenesisHash, isEthereum] ); const _onChangePass = useCallback( @@ -136,6 +143,7 @@ function Import ({ className = '', onClose, onStatusChange }: Props): React.Reac + {warning && } diff --git a/yarn.lock b/yarn.lock index b9f5c6fc3f..1bdb268bd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2583,7 +2583,7 @@ __metadata: "@subsocial/types": 0.5.9-dev.2 "@zeitgeistpm/type-defs": 0.2.0 i18next: ^19.9.2 - moonbeam-types-bundle: 1.1.25 + moonbeam-types-bundle: 1.1.26 pontem-types-bundle: 1.0.5 rxjs: ^7.2.0 languageName: unknown @@ -14023,14 +14023,15 @@ __metadata: languageName: node linkType: hard -"moonbeam-types-bundle@npm:1.1.25": - version: 1.1.25 - resolution: "moonbeam-types-bundle@npm:1.1.25" +"moonbeam-types-bundle@npm:1.1.26": + version: 1.1.26 + resolution: "moonbeam-types-bundle@npm:1.1.26" dependencies: - "@polkadot/keyring": ^6.8.1 - "@polkadot/types": ^4.14.1 - typescript: ^4.1.3 - checksum: 33d5a2f7dbdc77bb524574dc7784973e25bea487d6996c65198378be7f5fb840e389642dd5617f8e6164182f7a00409c28019b04a819420d092686eb84c32a87 + "@polkadot/keyring": ^7.0.1 + "@polkadot/types": ^5.0.1 + rxjs: ^7.2.0 + typescript: ^4.3.5 + checksum: 65606309f135f4e99759d07f178c45348f41d15cdc9bd74fcf017eda05c2ea5c6f9048a8634765ab45fbbd27a442765de80d2e55dd8569f9fce2b6c11cc3a8da languageName: node linkType: hard