// Copyright 2017-2019 @polkadot/ui-signer 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 { SubmittableResult } from '@polkadot/api/SubmittableExtrinsic'; import { SubmittableExtrinsic } from '@polkadot/api/promise/types'; 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 { QueueTx, QueueTx$MessageSetStatus, QueueTx$Result, QueueTx$Status } from '@polkadot/ui-app/Status/types'; import React from 'react'; import { web3FromSource } from '@polkadot/extension-dapp'; import { Button, Modal } from '@polkadot/ui-app'; import { withApi, withMulti, withObservable } from '@polkadot/ui-api'; import keyring from '@polkadot/ui-keyring'; import { assert, isFunction } from '@polkadot/util'; import { format } from '@polkadot/util/logger'; import Transaction from './Transaction'; import Unlock from './Unlock'; import translate from './translate'; type BaseProps = BareProps & { queue: Array, queueSetTxStatus: QueueTx$MessageSetStatus }; type Props = I18nProps & ApiProps & BaseProps & { allAccounts?: SubjectInfo }; type State = { currentItem?: QueueTx, isSendable: boolean, password: string, unlockError?: string | null }; class Signer extends React.PureComponent { state: State = { isSendable: false, password: '', unlockError: null }; static getDerivedStateFromProps ({ allAccounts, queue }: Props, { currentItem, password, unlockError }: State): State { const nextItem = queue.find(({ status }) => status === 'queued' ); const isSame = !!nextItem && !!currentItem && ( (!nextItem.accountId && !currentItem.accountId) || ( (nextItem.accountId && nextItem.accountId.toString()) === (currentItem.accountId && currentItem.accountId.toString()) ) ); 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 }; } async componentDidUpdate () { const { currentItem } = this.state; if (currentItem && currentItem.status === 'queued' && !currentItem.extrinsic) { return this.sendRpc(currentItem); } } render () { const { currentItem } = this.state; if (!currentItem) { return null; } return ( {this.renderContent()} {this.renderButtons()} ); } private renderButtons () { const { t } = this.props; const { currentItem, isSendable } = this.state; if (!currentItem) { return null; } return (