@polkadot/types-codec 8.5.2-5 → 8.5.2-6

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/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<T extends INumber>(Type: CodecClass<T> | string): CodecClass<Compact<T>>;
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
@@ -4,6 +4,10 @@ import { assert, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefine
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
 
@@ -132,12 +136,15 @@ export class Enum {
132
136
  #isIndexed;
133
137
  #raw;
134
138
 
135
- constructor(registry, Types, value, index) {
139
+ constructor(registry, Types, value, index, {
140
+ definition,
141
+ setDefinition = noopSetDefinition
142
+ } = {}) {
136
143
  const {
137
144
  def,
138
145
  isBasic,
139
146
  isIndexed
140
- } = extractDef(registry, Types); // shortcut isU8a as used in SCALE decoding
147
+ } = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
141
148
 
142
149
  const decoded = isU8a(value) && value.length && !isNumber(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
143
150
  this.registry = registry;
@@ -166,9 +173,16 @@ export class Enum {
166
173
  isKeys[i] = `is${name}`;
167
174
  }
168
175
 
176
+ let definition; // eslint-disable-next-line no-return-assign
177
+
178
+ const setDefinition = d => definition = d;
179
+
169
180
  return class extends Enum {
170
181
  constructor(registry, value, index) {
171
- super(registry, Types, value, index);
182
+ super(registry, Types, value, index, {
183
+ definition,
184
+ setDefinition
185
+ });
172
186
  objectProperties(this, isKeys, (_, i) => this.type === keys[i]);
173
187
  objectProperties(this, asKeys, (k, i) => {
174
188
  assert(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
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
@@ -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
- const Type = typeToConstructor(registry, typeName);
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
@@ -1,9 +1,16 @@
1
1
  import type { AnyTupleValue, Codec, CodecClass, Inspect, ITuple, Registry } from '../types';
2
2
  import { AbstractArray } from '../abstract/AbstractArray';
3
+ declare type TupleCodecClass = CodecClass[] | {
4
+ [index: string]: CodecClass;
5
+ };
3
6
  declare type TupleType = (CodecClass | string);
4
7
  declare type TupleTypes = TupleType[] | {
5
8
  [index: string]: CodecClass | string;
6
9
  };
10
+ interface Options {
11
+ definition?: TupleCodecClass;
12
+ setDefinition?: (d: TupleCodecClass) => TupleCodecClass;
13
+ }
7
14
  /**
8
15
  * @name Tuple
9
16
  * @description
@@ -12,7 +19,7 @@ declare type TupleTypes = TupleType[] | {
12
19
  */
13
20
  export declare class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
14
21
  #private;
15
- constructor(registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue);
22
+ constructor(registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition }?: Options);
16
23
  static with(Types: TupleTypes | TupleType): CodecClass<Tuple>;
17
24
  /**
18
25
  * @description The length of the value when encoded as a Uint8Array
package/base/Tuple.js CHANGED
@@ -4,7 +4,12 @@ import { isFunction, isHex, isString, isU8a, stringify, u8aConcat, u8aToU8a } fr
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 */
11
+
12
+
8
13
  function decodeTuple(registry, Classes, value) {
9
14
  if (isU8a(value) || isHex(value)) {
10
15
  return decodeU8a(registry, u8aToU8a(value), Classes);
@@ -36,17 +41,27 @@ function decodeTuple(registry, Classes, value) {
36
41
  export class Tuple extends AbstractArray {
37
42
  #Types;
38
43
 
39
- constructor(registry, Types, value) {
40
- const Classes = Array.isArray(Types) ? Types.map(t => typeToConstructor(registry, t)) : isFunction(Types) || isString(Types) ? [typeToConstructor(registry, Types)] : mapToTypeMap(registry, Types);
44
+ constructor(registry, Types, value, {
45
+ definition,
46
+ setDefinition = noopSetDefinition
47
+ } = {}) {
48
+ const Classes = definition || setDefinition(Array.isArray(Types) ? Types.map(t => typeToConstructor(registry, t)) : isFunction(Types) || isString(Types) ? [typeToConstructor(registry, Types)] : mapToTypeMap(registry, Types));
41
49
  const [values, decodedLength] = isU8a(value) ? decodeU8a(registry, value, Classes) : decodeTuple(registry, Classes, value);
42
50
  super(registry, values, decodedLength);
43
51
  this.#Types = Classes;
44
52
  }
45
53
 
46
54
  static with(Types) {
55
+ let definition; // eslint-disable-next-line no-return-assign
56
+
57
+ const setDefinition = d => definition = d;
58
+
47
59
  return class extends Tuple {
48
60
  constructor(registry, value) {
49
- super(registry, Types, value);
61
+ super(registry, Types, value, {
62
+ definition,
63
+ setDefinition
64
+ });
50
65
  }
51
66
 
52
67
  };
package/base/Vec.d.ts CHANGED
@@ -1,6 +1,10 @@
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
+ interface Options<T> {
5
+ definition?: CodecClass<T>;
6
+ setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
7
+ }
4
8
  export declare function decodeVec<T extends Codec>(registry: Registry, Type: CodecClass<T>, value: Uint8Array | HexString | unknown[], length?: number): [T[], number, number];
5
9
  /**
6
10
  * @name Vec
@@ -11,7 +15,7 @@ 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
- constructor(registry: Registry, Type: CodecClass<T> | string, value?: Uint8Array | HexString | unknown[]);
18
+ constructor(registry: Registry, Type: CodecClass<T> | string, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
15
19
  static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Vec<O>>;
16
20
  /**
17
21
  * @description The type for the items
@@ -26,3 +30,4 @@ export declare class Vec<T extends Codec> extends AbstractArray<T> {
26
30
  */
27
31
  toRawType(): string;
28
32
  }
33
+ export {};
package/base/Vec.js CHANGED
@@ -5,6 +5,11 @@ 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
+
9
+ function noopSetDefinition(d) {
10
+ return d;
11
+ }
12
+
8
13
  export function decodeVec(registry, Type, value, length = -1) {
9
14
  if (Array.isArray(value)) {
10
15
  const result = new Array(value.length);
@@ -46,17 +51,27 @@ export function decodeVec(registry, Type, value, length = -1) {
46
51
  export class Vec extends AbstractArray {
47
52
  #Type;
48
53
 
49
- constructor(registry, Type, value = []) {
50
- const Clazz = typeToConstructor(registry, Type);
54
+ constructor(registry, Type, value = [], {
55
+ definition,
56
+ setDefinition = noopSetDefinition
57
+ } = {}) {
58
+ const Clazz = definition || setDefinition(typeToConstructor(registry, Type));
51
59
  const [values, decodedLength] = decodeVec(registry, Clazz, value);
52
60
  super(registry, values, decodedLength);
53
61
  this.#Type = Clazz;
54
62
  }
55
63
 
56
64
  static with(Type) {
65
+ let definition; // eslint-disable-next-line no-return-assign
66
+
67
+ const setDefinition = d => definition = d;
68
+
57
69
  return class extends Vec {
58
70
  constructor(registry, value) {
59
- super(registry, Type, value);
71
+ super(registry, Type, value, {
72
+ definition,
73
+ setDefinition
74
+ });
60
75
  }
61
76
 
62
77
  };
@@ -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,7 @@ import { AbstractArray } from '../abstract/AbstractArray';
8
12
  */
9
13
  export declare class VecFixed<T extends Codec> extends AbstractArray<T> {
10
14
  #private;
11
- constructor(registry: Registry, Type: CodecClass<T> | string, length: number, value?: Uint8Array | HexString | unknown[]);
15
+ constructor(registry: Registry, Type: CodecClass<T> | string, length: number, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
12
16
  static with<O extends Codec>(Type: CodecClass<O> | string, length: number): CodecClass<VecFixed<O>>;
13
17
  /**
14
18
  * @description The type for the items
@@ -28,3 +32,4 @@ export declare class VecFixed<T extends Codec> extends AbstractArray<T> {
28
32
  */
29
33
  toRawType(): string;
30
34
  }
35
+ export {};
package/base/VecFixed.js CHANGED
@@ -4,8 +4,13 @@ import { assert, isU8a, u8aConcat } 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
+
8
+ function noopSetDefinition(d) {
9
+ return d;
10
+ }
7
11
  /** @internal */
8
12
 
13
+
9
14
  function decodeVecFixed(registry, value, Type, length) {
10
15
  const [values, decodedLength, decodedLengthNoOffset] = decodeVec(registry, Type, value, length);
11
16
 
@@ -26,17 +31,27 @@ function decodeVecFixed(registry, value, Type, length) {
26
31
  export class VecFixed extends AbstractArray {
27
32
  #Type;
28
33
 
29
- constructor(registry, Type, length, value = []) {
30
- const Clazz = typeToConstructor(registry, Type);
34
+ constructor(registry, Type, length, value = [], {
35
+ definition,
36
+ setDefinition = noopSetDefinition
37
+ } = {}) {
38
+ const Clazz = definition || setDefinition(typeToConstructor(registry, Type));
31
39
  const [values,, decodedLengthNoOffset] = isU8a(value) ? decodeU8aVec(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
32
40
  super(registry, values, decodedLengthNoOffset);
33
41
  this.#Type = Clazz;
34
42
  }
35
43
 
36
44
  static with(Type, length) {
45
+ let definition; // eslint-disable-next-line no-return-assign
46
+
47
+ const setDefinition = d => definition = d;
48
+
37
49
  return class extends VecFixed {
38
50
  constructor(registry, value) {
39
- super(registry, Type, length, value);
51
+ super(registry, Type, length, value, {
52
+ definition,
53
+ setDefinition
54
+ });
40
55
  }
41
56
 
42
57
  };
@@ -11,7 +11,9 @@ var _utils = require("../utils");
11
11
 
12
12
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
13
13
  // SPDX-License-Identifier: Apache-2.0
14
-
14
+ function noopSetDefinition(d) {
15
+ return d;
16
+ }
15
17
  /**
16
18
  * @name Compact
17
19
  * @description
@@ -20,23 +22,36 @@ var _utils = require("../utils");
20
22
  * used by other types to add length-prefixed encoding, or in the case of wrapped types, taking
21
23
  * a number and making the compact representation thereof
22
24
  */
25
+
26
+
23
27
  class Compact {
24
28
  #Type;
25
29
  #raw;
26
30
 
27
31
  constructor(registry, Type) {
28
32
  let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
33
+ let {
34
+ definition,
35
+ setDefinition = noopSetDefinition
36
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
29
37
  this.registry = registry;
30
- this.#Type = (0, _utils.typeToConstructor)(registry, Type);
38
+ this.#Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
31
39
  const [raw, decodedLength] = Compact.decodeCompact(registry, this.#Type, value);
32
40
  this.initialU8aLength = decodedLength;
33
41
  this.#raw = raw;
34
42
  }
35
43
 
36
44
  static with(Type) {
45
+ let definition; // eslint-disable-next-line no-return-assign
46
+
47
+ const setDefinition = d => definition = d;
48
+
37
49
  return class extends Compact {
38
50
  constructor(registry, value) {
39
- super(registry, Type, value);
51
+ super(registry, Type, value, {
52
+ definition,
53
+ setDefinition
54
+ });
40
55
  }
41
56
 
42
57
  };
package/cjs/base/Enum.js CHANGED
@@ -13,6 +13,10 @@ var _Null = require("./Null");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
19
+
16
20
  function isRustEnum(def) {
17
21
  const defValues = Object.values(def);
18
22
 
@@ -147,11 +151,15 @@ class Enum {
147
151
  #raw;
148
152
 
149
153
  constructor(registry, Types, value, index) {
154
+ let {
155
+ definition,
156
+ setDefinition = noopSetDefinition
157
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
150
158
  const {
151
159
  def,
152
160
  isBasic,
153
161
  isIndexed
154
- } = extractDef(registry, Types); // shortcut isU8a as used in SCALE decoding
162
+ } = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
155
163
 
156
164
  const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isNumber)(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
157
165
  this.registry = registry;
@@ -183,9 +191,16 @@ class Enum {
183
191
  isKeys[i] = `is${name}`;
184
192
  }
185
193
 
194
+ let definition; // eslint-disable-next-line no-return-assign
195
+
196
+ const setDefinition = d => definition = d;
197
+
186
198
  return class extends Enum {
187
199
  constructor(registry, value, index) {
188
- super(registry, Types, value, index);
200
+ super(registry, Types, value, index, {
201
+ definition,
202
+ setDefinition
203
+ });
189
204
  (0, _util.objectProperties)(this, isKeys, (_, i) => this.type === keys[i]);
190
205
  (0, _util.objectProperties)(this, asKeys, (k, i) => {
191
206
  (0, _util.assert)(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
@@ -13,6 +13,10 @@ var _Null = require("./Null");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
19
+
16
20
  class None extends _Null.Null {
17
21
  /**
18
22
  * @description Returns the base runtime type name for this instance
@@ -60,7 +64,11 @@ class Option {
60
64
  #raw;
61
65
 
62
66
  constructor(registry, typeName, value) {
63
- const Type = (0, _utils.typeToConstructor)(registry, typeName);
67
+ let {
68
+ definition,
69
+ setDefinition = noopSetDefinition
70
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
71
+ const Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, typeName));
64
72
  const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isCodec)(value) ? value[0] === 0 ? new None(registry) : new Type(registry, value.subarray(1)) : decodeOption(registry, Type, value);
65
73
  this.registry = registry;
66
74
  this.#Type = Type;
@@ -72,9 +80,19 @@ class Option {
72
80
  }
73
81
 
74
82
  static with(Type) {
83
+ let definition;
84
+
85
+ const setDefinition = d => {
86
+ definition = d;
87
+ return d;
88
+ };
89
+
75
90
  return class extends Option {
76
91
  constructor(registry, value) {
77
- super(registry, Type, value);
92
+ super(registry, Type, value, {
93
+ definition,
94
+ setDefinition
95
+ });
78
96
  }
79
97
 
80
98
  };
package/cjs/base/Tuple.js CHANGED
@@ -13,8 +13,12 @@ var _utils = require("../utils");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
-
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
17
19
  /** @internal */
20
+
21
+
18
22
  function decodeTuple(registry, Classes, value) {
19
23
  if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
20
24
  return (0, _utils.decodeU8a)(registry, (0, _util.u8aToU8a)(value), Classes);
@@ -47,16 +51,27 @@ class Tuple extends _AbstractArray.AbstractArray {
47
51
  #Types;
48
52
 
49
53
  constructor(registry, Types, value) {
50
- const Classes = Array.isArray(Types) ? Types.map(t => (0, _utils.typeToConstructor)(registry, t)) : (0, _util.isFunction)(Types) || (0, _util.isString)(Types) ? [(0, _utils.typeToConstructor)(registry, Types)] : (0, _utils.mapToTypeMap)(registry, Types);
54
+ let {
55
+ definition,
56
+ setDefinition = noopSetDefinition
57
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
58
+ const Classes = definition || setDefinition(Array.isArray(Types) ? Types.map(t => (0, _utils.typeToConstructor)(registry, t)) : (0, _util.isFunction)(Types) || (0, _util.isString)(Types) ? [(0, _utils.typeToConstructor)(registry, Types)] : (0, _utils.mapToTypeMap)(registry, Types));
51
59
  const [values, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, value, Classes) : decodeTuple(registry, Classes, value);
52
60
  super(registry, values, decodedLength);
53
61
  this.#Types = Classes;
54
62
  }
55
63
 
56
64
  static with(Types) {
65
+ let definition; // eslint-disable-next-line no-return-assign
66
+
67
+ const setDefinition = d => definition = d;
68
+
57
69
  return class extends Tuple {
58
70
  constructor(registry, value) {
59
- super(registry, Types, value);
71
+ super(registry, Types, value, {
72
+ definition,
73
+ setDefinition
74
+ });
60
75
  }
61
76
 
62
77
  };
package/cjs/base/Vec.js CHANGED
@@ -17,6 +17,10 @@ var _utils = require("../utils");
17
17
  const MAX_LENGTH = 64 * 1024;
18
18
  const l = (0, _util.logger)('Vec');
19
19
 
20
+ function noopSetDefinition(d) {
21
+ return d;
22
+ }
23
+
20
24
  function decodeVec(registry, Type, value) {
21
25
  let length = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
22
26
 
@@ -63,16 +67,27 @@ class Vec extends _AbstractArray.AbstractArray {
63
67
 
64
68
  constructor(registry, Type) {
65
69
  let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
66
- const Clazz = (0, _utils.typeToConstructor)(registry, Type);
70
+ let {
71
+ definition,
72
+ setDefinition = noopSetDefinition
73
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
74
+ const Clazz = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
67
75
  const [values, decodedLength] = decodeVec(registry, Clazz, value);
68
76
  super(registry, values, decodedLength);
69
77
  this.#Type = Clazz;
70
78
  }
71
79
 
72
80
  static with(Type) {
81
+ let definition; // eslint-disable-next-line no-return-assign
82
+
83
+ const setDefinition = d => definition = d;
84
+
73
85
  return class extends Vec {
74
86
  constructor(registry, value) {
75
- super(registry, Type, value);
87
+ super(registry, Type, value, {
88
+ definition,
89
+ setDefinition
90
+ });
76
91
  }
77
92
 
78
93
  };
@@ -15,8 +15,12 @@ var _Vec = require("./Vec");
15
15
 
16
16
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
17
17
  // SPDX-License-Identifier: Apache-2.0
18
-
18
+ function noopSetDefinition(d) {
19
+ return d;
20
+ }
19
21
  /** @internal */
22
+
23
+
20
24
  function decodeVecFixed(registry, value, Type, length) {
21
25
  const [values, decodedLength, decodedLengthNoOffset] = (0, _Vec.decodeVec)(registry, Type, value, length);
22
26
 
@@ -39,16 +43,27 @@ class VecFixed extends _AbstractArray.AbstractArray {
39
43
 
40
44
  constructor(registry, Type, length) {
41
45
  let value = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
42
- const Clazz = (0, _utils.typeToConstructor)(registry, Type);
46
+ let {
47
+ definition,
48
+ setDefinition = noopSetDefinition
49
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
50
+ const Clazz = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
43
51
  const [values,, decodedLengthNoOffset] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8aVec)(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
44
52
  super(registry, values, decodedLengthNoOffset);
45
53
  this.#Type = Clazz;
46
54
  }
47
55
 
48
56
  static with(Type, length) {
57
+ let definition; // eslint-disable-next-line no-return-assign
58
+
59
+ const setDefinition = d => definition = d;
60
+
49
61
  return class extends VecFixed {
50
62
  constructor(registry, value) {
51
- super(registry, Type, length, value);
63
+ super(registry, Type, length, value, {
64
+ definition,
65
+ setDefinition
66
+ });
52
67
  }
53
68
 
54
69
  };
@@ -11,8 +11,12 @@ var _utils = require("../utils");
11
11
 
12
12
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
13
13
  // SPDX-License-Identifier: Apache-2.0
14
-
14
+ function noopSetDefinition(d) {
15
+ return d;
16
+ }
15
17
  /** @internal */
18
+
19
+
16
20
  function decodeStructFromObject(registry, Types, value, jsonMap) {
17
21
  let jsonObj;
18
22
  const inputKeys = Object.keys(Types);
@@ -84,7 +88,11 @@ class Struct extends Map {
84
88
 
85
89
  constructor(registry, Types, value) {
86
90
  let jsonMap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new Map();
87
- const typeMap = (0, _utils.mapToTypeMap)(registry, Types);
91
+ let {
92
+ definition,
93
+ setDefinition = noopSetDefinition
94
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
95
+ const typeMap = definition || setDefinition((0, _utils.mapToTypeMap)(registry, Types));
88
96
  const [decoded, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, value, typeMap, true) : (0, _util.isHex)(value) ? (0, _utils.decodeU8a)(registry, (0, _util.u8aToU8a)(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
89
97
  super(decoded);
90
98
  this.#registry = registry;
@@ -95,9 +103,16 @@ class Struct extends Map {
95
103
 
96
104
  static with(Types, jsonMap) {
97
105
  const keys = Object.keys(Types);
106
+ let definition; // eslint-disable-next-line no-return-assign
107
+
108
+ const setDefinition = d => definition = d;
109
+
98
110
  return class extends Struct {
99
111
  constructor(registry, value) {
100
- super(registry, Types, value, jsonMap);
112
+ super(registry, Types, value, jsonMap, {
113
+ definition,
114
+ setDefinition
115
+ });
101
116
  (0, _util.objectProperties)(this, keys, k => this.get(k));
102
117
  }
103
118
 
@@ -11,6 +11,6 @@ const packageInfo = {
11
11
  name: '@polkadot/types-codec',
12
12
  path: typeof __dirname === 'string' ? __dirname : 'auto',
13
13
  type: 'cjs',
14
- version: '8.5.2-5'
14
+ version: '8.5.2-6'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
@@ -1,6 +1,10 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
- import type { AnyJson, BareOpts, Codec, CodecClass, Inspect, IStruct, IU8a, Registry } from '../types';
2
+ import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, Inspect, IStruct, IU8a, Registry } from '../types';
3
3
  declare type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
4
+ interface Options {
5
+ definition?: CodecClassDef;
6
+ setDefinition?: (d: CodecClassDef) => CodecClassDef;
7
+ }
4
8
  /**
5
9
  * @name Struct
6
10
  * @description
@@ -23,7 +27,7 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
23
27
  #private;
24
28
  createdAtHash?: IU8a;
25
29
  readonly initialU8aLength?: number;
26
- constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>);
30
+ constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>, { definition, setDefinition }?: Options);
27
31
  static with<S extends TypesDef>(Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>>;
28
32
  /**
29
33
  * @description The available keys for this struct
package/native/Struct.js CHANGED
@@ -3,7 +3,12 @@
3
3
  import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
4
4
  import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from "../utils/index.js";
5
5
 
6
+ function noopSetDefinition(d) {
7
+ return d;
8
+ }
6
9
  /** @internal */
10
+
11
+
7
12
  function decodeStructFromObject(registry, Types, value, jsonMap) {
8
13
  let jsonObj;
9
14
  const inputKeys = Object.keys(Types);
@@ -73,8 +78,11 @@ export class Struct extends Map {
73
78
  #jsonMap;
74
79
  #Types;
75
80
 
76
- constructor(registry, Types, value, jsonMap = new Map()) {
77
- const typeMap = mapToTypeMap(registry, Types);
81
+ constructor(registry, Types, value, jsonMap = new Map(), {
82
+ definition,
83
+ setDefinition = noopSetDefinition
84
+ } = {}) {
85
+ const typeMap = definition || setDefinition(mapToTypeMap(registry, Types));
78
86
  const [decoded, decodedLength] = isU8a(value) ? decodeU8a(registry, value, typeMap, true) : isHex(value) ? decodeU8a(registry, u8aToU8a(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
79
87
  super(decoded);
80
88
  this.#registry = registry;
@@ -85,9 +93,16 @@ export class Struct extends Map {
85
93
 
86
94
  static with(Types, jsonMap) {
87
95
  const keys = Object.keys(Types);
96
+ let definition; // eslint-disable-next-line no-return-assign
97
+
98
+ const setDefinition = d => definition = d;
99
+
88
100
  return class extends Struct {
89
101
  constructor(registry, value) {
90
- super(registry, Types, value, jsonMap);
102
+ super(registry, Types, value, jsonMap, {
103
+ definition,
104
+ setDefinition
105
+ });
91
106
  objectProperties(this, keys, k => this.get(k));
92
107
  }
93
108
 
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.5.2-5",
23
+ "version": "8.5.2-6",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
package/packageInfo.js CHANGED
@@ -5,5 +5,5 @@ export const packageInfo = {
5
5
  name: '@polkadot/types-codec',
6
6
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
7
7
  type: 'esm',
8
- version: '8.5.2-5'
8
+ version: '8.5.2-6'
9
9
  };