Session keys for 2.x (#1461)

This commit is contained in:
Jaco Greeff 2019-08-02 13:52:01 +02:00 committed by GitHub
parent 07e79097ff
commit c7b6e26e45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 26 deletions

View File

@ -21,7 +21,7 @@ interface State {
error: string | null;
}
class ValidateSession extends React.PureComponent<Props, State> {
class ValidateSessionEd25519 extends React.PureComponent<Props, State> {
public state: State = {
error: null
};
@ -68,4 +68,4 @@ class ValidateSession extends React.PureComponent<Props, State> {
}
}
export default translate(ValidateSession);
export default translate(ValidateSessionEd25519);

View File

@ -0,0 +1,71 @@
// Copyright 2017-2019 @polkadot/ui-staking 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 { I18nProps } from '@polkadot/ui-app/types';
import React from 'react';
import { Icon } from '@polkadot/ui-app';
import keyring from '@polkadot/ui-keyring';
import translate from '../../translate';
interface Props extends I18nProps {
controllerId: string;
onError: (error: string | null) => void;
sessionId: string;
stashId: string;
}
interface State {
error: string | null;
}
class ValidateSessionSr25519 extends React.PureComponent<Props, State> {
public state: State = {
error: null
};
public static getDerivedStateFromProps ({ onError, sessionId, stashId, t }: Props, prevState: State): State | null {
let error = null;
try {
const pair = keyring.getPair(sessionId);
if (pair.type !== 'sr25519') {
error = t('The selected account is not an sr25519 (Schnorrkel) account as required for validation');
} else if (sessionId === stashId) {
error = t('For fund security, your session key should not match your stash key.');
} else {
error = null;
}
} catch (e) {
// this _should_ never happen...
error = t('The account {{sessionId}} is not a valid account', { replace: { sessionId } });
}
if (error === prevState.error) {
return null;
}
onError(error);
return { error };
}
public render (): React.ReactNode {
const { error } = this.state;
if (!error) {
return null;
}
return (
<article className='warning'>
<div><Icon name='warning sign' />{error}</div>
</article>
);
}
}
export default translate(ValidateSessionSr25519);

View File

@ -9,7 +9,8 @@ import React from 'react';
import { Button, InputAddress, Modal, TxButton } from '@polkadot/ui-app';
import { withApi, withMulti } from '@polkadot/ui-api';
import ValidateSession from './InputValidationSession';
import ValidateSessionEd25519 from './InputValidationSessionEd25519';
import ValidateSessionSr25519 from './InputValidationSessionSr25519';
import translate from '../../translate';
type Props = I18nProps & ApiProps & {
@ -21,8 +22,10 @@ type Props = I18nProps & ApiProps & {
};
interface State {
sessionError: string | null;
sessionId: string;
ed25519: string;
ed25519Error: string | null;
sr25519: string;
sr25519Error: string | null;
}
class SetSessionKey extends React.PureComponent<Props, State> {
@ -32,20 +35,23 @@ class SetSessionKey extends React.PureComponent<Props, State> {
super(props);
this.state = {
sessionError: null,
sessionId: props.sessionId || props.controllerId
ed25519: props.sessionId || props.controllerId,
ed25519Error: null,
sr25519: '',
sr25519Error: null
};
}
public render (): React.ReactNode {
const { api, controllerId, isOpen, onClose, t } = this.props;
const { sessionError, sessionId } = this.state;
const isV2 = !!api.tx.session.setKeys;
const { controllerId, isOpen, isSubstrateV2, onClose, t } = this.props;
const { ed25519, ed25519Error, sr25519, sr25519Error } = this.state;
if (!isOpen) {
return null;
}
const hasError = !ed25519 || !!ed25519Error || (isSubstrateV2 ? (!sr25519 || !!sr25519Error) : false);
return (
<Modal
className='staking--SetSessionAccount'
@ -64,12 +70,20 @@ class SetSessionKey extends React.PureComponent<Props, State> {
<Button.Or />
<TxButton
accountId={controllerId}
isDisabled={!sessionId || !!sessionError}
isDisabled={hasError}
isPrimary
label={t('Set Session Key')}
onClick={ onClose }
params={ isV2 ? [{ auraKey: sessionId, grandpaKey: sessionId }, new Uint8Array([])] : [sessionId]}
tx={isV2 ? 'session.setKeys' : 'session.setKey'}
onClick={onClose}
params={
isSubstrateV2
? [{ ed25519, sr25519 }, new Uint8Array([])]
: [ed25519]
}
tx={
isSubstrateV2
? 'session.setKeys'
: 'session.setKey'
}
/>
</Button.Group>
</Modal.Actions>
@ -78,8 +92,8 @@ class SetSessionKey extends React.PureComponent<Props, State> {
}
private renderContent (): React.ReactNode {
const { controllerId, stashId, t } = this.props;
const { sessionId } = this.state;
const { controllerId, isSubstrateV2, stashId, t } = this.props;
const { ed25519, sr25519 } = this.state;
return (
<>
@ -96,28 +110,59 @@ class SetSessionKey extends React.PureComponent<Props, State> {
<InputAddress
className='medium'
help={t('Changing the key only takes effect at the start of the next session. If validating, it must be an ed25519 key.')}
label={t('session key')}
onChange={this.onChangeSession}
label={t('ed25519 key')}
onChange={this.onChangeEd25519}
type='account'
value={sessionId}
value={ed25519}
/>
<ValidateSession
<ValidateSessionEd25519
controllerId={controllerId}
onError={this.onSessionError}
sessionId={sessionId}
onError={this.onSessionErrorEd25519}
sessionId={ed25519}
stashId={stashId}
/>
{
isSubstrateV2
? (
<>
<InputAddress
className='medium'
help={t('Changing the key only takes effect at the start of the next session. If validating, it must be an sr25519 key.')}
label={t('sr25519 key')}
onChange={this.onChangeSr25519}
type='account'
value={sr25519}
/>
<ValidateSessionSr25519
controllerId={controllerId}
onError={this.onSessionErrorSr25519}
sessionId={sr25519}
stashId={stashId}
/>
</>
)
: null
}
</Modal.Content>
</>
);
}
private onChangeSession = (sessionId: string): void => {
this.setState({ sessionId });
private onChangeEd25519 = (ed25519: string): void => {
this.setState({ ed25519 });
}
private onSessionError = (sessionError: string | null): void => {
this.setState({ sessionError });
private onChangeSr25519 = (sr25519: string): void => {
this.setState({ sr25519 });
}
private onSessionErrorEd25519 = (ed25519Error: string | null): void => {
this.setState({ ed25519Error });
}
private onSessionErrorSr25519 = (sr25519Error: string | null): void => {
this.setState({ sr25519Error });
}
}