94 lines
2.5 KiB
TypeScript
94 lines
2.5 KiB
TypeScript
// Copyright 2019-2026 @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 { hexToU8a } from '@polkadot/util';
|
|
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 <url>`
|
|
|
|
const definitions = new Map<string, MetadataDef>(
|
|
// [kusama].map((def) => [def.genesisHash, def])
|
|
);
|
|
|
|
const expanded = new Map<string, Chain>();
|
|
|
|
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, rawMetadata, 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 metadataBytes = metaCalls
|
|
? base64Decode(metaCalls)
|
|
: rawMetadata
|
|
? hexToU8a(rawMetadata)
|
|
: null;
|
|
const hasMetadata = !!metadataBytes && !isPartial;
|
|
|
|
if (hasMetadata) {
|
|
registry.setMetadata(new Metadata(registry, metadataBytes), 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()];
|
|
}
|