From 6e18bb3b1d01806d0fd5120990a8a608b64e7bd9 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 29 Apr 2021 15:07:32 +0300 Subject: [PATCH] Allow Metadata instances from JSON or Map (#3459) * assert isU8a & isHex inputs on Metadata * Remove _ in param * Allow hex parsing * CHANGELOG --- CHANGELOG.md | 1 + packages/metadata/src/Metadata.ts | 50 ++++++++++++++++--------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19f3ce7607..7081f7c5c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Changes: - Adjust xcm types as per latest, e.g. `XcmError` - Fix generate of well-known storage keys (e.g. `:code`) - Optimize metadata unknown type validation +- Allow metadata creation for `Map` or `Record` ## 4.7.2 Apr 26, 2021 diff --git a/packages/metadata/src/Metadata.ts b/packages/metadata/src/Metadata.ts index d48fb6a397..147239c47c 100644 --- a/packages/metadata/src/Metadata.ts +++ b/packages/metadata/src/Metadata.ts @@ -3,7 +3,7 @@ import type { Registry } from '@polkadot/types/types'; -import { isString, u8aToU8a } from '@polkadot/util'; +import { isHex, isU8a, u8aToU8a } from '@polkadot/util'; import { MetadataVersioned } from './MetadataVersioned'; @@ -14,34 +14,36 @@ const VERSION_IDX = 4; const EMPTY_METADATA = new Uint8Array([0x6d, 0x65, 0x74, 0x61, 9]); const EMPTY_U8A = new Uint8Array(); -function sanitizeInput (_value: Uint8Array | string = EMPTY_U8A): Uint8Array { - if (isString(_value)) { - return sanitizeInput(u8aToU8a(_value)); - } - - return _value.length === 0 - ? EMPTY_METADATA - : _value; +function toU8a (value: Uint8Array | string = EMPTY_U8A): Uint8Array { + return isHex(value) + ? toU8a(u8aToU8a(value)) + : isU8a(value) && value.length === 0 + ? EMPTY_METADATA + : value; } -function decodeMetadata (registry: Registry, _value?: Uint8Array | string): MetadataVersioned { - const value = sanitizeInput(_value); - const version = value[VERSION_IDX]; +function decodeMetadata (registry: Registry, _value?: Uint8Array | string | Map | Record): MetadataVersioned { + if (!_value || isU8a(_value) || isHex(_value)) { + const value = toU8a(_value); + const version = value[VERSION_IDX]; - try { - return new MetadataVersioned(registry, value); - } catch (error) { - // This is an f-ing hack as a follow-up to another ugly hack - // https://github.com/polkadot-js/api/commit/a9211690be6b68ad6c6dad7852f1665cadcfa5b2 - // when we fail on V9, try to re-parse it as v10... yes... HACK - if (version === 9) { - value[VERSION_IDX] = 10; + try { + return new MetadataVersioned(registry, value); + } catch (error) { + // This is an f-ing hack as a follow-up to another ugly hack + // https://github.com/polkadot-js/api/commit/a9211690be6b68ad6c6dad7852f1665cadcfa5b2 + // when we fail on V9, try to re-parse it as v10... yes... HACK + if (version === 9) { + value[VERSION_IDX] = 10; - return decodeMetadata(registry, value); + return decodeMetadata(registry, value); + } + + throw error; } - - throw error; } + + return new MetadataVersioned(registry, _value); } /** @@ -50,7 +52,7 @@ function decodeMetadata (registry: Registry, _value?: Uint8Array | string): Meta * The versioned runtime metadata as a decoded structure */ export class Metadata extends MetadataVersioned { - constructor (registry: Registry, value?: Uint8Array | string) { + constructor (registry: Registry, value?: Uint8Array | string | Map | Record) { super(registry, decodeMetadata(registry, value)); } }