Make Metadata <-> hex encoding work (#1862)

* Make Metadata <-> hex encoding work

* Cleanup encoded, only toString exposes santized

* toHex is actual original toHex, remove override

* Update CHANGELOG

* CHANGELOG for .entries argument typing

* Update CHANGELOG.md

* Manual check type for DoubleMap

* Comment around super.toString usage

* Runtime version
This commit is contained in:
Jaco Greeff
2020-02-16 07:34:45 +01:00
committed by GitHub
parent 0822d4d8c6
commit c465252e69
9 changed files with 141 additions and 122 deletions
@@ -0,0 +1,18 @@
// Copyright 2017-2020 @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 { TypeRegistry } from '@polkadot/types';
import substrateData from './static';
import Metadata from './Metadata';
describe('Metadata', (): void => {
it('allows creation from hex with JSON equivalence', (): void => {
const test = new Metadata(new TypeRegistry(), substrateData);
expect(
new Metadata(new TypeRegistry(), test.toHex()).toJSON()
).toEqual(test.toJSON());
});
});
+27 -27
View File
@@ -9,6 +9,32 @@ import { bnToU8a, isHex, hexToU8a, u8aConcat } from '@polkadot/util';
import { MAGIC_NUMBER } from './MagicNumber';
import MetadataVersioned from './MetadataVersioned';
// 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
function decodeMetadata (registry: Registry, _value: Uint8Array | string = new Uint8Array()): MetadataVersioned {
const value = isHex(_value)
? hexToU8a(_value)
: _value;
try {
return new MetadataVersioned(registry, value);
} catch (error) {
if (error.message.indexOf('MagicNumber mismatch') === -1) {
throw error;
}
}
return new MetadataVersioned(
registry,
u8aConcat(
bnToU8a(MAGIC_NUMBER), // manually add the magic number
Uint8Array.from([0]), // add the version for the original
value // the actual data as retrieved
)
);
}
/**
* @name Metadata
* @description
@@ -16,32 +42,6 @@ import MetadataVersioned from './MetadataVersioned';
*/
export default class Metadata extends MetadataVersioned {
constructor (registry: Registry, value?: Uint8Array | string) {
super(registry, Metadata.decodeMetadata(registry, 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 (registry: Registry, _value: Uint8Array | string = new Uint8Array()): MetadataVersioned {
const value = isHex(_value)
? hexToU8a(_value)
: _value;
try {
return new MetadataVersioned(registry, value);
} catch (error) {
if (error.message.indexOf('MagicNumber mismatch') === -1) {
throw error;
}
}
return new MetadataVersioned(
registry,
u8aConcat(
bnToU8a(MAGIC_NUMBER), // manually add the magic number
Uint8Array.from([0]), // add the version for the original
value // the actual data as retrieved
)
);
super(registry, decodeMetadata(registry, value));
}
}