diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index 8b05c1a28e..b32221271a 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -5,6 +5,7 @@ import { hexToU8a, isHex, isObject, isU8a, u8aConcat, u8aToHex } from '@polkadot/util'; import { Codec, Constructor, ConstructorDef } from '../types'; +import decodeU8a from './utils/decodeU8a'; /** * @name Struct @@ -65,15 +66,24 @@ export default class Struct< S extends ConstructorDef, _, T extends { [K in keyof S]: Codec } - > (Types: S, value: any, jsonMap: Map): T { + > (Types: S, value: any, jsonMap: Map): T { // l.debug(() => ['Struct.decode', { Types, value }]); if (isHex(value)) { return Struct.decodeStruct(Types, hexToU8a(value as string), jsonMap); + } else if (isU8a(value)) { + const values = decodeU8a(value, Object.values(Types)); + + // Transform array of values to {key: value} mapping + return Object.keys(Types).reduce((raw: T, key: keyof S, index) => { + raw[key] = values[index]; + return raw; + }, {} as T); } else if (!value) { return {} as T; } + // We assume from here that value is a JS object (Array, Map, Object) return Struct.decodeStructFromObject(Types, value, jsonMap); } @@ -81,22 +91,13 @@ export default class Struct< S extends ConstructorDef, _, T extends { [K in keyof S]: Codec } - > (Types: S, value: any, jsonMap: Map): T { - // `currentIndex` is only used when we have a UintArray/U8a as value. It's - // used to track at which index we are currently parsing in that array. - let currentIndex = 0; - + > (Types: S, value: any, jsonMap: Map): T { return Object.keys(Types).reduce((raw: T, key: keyof S, index) => { // The key in the JSON can be snake_case (or other cases), but in our // Types, result or any other maps, it's camelCase const jsonKey = (jsonMap.get(key as any) && !value[key]) ? jsonMap.get(key as any) : key; - if (isU8a(value)) { - raw[key] = new Types[key](value.subarray(currentIndex)); - - // Move the currentIndex forward - currentIndex += raw[key].encodedLength; - } else if (Array.isArray(value)) { + if (Array.isArray(value)) { raw[key] = value[index] instanceof Types[key] ? value[index] : new Types[key](value[index]); @@ -120,7 +121,7 @@ export default class Struct< static with< S extends ConstructorDef - > (Types: S): Constructor> { + > (Types: S): Constructor> { return class extends Struct { constructor (value?: any, jsonMap?: Map) { super(Types, value, jsonMap); diff --git a/packages/types/src/codec/Tuple.ts b/packages/types/src/codec/Tuple.ts index 5a0a8ecfa4..cbd475a901 100644 --- a/packages/types/src/codec/Tuple.ts +++ b/packages/types/src/codec/Tuple.ts @@ -5,6 +5,7 @@ import { isU8a, u8aConcat, u8aToHex, isHex, hexToU8a } from '@polkadot/util'; import { AnyNumber, AnyU8a, AnyString, Codec, Constructor } from '../types'; +import decodeU8a from './utils/decodeU8a'; /** * @name Tuple @@ -27,20 +28,9 @@ export default class Tuple< private static decodeTuple< S extends Array - > (Types: S, value: AnyU8a | string | Array): Array { + > (Types: S, value: AnyU8a | string | Array): Array { if (isU8a(value)) { - // `currentIndex` is only used when we have a UintArray/U8a as value. It's - // used to track at which index we are currently parsing in the U8a. - let currentIndex = 0; - - return Types - .map((Type) => { - const raw = new Type(value.subarray(currentIndex)); - // Move the currentIndex forward - currentIndex += raw.encodedLength; - - return raw; - }); + return decodeU8a(value, Types); } else if (isHex(value)) { return Tuple.decodeTuple(Types, hexToU8a(value)); } @@ -52,7 +42,7 @@ export default class Tuple< static with< S extends Array - > (Types: S): Constructor> { + > (Types: S): Constructor> { return class extends Tuple { constructor (value?: any) { super(Types, value); diff --git a/packages/types/src/codec/Vector.ts b/packages/types/src/codec/Vector.ts index 03ef4c50ad..ce8692e63e 100644 --- a/packages/types/src/codec/Vector.ts +++ b/packages/types/src/codec/Vector.ts @@ -6,6 +6,7 @@ import { u8aConcat, u8aToU8a, u8aToHex } from '@polkadot/util'; import Compact from './Compact'; import { Codec, Constructor } from '../types'; +import decodeU8a from './utils/decodeU8a'; /** * @name Vector @@ -42,16 +43,7 @@ export default class Vector< let [offset, _length] = Compact.decodeU8a(u8a); const length = _length.toNumber(); - const result = []; - - for (let index = 0; index < length; index++) { - const decoded = new Type(u8a.subarray(offset)); - - result.push(decoded); - offset += decoded.encodedLength; - } - - return result; + return decodeU8a(u8a.subarray(offset), new Array(length).fill(Type)) as T[]; } static with (Type: Constructor): Constructor> { diff --git a/packages/types/src/codec/utils/decodeU8a.ts b/packages/types/src/codec/utils/decodeU8a.ts new file mode 100644 index 0000000000..407a00d6b6 --- /dev/null +++ b/packages/types/src/codec/utils/decodeU8a.ts @@ -0,0 +1,23 @@ +// Copyright 2017-2018 @polkadot/types 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 { Codec, Constructor } from '../../types'; + +/** + * Given an u8a, and an array of Type constructors, decode the u8a against the + * types, and return an array of decoded values. + * + * @param u8a - The u8a to decode. + * @param types - The array of Constructor to decode the U8a against. + */ +export default function decodeU8a (u8a: Uint8Array, types: Constructor[]): Codec[] { + if (!types.length) { + return []; + } + + const Type = types[0]; + const value = new Type(u8a); + + return [value].concat(decodeU8a(u8a.subarray(value.encodedLength), types.slice(1))); +}