diff --git a/packages/types/src/ExtrinsicSignature.ts b/packages/types/src/ExtrinsicSignature.ts index db0080b6e8..4adef978a6 100644 --- a/packages/types/src/ExtrinsicSignature.ts +++ b/packages/types/src/ExtrinsicSignature.ts @@ -57,9 +57,9 @@ export default class ExtrinsicSignature extends Struct { if ((version & BIT_SIGNED) === BIT_SIGNED) { return value.subarray(1); - } else { - return {}; } + + return {}; } return value as any; diff --git a/packages/types/src/codec/Enum.spec.js b/packages/types/src/codec/Enum.spec.js index 4c8d23f125..b89cbfcc16 100644 --- a/packages/types/src/codec/Enum.spec.js +++ b/packages/types/src/codec/Enum.spec.js @@ -17,11 +17,11 @@ const testEncode = (to, expected) => }); describe('Enum', () => { - testDecode('Enum', undefined, 'foo'); testDecode('Enum', new Enum([], 1), 'bar'); testDecode('number', 0, 'foo'); testDecode('number', 1, 'bar'); + testDecode('string', 'bar', 'bar'); testDecode('Uint8Array', Uint8Array.from([0]), 'foo'); testDecode('Uint8Array', Uint8Array.from([1]), 'bar'); diff --git a/packages/types/src/codec/Enum.ts b/packages/types/src/codec/Enum.ts index 5c00f33440..0527320f1b 100644 --- a/packages/types/src/codec/Enum.ts +++ b/packages/types/src/codec/Enum.ts @@ -2,13 +2,17 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { isU8a, u8aToHex } from '@polkadot/util'; +import { isString, isU8a, u8aToHex, assert } from '@polkadot/util'; import { Codec } from '../types'; import Base from './Base'; -type EnumDef = { +type EnumMap = { [index: number]: string +}; + +type EnumDef = { + [index: string]: number } | Array; /** @@ -22,24 +26,36 @@ type EnumDef = { // TODO: // - It would be great if this could actually wrap actual TS enums export default class Enum extends Base implements Codec { - private _enum: EnumDef; + private _enum: EnumMap | Array; - constructor (def: EnumDef, value: Enum | Uint8Array | number = 0) { - super( - Enum.decodeEnum(value) - ); + constructor (def: EnumDef, value: Enum | Uint8Array | string | number = 0) { + const decoded = Enum.decodeEnum(def, value); - this._enum = def; + assert(decoded !== -1, `Unable to initialise Enum with value ${value}`); + + super(decoded); + + this._enum = Array.isArray(def) + ? def + : Object.keys(def).reduce((result, key) => { + result[def[key]] = key; + + return result; + }, {} as EnumMap); } - static decodeEnum (value: Enum | Uint8Array | number = 0): number { + static decodeEnum (def: EnumDef, value: Enum | Uint8Array | string | number): number | undefined { if (value instanceof Enum) { return value.raw; } else if (isU8a(value)) { return value[0]; - } else { - return value; + } else if (isString(value)) { + return Array.isArray(def) + ? def.indexOf(value) + : def[value] || -1; } + + return value; } /** diff --git a/packages/types/src/codec/Set.spec.ts b/packages/types/src/codec/Set.spec.ts index e6070e7046..90b49ae05b 100644 --- a/packages/types/src/codec/Set.spec.ts +++ b/packages/types/src/codec/Set.spec.ts @@ -4,14 +4,23 @@ import Set from './Set'; +const SET_FIELDS = { + header: 0b00000001, + body: 0b00000010, + receipt: 0b00000100, + messageQueue: 0b00001000, + justification: 0b00010000 +}; +const SET_ROLES = { + none: 0b00000000, + full: 0b00000001, + light: 0b00000010, + authority: 0b00000100 +}; + describe('Set', () => { it('constructs via an Array', () => { - const set = new Set({ - none: 0b00000000, - full: 0b00000001, - light: 0b00000010, - authority: 0b00000100 - }, ['full', 'authority', 'invalid']); + const set = new Set(SET_ROLES, ['full', 'authority', 'invalid']); expect(set.isEmpty).toEqual(false); expect(set.toString()).toEqual( @@ -20,17 +29,17 @@ describe('Set', () => { }); it('constructs via Uint8Array', () => { - const set = new Set({ - header: 0b00000001, - body: 0b00000010, - receipt: 0b00000100, - messageQueue: 0b00001000, - justification: 0b00010000 - }, new Uint8Array([0b00000001 | 0b00000010 | 0b00010000 | 0b10000000])); + const set = new Set(SET_FIELDS, new Uint8Array([0b00000001 | 0b00000010 | 0b00010000 | 0b10000000])); expect(set.encodedLength).toEqual(1); expect(set.toJSON()).toEqual([ 'header', 'body', 'justification' ]); }); + + it('hash a valid encoding', () => { + const set = new Set(SET_FIELDS, ['header', 'body', 'justification']); + + expect(set.toU8a()).toEqual(new Uint8Array([19])); + }); }); diff --git a/packages/types/src/codec/Set.ts b/packages/types/src/codec/Set.ts index 52e9c60715..5f4b42fed8 100644 --- a/packages/types/src/codec/Set.ts +++ b/packages/types/src/codec/Set.ts @@ -120,6 +120,6 @@ export default class Set extends Base> implements Codec { * @param isBare true when the value has none of the type-specific prefixes (internal) */ toU8a (isBare?: boolean): Uint8Array { - return new Uint8Array(this.valueEncoded); + return new Uint8Array([this.valueEncoded]); } } diff --git a/packages/types/src/codec/Tuple.ts b/packages/types/src/codec/Tuple.ts index 39d27a5ab8..5a0a8ecfa4 100644 --- a/packages/types/src/codec/Tuple.ts +++ b/packages/types/src/codec/Tuple.ts @@ -43,11 +43,11 @@ export default class Tuple< }); } else if (isHex(value)) { return Tuple.decodeTuple(Types, hexToU8a(value)); - } else { - return Types.map((Type, index) => { - return new Type(value && value[index]); - }); } + + return Types.map((Type, index) => { + return new Type(value && value[index]); + }); } static with< diff --git a/packages/types/src/codec/U8a.ts b/packages/types/src/codec/U8a.ts index 8316d5d4b1..94242a1f78 100644 --- a/packages/types/src/codec/U8a.ts +++ b/packages/types/src/codec/U8a.ts @@ -25,9 +25,9 @@ export default class U8a extends Uint8Array implements Codec { private static decodeU8a (value: any): Uint8Array { if (isU8a(value)) { return value; - } else { - return u8aToU8a(value); } + + return u8aToU8a(value); } /**