// Copyright 2017-2019 @polkadot/reactnative-identicon authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { Prefix } from '@polkadot/util-crypto/address/types'; import { Props as ComponentProps } from './types'; import React from 'react'; import { isHex, isU8a, u8aToHex } from '@polkadot/util'; import { decodeAddress, encodeAddress } from '@polkadot/util-crypto'; import { Empty, Polkadot } from './icons'; const Fallback = Polkadot; interface Props { prefix?: Prefix; size?: number; theme?: 'polkadot'; value?: string | Uint8Array | null; } interface State { address: string; publicKey: string; } const DEFAULT_SIZE = 64; const DEFAULT_THEME = 'polkadot'; const Components: Record> = { polkadot: Polkadot }; export default class IdentityIcon extends React.PureComponent { public state: State = { address: '', publicKey: '0x' }; private static prefix?: Prefix = undefined; public static setDefaultPrefix (prefix: Prefix): void { IdentityIcon.prefix = prefix; } public static getDerivedStateFromProps ({ prefix = IdentityIcon.prefix, value }: Props, prevState: State): State | null { try { const address = isU8a(value) || isHex(value) ? encodeAddress(value, prefix) : (value || ''); const publicKey = u8aToHex(decodeAddress(address, true, prefix)); return address === prevState.address ? null : { address, publicKey }; } catch (error) { return { address: '', publicKey: '0x' }; } } public render (): React.ReactNode { const { theme = DEFAULT_THEME, size = DEFAULT_SIZE } = this.props; const { address, publicKey } = this.state; const Component = !address ? Empty : Components[theme] || Fallback; return ( ); } }