// Copyright 2017-2019 @polkadot/app-toolbox 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 { Button , Input, InputAddress, Output, Static } from '@polkadot/ui-app'; import { I18nProps as Props } from '@polkadot/ui-app/types'; import keyring from '@polkadot/ui-keyring'; import { KeyringPair } from '@polkadot/keyring/types'; import { hexToU8a, isHex, stringToU8a, u8aToHex } from '@polkadot/util'; import React from 'react'; import styled from 'styled-components'; import translate from './translate'; import Unlock from './Unlock'; type State = { currentPair: KeyringPair | null, data: string, isHexData: boolean, isLocked: boolean, isUnlockVisible: boolean, signature: string }; const Wrapper = styled.div` position: relative; width: 100%; height: 100%; .unlock-overlay { position: absolute; width: 100%; height: 100%; top:0; left:0; background-color: #0f0e0e7a; } .unlock-overlay-warning { display: flex; align-items: center; justify-content: center; height:100%; } .unlock-overlay-content { color:#fff; text-align:center; .ui--Button-Group { text-align: center; } } `; class Sign extends React.PureComponent { state: State; constructor (props: Props) { super(props); const pairs = keyring.getPairs(); const currentPair = pairs[0] || null; this.state = { currentPair, data: '', isHexData: false, isLocked: currentPair ? currentPair.isLocked() : false, isUnlockVisible: false, signature: '' }; } render () { const { isLocked } = this.state; return (
{this.renderAccount()} {this.renderInput()} {this.renderSignature()} {this.renderUnlock()}
); } renderAccount () { const { t } = this.props; return (
); } renderUnlockWarning () { const { t } = this.props; const { isLocked } = this.state; if (!isLocked) { return null; } return (
{t('You need to unlock this account to be able to sign data.')}
); } renderInput () { const { t } = this.props; const { data, isHexData } = this.state; return ( <>
); } renderSignature () { const { t } = this.props; const { signature } = this.state; return (
); } renderUnlock () { const { currentPair, isUnlockVisible } = this.state; if (!isUnlockVisible) { return null; } return ( ); } nextState = (newState: State): void => { this.setState( (prevState: State): State => { const { currentPair = prevState.currentPair, data = prevState.data, isHexData = prevState.isHexData, isUnlockVisible = prevState.isUnlockVisible } = newState; const isLocked = !currentPair || currentPair.isLocked(); let signature = ''; if (!isLocked && currentPair) { signature = u8aToHex( currentPair.sign( isHexData ? hexToU8a(data) : stringToU8a(data) ) ); } return { currentPair, data, isHexData, isLocked, isUnlockVisible, signature }; } ); } toggleUnlock = (): void => { const { isUnlockVisible } = this.state; this.nextState({ isUnlockVisible: !isUnlockVisible } as State); } onChangeAccount = (accountId: string): void => { const currentPair = keyring.getPair(accountId); this.nextState({ currentPair } as State); } onChangeData = (data: string): void => { const isHexData = isHex(data); this.nextState({ data, isHexData } as State); } } export default translate(Sign);