Support for multi genesisHash in keyring accounts (#481)

* Support for multi genesisHash in keyring accounts

* genesis

* Only single in keyring
This commit is contained in:
Jaco
2021-12-28 08:29:03 +01:00
committed by GitHub
parent 1905a165c4
commit 2904beb171
4 changed files with 37 additions and 16 deletions
+5
View File
@@ -1,5 +1,10 @@
# CHANGELOG
## master
- Allow for loading of accounts on additional specified hashes
## 0.87.7 Dec 27, 2021
Changes:
+9
View File
@@ -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();
+22 -16
View File
@@ -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)}`);
}
}
});
+1
View File
@@ -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;
}