Only apply knownGenesis once for metadata (#949)
* Only apply knownGenesis once for metadata * Adjust * Clear old known metadata entries * Add missing dep
This commit is contained in:
parent
ea06336d54
commit
45a6e18fc2
@ -1,5 +1,13 @@
|
||||
# CHANGELOG
|
||||
|
||||
## master
|
||||
|
||||
Changes:
|
||||
|
||||
- Ensure that only latest metadata is applied (when multiple genesis)
|
||||
- Rename all `*.test.ts` to `*.spec.ts` (cross-repo consistency)
|
||||
|
||||
|
||||
## 0.42.4 Dec 27, 2021
|
||||
|
||||
**Important** As 0.42.3, not published to the stores, fixes dependency issue in 0.42.4.
|
||||
|
||||
@ -26,6 +26,7 @@
|
||||
"@polkadot/extension-dapp": "^0.42.5-1",
|
||||
"@polkadot/extension-inject": "^0.42.5-1",
|
||||
"@polkadot/keyring": "^8.2.2",
|
||||
"@polkadot/networks": "^8.2.2",
|
||||
"@polkadot/phishing": "^0.6.585",
|
||||
"@polkadot/rpc-provider": "^7.1.2-0",
|
||||
"@polkadot/types": "^7.1.2-0",
|
||||
|
||||
@ -9,6 +9,7 @@ import { BehaviorSubject } from 'rxjs';
|
||||
|
||||
import { getId } from '@polkadot/extension-base/utils/getId';
|
||||
import { addMetadata, knownMetadata } from '@polkadot/extension-chains';
|
||||
import { knownGenesis } from '@polkadot/networks/defaults';
|
||||
import settings from '@polkadot/ui-settings';
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
@ -85,6 +86,38 @@ export enum NotificationOptions {
|
||||
|
||||
const AUTH_URLS_KEY = 'authUrls';
|
||||
|
||||
function extractMetadata (store: MetadataStore): void {
|
||||
const knownEntries = Object.entries(knownGenesis);
|
||||
const defs: Record<string, { def: MetadataDef, index: number, key: string }> = {};
|
||||
const removals: string[] = [];
|
||||
|
||||
store.all((key: string, def: MetadataDef): void => {
|
||||
const entry = knownEntries.find(([, hashes]) => hashes.includes(def.genesisHash));
|
||||
|
||||
if (entry) {
|
||||
const [name, hashes] = entry;
|
||||
const index = hashes.indexOf(def.genesisHash);
|
||||
|
||||
// flatten the known metadata based on the genesis index
|
||||
// (lower is better/newer)
|
||||
if (!defs[name] || (defs[name].index > index)) {
|
||||
if (defs[name]) {
|
||||
// remove the old version of the metadata
|
||||
removals.push(defs[name].key);
|
||||
}
|
||||
|
||||
defs[name] = { def, index, key };
|
||||
}
|
||||
} else {
|
||||
// this is not a known entry, so we will just apply it
|
||||
defs[key] = { def, index: 0, key };
|
||||
}
|
||||
});
|
||||
|
||||
removals.forEach((key) => store.remove(key));
|
||||
Object.values(defs).forEach(({ def }) => addMetadata(def));
|
||||
}
|
||||
|
||||
export default class State {
|
||||
readonly #authUrls: AuthUrls = {};
|
||||
|
||||
@ -115,9 +148,7 @@ export default class State {
|
||||
constructor (providers: Providers = {}) {
|
||||
this.#providers = providers;
|
||||
|
||||
this.#metaStore.all((_key: string, def: MetadataDef): void => {
|
||||
addMetadata(def);
|
||||
});
|
||||
extractMetadata(this.#metaStore);
|
||||
|
||||
// retrieve previously set authorizations
|
||||
const authString = localStorage.getItem(AUTH_URLS_KEY) || '{}';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user