// Copyright 2019-2025 @polkadot/extension-ui authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ResponseJsonGetAccountInfo } from '@polkadot/extension-base/background/types'; import type { KeyringPair$Json } from '@polkadot/keyring/types'; import type { KeyringPairs$Json } from '@polkadot/ui-keyring/types'; import React, { useCallback, useContext, useEffect, useState } from 'react'; import { u8aToString } from '@polkadot/util'; import { AccountContext, ActionContext, Address, Button, InputFileWithLabel, InputWithLabel, Warning } from '../components/index.js'; import { useTranslation } from '../hooks/index.js'; import { batchRestore, jsonGetAccountInfo, jsonRestore } from '../messaging.js'; import { Header } from '../partials/index.js'; import { styled } from '../styled.js'; import { DEFAULT_TYPE } from '../util/defaultType.js'; import { isKeyringPairs$Json } from '../util/typeGuards.js'; const acceptedFormats = ['application/json', 'text/plain'].join(', '); interface Props { className?: string; } function Upload ({ className }: Props): React.ReactElement { const { t } = useTranslation(); const { accounts } = useContext(AccountContext); const onAction = useContext(ActionContext); const [isBusy, setIsBusy] = useState(false); const [accountsInfo, setAccountsInfo] = useState([]); const [password, setPassword] = useState(''); const [isFileError, setFileError] = useState(false); const [requirePassword, setRequirePassword] = useState(false); const [isPasswordError, setIsPasswordError] = useState(false); // don't use the info from the file directly // rather use what comes from the background from jsonGetAccountInfo const [file, setFile] = useState(undefined); useEffect((): void => { !accounts.length && onAction(); }, [accounts, onAction]); const _onChangePass = useCallback( (pass: string): void => { setPassword(pass); setIsPasswordError(false); }, [] ); const _onChangeFile = useCallback( (file: Uint8Array): void => { setAccountsInfo(() => []); let json: KeyringPair$Json | KeyringPairs$Json | undefined; try { json = JSON.parse(u8aToString(file)) as KeyringPair$Json | KeyringPairs$Json; setFile(json); } catch (e) { console.error(e); setFileError(true); } if (json === undefined) { return; } if (isKeyringPairs$Json(json)) { setRequirePassword(true); json.accounts.forEach((account) => { setAccountsInfo((old) => [...old, { address: account.address, genesisHash: account.meta.genesisHash, name: account.meta.name } as ResponseJsonGetAccountInfo]); }); } else { setRequirePassword(true); jsonGetAccountInfo(json) .then((accountInfo) => setAccountsInfo((old) => [...old, accountInfo])) .catch((e) => { setFileError(true); console.error(e); }); } }, [] ); const _onRestore = useCallback( (): void => { if (!file) { return; } if (requirePassword && !password) { return; } setIsBusy(true); (isKeyringPairs$Json(file) ? batchRestore(file, password) : jsonRestore(file, password)) .then(() => { onAction('/'); }) .catch((e) => { console.error(e); setIsBusy(false); setIsPasswordError(true); }); }, [file, onAction, password, requirePassword] ); return ( <>
{accountsInfo.map(({ address, genesisHash, name, type = DEFAULT_TYPE }, index) => (
))} {isFileError && ( {t('Invalid Json file')} )} {requirePassword && (
{isPasswordError && ( {t('Unable to decode using the supplied passphrase')} )}
)}
); } export default styled(Upload)` .restoreButton { margin-top: 16px; } `;