// Copyright 2019-2020 @polkadot/extension-chains authors & contributors // SPDX-License-Identifier: Apache-2.0 import { MetadataDef } from '@polkadot/extension-inject/types'; import { Chain } from './types'; import { Metadata, TypeRegistry } from '@polkadot/types'; // imports chain details, generally metadata. For the generation of these, // inside the api, run `yarn chain:info --ws ` // import kusama from './kusama'; const definitions = new Map( // [kusama].map((def) => [def.genesisHash, def]) ); const expanded = new Map(); export function metadataExpand (definition: MetadataDef, isPartial = false): Chain { const cached = expanded.get(definition.genesisHash); if (cached && cached.specVersion === definition.specVersion) { return cached; } const { chain, genesisHash, icon, metaCalls, specVersion, ss58Format, tokenDecimals, tokenSymbol, types } = definition; const registry = new TypeRegistry(); if (!isPartial) { registry.register(types); } const isUnknown = genesisHash === '0x'; registry.setChainProperties(registry.createType('ChainProperties', { ss58Format, tokenDecimals, tokenSymbol })); const metadata = metaCalls && !isPartial ? new Metadata(registry, Buffer.from(metaCalls, 'base64')) : null; const result = { definition, genesisHash: isUnknown ? undefined : genesisHash, hasMetadata: !!metadata, icon: icon || 'substrate', isUnknown, name: chain, registry, specVersion, ss58Format, tokenDecimals, tokenSymbol }; if (result.genesisHash && !isPartial) { expanded.set(result.genesisHash, result); } return result; } export function findChain (definitions: MetadataDef[], genesisHash?: string | null): Chain | null { const def = definitions.find((def) => def.genesisHash === genesisHash); return def ? metadataExpand(def) : null; } export function addMetadata (def: MetadataDef): void { definitions.set(def.genesisHash, def); } export function knownMetadata (): MetadataDef[] { return [...definitions.values()]; }