decode method

DecodedMetadata decode(
  1. String metadataHex
)

Decode metadata from Input

Implementation

DecodedMetadata decode(String metadataHex) {
  //
  // Create input from Hexa-deciaml String
  final source = Input.fromHex(metadataHex);

  //
  // Decode the magic number
  final magic = U32Codec.codec.decode(source);

  //
  // assert that the magic number is 0x6174656d
  assertion(magic == 0x6174656d,
      'Expected magic number 0x6174656d, but got $magic');

  // decode the version information
  final version = U8Codec.codec.decode(source);
  assertion(9 <= version,
      'Expected version greater then 9, but got $version. Versions below 9 are not supported by this lib');
  assertion(15 > version,
      'Expected version less then 15, but got $version. Versions above 15 are not supported by this lib');

  // Kusama Hack :o
  // See https://github.com/polkadot-js/api/commit/a9211690be6b68ad6c6dad7852f1665cadcfa5b2
  // for why try-catch and version decoding stuff is here
  try {
    final metadata = ScaleCodec(RegistryCreator.instance[version])
        .decode('MetadataV$version', source);

    return DecodedMetadata(metadata: metadata, version: version);
  } catch (e) {
    if (version != 9) {
      rethrow;
    }
    try {
      final clonnedSource = Input.fromHex(metadataHex);

      U32Codec.codec.decode(clonnedSource);
      U8Codec.codec.decode(clonnedSource);

      final metadata = ScaleCodec(RegistryCreator.instance[10])
          .decode('MetadataV10', clonnedSource);
      return DecodedMetadata(metadata: metadata, version: 10);
    } catch (unknownError) {
      rethrow;
    }
  }
}