Swap to eslint (#154)

* 311 problems (173 errors, 138 warnings)

* Make a start...

* swap to react config

* Literally a handful left

* Clean.

* any removal

* Use Record

* Adjust versions

* Update with latest eslint-standard ruleset

* Update defaults.ts
This commit is contained in:
Jaco Greeff
2019-07-12 22:01:19 +02:00
committed by GitHub
parent 7b6a18cbfb
commit fd67ecdf3a
46 changed files with 412 additions and 395 deletions
+1 -1
View File
@@ -20,7 +20,7 @@
},
"devDependencies": {
"@polkadot/keyring": "^0.94.0-beta.9",
"@polkadot/types": "^0.82.0-beta.48",
"@polkadot/types": "^0.82.0-beta.53",
"@polkadot/util": "^0.94.0-beta.9"
},
"peerDependencies": {
+28 -23
View File
@@ -19,34 +19,39 @@ import { MAX_PASS_LEN } from './defaults';
export default class Base {
private _accounts: AddressSubject;
private _addresses: AddressSubject;
private _contracts: AddressSubject;
private _keyring?: KeyringInstance;
private _prefix?: Prefix;
protected _genesisHash?: string;
protected _store: KeyringStore;
constructor () {
private _addresses: AddressSubject;
private _contracts: AddressSubject;
private _keyring?: KeyringInstance;
private _prefix?: Prefix;
protected _genesisHash?: string;
protected _store!: KeyringStore;
public constructor () {
this._accounts = accounts;
this._addresses = addresses;
this._contracts = contracts;
this._keyring = undefined;
this._store = null as any;
}
get accounts () {
public get accounts (): AddressSubject {
return this._accounts;
}
get addresses () {
public get addresses (): AddressSubject {
return this._addresses;
}
get contracts () {
public get contracts (): AddressSubject {
return this._contracts;
}
get keyring (): KeyringInstance {
public get keyring (): KeyringInstance {
if (this._keyring) {
return this._keyring;
}
@@ -54,29 +59,29 @@ export default class Base {
throw new Error(`Keyring should be initialised via 'loadAll' before use`);
}
get genesisHash () {
public get genesisHash (): string | undefined {
return this._genesisHash;
}
decodeAddress = (key: string | Uint8Array, ignoreChecksum?: boolean): Uint8Array => {
public decodeAddress = (key: string | Uint8Array, ignoreChecksum?: boolean): Uint8Array => {
return this.keyring.decodeAddress(key, ignoreChecksum);
}
encodeAddress = (key: string | Uint8Array): string => {
public encodeAddress = (key: string | Uint8Array): string => {
return this.keyring.encodeAddress(key);
}
getPair (address: string | Uint8Array): KeyringPair {
public getPair (address: string | Uint8Array): KeyringPair {
return this.keyring.getPair(address);
}
getPairs (): Array<KeyringPair> {
return this.keyring.getPairs().filter((pair: KeyringPair) =>
public getPairs (): KeyringPair[] {
return this.keyring.getPairs().filter((pair: KeyringPair): boolean =>
env.isDevelopment() || pair.meta.isTesting !== true
);
}
isAvailable (_address: Uint8Array | string): boolean {
public isAvailable (_address: Uint8Array | string): boolean {
const accountsValue = this.accounts.subject.getValue();
const addressesValue = this.addresses.subject.getValue();
const contractsValue = this.contracts.subject.getValue();
@@ -87,15 +92,15 @@ export default class Base {
return !accountsValue[address] && !addressesValue[address] && !contractsValue[address];
}
isPassValid (password: string): boolean {
public isPassValid (password: string): boolean {
return password.length > 0 && password.length <= MAX_PASS_LEN;
}
setAddressPrefix (prefix: number): void {
public setAddressPrefix (prefix: number): void {
this._prefix = prefix as Prefix;
}
setDevMode (isDevelopment: boolean): void {
public setDevMode (isDevelopment: boolean): void {
env.set(isDevelopment);
}
@@ -116,7 +121,7 @@ export default class Base {
protected addAccountPairs (): void {
this.keyring
.getPairs()
.forEach(({ address, meta }: KeyringPair) => {
.forEach(({ address, meta }: KeyringPair): void => {
this.accounts.add(this._store, address, { address, meta });
});
}
+45 -45
View File
@@ -4,7 +4,7 @@
import { KeyringPair, KeyringPair$Meta, KeyringPair$Json } from '@polkadot/keyring/types';
import { KeypairType } from '@polkadot/util-crypto/types';
import { SingleAddress } from './observable/types';
import { AddressSubject, SingleAddress } from './observable/types';
import { CreateResult, KeyringAddress, KeyringAddressType, KeyringItemType, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
import createPair from '@polkadot/keyring/pair';
@@ -21,13 +21,13 @@ 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 {
stores = {
address: () => this.addresses,
contract: () => this.contracts,
account: () => this.accounts
private stores = {
address: (): AddressSubject => this.addresses,
contract: (): AddressSubject => this.contracts,
account: (): AddressSubject => this.accounts
};
addExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): CreateResult {
public addExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): CreateResult {
const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true }, null);
const json = this.saveAccount(pair);
@@ -37,7 +37,7 @@ export class Keyring extends Base implements KeyringStruct {
};
}
addPair (pair: KeyringPair, password: string): CreateResult {
public addPair (pair: KeyringPair, password: string): CreateResult {
this.keyring.addPair(pair);
const json = this.saveAccount(pair, password);
@@ -47,7 +47,7 @@ export class Keyring extends Base implements KeyringStruct {
};
}
addUri (suri: string, password?: string, meta: KeyringPair$Meta = {}, type?: KeypairType): CreateResult {
public addUri (suri: string, password?: string, meta: KeyringPair$Meta = {}, type?: KeypairType): CreateResult {
const pair = this.keyring.addFromUri(suri, meta, type);
const json = this.saveAccount(pair, password);
@@ -57,7 +57,7 @@ export class Keyring extends Base implements KeyringStruct {
};
}
backupAccount (pair: KeyringPair, password: string): KeyringPair$Json {
public backupAccount (pair: KeyringPair, password: string): KeyringPair$Json {
if (!pair.isLocked) {
pair.lock();
}
@@ -67,11 +67,11 @@ export class Keyring extends Base implements KeyringStruct {
return pair.toJson(password);
}
createFromUri (suri: string, meta: KeyringPair$Meta = {}, type?: KeypairType): KeyringPair {
public createFromUri (suri: string, meta: KeyringPair$Meta = {}, type?: KeypairType): KeyringPair {
return this.keyring.createFromUri(suri, meta, type);
}
encryptAccount (pair: KeyringPair, password: string): void {
public encryptAccount (pair: KeyringPair, password: string): void {
const json = pair.toJson(password);
json.meta.whenEdited = Date.now();
@@ -80,33 +80,33 @@ export class Keyring extends Base implements KeyringStruct {
this.accounts.add(this._store, pair.address, json);
}
forgetAccount (address: string): void {
public forgetAccount (address: string): void {
this.keyring.removePair(address);
this.accounts.remove(this._store, address);
}
forgetAddress (address: string): void {
public forgetAddress (address: string): void {
this.addresses.remove(this._store, address);
}
forgetContract (address: string): void {
public forgetContract (address: string): void {
this.contracts.remove(this._store, address);
}
getAccount (address: string | Uint8Array): KeyringAddress | undefined {
public getAccount (address: string | Uint8Array): KeyringAddress | undefined {
return this.getAddress(address, 'account');
}
getAccounts (): Array<KeyringAddress> {
public getAccounts (): KeyringAddress[] {
const available = this.accounts.subject.getValue();
return Object
.keys(available)
.map((address) => this.getAddress(address, 'account') as KeyringAddress)
.filter((account) => env.isDevelopment() || account.meta.isTesting !== true);
.map((address): KeyringAddress => this.getAddress(address, 'account') as KeyringAddress)
.filter((account): boolean => env.isDevelopment() || account.meta.isTesting !== true);
}
getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress | undefined {
public getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress | undefined {
const address = isString(_address)
? _address
: this.encodeAddress(_address);
@@ -116,9 +116,8 @@ export class Keyring extends Base implements KeyringStruct {
? [this.stores[type]]
: Object.values(this.stores);
const info = stores.reduce<SingleAddress | undefined>((lastInfo, store) =>
(store().subject.getValue()[address] || lastInfo),
undefined);
const info = stores.reduce<SingleAddress | undefined>((lastInfo, store): SingleAddress | undefined =>
(store().subject.getValue()[address] || lastInfo), undefined);
return info && {
address,
@@ -127,31 +126,32 @@ export class Keyring extends Base implements KeyringStruct {
};
}
getAddresses (): Array<KeyringAddress> {
public getAddresses (): KeyringAddress[] {
const available = this.addresses.subject.getValue();
return Object
.keys(available)
.map((address) => this.getAddress(address) as KeyringAddress);
.map((address): KeyringAddress => this.getAddress(address) as KeyringAddress);
}
getContract (address: string | Uint8Array): KeyringAddress | undefined {
public getContract (address: string | Uint8Array): KeyringAddress | undefined {
return this.getAddress(address, 'contract');
}
getContracts (): Array<KeyringAddress> {
public getContracts (): KeyringAddress[] {
const available = this.contracts.subject.getValue();
return Object
.entries(available)
.filter(([, data]) => {
.filter(([, data]): boolean => {
const { json: { meta: { contract } } } = data;
return contract && contract.genesisHash === this.genesisHash;
return !!contract && contract.genesisHash === this.genesisHash;
})
.map(([address]) => this.getContract(address) as KeyringAddress);
.map(([address]): KeyringAddress => this.getContract(address) as KeyringAddress);
}
private rewriteKey (json: KeyringJson, key: string, hexAddr: string, creator: (addr: string) => string) {
private rewriteKey (json: KeyringJson, key: string, hexAddr: string, creator: (addr: string) => string): void {
if (hexAddr.substr(0, 2) === '0x') {
return;
}
@@ -160,7 +160,7 @@ export class Keyring extends Base implements KeyringStruct {
this._store.set(creator(hexAddr), json);
}
private loadAccount (json: KeyringJson, key: string) {
private loadAccount (json: KeyringJson, key: string): void {
if (!json.meta.isTesting && (json as KeyringPair$Json).encoded) {
// FIXME Just for the transition period (ignoreChecksum)
const pair = this.keyring.addFromJson(json as KeyringPair$Json, true);
@@ -173,7 +173,7 @@ export class Keyring extends Base implements KeyringStruct {
this.rewriteKey(json, key, hexAddr.trim(), accountKey);
}
private loadAddress (json: KeyringJson, key: string) {
private loadAddress (json: KeyringJson, key: string): void {
const { isRecent, whenCreated = 0 } = json.meta;
if (isRecent && (Date.now() - whenCreated) > RECENT_EXPIRY) {
@@ -193,7 +193,7 @@ export class Keyring extends Base implements KeyringStruct {
this.rewriteKey(json, key, hexAddr, addressKey);
}
private loadContract (json: KeyringJson, key: string) {
private loadContract (json: KeyringJson, key: string): void {
const address = this.encodeAddress(
this.decodeAddress(json.address)
);
@@ -203,7 +203,7 @@ export class Keyring extends Base implements KeyringStruct {
this.rewriteKey(json, key, hexAddr, contractKey);
}
private loadInjected (address: string, meta: KeyringJson$Meta) {
private loadInjected (address: string, meta: KeyringJson$Meta): void {
const json = {
address,
meta: {
@@ -216,10 +216,10 @@ export class Keyring extends Base implements KeyringStruct {
this.accounts.add(this._store, pair.address, json);
}
loadAll (options: KeyringOptions, injected: Array<{ address: string, meta: KeyringJson$Meta }> = []): void {
public loadAll (options: KeyringOptions, injected: { address: string; meta: KeyringJson$Meta }[] = []): void {
super.initKeyring(options);
this._store.all((key: string, json: KeyringJson) => {
this._store.all((key: string, json: KeyringJson): void => {
if (options.filter ? options.filter(json) : true) {
if (accountRegex.test(key)) {
this.loadAccount(json, key);
@@ -237,14 +237,14 @@ export class Keyring extends Base implements KeyringStruct {
}
});
injected.forEach(({ address, meta }) =>
injected.forEach(({ address, meta }): void =>
this.loadInjected(address, meta)
);
keyringOption.init(this);
}
restoreAccount (json: KeyringPair$Json, password: string): KeyringPair {
public restoreAccount (json: KeyringPair$Json, password: string): KeyringPair {
const type = Array.isArray(json.encoding.content) ? json.encoding.content[1] : 'ed25519';
const pair = createPair(
type,
@@ -264,7 +264,7 @@ export class Keyring extends Base implements KeyringStruct {
return pair;
}
saveAccount (pair: KeyringPair, password?: string): KeyringPair$Json {
public saveAccount (pair: KeyringPair, password?: string): KeyringPair$Json {
this.addTimestamp(pair);
const json = pair.toJson(password);
@@ -275,10 +275,10 @@ export class Keyring extends Base implements KeyringStruct {
return json;
}
saveAccountMeta (pair: KeyringPair, meta: KeyringPair$Meta): void {
public saveAccountMeta (pair: KeyringPair, meta: KeyringPair$Meta): void {
const address = pair.address;
this._store.get(accountKey(address), (json: KeyringJson) => {
this._store.get(accountKey(address), (json: KeyringJson): void => {
pair.setMeta(meta);
json.meta = pair.meta;
@@ -286,7 +286,7 @@ export class Keyring extends Base implements KeyringStruct {
});
}
saveAddress (address: string, meta: KeyringPair$Meta, type: KeyringAddressType = 'address'): KeyringPair$Json {
public saveAddress (address: string, meta: KeyringPair$Meta, type: KeyringAddressType = 'address'): KeyringPair$Json {
const available = this.addresses.subject.getValue();
const json = (available[address] && available[address].json) || {
@@ -297,7 +297,7 @@ export class Keyring extends Base implements KeyringStruct {
}
};
Object.keys(meta).forEach((key) => {
Object.keys(meta).forEach((key): void => {
json.meta[key] = meta[key];
});
@@ -308,11 +308,11 @@ export class Keyring extends Base implements KeyringStruct {
return json as KeyringPair$Json;
}
saveContract (address: string, meta: KeyringPair$Meta): KeyringPair$Json {
public saveContract (address: string, meta: KeyringPair$Meta): KeyringPair$Json {
return this.saveAddress(address, meta, 'contract');
}
saveRecent (address: string): SingleAddress {
public saveRecent (address: string): SingleAddress {
const available = this.addresses.subject.getValue();
if (!available[address]) {
@@ -10,20 +10,19 @@ import { BehaviorSubject } from 'rxjs';
import createOptionItem from '../options/item';
import development from './development';
function callNext (current: SubjectInfo, subject: BehaviorSubject<any>, withTest: boolean) {
function callNext (current: SubjectInfo, subject: BehaviorSubject<SubjectInfo>, withTest: boolean): void {
const isDevMode = development.isDevelopment();
const filtered: SubjectInfo = {};
subject.next(
Object.keys(current).reduce((filtered, key) => {
const { json: { meta: { isTesting = false } = {} } = {} } = current[key];
Object.keys(current).forEach((key): void => {
const { json: { meta: { isTesting = false } = {} } = {} } = current[key];
if (!withTest || isDevMode || isTesting !== true) {
filtered[key] = current[key];
}
if (!withTest || isDevMode || isTesting !== true) {
filtered[key] = current[key];
}
});
return filtered;
}, {} as SubjectInfo)
);
subject.next(filtered);
}
export default function genericSubject (keyCreator: (address: string) => string, withTest: boolean = false): AddressSubject {
@@ -56,7 +55,7 @@ export default function genericSubject (keyCreator: (address: string) => string,
return current[address];
},
remove: (store: KeyringStore, address: string) => {
remove: (store: KeyringStore, address: string): void => {
current = { ...current };
delete current[address];
+9 -1
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { SubjectInfo } from './types';
import { combineLatest } from 'rxjs';
import { map } from 'rxjs/operators';
@@ -9,12 +11,18 @@ import accounts from './accounts';
import addresses from './addresses';
import contracts from './contracts';
interface Result {
accounts: SubjectInfo;
addresses: SubjectInfo;
contracts: SubjectInfo;
}
export default combineLatest(
accounts.subject,
addresses.subject,
contracts.subject
).pipe(
map(([accounts, addresses, contracts]) => ({
map(([accounts, addresses, contracts]): Result => ({
accounts,
addresses,
contracts
+12 -12
View File
@@ -6,17 +6,17 @@ import { BehaviorSubject } from 'rxjs';
import { KeyringSectionOption } from '../options/types';
import { KeyringJson, KeyringStore } from '../types';
export type SingleAddress = {
json: KeyringJson,
option: KeyringSectionOption
};
export interface SingleAddress {
json: KeyringJson;
option: KeyringSectionOption;
}
export type SubjectInfo = {
[index: string]: SingleAddress
};
export interface SubjectInfo {
[index: string]: SingleAddress;
}
export type AddressSubject = {
add: (store: KeyringStore, address: string, json: KeyringJson) => SingleAddress,
remove: (store: KeyringStore, address: string) => void,
subject: BehaviorSubject<SubjectInfo>
};
export interface AddressSubject {
add: (store: KeyringStore, address: string, json: KeyringJson) => SingleAddress;
remove: (store: KeyringStore, address: string) => void;
subject: BehaviorSubject<SubjectInfo>;
}
+8 -10
View File
@@ -6,15 +6,13 @@ import React from 'react';
import styled from 'styled-components';
import IdentityIcon from '@polkadot/ui-identicon';
type Props = {
address: string,
className?: string,
isUppercase: boolean,
name: string,
style?: {
[index: string]: string
}
};
interface Props {
address: string;
className?: string;
isUppercase: boolean;
name: string;
style?: Record<string, string>;
}
const Wrapper = styled.div`
display: flex;
@@ -54,7 +52,7 @@ const Wrapper = styled.div`
`;
export default class KeyPair extends React.PureComponent<Props> {
render () {
public render (): React.ReactNode {
const { address, className, isUppercase, name, style } = this.props;
return (
@@ -2,17 +2,19 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { KeyringStruct } from '../types';
import keyringOptionInstance from '.';
describe('KeyringOption', () => {
it('should not allow initOptions to be called more than once', () => {
const state = {};
describe('KeyringOption', (): void => {
it('should not allow initOptions to be called more than once', (): void => {
const state: Partial<KeyringStruct> = {};
// first call
keyringOptionInstance.init(state as any);
keyringOptionInstance.init(state as KeyringStruct);
// second call
expect(() => {
keyringOptionInstance.init(state as any);
expect((): void => {
keyringOptionInstance.init(state as KeyringStruct);
}).toThrowError('Unable to initialise options more than once');
});
});
+19 -23
View File
@@ -13,14 +13,14 @@ import observableAll from '../observable';
let hasCalledInitOptions = false;
const sortByName = (a: SingleAddress, b: SingleAddress) => {
const sortByName = (a: SingleAddress, b: SingleAddress): number => {
const valueA = a.option.name;
const valueB = b.option.name;
return valueA.localeCompare(valueB);
};
const sortByCreated = (a: SingleAddress, b: SingleAddress) => {
const sortByCreated = (a: SingleAddress, b: SingleAddress): number => {
const valueA = a.json.meta.whenCreated || 0;
const valueB = b.json.meta.whenCreated || 0;
@@ -36,9 +36,9 @@ const sortByCreated = (a: SingleAddress, b: SingleAddress) => {
};
class KeyringOption implements KeyringOptionInstance {
optionsSubject: BehaviorSubject<KeyringOptions> = new BehaviorSubject(this.emptyOptions());
public readonly optionsSubject: BehaviorSubject<KeyringOptions> = new BehaviorSubject(this.emptyOptions());
createOptionHeader (name: string): KeyringSectionOption {
public createOptionHeader (name: string): KeyringSectionOption {
return {
className: 'header disabled',
name,
@@ -48,10 +48,10 @@ class KeyringOption implements KeyringOptionInstance {
};
}
init (keyring: KeyringStruct): void {
public init (keyring: KeyringStruct): void {
assert(!hasCalledInitOptions, 'Unable to initialise options more than once');
observableAll.subscribe(() => {
observableAll.subscribe((): void => {
const options = this.emptyOptions();
this.addAccounts(keyring, options);
@@ -59,15 +59,15 @@ class KeyringOption implements KeyringOptionInstance {
this.addContracts(keyring, options);
options.address = this.linkItems({
'Addresses': options.address,
'Recent': options.recent
Addresses: options.address,
Recent: options.recent
});
options.account = this.linkItems({
'Accounts': options.account,
'Development': options.testing
Accounts: options.account,
Development: options.testing
});
options.contract = this.linkItems({
'Contracts': options.contract
Contracts: options.contract
});
options.all = ([] as KeyringSectionOptions).concat(
@@ -87,8 +87,8 @@ class KeyringOption implements KeyringOptionInstance {
hasCalledInitOptions = true;
}
private linkItems (items: { [index: string]: KeyringSectionOptions }) {
return Object.keys(items).reduce((result, header) => {
private linkItems (items: { [index: string]: KeyringSectionOptions }): KeyringSectionOptions {
return Object.keys(items).reduce((result, header): KeyringSectionOptions => {
const options = items[header];
return result.concat(
@@ -106,7 +106,7 @@ class KeyringOption implements KeyringOptionInstance {
Object
.values(available)
.sort(sortByName)
.forEach(({ json: { meta: { isTesting = false } }, option }: SingleAddress) => {
.forEach(({ json: { meta: { isTesting = false } }, option }: SingleAddress): void => {
if (!isTesting) {
options.account.push(option);
} else {
@@ -120,21 +120,17 @@ class KeyringOption implements KeyringOptionInstance {
Object
.values(available)
.filter(({ json }: SingleAddress) => {
return json.meta.isRecent;
})
.filter(({ json }: SingleAddress): boolean => !!json.meta.isRecent)
.sort(sortByCreated)
.forEach(({ option }: SingleAddress) => {
.forEach(({ option }: SingleAddress): void => {
options.recent.push(option);
});
Object
.values(available)
.filter(({ json }: SingleAddress) => {
return !json.meta.isRecent;
})
.filter(({ json }: SingleAddress): boolean => !json.meta.isRecent)
.sort(sortByName)
.forEach(({ option }: SingleAddress) => {
.forEach(({ option }: SingleAddress): void => {
options.address.push(option);
});
}
@@ -145,7 +141,7 @@ class KeyringOption implements KeyringOptionInstance {
Object
.values(available)
.sort(sortByName)
.forEach(({ option }: SingleAddress) => {
.forEach(({ option }: SingleAddress): void => {
options.contract.push(option);
});
}
+12 -10
View File
@@ -4,17 +4,19 @@
import { KeyringItemType, KeyringStruct } from '../types';
export type KeyringSectionOption = {
className?: string,
disabled?: boolean,
content?: any | string, // node?
key: string | null,
name: string,
text: any | string, // node?
value: string | null
};
import React from 'react';
export type KeyringSectionOptions = Array<KeyringSectionOption>;
export interface KeyringSectionOption {
className?: string;
disabled?: boolean;
content?: React.ReactNode;
key: string | null;
name: string;
text: React.ReactNode;
value: string | null;
}
export type KeyringSectionOptions = KeyringSectionOption[];
export type KeyringOptions = {
[type in KeyringItemType | 'all' | 'allPlus' | 'recent' | 'testing']: KeyringSectionOptions
+8 -8
View File
@@ -2,27 +2,27 @@
// 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 { KeyringStore, KeyringJson } from '../types';
import store from 'store';
export default class BrowserStore implements KeyringStore {
all (cb: (key: string, value: any) => void): void {
store.each((value: any, key: string) =>
cb(key, value)
);
public all (cb: (key: string, value: KeyringJson) => void): void {
store.each((value: KeyringJson, key: string): void => {
cb(key, value);
});
}
get (key: string, cb: (value: any) => void): void {
public get (key: string, cb: (value: KeyringJson) => void): void {
cb(store.get(key));
}
remove (key: string, cb?: () => void): void {
public remove (key: string, cb?: () => void): void {
store.remove(key);
cb && cb();
}
set (key: string, value: any, cb?: () => void): void {
public set (key: string, value: KeyringJson, cb?: () => void): void {
store.set(key, value);
cb && cb();
}
+16 -12
View File
@@ -2,10 +2,13 @@
// 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 { KeyringStore, KeyringJson } from '../types';
import extension from 'extensionizer';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type StoreValue = Record<string, any>;
const lastError = (type: string): void => {
const error = extension.runtime.lastError;
@@ -15,33 +18,34 @@ const lastError = (type: string): void => {
};
export default class ExtensionStore implements KeyringStore {
all (cb: (key: string, value: any) => void): void {
extension.storage.local.get(null, (result: { [index: string]: any }) => {
public all (cb: (key: string, value: KeyringJson) => void): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
extension.storage.local.get(null, (result: StoreValue): void => {
lastError('all');
Object.entries(result).forEach(([key, value]) =>
cb(key, value)
);
Object.entries(result).forEach(([key, value]): void => {
cb(key, value);
});
});
}
get (key: string, cb: (value: any) => void): void {
extension.storage.local.get([key], (result: { [index: string]: any }) => {
public get (key: string, cb: (value: KeyringJson) => void): void {
extension.storage.local.get([key], (result: StoreValue): void => {
lastError('get');
cb(result[key]);
});
}
remove (key: string, cb?: () => void): void {
extension.storage.local.remove(key, () => {
public remove (key: string, cb?: () => void): void {
extension.storage.local.remove(key, (): void => {
lastError('remove');
cb && cb();
});
}
set (key: string, value: any, cb?: () => void): void {
public set (key: string, value: KeyringJson, cb?: () => void): void {
// shortcut, don't save testing accounts in extension storage
if (key.indexOf('account:') === 0 && value.meta && value.meta.isTesting) {
cb && cb();
@@ -49,7 +53,7 @@ export default class ExtensionStore implements KeyringStore {
return;
}
extension.storage.local.set({ [key]: value }, () => {
extension.storage.local.set({ [key]: value }, (): void => {
lastError('set');
cb && cb();
+11 -9
View File
@@ -2,7 +2,7 @@
// 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 { KeyringStore, KeyringJson } from '../types';
import fs from 'fs';
import mkdirp from 'mkdirp';
@@ -12,7 +12,7 @@ import path from 'path';
export default class FileStore implements KeyringStore {
private _path: string;
constructor (path: string) {
public constructor (path: string) {
if (!fs.existsSync(path)) {
mkdirp.sync(path);
}
@@ -20,23 +20,25 @@ export default class FileStore implements KeyringStore {
this._path = path;
}
all (cb: (key: string, value: any) => void): void {
public all (cb: (key: string, value: KeyringJson) => void): void {
fs
.readdirSync(this._path)
.filter((key) => !['.', '..'].includes(key))
.forEach((key) => cb(key, this._readKey(key)));
.filter((key): boolean => !['.', '..'].includes(key))
.forEach((key): void => {
cb(key, this._readKey(key));
});
}
get (key: string, cb: (value: any) => void): void {
public get (key: string, cb: (value: KeyringJson) => void): void {
cb(this._readKey(key));
}
remove (key: string, cb?: () => void): void {
public remove (key: string, cb?: () => void): void {
fs.unlinkSync(this._getPath(key));
cb && cb();
}
set (key: string, value: any, cb?: () => void): void {
public set (key: string, value: KeyringJson, cb?: () => void): void {
fs.writeFileSync(this._getPath(key), Buffer.from(JSON.stringify(value), 'utf-8'));
cb && cb();
}
@@ -45,7 +47,7 @@ export default class FileStore implements KeyringStore {
return path.join(this._path, key);
}
private _readKey (key: string): any {
private _readKey (key: string): KeyringJson {
return JSON.parse(
fs.readFileSync(this._getPath(key)).toString('utf-8')
);
+46 -44
View File
@@ -7,56 +7,58 @@ import { KeyringInstance as BaseKeyringInstance, KeyringPair, KeyringPair$Meta,
import { KeypairType } from '@polkadot/util-crypto/types';
import { AddressSubject, SingleAddress } from './observable/types';
export type ContractMeta = {
abi: string,
genesisHash?: string
};
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 interface ContractMeta {
abi: string;
genesisHash?: string;
}
export type KeyringOptions = KeyringOptionsBase & {
filter?: (json: KeyringJson) => boolean,
genesisHash?: Hash,
isDevelopment?: boolean,
store?: KeyringStore
};
export interface KeyringStore {
all: (cb: (key: string, value: KeyringJson) => void) => void;
get: (key: string, cb: (value: KeyringJson) => void) => void;
remove: (key: string, cb?: () => void) => void;
set: (key: string, value: KeyringJson, cb?: () => void) => void;
}
export type KeyringJson$Meta = {
contract?: ContractMeta,
isInjected?: boolean,
isRecent?: boolean,
isTesting?: boolean,
name?: string,
whenCreated?: number,
whenEdited?: number,
whenUsed?: number,
[index: string]: any
};
export interface KeyringOptions extends KeyringOptionsBase {
filter?: (json: KeyringJson) => boolean;
genesisHash?: Hash;
isDevelopment?: boolean;
store?: KeyringStore;
}
export type KeyringJson = {
address: string,
meta: KeyringJson$Meta
};
// eslint-disable-next-line @typescript-eslint/class-name-casing
export interface KeyringJson$Meta {
contract?: ContractMeta;
isInjected?: boolean;
isRecent?: boolean;
isTesting?: boolean;
name?: string;
whenCreated?: number;
whenEdited?: number;
whenUsed?: number;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[index: string]: any;
}
export type KeyringAddress = {
readonly address: string,
readonly meta: KeyringJson$Meta,
readonly publicKey: Uint8Array
};
export interface KeyringJson {
address: string;
meta: KeyringJson$Meta;
}
export interface KeyringAddress {
readonly address: string;
readonly meta: KeyringJson$Meta;
readonly publicKey: Uint8Array;
}
export type KeyringAddressType = 'address' | 'contract';
export type KeyringItemType = 'account' | KeyringAddressType;
export type CreateResult = {
json: KeyringPair$Json,
pair: KeyringPair
};
export interface CreateResult {
json: KeyringPair$Json;
pair: KeyringPair;
}
export interface KeyringStruct {
readonly accounts: AddressSubject;
@@ -77,13 +79,13 @@ export interface KeyringStruct {
forgetAddress: (address: string) => void;
forgetContract: (address: string) => void;
getAccount: (address: string | Uint8Array) => KeyringAddress | undefined;
getAccounts: () => Array<KeyringAddress>;
getAccounts: () => KeyringAddress[];
getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress | undefined;
getAddresses: () => Array<KeyringAddress>;
getAddresses: () => KeyringAddress[];
getContract: (address: string | Uint8Array) => KeyringAddress | undefined;
getContracts: (genesisHash?: string) => Array<KeyringAddress>;
getContracts: (genesisHash?: string) => KeyringAddress[];
getPair: (address: string | Uint8Array) => KeyringPair;
getPairs: () => Array<KeyringPair>;
getPairs: () => KeyringPair[];
isAvailable: (address: string | Uint8Array) => boolean;
isPassValid: (password: string) => boolean;
loadAll: (options: KeyringOptions) => void;