From 2a30f2f4831b58525b7dea8d7dc0b528b9a6a1df Mon Sep 17 00:00:00 2001 From: Axel Chalon Date: Wed, 19 Jun 2019 18:17:15 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + packages/ui-keyring/README.md | 4 +-- packages/ui-keyring/src/Keyring.ts | 52 +++++++++--------------------- packages/ui-keyring/src/types.ts | 7 ++-- 4 files changed, 22 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2d9d4b4..127073c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/packages/ui-keyring/README.md b/packages/ui-keyring/README.md index 63adb1e8..cc9340b4 100644 --- a/packages/ui-keyring/README.md +++ b/packages/ui-keyring/README.md @@ -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); diff --git a/packages/ui-keyring/src/Keyring.ts b/packages/ui-keyring/src/Keyring.ts index 96398ccf..80cd4961 100644 --- a/packages/ui-keyring/src/Keyring.ts +++ b/packages/ui-keyring/src/Keyring.ts @@ -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((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) { diff --git a/packages/ui-keyring/src/types.ts b/packages/ui-keyring/src/types.ts index 576e4ec8..91a30d46 100644 --- a/packages/ui-keyring/src/types.ts +++ b/packages/ui-keyring/src/types.ts @@ -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; - getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress; + getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress | undefined; getAddresses: () => Array; - getContract: (address: string | Uint8Array) => KeyringAddress; + getContract: (address: string | Uint8Array) => KeyringAddress | undefined; getContracts: (genesisHash?: string) => Array; getPair: (address: string | Uint8Array) => KeyringPair; getPairs: () => Array;