// 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 { I18nProps } from '@polkadot/react-components/types'; import { KeyringPair } from '@polkadot/keyring/types'; import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import { withMulti } from '@polkadot/react-api'; import { Button, Input, InputAddress, Output, Static } from '@polkadot/react-components'; import keyring from '@polkadot/ui-keyring'; import { hexToU8a, isHex, stringToU8a, u8aToHex } from '@polkadot/util'; import translate from './translate'; import Unlock from './Unlock'; interface Props extends I18nProps { className?: string; } interface State { currentPair: KeyringPair | null; data: string; isHexData: boolean; isLocked: boolean; isUnlockVisible: boolean; signature: string; } function Sign ({ className, t }: Props): React.ReactElement { const [state, setState] = useState({ currentPair: null, data: '', isHexData: false, isLocked: false, isUnlockVisible: false, signature: '' }); useEffect((): void => { const pairs = keyring.getPairs(); const currentPair = pairs[0] || null; setState({ currentPair, data: '', isHexData: false, isLocked: currentPair ? currentPair.isLocked : false, isUnlockVisible: false, signature: '' }); }, []); const _nextState = ({ currentPair = state.currentPair, data = state.data, isHexData = state.isHexData, isUnlockVisible = state.isUnlockVisible }: Partial): void => { const isLocked = !currentPair || currentPair.isLocked; let signature = ''; if (!isLocked && currentPair) { signature = u8aToHex( currentPair.sign( isHexData ? hexToU8a(data) : stringToU8a(data) ) ); } setState({ currentPair, data, isHexData, isLocked, isUnlockVisible, signature }); }; const _toggleUnlock = (): void => _nextState({ isUnlockVisible: !state.isUnlockVisible }); const _onChangeAccount = (accountId: string | null): void => _nextState({ currentPair: keyring.getPair(accountId || '') }); const _onChangeData = (data: string): void => _nextState({ data, isHexData: isHex(data) }); const { currentPair, data, isHexData, isLocked, isUnlockVisible, signature } = state; return (
{isUnlockVisible && ( )}
); } export default withMulti( styled(Sign)` .toolbox--Sign-input { 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; } } } `, translate );