mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-05-31 18:01:04 +00:00
KeyringStore interface (#127)
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
import { Prefix } from '@polkadot/keyring/address/types';
|
||||
import { KeyringInstance, KeyringPair } from '@polkadot/keyring/types';
|
||||
import { AddressSubject } from './observable/types';
|
||||
import { KeyringOptions } from './types';
|
||||
import { KeyringOptions, KeyringStore } from './types';
|
||||
|
||||
import testKeyring from '@polkadot/keyring/testing';
|
||||
import { isBoolean, isString } from '@polkadot/util';
|
||||
@@ -13,6 +13,7 @@ import { isBoolean, isString } from '@polkadot/util';
|
||||
import accounts from './observable/accounts';
|
||||
import addresses from './observable/addresses';
|
||||
import env from './observable/development';
|
||||
import LocalStorageStore from './stores/LocalStorage';
|
||||
import { MAX_PASS_LEN } from './defaults';
|
||||
|
||||
export default class Base {
|
||||
@@ -20,11 +21,13 @@ export default class Base {
|
||||
private _addresses: AddressSubject;
|
||||
private _keyring?: KeyringInstance;
|
||||
private _prefix?: Prefix;
|
||||
protected _store: KeyringStore;
|
||||
|
||||
constructor () {
|
||||
this._accounts = accounts;
|
||||
this._addresses = addresses;
|
||||
this._keyring = undefined;
|
||||
this._store = null as any;
|
||||
}
|
||||
|
||||
get accounts () {
|
||||
@@ -91,6 +94,7 @@ export default class Base {
|
||||
}
|
||||
|
||||
this._keyring = keyring;
|
||||
this._store = options.store || new LocalStorageStore();
|
||||
|
||||
this.addAccountPairs();
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
import { SingleAddress } from './observable/types';
|
||||
import { CreateResult, KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
|
||||
|
||||
import store from 'store';
|
||||
import createPair from '@polkadot/keyring/pair';
|
||||
import { hexToU8a, isHex, isString, u8aToHex } from '@polkadot/util';
|
||||
|
||||
@@ -149,8 +148,8 @@ export class Keyring extends Base implements KeyringStruct {
|
||||
return;
|
||||
}
|
||||
|
||||
store.remove(key);
|
||||
store.set(creator(hexAddr), json);
|
||||
this._store.remove(key);
|
||||
this._store.set(creator(hexAddr), json);
|
||||
}
|
||||
|
||||
private loadAccount (json: KeyringJson, key: string) {
|
||||
@@ -170,7 +169,7 @@ export class Keyring extends Base implements KeyringStruct {
|
||||
const { isRecent, whenCreated = 0 } = json.meta;
|
||||
|
||||
if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) {
|
||||
store.remove(key);
|
||||
this._store.remove(key);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -189,7 +188,7 @@ export class Keyring extends Base implements KeyringStruct {
|
||||
loadAll (options: KeyringOptions): void {
|
||||
super.initKeyring(options);
|
||||
|
||||
store.each((json: KeyringJson, key: string) => {
|
||||
this._store.all((key: string, json: KeyringJson) => {
|
||||
if (accountRegex.test(key)) {
|
||||
this.loadAccount(json, key);
|
||||
} else if (addressRegex.test(key)) {
|
||||
@@ -233,12 +232,13 @@ export class Keyring extends Base implements KeyringStruct {
|
||||
|
||||
saveAccountMeta (pair: KeyringPair, meta: KeyringPair$Meta): void {
|
||||
const address = pair.address();
|
||||
const json = store.get(accountKey(address));
|
||||
|
||||
pair.setMeta(meta);
|
||||
json.meta = pair.getMeta();
|
||||
this._store.get(accountKey(address), (json: KeyringJson) => {
|
||||
pair.setMeta(meta);
|
||||
json.meta = pair.getMeta();
|
||||
|
||||
this.accounts.add(json.address, json);
|
||||
this.accounts.add(json.address, json);
|
||||
});
|
||||
}
|
||||
|
||||
saveAddress (address: string, meta: KeyringPair$Meta): KeyringPair$Json {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2019 @polkadot/ui-keyring authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { KeyringStore } from '../types';
|
||||
|
||||
import store from 'store';
|
||||
|
||||
export default class Store implements KeyringStore {
|
||||
all (cb: (key: string, value: any) => void): void {
|
||||
store.each((value: any, key: string) =>
|
||||
cb(key, value)
|
||||
);
|
||||
}
|
||||
|
||||
get (key: string, cb: (value: any) => void): void {
|
||||
cb(store.get(key));
|
||||
}
|
||||
|
||||
remove (key: string, cb?: () => void): void {
|
||||
store.remove(key);
|
||||
cb && cb();
|
||||
}
|
||||
|
||||
set (key: string, value: any, cb?: () => void): void {
|
||||
store.set(key, value);
|
||||
cb && cb();
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,16 @@ import { KeyringInstance as BaseKeyringInstance, KeyringPair, KeyringPair$Meta,
|
||||
import { KeypairType } from '@polkadot/util-crypto/types';
|
||||
import { AddressSubject, SingleAddress } from './observable/types';
|
||||
|
||||
export interface KeyringStore {
|
||||
all: (cb: (key: string, value: any) => void) => void;
|
||||
get: (key: string, cb: (value: any) => void) => void;
|
||||
remove: (key: string, cb?: () => void) => void;
|
||||
set: (key: string, value: any, cb?: () => void) => void;
|
||||
}
|
||||
|
||||
export type KeyringOptions = KeyringOptionsBase & {
|
||||
isDevelopment?: boolean
|
||||
isDevelopment?: boolean,
|
||||
store?: KeyringStore
|
||||
};
|
||||
|
||||
export type KeyringJson$Meta = {
|
||||
|
||||
Reference in New Issue
Block a user