From aabb3df18cb9bfea41f4bbedf978ed1acb77e995 Mon Sep 17 00:00:00 2001 From: kwingram25 Date: Fri, 12 Apr 2019 06:36:48 -0400 Subject: [PATCH] Sort keyring address, remove expired recent (#115) * Sort keyring address, remove expired recent * minor fixes --- packages/ui-keyring/src/Keyring.ts | 9 ++++ packages/ui-keyring/src/options/index.ts | 57 +++++++++++++++++------- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/packages/ui-keyring/src/Keyring.ts b/packages/ui-keyring/src/Keyring.ts index 56cc8120..c88deac0 100644 --- a/packages/ui-keyring/src/Keyring.ts +++ b/packages/ui-keyring/src/Keyring.ts @@ -16,6 +16,8 @@ import Base from './Base'; import { accountKey, addressKey, accountRegex, addressRegex } from './defaults'; import keyringOption from './options'; +const RECENT_EXPIRY = 24 * 60 * 60; + // No accounts (or test accounts) should be loaded until after the chain determination. // Chain determination occurs outside of Keyring. Loading `keyring.loadAll({ type: 'ed25519' | 'sr25519' })` is triggered // from the API after the chain is received @@ -165,6 +167,13 @@ export class Keyring extends Base implements KeyringStruct { } private loadAddress (json: KeyringJson, key: string) { + const { isRecent, whenCreated = 0 } = json.meta; + + if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) { + store.remove(key); + return; + } + const address = this.encodeAddress( isHex(json.address) ? hexToU8a(json.address) diff --git a/packages/ui-keyring/src/options/index.ts b/packages/ui-keyring/src/options/index.ts index d17f4af5..74e9758a 100644 --- a/packages/ui-keyring/src/options/index.ts +++ b/packages/ui-keyring/src/options/index.ts @@ -13,6 +13,28 @@ import observableAll from '../observable'; let hasCalledInitOptions = false; +const sortByName = (a: SingleAddress, b: SingleAddress) => { + const valueA = a.option.name; + const valueB = b.option.name; + + return valueA.localeCompare(valueB); +}; + +const sortByCreated = (a: SingleAddress, b: SingleAddress) => { + const valueA = a.json.meta.whenCreated || 0; + const valueB = b.json.meta.whenCreated || 0; + + if (valueA < valueB) { + return 1; + } + + if (valueA > valueB) { + return -1; + } + + return 0; +}; + class KeyringOption implements KeyringOptionInstance { optionsSubject: BehaviorSubject = new BehaviorSubject(this.emptyOptions()); @@ -29,7 +51,7 @@ class KeyringOption implements KeyringOptionInstance { init (keyring: KeyringStruct): void { assert(!hasCalledInitOptions, 'Unable to initialise options more than once'); - observableAll.subscribe((value) => { + observableAll.subscribe(() => { const options = this.emptyOptions(); this.addAccounts(keyring, options); @@ -72,10 +94,8 @@ class KeyringOption implements KeyringOptionInstance { const available = keyring.accounts.subject.getValue(); Object - .keys(available) - .map((address) => - available[address] - ) + .values(available) + .sort(sortByName) .forEach(({ json: { meta: { isTesting = false } }, option }: SingleAddress) => { if (!isTesting) { options.account.push(option); @@ -89,16 +109,23 @@ class KeyringOption implements KeyringOptionInstance { const available = keyring.addresses.subject.getValue(); Object - .keys(available) - .map((address) => - available[address] - ) - .forEach(({ json: { meta: { isRecent = false } }, option }: SingleAddress) => { - if (isRecent) { - options.recent.push(option); - } else { - options.address.push(option); - } + .values(available) + .filter(({ json }: SingleAddress) => { + return json.meta.isRecent; + }) + .sort(sortByCreated) + .forEach(({ option }: SingleAddress) => { + options.recent.push(option); + }); + + Object + .values(available) + .filter(({ json }: SingleAddress) => { + return !json.meta.isRecent; + }) + .sort(sortByName) + .forEach(({ option }: SingleAddress) => { + options.address.push(option); }); }