From b5b66e5990e0ea1fe168913614eb3a47768dd008 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 20 Aug 2018 10:56:30 +0200 Subject: [PATCH] Store addresses via actual address (when PK pasted) (#239) * Store addresses via actual address (when PK pasted) * Reformat pk addresses (wrong) as encoded * Remove debug log --- packages/ui-app/src/InputAddress/index.tsx | 13 +++++++++++-- .../addressToAddress.ts => util/toAddress.ts} | 8 ++++++-- packages/ui-keyring/src/address/meta.ts | 2 +- packages/ui-keyring/src/loadAll.ts | 17 +++++++++++++++++ 4 files changed, 35 insertions(+), 5 deletions(-) rename packages/ui-app/src/{InputAddress/addressToAddress.ts => util/toAddress.ts} (64%) diff --git a/packages/ui-app/src/InputAddress/index.tsx b/packages/ui-app/src/InputAddress/index.tsx index e4165fffea..e70c719adb 100644 --- a/packages/ui-app/src/InputAddress/index.tsx +++ b/packages/ui-app/src/InputAddress/index.tsx @@ -12,11 +12,14 @@ import React from 'react'; import keyring from '@polkadot/ui-keyring/index'; import createOptionHeader from '@polkadot/ui-keyring/options/header'; import addressDecode from '@polkadot/util-keyring/address/decode'; +import addressEncode from '@polkadot/util-keyring/address/encode'; import makeOption from '@polkadot/ui-keyring/options/item'; +import isHex from '@polkadot/util/is/hex'; +import hexToU8a from '@polkadot/util/hex/toU8a'; import Dropdown from '../Dropdown'; import classes from '../util/classes'; -import addressToAddress from './addressToAddress'; +import addressToAddress from '../util/toAddress'; type Props = BareProps & { defaultValue?: string | Uint8Array | null, @@ -41,6 +44,10 @@ type State = { const RECENT_KEY = 'header-recent'; const transform = (value: string): Uint8Array => { + if (isHex(value)) { + return hexToU8a(value); + } + try { return addressDecode(value); } catch (error) { @@ -130,7 +137,9 @@ export default class InputAddress extends React.Component { } matches.push( - keyring.saveRecent(query) + keyring.saveRecent( + addressEncode(publicKey) + ) ); } } diff --git a/packages/ui-app/src/InputAddress/addressToAddress.ts b/packages/ui-app/src/util/toAddress.ts similarity index 64% rename from packages/ui-app/src/InputAddress/addressToAddress.ts rename to packages/ui-app/src/util/toAddress.ts index 0305561611..d61df9d6d1 100644 --- a/packages/ui-app/src/InputAddress/addressToAddress.ts +++ b/packages/ui-app/src/util/toAddress.ts @@ -4,15 +4,19 @@ import addressDecode from '@polkadot/util-keyring/address/decode'; import addressEncode from '@polkadot/util-keyring/address/encode'; +import isHex from '@polkadot/util/is/hex'; +import hexToU8a from '@polkadot/util/hex/toU8a'; -export default function addressToAddress (value?: string | Uint8Array): string | undefined { +export default function toAddress (value?: string | Uint8Array): string | undefined { if (!value) { return; } try { return addressEncode( - addressDecode(value) + isHex(value) + ? hexToU8a(value as string) + : addressDecode(value) ); } catch (error) { console.error('Unable to encode address', value); diff --git a/packages/ui-keyring/src/address/meta.ts b/packages/ui-keyring/src/address/meta.ts index 035328b6a8..7d30439ee7 100644 --- a/packages/ui-keyring/src/address/meta.ts +++ b/packages/ui-keyring/src/address/meta.ts @@ -3,7 +3,7 @@ // of the ISC license. See the LICENSE file for details. import { KeyringPair$Meta } from '@polkadot/util-keyring/types'; -import { KeyringJson, State } from '../types'; +import { State } from '../types'; import store from 'store'; diff --git a/packages/ui-keyring/src/loadAll.ts b/packages/ui-keyring/src/loadAll.ts index 6948dbede3..a70b7e2596 100644 --- a/packages/ui-keyring/src/loadAll.ts +++ b/packages/ui-keyring/src/loadAll.ts @@ -6,7 +6,11 @@ import { KeyringPair$Json } from '@polkadot/util-keyring/types'; import { State, KeyringJson } from './types'; import store from 'store'; +import addressEncode from '@polkadot/util-keyring/address/encode'; +import isHex from '@polkadot/util/is/hex'; +import hexToU8a from '@polkadot/util/hex/toU8a'; +import saveAddress from './address/meta'; import createOptions from './options'; import { accountRegex, addressRegex } from './defaults'; @@ -18,6 +22,19 @@ export default function loadAll (state: State): void { keyring.addFromJson(json as KeyringPair$Json); available.account[json.address] = json; } else if (addressRegex.test(key)) { + const address = isHex(json.address) + ? addressEncode(hexToU8a(json.address)) + : json.address; + + // NOTE This is a fix for an older version where publicKeys instead of addresses + // were saved. Here we clean the old and replace with a new address-specific key + if (address !== json.address) { + json.address = address; + + store.remove(key); + saveAddress(state, address, json.meta); + } + available.address[json.address] = json; } });