Adapt interfaces for latest @polkadot/keyring (#146)

* Adapt interfaces for latest @polkadot/keyring

* Re-write yarn.lock

* Bump latest api
This commit is contained in:
Jaco Greeff
2019-06-13 16:16:53 +02:00
committed by GitHub
parent 77f3ad1b55
commit b26a0ef083
8 changed files with 109 additions and 131 deletions
+7 -12
View File
@@ -58,11 +58,11 @@ export default class Base {
return this._genesisHash;
}
decodeAddress (key: string | Uint8Array, ignoreChecksum?: boolean): Uint8Array {
decodeAddress = (key: string | Uint8Array, ignoreChecksum?: boolean): Uint8Array => {
return this.keyring.decodeAddress(key, ignoreChecksum);
}
encodeAddress (key: string | Uint8Array): string {
encodeAddress = (key: string | Uint8Array): string => {
return this.keyring.encodeAddress(key);
}
@@ -72,7 +72,7 @@ export default class Base {
getPairs (): Array<KeyringPair> {
return this.keyring.getPairs().filter((pair: KeyringPair) =>
env.isDevelopment() || pair.getMeta().isTesting !== true
env.isDevelopment() || pair.meta.isTesting !== true
);
}
@@ -116,19 +116,14 @@ export default class Base {
protected addAccountPairs (): void {
this.keyring
.getPairs()
.forEach((pair: KeyringPair) => {
const address = pair.address();
this.accounts.add(this._store, address, {
address,
meta: pair.getMeta()
});
.forEach(({ address, meta }: KeyringPair) => {
this.accounts.add(this._store, address, { address, meta });
});
}
protected addTimestamp (pair: KeyringPair): void {
if (!pair.getMeta().whenCreated) {
pair.setMeta({ whenCreated: Date.now() });
if (!pair.meta.whenCreated) {
pair.setMeta({ whenCreated: Date.now() });
}
}
}
+23 -35
View File
@@ -8,7 +8,7 @@ import { SingleAddress } from './observable/types';
import { CreateResult, KeyringAddress, KeyringAddressType, KeyringItemType, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './types';
import createPair from '@polkadot/keyring/pair';
import { hexToU8a, isHex, isString, u8aToHex } from '@polkadot/util';
import { hexToU8a, isHex, isString } from '@polkadot/util';
import env from './observable/development';
import Base from './Base';
@@ -58,7 +58,7 @@ export class Keyring extends Base implements KeyringStruct {
}
backupAccount (pair: KeyringPair, password: string): KeyringPair$Json {
if (!pair.isLocked()) {
if (!pair.isLocked) {
pair.lock();
}
@@ -67,24 +67,6 @@ export class Keyring extends Base implements KeyringStruct {
return pair.toJson(password);
}
createAccount (seed: Uint8Array, password?: string, meta?: KeyringPair$Meta): KeyringPair {
console.warn('createAccount deprecated, use addUri instead');
return this.addUri(u8aToHex(seed), password, meta).pair;
}
createAccountExternal (publicKey: Uint8Array, meta?: KeyringPair$Meta): KeyringPair {
console.warn('createAccountExternal deprecated, use addExternal instead');
return this.addExternal(publicKey, meta).pair;
}
createAccountMnemonic (seed: string, password?: string, meta?: KeyringPair$Meta): KeyringPair {
console.warn('createAccountMnemonic deprecated, use createUri instead');
return this.addUri(seed, password, meta).pair;
}
createFromUri (suri: string, meta: KeyringPair$Meta = {}, type?: KeypairType): KeyringPair {
return this.keyring.createFromUri(suri, meta, type);
}
@@ -95,7 +77,7 @@ export class Keyring extends Base implements KeyringStruct {
json.meta.whenEdited = Date.now();
this.keyring.addFromJson(json);
this.accounts.add(this._store, pair.address(), json);
this.accounts.add(this._store, pair.address, json);
}
forgetAccount (address: string): void {
@@ -121,10 +103,11 @@ export class Keyring extends Base implements KeyringStruct {
return Object
.keys(available)
.map((address) => this.getAddress(address, 'account'))
.filter((account) => env.isDevelopment() || account.getMeta().isTesting !== true);
.filter((account) => env.isDevelopment() || account.meta.isTesting !== true);
}
getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress {
const encodeAddress = this.encodeAddress;
const address = isString(_address)
? _address
: this.encodeAddress(_address);
@@ -148,14 +131,19 @@ export class Keyring extends Base implements KeyringStruct {
}
return {
address: (): string =>
address,
isValid: (): boolean =>
!!subject.getValue()[address],
publicKey: (): Uint8Array =>
publicKey,
getMeta: (): KeyringJson$Meta =>
subject.getValue()[address].json.meta
get address (): string {
return encodeAddress(publicKey);
},
get isValid (): boolean {
return !!(subject && subject.getValue()[address]);
},
get publicKey (): Uint8Array {
return publicKey;
},
get meta (): KeyringJson$Meta {
// This is actually non-applicable, i.e. here we will have a subject
return subject ? subject.getValue()[address].json.meta : {};
}
};
}
@@ -197,7 +185,7 @@ export class Keyring extends Base implements KeyringStruct {
// FIXME Just for the transition period (ignoreChecksum)
const pair = this.keyring.addFromJson(json as KeyringPair$Json, true);
this.accounts.add(this._store, pair.address(), json);
this.accounts.add(this._store, pair.address, json);
}
const [, hexAddr] = key.split(':');
@@ -245,7 +233,7 @@ export class Keyring extends Base implements KeyringStruct {
};
const pair = this.keyring.addFromAddress(address, json.meta);
this.accounts.add(this._store, pair.address(), json);
this.accounts.add(this._store, pair.address, json);
}
loadAll (options: KeyringOptions, injected: Array<{ address: string, meta: KeyringJson$Meta }> = []): void {
@@ -302,17 +290,17 @@ export class Keyring extends Base implements KeyringStruct {
const json = pair.toJson(password);
this.keyring.addFromJson(json);
this.accounts.add(this._store, pair.address(), json);
this.accounts.add(this._store, pair.address, json);
return json;
}
saveAccountMeta (pair: KeyringPair, meta: KeyringPair$Meta): void {
const address = pair.address();
const address = pair.address;
this._store.get(accountKey(address), (json: KeyringJson) => {
pair.setMeta(meta);
json.meta = pair.getMeta();
json.meta = pair.meta;
this.accounts.add(this._store, address, json);
});
+4 -7
View File
@@ -44,10 +44,10 @@ export type KeyringJson = {
};
export type KeyringAddress = {
address: () => string,
isValid: () => boolean,
publicKey: () => Uint8Array,
getMeta: () => KeyringJson$Meta
readonly address: string,
readonly isValid: boolean,
readonly meta: KeyringJson$Meta,
readonly publicKey: Uint8Array
};
export type KeyringAddressType = 'address' | 'contract';
@@ -70,9 +70,6 @@ export interface KeyringStruct {
addPair: (pair: KeyringPair, password: string) => CreateResult;
addUri: (suri: string, password?: string, meta?: KeyringPair$Meta, type?: KeypairType) => CreateResult;
backupAccount: (pair: KeyringPair, password: string) => KeyringPair$Json;
createAccount: (seed: Uint8Array, password?: string, meta?: KeyringPair$Meta) => KeyringPair;
createAccountExternal: (publicKey: Uint8Array, meta?: KeyringPair$Meta) => KeyringPair;
createAccountMnemonic: (seed: string, password?: string, meta?: KeyringPair$Meta) => KeyringPair;
createFromUri (suri: string, meta?: KeyringPair$Meta, type?: KeypairType): KeyringPair;
decodeAddress: (key: string | Uint8Array) => Uint8Array;
encodeAddress: (key: string | Uint8Array) => string;