Alert for multisig approvals (#2953)
* Alert for multisig approvals * Trigger on multisig events * Allow display/submission of call data
This commit is contained in:
parent
2286d2863e
commit
fc5e75f01e
@ -5,7 +5,7 @@
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
import { DeriveBalancesAll, DeriveDemocracyLock } from '@polkadot/api-derive/types';
|
||||
import { ActionStatus } from '@polkadot/react-components/Status/types';
|
||||
import { H256, Multisig, RecoveryConfig } from '@polkadot/types/interfaces';
|
||||
import { RecoveryConfig } from '@polkadot/types/interfaces';
|
||||
import { SortedAccount } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
@ -14,8 +14,8 @@ import styled from 'styled-components';
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
import { getLedger } from '@polkadot/react-api';
|
||||
import { AddressInfo, AddressMini, AddressSmall, Badge, Button, ChainLock, CryptoType, Forget, Icon, IdentityIcon, LinkExternal, Menu, Popup, StatusContext, Tags } from '@polkadot/react-components';
|
||||
import { useAccountInfo, useApi, useCall, useIncrement, useToggle } from '@polkadot/react-hooks';
|
||||
import { Option, StorageKey } from '@polkadot/types';
|
||||
import { useAccountInfo, useApi, useCall, useToggle } from '@polkadot/react-hooks';
|
||||
import { Option } from '@polkadot/types';
|
||||
import keyring from '@polkadot/ui-keyring';
|
||||
import { formatBalance, formatNumber } from '@polkadot/util';
|
||||
|
||||
@ -28,6 +28,7 @@ import MultisigApprove from './modals/MultisigApprove';
|
||||
import RecoverAccount from './modals/RecoverAccount';
|
||||
import RecoverSetup from './modals/RecoverSetup';
|
||||
import Transfer from './modals/Transfer';
|
||||
import useMultisigApprovals from './useMultisigApprovals';
|
||||
|
||||
interface Props extends SortedAccount {
|
||||
className?: string;
|
||||
@ -65,19 +66,13 @@ function Account ({ account: { address, meta }, className = '', filter, isFavori
|
||||
const { t } = useTranslation();
|
||||
const { queueExtrinsic } = useContext(StatusContext);
|
||||
const api = useApi();
|
||||
const [multiInc, refreshMulti] = useIncrement();
|
||||
const bestNumber = useCall<BN>(api.api.derive.chain.bestNumber, []);
|
||||
const balancesAll = useCall<DeriveBalancesAll>(api.api.derive.balances.all, [address]);
|
||||
const democracyLocks = useCall<DeriveDemocracyLock[]>(api.api.derive.democracy?.locks, [address]);
|
||||
const recoveryInfo = useCall<RecoveryConfig | null>(api.api.query.recovery?.recoverable, [address], {
|
||||
transform: (opt: Option<RecoveryConfig>) => opt.unwrapOr(null)
|
||||
});
|
||||
const multiInfos = useCall<[H256, Multisig][]>(multiInc && (api.api.query.multisig || api.api.query.utility)?.multisigs.entries as any, [address], {
|
||||
transform: (infos: [StorageKey, Option<Multisig>][]): [H256, Multisig][] =>
|
||||
infos
|
||||
.filter(([, opt]) => opt.isSome)
|
||||
.map(([key, opt]) => [key.args[1] as H256, opt.unwrap()])
|
||||
});
|
||||
const multiInfos = useMultisigApprovals(address);
|
||||
const { flags: { isDevelopment, isExternal, isHardware, isInjected, isMultisig }, genesisHash, name: accName, onSetGenesisHash, tags } = useAccountInfo(address);
|
||||
const [{ democracyUnlockTx }, setUnlockableIds] = useState<DemocracyUnlockable>({ democracyUnlockTx: null, ids: [] });
|
||||
const [isVisible, setIsVisible] = useState(true);
|
||||
@ -149,14 +144,6 @@ function Account ({ account: { address, meta }, className = '', filter, isFavori
|
||||
[address, t]
|
||||
);
|
||||
|
||||
const _closeMultisig = useCallback(
|
||||
(): void => {
|
||||
toggleMultisig();
|
||||
refreshMulti();
|
||||
},
|
||||
[refreshMulti, toggleMultisig]
|
||||
);
|
||||
|
||||
const _clearDemocracyLocks = useCallback(
|
||||
(): void => {
|
||||
democracyUnlockTx && queueExtrinsic({
|
||||
@ -231,6 +218,15 @@ function Account ({ account: { address, meta }, className = '', filter, isFavori
|
||||
type='online'
|
||||
/>
|
||||
)}
|
||||
{multiInfos && multiInfos.length !== 0 && (
|
||||
<Badge
|
||||
hover={t<string>('Multisig approvals pending')}
|
||||
info={multiInfos.length}
|
||||
isInline
|
||||
isTooltip
|
||||
type='brown'
|
||||
/>
|
||||
)}
|
||||
</td>
|
||||
<td className='address'>
|
||||
<AddressSmall value={address} />
|
||||
@ -281,7 +277,7 @@ function Account ({ account: { address, meta }, className = '', filter, isFavori
|
||||
<MultisigApprove
|
||||
address={address}
|
||||
key='multisig-approve'
|
||||
onClose={_closeMultisig}
|
||||
onClose={toggleMultisig}
|
||||
ongoing={multiInfos}
|
||||
threshold={meta.threshold as number}
|
||||
who={meta.who as string[]}
|
||||
|
||||
@ -2,12 +2,14 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AccountId, H256, Multisig } from '@polkadot/types/interfaces';
|
||||
import { AccountId, Call, H256, Multisig } from '@polkadot/types/interfaces';
|
||||
import { SubmittableExtrinsic } from '@polkadot/api/types';
|
||||
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { registry } from '@polkadot/react-api';
|
||||
import { Dropdown, InputAddress, Modal, TxButton } from '@polkadot/react-components';
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import { Dropdown, InputAddress, Modal, TxButton, Input } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi } from '@polkadot/react-hooks';
|
||||
import { assert, isHex } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../../translate';
|
||||
|
||||
@ -20,6 +22,11 @@ interface Props {
|
||||
who: string[];
|
||||
}
|
||||
|
||||
interface MultiInfo {
|
||||
isMultiCall: boolean;
|
||||
multisig: Multisig | null;
|
||||
}
|
||||
|
||||
interface Option {
|
||||
text: string;
|
||||
value: string;
|
||||
@ -28,12 +35,15 @@ interface Option {
|
||||
function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { allAccounts } = useAccounts();
|
||||
const [callData, setCallData] = useState<Call | null>(null);
|
||||
const [hash, setHash] = useState<string | null>(ongoing[0][0].toHex());
|
||||
const [multisig, setMultisig] = useState<[H256, Multisig] | null>(null);
|
||||
const [{ isMultiCall, multisig }, setMultisig] = useState<MultiInfo>({ isMultiCall: false, multisig: null });
|
||||
const [others, setOthers] = useState<AccountId[]>([]);
|
||||
const [signatory, setSignatory] = useState<string | null>(null);
|
||||
const [whoFilter, setWhoFilter] = useState<string[]>([]);
|
||||
const [type, setType] = useState<string | null>('aye');
|
||||
const [tx, setTx] = useState<SubmittableExtrinsic<'promise'> | null>(null);
|
||||
const calltypes = useMemo<Option[]>(
|
||||
() => [
|
||||
{ text: t<string>('Approve this call hash'), value: 'aye' },
|
||||
@ -46,12 +56,18 @@ function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }:
|
||||
[ongoing]
|
||||
);
|
||||
|
||||
// filter the current multisig by supplied hash
|
||||
useEffect((): void => {
|
||||
hash && setMultisig(
|
||||
ongoing.find(([h]) => h.eq(hash)) || null
|
||||
);
|
||||
}, [hash, ongoing]);
|
||||
const [, multisig] = ongoing.find(([h]) => h.eq(hash)) || [null, null];
|
||||
|
||||
setMultisig({
|
||||
isMultiCall: !!multisig && (multisig.approvals.length + 1) >= threshold,
|
||||
multisig
|
||||
});
|
||||
setCallData(null);
|
||||
}, [hash, ongoing, threshold]);
|
||||
|
||||
// the others are all the who elements, without the current signatory (re-encoded)
|
||||
useEffect((): void => {
|
||||
setOthers(
|
||||
who
|
||||
@ -60,11 +76,55 @@ function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }:
|
||||
);
|
||||
}, [signatory, who]);
|
||||
|
||||
// filter the who by those not approved yet that is an actual account we own
|
||||
useEffect((): void => {
|
||||
setWhoFilter(
|
||||
who.map((w) => registry.createType('AccountId', w).toString())
|
||||
who
|
||||
.map((w) => registry.createType('AccountId', w).toString())
|
||||
.filter((w) => multisig && !multisig.approvals.some((a) => a.eq(w)) && allAccounts.some((a) => a === w))
|
||||
);
|
||||
}, [who]);
|
||||
}, [allAccounts, multisig, who]);
|
||||
|
||||
// based on the type, multisig, others create the tx. This can be either an approval or final call
|
||||
useEffect((): void => {
|
||||
const multiMod = api.tx.multisig || api.tx.utility;
|
||||
|
||||
setTx(() =>
|
||||
hash && multisig
|
||||
? type === 'aye'
|
||||
? isMultiCall
|
||||
? callData
|
||||
? multiMod.asMulti.meta.args.length === 5
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
? multiMod.asMulti(threshold, others, multisig.when, callData, false)
|
||||
: multiMod.asMulti(threshold, others, multisig.when, callData)
|
||||
: null
|
||||
: multiMod.approveAsMulti(threshold, others, multisig.when, hash)
|
||||
: multiMod.cancelAsMulti(threshold, others, multisig.when, hash)
|
||||
: null
|
||||
);
|
||||
}, [api, callData, hash, isMultiCall, others, multisig, threshold, type]);
|
||||
|
||||
// when the actual call input changes, create a call and set it
|
||||
const _setCallData = useCallback(
|
||||
(callHex: string): void => {
|
||||
try {
|
||||
assert(isHex(callHex), 'Hex call data required');
|
||||
|
||||
const callData = registry.createType('Call', callHex);
|
||||
|
||||
setCallData(
|
||||
callData.hash.eq(hash)
|
||||
? callData
|
||||
: null
|
||||
);
|
||||
} catch (error) {
|
||||
setCallData(null);
|
||||
}
|
||||
},
|
||||
[hash]
|
||||
);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
@ -72,12 +132,6 @@ function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }:
|
||||
header={t<string>('Pending call hashes')}
|
||||
>
|
||||
<Modal.Content>
|
||||
<InputAddress
|
||||
filter={whoFilter}
|
||||
help={t<string>('The signatory to send the approval/cancel from')}
|
||||
label={t<string>('signatory')}
|
||||
onChange={setSignatory}
|
||||
/>
|
||||
<Dropdown
|
||||
help={t<string>('The call hashes that have not been executed as of yet.')}
|
||||
label={t<string>('pending hashes')}
|
||||
@ -85,23 +139,41 @@ function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }:
|
||||
options={hashes}
|
||||
value={hash}
|
||||
/>
|
||||
<Dropdown
|
||||
help={t<string>('Either approve or reject this call.')}
|
||||
label={t<string>('approval type')}
|
||||
onChange={setType}
|
||||
options={calltypes}
|
||||
value={type}
|
||||
/>
|
||||
{whoFilter.length !== 0 && (
|
||||
<>
|
||||
<InputAddress
|
||||
filter={whoFilter}
|
||||
help={t<string>('The signatory to send the approval/cancel from')}
|
||||
label={t<string>('signatory')}
|
||||
onChange={setSignatory}
|
||||
/>
|
||||
<Dropdown
|
||||
help={t<string>('Either approve or reject this call.')}
|
||||
label={t<string>('approval type')}
|
||||
onChange={setType}
|
||||
options={calltypes}
|
||||
value={type}
|
||||
/>
|
||||
{type === 'aye' && isMultiCall && (
|
||||
<Input
|
||||
autoFocus
|
||||
help={t('For final approvals, the actual full call data is required to execute the transaction')}
|
||||
isError={!callData}
|
||||
label={t('call data for final approval')}
|
||||
onChange={_setCallData}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</Modal.Content>
|
||||
<Modal.Actions onCancel={onClose}>
|
||||
<TxButton
|
||||
accountId={signatory}
|
||||
extrinsic={tx}
|
||||
icon={type === 'aye' ? 'check' : 'cancel'}
|
||||
isDisabled={!multisig}
|
||||
isDisabled={!tx || !whoFilter.length}
|
||||
label={type === 'aye' ? 'Approve' : 'Reject'}
|
||||
onStart={onClose}
|
||||
params={[threshold, others, multisig ? multisig[1].when : null, hash]}
|
||||
tx={`${api.tx.multisig ? 'multisig' : 'utility'}.${type === 'aye' ? 'approveAsMulti' : 'cancelAsMulti'}`}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
|
||||
51
packages/page-accounts/src/Accounts/useMultisigApprovals.ts
Normal file
51
packages/page-accounts/src/Accounts/useMultisigApprovals.ts
Normal file
@ -0,0 +1,51 @@
|
||||
// Copyright 2017-2020 @polkadot/app-accounts 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 { H256, Multisig } from '@polkadot/types/interfaces';
|
||||
|
||||
import { useContext, useEffect, useState } from 'react';
|
||||
import { useApi, useIncrement, useIsMountedRef } from '@polkadot/react-hooks';
|
||||
import { EventsContext } from '@polkadot/react-query';
|
||||
import { Option, StorageKey } from '@polkadot/types';
|
||||
|
||||
export default function useMultisigApprovals (address: string): [H256, Multisig][] {
|
||||
const events = useContext(EventsContext);
|
||||
const { api } = useApi();
|
||||
const [multiInfos, setMultiInfos] = useState<[H256, Multisig][]>([]);
|
||||
const [trigger, incTrigger] = useIncrement();
|
||||
const mountedRef = useIsMountedRef();
|
||||
|
||||
// increment the trigger by looking at all events
|
||||
// - filter the by multisig module (old utility is not supported)
|
||||
// - find anything data item where the type is AccountId
|
||||
// - increment the trigger when at least one matches our address
|
||||
useEffect((): void => {
|
||||
events
|
||||
.filter(({ record: { event: { section } } }) => section === 'multisig')
|
||||
.reduce((hasMultisig: boolean, { record: { event: { data } } }) =>
|
||||
data.reduce((hasMultisig: boolean, item) =>
|
||||
hasMultisig || (item.toRawType() === 'AccountId' && item.eq(address)),
|
||||
hasMultisig),
|
||||
false) && incTrigger();
|
||||
}, [address, events, incTrigger]);
|
||||
|
||||
// query all the entries for the multisig, extracting approvals with their hash
|
||||
useEffect((): void => {
|
||||
const multiModule = api.query.multisig || api.query.utility;
|
||||
|
||||
multiModule && multiModule.multisigs &&
|
||||
multiModule.multisigs
|
||||
.entries(address)
|
||||
.then((infos: [StorageKey, Option<Multisig>][]): void => {
|
||||
mountedRef.current && setMultiInfos(
|
||||
infos
|
||||
.filter(([, opt]) => opt.isSome)
|
||||
.map(([key, opt]) => [key.args[1] as H256, opt.unwrap()])
|
||||
);
|
||||
})
|
||||
.catch(console.error);
|
||||
}, [address, api, mountedRef, trigger]);
|
||||
|
||||
return multiInfos;
|
||||
}
|
||||
@ -15,25 +15,31 @@ interface Props extends BareProps {
|
||||
children?: React.ReactNode;
|
||||
help?: React.ReactNode;
|
||||
isError?: boolean;
|
||||
isFull?: boolean;
|
||||
isHidden?: boolean;
|
||||
isMonospace?: boolean;
|
||||
isTrimmed?: boolean;
|
||||
label?: React.ReactNode;
|
||||
value?: string;
|
||||
withCopy?: boolean;
|
||||
withLabel?: boolean;
|
||||
}
|
||||
|
||||
function Output ({ children, className = '', help, isError, isHidden, isMonospace, label, value, withCopy = false, withLabel }: Props): React.ReactElement<Props> {
|
||||
function Output ({ children, className = '', help, isError, isFull, isHidden, isMonospace, isTrimmed, label, value, withCopy = false, withLabel }: Props): React.ReactElement<Props> {
|
||||
return (
|
||||
<Labelled
|
||||
className={className}
|
||||
help={help}
|
||||
isFull={isFull}
|
||||
isHidden={isHidden}
|
||||
label={label}
|
||||
withLabel={withLabel}
|
||||
>
|
||||
<div className={classes('ui--output', isError && 'error', isMonospace && 'monospace')}>
|
||||
{value}
|
||||
{isTrimmed && value && (value.length > 256)
|
||||
? `${value.substr(0, 96)}…${value.substr(-96)}`
|
||||
: value
|
||||
}
|
||||
{children}
|
||||
{withCopy
|
||||
? (
|
||||
|
||||
@ -11,10 +11,9 @@ export default css`
|
||||
border: 1px dashed #eee;
|
||||
box-sizing: border-box;
|
||||
line-height: 1rem;
|
||||
margin: 0.25rem;
|
||||
padding: .75rem 1rem;
|
||||
position: relative;
|
||||
word-wrap: break-word;
|
||||
word-break: break-all;
|
||||
|
||||
&.error {
|
||||
background: #fff6f6;
|
||||
|
||||
@ -3,12 +3,16 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { useCallback, useState } from 'react';
|
||||
import useIsMountedRef from './useIsMountedRef';
|
||||
|
||||
export default function useIncrement (defaultValue = 1): [number, () => void, (value: number) => void] {
|
||||
const mountedRef = useIsMountedRef();
|
||||
const [value, setValue] = useState(defaultValue);
|
||||
const increment = useCallback(
|
||||
(): void => setValue((value: number) => ++value),
|
||||
[]
|
||||
(): void => {
|
||||
mountedRef.current && setValue((value: number) => ++value);
|
||||
},
|
||||
[mountedRef]
|
||||
);
|
||||
|
||||
return [value, increment, setValue];
|
||||
|
||||
@ -73,11 +73,20 @@ function SignFields ({ address, onChange, signedTx }: Props): React.ReactElement
|
||||
</Modal.Column>
|
||||
</Modal.Columns>
|
||||
{!!signedTx && (
|
||||
<Output
|
||||
label={t<string>('Signed transaction')}
|
||||
value={signedTx}
|
||||
withCopy
|
||||
/>
|
||||
<Modal.Columns>
|
||||
<Modal.Column>
|
||||
<Output
|
||||
isFull
|
||||
isTrimmed
|
||||
label={t<string>('Signed transaction')}
|
||||
value={signedTx}
|
||||
withCopy
|
||||
/>
|
||||
</Modal.Column>
|
||||
<Modal.Column>
|
||||
{t('The actual fully constructed signed output. This can be used for submission via other channels.')}
|
||||
</Modal.Column>
|
||||
</Modal.Columns>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
|
||||
@ -15,7 +15,7 @@ import styled from 'styled-components';
|
||||
import { ApiPromise } from '@polkadot/api';
|
||||
import { web3FromSource } from '@polkadot/extension-dapp';
|
||||
import { registry } from '@polkadot/react-api';
|
||||
import { Button, ErrorBoundary, Modal, StatusContext, Toggle } from '@polkadot/react-components';
|
||||
import { Button, ErrorBoundary, Modal, Output, StatusContext, Toggle } from '@polkadot/react-components';
|
||||
import { useApi, useToggle } from '@polkadot/react-hooks';
|
||||
import { Option } from '@polkadot/types';
|
||||
import keyring from '@polkadot/ui-keyring';
|
||||
@ -185,6 +185,7 @@ function TxSigned ({ className, currentItem, requestAddress }: Props): React.Rea
|
||||
const [senderInfo, setSenderInfo] = useState<AddressProxy>({ isMultiCall: false, multiRoot: null, proxyRoot: null, signAddress: requestAddress, signPassword: '' });
|
||||
const [signedOptions, setSignedOptions] = useState<Partial<SignerOptions>>({});
|
||||
const [signedTx, setSignedTx] = useState<string | null>(null);
|
||||
const [multiCall, setMultiCall] = useState<string | null>(null);
|
||||
const [tip, setTip] = useState(BN_ZERO);
|
||||
|
||||
useEffect((): void => {
|
||||
@ -192,6 +193,17 @@ function TxSigned ({ className, currentItem, requestAddress }: Props): React.Rea
|
||||
setPasswordError(null);
|
||||
}, [senderInfo]);
|
||||
|
||||
// when we are sending the hash only, get the wrapped call for display (proxies if required)
|
||||
useEffect((): void => {
|
||||
setMultiCall(
|
||||
currentItem.extrinsic && senderInfo.multiRoot
|
||||
? senderInfo.proxyRoot
|
||||
? api.tx.proxy.proxy(senderInfo.proxyRoot, null, currentItem.extrinsic).method.toHex()
|
||||
: currentItem.extrinsic.method.toHex()
|
||||
: null
|
||||
);
|
||||
}, [api, currentItem, senderInfo]);
|
||||
|
||||
const _addQrSignature = useCallback(
|
||||
({ signature }: { signature: string }) => qrResolve && qrResolve({
|
||||
id: ++qrId,
|
||||
@ -304,6 +316,22 @@ function TxSigned ({ className, currentItem, requestAddress }: Props): React.Rea
|
||||
signedTx={signedTx}
|
||||
/>
|
||||
)}
|
||||
{isSubmit && !senderInfo.isMultiCall && multiCall && (
|
||||
<Modal.Columns>
|
||||
<Modal.Column>
|
||||
<Output
|
||||
isFull
|
||||
isTrimmed
|
||||
label={t<string>('multisig call data')}
|
||||
value={multiCall}
|
||||
withCopy
|
||||
/>
|
||||
</Modal.Column>
|
||||
<Modal.Column>
|
||||
{t('The call data that can be supplied to a final call to multi approvals')}
|
||||
</Modal.Column>
|
||||
</Modal.Columns>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user