UNPKG

1.23 kBJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/types authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { isString, isU8a, u8aToU8a } from '@polkadot/util';
4import { MetadataVersioned } from "./MetadataVersioned.js"; // magic u32 preceding the version id
5
6const VERSION_IDX = 4; // magic + lowest supported version
7
8const EMPTY_METADATA = new Uint8Array([0x6d, 0x65, 0x74, 0x61, 9]);
9
10function decodeU8a(registry, value) {
11 const u8a = value.length === 0 ? EMPTY_METADATA : value; // This is an f-ing hack as a follow-up to another ugly hack
12 // https://github.com/polkadot-js/api/commit/a9211690be6b68ad6c6dad7852f1665cadcfa5b2
13 // when we fail on V9, try to re-parse it as v10...
14
15 if (u8a[VERSION_IDX] === 9) {
16 try {
17 return new MetadataVersioned(registry, u8a);
18 } catch (error) {
19 u8a[VERSION_IDX] = 10;
20 return u8a;
21 }
22 }
23
24 return u8a;
25}
26/**
27 * @name Metadata
28 * @description
29 * The versioned runtime metadata as a decoded structure
30 */
31
32
33export class Metadata extends MetadataVersioned {
34 constructor(registry, value) {
35 // console.time('Metadata')
36 super(registry, isU8a(value) || isString(value) ? decodeU8a(registry, u8aToU8a(value)) : value); // console.timeEnd('Metadata')
37 }
38
39}
\No newline at end of file