diff --git a/packages/react-components/src/InputAddress/index.tsx b/packages/react-components/src/InputAddress/index.tsx index e5f1f9d162..36b7081a86 100644 --- a/packages/react-components/src/InputAddress/index.tsx +++ b/packages/react-components/src/InputAddress/index.tsx @@ -125,7 +125,7 @@ class InputAddress extends React.PureComponent { return { lastValue: lastValue || getLastValue(type), value: Array.isArray(value) - ? value.map(addressToAddress) + ? value.map((v) => addressToAddress(v)) : (addressToAddress(value) || undefined) }; } catch (error) { diff --git a/packages/react-components/src/util/toAddress.ts b/packages/react-components/src/util/toAddress.ts index 5db13098ba..edd70f4c3c 100644 --- a/packages/react-components/src/util/toAddress.ts +++ b/packages/react-components/src/util/toAddress.ts @@ -3,24 +3,22 @@ // of the Apache-2.0 license. See the LICENSE file for details. import keyring from '@polkadot/ui-keyring'; -import { hexToU8a, isHex } from '@polkadot/util'; +import { hexToU8a, isHex, assert } from '@polkadot/util'; -export default function toAddress (value?: string | Uint8Array | null): string | undefined { - if (!value) { - return; - } - - let address: string | undefined; - - try { - address = keyring.encodeAddress( - isHex(value) +export default function toAddress (value?: string | Uint8Array | null, allowIndices = false): string | undefined { + if (value) { + try { + const u8a = isHex(value) ? hexToU8a(value) - : keyring.decodeAddress(value) - ); - } catch (error) { - console.error('Unable to encode address', value); + : keyring.decodeAddress(value); + + assert(allowIndices || u8a.length === 32, 'AccountIndex values not allowed'); + + return keyring.encodeAddress(u8a); + } catch (error) { + console.error('Unable to encode address', (error as Error).message, value); + } } - return address; + return undefined; }