diff --git a/packages/react-components/src/InputAddressSimple.tsx b/packages/react-components/src/InputAddressSimple.tsx index bb0cd7619f..cd7f0025a9 100644 --- a/packages/react-components/src/InputAddressSimple.tsx +++ b/packages/react-components/src/InputAddressSimple.tsx @@ -10,6 +10,7 @@ import { toAddress } from './util'; interface Props { autoFocus?: boolean; + bytesLength?: 20 | 32; children?: React.ReactNode; className?: string; defaultValue?: string | null; @@ -23,14 +24,15 @@ interface Props { onChange?: (address: string | null) => void; onEnter?: () => void; onEscape?: () => void; + placeholder?: string; } -function InputAddressSimple ({ autoFocus, children, className = '', defaultValue, forceIconType, help, isDisabled, isError, isFull, label, noConvert, onChange, onEnter, onEscape }: Props): React.ReactElement { +function InputAddressSimple ({ autoFocus, bytesLength, children, className = '', defaultValue, forceIconType, help, isDisabled, isError, isFull, label, noConvert, onChange, onEnter, onEscape, placeholder }: Props): React.ReactElement { const [address, setAddress] = useState(defaultValue || null); const _onChange = useCallback( (_address: string): void => { - const address = toAddress(_address) || null; + const address = toAddress(_address, undefined, bytesLength) || null; const output = noConvert ? address ? _address @@ -40,7 +42,7 @@ function InputAddressSimple ({ autoFocus, children, className = '', defaultValue setAddress(output); onChange && onChange(output); }, - [noConvert, onChange] + [bytesLength, noConvert, onChange] ); return ( @@ -56,6 +58,7 @@ function InputAddressSimple ({ autoFocus, children, className = '', defaultValue onChange={_onChange} onEnter={onEnter} onEscape={onEscape} + placeholder={placeholder} > {children} diff --git a/packages/react-components/src/util/toAddress.ts b/packages/react-components/src/util/toAddress.ts index 136caeca49..9f981f6cc6 100644 --- a/packages/react-components/src/util/toAddress.ts +++ b/packages/react-components/src/util/toAddress.ts @@ -2,17 +2,21 @@ // SPDX-License-Identifier: Apache-2.0 import { keyring } from '@polkadot/ui-keyring'; -import { assert, hexToU8a, isHex } from '@polkadot/util'; +import { hexToU8a, isHex } from '@polkadot/util'; import { ethereumEncode } from '@polkadot/util-crypto'; -export function toAddress (value?: string | Uint8Array | null, allowIndices = false): string | undefined { +export function toAddress (value?: string | Uint8Array | null, allowIndices = false, bytesLength?: 20 | 32): string | undefined { if (value) { try { const u8a = isHex(value) ? hexToU8a(value) : keyring.decodeAddress(value); - assert(allowIndices || u8a.length === 32 || u8a.length === 20, 'AccountIndex values not allowed'); + if (!allowIndices && u8a.length !== 32 && u8a.length !== 20) { + throw new Error('AccountIndex values not allowed'); + } else if (bytesLength && u8a.length !== bytesLength) { + throw new Error('Invalid key length'); + } if (u8a.length === 20) { return ethereumEncode(u8a); diff --git a/packages/react-params/src/Param/BasicAccountIdBase.tsx b/packages/react-params/src/Param/BasicAccountIdBase.tsx index f59d5e5dba..fb32a32a24 100644 --- a/packages/react-params/src/Param/BasicAccountIdBase.tsx +++ b/packages/react-params/src/Param/BasicAccountIdBase.tsx @@ -6,7 +6,6 @@ import type { Props as BaseProps } from '../types'; import React, { useCallback, useState } from 'react'; import { InputAddressSimple } from '@polkadot/react-components'; -import { useApi } from '@polkadot/react-hooks'; import { isEthereumAddress, validateAddress } from '@polkadot/util-crypto'; import Bare from './Bare'; @@ -34,7 +33,6 @@ function isValidAddress (value: string | null | undefined, isEthereum: boolean): } function BasicAccountIdBase (props: Props): React.ReactElement { - const { isEthereum } = useApi(); const { bytesLength, className = '', defaultValue: { value }, isDisabled, isError, isInOption, label, onChange, withLabel } = props; const [defaultValue] = useState(() => (value as string)?.toString()); @@ -50,6 +48,7 @@ function BasicAccountIdBase (props: Props): React.ReactElement { return ( { label={label} noConvert onChange={_onChange} - placeholder={isEthereum ? '5...' : '0x1...'} + placeholder={bytesLength === 20 ? '0x1...' : '5...'} withLabel={withLabel} /> diff --git a/packages/react-params/src/initValue.ts b/packages/react-params/src/initValue.ts index 430c7354eb..f8e5fd162f 100644 --- a/packages/react-params/src/initValue.ts +++ b/packages/react-params/src/initValue.ts @@ -96,6 +96,8 @@ export default function getInitValue (registry: Registry, def: TypeDef): unknown return ''; case 'AccountId': + case 'AccountId20': + case 'AccountId32': case 'AccountIdOf': case 'Address': case 'Call':