|
| 1 | +/* eslint-disable complexity */ |
| 2 | +import { Alert, Button, Input, Modal, message } from 'antd'; |
| 3 | +import keyring from '@polkadot/ui-keyring'; |
| 4 | +import { encodeAddress } from '@polkadot/util-crypto'; |
| 5 | +import { useEffect, useRef, useState } from 'react'; |
| 6 | +import { useTranslation } from 'react-i18next'; |
| 7 | +import { useHistory } from 'react-router-dom'; |
| 8 | +import { useApi } from '../hooks'; |
| 9 | +import axiosRequest from '../hooks/AxiosRequest'; |
| 10 | +import { ShareScope } from '../model'; |
| 11 | +import { findLocalMultisig, updateMultiAccountScope } from '../utils'; |
| 12 | +import { |
| 13 | + ImportedMultisig, |
| 14 | + MultisigSearchResponse, |
| 15 | + parseMultisigSearch, |
| 16 | + verifyMultisig, |
| 17 | +} from '../utils/helper/importMultisig'; |
| 18 | +import { ApiKeyModal } from './modals/ApiKeyModal'; |
| 19 | + |
| 20 | +export function ImportWallet() { |
| 21 | + const { network, networkConfig, api, chain } = useApi(); |
| 22 | + // Changing networks discards the preview and invalidates any outstanding lookup. |
| 23 | + return ( |
| 24 | + <ImportWalletForm |
| 25 | + key={`${network}:${networkConfig?.api?.subscan}:${api?.genesisHash.toHex()}:${chain.ss58Format}`} |
| 26 | + /> |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +function ImportWalletForm() { |
| 31 | + const { t } = useTranslation(); |
| 32 | + const history = useHistory(); |
| 33 | + const { api, chain, network, networkConfig } = useApi(); |
| 34 | + const [visible, setVisible] = useState(false); |
| 35 | + const [apiKeyVisible, setApiKeyVisible] = useState(false); |
| 36 | + const [address, setAddress] = useState(''); |
| 37 | + const [name, setName] = useState(''); |
| 38 | + const [result, setResult] = useState<ImportedMultisig | null>(null); |
| 39 | + const [error, setError] = useState(''); |
| 40 | + const [loading, setLoading] = useState(false); |
| 41 | + const generation = useRef(0); |
| 42 | + useEffect( |
| 43 | + () => () => { |
| 44 | + generation.current++; |
| 45 | + }, |
| 46 | + [] |
| 47 | + ); |
| 48 | + |
| 49 | + const reset = () => { |
| 50 | + generation.current++; |
| 51 | + setResult(null); |
| 52 | + setError(''); |
| 53 | + setLoading(false); |
| 54 | + }; |
| 55 | + const lookup = async () => { |
| 56 | + reset(); |
| 57 | + const request = generation.current; |
| 58 | + let normalized: string; |
| 59 | + try { |
| 60 | + normalized = encodeAddress(address.trim(), Number(chain.ss58Format)); |
| 61 | + } catch { |
| 62 | + setError('You must input a ss58 format address'); |
| 63 | + return; |
| 64 | + } |
| 65 | + if (!networkConfig?.api?.subscan) { |
| 66 | + setError('importWallet.unsupported'); |
| 67 | + return; |
| 68 | + } |
| 69 | + setLoading(true); |
| 70 | + try { |
| 71 | + const { data } = await axiosRequest.post<MultisigSearchResponse['data']>( |
| 72 | + `${networkConfig.api.subscan}/api/v2/scan/search`, |
| 73 | + { key: normalized } |
| 74 | + ); |
| 75 | + const recovered = parseMultisigSearch(normalized, data); |
| 76 | + if (generation.current !== request) return; |
| 77 | + if (!recovered) { |
| 78 | + setError('importWallet.notFound'); |
| 79 | + return; |
| 80 | + } |
| 81 | + verifyMultisig( |
| 82 | + recovered, |
| 83 | + api?.consts.multisig?.maxSignatories ? Number(api.consts.multisig.maxSignatories.toString()) : undefined |
| 84 | + ); |
| 85 | + setResult({ ...recovered, id: normalized }); |
| 86 | + } catch (caught) { |
| 87 | + if (generation.current !== request) return; |
| 88 | + const response = (caught as { response?: { status?: number; data?: { code?: number } } }).response; |
| 89 | + const status = response?.data?.code || response?.status; |
| 90 | + // eslint-disable-next-line no-magic-numbers |
| 91 | + setError( |
| 92 | + status === 401 || status === 403 |
| 93 | + ? 'importWallet.apiKeyRequired' |
| 94 | + : caught instanceof Error && caught.message.startsWith('importWallet.') |
| 95 | + ? caught.message |
| 96 | + : 'importWallet.requestFailed' |
| 97 | + ); |
| 98 | + } finally { |
| 99 | + if (generation.current === request) setLoading(false); |
| 100 | + } |
| 101 | + }; |
| 102 | + const save = () => { |
| 103 | + if (!result || !api) return; |
| 104 | + try { |
| 105 | + verifyMultisig( |
| 106 | + result, |
| 107 | + api.consts.multisig?.maxSignatories ? Number(api.consts.multisig.maxSignatories.toString()) : undefined |
| 108 | + ); |
| 109 | + const existing = findLocalMultisig(result.id); |
| 110 | + if (existing && !existing.meta.isMultisig) throw new Error('importWallet.mismatch'); |
| 111 | + if (!existing || existing.meta.isTemp) { |
| 112 | + const members = result.members.map((member, index) => ({ |
| 113 | + address: encodeAddress(member, Number(chain.ss58Format)), |
| 114 | + name: `${t('importWallet.member')} ${index + 1}`, |
| 115 | + })); |
| 116 | + const walletName = name.trim() || `${result.id.slice(0, 6)}…${result.id.slice(-6)}`; |
| 117 | + keyring.addMultisig(result.members, result.threshold, { |
| 118 | + name: walletName, |
| 119 | + addressPair: members, |
| 120 | + genesisHash: api.genesisHash.toHex(), |
| 121 | + isTemp: false, |
| 122 | + }); |
| 123 | + updateMultiAccountScope( |
| 124 | + { |
| 125 | + name: walletName, |
| 126 | + members, |
| 127 | + threshold: result.threshold, |
| 128 | + share: ShareScope.current, |
| 129 | + rememberExternal: false, |
| 130 | + }, |
| 131 | + network |
| 132 | + ); |
| 133 | + } |
| 134 | + message.success(t('success')); |
| 135 | + setVisible(false); |
| 136 | + history.push(`/account/${result.id}${history.location.hash}`); |
| 137 | + } catch (caught) { |
| 138 | + setError(caught instanceof Error ? caught.message : 'importWallet.requestFailed'); |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + return ( |
| 143 | + <> |
| 144 | + <Button |
| 145 | + onClick={() => { |
| 146 | + reset(); |
| 147 | + setVisible(true); |
| 148 | + }} |
| 149 | + > |
| 150 | + {t('importWallet.title')} |
| 151 | + </Button> |
| 152 | + <Modal |
| 153 | + title={t('importWallet.title')} |
| 154 | + visible={visible} |
| 155 | + onCancel={() => { |
| 156 | + reset(); |
| 157 | + setVisible(false); |
| 158 | + }} |
| 159 | + footer={null} |
| 160 | + destroyOnClose |
| 161 | + > |
| 162 | + <p>{t('importWallet.network', { network: networkConfig?.displayName || network })}</p> |
| 163 | + <Alert type="info" showIcon message={t('importWallet.scope')} className="mb-4" /> |
| 164 | + <label htmlFor="import-wallet-address">{t('importWallet.address')}</label> |
| 165 | + <Input |
| 166 | + id="import-wallet-address" |
| 167 | + value={address} |
| 168 | + onChange={(event) => { |
| 169 | + reset(); |
| 170 | + setAddress(event.target.value); |
| 171 | + }} |
| 172 | + onPressEnter={() => !loading && lookup()} |
| 173 | + className="mb-4" |
| 174 | + /> |
| 175 | + <Button loading={loading} onClick={lookup} disabled={!address.trim()}> |
| 176 | + {t('importWallet.lookup')} |
| 177 | + </Button> |
| 178 | + <Button type="link" onClick={() => setApiKeyVisible(true)}> |
| 179 | + {t('api_key.title')} |
| 180 | + </Button> |
| 181 | + {error && <Alert className="mt-4" type="error" showIcon message={t(error)} />} |
| 182 | + {result && ( |
| 183 | + <div className="mt-4"> |
| 184 | + <p>{t('importWallet.threshold', { threshold: result.threshold, count: result.members.length })}</p> |
| 185 | + <ul className="mb-4"> |
| 186 | + {result.members.map((member) => ( |
| 187 | + <li key={member} className="break-all py-1"> |
| 188 | + {encodeAddress(member, Number(chain.ss58Format))} |
| 189 | + </li> |
| 190 | + ))} |
| 191 | + </ul> |
| 192 | + <label htmlFor="import-wallet-name">{t('importWallet.name')}</label> |
| 193 | + <Input |
| 194 | + id="import-wallet-name" |
| 195 | + value={name} |
| 196 | + onChange={(event) => setName(event.target.value)} |
| 197 | + className="mb-4" |
| 198 | + /> |
| 199 | + <Button type="primary" onClick={save}> |
| 200 | + {t('importWallet.confirm')} |
| 201 | + </Button> |
| 202 | + </div> |
| 203 | + )} |
| 204 | + <p className="mt-4"> |
| 205 | + <a href={`/wallet${history.location.hash}`}>{t('importWallet.manual')}</a> |
| 206 | + </p> |
| 207 | + </Modal> |
| 208 | + <ApiKeyModal visible={apiKeyVisible} onCancel={() => setApiKeyVisible(false)} /> |
| 209 | + </> |
| 210 | + ); |
| 211 | +} |
0 commit comments