Skip to content

Commit 6f1a30a

Browse files
joelamouchejacogr
andauthored
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 <[email protected]> * add types * Update packages/apps-config/package.json Co-authored-by: Jaco <[email protected]> * fix moonbeam types bundle version Co-authored-by: Jaco <[email protected]>
1 parent 7f619f8 commit 6f1a30a

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

packages/apps-config/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@sora-substrate/type-definitions": "1.3.34",
2626
"@subsocial/types": "0.5.9-dev.2",
2727
"@zeitgeistpm/type-defs": "0.2.0",
28-
"moonbeam-types-bundle": "1.1.25",
28+
"moonbeam-types-bundle": "1.1.26",
2929
"pontem-types-bundle": "1.0.5",
3030
"rxjs": "^7.2.0"
3131
},

packages/page-accounts/src/modals/Import.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2017-2021 @polkadot/app-accounts authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
import type { Dispatch, SetStateAction } from 'react';
45
import type { KeyringPair, KeyringPair$Json } from '@polkadot/keyring/types';
56
import type { ActionStatus } from '@polkadot/react-components/Status/types';
67
import type { ModalProps } from '../types';
@@ -28,28 +29,34 @@ interface PassState {
2829

2930
const acceptedFormats = ['application/json', 'text/plain'].join(', ');
3031

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 {
3233
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;
3439
} catch (error) {
3540
console.error(error);
41+
setWarning((error as Error).message ? (error as Error).message : (error as Error).toString());
3642
}
3743

3844
return null;
3945
}
4046

4147
function Import ({ className = '', onClose, onStatusChange }: Props): React.ReactElement<Props> {
4248
const { t } = useTranslation();
43-
const { api, isDevelopment } = useApi();
49+
const { api, isDevelopment, isEthereum } = useApi();
4450
const [isBusy, setIsBusy] = useState(false);
4551
const [pair, setPair] = useState<KeyringPair | null>(null);
52+
const [warning, setWarning] = useState<string | null>(null);
4653
const [{ isPassValid, password }, setPass] = useState<PassState>({ isPassValid: false, password: '' });
4754
const apiGenesisHash = useMemo(() => isDevelopment ? null : api.genesisHash.toHex(), [api, isDevelopment]);
4855
const differentGenesis = useMemo(() => pair?.meta.genesisHash && pair.meta.genesisHash !== apiGenesisHash, [apiGenesisHash, pair]);
4956

5057
const _onChangeFile = useCallback(
51-
(file: Uint8Array) => setPair(parseFile(file, apiGenesisHash)),
52-
[apiGenesisHash]
58+
(file: Uint8Array) => setPair(parseFile(file, setWarning, isEthereum, apiGenesisHash)),
59+
[apiGenesisHash, isEthereum]
5360
);
5461

5562
const _onChangePass = useCallback(
@@ -136,6 +143,7 @@ function Import ({ className = '', onClose, onStatusChange }: Props): React.Reac
136143
</Modal.Columns>
137144
<Modal.Columns>
138145
<ExternalWarning />
146+
{warning && <MarkWarning content={warning} />}
139147
</Modal.Columns>
140148
</Modal.Content>
141149
<Modal.Actions onCancel={onClose}>

yarn.lock

+9-8
Original file line numberDiff line numberDiff line change
@@ -2583,7 +2583,7 @@ __metadata:
25832583
"@subsocial/types": 0.5.9-dev.2
25842584
"@zeitgeistpm/type-defs": 0.2.0
25852585
i18next: ^19.9.2
2586-
moonbeam-types-bundle: 1.1.25
2586+
moonbeam-types-bundle: 1.1.26
25872587
pontem-types-bundle: 1.0.5
25882588
rxjs: ^7.2.0
25892589
languageName: unknown
@@ -14023,14 +14023,15 @@ __metadata:
1402314023
languageName: node
1402414024
linkType: hard
1402514025

14026-
"moonbeam-types-bundle@npm:1.1.25":
14027-
version: 1.1.25
14028-
resolution: "moonbeam-types-bundle@npm:1.1.25"
14026+
"moonbeam-types-bundle@npm:1.1.26":
14027+
version: 1.1.26
14028+
resolution: "moonbeam-types-bundle@npm:1.1.26"
1402914029
dependencies:
14030-
"@polkadot/keyring": ^6.8.1
14031-
"@polkadot/types": ^4.14.1
14032-
typescript: ^4.1.3
14033-
checksum: 33d5a2f7dbdc77bb524574dc7784973e25bea487d6996c65198378be7f5fb840e389642dd5617f8e6164182f7a00409c28019b04a819420d092686eb84c32a87
14030+
"@polkadot/keyring": ^7.0.1
14031+
"@polkadot/types": ^5.0.1
14032+
rxjs: ^7.2.0
14033+
typescript: ^4.3.5
14034+
checksum: 65606309f135f4e99759d07f178c45348f41d15cdc9bd74fcf017eda05c2ea5c6f9048a8634765ab45fbbd27a442765de80d2e55dd8569f9fce2b6c11cc3a8da
1403414035
languageName: node
1403514036
linkType: hard
1403614037

0 commit comments

Comments
 (0)