Skip to content

Commit 8dc0f04

Browse files
authored
Small decoding cleanups (#4823)
* Small decoding cleanups * isHex * Adjust struct * Additional
1 parent 6159270 commit 8dc0f04

File tree

13 files changed

+63
-84
lines changed

13 files changed

+63
-84
lines changed

packages/types-codec/src/abstract/AbstractInt.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ function toPercentage (value: BN, divisor: BN): string {
2525
}
2626

2727
/** @internal */
28-
function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegative: boolean): string {
29-
if (isU8a(value)) {
30-
return u8aToBn(value.subarray(0, bitLength / 8), { isLe: true, isNegative }).toString();
31-
} else if (isBn(value)) {
28+
function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array>, isNegative: boolean): string {
29+
if (isBn(value)) {
3230
return value.toString();
3331
} else if (isHex(value, -1, true)) {
3432
return hexToBn(value, { isLe: false, isNegative }).toString();
@@ -65,7 +63,7 @@ export abstract class AbstractInt extends BN implements INumber {
6563
// shortcut isU8a as used in SCALE decoding
6664
isU8a(value)
6765
? u8aToBn(value.subarray(0, bitLength / 8), { isLe: true, isNegative: isSigned }).toString()
68-
: decodeAbstractInt(value, bitLength, isSigned)
66+
: decodeAbstractInt(value, isSigned)
6967
);
7068

7169
this.registry = registry;

packages/types-codec/src/base/Enum.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import type { HexString } from '@polkadot/util/types';
55
import type { AnyJson, Codec, CodecClass, IEnum, Inspect, IU8a, Registry } from '../types';
66

7-
import { assert, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
7+
import { assert, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
88

99
import { mapToTypeMap, typesToMap } from '../utils';
1010
import { Null } from './Null';
@@ -88,7 +88,7 @@ function createFromValue (registry: Registry, def: TypesDef, index = 0, value?:
8888

8989
return {
9090
index,
91-
value: value instanceof entry.Type
91+
value: isCodec(value) && value instanceof entry.Type
9292
? value
9393
: new entry.Type(registry, value)
9494
};
@@ -266,7 +266,7 @@ export class Enum implements IEnum {
266266
* @deprecated use isNone
267267
*/
268268
public get isNull (): boolean {
269-
return this.#raw instanceof Null;
269+
return this.isNone;
270270
}
271271

272272
/**
@@ -310,7 +310,7 @@ export class Enum implements IEnum {
310310
return this.type === other;
311311
} else if (isHex(other)) {
312312
return this.toHex() === other;
313-
} else if (other instanceof Enum) {
313+
} else if (isCodec(other) && other instanceof Enum) {
314314
return this.index === other.index && this.value.eq(other.value);
315315
} else if (isObject(other)) {
316316
return this.value.eq(other[this.type]);
@@ -404,7 +404,7 @@ export class Enum implements IEnum {
404404
* @description Returns the string representation of the value
405405
*/
406406
public toString (): string {
407-
return this.isNull
407+
return this.isNone
408408
? this.type
409409
: stringify(this.toJSON());
410410
}

packages/types-codec/src/base/Tuple.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type TupleTypes = TupleType[] | {
1919
};
2020

2121
/** @internal */
22-
function decodeTuple (registry: Registry, Classes: TupleCodecClasss, value?: AnyTupleValue): [Codec[], number] {
22+
function decodeTuple (registry: Registry, Classes: TupleCodecClasss, value?: Exclude<AnyTupleValue, Uint8Array>): [Codec[], number] {
2323
if (isU8a(value) || isHex(value)) {
2424
return decodeU8a(registry, u8aToU8a(value), Classes);
2525
}

packages/types-codec/src/base/VecFixed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { decodeU8aVec, typeToConstructor } from '../utils';
1111
import { decodeVec } from './Vec';
1212

1313
/** @internal */
14-
function decodeVecFixed<T extends Codec> (registry: Registry, value: Uint8Array | HexString | unknown[], Type: CodecClass<T>, length: number): [T[], number, number] {
14+
function decodeVecFixed<T extends Codec> (registry: Registry, value: HexString | unknown[], Type: CodecClass<T>, length: number): [T[], number, number] {
1515
const [values, decodedLength, decodedLengthNoOffset] = decodeVec(registry, Type, value, length);
1616

1717
while (values.length < length) {

packages/types-codec/src/extended/BTreeSet.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from '../utils';
1111
const l = logger('BTreeSet');
1212

1313
/** @internal */
14-
function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: CodecClass<V>, u8a: Uint8Array): [Set<V>, number] {
14+
function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: CodecClass<V>, u8a: Uint8Array): [CodecClass<V>, Set<V>, number] {
1515
const output = new Set<V>();
1616
const [offset, length] = compactFromU8a(u8a);
1717
const [values, decodedLength] = decodeU8aVec(registry, u8a, offset, ValClass, length.toNumber());
@@ -20,11 +20,11 @@ function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: CodecC
2020
output.add(values[i]);
2121
}
2222

23-
return [output, decodedLength];
23+
return [ValClass, output, decodedLength];
2424
}
2525

2626
/** @internal */
27-
function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: CodecClass<V>, value: Set<any> | string[]): [Set<V>, number] {
27+
function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: CodecClass<V>, value: Set<any> | string[]): [CodecClass<V>, Set<V>, number] {
2828
const output = new Set<V>();
2929

3030
value.forEach((val: any) => {
@@ -37,7 +37,7 @@ function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: CodecC
3737
}
3838
});
3939

40-
return [output, 0];
40+
return [ValClass, output, 0];
4141
}
4242

4343
/**
@@ -54,14 +54,12 @@ function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: CodecC
5454
* @param jsonSet
5555
* @internal
5656
*/
57-
function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V> | string, value?: Uint8Array | string | string[] | Set<any>): [Set<V>, number] {
58-
if (!value) {
59-
return [new Set<V>(), 0];
60-
}
61-
57+
function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V> | string, value?: Uint8Array | string | string[] | Set<any>): [CodecClass<V>, Set<V>, number] {
6258
const ValClass = typeToConstructor(registry, valType);
6359

64-
if (isU8a(value) || isHex(value)) {
60+
if (!value) {
61+
return [ValClass, new Set<V>(), 0];
62+
} else if (isU8a(value) || isHex(value)) {
6563
return decodeSetFromU8a<V>(registry, ValClass, u8aToU8a(value));
6664
} else if (Array.isArray(value) || value instanceof Set) {
6765
return decodeSetFromSet<V>(registry, ValClass, value);
@@ -80,13 +78,13 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
8078
readonly #ValClass: CodecClass<V>;
8179

8280
constructor (registry: Registry, valType: CodecClass<V> | string, rawValue?: Uint8Array | string | string[] | Set<any>) {
83-
const [values, decodedLength] = decodeSet(registry, valType, rawValue);
81+
const [ValClass, values, decodedLength] = decodeSet(registry, valType, rawValue);
8482

8583
super(sortSet(values));
8684

8785
this.registry = registry;
8886
this.initialU8aLength = decodedLength;
89-
this.#ValClass = typeToConstructor(registry, valType);
87+
this.#ValClass = ValClass;
9088
}
9189

9290
public static with<V extends Codec> (valType: CodecClass<V> | string): CodecClass<BTreeSet<V>> {

packages/types-codec/src/extended/Bytes.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import type { AnyU8a, Inspect, Registry } from '../types';
55

6-
import { assert, compactAddLength, compactFromU8a, compactToU8a, isString, isU8a, u8aToU8a } from '@polkadot/util';
6+
import { assert, compactAddLength, compactFromU8a, compactToU8a, isCodec, isString, isU8a, u8aToU8a } from '@polkadot/util';
77

88
import { Raw } from '../native/Raw';
99

@@ -26,19 +26,6 @@ function decodeBytesU8a (value: Uint8Array): [Uint8Array, number] {
2626
return [value.subarray(offset, total), total];
2727
}
2828

29-
/** @internal */
30-
function decodeBytes (value?: AnyU8a): [Uint8Array | undefined, number] {
31-
if (Array.isArray(value) || isString(value)) {
32-
return [u8aToU8a(value), 0];
33-
} else if (!(value instanceof Raw) && isU8a(value)) {
34-
// We are ensuring we are not a Raw instance. In the case of a Raw we already have gotten
35-
// rid of the length, i.e. new Bytes(new Bytes(...)) will work as expected
36-
return decodeBytesU8a(value);
37-
}
38-
39-
return [value, 0];
40-
}
41-
4229
/**
4330
* @name Bytes
4431
* @description
@@ -48,7 +35,11 @@ function decodeBytes (value?: AnyU8a): [Uint8Array | undefined, number] {
4835
*/
4936
export class Bytes extends Raw {
5037
constructor (registry: Registry, value?: AnyU8a) {
51-
const [u8a, decodedLength] = decodeBytes(value);
38+
const [u8a, decodedLength] = isU8a(value) && !(isCodec(value) && value instanceof Raw)
39+
? decodeBytesU8a(value)
40+
: Array.isArray(value) || isString(value)
41+
? [u8aToU8a(value), 0]
42+
: [value, 0];
5243

5344
super(registry, u8a, decodedLength);
5445
}

packages/types-codec/src/extended/Map.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { compareMap, decodeU8a, sortMap, typeToConstructor } from '../utils';
1414
const l = logger('Map');
1515

1616
/** @internal */
17-
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, u8a: Uint8Array): [Map<K, V>, number] {
17+
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, u8a: Uint8Array): [CodecClass<K>, CodecClass<V>, Map<K, V>, number] {
1818
const output = new Map<K, V>();
1919
const [offset, length] = compactFromU8a(u8a);
2020
const count = length.toNumber();
@@ -30,11 +30,11 @@ function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry,
3030
output.set(values[i] as K, values[i + 1] as V);
3131
}
3232

33-
return [output, offset + decodedLength];
33+
return [KeyClass, ValClass, output, offset + decodedLength];
3434
}
3535

3636
/** @internal */
37-
function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, value: Map<any, any>): [Map<K, V>, number] {
37+
function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, value: Map<any, any>): [CodecClass<K>, CodecClass<V>, Map<K, V>, number] {
3838
const output = new Map<K, V>();
3939

4040
for (const [key, val] of value.entries()) {
@@ -58,7 +58,7 @@ function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry,
5858
}
5959
}
6060

61-
return [output, 0];
61+
return [KeyClass, ValClass, output, 0];
6262
}
6363

6464
/**
@@ -76,12 +76,12 @@ function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry,
7676
* @param jsonMap
7777
* @internal
7878
*/
79-
function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, value?: Uint8Array | string | Map<any, any>): [Map<K, V>, number] {
79+
function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, value?: Uint8Array | string | Map<any, any>): [CodecClass<K>, CodecClass<V>, Map<K, V>, number] {
8080
const KeyClass = typeToConstructor(registry, keyType);
8181
const ValClass = typeToConstructor(registry, valType);
8282

8383
if (!value) {
84-
return [new Map<K, V>(), 0];
84+
return [KeyClass, ValClass, new Map<K, V>(), 0];
8585
} else if (isU8a(value) || isHex(value)) {
8686
return decodeMapFromU8a<K, V>(registry, KeyClass, ValClass, u8aToU8a(value));
8787
} else if (value instanceof Map) {
@@ -107,14 +107,14 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
107107
readonly #type: string;
108108

109109
constructor (registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
110-
const [decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
110+
const [KeyClass, ValClass, decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
111111

112112
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
113113

114114
this.registry = registry;
115115
this.initialU8aLength = decodedLength;
116-
this.#KeyClass = typeToConstructor(registry, keyType);
117-
this.#ValClass = typeToConstructor(registry, valType);
116+
this.#KeyClass = KeyClass;
117+
this.#ValClass = ValClass;
118118
this.#type = type;
119119
}
120120

packages/types-codec/src/native/Bool.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ import type { Codec, Inspect, IU8a, Registry } from '../types';
66

77
import { isU8a, u8aToHex } from '@polkadot/util';
88

9-
/** @internal */
10-
function decodeBool (value: any): boolean {
11-
if (isU8a(value)) {
12-
return value[0] === 1;
13-
} else if (value instanceof Boolean) {
14-
return value.valueOf();
15-
}
16-
17-
return !!value;
18-
}
19-
209
/**
2110
* @name bool
2211
* @description
@@ -30,7 +19,13 @@ export class bool extends Boolean implements Codec {
3019

3120
// eslint-disable-next-line @typescript-eslint/ban-types
3221
constructor (registry: Registry, value: bool | Boolean | Uint8Array | boolean | number = false) {
33-
super(decodeBool(value));
22+
super(
23+
isU8a(value)
24+
? value[0] === 1
25+
: value instanceof Boolean
26+
? value.valueOf()
27+
: !!value
28+
);
3429

3530
this.registry = registry;
3631
}

packages/types-codec/src/native/Date.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const BITLENGTH: UIntBitLength = 64;
1010

1111
function decodeDate (value: CodecDate | Date | AnyNumber): Date {
1212
if (isU8a(value)) {
13-
value = u8aToBn(value.subarray(0, BITLENGTH / 8), true);
13+
value = u8aToBn(value.subarray(0, BITLENGTH / 8));
1414
} else if (value instanceof Date) {
1515
return value;
1616
} else if (isString(value)) {

packages/types-codec/src/native/Set.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ export class CodecSet extends Set<string> implements ISet<string> {
111111
}
112112

113113
return class extends CodecSet {
114-
constructor (registry: Registry, value?: unknown) {
115-
super(registry, values, value as undefined, bitLength);
114+
constructor (registry: Registry, value?: string[] | Set<string> | Uint8Array | BN | number | string) {
115+
super(registry, values, value, bitLength);
116116

117117
objectProperties(this, isKeys, (_, i) => this.strings.includes(keys[i]));
118118
}

0 commit comments

Comments
 (0)