From c2e3d5fabe8c259344015c859610d2b12a12dac5 Mon Sep 17 00:00:00 2001 From: kwingram25 Date: Fri, 7 Jun 2019 14:27:28 +0200 Subject: [PATCH] Ki contracts (#143) * add contracts to keyring * genesisHash * requested changes + allPlus option type * remove duplicate fn * add contracts to keyring * genesisHash * requested changes + allPlus option type * remove duplicate fn * changes * contract key * prefixes * remove fallback hell * errors * remove breaking api --- packages/ui-keyring/src/Keyring.ts | 44 +++++++++++++----------- packages/ui-keyring/src/options/types.ts | 10 ++---- packages/ui-keyring/src/types.ts | 6 +++- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/packages/ui-keyring/src/Keyring.ts b/packages/ui-keyring/src/Keyring.ts index bcd65cc7..3cab2542 100644 --- a/packages/ui-keyring/src/Keyring.ts +++ b/packages/ui-keyring/src/Keyring.ts @@ -5,7 +5,7 @@ import { KeyringPair, KeyringPair$Meta, KeyringPair$Json } from '@polkadot/keyring/types'; import { KeypairType } from '@polkadot/util-crypto/types'; import { SingleAddress } from './observable/types'; -import { CreateResult, KeyringAddress, KeyringJson, KeyringJson$Meta, KeyringOptions, KeyringStruct } from './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'; @@ -21,6 +21,12 @@ 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 + }; + addExternal (publicKey: Uint8Array, meta: KeyringPair$Meta = {}): CreateResult { const pair = this.keyring.addFromAddress(publicKey, { ...meta, isExternal: true }, null); const json = this.saveAccount(pair); @@ -118,22 +124,29 @@ export class Keyring extends Base implements KeyringStruct { .filter((account) => env.isDevelopment() || account.getMeta().isTesting !== true); } - getAddress (_address: string | Uint8Array, type: 'account' | 'address' | 'contract' = 'address'): KeyringAddress { + getAddress (_address: string | Uint8Array, type: KeyringItemType | null = null): KeyringAddress { const address = isString(_address) ? _address : this.encodeAddress(_address); const publicKey = this.decodeAddress(address); const subject = (() => { - switch (type) { - case 'account': - return this.accounts.subject; - case 'address': - return this.addresses.subject; - case 'contract': - return this.contracts.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; })(); + if (!subject) { + throw new Error('Address not found'); + } + return { address: (): string => address, @@ -305,7 +318,7 @@ export class Keyring extends Base implements KeyringStruct { }); } - saveAddress (address: string, meta: KeyringPair$Meta, type: 'address' | 'contract' = 'address'): KeyringPair$Json { + saveAddress (address: string, meta: KeyringPair$Meta, type: KeyringAddressType = 'address'): KeyringPair$Json { const available = this.addresses.subject.getValue(); const json = (available[address] && available[address].json) || { @@ -322,16 +335,7 @@ export class Keyring extends Base implements KeyringStruct { delete json.meta.isRecent; - const key = (() => { - switch (type) { - case 'contract': - return 'contracts'; - default: - return 'addresses'; - } - })(); - - this[key].add(this._store, address, json); + this.stores[type]().add(this._store, address, json); return json as KeyringPair$Json; } diff --git a/packages/ui-keyring/src/options/types.ts b/packages/ui-keyring/src/options/types.ts index 0f9e73c8..a929739c 100644 --- a/packages/ui-keyring/src/options/types.ts +++ b/packages/ui-keyring/src/options/types.ts @@ -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 { KeyringStruct } from '../types'; +import { KeyringItemType, KeyringStruct } from '../types'; export type KeyringSectionOption = { className?: string, @@ -17,13 +17,7 @@ export type KeyringSectionOption = { export type KeyringSectionOptions = Array; export type KeyringOptions = { - account: KeyringSectionOptions, - address: KeyringSectionOptions, - all: KeyringSectionOptions, - allPlus: KeyringSectionOptions, - contract: KeyringSectionOptions, - recent: KeyringSectionOptions, - testing: KeyringSectionOptions + [type in KeyringItemType | 'all' | 'allPlus' | 'recent' | 'testing']: KeyringSectionOptions }; export type KeyringOption$Type = keyof KeyringOptions; diff --git a/packages/ui-keyring/src/types.ts b/packages/ui-keyring/src/types.ts index 1ee1ad0c..a326288e 100644 --- a/packages/ui-keyring/src/types.ts +++ b/packages/ui-keyring/src/types.ts @@ -50,6 +50,10 @@ export type KeyringAddress = { getMeta: () => KeyringJson$Meta }; +export type KeyringAddressType = 'address' | 'contract'; + +export type KeyringItemType = 'account' | KeyringAddressType; + export type CreateResult = { json: KeyringPair$Json, pair: KeyringPair @@ -78,7 +82,7 @@ export interface KeyringStruct { forgetContract: (address: string) => void; getAccount: (address: string | Uint8Array) => KeyringAddress; getAccounts: () => Array; - getAddress: (address: string | Uint8Array) => KeyringAddress; + getAddress: (address: string | Uint8Array, type: KeyringItemType | null) => KeyringAddress; getAddresses: () => Array; getContract: (address: string | Uint8Array) => KeyringAddress; getContracts: (genesisHash?: string) => Array;