Derive.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2017-2020 @polkadot/app-accounts authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { KeyringPair } from '@polkadot/keyring/types';
  5. import { ActionStatus } from '@polkadot/react-components/Status/types';
  6. import { KeypairType } from '@polkadot/util-crypto/types';
  7. import React, { useContext, useEffect, useState } from 'react';
  8. import { AddressRow, Button, Input, InputAddress, Modal, Password, StatusContext } from '@polkadot/react-components';
  9. import { useDebounce } from '@polkadot/react-hooks';
  10. import keyring from '@polkadot/ui-keyring';
  11. import { keyExtractPath } from '@polkadot/util-crypto';
  12. import { useTranslation } from '../../translate';
  13. import { downloadAccount } from './Create';
  14. import CreateConfirmation from './CreateConfirmation';
  15. interface Props {
  16. className?: string;
  17. from: string;
  18. onClose: () => void;
  19. }
  20. interface DeriveAddress {
  21. address: string | null;
  22. deriveError: string | null;
  23. }
  24. function deriveValidate (suri: string, pairType: KeypairType): string | null {
  25. try {
  26. const { path } = keyExtractPath(suri);
  27. // we don't allow soft for ed25519
  28. if (pairType === 'ed25519' && path.some(({ isSoft }): boolean => isSoft)) {
  29. return 'Soft derivation paths are not allowed on ed25519';
  30. }
  31. } catch (error) {
  32. return error.message;
  33. }
  34. return null;
  35. }
  36. function createAccount (source: KeyringPair, suri: string, name: string, password: string, success: string): ActionStatus {
  37. // we will fill in all the details below
  38. const status = { action: 'create' } as ActionStatus;
  39. try {
  40. const derived = source.derive(suri);
  41. derived.setMeta({ ...derived.meta, name, tags: [] });
  42. const result = keyring.addPair(derived, password || '');
  43. const { address } = result.pair;
  44. status.account = address;
  45. status.status = 'success';
  46. status.message = success;
  47. downloadAccount(result);
  48. } catch (error) {
  49. status.status = 'error';
  50. status.message = error.message;
  51. }
  52. return status;
  53. }
  54. function Derive ({ className, from, onClose }: Props): React.ReactElement {
  55. const { t } = useTranslation();
  56. const { queueAction } = useContext(StatusContext);
  57. const [source] = useState(keyring.getPair(from));
  58. const [{ address, deriveError }, setDerive] = useState<DeriveAddress>({ address: null, deriveError: null });
  59. const [isConfirmationOpen, setIsConfirmationOpen] = useState(false);
  60. const [isLocked, setIsLocked] = useState(source.isLocked);
  61. const [{ isNameValid, name }, setName] = useState({ isNameValid: false, name: '' });
  62. const [{ isPassValid, password }, setPassword] = useState({ isPassValid: false, password: '' });
  63. const [{ isPass2Valid, password2 }, setPassword2] = useState({ isPass2Valid: false, password2: '' });
  64. const [rootPass, setRootPass] = useState('');
  65. const [suri, setSuri] = useState('');
  66. const debouncedSuri = useDebounce(suri);
  67. const isValid = !!address && !deriveError && isNameValid && isPassValid && isPass2Valid;
  68. useEffect((): void => {
  69. setIsLocked(source.isLocked);
  70. }, [source]);
  71. useEffect((): void => {
  72. setDerive((): DeriveAddress => {
  73. let address: string | null = null;
  74. const deriveError = deriveValidate(debouncedSuri, source.type);
  75. if (!deriveError) {
  76. const result = source.derive(debouncedSuri);
  77. address = result.address;
  78. }
  79. return { address, deriveError };
  80. });
  81. }, [debouncedSuri, source]);
  82. const _onChangeName = (name: string): void =>
  83. setName({ isNameValid: !!name.trim(), name });
  84. const _onChangePass = (password: string): void =>
  85. setPassword({ isPassValid: keyring.isPassValid(password), password });
  86. const _onChangePass2 = (password2: string): void =>
  87. setPassword2({ isPass2Valid: keyring.isPassValid(password2) && (password2 === password), password2 });
  88. const _toggleConfirmation = (): void => setIsConfirmationOpen(!isConfirmationOpen);
  89. const _onUnlock = (): void => {
  90. try {
  91. source.decodePkcs8(rootPass);
  92. } catch (error) {
  93. console.error(error);
  94. }
  95. setIsLocked(source.isLocked);
  96. };
  97. const _onCommit = (): void => {
  98. if (!isValid) {
  99. return;
  100. }
  101. const status = createAccount(source, suri, name, password, t('created account'));
  102. _toggleConfirmation();
  103. queueAction(status);
  104. onClose();
  105. };
  106. const sourceStatic = (
  107. <InputAddress
  108. help={t('The selected account to perform the derivation on.')}
  109. isDisabled
  110. label={t('derive root account')}
  111. value={from}
  112. />
  113. );
  114. return (
  115. <Modal
  116. className={className}
  117. header={t('Derive account from pair')}
  118. >
  119. {address && isConfirmationOpen && (
  120. <CreateConfirmation
  121. address={address}
  122. name={name}
  123. onClose={_toggleConfirmation}
  124. onCommit={_onCommit}
  125. />
  126. )}
  127. <Modal.Content>
  128. {isLocked && (
  129. <>
  130. {sourceStatic}
  131. <Password
  132. autoFocus
  133. help={t('The password to unlock the selected account.')}
  134. label={t('password')}
  135. onChange={setRootPass}
  136. value={rootPass}
  137. />
  138. </>
  139. )}
  140. {!isLocked && (
  141. <AddressRow
  142. defaultName={name}
  143. noDefaultNameOpacity
  144. value={deriveError ? '' : address}
  145. >
  146. {sourceStatic}
  147. <Input
  148. autoFocus
  149. help={t('You can set a custom derivation path for this account using the following syntax "/<soft-key>//<hard-key>///<password>". The "/<soft-key>" and "//<hard-key>" may be repeated and mixed`. The "///password" is optional and should only occur once.')}
  150. label={t('derivation path')}
  151. onChange={setSuri}
  152. placeholder={t('//hard/soft')}
  153. />
  154. <Input
  155. className='full'
  156. help={t('Name given to this account. You can edit it. To use the account to validate or nominate, it is a good practice to append the function of the account in the name, e.g "name_you_want - stash".')}
  157. isError={!isNameValid}
  158. label={t('name')}
  159. onChange={_onChangeName}
  160. onEnter={_onCommit}
  161. placeholder={t('new account')}
  162. value={name}
  163. />
  164. <Password
  165. className='full'
  166. help={t('This password is used to encrypt your private key. It must be strong and unique! You will need it to sign transactions with this account. You can recover this account using this password together with the backup file (generated in the next step).')}
  167. isError={!isPassValid}
  168. label={t('password')}
  169. onChange={_onChangePass}
  170. onEnter={_onCommit}
  171. value={password}
  172. />
  173. <Password
  174. className='full'
  175. help={t('Verify the password entered above.')}
  176. isError={!isPass2Valid}
  177. label={t('password (repeat)')}
  178. onChange={_onChangePass2}
  179. onEnter={_onCommit}
  180. value={password2}
  181. />
  182. </AddressRow>
  183. )}
  184. </Modal.Content>
  185. <Modal.Actions onCancel={onClose}>
  186. {isLocked
  187. ? (
  188. <Button
  189. icon='lock'
  190. isDisabled={!rootPass}
  191. isPrimary
  192. label={t('Unlock')}
  193. onClick={_onUnlock}
  194. />
  195. )
  196. : (
  197. <Button
  198. icon='plus'
  199. isDisabled={!isValid}
  200. isPrimary
  201. label={t('Save')}
  202. onClick={_toggleConfirmation}
  203. />
  204. )
  205. }
  206. </Modal.Actions>
  207. </Modal>
  208. );
  209. }
  210. export default React.memo(Derive);