From 38c02cd6662f42a79da56983f18170ad1558e867 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 10 May 2018 13:57:49 +0200 Subject: [PATCH] Allow unlock on locked accounts (extrinsics) (#37) --- packages/app-accounts/src/Creator.js | 15 +- packages/app-extrinsics/src/Signer/Decoded.js | 48 +++-- packages/app-extrinsics/src/Signer/Signer.css | 15 +- packages/app-extrinsics/src/Signer/Unlock.js | 108 +++++++++++ packages/app-extrinsics/src/Signer/index.js | 175 ++++++++++++++---- packages/app-extrinsics/src/index.js | 6 +- packages/app-extrinsics/src/types.js | 2 +- packages/ui-keyring/src/index.js | 8 +- packages/ui-keyring/src/save/accountCreate.js | 17 ++ packages/ui-keyring/src/types.js | 5 +- 10 files changed, 316 insertions(+), 83 deletions(-) create mode 100644 packages/app-extrinsics/src/Signer/Unlock.js create mode 100644 packages/ui-keyring/src/save/accountCreate.js diff --git a/packages/app-accounts/src/Creator.js b/packages/app-accounts/src/Creator.js index 211e88fb97..c7b0827660 100644 --- a/packages/app-accounts/src/Creator.js +++ b/packages/app-accounts/src/Creator.js @@ -27,6 +27,7 @@ type Props = I18nProps & { type State = { address: string | null, + fieldName: string, isNameValid: boolean, isSeedValid: boolean, isPassValid: boolean, @@ -60,7 +61,7 @@ class Creator extends React.PureComponent { render (): React$Node { const { className, style, t } = this.props; - const { address, isNameValid, isPassValid, isPassVisible, isSeedValid, isValid, name, password, seed } = this.state; + const { address, fieldName, isNameValid, isPassValid, isPassVisible, isSeedValid, isValid, name, password, seed } = this.state; return (
{ })} @@ -92,6 +94,7 @@ class Creator extends React.PureComponent { })} @@ -105,6 +108,7 @@ class Creator extends React.PureComponent { { return { address, + fieldName: `field_${Date.now()}`, isNameValid: true, isPassValid: false, isPassVisible: false, @@ -205,12 +210,10 @@ class Creator extends React.PureComponent { onCommit = (): void => { const { onBack } = this.props; const { name, password, seed } = this.state; - const pair = keyring.addFromSeed( - formatSeed(seed), - { name } - ); - keyring.saveAccount(pair, password); + keyring.createAccount( + formatSeed(seed), password, { name } + ); onBack(); } diff --git a/packages/app-extrinsics/src/Signer/Decoded.js b/packages/app-extrinsics/src/Signer/Decoded.js index b05ab5124d..56beb57de7 100644 --- a/packages/app-extrinsics/src/Signer/Decoded.js +++ b/packages/app-extrinsics/src/Signer/Decoded.js @@ -6,9 +6,9 @@ import type { I18nProps } from '@polkadot/ui-react-app/types'; import type { QueueTx } from '../types'; +import BN from 'bn.js'; import React from 'react'; import { Trans } from 'react-i18next'; -import Modal from 'semantic-ui-react/dist/es/modules/Modal'; import IdentityIcon from '@polkadot/ui-react/IdentityIcon'; import u8aToHexShort from '@polkadot/util/u8a/toHexShort'; import addressEncode from '@polkadot/util-keyring/address/encode'; @@ -19,36 +19,34 @@ type Props = I18nProps & { value: QueueTx }; -function Decoded ({ className, style, t, value: { extrinsic: { name, section }, nonce, publicKey, value } }: Props): React$Node { +function Decoded ({ className, style, t, value: { extrinsic: { name, section }, nonce = new BN(0), publicKey, value } }: Props): React$Node { const from = addressEncode(publicKey); return ( - -
- -
-

- - You are about to sign a message from {from} calling {section}.{name} with an index of {nonce.toString()} - -

-

- {t('decoded.data', { - defaultValue: 'The encoded parameters contains the data' - })} -

-

- {u8aToHexShort(value, 512)} -

-
+
+

+ + You are about to sign a message from {from} calling {section}.{name} with an index of {nonce.toString()} + +

+

+ {t('decoded.data', { + defaultValue: 'The encoded parameters contains the data' + })} +

+

+ {u8aToHexShort(value, 512)} +

- + +
); } diff --git a/packages/app-extrinsics/src/Signer/Signer.css b/packages/app-extrinsics/src/Signer/Signer.css index b1e4d52c08..e7ad37d1b5 100644 --- a/packages/app-extrinsics/src/Signer/Signer.css +++ b/packages/app-extrinsics/src/Signer/Signer.css @@ -4,19 +4,22 @@ .extrinsics--Signer {} -.extrinsics--Signer-Extrinsic {} - -.extrinsics--Signer-Extrinsic > .body { +.extrinsics--Signer-Decoded, +.extrinsics--Signer-Unlock { align-content: center; display: flex; line-height: 1.5em; } -.extrinsics--Signer-Extrinsic > .body > .expanded { - padding-left: 1.5em; +.extrinsics--Signer-Unlock { + padding-top: 1.5em; } -.extrinsics--Signer-Extrinsic > .body > .expanded .code { +.extrinsics--Signer-Content .expanded { + padding-right: 2em; +} + +.extrinsics--Signer-Content .expanded .code { background: #f5f5f5; font-family: monospace; margin: 0 0.25em; diff --git a/packages/app-extrinsics/src/Signer/Unlock.js b/packages/app-extrinsics/src/Signer/Unlock.js new file mode 100644 index 0000000000..0297d42aeb --- /dev/null +++ b/packages/app-extrinsics/src/Signer/Unlock.js @@ -0,0 +1,108 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +import type { I18nProps } from '@polkadot/ui-react-app/types'; +import type { KeyringPair } from '@polkadot/util-keyring/types'; + +import React from 'react'; +import Button from 'semantic-ui-react/dist/es/elements/Button'; +import Input from 'semantic-ui-react/dist/es/elements/Input'; +import Label from 'semantic-ui-react/dist/es/elements/Label'; +import keyring from '@polkadot/ui-keyring/src'; + +import translate from '../translate'; + +type Props = I18nProps & { + error?: string, + onChange: (password: string) => void, + publicKey: Uint8Array, + password: string, + value: Uint8Array, +} + +type State = { + fieldName: string, + isError: boolean, + isLocked: boolean, + isVisible: boolean, + pair: KeyringPair +} + +class Unlock extends React.PureComponent { + constructor (props: Props) { + super(props); + + this.state = ({ + isVisible: false + }: $Shape); + } + + static getDerivedStateFromProps ({ error, password, value }: Props, { isVisible }: State): $Shape { + const pair = keyring.getPair(value); + const isLocked = !pair.hasSecretKey(); + + return { + fieldName: `pass_${Date.now()}`, + isError: !!error, + isLocked, + isVisible: password.length === 0 + ? false + : isVisible, + pair + }; + } + + render (): React$Node { + const { className, password, style, t } = this.props; + const { fieldName, isError, isLocked, isVisible } = this.state; + + if (!isLocked) { + return null; + } + + return ( +
+
+
+ + + +
+
+
+ ); + } + + // eslint-disable-next-line no-unused-vars + onChange = (event: SyntheticEvent<*>, { value }): void => { + this.props.onChange(value); + } + + togglePassword = (): void => { + this.setState({ + isVisible: !this.state.isVisible + }); + } +} + +export default translate(Unlock); diff --git a/packages/app-extrinsics/src/Signer/index.js b/packages/app-extrinsics/src/Signer/index.js index 39c6371e95..836a1ae0f9 100644 --- a/packages/app-extrinsics/src/Signer/index.js +++ b/packages/app-extrinsics/src/Signer/index.js @@ -12,10 +12,12 @@ import './Signer.css'; import React from 'react'; import Button from 'semantic-ui-react/dist/es/elements/Button'; import Modal from 'semantic-ui-react/dist/es/modules/Modal'; +import keyring from '@polkadot/ui-keyring/src'; import withApi from '@polkadot/ui-react-rx/with/api'; import translate from '../translate'; import Decoded from './Decoded'; +import Unlock from './Unlock'; import submitExtrinsic from './submit'; type Props = I18nProps & ApiProps & { @@ -23,51 +25,146 @@ type Props = I18nProps & ApiProps & { queue: Array }; -function Signer ({ api, className, onSetStatus, queue, style, t }: Props): React$Node { - const first = queue.find(({ status }) => status === 'queued'); +type UnlockI18n = { + key: string, + value: I18Next$Translate$Config +} + +type State = { + currentItem?: QueueTx, + password: string, + unlockError: UnlockI18n | null +}; + +class Signer extends React.PureComponent { + constructor (props: Props) { + super(props); + + this.state = { + password: '', + unlockError: null + }; + } + + static getDerivedStateFromProps ({ queue }: Props, { currentItem, password, unlockError }: State): $Shape { + const nextItem = queue.find(({ status }) => + status === 'queued' + ); + const isSame = + !!nextItem && + !!currentItem && + nextItem.publicKey.toString() === currentItem.publicKey.toString(); + + return { + currentItem: nextItem, + password: isSame ? password : '', + unlockError: isSame ? unlockError : null + }; + } + + render (): React$Node { + const { className, style, t } = this.props; + const { currentItem, password, unlockError } = this.state; + + if (!currentItem) { + return null; + } + + return ( + + + + + + + + + + + ); + } + + unlockAccount (publicKey: Uint8Array, password?: string): ?UnlockI18n { + const pair = keyring.getPair(publicKey); + + if (pair.hasSecretKey()) { + return null; + } + + try { + // $FlowFixMe typo in underlying type, fixed at base (upgrades) + pair.decodePkcs8(void 0, password); + } catch (error) { + return { + key: 'signer.unlock.generic', + value: { + defaultValue: error.message + } + }; + } - if (!first) { return null; } - const onClose = (): void => - onSetStatus(first.id, 'cancelled'); - const onSign = async (): Promise => { - const status = await submitExtrinsic(api, first); + onChangePassword = (password: string): void => { + this.setState({ + password, + unlockError: null + }); + } - onSetStatus(first.id, status); + onCancel = (): void => { + const { onSetStatus } = this.props; + const { currentItem } = this.state; + + // This should never be executed + if (!currentItem) { + return; + } + + onSetStatus(currentItem.id, 'cancelled'); + } + + onSign = async (): Promise => { + const { api, onSetStatus } = this.props; + const { currentItem, password } = this.state; + + // This should never be executed + if (!currentItem) { + return; + } + + const unlockError = this.unlockAccount(currentItem.publicKey, password); + + if (unlockError) { + this.setState({ unlockError }); + return; + } + + onSetStatus(currentItem.id, 'sending'); + onSetStatus(currentItem.id, await submitExtrinsic(api, currentItem)); }; - - return ( - - - {t('signer.header', { - defaultValue: 'Sign and submit' - })} - - - - - - - - ); } export default translate( diff --git a/packages/app-extrinsics/src/index.js b/packages/app-extrinsics/src/index.js index e92cf823ce..e6c2671e62 100644 --- a/packages/app-extrinsics/src/index.js +++ b/packages/app-extrinsics/src/index.js @@ -61,11 +61,9 @@ export default class App extends React.PureComponent { }) ); - if (!['cancelled', 'error', 'sent'].includes(status)) { - return; + if (['cancelled', 'error', 'sent'].includes(status)) { + setTimeout(() => this.setStatus(id, 'completed'), 5000); } - - setTimeout(() => this.setStatus(id, 'completed'), 5000); } onQueue = (value: QueueTx): void => { diff --git a/packages/app-extrinsics/src/types.js b/packages/app-extrinsics/src/types.js index 3203b5d5aa..2f6cbbc16d 100644 --- a/packages/app-extrinsics/src/types.js +++ b/packages/app-extrinsics/src/types.js @@ -12,7 +12,7 @@ export type EncodedMessage = { value: Uint8Array }; -export type QueueTx$Status = 'cancelled' | 'completed' | 'error' | 'incomplete' | 'queued' | 'sent'; +export type QueueTx$Status = 'cancelled' | 'completed' | 'error' | 'incomplete' | 'queued' | 'sending' | 'sent'; export type QueueTx = EncodedMessage & { id: number, diff --git a/packages/ui-keyring/src/index.js b/packages/ui-keyring/src/index.js index 23bbe7fece..cd6ae6211e 100644 --- a/packages/ui-keyring/src/index.js +++ b/packages/ui-keyring/src/index.js @@ -9,6 +9,7 @@ import type { KeyringInstance, KeyringOption$Type, KeyringOption, KeyringOptions import testKeyring from '@polkadot/util-keyring/testing'; import loadAll from './loadAll'; +import createAccount from './save/accountCreate'; import saveAccount from './save/account'; import saveAccountMeta from './save/accountMeta'; import saveRecent from './save/recent'; @@ -25,11 +26,16 @@ const state: State = { loadAll(state); export default ({ - ...state.keyring, + getPair: (address: string | Uint8Array): KeyringPair => + state.keyring.getPair(address), + getPairs: (): Array => + state.keyring.getPairs(), getOptions: (type: KeyringOption$Type): KeyringOptions => state.options[type], loadAll: (): void => loadAll(state), + createAccount: (seed: Uint8Array, password?: string, meta?: KeyringPair$Meta): KeyringPair => + createAccount(state, seed, password, meta), saveAccount: (pair: KeyringPair, password?: string): void => saveAccount(state, pair, password), saveAccountMeta: (pair: KeyringPair, meta: KeyringPair$Meta): void => diff --git a/packages/ui-keyring/src/save/accountCreate.js b/packages/ui-keyring/src/save/accountCreate.js new file mode 100644 index 0000000000..fbe2c9d318 --- /dev/null +++ b/packages/ui-keyring/src/save/accountCreate.js @@ -0,0 +1,17 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +import type { KeyringPair, KeyringPair$Meta } from '@polkadot/util-keyring/types'; +import type { State } from '../types'; + +import saveAccount from './account'; + +export default function accountCreate (state: State, seed: Uint8Array, password?: string, meta?: KeyringPair$Meta = {}): KeyringPair { + const pair = state.keyring.addFromSeed(seed, meta); + + saveAccount(state, pair, password); + + return pair; +} diff --git a/packages/ui-keyring/src/types.js b/packages/ui-keyring/src/types.js index 5ef0fa41fb..0fbab40710 100644 --- a/packages/ui-keyring/src/types.js +++ b/packages/ui-keyring/src/types.js @@ -48,9 +48,12 @@ export type State = { } }; -export type KeyringInstance = BaseKeyringInstance & { +export type KeyringInstance = { getOptions: (type: KeyringOption$Type) => KeyringOptions, + getPair: (address: string | Uint8Array) => KeyringPair, + getPairs: () => Array, loadAll: () => void, + createAccount: (seed: Uint8Array, password?: string, meta?: KeyringPair$Meta) => KeyringPair, saveAccount: (pair: KeyringPair, password?: string) => void, saveAccountMeta: (pair: KeyringPair, meta: KeyringPair$Meta) => void, saveRecent: (address: string) => KeyringOption