// Copyright 2017-2019 @polkadot/react-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 { SignerOptions } from '@polkadot/api/types'; import { SubmittableExtrinsic } from '@polkadot/api/promise/types'; import { ApiProps } from '@polkadot/react-api/types'; import { I18nProps, BareProps } from '@polkadot/react-components/types'; import { RpcMethod } from '@polkadot/jsonrpc/types'; import { KeyringPair } from '@polkadot/keyring/types'; import { SubjectInfo } from '@polkadot/ui-keyring/observable/types'; import { QueueTx, QueueTxMessageSetStatus, QueueTxResult, QueueTxStatus } from '@polkadot/react-components/Status/types'; import { SignerPayloadJSON } from '@polkadot/types/types'; import BN from 'bn.js'; import React from 'react'; import { SubmittableResult } from '@polkadot/api'; import { web3FromSource } from '@polkadot/extension-dapp'; import { createType } from '@polkadot/types'; import { Button, InputBalance, Modal } from '@polkadot/react-components'; import { withApi, withMulti, withObservable } from '@polkadot/react-api'; import keyring from '@polkadot/ui-keyring'; import { assert, isFunction } from '@polkadot/util'; import { format } from '@polkadot/util/logger'; import PasswordCheck from './PasswordCheck'; import Transaction from './Transaction'; import translate from './translate'; import Unlock from './Unlock'; interface BaseProps extends BareProps { queue: QueueTx[]; queueSetTxStatus: QueueTxMessageSetStatus; } interface Props extends I18nProps, ApiProps, BaseProps { allAccounts?: SubjectInfo; } interface State { currentItem?: QueueTx; isSendable: boolean; isV2?: boolean; password: string; tip?: BN; unlockError?: string | null; } class Signer extends React.PureComponent { public state: State = { isSendable: false, password: '', unlockError: null }; public static getDerivedStateFromProps ({ allAccounts, api, queue }: Props, { currentItem, password, unlockError }: State): State { let isV2: boolean; try { isV2 = !!api.tx.session.setKeys; } catch (e) { isV2 = false; } const nextItem = queue.find(({ status }): boolean => 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, isV2, password: isSame ? password : '', unlockError: isSame ? unlockError : null }; } public async componentDidUpdate (): Promise { const { currentItem } = this.state; if (currentItem && currentItem.status === 'queued' && !(currentItem.extrinsic || currentItem.payload)) { return this.sendRpc(currentItem); } } public render (): React.ReactNode { const { currentItem } = this.state; if (!currentItem) { return null; } return ( {this.renderContent()} {this.renderButtons()} ); } private renderButtons (): React.ReactNode { const { t } = this.props; const { currentItem, isSendable } = this.state; if (!currentItem) { return null; } return (