From 4c007e29eff229775cbd48139fc78c25b6cdf7b7 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 26 Aug 2020 19:37:10 +0200 Subject: [PATCH] No indices lookups anywhere, only on address book (#3531) * No indices lookups anywhere, only on address book * Linting --- .../src/InputAddress/index.tsx | 2 +- .../react-components/src/util/toAddress.ts | 30 +++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) 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; }