mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-06-14 17:11:02 +00:00
Sort keyring address, remove expired recent (#115)
* Sort keyring address, remove expired recent * minor fixes
This commit is contained in:
@@ -16,6 +16,8 @@ import Base from './Base';
|
|||||||
import { accountKey, addressKey, accountRegex, addressRegex } from './defaults';
|
import { accountKey, addressKey, accountRegex, addressRegex } from './defaults';
|
||||||
import keyringOption from './options';
|
import keyringOption from './options';
|
||||||
|
|
||||||
|
const RECENT_EXPIRY = 24 * 60 * 60;
|
||||||
|
|
||||||
// No accounts (or test accounts) should be loaded until after the chain determination.
|
// 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
|
// Chain determination occurs outside of Keyring. Loading `keyring.loadAll({ type: 'ed25519' | 'sr25519' })` is triggered
|
||||||
// from the API after the chain is received
|
// 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) {
|
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(
|
const address = this.encodeAddress(
|
||||||
isHex(json.address)
|
isHex(json.address)
|
||||||
? hexToU8a(json.address)
|
? hexToU8a(json.address)
|
||||||
|
|||||||
@@ -13,6 +13,28 @@ import observableAll from '../observable';
|
|||||||
|
|
||||||
let hasCalledInitOptions = false;
|
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 {
|
class KeyringOption implements KeyringOptionInstance {
|
||||||
optionsSubject: BehaviorSubject<KeyringOptions> = new BehaviorSubject(this.emptyOptions());
|
optionsSubject: BehaviorSubject<KeyringOptions> = new BehaviorSubject(this.emptyOptions());
|
||||||
|
|
||||||
@@ -29,7 +51,7 @@ class KeyringOption implements KeyringOptionInstance {
|
|||||||
init (keyring: KeyringStruct): void {
|
init (keyring: KeyringStruct): void {
|
||||||
assert(!hasCalledInitOptions, 'Unable to initialise options more than once');
|
assert(!hasCalledInitOptions, 'Unable to initialise options more than once');
|
||||||
|
|
||||||
observableAll.subscribe((value) => {
|
observableAll.subscribe(() => {
|
||||||
const options = this.emptyOptions();
|
const options = this.emptyOptions();
|
||||||
|
|
||||||
this.addAccounts(keyring, options);
|
this.addAccounts(keyring, options);
|
||||||
@@ -72,10 +94,8 @@ class KeyringOption implements KeyringOptionInstance {
|
|||||||
const available = keyring.accounts.subject.getValue();
|
const available = keyring.accounts.subject.getValue();
|
||||||
|
|
||||||
Object
|
Object
|
||||||
.keys(available)
|
.values(available)
|
||||||
.map((address) =>
|
.sort(sortByName)
|
||||||
available[address]
|
|
||||||
)
|
|
||||||
.forEach(({ json: { meta: { isTesting = false } }, option }: SingleAddress) => {
|
.forEach(({ json: { meta: { isTesting = false } }, option }: SingleAddress) => {
|
||||||
if (!isTesting) {
|
if (!isTesting) {
|
||||||
options.account.push(option);
|
options.account.push(option);
|
||||||
@@ -89,16 +109,23 @@ class KeyringOption implements KeyringOptionInstance {
|
|||||||
const available = keyring.addresses.subject.getValue();
|
const available = keyring.addresses.subject.getValue();
|
||||||
|
|
||||||
Object
|
Object
|
||||||
.keys(available)
|
.values(available)
|
||||||
.map((address) =>
|
.filter(({ json }: SingleAddress) => {
|
||||||
available[address]
|
return json.meta.isRecent;
|
||||||
)
|
})
|
||||||
.forEach(({ json: { meta: { isRecent = false } }, option }: SingleAddress) => {
|
.sort(sortByCreated)
|
||||||
if (isRecent) {
|
.forEach(({ option }: SingleAddress) => {
|
||||||
options.recent.push(option);
|
options.recent.push(option);
|
||||||
} else {
|
});
|
||||||
options.address.push(option);
|
|
||||||
}
|
Object
|
||||||
|
.values(available)
|
||||||
|
.filter(({ json }: SingleAddress) => {
|
||||||
|
return !json.meta.isRecent;
|
||||||
|
})
|
||||||
|
.sort(sortByName)
|
||||||
|
.forEach(({ option }: SingleAddress) => {
|
||||||
|
options.address.push(option);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user