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
This commit is contained in:
Jaco Greeff 2018-08-20 10:56:30 +02:00 committed by GitHub
parent 2edae7f11b
commit b5b66e5990
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 5 deletions

View File

@ -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<Props, State> {
}
matches.push(
keyring.saveRecent(query)
keyring.saveRecent(
addressEncode(publicKey)
)
);
}
}

View File

@ -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);

View File

@ -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';

View File

@ -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;
}
});