Simplify KeyringAddress interface (#144)

* Simplify ui-keyring KeyringAddress

* Adapt to new @polkadot/keyring KeyPair interface

* Pull in common 0.93.0-beta.4

* KeyringAddress | undefined
This commit is contained in:
Axel Chalon
2019-06-19 18:17:15 +02:00
committed by Jaco Greeff
parent 03f22988a4
commit 2a30f2f483
4 changed files with 22 additions and 42 deletions
+1
View File
@@ -4,6 +4,7 @@
- expose `address` getter instead of `address()`
- expose `publicKey` getter instead of `publicKey()`
- expose `meta` getter instead of `getMeta()`
- The functions `getAccount` `getAddress` `getContract` in `@polkadot/ui-keyring` now return either undefined or an object with the above properties.
- Add support for the saving of contracts to the keyring
- Use the injection of stores, providing an additional `ExtensionStore` for saving to Chrome/FF extensions (in addition to the standard localStorage saving)
- Remove previously deprecated kering functions, `createAccount`, `createAccountExternal` & `createAccountMnemonic`
+2 -2
View File
@@ -20,8 +20,8 @@ render () {
const pair = keyring.getPair(address);
// ask questions about that particular keyring pair
const isLocked = pair.isLocked();
const meta = pair.getMeta();
const isLocked = pair.isLocked;
const meta = pair.meta;
// save account from pair
keyring.saveAccount(pair, password);
+16 -36
View File
@@ -93,7 +93,7 @@ export class Keyring extends Base implements KeyringStruct {
this.contracts.remove(this._store, address);
}
getAccount (address: string | Uint8Array): KeyringAddress {
getAccount (address: string | Uint8Array): KeyringAddress | undefined {
return this.getAddress(address, 'account');
}
@@ -102,48 +102,28 @@ export class Keyring extends Base implements KeyringStruct {
return Object
.keys(available)
.map((address) => this.getAddress(address, 'account'))
.map((address) => this.getAddress(address, 'account') as KeyringAddress)
.filter((account) => env.isDevelopment() || account.meta.isTesting !== true);
}
getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress {
const encodeAddress = this.encodeAddress;
getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress | undefined {
const address = isString(_address)
? _address
: this.encodeAddress(_address);
const publicKey = this.decodeAddress(address);
const subject = (() => {
if (type && this.stores[type]) {
return this.stores[type]().subject;
}
let subject;
Object.values(this.stores).forEach((store) => {
if (store && store().subject.getValue()[address]) {
subject = store().subject;
}
});
return subject;
})();
const stores = type
? [this.stores[type]]
: Object.values(this.stores);
if (!subject) {
throw new Error('Address not found');
}
const info = stores.reduce<SingleAddress | undefined>((lastInfo, store) =>
(store().subject.getValue()[address] || lastInfo),
undefined);
return {
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 : {};
}
return info && {
address,
publicKey,
meta: info.json.meta
};
}
@@ -152,10 +132,10 @@ export class Keyring extends Base implements KeyringStruct {
return Object
.keys(available)
.map((address) => this.getAddress(address));
.map((address) => this.getAddress(address) as KeyringAddress);
}
getContract (address: string | Uint8Array): KeyringAddress {
getContract (address: string | Uint8Array): KeyringAddress | undefined {
return this.getAddress(address, 'contract');
}
@@ -168,7 +148,7 @@ export class Keyring extends Base implements KeyringStruct {
const { json: { meta: { contract } } } = data;
return contract && contract.genesisHash === this.genesisHash;
})
.map(([address]) => this.getContract(address));
.map(([address]) => this.getContract(address) as KeyringAddress);
}
private rewriteKey (json: KeyringJson, key: string, hexAddr: string, creator: (addr: string) => string) {
+3 -4
View File
@@ -45,7 +45,6 @@ export type KeyringJson = {
export type KeyringAddress = {
readonly address: string,
readonly isValid: boolean,
readonly meta: KeyringJson$Meta,
readonly publicKey: Uint8Array
};
@@ -77,11 +76,11 @@ export interface KeyringStruct {
forgetAccount: (address: string) => void;
forgetAddress: (address: string) => void;
forgetContract: (address: string) => void;
getAccount: (address: string | Uint8Array) => KeyringAddress;
getAccount: (address: string | Uint8Array) => KeyringAddress | undefined;
getAccounts: () => Array<KeyringAddress>;
getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress;
getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress | undefined;
getAddresses: () => Array<KeyringAddress>;
getContract: (address: string | Uint8Array) => KeyringAddress;
getContract: (address: string | Uint8Array) => KeyringAddress | undefined;
getContracts: (genesisHash?: string) => Array<KeyringAddress>;
getPair: (address: string | Uint8Array) => KeyringPair;
getPairs: () => Array<KeyringPair>;