Control priv access via # (#286)

This commit is contained in:
Jaco Greeff
2020-02-22 18:19:35 +01:00
committed by GitHub
parent 3b71f10d1e
commit e69e98182b
13 changed files with 332 additions and 378 deletions
+3 -3
View File
@@ -26,9 +26,9 @@
"store": "^2.0.12"
},
"devDependencies": {
"@polkadot/keyring": "^2.4.1",
"@polkadot/types": "^1.4.0-beta.9",
"@polkadot/util": "^2.4.1",
"@polkadot/keyring": "^2.5.1",
"@polkadot/types": "^1.4.0-beta.30",
"@polkadot/util": "^2.5.1",
"@types/ledgerhq__hw-transport-node-hid": "^4.21.1",
"@types/ledgerhq__hw-transport-u2f": "^4.21.1",
"@types/mkdirp": "^0.5.2",
+19 -19
View File
@@ -18,40 +18,40 @@ import BrowserStore from './stores/Browser'; // direct import (skip index with a
import { MAX_PASS_LEN } from './defaults';
export default class Base {
private _accounts: AddressSubject;
#accounts: AddressSubject;
private _addresses: AddressSubject;
#addresses: AddressSubject;
private _contracts: AddressSubject;
#contracts: AddressSubject;
private _keyring?: KeyringInstance;
#keyring?: KeyringInstance;
protected _store: KeyringStore;
protected _genesisHash?: string;
protected _store!: KeyringStore;
constructor () {
this._accounts = accounts;
this._addresses = addresses;
this._contracts = contracts;
this._keyring = undefined;
this.#accounts = accounts;
this.#addresses = addresses;
this.#contracts = contracts;
this._store = new BrowserStore();
}
public get accounts (): AddressSubject {
return this._accounts;
return this.#accounts;
}
public get addresses (): AddressSubject {
return this._addresses;
return this.#addresses;
}
public get contracts (): AddressSubject {
return this._contracts;
return this.#contracts;
}
public get keyring (): KeyringInstance {
if (this._keyring) {
return this._keyring;
if (this.#keyring) {
return this.#keyring;
}
throw new Error('Keyring should be initialised via \'loadAll\' before use');
@@ -95,8 +95,8 @@ export default class Base {
}
public setSS58Format (ss58Format?: Prefix): void {
if (this._keyring && ss58Format) {
this._keyring.setSS58Format(ss58Format);
if (this.#keyring && ss58Format) {
this.#keyring.setSS58Format(ss58Format);
}
}
@@ -111,9 +111,9 @@ export default class Base {
this.setDevMode(options.isDevelopment);
}
this._keyring = keyring;
this.#keyring = keyring;
this._genesisHash = options.genesisHash && options.genesisHash.toHex();
this._store = options.store || new BrowserStore();
this._store = options.store || this._store;
this.addAccountPairs();
}
+4 -4
View File
@@ -22,7 +22,7 @@ const RECENT_EXPIRY = 24 * 60 * 60;
// Chain determination occurs outside of Keyring. Loading `keyring.loadAll({ type: 'ed25519' | 'sr25519' })` is triggered
// from the API after the chain is received
export class Keyring extends Base implements KeyringStruct {
private stores = {
#stores = {
address: (): AddressSubject => this.addresses,
contract: (): AddressSubject => this.contracts,
account: (): AddressSubject => this.accounts
@@ -114,8 +114,8 @@ export class Keyring extends Base implements KeyringStruct {
: this.encodeAddress(_address);
const publicKey = this.decodeAddress(address);
const stores = type
? [this.stores[type]]
: Object.values(this.stores);
? [this.#stores[type]]
: Object.values(this.#stores);
const info = stores.reduce<SingleAddress | undefined>((lastInfo, store): SingleAddress | undefined =>
(store().subject.getValue()[address] || lastInfo), undefined);
@@ -319,7 +319,7 @@ export class Keyring extends Base implements KeyringStruct {
delete json.meta.isRecent;
this.stores[type]().add(this._store, address, json);
this.#stores[type]().add(this._store, address, json);
return json as KeyringPair$Json;
}
+4 -4
View File
@@ -10,19 +10,19 @@ import path from 'path';
// NOTE untested and unused by any known apps, probably broken in various mysterious ways
export default class FileStore implements KeyringStore {
private _path: string;
#path: string;
constructor (path: string) {
if (!fs.existsSync(path)) {
mkdirp.sync(path);
}
this._path = path;
this.#path = path;
}
public all (cb: (key: string, value: KeyringJson) => void): void {
fs
.readdirSync(this._path)
.readdirSync(this.#path)
.filter((key): boolean => !['.', '..'].includes(key))
.forEach((key): void => {
cb(key, this._readKey(key));
@@ -44,7 +44,7 @@ export default class FileStore implements KeyringStore {
}
private _getPath (key: string): string {
return path.join(this._path, key);
return path.join(this.#path, key);
}
private _readKey (key: string): KeyringJson {