Unlock.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2017-2020 @polkadot/app-toolbox 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 React, { useCallback, useEffect, useState } from 'react';
  6. import { Button, InputAddress, Modal, Password } from '@polkadot/react-components';
  7. import { useTranslation } from './translate';
  8. interface Props {
  9. onClose: () => void;
  10. onUnlock: () => void;
  11. pair: KeyringPair | null;
  12. }
  13. function Unlock ({ onClose, onUnlock, pair }: Props): React.ReactElement<Props> | null {
  14. const { t } = useTranslation();
  15. const [isBusy, setIsBusy] = useState(false);
  16. const [address, setAddress] = useState('');
  17. const [password, setPassword] = useState('');
  18. const [unlockError, setUnlockError] = useState<string | null>(null);
  19. useEffect((): void => {
  20. setAddress(pair?.address || '');
  21. }, [pair?.address]);
  22. useEffect((): void => {
  23. setUnlockError(null);
  24. }, [password]);
  25. const _onUnlock = useCallback(
  26. (): void => {
  27. if (!pair || !pair.isLocked) {
  28. return;
  29. }
  30. setIsBusy(true);
  31. setTimeout((): void => {
  32. try {
  33. pair.decodePkcs8(password);
  34. } catch (error) {
  35. setIsBusy(false);
  36. return setUnlockError((error as Error).message);
  37. }
  38. setIsBusy(false);
  39. onUnlock();
  40. }, 0);
  41. },
  42. [onUnlock, pair, password]
  43. );
  44. if (!pair) {
  45. return null;
  46. }
  47. return (
  48. <Modal
  49. className='toolbox--Unlock'
  50. header={t<string>('Unlock account')}
  51. size='large'
  52. >
  53. <Modal.Content>
  54. <Modal.Columns>
  55. <Modal.Column>
  56. <InputAddress
  57. help={t<string>('The selected account to be unlocked.')}
  58. isDisabled
  59. label={t<string>('account')}
  60. value={address}
  61. />
  62. </Modal.Column>
  63. <Modal.Column>
  64. <p>{t<string>('This account that will perform the message signing.')}</p>
  65. </Modal.Column>
  66. </Modal.Columns>
  67. <Modal.Columns>
  68. <Modal.Column>
  69. <Password
  70. autoFocus
  71. help={t<string>('The account\'s password specified at the creation of this account.')}
  72. isError={!!unlockError}
  73. label={t<string>('password')}
  74. onChange={setPassword}
  75. onEnter={_onUnlock}
  76. value={password}
  77. />
  78. </Modal.Column>
  79. <Modal.Column>
  80. <p>{t<string>('Unlock the account for signing. Once active the signature will be generated based on the content provided.')}</p>
  81. </Modal.Column>
  82. </Modal.Columns>
  83. </Modal.Content>
  84. <Modal.Actions onCancel={onClose}>
  85. <Button
  86. icon='unlock'
  87. isBusy={isBusy}
  88. label={t<string>('Unlock')}
  89. onClick={_onUnlock}
  90. />
  91. </Modal.Actions>
  92. </Modal>
  93. );
  94. }
  95. export default React.memo(Unlock);