|
1 | 1 | // Copyright 2017-2021 @polkadot/app-accounts authors & contributors
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
| 4 | +import type { Dispatch, SetStateAction } from 'react'; |
4 | 5 | import type { KeyringPair, KeyringPair$Json } from '@polkadot/keyring/types';
|
5 | 6 | import type { ActionStatus } from '@polkadot/react-components/Status/types';
|
6 | 7 | import type { ModalProps } from '../types';
|
@@ -28,28 +29,34 @@ interface PassState {
|
28 | 29 |
|
29 | 30 | const acceptedFormats = ['application/json', 'text/plain'].join(', ');
|
30 | 31 |
|
31 |
| -function parseFile (file: Uint8Array, genesisHash?: string | null): KeyringPair | null { |
| 32 | +function parseFile (file: Uint8Array, setWarning: Dispatch<SetStateAction<string | null>>, isEthereum: boolean, genesisHash?: string | null): KeyringPair | null { |
32 | 33 | try {
|
33 |
| - return keyring.createFromJson(JSON.parse(u8aToString(file)) as KeyringPair$Json, { genesisHash }); |
| 34 | + const pair = keyring.createFromJson(JSON.parse(u8aToString(file)) as KeyringPair$Json, { genesisHash }); |
| 35 | + |
| 36 | + if (isEthereum && pair.type !== 'ethereum') { throw new Error('JSON File does not contain an ethereum type key pair'); } |
| 37 | + |
| 38 | + return pair; |
34 | 39 | } catch (error) {
|
35 | 40 | console.error(error);
|
| 41 | + setWarning((error as Error).message ? (error as Error).message : (error as Error).toString()); |
36 | 42 | }
|
37 | 43 |
|
38 | 44 | return null;
|
39 | 45 | }
|
40 | 46 |
|
41 | 47 | function Import ({ className = '', onClose, onStatusChange }: Props): React.ReactElement<Props> {
|
42 | 48 | const { t } = useTranslation();
|
43 |
| - const { api, isDevelopment } = useApi(); |
| 49 | + const { api, isDevelopment, isEthereum } = useApi(); |
44 | 50 | const [isBusy, setIsBusy] = useState(false);
|
45 | 51 | const [pair, setPair] = useState<KeyringPair | null>(null);
|
| 52 | + const [warning, setWarning] = useState<string | null>(null); |
46 | 53 | const [{ isPassValid, password }, setPass] = useState<PassState>({ isPassValid: false, password: '' });
|
47 | 54 | const apiGenesisHash = useMemo(() => isDevelopment ? null : api.genesisHash.toHex(), [api, isDevelopment]);
|
48 | 55 | const differentGenesis = useMemo(() => pair?.meta.genesisHash && pair.meta.genesisHash !== apiGenesisHash, [apiGenesisHash, pair]);
|
49 | 56 |
|
50 | 57 | const _onChangeFile = useCallback(
|
51 |
| - (file: Uint8Array) => setPair(parseFile(file, apiGenesisHash)), |
52 |
| - [apiGenesisHash] |
| 58 | + (file: Uint8Array) => setPair(parseFile(file, setWarning, isEthereum, apiGenesisHash)), |
| 59 | + [apiGenesisHash, isEthereum] |
53 | 60 | );
|
54 | 61 |
|
55 | 62 | const _onChangePass = useCallback(
|
@@ -136,6 +143,7 @@ function Import ({ className = '', onClose, onStatusChange }: Props): React.Reac
|
136 | 143 | </Modal.Columns>
|
137 | 144 | <Modal.Columns>
|
138 | 145 | <ExternalWarning />
|
| 146 | + {warning && <MarkWarning content={warning} />} |
139 | 147 | </Modal.Columns>
|
140 | 148 | </Modal.Content>
|
141 | 149 | <Modal.Actions onCancel={onClose}>
|
|
0 commit comments