Small decoding cleanups (#4823)
* Small decoding cleanups * isHex * Adjust struct * Additional
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import type { Registry } from '@polkadot/types-codec/types';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
|
||||
import { isHex, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
import { isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { MetadataVersioned } from './MetadataVersioned';
|
||||
|
||||
@@ -14,25 +14,25 @@ const VERSION_IDX = 4;
|
||||
// magic + lowest supported version
|
||||
const EMPTY_METADATA = new Uint8Array([0x6d, 0x65, 0x74, 0x61, 9]);
|
||||
|
||||
function decodeU8a (registry: Registry, value: Uint8Array): MetadataVersioned {
|
||||
function decodeU8a (registry: Registry, value: Uint8Array): MetadataVersioned | Uint8Array {
|
||||
const u8a = value.length === 0
|
||||
? EMPTY_METADATA
|
||||
: value;
|
||||
|
||||
try {
|
||||
return new MetadataVersioned(registry, u8a);
|
||||
} 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 (u8a[VERSION_IDX] === 9) {
|
||||
// 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...
|
||||
if (u8a[VERSION_IDX] === 9) {
|
||||
try {
|
||||
return new MetadataVersioned(registry, u8a);
|
||||
} catch (error) {
|
||||
u8a[VERSION_IDX] = 10;
|
||||
|
||||
return decodeU8a(registry, u8a);
|
||||
return u8a;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
return u8a;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,11 +46,9 @@ export class Metadata extends MetadataVersioned {
|
||||
|
||||
super(
|
||||
registry,
|
||||
isU8a(value)
|
||||
? decodeU8a(registry, value)
|
||||
: isHex(value)
|
||||
? decodeU8a(registry, u8aToU8a(value))
|
||||
: new MetadataVersioned(registry, value)
|
||||
isU8a(value) || isString(value)
|
||||
? decodeU8a(registry, u8aToU8a(value))
|
||||
: value
|
||||
);
|
||||
|
||||
// console.timeEnd('Metadata')
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyJson } from '@polkadot/types-codec/types';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { MetadataAll, MetadataLatest, MetadataV9, MetadataV10, MetadataV11, MetadataV12, MetadataV13, MetadataV14 } from '../interfaces/metadata';
|
||||
import type { Registry } from '../types';
|
||||
|
||||
@@ -31,13 +32,13 @@ const LATEST_VERSION = 14;
|
||||
export class MetadataVersioned extends Struct {
|
||||
readonly #converted = new Map<MetaVersions, MetaMapped>();
|
||||
|
||||
constructor (registry: Registry, value?: unknown) {
|
||||
constructor (registry: Registry, value?: Uint8Array | HexString | Map<string, unknown> | Record<string, unknown>) {
|
||||
// console.time('MetadataVersioned')
|
||||
|
||||
super(registry, {
|
||||
magicNumber: MagicNumber,
|
||||
metadata: 'MetadataAll'
|
||||
}, value as Map<unknown, unknown>);
|
||||
}, value);
|
||||
|
||||
// console.timeEnd('MetadataVersioned')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user