Allow unlock on locked accounts (extrinsics) (#37)

This commit is contained in:
Jaco Greeff 2018-05-10 13:57:49 +02:00 committed by GitHub
parent 4ca16458f2
commit 38c02cd666
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 316 additions and 83 deletions

View File

@ -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<Props, State> {
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 (
<div
@ -74,6 +75,7 @@ class Creator extends React.PureComponent<Props, State> {
})}</Label>
<Input
error={!isSeedValid}
name={`${fieldName}_seed`}
onChange={this.onChangeSeed}
value={seed}
/>
@ -92,6 +94,7 @@ class Creator extends React.PureComponent<Props, State> {
})}</Label>
<Input
error={!isNameValid}
name={`${fieldName}_name`}
onChange={this.onChangeName}
value={name}
/>
@ -105,6 +108,7 @@ class Creator extends React.PureComponent<Props, State> {
<Input
action
error={!isPassValid}
name={`${fieldName}_pass`}
onChange={this.onChangePass}
type={isPassVisible ? 'text' : 'password'}
value={password}
@ -148,6 +152,7 @@ class Creator extends React.PureComponent<Props, State> {
return {
address,
fieldName: `field_${Date.now()}`,
isNameValid: true,
isPassValid: false,
isPassVisible: false,
@ -205,12 +210,10 @@ class Creator extends React.PureComponent<Props, State> {
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();
}

View File

@ -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 (
<Modal.Content
className={['extrinsics--Signer-Extrinsic', className].join(' ')}
<div
className={['extrinsics--Signer-Decoded', className].join(' ')}
style={style}
>
<div className='body'>
<IdentityIcon
className='icon'
value={from}
/>
<div className='expanded'>
<p>
<Trans i18nkey='decoded.short'>
You are about to sign a message from <span className='code'>{from}</span> calling <span className='code'>{section}.{name}</span> with an index of <span className='code'>{nonce.toString()}</span>
</Trans>
</p>
<p>
{t('decoded.data', {
defaultValue: 'The encoded parameters contains the data'
})}
</p>
<p className='code'>
{u8aToHexShort(value, 512)}
</p>
</div>
<div className='expanded'>
<p>
<Trans i18nkey='decoded.short'>
You are about to sign a message from <span className='code'>{from}</span> calling <span className='code'>{section}.{name}</span> with an index of <span className='code'>{nonce.toString()}</span>
</Trans>
</p>
<p>
{t('decoded.data', {
defaultValue: 'The encoded parameters contains the data'
})}
</p>
<p className='code'>
{u8aToHexShort(value, 512)}
</p>
</div>
</Modal.Content>
<IdentityIcon
className='icon'
value={from}
/>
</div>
);
}

View File

@ -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;

View File

@ -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<Props, State> {
constructor (props: Props) {
super(props);
this.state = ({
isVisible: false
}: $Shape<State>);
}
static getDerivedStateFromProps ({ error, password, value }: Props, { isVisible }: State): $Shape<State> {
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 (
<div
className={['extrinsics--Signer-Unlock', className].join(' ')}
style={style}
>
<div className='expanded ui--form'>
<div className='medium'>
<Label>{t('unlock.password', {
defaultValue: 'unlock account using'
})}</Label>
<Input
action
error={isError}
name={fieldName}
onChange={this.onChange}
type={isVisible ? 'text' : 'password'}
value={password}
>
<input />
<Button
icon='eye'
primary
onClick={this.togglePassword}
/>
</Input>
</div>
</div>
</div>
);
}
// 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);

View File

@ -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<QueueTx>
};
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<Props, State> {
constructor (props: Props) {
super(props);
this.state = {
password: '',
unlockError: null
};
}
static getDerivedStateFromProps ({ queue }: Props, { currentItem, password, unlockError }: State): $Shape<State> {
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 (
<Modal
className={['extrinsics--Signer', className].join(' ')}
dimmer='inverted'
open
style={style}
>
<Modal.Content className='extrinsics--Signer-Content'>
<Decoded value={currentItem} />
<Unlock
error={unlockError && t(unlockError.key, unlockError.value)}
onChange={this.onChangePassword}
password={password}
value={currentItem.publicKey}
/>
</Modal.Content>
<Modal.Actions>
<Button onClick={this.onCancel}>
{t('signer.cancel', {
defaultValue: 'Cancel'
})}
</Button>
<Button
onClick={this.onSign}
primary
>
{t('signer.send', {
defaultValue: 'Sign and Submit'
})}
</Button>
</Modal.Actions>
</Modal>
);
}
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<void> => {
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<void> => {
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 (
<Modal
className={['extrinsics--Signer', className].join(' ')}
dimmer='inverted'
open
style={style}
>
<Modal.Header>
{t('signer.header', {
defaultValue: 'Sign and submit'
})}
</Modal.Header>
<Decoded value={first} />
<Modal.Actions>
<Button onClick={onClose}>
{t('signer.cancel', {
defaultValue: 'Cancel'
})}
</Button>
<Button
onClick={onSign}
primary
>
{t('signer.send', {
defaultValue: 'Sign and Submit'
})}
</Button>
</Modal.Actions>
</Modal>
);
}
export default translate(

View File

@ -61,11 +61,9 @@ export default class App extends React.PureComponent<Props, State> {
})
);
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 => {

View File

@ -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,

View File

@ -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<KeyringPair> =>
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 =>

View File

@ -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;
}

View File

@ -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<KeyringPair>,
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