Display extrinsic signature & type (#4489)

This commit is contained in:
Jaco Greeff 2021-01-25 11:08:48 +01:00 committed by GitHub
parent b2816b92a6
commit 3a40a10420
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 26 deletions

View File

@ -12,7 +12,7 @@
"@babel/runtime": "^7.12.5",
"@polkadot/phishing": "^0.4.9",
"@polkadot/react-components": "^0.76.2-30",
"@polkadot/vanitygen": "^0.25.2-3",
"@polkadot/vanitygen": "^0.26.1",
"detect-browser": "^5.2.0",
"file-saver": "^2.0.5"
},

View File

@ -70,6 +70,7 @@ function ExtrinsicDisplay ({ blockNumber, className = '', events, index, value }
tip={value.tip?.toBn()}
value={value}
withHash
withSignature
/>
</Expander>
</td>

View File

@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Hash } from '@polkadot/types/interfaces';
import type { ExtrinsicSignature } from '@polkadot/types/interfaces';
import type { Codec, IExtrinsic, IMethod, TypeDef } from '@polkadot/types/types';
import BN from 'bn.js';
@ -10,7 +10,7 @@ import styled from 'styled-components';
import Params from '@polkadot/react-params';
import { FormatBalance } from '@polkadot/react-query';
import { GenericCall, getTypeDef } from '@polkadot/types';
import { Enum, GenericCall, getTypeDef } from '@polkadot/types';
import Static from './Static';
import { useTranslation } from './translate';
@ -19,11 +19,13 @@ export interface Props {
children?: React.ReactNode;
className?: string;
labelHash?: React.ReactNode;
labelSignature?: React.ReactNode;
mortality?: string;
onError?: () => void;
value: IExtrinsic | IMethod;
withBorder?: boolean;
withHash?: boolean;
withSignature?: boolean;
tip?: BN;
}
@ -38,30 +40,57 @@ interface Value {
}
interface Extracted {
hash: Hash | null;
hash: string | null;
params: Param[];
signature: string | null;
signatureType: string | null;
values: Value[];
}
function Call ({ children, className = '', labelHash, mortality, onError, tip, value, withBorder, withHash }: Props): React.ReactElement<Props> {
function isExtrinsic (value: IExtrinsic | IMethod): value is IExtrinsic {
return !!(value as IExtrinsic).signature;
}
// This is no doubt NOT the way to do things - however there is no other option
function getRawSignature (value: IExtrinsic): ExtrinsicSignature | undefined {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return (value as any)._raw?.signature?.multiSignature as ExtrinsicSignature;
}
function extractState (value:IExtrinsic | IMethod, withHash?: boolean, withSignature?: boolean): Extracted {
const params = GenericCall.filterOrigin(value.meta).map(({ name, type }): Param => ({
name: name.toString(),
type: getTypeDef(type.toString())
}));
const values = value.args.map((value): Value => ({
isValid: true,
value
}));
const hash = withHash
? value.hash.toHex()
: null;
let signature: string | null = null;
let signatureType: string | null = null;
if (withSignature && isExtrinsic(value) && value.isSigned) {
const raw = getRawSignature(value);
signature = value.signature.toHex();
signatureType = raw instanceof Enum
? raw.type
: null;
}
return { hash, params, signature, signatureType, values };
}
function Call ({ children, className = '', labelHash, labelSignature, mortality, onError, tip, value, withBorder, withHash, withSignature }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [{ hash, params, values }, setExtracted] = useState<Extracted>({ hash: null, params: [], values: [] });
const [{ hash, params, signature, signatureType, values }, setExtracted] = useState<Extracted>({ hash: null, params: [], signature: null, signatureType: null, values: [] });
useEffect((): void => {
const params = GenericCall.filterOrigin(value.meta).map(({ name, type }): Param => ({
name: name.toString(),
type: getTypeDef(type.toString())
}));
const values = value.args.map((value): Value => ({
isValid: true,
value
}));
const hash = withHash
? value.hash
: null;
setExtracted({ hash, params, values });
}, [value, withHash]);
setExtracted(extractState(value, withHash, withSignature));
}, [value, withHash, withSignature]);
return (
<div className={`ui--Extrinsic ${className}`}>
@ -74,13 +103,22 @@ function Call ({ children, className = '', labelHash, mortality, onError, tip, v
/>
{children}
<div className='ui--Extrinsic--toplevel'>
{signature && (
<Static
className='hash'
label={labelSignature || t<string>('signature {{type}}', { replace: { type: signatureType ? `(${signatureType})` : '' } })}
withCopy
>
{signature}
</Static>
)}
{hash && (
<Static
className='hash'
label={labelHash || t<string>('extrinsic hash')}
withCopy
>
{hash.toHex()}
{hash}
</Static>
)}
{mortality && (

View File

@ -1968,7 +1968,7 @@ __metadata:
"@polkadot/phishing": ^0.4.9
"@polkadot/react-components": ^0.76.2-30
"@polkadot/test-support": 0.76.2-30
"@polkadot/vanitygen": ^0.25.2-3
"@polkadot/vanitygen": ^0.26.1
detect-browser: ^5.2.0
file-saver: ^2.0.5
languageName: unknown
@ -2765,9 +2765,9 @@ __metadata:
languageName: node
linkType: hard
"@polkadot/vanitygen@npm:^0.25.2-3":
version: 0.25.2-3
resolution: "@polkadot/vanitygen@npm:0.25.2-3"
"@polkadot/vanitygen@npm:^0.26.1":
version: 0.26.1
resolution: "@polkadot/vanitygen@npm:0.26.1"
dependencies:
"@babel/core": ^7.12.10
"@babel/register": ^7.12.10
@ -2778,7 +2778,7 @@ __metadata:
yargs: ^16.2.0
bin:
polkadot-js-vanitygen: index.js
checksum: c4a5c8500e4ce2ec80e1f3f0d0bcfa2b5fe49c7b85aa0a08a3b9f0f9eb1a8c523273747d4c46b852feb431f8ad6bfae4435be9e9e90fa11a91be7004837fe65f
checksum: b8729331cb931d8157c6b439a6bf3f3e008f5f0a383ec45fb02b828c0dc7f1ff6f0d529a0c1196087045ffe5671d7cb7bd42e7f06020c23cff96cd8e6ee2f161
languageName: node
linkType: hard