diff --git a/packages/page-extrinsics/src/Decoded.tsx b/packages/page-extrinsics/src/Decoded.tsx new file mode 100644 index 0000000000..688747fd17 --- /dev/null +++ b/packages/page-extrinsics/src/Decoded.tsx @@ -0,0 +1,95 @@ +// Copyright 2017-2022 @polkadot/app-extrinsics authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { SubmittableExtrinsic } from '@polkadot/api/types'; +import type { Inspect } from '@polkadot/types/types'; + +import React, { useMemo } from 'react'; +import styled from 'styled-components'; + +import { Columar, Output } from '@polkadot/react-components'; +import { u8aToHex } from '@polkadot/util'; + +import DecodedInspect from './DecodedInspect'; +import { useTranslation } from './translate'; + +interface Props { + className?: string; + extrinsic?: SubmittableExtrinsic<'promise'> | null; + withData?: boolean; + withHash?: boolean; +} + +function extract (extrinsic?: SubmittableExtrinsic<'promise'> | null): [string, string, Inspect | null] { + if (!extrinsic) { + return ['0x', '0x', null]; + } + + const u8a = extrinsic.method.toU8a(); + + // don't use the built-in hash, we only want to convert once + return [ + u8aToHex(u8a), + extrinsic.registry.hash(u8a).toHex(), + // 0x410284009a81870b7ba4774b2a7efc31a635ec4042088d97ecbdaaf9552c3e21f74aae5201122fea94623d7aecda9870009506865a666d8a962aa2b58f12d5b512258f1a0aab569d7982bfd616c90d7ccf6960dadb29c5de46d2da73a159f5c6bbd5206f8b85029c00040300429599ba5f521844f2332524dec987f9cfb116a2570f83b2417184af0c74ab130700743ba40b + extrinsic.isSigned + ? extrinsic.inspect() + : extrinsic.method.inspect() + ]; +} + +function Decoded ({ className, extrinsic, withData = true, withHash = true }: Props): React.ReactElement | null { + const { t } = useTranslation(); + + const [hex, hash, inspect] = useMemo( + () => extract(extrinsic), + [extrinsic] + ); + + if (!inspect) { + return null; + } + + return ( + + + {withData && ( + ('encoded call data')} + value={hex} + withCopy + /> + )} + {withHash && ( + ('encoded call hash')} + value={hash} + withCopy + /> + )} + + + ('encoded call details')} + /> + + + ); +} + +export default React.memo(styled(Decoded)` + .ui--Column:last-child .ui--Labelled { + padding-left: 0.5rem; + + label { + left: 2.05rem; /* 3.55 - 1.5 (diff from padding above) */ + } + } +`); diff --git a/packages/page-extrinsics/src/DecodedInspect.tsx b/packages/page-extrinsics/src/DecodedInspect.tsx new file mode 100644 index 0000000000..ffe04efab8 --- /dev/null +++ b/packages/page-extrinsics/src/DecodedInspect.tsx @@ -0,0 +1,79 @@ +// Copyright 2017-2022 @polkadot/app-extrinsics authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Inspect } from '@polkadot/types/types'; + +import React, { useMemo } from 'react'; +import styled from 'styled-components'; + +import { Output } from '@polkadot/react-components'; +import { u8aToHex } from '@polkadot/util'; + +interface Props { + className?: string; + inspect?: Inspect | null; + label: React.ReactNode; +} + +interface Inspected { + name: string; + value: string; +} + +function formatInspect ({ inner, name = '', value }: Inspect, result: Inspected[] = []): Inspected[] { + if (value && value.length) { + result.push({ name, value: u8aToHex(value, undefined, false) }); + } + + for (let i = 0; i < inner.length; i++) { + formatInspect(inner[i], result); + } + + return result; +} + +function DecodedInspect ({ className, inspect, label }: Props): React.ReactElement | null { + const formatted = useMemo( + () => inspect && formatInspect(inspect), + [inspect] + ); + + if (!formatted) { + return null; + } + + return ( + + + + {formatted.map(({ name, value }, i) => ( + + + + + ))} + +
{name}{value}
+
+ ); +} + +export default React.memo(styled(DecodedInspect)` + table { + tr { + td:first-child { + color: var(--color-label); + padding-right: 0.5em; + text-align: right; + white-space: nowrap; + } + + td:last-child { + font: var(--font-mono); + } + } +`); diff --git a/packages/page-extrinsics/src/Decoder.tsx b/packages/page-extrinsics/src/Decoder.tsx index 148b520007..9e7bdc01df 100644 --- a/packages/page-extrinsics/src/Decoder.tsx +++ b/packages/page-extrinsics/src/Decoder.tsx @@ -5,12 +5,14 @@ import type { SubmittableExtrinsic, SubmittableExtrinsicFunction } from '@polkad import type { Call } from '@polkadot/types/interfaces'; import React, { useCallback, useState } from 'react'; +import styled from 'styled-components'; -import { Button, Call as CallDisplay, Input, InputAddress, InputExtrinsic, MarkError, Output, TxButton } from '@polkadot/react-components'; +import { Button, Call as CallDisplay, Input, InputAddress, InputExtrinsic, MarkError, TxButton } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; import { BalanceFree } from '@polkadot/react-query'; import { assert, isHex } from '@polkadot/util'; +import Decoded from './Decoded'; import { useTranslation } from './translate'; interface Props { @@ -18,27 +20,27 @@ interface Props { } interface ExtrinsicInfo { + decoded: SubmittableExtrinsic<'promise'> | null; extrinsic: SubmittableExtrinsic<'promise'> | null; extrinsicCall: Call | null; extrinsicError: string | null; extrinsicFn: SubmittableExtrinsicFunction<'promise'> | null; - extrinsicHash: string | null; extrinsicHex: string | null; } const DEFAULT_INFO: ExtrinsicInfo = { + decoded: null, extrinsic: null, extrinsicCall: null, extrinsicError: null, extrinsicFn: null, - extrinsicHash: null, extrinsicHex: null }; function Decoder ({ className }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); - const [{ extrinsic, extrinsicCall, extrinsicError, extrinsicFn, extrinsicHash }, setExtrinsicInfo] = useState(DEFAULT_INFO); + const [{ decoded, extrinsic, extrinsicCall, extrinsicError, extrinsicFn }, setExtrinsicInfo] = useState(DEFAULT_INFO); const [accountId, setAccountId] = useState(null); const _setExtrinsicHex = useCallback( @@ -47,21 +49,25 @@ function Decoder ({ className }: Props): React.ReactElement { assert(isHex(extrinsicHex), 'Expected a hex-encoded call'); let extrinsicCall: Call; + let decoded: SubmittableExtrinsic<'promise'> | null = null; try { // cater for an extrinsic input... - extrinsicCall = api.createType('Call', api.tx(extrinsicHex).method); + decoded = api.tx(extrinsicHex); + extrinsicCall = api.createType('Call', decoded.method); } catch (e) { extrinsicCall = api.createType('Call', extrinsicHex); } - const extrinsicHash = extrinsicCall.hash.toHex(); const { method, section } = api.registry.findMetaCall(extrinsicCall.callIndex); const extrinsicFn = api.tx[section][method]; - const extrinsic = extrinsicFn(...extrinsicCall.args); - setExtrinsicInfo({ ...DEFAULT_INFO, extrinsic, extrinsicCall, extrinsicFn, extrinsicHash, extrinsicHex }); + if (!decoded) { + decoded = extrinsic; + } + + setExtrinsicInfo({ ...DEFAULT_INFO, decoded, extrinsic, extrinsicCall, extrinsicFn, extrinsicHex }); } catch (e) { setExtrinsicInfo({ ...DEFAULT_INFO, extrinsicError: (e as Error).message }); } @@ -93,14 +99,10 @@ function Decoder ({ className }: Props): React.ReactElement { /> )} - {extrinsicHash && ( - - )} + ('using the selected account')} labelExtra={ @@ -131,4 +133,8 @@ function Decoder ({ className }: Props): React.ReactElement { ); } -export default React.memo(Decoder); +export default React.memo(styled(Decoder)` + .ui--Extrinsic--toplevel { + margin-top: 0; + } +`); diff --git a/packages/page-extrinsics/src/Submission.tsx b/packages/page-extrinsics/src/Submission.tsx index bed1fbd66a..011f1fb8a7 100644 --- a/packages/page-extrinsics/src/Submission.tsx +++ b/packages/page-extrinsics/src/Submission.tsx @@ -3,13 +3,13 @@ import type { SubmittableExtrinsic } from '@polkadot/api/types'; -import React, { useCallback, useMemo, useState } from 'react'; +import React, { useCallback, useState } from 'react'; -import { Button, Extrinsic, InputAddress, MarkError, Output, TxButton } from '@polkadot/react-components'; +import { Button, Extrinsic, InputAddress, MarkError, TxButton } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; import { BalanceFree } from '@polkadot/react-query'; -import { u8aToHex } from '@polkadot/util'; +import Decoded from './Decoded'; import { useTranslation } from './translate'; interface Props { @@ -33,20 +33,6 @@ function Selection ({ className }: Props): React.ReactElement { [] ); - const [extrinsicHex, extrinsicHash] = useMemo( - (): [string, string] => { - if (!extrinsic) { - return ['0x', '0x']; - } - - const u8a = extrinsic.method.toU8a(); - - // don't use the built-in hash, we only want to convert once - return [u8aToHex(u8a), extrinsic.registry.hash(u8a).toHex()]; - }, - [extrinsic] - ); - return (
{ onChange={_onExtrinsicChange} onError={_onExtrinsicError} /> - ('encoded call data')} - value={extrinsicHex} - withCopy - /> - ('encoded call hash')} - value={extrinsicHash} - withCopy - /> + {error && !extrinsic && ( )} diff --git a/packages/page-storage/src/Selection/Modules.tsx b/packages/page-storage/src/Selection/Modules.tsx index ca1cc444be..aede48534b 100644 --- a/packages/page-storage/src/Selection/Modules.tsx +++ b/packages/page-storage/src/Selection/Modules.tsx @@ -4,14 +4,14 @@ import type { QueryableStorageEntry } from '@polkadot/api/types'; import type { RawParams } from '@polkadot/react-params/types'; import type { StorageEntryTypeLatest } from '@polkadot/types/interfaces'; -import type { StorageEntry } from '@polkadot/types/primitive/types'; -import type { Registry, TypeDef } from '@polkadot/types/types'; +import type { Inspect, Registry, TypeDef } from '@polkadot/types/types'; import type { ComponentProps as Props } from '../types'; import React, { useCallback, useMemo, useState } from 'react'; import styled from 'styled-components'; import { ApiPromise } from '@polkadot/api'; +import DecodeInspect from '@polkadot/app-extrinsics/DecodedInspect'; import { Button, Columar, Input, InputStorage, Output } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; import Params from '@polkadot/react-params'; @@ -45,11 +45,6 @@ interface BlockHash { textHash: string; } -interface Inspect { - name: string; - value: string; -} - function areParamsValid ({ creator: { meta: { type } } }: QueryableStorageEntry<'promise'>, values: RawParams): boolean { return values.reduce((isValid: boolean, value) => isValid && @@ -59,17 +54,6 @@ function areParamsValid ({ creator: { meta: { type } } }: QueryableStorageEntry< (values.length === (type.isPlain ? 0 : type.asMap.hashers.length))); } -function getInspect (creator: StorageEntry, args: unknown[]): Inspect[] | null { - const { inner } = creator.inspect(...args); - - return inner.length - ? inner.map(({ name, value }) => ({ - name: name || '', - value: u8aToHex(value, undefined, false) - })) - : null; -} - function expandParams (registry: Registry, st: StorageEntryTypeLatest, isIterable: boolean): ParamsType { let types: string[] = []; @@ -150,7 +134,7 @@ function Modules ({ className = '', onAdd }: Props): React.ReactElement { ); const [isPartialKey, hexKey, inspect] = useMemo( - (): [boolean, string, Inspect[] | null] => { + (): [boolean, string, Inspect | null] => { if (isValid) { try { const [params] = extractParams(isIterable, values); @@ -165,7 +149,7 @@ function Modules ({ className = '', onAdd }: Props): React.ReactElement { : u8aToHex(compactStripLength(key.creator(...args))[1]); const inspect = isPartialKey ? null - : getInspect(key.creator, args); + : key.creator.inspect(...args); return [isPartialKey, hexKey, inspect]; } catch { @@ -268,24 +252,10 @@ function Modules ({ className = '', onAdd }: Props): React.ReactElement { /> - {inspect && ( - ('encoded key details')} - value={ - - - {inspect.map(({ name, value }, i) => ( - - - - - ))} - -
{name}{value}
- } - /> - )} + ('encoded key details')} + />
@@ -301,24 +271,11 @@ function Modules ({ className = '', onAdd }: Props): React.ReactElement { } export default React.memo(styled(Modules)` - .keyColumar .ui--Column:last-child .ui--Labelled { + .ui--Column:last-child .ui--Labelled { padding-left: 0.5rem; label { left: 2.05rem; /* 3.55 - 1.5 (diff from padding above) */ } } - - .keyTable { - tr { - td:first-child { - padding-right: 1em; - text-align: right; - } - - td:last-child { - font: var(--font-mono); - } - } - } `); diff --git a/packages/react-components/src/Output.tsx b/packages/react-components/src/Output.tsx index 0e82795af0..b9ef2b2128 100644 --- a/packages/react-components/src/Output.tsx +++ b/packages/react-components/src/Output.tsx @@ -37,7 +37,7 @@ function Output ({ children, className = '', help, isDisabled, isError, isFull, labelExtra={labelExtra} withLabel={withLabel} > -
+
{isTrimmed && value && (value.length > 256) ? `${value.substr(0, 96)}…${value.substr(-96)}` : value @@ -52,6 +52,12 @@ function Output ({ children, className = '', help, isDisabled, isError, isFull, } export default React.memo(styled(Output)` + .ui.selection.dropdown.ui--output.isDisabled { + background: transparent; + border-style: dashed; + opacity: 1; + } + pre { margin: 0; overflow: hidden;