// Copyright 2019-2025 @polkadot/extension-chains authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { MetadataDef } from '@polkadot/extension-inject/types'; import type { ChainProperties } from '@polkadot/types/interfaces'; import type { Chain } from './types.js'; import { Metadata, TypeRegistry } from '@polkadot/types'; import { base64Decode } from '@polkadot/util-crypto'; export { packageInfo } from './packageInfo.js'; // imports chain details, generally metadata. For the generation of these, // inside the api, run `yarn chain:info --ws ` 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, userExtensions } = definition; const registry = new TypeRegistry(); if (!isPartial) { registry.register(types); } registry.setChainProperties(registry.createType('ChainProperties', { ss58Format, tokenDecimals, tokenSymbol }) as unknown as ChainProperties); const hasMetadata = !!metaCalls && !isPartial; if (hasMetadata) { registry.setMetadata(new Metadata(registry, base64Decode(metaCalls)), undefined, userExtensions); } const isUnknown = genesisHash === '0x'; const result = { definition, genesisHash: isUnknown ? undefined : genesisHash, hasMetadata, 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()]; }