* Added code to support custom signed extension/user extension * fix lint issues * added test * as per review comment * fix lint * as per review suggestion * fix lint * as per review comments * fix lint
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
// Copyright 2019-2021 @polkadot/extension-chains authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { MetadataDef } from '@polkadot/extension-inject/types';
|
|
import type { Chain } from './types';
|
|
|
|
import { Metadata } from '@polkadot/metadata';
|
|
import { TypeRegistry } from '@polkadot/types';
|
|
import { base64Decode } from '@polkadot/util-crypto';
|
|
|
|
// 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, specVersion, ss58Format, tokenDecimals, tokenSymbol, types, userExtensions } = definition;
|
|
const registry = new TypeRegistry();
|
|
|
|
if (!isPartial) {
|
|
registry.register(types);
|
|
}
|
|
|
|
registry.setChainProperties(registry.createType('ChainProperties', {
|
|
ss58Format,
|
|
tokenDecimals,
|
|
tokenSymbol
|
|
}));
|
|
|
|
const isUnknown = genesisHash === '0x';
|
|
let hasMetadata = false;
|
|
|
|
if (metaCalls && !isPartial) {
|
|
hasMetadata = true;
|
|
|
|
const metadata = new Metadata(registry, base64Decode(metaCalls));
|
|
const signedExtensions = metadata.asLatest.extrinsic.signedExtensions.toJSON() as string[];
|
|
|
|
registry.setMetadata(metadata, signedExtensions, userExtensions);
|
|
}
|
|
|
|
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()];
|
|
}
|