From 2904beb1718370274a946877c7f13f78fa33643d Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 28 Dec 2021 08:29:03 +0100 Subject: [PATCH] Support for multi genesisHash in keyring accounts (#481) * Support for multi genesisHash in keyring accounts * genesis * Only single in keyring --- CHANGELOG.md | 5 ++++ packages/ui-keyring/src/Base.ts | 9 +++++++ packages/ui-keyring/src/Keyring.ts | 38 +++++++++++++++++------------- packages/ui-keyring/src/types.ts | 1 + 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d0b7550..a8e7d8ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## master + +- Allow for loading of accounts on additional specified hashes + + ## 0.87.7 Dec 27, 2021 Changes: diff --git a/packages/ui-keyring/src/Base.ts b/packages/ui-keyring/src/Base.ts index a948e4e5..acba3bc2 100644 --- a/packages/ui-keyring/src/Base.ts +++ b/packages/ui-keyring/src/Base.ts @@ -28,6 +28,8 @@ export class Base { protected _genesisHash?: string; + protected _genesisHashAdd: string[] = []; + constructor () { this.#accounts = accounts; this.#addresses = addresses; @@ -59,6 +61,12 @@ export class Base { return this._genesisHash; } + public get genesisHashes (): string[] { + return this._genesisHash + ? [this._genesisHash, ...this._genesisHashAdd] + : [...this._genesisHashAdd]; + } + public decodeAddress = (key: string | Uint8Array, ignoreChecksum?: boolean, ss58Format?: Prefix): Uint8Array => { return this.keyring.decodeAddress(key, ignoreChecksum, ss58Format); }; @@ -115,6 +123,7 @@ export class Base { ? options.genesisHash.toString() : options.genesisHash.toHex() ); + this._genesisHashAdd = options.genesisHashAdd || []; this._store = options.store || this._store; this.addAccountPairs(); diff --git a/packages/ui-keyring/src/Keyring.ts b/packages/ui-keyring/src/Keyring.ts index 63ee016a..a52e28d4 100644 --- a/packages/ui-keyring/src/Keyring.ts +++ b/packages/ui-keyring/src/Keyring.ts @@ -10,7 +10,7 @@ import type { CreateResult, KeyringAddress, KeyringAddressType, KeyringItemType, import { createPair } from '@polkadot/keyring'; import { chains } from '@polkadot/ui-settings'; -import { bnToBn, hexToU8a, isHex, isString, objectSpread, stringToU8a, u8aSorted, u8aToString } from '@polkadot/util'; +import { bnToBn, hexToU8a, isFunction, isHex, isString, objectSpread, stringify, stringToU8a, u8aSorted, u8aToString } from '@polkadot/util'; import { base64Decode, createKeyMulti, jsonDecrypt, jsonEncrypt } from '@polkadot/util-crypto'; import { env } from './observable/env'; @@ -185,7 +185,7 @@ export class Keyring extends Base implements KeyringStruct { .filter(([, { json: { meta: { contract } } }]): boolean => !!contract && contract.genesisHash === this.genesisHash ) - .map(([address]): KeyringAddress => this.getContract(address) as KeyringAddress); + .map(([address]) => this.getContract(address) as KeyringAddress); } private rewriteKey (json: KeyringJson, key: string, hexAddr: string, creator: (addr: string) => string): void { @@ -199,7 +199,6 @@ export class Keyring extends Base implements KeyringStruct { 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); this.accounts.add(this._store, pair.address, json, pair.type); @@ -259,16 +258,13 @@ export class Keyring extends Base implements KeyringStruct { this.accounts.add(this._store, pair.address, json, pair.type); } - private allowGenesis (json?: KeyringJson | { meta: KeyringJson$Meta } | null): boolean { - if (json && json.meta && this.genesisHash) { - const hashes: (string | null | undefined)[] = Object.values(chains).find((hashes): boolean => - hashes.includes(this.genesisHash || '') - ) || [this.genesisHash]; - + private allowGenesis (hashes: string[], json?: KeyringJson | { meta: KeyringJson$Meta } | null): boolean { + if (json && json.meta) { if (json.meta.genesisHash) { return hashes.includes(json.meta.genesisHash); - } else if (json.meta.contract) { - return hashes.includes(json.meta.contract.genesisHash); + } else if (json.meta.contract && json.meta.contract.genesisHash) { + // for contracts, we only allow the primary/first hash + return hashes[0] === json.meta.contract.genesisHash; } } @@ -278,10 +274,20 @@ export class Keyring extends Base implements KeyringStruct { public loadAll (options: KeyringOptions, injected: { address: string; meta: KeyringJson$Meta, type?: KeypairType }[] = []): void { super.initKeyring(options); + const hashes = [ + ...this.genesisHashes, + ...( + Object + .values(chains) + .find((h) => h.includes(this.genesisHash || '')) || + [] + ) + ]; + this._store.all((key: string, json: KeyringJson): void => { - if (options.filter ? options.filter(json) : true) { + if (!isFunction(options.filter) || options.filter(json)) { try { - if (this.allowGenesis(json)) { + if (this.allowGenesis(hashes, json)) { if (accountRegex.test(key)) { this.loadAccount(json, key); } else if (addressRegex.test(key)) { @@ -291,17 +297,17 @@ export class Keyring extends Base implements KeyringStruct { } } } catch (error) { - // ignore + console.warn(`Keyring: Unable to load ${key}:${stringify(json)}`); } } }); injected.forEach((account): void => { - if (this.allowGenesis(account)) { + if (this.allowGenesis(hashes, account)) { try { this.loadInjected(account.address, account.meta, account.type); } catch (error) { - // ignore + console.warn(`Keyring: Unable to inject ${stringify(account)}`); } } }); diff --git a/packages/ui-keyring/src/types.ts b/packages/ui-keyring/src/types.ts index 05eef114..7e529210 100644 --- a/packages/ui-keyring/src/types.ts +++ b/packages/ui-keyring/src/types.ts @@ -45,6 +45,7 @@ export interface KeyringStore { export interface KeyringOptions extends KeyringOptionsBase { filter?: (json: KeyringJson) => boolean; genesisHash?: string | { toHex: () => string }; + genesisHashAdd?: string[]; isDevelopment?: boolean; store?: KeyringStore; }