Trap accounts not in keyring (#709)

* Trap accounts not in keyring

* Remove loose log

* Add isSendable prop

* Remove useless spec
This commit is contained in:
Jaco Greeff 2019-02-13 21:50:01 +01:00 committed by GitHub
parent 011e7711f1
commit 6fb30b9ae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 65 additions and 100 deletions

View File

@ -67,6 +67,7 @@ class Transfer extends React.PureComponent<Props, State> {
<Checks
accountId={accountId}
extrinsic={extrinsic}
isSendable
onChange={this.onChangeFees}
/>
<QueueConsumer>

View File

@ -36,6 +36,7 @@ type Props = I18nProps & {
balances_votingBalance?: DerivedBalances,
accountId?: string | null,
extrinsic?: Extrinsic | null,
isSendable: boolean,
onChange?: (hasAvailble: boolean) => void,
system_accountNonce?: BN
};
@ -117,7 +118,7 @@ class FeeDisplay extends React.PureComponent<Props, State> {
}
render () {
const { accountId, className, t } = this.props;
const { accountId, className, isSendable, t } = this.props;
const { allFees, allTotal, allWarn, hasAvailable, isRemovable, isReserved, overLimit } = this.state;
if (!accountId) {
@ -138,6 +139,11 @@ class FeeDisplay extends React.PureComponent<Props, State> {
className={[className, feeClass, 'padded'].join(' ')}
key='txinfo'
>
{
isSendable
? undefined
: <div><Icon name='ban' />{t('The selected account does not exist on your keyring')}</div>
}
{
hasAvailable
? undefined

View File

@ -1,81 +0,0 @@
// Copyright 2017-2019 @polkadot/ui-app authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import React from 'react';
import { shallow } from '../../../test/enzyme';
import { Signer } from './Modal';
const mockT = (key, options) => (key);
describe('Signer', () => {
let expectedNextCurrentItemState, fixtureCurrentItemState, fixtureQueueProp, unlockComponent, wrapper;
beforeEach(() => {
fixtureCurrentItemState = {
rpc: {
isSigned: true
},
id: 'test1',
publicKey: new Uint8Array([1]),
status: 'sending'
};
fixtureQueueProp = [{
rpc: {
isSigned: true
},
id: 'test2',
publicKey: new Uint8Array([2]),
status: 'queued'
}];
expectedNextCurrentItemState = fixtureQueueProp[0];
wrapper = shallow(
<Signer
t={mockT}
queue={[]}
/>, {}
);
});
it('creates the element', () => {
return new Promise((resolve) => {
wrapper.setState({
currentItem: fixtureCurrentItemState,
password: '123',
unlockError: null
}, () => {
wrapper.setProps({
queue: fixtureQueueProp
}, resolve);
});
}).then(() => {
wrapper.update();
expect(wrapper).toHaveLength(1);
});
});
// Reference: https://github.com/airbnb/enzyme/issues/1794
it('renders Unlock component with empty password', () => {
return new Promise((resolve) => {
wrapper.setState({
currentItem: fixtureCurrentItemState,
password: '123',
unlockError: null
}, () => {
wrapper.setProps({
queue: fixtureQueueProp
}, resolve);
});
}).then(() => {
wrapper.update();
expect(wrapper.state('currentItem')).toEqual(expectedNextCurrentItemState);
expect(wrapper.find('.ui--signer-Signer')).toHaveLength(1);
unlockComponent = wrapper.find('.ui--signer-Signer').children().at(0).childAt(0);
expect(unlockComponent.props().password).toEqual('');
});
});
});

View File

@ -8,13 +8,15 @@ import { ApiProps } from '@polkadot/ui-api/types';
import { I18nProps, BareProps } from '@polkadot/ui-app/types';
import { RpcMethod } from '@polkadot/jsonrpc/types';
import { KeyringPair } from '@polkadot/keyring/types';
import { SubjectInfo } from '@polkadot/ui-keyring/observable/types';
import { SignatureOptions } from '@polkadot/types/ExtrinsicSignature';
import { QueueTx, QueueTx$MessageSetStatus, QueueTx$Result, QueueTx$Status, SignerCallback } from '@polkadot/ui-app/Status/types';
import React from 'react';
import { Button, Modal } from '@polkadot/ui-app/index';
import keyring from '@polkadot/ui-keyring';
import { withApi, withMulti } from '@polkadot/ui-api/index';
import { withApi, withMulti, withObservable } from '@polkadot/ui-api/index';
import accountObservable from '@polkadot/ui-keyring/observable/accounts';
import { assert } from '@polkadot/util';
import { format } from '@polkadot/util/logger';
@ -27,7 +29,9 @@ type BaseProps = BareProps & {
queueSetTxStatus: QueueTx$MessageSetStatus
};
type Props = I18nProps & ApiProps & BaseProps;
type Props = I18nProps & ApiProps & BaseProps & {
allAccounts?: SubjectInfo
};
type UnlockI18n = {
key: string,
@ -36,17 +40,19 @@ type UnlockI18n = {
type State = {
currentItem?: QueueTx,
isSendable: boolean,
password: string,
unlockError: UnlockI18n | null
};
class Signer extends React.PureComponent<Props, State> {
state: State = {
isSendable: false,
password: '',
unlockError: null
};
static getDerivedStateFromProps ({ queue }: Props, { currentItem, password, unlockError }: State): State {
static getDerivedStateFromProps ({ allAccounts, queue }: Props, { currentItem, password, unlockError }: State): State {
const nextItem = queue.find(({ status }) =>
status === 'queued'
);
@ -60,8 +66,21 @@ class Signer extends React.PureComponent<Props, State> {
)
);
let isSendable = !!nextItem && !!nextItem.isUnsigned;
if (!isSendable && nextItem && nextItem.accountId && allAccounts) {
try {
const pair = keyring.getPair(nextItem.accountId);
isSendable = !!pair && !!allAccounts[nextItem.accountId];
} catch (error) {
// swallow
}
}
return {
currentItem: nextItem,
isSendable,
password: isSame ? password : '',
unlockError: isSame ? unlockError : null
};
@ -96,7 +115,7 @@ class Signer extends React.PureComponent<Props, State> {
private renderButtons () {
const { t } = this.props;
const { currentItem } = this.state;
const { currentItem, isSendable } = this.state;
if (!currentItem) {
return null;
@ -114,6 +133,7 @@ class Signer extends React.PureComponent<Props, State> {
<Button.Or />
<Button
className='ui--signer-Signer-Submit'
isDisabled={!isSendable}
isPrimary
onClick={this.onSend}
tabIndex={2}
@ -129,14 +149,17 @@ class Signer extends React.PureComponent<Props, State> {
}
private renderContent () {
const { currentItem } = this.state;
const { currentItem, isSendable } = this.state;
if (!currentItem) {
return null;
}
return (
<Transaction value={currentItem}>
<Transaction
isSendable={isSendable}
value={currentItem}
>
{this.renderUnlock()}
</Transaction>
);
@ -144,9 +167,9 @@ class Signer extends React.PureComponent<Props, State> {
private renderUnlock () {
const { t } = this.props;
const { currentItem, password, unlockError } = this.state;
const { currentItem, isSendable, password, unlockError } = this.state;
if (!currentItem || currentItem.isUnsigned) {
if (!isSendable || !currentItem || currentItem.isUnsigned) {
return null;
}
@ -168,9 +191,15 @@ class Signer extends React.PureComponent<Props, State> {
try {
publicKey = keyring.decodeAddress(accountId);
} catch (err) {
console.error(err);
return null;
} catch (error) {
console.error(error);
return {
key: 'signer.unlock.address',
value: {
defaultValue: 'unable to decode address'
}
};
}
const pair = keyring.getPair(publicKey);
@ -183,6 +212,7 @@ class Signer extends React.PureComponent<Props, State> {
pair.decodePkcs8(password);
} catch (error) {
console.error(error);
return {
key: 'signer.unlock.generic',
value: {
@ -340,5 +370,6 @@ export {
export default withMulti(
Signer,
translate,
withApi
withApi,
withObservable(accountObservable.subject, { propName: 'allAccounts' })
);

View File

@ -14,6 +14,7 @@ import translate from './translate';
type Props = I18nProps & {
children?: React.ReactNode,
isSendable: boolean,
value: QueueTx
};
@ -67,7 +68,7 @@ class Transaction extends React.PureComponent<Props> {
}
private renderChecks () {
const { value: { accountId, extrinsic, isUnsigned } } = this.props;
const { isSendable, value: { accountId, extrinsic, isUnsigned } } = this.props;
if (isUnsigned) {
return null;
@ -77,6 +78,7 @@ class Transaction extends React.PureComponent<Props> {
<Checks
accountId={accountId}
extrinsic={extrinsic}
isSendable={isSendable}
/>
);
}

View File

@ -14,7 +14,6 @@ import translate from './translate';
type Props = I18nProps & {
autoFocus?: boolean,
error?: string,
label?: string,
onChange: (password: string) => void,
onKeyDown?: (event: React.KeyboardEvent<Element>) => void,
password: string,
@ -29,10 +28,17 @@ type State = {
};
class Unlock extends React.PureComponent<Props, State> {
state: State = {} as State;
state: State = {
isLocked: false
} as State;
static getDerivedStateFromProps ({ error, value }: Props): State {
static getDerivedStateFromProps ({ error, value }: Props): State | null {
const pair = keyring.getPair(value as string);
if (!pair) {
return null;
}
const isLocked = pair.isLocked();
return {
@ -43,7 +49,7 @@ class Unlock extends React.PureComponent<Props, State> {
}
render () {
const { autoFocus, label, onChange, onKeyDown, password, t, tabIndex } = this.props;
const { autoFocus, onChange, onKeyDown, password, t, tabIndex } = this.props;
const { isError, isLocked } = this.state;
if (!isLocked) {
@ -55,7 +61,7 @@ class Unlock extends React.PureComponent<Props, State> {
<Password
autoFocus={autoFocus}
isError={isError}
label={label || t('unlock account with password')}
label={t('unlock account with password')}
onChange={onChange}
onKeyDown={onKeyDown}
tabIndex={tabIndex}