From 3a40a10420c4819a9468387e527e1f7f1d809f92 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 25 Jan 2021 11:08:48 +0100 Subject: [PATCH] Display extrinsic signature & type (#4489) --- packages/page-accounts/package.json | 2 +- .../page-explorer/src/BlockInfo/Extrinsic.tsx | 1 + packages/react-components/src/Call.tsx | 78 ++++++++++++++----- yarn.lock | 10 +-- 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/packages/page-accounts/package.json b/packages/page-accounts/package.json index 9dc9a6db34..03dddfb70a 100644 --- a/packages/page-accounts/package.json +++ b/packages/page-accounts/package.json @@ -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" }, diff --git a/packages/page-explorer/src/BlockInfo/Extrinsic.tsx b/packages/page-explorer/src/BlockInfo/Extrinsic.tsx index 73a17718e0..56b229ea74 100644 --- a/packages/page-explorer/src/BlockInfo/Extrinsic.tsx +++ b/packages/page-explorer/src/BlockInfo/Extrinsic.tsx @@ -70,6 +70,7 @@ function ExtrinsicDisplay ({ blockNumber, className = '', events, index, value } tip={value.tip?.toBn()} value={value} withHash + withSignature /> diff --git a/packages/react-components/src/Call.tsx b/packages/react-components/src/Call.tsx index d1b940d7d1..ac94001f32 100644 --- a/packages/react-components/src/Call.tsx +++ b/packages/react-components/src/Call.tsx @@ -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 { +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 { const { t } = useTranslation(); - const [{ hash, params, values }, setExtracted] = useState({ hash: null, params: [], values: [] }); + const [{ hash, params, signature, signatureType, values }, setExtracted] = useState({ 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 (
@@ -74,13 +103,22 @@ function Call ({ children, className = '', labelHash, mortality, onError, tip, v /> {children}
+ {signature && ( + ('signature {{type}}', { replace: { type: signatureType ? `(${signatureType})` : '' } })} + withCopy + > + {signature} + + )} {hash && ( ('extrinsic hash')} withCopy > - {hash.toHex()} + {hash} )} {mortality && ( diff --git a/yarn.lock b/yarn.lock index a7b9928232..e5c2328b5b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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