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 <jacogr@gmail.com>

* add types

* Update packages/apps-config/package.json

Co-authored-by: Jaco <jacogr@gmail.com>

* fix moonbeam types bundle version

Co-authored-by: Jaco <jacogr@gmail.com>
This commit is contained in:
Antoine Estienne 2021-07-20 12:12:17 +02:00 committed by GitHub
parent 7f619f8031
commit 6f1a30ab23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 14 deletions

View File

@ -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"
},

View File

@ -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<SetStateAction<string | null>>, 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<Props> {
const { t } = useTranslation();
const { api, isDevelopment } = useApi();
const { api, isDevelopment, isEthereum } = useApi();
const [isBusy, setIsBusy] = useState(false);
const [pair, setPair] = useState<KeyringPair | null>(null);
const [warning, setWarning] = useState<string | null>(null);
const [{ isPassValid, password }, setPass] = useState<PassState>({ 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
</Modal.Columns>
<Modal.Columns>
<ExternalWarning />
{warning && <MarkWarning content={warning} />}
</Modal.Columns>
</Modal.Content>
<Modal.Actions onCancel={onClose}>

View File

@ -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