From 01e4c0e60fcf8e21c9ff8f772c75630ec90d8aa1 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 3 Oct 2019 09:38:58 +0200 Subject: [PATCH] Allow ss58 input on Bytes fields (#1691) * Allow ss58 input on Bytes fields * Remove stray debug logs --- packages/react-params/src/Param/BaseBytes.tsx | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/packages/react-params/src/Param/BaseBytes.tsx b/packages/react-params/src/Param/BaseBytes.tsx index 55ddadfd3d..628a893fd0 100644 --- a/packages/react-params/src/Param/BaseBytes.tsx +++ b/packages/react-params/src/Param/BaseBytes.tsx @@ -8,6 +8,7 @@ import React from 'react'; import { Compact } from '@polkadot/types'; import { Input } from '@polkadot/react-components'; import { hexToU8a, isHex, u8aToHex } from '@polkadot/util'; +import { decodeAddress } from '@polkadot/util-crypto'; import Bare from './Bare'; @@ -23,17 +24,27 @@ interface Props extends BaseProps { const defaultValidate = (): boolean => true; +function convertInput (value: string): [boolean, Uint8Array] { + // try hex conversion + try { + return [true, hexToU8a(value)]; + } catch (error) { + // we continue... + } + + // maybe it is an ss58? + try { + return [true, decodeAddress(value)]; + } catch (error) { + // we continue + } + + return [false, new Uint8Array([])]; +} + function onChange ({ asHex, length = -1, onChange, validate = defaultValidate, withLength }: Props): (_: string) => void { return function (hex: string): void { - let value: Uint8Array; - let isValid = true; - - try { - value = hexToU8a(hex); - } catch (error) { - value = new Uint8Array([]); - isValid = false; - } + let [isValid, value] = convertInput(hex); isValid = isValid && validate(value) && ( length !== -1