@polkadot/types-codec 8.5.2-1 → 8.5.2-10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/abstract/AbstractArray.d.ts +1 -2
- package/abstract/AbstractArray.js +4 -10
- package/base/Compact.d.ts +7 -2
- package/base/Compact.js +18 -3
- package/base/Enum.d.ts +16 -1
- package/base/Enum.js +29 -15
- package/base/Option.d.ts +7 -2
- package/base/Option.js +20 -3
- package/base/Tuple.d.ts +7 -1
- package/base/Tuple.js +36 -22
- package/base/Vec.d.ts +8 -2
- package/base/Vec.js +39 -22
- package/base/VecFixed.d.ts +7 -1
- package/base/VecFixed.js +20 -18
- package/cjs/abstract/AbstractArray.js +3 -9
- package/cjs/base/Compact.js +18 -3
- package/cjs/base/Enum.js +27 -12
- package/cjs/base/Option.js +20 -2
- package/cjs/base/Tuple.js +35 -21
- package/cjs/base/Vec.js +36 -21
- package/cjs/base/VecFixed.js +19 -17
- package/cjs/extended/BTreeSet.js +6 -5
- package/cjs/extended/BitVec.js +4 -4
- package/cjs/extended/Bytes.js +5 -5
- package/cjs/extended/Map.js +3 -4
- package/cjs/extended/OptionBool.js +99 -0
- package/cjs/extended/index.js +8 -0
- package/cjs/native/Struct.js +33 -19
- package/cjs/native/Text.js +3 -3
- package/cjs/packageInfo.js +1 -1
- package/cjs/utils/decodeU8a.js +8 -8
- package/cjs/utils/mapToTypeMap.js +4 -2
- package/cjs/utils/sanitize.js +12 -6
- package/cjs/utils/typesToMap.js +4 -5
- package/extended/BTreeSet.js +7 -6
- package/extended/BitVec.js +5 -5
- package/extended/Bytes.d.ts +1 -1
- package/extended/Bytes.js +6 -6
- package/extended/Map.js +4 -5
- package/extended/OptionBool.d.ts +40 -0
- package/extended/OptionBool.js +88 -0
- package/extended/index.d.ts +1 -0
- package/extended/index.js +1 -0
- package/native/Struct.d.ts +7 -2
- package/native/Struct.js +33 -20
- package/native/Text.d.ts +2 -2
- package/native/Text.js +4 -4
- package/package.json +7 -2
- package/packageInfo.js +1 -1
- package/types/codec.d.ts +1 -2
- package/utils/decodeU8a.d.ts +2 -4
- package/utils/decodeU8a.js +7 -8
- package/utils/mapToTypeMap.d.ts +1 -1
- package/utils/mapToTypeMap.js +4 -2
- package/utils/sanitize.d.ts +1 -0
- package/utils/sanitize.js +10 -6
- package/utils/typesToMap.d.ts +1 -1
- package/utils/typesToMap.js +3 -5
|
@@ -10,8 +10,7 @@ import type { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types';
|
|
|
10
10
|
export declare abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
|
|
11
11
|
readonly registry: Registry;
|
|
12
12
|
createdAtHash?: IU8a;
|
|
13
|
-
|
|
14
|
-
protected constructor(registry: Registry, values: T[], initialU8aLength?: number);
|
|
13
|
+
protected constructor(registry: Registry, length: number);
|
|
15
14
|
/**
|
|
16
15
|
* @description The length of the value when encoded as a Uint8Array
|
|
17
16
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { compactToU8a,
|
|
3
|
+
import { compactToU8a, u8aConcatStrict, u8aToHex } from '@polkadot/util';
|
|
4
4
|
import { compareArray } from "../utils/compareArray.js";
|
|
5
5
|
/**
|
|
6
6
|
* @name AbstractArray
|
|
@@ -11,15 +11,9 @@ import { compareArray } from "../utils/compareArray.js";
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
export class AbstractArray extends Array {
|
|
14
|
-
constructor(registry,
|
|
15
|
-
super(
|
|
16
|
-
|
|
17
|
-
for (let i = 0; i < values.length; i++) {
|
|
18
|
-
this[i] = values[i];
|
|
19
|
-
}
|
|
20
|
-
|
|
14
|
+
constructor(registry, length) {
|
|
15
|
+
super(length);
|
|
21
16
|
this.registry = registry;
|
|
22
|
-
this.initialU8aLength = initialU8aLength;
|
|
23
17
|
}
|
|
24
18
|
/**
|
|
25
19
|
* @description The length of the value when encoded as a Uint8Array
|
|
@@ -160,7 +154,7 @@ export class AbstractArray extends Array {
|
|
|
160
154
|
|
|
161
155
|
toU8a(isBare) {
|
|
162
156
|
const encoded = this.toU8aInner();
|
|
163
|
-
return isBare ?
|
|
157
|
+
return isBare ? u8aConcatStrict(encoded) : u8aConcatStrict([compactToU8a(this.length), ...encoded]);
|
|
164
158
|
}
|
|
165
159
|
|
|
166
160
|
toU8aInner(isBare) {
|
package/base/Compact.d.ts
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
import type { BN } from '@polkadot/util';
|
|
3
3
|
import type { HexString } from '@polkadot/util/types';
|
|
4
4
|
import type { AnyJson, AnyNumber, CodecClass, ICompact, Inspect, INumber, IU8a, Registry } from '../types';
|
|
5
|
+
interface Options<T> {
|
|
6
|
+
definition?: CodecClass<T>;
|
|
7
|
+
setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
|
|
8
|
+
}
|
|
5
9
|
/**
|
|
6
10
|
* @name Compact
|
|
7
11
|
* @description
|
|
@@ -15,8 +19,8 @@ export declare class Compact<T extends INumber> implements ICompact<T> {
|
|
|
15
19
|
readonly registry: Registry;
|
|
16
20
|
createdAtHash?: IU8a;
|
|
17
21
|
readonly initialU8aLength?: number;
|
|
18
|
-
constructor(registry: Registry, Type: CodecClass<T> | string, value?: Compact<T> | AnyNumber);
|
|
19
|
-
static with<
|
|
22
|
+
constructor(registry: Registry, Type: CodecClass<T> | string, value?: Compact<T> | AnyNumber, { definition, setDefinition }?: Options<T>);
|
|
23
|
+
static with<O extends INumber>(Type: CodecClass<O> | string): CodecClass<Compact<O>>;
|
|
20
24
|
/** @internal */
|
|
21
25
|
static decodeCompact<T extends INumber>(registry: Registry, Type: CodecClass<T>, value: Compact<T> | AnyNumber): [T, number];
|
|
22
26
|
/**
|
|
@@ -85,3 +89,4 @@ export declare class Compact<T extends INumber> implements ICompact<T> {
|
|
|
85
89
|
*/
|
|
86
90
|
unwrap(): T;
|
|
87
91
|
}
|
|
92
|
+
export {};
|
package/base/Compact.js
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { compactFromU8a, compactToU8a, isU8a } from '@polkadot/util';
|
|
4
4
|
import { typeToConstructor } from "../utils/index.js";
|
|
5
|
+
|
|
6
|
+
function noopSetDefinition(d) {
|
|
7
|
+
return d;
|
|
8
|
+
}
|
|
5
9
|
/**
|
|
6
10
|
* @name Compact
|
|
7
11
|
* @description
|
|
@@ -11,22 +15,33 @@ import { typeToConstructor } from "../utils/index.js";
|
|
|
11
15
|
* a number and making the compact representation thereof
|
|
12
16
|
*/
|
|
13
17
|
|
|
18
|
+
|
|
14
19
|
export class Compact {
|
|
15
20
|
#Type;
|
|
16
21
|
#raw;
|
|
17
22
|
|
|
18
|
-
constructor(registry, Type, value = 0
|
|
23
|
+
constructor(registry, Type, value = 0, {
|
|
24
|
+
definition,
|
|
25
|
+
setDefinition = noopSetDefinition
|
|
26
|
+
} = {}) {
|
|
19
27
|
this.registry = registry;
|
|
20
|
-
this.#Type = typeToConstructor(registry, Type);
|
|
28
|
+
this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
|
|
21
29
|
const [raw, decodedLength] = Compact.decodeCompact(registry, this.#Type, value);
|
|
22
30
|
this.initialU8aLength = decodedLength;
|
|
23
31
|
this.#raw = raw;
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
static with(Type) {
|
|
35
|
+
let definition; // eslint-disable-next-line no-return-assign
|
|
36
|
+
|
|
37
|
+
const setDefinition = d => definition = d;
|
|
38
|
+
|
|
27
39
|
return class extends Compact {
|
|
28
40
|
constructor(registry, value) {
|
|
29
|
-
super(registry, Type, value
|
|
41
|
+
super(registry, Type, value, {
|
|
42
|
+
definition,
|
|
43
|
+
setDefinition
|
|
44
|
+
});
|
|
30
45
|
}
|
|
31
46
|
|
|
32
47
|
};
|
package/base/Enum.d.ts
CHANGED
|
@@ -3,6 +3,20 @@ import type { AnyJson, Codec, CodecClass, IEnum, Inspect, IU8a, Registry } from
|
|
|
3
3
|
export interface EnumCodecClass<T = Codec> {
|
|
4
4
|
new (registry: Registry, value?: any, index?: number): T;
|
|
5
5
|
}
|
|
6
|
+
interface Definition {
|
|
7
|
+
def: TypesDef;
|
|
8
|
+
isBasic: boolean;
|
|
9
|
+
isIndexed: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface EntryDef {
|
|
12
|
+
Type: CodecClass;
|
|
13
|
+
index: number;
|
|
14
|
+
}
|
|
15
|
+
declare type TypesDef = Record<string, EntryDef>;
|
|
16
|
+
interface Options {
|
|
17
|
+
definition?: Definition;
|
|
18
|
+
setDefinition?: (d: Definition) => Definition;
|
|
19
|
+
}
|
|
6
20
|
/**
|
|
7
21
|
* @name Enum
|
|
8
22
|
* @description
|
|
@@ -14,7 +28,7 @@ export declare class Enum implements IEnum {
|
|
|
14
28
|
readonly registry: Registry;
|
|
15
29
|
createdAtHash?: IU8a;
|
|
16
30
|
readonly initialU8aLength?: number;
|
|
17
|
-
constructor(registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number);
|
|
31
|
+
constructor(registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number, { definition, setDefinition }?: Options);
|
|
18
32
|
static with(Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum>;
|
|
19
33
|
/**
|
|
20
34
|
* @description The length of the value when encoded as a Uint8Array
|
|
@@ -107,3 +121,4 @@ export declare class Enum implements IEnum {
|
|
|
107
121
|
*/
|
|
108
122
|
toU8a(isBare?: boolean): Uint8Array;
|
|
109
123
|
}
|
|
124
|
+
export {};
|
package/base/Enum.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { assert, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase,
|
|
3
|
+
import { assert, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { mapToTypeMap, typesToMap } from "../utils/index.js";
|
|
5
5
|
import { Null } from "./Null.js"; // export interface, this is used in Enum.with, so required as public by TS
|
|
6
6
|
|
|
7
|
+
function noopSetDefinition(d) {
|
|
8
|
+
return d;
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
function isRustEnum(def) {
|
|
8
12
|
const defValues = Object.values(def);
|
|
9
13
|
|
|
@@ -31,12 +35,11 @@ function extractDef(registry, _def) {
|
|
|
31
35
|
isBasic = true;
|
|
32
36
|
isIndexed = false;
|
|
33
37
|
} else if (isRustEnum(_def)) {
|
|
34
|
-
const
|
|
38
|
+
const [Types, keys] = mapToTypeMap(registry, _def);
|
|
35
39
|
|
|
36
|
-
for (let i = 0; i <
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
Type,
|
|
40
|
+
for (let i = 0; i < keys.length; i++) {
|
|
41
|
+
def[keys[i]] = {
|
|
42
|
+
Type: Types[i],
|
|
40
43
|
index: i
|
|
41
44
|
};
|
|
42
45
|
}
|
|
@@ -132,12 +135,15 @@ export class Enum {
|
|
|
132
135
|
#isIndexed;
|
|
133
136
|
#raw;
|
|
134
137
|
|
|
135
|
-
constructor(registry, Types, value, index
|
|
138
|
+
constructor(registry, Types, value, index, {
|
|
139
|
+
definition,
|
|
140
|
+
setDefinition = noopSetDefinition
|
|
141
|
+
} = {}) {
|
|
136
142
|
const {
|
|
137
143
|
def,
|
|
138
144
|
isBasic,
|
|
139
145
|
isIndexed
|
|
140
|
-
} = extractDef(registry, Types); // shortcut isU8a as used in SCALE decoding
|
|
146
|
+
} = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
|
|
141
147
|
|
|
142
148
|
const decoded = isU8a(value) && value.length && !isNumber(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
|
|
143
149
|
this.registry = registry;
|
|
@@ -166,9 +172,16 @@ export class Enum {
|
|
|
166
172
|
isKeys[i] = `is${name}`;
|
|
167
173
|
}
|
|
168
174
|
|
|
175
|
+
let definition; // eslint-disable-next-line no-return-assign
|
|
176
|
+
|
|
177
|
+
const setDefinition = d => definition = d;
|
|
178
|
+
|
|
169
179
|
return class extends Enum {
|
|
170
180
|
constructor(registry, value, index) {
|
|
171
|
-
super(registry, Types, value, index
|
|
181
|
+
super(registry, Types, value, index, {
|
|
182
|
+
definition,
|
|
183
|
+
setDefinition
|
|
184
|
+
});
|
|
172
185
|
objectProperties(this, isKeys, (_, i) => this.type === keys[i]);
|
|
173
186
|
objectProperties(this, asKeys, (k, i) => {
|
|
174
187
|
assert(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
|
|
@@ -369,13 +382,14 @@ export class Enum {
|
|
|
369
382
|
}, {}) : this.defKeys;
|
|
370
383
|
}
|
|
371
384
|
|
|
372
|
-
const
|
|
385
|
+
const entries = Object.entries(this.#def);
|
|
386
|
+
return typesToMap(this.registry, entries.reduce((out, [key, {
|
|
373
387
|
Type
|
|
374
|
-
}]) => {
|
|
375
|
-
out[
|
|
388
|
+
}], i) => {
|
|
389
|
+
out[0][i] = Type;
|
|
390
|
+
out[1][i] = key;
|
|
376
391
|
return out;
|
|
377
|
-
},
|
|
378
|
-
return typesToMap(this.registry, typeMap);
|
|
392
|
+
}, [new Array(entries.length), new Array(entries.length)]));
|
|
379
393
|
}
|
|
380
394
|
/**
|
|
381
395
|
* @description Returns the base runtime type name for this instance
|
|
@@ -402,7 +416,7 @@ export class Enum {
|
|
|
402
416
|
|
|
403
417
|
|
|
404
418
|
toU8a(isBare) {
|
|
405
|
-
return
|
|
419
|
+
return isBare ? this.#raw.toU8a(isBare) : u8aConcatStrict([new Uint8Array([this.index]), this.#raw.toU8a(isBare)]);
|
|
406
420
|
}
|
|
407
421
|
|
|
408
422
|
}
|
package/base/Option.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { HexString } from '@polkadot/util/types';
|
|
2
2
|
import type { AnyJson, Codec, CodecClass, Inspect, IOption, IU8a, Registry } from '../types';
|
|
3
|
+
interface Options<T> {
|
|
4
|
+
definition?: CodecClass<T>;
|
|
5
|
+
setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
|
|
6
|
+
}
|
|
3
7
|
/**
|
|
4
8
|
* @name Option
|
|
5
9
|
* @description
|
|
@@ -12,7 +16,7 @@ export declare class Option<T extends Codec> implements IOption<T> {
|
|
|
12
16
|
#private;
|
|
13
17
|
readonly registry: Registry;
|
|
14
18
|
createdAtHash?: IU8a;
|
|
15
|
-
constructor(registry: Registry, typeName: CodecClass<T> | string, value?: unknown);
|
|
19
|
+
constructor(registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { definition, setDefinition }?: Options<T>);
|
|
16
20
|
static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Option<O>>;
|
|
17
21
|
/**
|
|
18
22
|
* @description The length of the value when encoded as a Uint8Array
|
|
@@ -41,7 +45,7 @@ export declare class Option<T extends Codec> implements IOption<T> {
|
|
|
41
45
|
/**
|
|
42
46
|
* @description The actual value for the Option
|
|
43
47
|
*/
|
|
44
|
-
get value():
|
|
48
|
+
get value(): T;
|
|
45
49
|
/**
|
|
46
50
|
* @description Compares the value of the input to see if there is a match
|
|
47
51
|
*/
|
|
@@ -90,3 +94,4 @@ export declare class Option<T extends Codec> implements IOption<T> {
|
|
|
90
94
|
*/
|
|
91
95
|
unwrapOrDefault(): T;
|
|
92
96
|
}
|
|
97
|
+
export {};
|
package/base/Option.js
CHANGED
|
@@ -4,6 +4,10 @@ import { assert, isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot
|
|
|
4
4
|
import { typeToConstructor } from "../utils/index.js";
|
|
5
5
|
import { Null } from "./Null.js";
|
|
6
6
|
|
|
7
|
+
function noopSetDefinition(d) {
|
|
8
|
+
return d;
|
|
9
|
+
}
|
|
10
|
+
|
|
7
11
|
class None extends Null {
|
|
8
12
|
/**
|
|
9
13
|
* @description Returns the base runtime type name for this instance
|
|
@@ -50,8 +54,11 @@ export class Option {
|
|
|
50
54
|
#initialU8aLength;
|
|
51
55
|
#raw;
|
|
52
56
|
|
|
53
|
-
constructor(registry, typeName, value
|
|
54
|
-
|
|
57
|
+
constructor(registry, typeName, value, {
|
|
58
|
+
definition,
|
|
59
|
+
setDefinition = noopSetDefinition
|
|
60
|
+
} = {}) {
|
|
61
|
+
const Type = definition || setDefinition(typeToConstructor(registry, typeName));
|
|
55
62
|
const decoded = isU8a(value) && value.length && !isCodec(value) ? value[0] === 0 ? new None(registry) : new Type(registry, value.subarray(1)) : decodeOption(registry, Type, value);
|
|
56
63
|
this.registry = registry;
|
|
57
64
|
this.#Type = Type;
|
|
@@ -63,9 +70,19 @@ export class Option {
|
|
|
63
70
|
}
|
|
64
71
|
|
|
65
72
|
static with(Type) {
|
|
73
|
+
let definition;
|
|
74
|
+
|
|
75
|
+
const setDefinition = d => {
|
|
76
|
+
definition = d;
|
|
77
|
+
return d;
|
|
78
|
+
};
|
|
79
|
+
|
|
66
80
|
return class extends Option {
|
|
67
81
|
constructor(registry, value) {
|
|
68
|
-
super(registry, Type, value
|
|
82
|
+
super(registry, Type, value, {
|
|
83
|
+
definition,
|
|
84
|
+
setDefinition
|
|
85
|
+
});
|
|
69
86
|
}
|
|
70
87
|
|
|
71
88
|
};
|
package/base/Tuple.d.ts
CHANGED
|
@@ -4,6 +4,11 @@ declare type TupleType = (CodecClass | string);
|
|
|
4
4
|
declare type TupleTypes = TupleType[] | {
|
|
5
5
|
[index: string]: CodecClass | string;
|
|
6
6
|
};
|
|
7
|
+
declare type Definition = [CodecClass[], string[]];
|
|
8
|
+
interface Options {
|
|
9
|
+
definition?: Definition;
|
|
10
|
+
setDefinition?: (d: Definition) => Definition;
|
|
11
|
+
}
|
|
7
12
|
/**
|
|
8
13
|
* @name Tuple
|
|
9
14
|
* @description
|
|
@@ -12,7 +17,8 @@ declare type TupleTypes = TupleType[] | {
|
|
|
12
17
|
*/
|
|
13
18
|
export declare class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
|
14
19
|
#private;
|
|
15
|
-
|
|
20
|
+
readonly initialU8aLength?: number;
|
|
21
|
+
constructor(registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition }?: Options);
|
|
16
22
|
static with(Types: TupleTypes | TupleType): CodecClass<Tuple>;
|
|
17
23
|
/**
|
|
18
24
|
* @description The length of the value when encoded as a Uint8Array
|
package/base/Tuple.js
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { isFunction, isHex, isString, isU8a, stringify,
|
|
3
|
+
import { isFunction, isHex, isString, isU8a, stringify, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { AbstractArray } from "../abstract/AbstractArray.js";
|
|
5
5
|
import { decodeU8a, mapToTypeMap, typeToConstructor } from "../utils/index.js";
|
|
6
6
|
|
|
7
|
+
function noopSetDefinition(d) {
|
|
8
|
+
return d;
|
|
9
|
+
}
|
|
7
10
|
/** @internal */
|
|
8
|
-
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
function decodeTuple(registry, result, Classes, value) {
|
|
9
14
|
if (isU8a(value) || isHex(value)) {
|
|
10
|
-
return decodeU8a(registry, u8aToU8a(value), Classes);
|
|
15
|
+
return decodeU8a(registry, result, u8aToU8a(value), Classes);
|
|
11
16
|
}
|
|
12
17
|
|
|
13
|
-
const Types =
|
|
14
|
-
return [Types.map((Type, index) => {
|
|
15
|
-
try {
|
|
16
|
-
const entry = value === null || value === void 0 ? void 0 : value[index];
|
|
17
|
-
|
|
18
|
-
if (entry instanceof Type) {
|
|
19
|
-
return entry;
|
|
20
|
-
}
|
|
18
|
+
const Types = Classes[0];
|
|
21
19
|
|
|
22
|
-
|
|
20
|
+
for (let i = 0; i < Types.length; i++) {
|
|
21
|
+
try {
|
|
22
|
+
const entry = value === null || value === void 0 ? void 0 : value[i];
|
|
23
|
+
result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
|
|
23
24
|
} catch (error) {
|
|
24
|
-
throw new Error(`Tuple: failed on ${
|
|
25
|
+
throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
|
|
25
26
|
}
|
|
26
|
-
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [result, 0];
|
|
27
30
|
}
|
|
28
31
|
/**
|
|
29
32
|
* @name Tuple
|
|
@@ -36,17 +39,28 @@ function decodeTuple(registry, Classes, value) {
|
|
|
36
39
|
export class Tuple extends AbstractArray {
|
|
37
40
|
#Types;
|
|
38
41
|
|
|
39
|
-
constructor(registry, Types, value
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
constructor(registry, Types, value, {
|
|
43
|
+
definition,
|
|
44
|
+
setDefinition = noopSetDefinition
|
|
45
|
+
} = {}) {
|
|
46
|
+
const Classes = definition || setDefinition(Array.isArray(Types) ? [Types.map(t => typeToConstructor(registry, t)), []] : isFunction(Types) || isString(Types) ? [[typeToConstructor(registry, Types)], []] : mapToTypeMap(registry, Types));
|
|
47
|
+
super(registry, Classes[0].length);
|
|
48
|
+
const [, decodedLength] = isU8a(value) ? decodeU8a(registry, this, value, Classes, false) : decodeTuple(registry, this, Classes, value);
|
|
49
|
+
this.initialU8aLength = decodedLength;
|
|
43
50
|
this.#Types = Classes;
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
static with(Types) {
|
|
54
|
+
let definition; // eslint-disable-next-line no-return-assign
|
|
55
|
+
|
|
56
|
+
const setDefinition = d => definition = d;
|
|
57
|
+
|
|
47
58
|
return class extends Tuple {
|
|
48
59
|
constructor(registry, value) {
|
|
49
|
-
super(registry, Types, value
|
|
60
|
+
super(registry, Types, value, {
|
|
61
|
+
definition,
|
|
62
|
+
setDefinition
|
|
63
|
+
});
|
|
50
64
|
}
|
|
51
65
|
|
|
52
66
|
};
|
|
@@ -71,7 +85,7 @@ export class Tuple extends AbstractArray {
|
|
|
71
85
|
|
|
72
86
|
|
|
73
87
|
get Types() {
|
|
74
|
-
return
|
|
88
|
+
return this.#Types[1].length ? this.#Types[1] : this.#Types[0].map(T => new T(this.registry).toRawType());
|
|
75
89
|
}
|
|
76
90
|
/**
|
|
77
91
|
* @description Returns a breakdown of the hex encoding for this Codec
|
|
@@ -89,7 +103,7 @@ export class Tuple extends AbstractArray {
|
|
|
89
103
|
|
|
90
104
|
|
|
91
105
|
toRawType() {
|
|
92
|
-
const types =
|
|
106
|
+
const types = this.#Types[0].map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
|
|
93
107
|
return `(${types.join(',')})`;
|
|
94
108
|
}
|
|
95
109
|
/**
|
|
@@ -108,7 +122,7 @@ export class Tuple extends AbstractArray {
|
|
|
108
122
|
|
|
109
123
|
|
|
110
124
|
toU8a(isBare) {
|
|
111
|
-
return
|
|
125
|
+
return u8aConcatStrict(this.toU8aInner(isBare));
|
|
112
126
|
}
|
|
113
127
|
|
|
114
128
|
}
|
package/base/Vec.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type { HexString } from '@polkadot/util/types';
|
|
2
2
|
import type { Codec, CodecClass, Registry } from '../types';
|
|
3
3
|
import { AbstractArray } from '../abstract/AbstractArray';
|
|
4
|
-
|
|
4
|
+
interface Options<T> {
|
|
5
|
+
definition?: CodecClass<T>;
|
|
6
|
+
setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
|
|
7
|
+
}
|
|
8
|
+
export declare function decodeVec<T extends Codec>(registry: Registry, result: T[], value: Uint8Array | HexString | unknown[], startAt: number, Type: CodecClass<T>): [number, number];
|
|
5
9
|
/**
|
|
6
10
|
* @name Vec
|
|
7
11
|
* @description
|
|
@@ -11,7 +15,8 @@ export declare function decodeVec<T extends Codec>(registry: Registry, Type: Cod
|
|
|
11
15
|
*/
|
|
12
16
|
export declare class Vec<T extends Codec> extends AbstractArray<T> {
|
|
13
17
|
#private;
|
|
14
|
-
|
|
18
|
+
readonly initialU8aLength?: number;
|
|
19
|
+
constructor(registry: Registry, Type: CodecClass<T> | string, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
|
|
15
20
|
static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Vec<O>>;
|
|
16
21
|
/**
|
|
17
22
|
* @description The type for the items
|
|
@@ -26,3 +31,4 @@ export declare class Vec<T extends Codec> extends AbstractArray<T> {
|
|
|
26
31
|
*/
|
|
27
32
|
toRawType(): string;
|
|
28
33
|
}
|
|
34
|
+
export {};
|
package/base/Vec.js
CHANGED
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { assert,
|
|
3
|
+
import { assert, compactFromU8aLim, isU8a, logger, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { AbstractArray } from "../abstract/AbstractArray.js";
|
|
5
5
|
import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
|
|
6
6
|
const MAX_LENGTH = 64 * 1024;
|
|
7
7
|
const l = logger('Vec');
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
function noopSetDefinition(d) {
|
|
10
|
+
return d;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function decodeVecLength(value) {
|
|
9
14
|
if (Array.isArray(value)) {
|
|
10
|
-
|
|
15
|
+
return [value, value.length, 0];
|
|
16
|
+
}
|
|
11
17
|
|
|
12
|
-
|
|
18
|
+
const u8a = u8aToU8a(value);
|
|
19
|
+
const [startAt, length] = compactFromU8aLim(u8a);
|
|
20
|
+
assert(length <= MAX_LENGTH, () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
|
21
|
+
return [u8a, length, startAt];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function decodeVec(registry, result, value, startAt, Type) {
|
|
25
|
+
if (Array.isArray(value)) {
|
|
26
|
+
const count = result.length;
|
|
27
|
+
|
|
28
|
+
for (let i = 0; i < count; i++) {
|
|
13
29
|
const entry = value[i];
|
|
14
30
|
|
|
15
31
|
try {
|
|
@@ -20,20 +36,10 @@ export function decodeVec(registry, Type, value, length = -1) {
|
|
|
20
36
|
}
|
|
21
37
|
}
|
|
22
38
|
|
|
23
|
-
return [
|
|
39
|
+
return [0, 0];
|
|
24
40
|
}
|
|
25
41
|
|
|
26
|
-
|
|
27
|
-
let offset = 0;
|
|
28
|
-
|
|
29
|
-
if (length === -1) {
|
|
30
|
-
const [_offset, _length] = compactFromU8a(u8a);
|
|
31
|
-
assert(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
|
|
32
|
-
length = _length.toNumber();
|
|
33
|
-
offset = _offset;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return decodeU8aVec(registry, u8a, offset, Type, length);
|
|
42
|
+
return decodeU8aVec(registry, result, u8aToU8a(value), startAt, Type);
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
39
45
|
* @name Vec
|
|
@@ -46,17 +52,28 @@ export function decodeVec(registry, Type, value, length = -1) {
|
|
|
46
52
|
export class Vec extends AbstractArray {
|
|
47
53
|
#Type;
|
|
48
54
|
|
|
49
|
-
constructor(registry, Type, value = []
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
constructor(registry, Type, value = [], {
|
|
56
|
+
definition,
|
|
57
|
+
setDefinition = noopSetDefinition
|
|
58
|
+
} = {}) {
|
|
59
|
+
const [decodeFrom, length, startAt] = decodeVecLength(value);
|
|
60
|
+
super(registry, length);
|
|
61
|
+
this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
|
|
62
|
+
const [decodedLength] = isU8a(decodeFrom) ? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type);
|
|
63
|
+
this.initialU8aLength = decodedLength;
|
|
54
64
|
}
|
|
55
65
|
|
|
56
66
|
static with(Type) {
|
|
67
|
+
let definition; // eslint-disable-next-line no-return-assign
|
|
68
|
+
|
|
69
|
+
const setDefinition = d => definition = d;
|
|
70
|
+
|
|
57
71
|
return class extends Vec {
|
|
58
72
|
constructor(registry, value) {
|
|
59
|
-
super(registry, Type, value
|
|
73
|
+
super(registry, Type, value, {
|
|
74
|
+
definition,
|
|
75
|
+
setDefinition
|
|
76
|
+
});
|
|
60
77
|
}
|
|
61
78
|
|
|
62
79
|
};
|
package/base/VecFixed.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { HexString } from '@polkadot/util/types';
|
|
2
2
|
import type { Codec, CodecClass, Inspect, Registry } from '../types';
|
|
3
3
|
import { AbstractArray } from '../abstract/AbstractArray';
|
|
4
|
+
interface Options<T> {
|
|
5
|
+
definition?: CodecClass<T>;
|
|
6
|
+
setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
|
|
7
|
+
}
|
|
4
8
|
/**
|
|
5
9
|
* @name VecFixed
|
|
6
10
|
* @description
|
|
@@ -8,7 +12,8 @@ import { AbstractArray } from '../abstract/AbstractArray';
|
|
|
8
12
|
*/
|
|
9
13
|
export declare class VecFixed<T extends Codec> extends AbstractArray<T> {
|
|
10
14
|
#private;
|
|
11
|
-
|
|
15
|
+
readonly initialU8aLength?: number;
|
|
16
|
+
constructor(registry: Registry, Type: CodecClass<T> | string, length: number, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
|
|
12
17
|
static with<O extends Codec>(Type: CodecClass<O> | string, length: number): CodecClass<VecFixed<O>>;
|
|
13
18
|
/**
|
|
14
19
|
* @description The type for the items
|
|
@@ -28,3 +33,4 @@ export declare class VecFixed<T extends Codec> extends AbstractArray<T> {
|
|
|
28
33
|
*/
|
|
29
34
|
toRawType(): string;
|
|
30
35
|
}
|
|
36
|
+
export {};
|
package/base/VecFixed.js
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import {
|
|
3
|
+
import { isU8a, u8aConcatStrict } from '@polkadot/util';
|
|
4
4
|
import { AbstractArray } from "../abstract/AbstractArray.js";
|
|
5
5
|
import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
|
|
6
6
|
import { decodeVec } from "./Vec.js";
|
|
7
|
-
/** @internal */
|
|
8
7
|
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
while (values.length < length) {
|
|
13
|
-
values.push(new Type(registry));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
assert(values.length === length, () => `Expected a length of exactly ${length} entries`);
|
|
17
|
-
return [values, decodedLength, decodedLengthNoOffset];
|
|
8
|
+
function noopSetDefinition(d) {
|
|
9
|
+
return d;
|
|
18
10
|
}
|
|
19
11
|
/**
|
|
20
12
|
* @name VecFixed
|
|
@@ -26,17 +18,27 @@ function decodeVecFixed(registry, value, Type, length) {
|
|
|
26
18
|
export class VecFixed extends AbstractArray {
|
|
27
19
|
#Type;
|
|
28
20
|
|
|
29
|
-
constructor(registry, Type, length, value = []
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
constructor(registry, Type, length, value = [], {
|
|
22
|
+
definition,
|
|
23
|
+
setDefinition = noopSetDefinition
|
|
24
|
+
} = {}) {
|
|
25
|
+
super(registry, length);
|
|
26
|
+
this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
|
|
27
|
+
const [, decodedLengthNoOffset] = isU8a(value) ? decodeU8aVec(registry, this, value, 0, this.#Type) : decodeVec(registry, this, value, 0, this.#Type);
|
|
28
|
+
this.initialU8aLength = decodedLengthNoOffset;
|
|
34
29
|
}
|
|
35
30
|
|
|
36
31
|
static with(Type, length) {
|
|
32
|
+
let definition; // eslint-disable-next-line no-return-assign
|
|
33
|
+
|
|
34
|
+
const setDefinition = d => definition = d;
|
|
35
|
+
|
|
37
36
|
return class extends VecFixed {
|
|
38
37
|
constructor(registry, value) {
|
|
39
|
-
super(registry, Type, length, value
|
|
38
|
+
super(registry, Type, length, value, {
|
|
39
|
+
definition,
|
|
40
|
+
setDefinition
|
|
41
|
+
});
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
};
|
|
@@ -78,7 +80,7 @@ export class VecFixed extends AbstractArray {
|
|
|
78
80
|
// we override, we don't add the length prefix for ourselves, and at the same time we
|
|
79
81
|
// ignore isBare on entries, since they should be properly encoded at all times
|
|
80
82
|
const encoded = this.toU8aInner();
|
|
81
|
-
return encoded.length ?
|
|
83
|
+
return encoded.length ? u8aConcatStrict(encoded) : new Uint8Array([]);
|
|
82
84
|
}
|
|
83
85
|
/**
|
|
84
86
|
* @description Returns the base runtime type name for this instance
|