Fix Substrate key generation (#3968)

* Fix Substrate key generation

* 6.0.2
This commit is contained in:
Jaco
2021-09-18 10:56:35 +02:00
committed by GitHub
parent 0865b91f88
commit caec49a8dc
15 changed files with 137 additions and 65 deletions
@@ -1,13 +1,19 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import metadataStatic from '@polkadot/types-support/metadata/static-substrate';
import { compactAddLength, u8aToU8a } from '@polkadot/util';
import { TypeRegistry } from '../../../create';
import { Metadata } from '../../Metadata';
import { getStorage } from './getStorage';
const registry = new TypeRegistry();
const metadata = new Metadata(registry, metadataStatic);
registry.setMetadata(metadata);
describe('getSorage', (): void => {
const registry = new TypeRegistry();
const storage = getStorage(registry);
it('should return well known keys', (): void => {
@@ -1,19 +1,34 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import metadataStatic from '@polkadot/types-support/metadata/static-substrate';
import { compactAddLength, u8aToU8a } from '@polkadot/util';
import { TypeRegistry } from '../../../create';
import { Metadata } from '../../Metadata';
import { substrate } from './substrate';
const registry = new TypeRegistry();
const metadata = new Metadata(registry, metadataStatic);
registry.setMetadata(metadata);
// console.log(JSON.stringify(registry.lookup.types));
describe('substrate', (): void => {
const code = substrate.code(registry);
it('creates a well-known :code key', (): void => {
expect(
substrate.code(registry)()
code()
).toEqual(
compactAddLength(u8aToU8a(':code'))
);
});
it('has the correct metadata', (): void => {
expect(
code.meta.type.isPlain
).toEqual(true);
});
});
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { StorageEntryMetadataLatest } from '../../../interfaces';
import type { PortableType } from '../../../interfaces';
import type { StorageEntry } from '../../../primitive/types';
import type { Registry } from '../../../types';
@@ -14,18 +14,60 @@ interface SubstrateMetadata {
type Creator = (registry: Registry) => StorageEntry;
function findSiPrimitive (registry: Registry, _prim: string): PortableType | undefined {
const prim = _prim.toLowerCase();
const portable = registry.lookup.types.find((t) =>
(
t.type.def.isPrimitive &&
t.type.def.asPrimitive.toString().toLowerCase() === prim
) || (
t.type.def.isHistoricMetaCompat &&
t.type.def.asHistoricMetaCompat.toString().toLowerCase() === prim
)
);
return portable;
}
function findSiType (registry: Registry, orig: string): PortableType | undefined {
let portable = findSiPrimitive(registry, orig);
if (!portable && orig === 'Bytes') {
const u8 = findSiPrimitive(registry, 'u8');
if (u8) {
portable = registry.lookup.types.find((t) =>
(
t.type.def.isSequence &&
t.type.def.asSequence.type.eq(u8.id)
) || (
t.type.def.isHistoricMetaCompat &&
t.type.def.asHistoricMetaCompat.eq(orig)
)
);
}
}
if (!portable) {
console.warn(`Unable to map ${orig} to a lookup index`);
}
return portable;
}
// Small helper function to factorize code on this page.
/** @internal */
function createRuntimeFunction (method: string, key: string, { docs, type }: SubstrateMetadata): (registry: Registry) => StorageEntry {
return (registry: Registry): StorageEntry =>
createFunction(registry, {
meta: {
meta: registry.createType('StorageEntryMetadataLatest', {
docs: registry.createType('Vec<Text>', [docs]),
modifier: registry.createType('StorageEntryModifierLatest', 1), // required
modifier: registry.createType('StorageEntryModifierLatest', 'Required'),
name: registry.createType('Text', method),
toJSON: (): any => key,
type: registry.createType('StorageEntryTypeLatest', type, 0)
} as StorageEntryMetadataLatest,
type: registry.createType('StorageEntryTypeLatest', { Plain: findSiType(registry, type)?.id || 0 })
}),
method,
prefix: 'Substrate',
section: 'substrate'