ghost-portal/packages/react-components/src/util/toAddress.ts
Jaco 829830b83a
Adjust query type display (#6150)
* Adjust query type display

* linting

* Fix test page
2021-09-19 17:58:56 +02:00

29 lines
860 B
TypeScript

// Copyright 2017-2021 @polkadot/react-components authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { keyring } from '@polkadot/ui-keyring';
import { assert, hexToU8a, isHex } from '@polkadot/util';
import { ethereumEncode } from '@polkadot/util-crypto';
export function toAddress (value?: string | Uint8Array | null, allowIndices = false): 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 (u8a.length === 20) {
return ethereumEncode(u8a);
} else {
return keyring.encodeAddress(u8a);
}
} catch (error) {
// noop, undefined return indicates invalid/transient
}
}
return undefined;
}