mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-07-25 09:25:56 +00:00
KeyringStore interface (#127)
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
import { Prefix } from '@polkadot/keyring/address/types';
|
import { Prefix } from '@polkadot/keyring/address/types';
|
||||||
import { KeyringInstance, KeyringPair } from '@polkadot/keyring/types';
|
import { KeyringInstance, KeyringPair } from '@polkadot/keyring/types';
|
||||||
import { AddressSubject } from './observable/types';
|
import { AddressSubject } from './observable/types';
|
||||||
import { KeyringOptions } from './types';
|
import { KeyringOptions, KeyringStore } from './types';
|
||||||
|
|
||||||
import testKeyring from '@polkadot/keyring/testing';
|
import testKeyring from '@polkadot/keyring/testing';
|
||||||
import { isBoolean, isString } from '@polkadot/util';
|
import { isBoolean, isString } from '@polkadot/util';
|
||||||
@@ -13,6 +13,7 @@ import { isBoolean, isString } from '@polkadot/util';
|
|||||||
import accounts from './observable/accounts';
|
import accounts from './observable/accounts';
|
||||||
import addresses from './observable/addresses';
|
import addresses from './observable/addresses';
|
||||||
import env from './observable/development';
|
import env from './observable/development';
|
||||||
|
import LocalStorageStore from './stores/LocalStorage';
|
||||||
import { MAX_PASS_LEN } from './defaults';
|
import { MAX_PASS_LEN } from './defaults';
|
||||||
|
|
||||||
export default class Base {
|
export default class Base {
|
||||||
@@ -20,11 +21,13 @@ export default class Base {
|
|||||||
private _addresses: AddressSubject;
|
private _addresses: AddressSubject;
|
||||||
private _keyring?: KeyringInstance;
|
private _keyring?: KeyringInstance;
|
||||||
private _prefix?: Prefix;
|
private _prefix?: Prefix;
|
||||||
|
protected _store: KeyringStore;
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
this._accounts = accounts;
|
this._accounts = accounts;
|
||||||
this._addresses = addresses;
|
this._addresses = addresses;
|
||||||
this._keyring = undefined;
|
this._keyring = undefined;
|
||||||
|
this._store = null as any;
|
||||||
}
|
}
|
||||||
|
|
||||||
get accounts () {
|
get accounts () {
|
||||||
@@ -91,6 +94,7 @@ export default class Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._keyring = keyring;
|
this._keyring = keyring;
|
||||||
|
this._store = options.store || new LocalStorageStore();
|
||||||
|
|
||||||
this.addAccountPairs();
|
this.addAccountPairs();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import { KeypairType } from '@polkadot/util-crypto/types';
|
|||||||
import { SingleAddress } from './observable/types';
|
import { SingleAddress } from './observable/types';
|
||||||
import { CreateResult, KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
|
import { CreateResult, KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
|
||||||
|
|
||||||
import store from 'store';
|
|
||||||
import createPair from '@polkadot/keyring/pair';
|
import createPair from '@polkadot/keyring/pair';
|
||||||
import { hexToU8a, isHex, isString, u8aToHex } from '@polkadot/util';
|
import { hexToU8a, isHex, isString, u8aToHex } from '@polkadot/util';
|
||||||
|
|
||||||
@@ -149,8 +148,8 @@ export class Keyring extends Base implements KeyringStruct {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
store.remove(key);
|
this._store.remove(key);
|
||||||
store.set(creator(hexAddr), json);
|
this._store.set(creator(hexAddr), json);
|
||||||
}
|
}
|
||||||
|
|
||||||
private loadAccount (json: KeyringJson, key: string) {
|
private loadAccount (json: KeyringJson, key: string) {
|
||||||
@@ -170,7 +169,7 @@ export class Keyring extends Base implements KeyringStruct {
|
|||||||
const { isRecent, whenCreated = 0 } = json.meta;
|
const { isRecent, whenCreated = 0 } = json.meta;
|
||||||
|
|
||||||
if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) {
|
if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) {
|
||||||
store.remove(key);
|
this._store.remove(key);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +188,7 @@ export class Keyring extends Base implements KeyringStruct {
|
|||||||
loadAll (options: KeyringOptions): void {
|
loadAll (options: KeyringOptions): void {
|
||||||
super.initKeyring(options);
|
super.initKeyring(options);
|
||||||
|
|
||||||
store.each((json: KeyringJson, key: string) => {
|
this._store.all((key: string, json: KeyringJson) => {
|
||||||
if (accountRegex.test(key)) {
|
if (accountRegex.test(key)) {
|
||||||
this.loadAccount(json, key);
|
this.loadAccount(json, key);
|
||||||
} else if (addressRegex.test(key)) {
|
} else if (addressRegex.test(key)) {
|
||||||
@@ -233,12 +232,13 @@ export class Keyring extends Base implements KeyringStruct {
|
|||||||
|
|
||||||
saveAccountMeta (pair: KeyringPair, meta: KeyringPair$Meta): void {
|
saveAccountMeta (pair: KeyringPair, meta: KeyringPair$Meta): void {
|
||||||
const address = pair.address();
|
const address = pair.address();
|
||||||
const json = store.get(accountKey(address));
|
|
||||||
|
|
||||||
pair.setMeta(meta);
|
this._store.get(accountKey(address), (json: KeyringJson) => {
|
||||||
json.meta = pair.getMeta();
|
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 {
|
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 { KeypairType } from '@polkadot/util-crypto/types';
|
||||||
import { AddressSubject, SingleAddress } from './observable/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 & {
|
export type KeyringOptions = KeyringOptionsBase & {
|
||||||
isDevelopment?: boolean
|
isDevelopment?: boolean,
|
||||||
|
store?: KeyringStore
|
||||||
};
|
};
|
||||||
|
|
||||||
export type KeyringJson$Meta = {
|
export type KeyringJson$Meta = {
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
"@babel/runtime": "^7.4.4"
|
"@babel/runtime": "^7.4.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@polkadot/types": "^0.79.0-beta.6"
|
"@polkadot/types": "^0.79.0-beta.31"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@polkadot/types": "*"
|
"@polkadot/types": "*"
|
||||||
|
|||||||
@@ -1854,10 +1854,10 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.56.tgz#ffd6e9c95704a7fb90b918193b9dc5c440114b27"
|
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.56.tgz#ffd6e9c95704a7fb90b918193b9dc5c440114b27"
|
||||||
integrity sha512-wnt4zXxZXyz6WaubTO/I+nUElwV2DogFzdl6CrKfVn2PTWp8uHN06W9s40FH57ORtmQfDr9rLRP8Nq+oIyElbg==
|
integrity sha512-wnt4zXxZXyz6WaubTO/I+nUElwV2DogFzdl6CrKfVn2PTWp8uHN06W9s40FH57ORtmQfDr9rLRP8Nq+oIyElbg==
|
||||||
|
|
||||||
"@polkadot/types@^0.79.0-beta.6":
|
"@polkadot/types@^0.79.0-beta.31":
|
||||||
version "0.79.0-beta.6"
|
version "0.79.0-beta.31"
|
||||||
resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-0.79.0-beta.6.tgz#cdbe76b88c3eed125091e23ee9663bea15a5a44a"
|
resolved "https://registry.yarnpkg.com/@polkadot/types/-/types-0.79.0-beta.31.tgz#3e66dec097653a17386f687a70a86bc2520894ea"
|
||||||
integrity sha512-1JK9IjuJcWjhzuIX/DrYkXhIDyo03R+xV60/8kzfdPz2OErnG4c1/KYFmfeScRqr9wKCR6BqRh3BGS6fo8oTGw==
|
integrity sha512-ibVRBIirFCUYSQsMFBLSNGl0hp86tWM2RXnx3MDT5WiX4qGUOEy5aKTFU+hCDC8NhHa5+oNw8EeIDZ3waRI/4g==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/runtime" "^7.4.4"
|
"@babel/runtime" "^7.4.4"
|
||||||
"@polkadot/keyring" "^0.91.0-beta.1"
|
"@polkadot/keyring" "^0.91.0-beta.1"
|
||||||
|
|||||||
Reference in New Issue
Block a user