mirror of
https://github.com/pezkuwichain/pezkuwi-ui.git
synced 2026-06-13 11:01:03 +00:00
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:
@@ -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)}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user