Add a Decorated class, which will be offline decorated Metadata (#1463)
* Move Metadata to api-metadata * Make tests in api-metadata pass * Make tests in api-metadata pass * Refactor fromMetadata into decorated * Fix some linting * Replace headers * Fix all tests * Small lint * Fix tests * Fix metadata docs scripts * Fix import ordering * Add line breaks * Grumbles * Fix forgotten import * rpcMetadata everywhere * Fixlint * s/api-metadata/metadata * Fix latest tests * Fix lint * Fix tests
This commit is contained in:
committed by
Jaco Greeff
parent
c87ba33893
commit
16e0ea9315
@@ -0,0 +1,44 @@
|
||||
// Copyright 2017-2019 @polkadot/metadata authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { isHex, hexToU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { MAGIC_NUMBER } from './MagicNumber';
|
||||
import MetadataVersioned from './MetadataVersioned';
|
||||
|
||||
/**
|
||||
* @name Metadata
|
||||
* @description
|
||||
* The versioned runtime metadata as a decoded structure
|
||||
*/
|
||||
export default class Metadata extends MetadataVersioned {
|
||||
constructor (value?: Uint8Array | string) {
|
||||
super(Metadata.decodeMetadata(value));
|
||||
}
|
||||
|
||||
// first we try and parse using the versioned structure, if this does fail,
|
||||
// we adjust with the magic number and a manual version and re-try. As soon as
|
||||
// we remove support for V0, we will just do a new here
|
||||
private static decodeMetadata (_value: Uint8Array | string = new Uint8Array()): MetadataVersioned {
|
||||
const value = isHex(_value)
|
||||
? hexToU8a(_value)
|
||||
: _value;
|
||||
|
||||
try {
|
||||
return new MetadataVersioned(value);
|
||||
} catch (error) {
|
||||
if (error.message.indexOf('MagicNumber mismatch') === -1) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return new MetadataVersioned(
|
||||
u8aConcat(
|
||||
MAGIC_NUMBER.toU8a(), // manually add the magic number
|
||||
Uint8Array.from([0]), // add the version for the original
|
||||
value // the actual data as retrieved
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user