From c7b6e26e45b39b2daac4ae40d7124c8e90266e16 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 2 Aug 2019 13:52:01 +0200 Subject: [PATCH] Session keys for 2.x (#1461) --- ....tsx => InputValidationSessionEd25519.tsx} | 4 +- .../Account/InputValidationSessionSr25519.tsx | 71 ++++++++++++++ .../src/Actions/Account/SetSessionAccount.tsx | 93 ++++++++++++++----- 3 files changed, 142 insertions(+), 26 deletions(-) rename packages/app-staking/src/Actions/Account/{InputValidationSession.tsx => InputValidationSessionEd25519.tsx} (93%) create mode 100644 packages/app-staking/src/Actions/Account/InputValidationSessionSr25519.tsx diff --git a/packages/app-staking/src/Actions/Account/InputValidationSession.tsx b/packages/app-staking/src/Actions/Account/InputValidationSessionEd25519.tsx similarity index 93% rename from packages/app-staking/src/Actions/Account/InputValidationSession.tsx rename to packages/app-staking/src/Actions/Account/InputValidationSessionEd25519.tsx index 2fa62ff396..d9c4864573 100644 --- a/packages/app-staking/src/Actions/Account/InputValidationSession.tsx +++ b/packages/app-staking/src/Actions/Account/InputValidationSessionEd25519.tsx @@ -21,7 +21,7 @@ interface State { error: string | null; } -class ValidateSession extends React.PureComponent { +class ValidateSessionEd25519 extends React.PureComponent { public state: State = { error: null }; @@ -68,4 +68,4 @@ class ValidateSession extends React.PureComponent { } } -export default translate(ValidateSession); +export default translate(ValidateSessionEd25519); diff --git a/packages/app-staking/src/Actions/Account/InputValidationSessionSr25519.tsx b/packages/app-staking/src/Actions/Account/InputValidationSessionSr25519.tsx new file mode 100644 index 0000000000..03c6f34329 --- /dev/null +++ b/packages/app-staking/src/Actions/Account/InputValidationSessionSr25519.tsx @@ -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 { + 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 ( +
+
{error}
+
+ ); + } +} + +export default translate(ValidateSessionSr25519); diff --git a/packages/app-staking/src/Actions/Account/SetSessionAccount.tsx b/packages/app-staking/src/Actions/Account/SetSessionAccount.tsx index 5c6c3af93d..a4d47a40db 100644 --- a/packages/app-staking/src/Actions/Account/SetSessionAccount.tsx +++ b/packages/app-staking/src/Actions/Account/SetSessionAccount.tsx @@ -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 { @@ -32,20 +35,23 @@ class SetSessionKey extends React.PureComponent { 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 ( { @@ -78,8 +92,8 @@ class SetSessionKey extends React.PureComponent { } 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 { - + { + isSubstrateV2 + ? ( + <> + + + + ) + : null + } + ); } - 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 }); } }