@polkadot/types-codec 9.11.2 → 9.12.1

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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -10,6 +10,8 @@ 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
+ initialU8aLength?: number;
14
+ isStorageFallback?: boolean;
13
15
  /**
14
16
  * @description This ensures that operators such as clice, filter, map, etc. return
15
17
  * new Array instances (without this we need to apply overrides)
@@ -6,9 +6,10 @@ import type { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types
6
6
  */
7
7
  export declare abstract class AbstractBase<T extends Codec> implements Codec {
8
8
  #private;
9
- createdAtHash?: IU8a;
10
- readonly initialU8aLength?: number;
11
9
  readonly registry: Registry;
10
+ createdAtHash?: IU8a;
11
+ initialU8aLength?: number;
12
+ isStorageFallback?: boolean;
12
13
  protected constructor(registry: Registry, value: T, initialU8aLength?: number);
13
14
  /**
14
15
  * @description The length of the value when encoded as a Uint8Array
package/abstract/Base.js CHANGED
@@ -8,8 +8,8 @@
8
8
  export class AbstractBase {
9
9
  #raw;
10
10
  constructor(registry, value, initialU8aLength) {
11
- this.#raw = value;
12
11
  this.initialU8aLength = initialU8aLength;
12
+ this.#raw = value;
13
13
  this.registry = registry;
14
14
  }
15
15
 
package/abstract/Int.d.ts CHANGED
@@ -11,9 +11,11 @@ export declare const DEFAULT_UINT_BITS = 64;
11
11
  export declare abstract class AbstractInt extends BN implements INumber {
12
12
  #private;
13
13
  readonly registry: Registry;
14
- createdAtHash?: IU8a;
15
14
  readonly encodedLength: number;
16
15
  readonly isUnsigned: boolean;
16
+ createdAtHash?: IU8a;
17
+ initialU8aLength?: number;
18
+ isStorageFallback?: boolean;
17
19
  constructor(registry: Registry, value?: AnyNumber, bitLength?: UIntBitLength, isSigned?: boolean);
18
20
  /**
19
21
  * @description returns a hash of the contents
package/abstract/Int.js CHANGED
@@ -72,6 +72,7 @@ export class AbstractInt extends BN {
72
72
  this.registry = registry;
73
73
  this.#bitLength = bitLength;
74
74
  this.encodedLength = this.#bitLength / 8;
75
+ this.initialU8aLength = this.#bitLength / 8;
75
76
  this.isUnsigned = !isSigned;
76
77
  const isNegative = this.isNeg();
77
78
  const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
package/base/Compact.d.ts CHANGED
@@ -18,7 +18,8 @@ export declare class Compact<T extends INumber> implements ICompact<T> {
18
18
  #private;
19
19
  readonly registry: Registry;
20
20
  createdAtHash?: IU8a;
21
- readonly initialU8aLength?: number;
21
+ initialU8aLength?: number;
22
+ isStorageFallback?: boolean;
22
23
  constructor(registry: Registry, Type: CodecClass<T> | string, value?: Compact<T> | AnyNumber, { definition, setDefinition }?: Options<T>);
23
24
  static with<O extends INumber>(Type: CodecClass<O> | string): CodecClass<Compact<O>>;
24
25
  /**
@@ -9,6 +9,7 @@ export declare class DoNotConstruct implements Codec {
9
9
  #private;
10
10
  readonly registry: Registry;
11
11
  createdAtHash?: IU8a;
12
+ isStorageFallback?: boolean;
12
13
  constructor(registry: Registry, typeName?: string);
13
14
  static with(typeName?: string): CodecClass;
14
15
  /**
package/base/Enum.d.ts CHANGED
@@ -27,7 +27,8 @@ export declare class Enum implements IEnum {
27
27
  #private;
28
28
  readonly registry: Registry;
29
29
  createdAtHash?: IU8a;
30
- readonly initialU8aLength?: number;
30
+ initialU8aLength?: number;
31
+ isStorageFallback?: boolean;
31
32
  constructor(registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number, { definition, setDefinition }?: Options);
32
33
  static with(Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum>;
33
34
  /**
package/base/Null.d.ts CHANGED
@@ -10,7 +10,8 @@ export declare class Null implements Codec {
10
10
  readonly isEmpty = true;
11
11
  readonly registry: Registry;
12
12
  createdAtHash?: IU8a;
13
- readonly initialU8aLength = 0;
13
+ initialU8aLength: number;
14
+ isStorageFallback?: boolean;
14
15
  constructor(registry: Registry);
15
16
  /**
16
17
  * @description returns a hash of the contents
package/base/Null.js CHANGED
@@ -11,7 +11,6 @@ import { isNull } from '@polkadot/util';
11
11
  export class Null {
12
12
  encodedLength = 0;
13
13
  isEmpty = true;
14
- // Added for compatibility reasons, e.g. see Option
15
14
  initialU8aLength = 0;
16
15
  constructor(registry) {
17
16
  this.registry = registry;
package/base/Option.d.ts CHANGED
@@ -17,6 +17,7 @@ export declare class Option<T extends Codec> implements IOption<T> {
17
17
  readonly registry: Registry;
18
18
  createdAtHash?: IU8a;
19
19
  initialU8aLength?: number;
20
+ isStorageFallback?: boolean;
20
21
  constructor(registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { definition, setDefinition }?: Options<T>);
21
22
  static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Option<O>>;
22
23
  /**
package/base/Option.js CHANGED
@@ -63,7 +63,7 @@ export class Option {
63
63
  this.registry = registry;
64
64
  this.#Type = Type;
65
65
  this.#raw = decoded;
66
- if (decoded && decoded.initialU8aLength) {
66
+ if (decoded != null && decoded.initialU8aLength) {
67
67
  this.initialU8aLength = 1 + decoded.initialU8aLength;
68
68
  }
69
69
  }
package/base/Tuple.d.ts CHANGED
@@ -17,7 +17,6 @@ interface Options {
17
17
  */
18
18
  export declare class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
19
19
  #private;
20
- readonly initialU8aLength?: number;
21
20
  constructor(registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition }?: Options);
22
21
  static with(Types: TupleTypes | TupleType): CodecClass<Tuple>;
23
22
  /**
package/base/Tuple.js CHANGED
@@ -10,19 +10,27 @@ function noopSetDefinition(d) {
10
10
 
11
11
  /** @internal */
12
12
  function decodeTuple(registry, result, value, Classes) {
13
- if (isU8a(value) || isHex(value)) {
13
+ if (Array.isArray(value)) {
14
+ const Types = Classes[0];
15
+ for (let i = 0; i < Types.length; i++) {
16
+ try {
17
+ const entry = value == null ? void 0 : value[i];
18
+ result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
19
+ } catch (error) {
20
+ throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
21
+ }
22
+ }
23
+ return [result, 0];
24
+ } else if (isHex(value)) {
14
25
  return decodeU8a(registry, result, u8aToU8a(value), Classes);
15
- }
16
- const Types = Classes[0];
17
- for (let i = 0; i < Types.length; i++) {
18
- try {
19
- const entry = value == null ? void 0 : value[i];
20
- result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
21
- } catch (error) {
22
- throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
26
+ } else if (!value || !result.length) {
27
+ const Types = Classes[0];
28
+ for (let i = 0; i < Types.length; i++) {
29
+ result[i] = new Types[i](registry);
23
30
  }
31
+ return [result, 0];
24
32
  }
25
- return [result, 0];
33
+ throw new Error(`Expected array input to Tuple decoding, found ${typeof value}: ${stringify(value)}`);
26
34
  }
27
35
 
28
36
  /**
package/base/Vec.d.ts CHANGED
@@ -5,7 +5,7 @@ interface Options<T> {
5
5
  definition?: CodecClass<T>;
6
6
  setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
7
7
  }
8
- export declare function decodeVec<T extends Codec>(registry: Registry, result: T[], value: Uint8Array | HexString | unknown[], startAt: number, Type: CodecClass<T>): [number, number];
8
+ export declare function decodeVec<T extends Codec>(registry: Registry, result: T[], value: Uint8Array | HexString | unknown[] | null, startAt: number, Type: CodecClass<T>): [number, number];
9
9
  /**
10
10
  * @name Vec
11
11
  * @description
@@ -15,7 +15,6 @@ export declare function decodeVec<T extends Codec>(registry: Registry, result: T
15
15
  */
16
16
  export declare class Vec<T extends Codec> extends AbstractArray<T> {
17
17
  #private;
18
- readonly initialU8aLength?: number;
19
18
  constructor(registry: Registry, Type: CodecClass<T> | string, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
20
19
  static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Vec<O>>;
21
20
  /**
package/base/Vec.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Copyright 2017-2023 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { compactFromU8aLim, isU8a, logger, u8aToU8a } from '@polkadot/util';
4
+ import { compactFromU8aLim, isHex, isU8a, logger, stringify, u8aToU8a } from '@polkadot/util';
5
5
  import { AbstractArray } from "../abstract/Array.js";
6
6
  import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
7
7
  const MAX_LENGTH = 64 * 1024;
@@ -12,13 +12,17 @@ function noopSetDefinition(d) {
12
12
  function decodeVecLength(value) {
13
13
  if (Array.isArray(value)) {
14
14
  return [value, value.length, 0];
15
+ } else if (isU8a(value) || isHex(value)) {
16
+ const u8a = u8aToU8a(value);
17
+ const [startAt, length] = compactFromU8aLim(u8a);
18
+ if (length > MAX_LENGTH) {
19
+ throw new Error(`Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
20
+ }
21
+ return [u8a, length, startAt];
22
+ } else if (!value) {
23
+ return [null, 0, 0];
15
24
  }
16
- const u8a = u8aToU8a(value);
17
- const [startAt, length] = compactFromU8aLim(u8a);
18
- if (length > MAX_LENGTH) {
19
- throw new Error(`Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
20
- }
21
- return [u8a, length, startAt];
25
+ throw new Error(`Expected array/hex input to Vec<*> decoding, found ${typeof value}: ${stringify(value)}`);
22
26
  }
23
27
  export function decodeVec(registry, result, value, startAt, Type) {
24
28
  if (Array.isArray(value)) {
@@ -35,7 +39,11 @@ export function decodeVec(registry, result, value, startAt, Type) {
35
39
  }
36
40
  }
37
41
  return [0, 0];
42
+ } else if (!value) {
43
+ return [0, 0];
38
44
  }
45
+
46
+ // we don't need more checks, we already limited it via the length decoding
39
47
  return decodeU8aVec(registry, result, u8aToU8a(value), startAt, Type);
40
48
  }
41
49
 
@@ -55,7 +63,12 @@ export class Vec extends AbstractArray {
55
63
  const [decodeFrom, length, startAt] = decodeVecLength(value);
56
64
  super(registry, length);
57
65
  this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
58
- this.initialU8aLength = (isU8a(decodeFrom) ? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type))[0];
66
+ try {
67
+ this.initialU8aLength = (isU8a(decodeFrom) ? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type))[0];
68
+ } catch (e) {
69
+ console.error(decodeFrom, length, startAt);
70
+ throw e;
71
+ }
59
72
  }
60
73
  static with(Type) {
61
74
  let definition;
@@ -12,7 +12,6 @@ interface Options<T> {
12
12
  */
13
13
  export declare class VecFixed<T extends Codec> extends AbstractArray<T> {
14
14
  #private;
15
- readonly initialU8aLength?: number;
16
15
  constructor(registry: Registry, Type: CodecClass<T> | string, length: number, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
17
16
  static with<O extends Codec>(Type: CodecClass<O> | string, length: number): CodecClass<VecFixed<O>>;
18
17
  /**
@@ -14,8 +14,8 @@ exports.AbstractBase = void 0;
14
14
  class AbstractBase {
15
15
  #raw;
16
16
  constructor(registry, value, initialU8aLength) {
17
- this.#raw = value;
18
17
  this.initialU8aLength = initialU8aLength;
18
+ this.#raw = value;
19
19
  this.registry = registry;
20
20
  }
21
21
 
@@ -82,6 +82,7 @@ class AbstractInt extends _util.BN {
82
82
  this.registry = registry;
83
83
  this.#bitLength = bitLength;
84
84
  this.encodedLength = this.#bitLength / 8;
85
+ this.initialU8aLength = this.#bitLength / 8;
85
86
  this.isUnsigned = !isSigned;
86
87
  const isNegative = this.isNeg();
87
88
  const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
package/cjs/base/Null.js CHANGED
@@ -16,7 +16,6 @@ var _util = require("@polkadot/util");
16
16
  class Null {
17
17
  encodedLength = 0;
18
18
  isEmpty = true;
19
- // Added for compatibility reasons, e.g. see Option
20
19
  initialU8aLength = 0;
21
20
  constructor(registry) {
22
21
  this.registry = registry;
@@ -70,7 +70,7 @@ class Option {
70
70
  this.registry = registry;
71
71
  this.#Type = Type;
72
72
  this.#raw = decoded;
73
- if (decoded && decoded.initialU8aLength) {
73
+ if (decoded != null && decoded.initialU8aLength) {
74
74
  this.initialU8aLength = 1 + decoded.initialU8aLength;
75
75
  }
76
76
  }
package/cjs/base/Tuple.js CHANGED
@@ -16,19 +16,27 @@ function noopSetDefinition(d) {
16
16
 
17
17
  /** @internal */
18
18
  function decodeTuple(registry, result, value, Classes) {
19
- if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
19
+ if (Array.isArray(value)) {
20
+ const Types = Classes[0];
21
+ for (let i = 0; i < Types.length; i++) {
22
+ try {
23
+ const entry = value == null ? void 0 : value[i];
24
+ result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
25
+ } catch (error) {
26
+ throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
27
+ }
28
+ }
29
+ return [result, 0];
30
+ } else if ((0, _util.isHex)(value)) {
20
31
  return (0, _utils.decodeU8a)(registry, result, (0, _util.u8aToU8a)(value), Classes);
21
- }
22
- const Types = Classes[0];
23
- for (let i = 0; i < Types.length; i++) {
24
- try {
25
- const entry = value == null ? void 0 : value[i];
26
- result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
27
- } catch (error) {
28
- throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
32
+ } else if (!value || !result.length) {
33
+ const Types = Classes[0];
34
+ for (let i = 0; i < Types.length; i++) {
35
+ result[i] = new Types[i](registry);
29
36
  }
37
+ return [result, 0];
30
38
  }
31
- return [result, 0];
39
+ throw new Error(`Expected array input to Tuple decoding, found ${typeof value}: ${(0, _util.stringify)(value)}`);
32
40
  }
33
41
 
34
42
  /**
package/cjs/base/Vec.js CHANGED
@@ -19,13 +19,17 @@ function noopSetDefinition(d) {
19
19
  function decodeVecLength(value) {
20
20
  if (Array.isArray(value)) {
21
21
  return [value, value.length, 0];
22
+ } else if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
23
+ const u8a = (0, _util.u8aToU8a)(value);
24
+ const [startAt, length] = (0, _util.compactFromU8aLim)(u8a);
25
+ if (length > MAX_LENGTH) {
26
+ throw new Error(`Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
27
+ }
28
+ return [u8a, length, startAt];
29
+ } else if (!value) {
30
+ return [null, 0, 0];
22
31
  }
23
- const u8a = (0, _util.u8aToU8a)(value);
24
- const [startAt, length] = (0, _util.compactFromU8aLim)(u8a);
25
- if (length > MAX_LENGTH) {
26
- throw new Error(`Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
27
- }
28
- return [u8a, length, startAt];
32
+ throw new Error(`Expected array/hex input to Vec<*> decoding, found ${typeof value}: ${(0, _util.stringify)(value)}`);
29
33
  }
30
34
  function decodeVec(registry, result, value, startAt, Type) {
31
35
  if (Array.isArray(value)) {
@@ -42,7 +46,11 @@ function decodeVec(registry, result, value, startAt, Type) {
42
46
  }
43
47
  }
44
48
  return [0, 0];
49
+ } else if (!value) {
50
+ return [0, 0];
45
51
  }
52
+
53
+ // we don't need more checks, we already limited it via the length decoding
46
54
  return (0, _utils.decodeU8aVec)(registry, result, (0, _util.u8aToU8a)(value), startAt, Type);
47
55
  }
48
56
 
@@ -64,7 +72,12 @@ class Vec extends _Array.AbstractArray {
64
72
  const [decodeFrom, length, startAt] = decodeVecLength(value);
65
73
  super(registry, length);
66
74
  this.#Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
67
- this.initialU8aLength = ((0, _util.isU8a)(decodeFrom) ? (0, _utils.decodeU8aVec)(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type))[0];
75
+ try {
76
+ this.initialU8aLength = ((0, _util.isU8a)(decodeFrom) ? (0, _utils.decodeU8aVec)(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type))[0];
77
+ } catch (e) {
78
+ console.error(decodeFrom, length, startAt);
79
+ throw e;
80
+ }
68
81
  }
69
82
  static with(Type) {
70
83
  let definition;
@@ -15,6 +15,7 @@ var _util = require("@polkadot/util");
15
15
  * @noInheritDoc
16
16
  */
17
17
  class bool extends Boolean {
18
+ initialU8aLength = 1;
18
19
  constructor(registry) {
19
20
  let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
20
21
  super((0, _util.isU8a)(value) ? value[0] === 1 : value instanceof Boolean ? value.valueOf() : !!value);
@@ -35,6 +35,7 @@ function decodeDate(value) {
35
35
  * @noInheritDoc
36
36
  */
37
37
  class CodecDate extends Date {
38
+ initialU8aLength = BITLENGTH / 8;
38
39
  constructor(registry) {
39
40
  let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
40
41
  super(decodeDate(value));
@@ -222,7 +222,7 @@ class Struct extends Map {
222
222
  toHuman(isExtended) {
223
223
  const json = {};
224
224
  for (const [k, v] of this.entries()) {
225
- json[k] = v && v.toHuman(isExtended);
225
+ json[k] = v.toHuman(isExtended);
226
226
  }
227
227
  return json;
228
228
  }
@@ -233,11 +233,9 @@ class Struct extends Map {
233
233
  toJSON() {
234
234
  const json = {};
235
235
  for (const [k, v] of this.entries()) {
236
- const jsonKey = this.#jsonMap.get(k) || k;
237
-
238
- // We actually log inside the U8a decoding and use JSON.stringify(...), which
239
- // means that the Vec may be partially populated (same applies to toHuman, same check)
240
- json[jsonKey] = v && v.toJSON();
236
+ // Here we pull out the entry against the JSON mapping (if supplied)
237
+ // since this representation goes over RPC and needs to be correct
238
+ json[this.#jsonMap.get(k) || k] = v.toJSON();
241
239
  }
242
240
  return json;
243
241
  }
@@ -248,11 +246,7 @@ class Struct extends Map {
248
246
  toPrimitive() {
249
247
  const json = {};
250
248
  for (const [k, v] of this.entries()) {
251
- const jsonKey = this.#jsonMap.get(k) || k;
252
-
253
- // We actually log inside the U8a decoding and use JSON.stringify(...), which
254
- // means that the Vec may be partially populated (same applies to toHuman, same check)
255
- json[jsonKey] = v && v.toPrimitive();
249
+ json[k] = v.toPrimitive();
256
250
  }
257
251
  return json;
258
252
  }
@@ -278,9 +272,7 @@ class Struct extends Map {
278
272
  toU8a(isBare) {
279
273
  const encoded = [];
280
274
  for (const [k, v] of this.entries()) {
281
- if (v && (0, _util.isFunction)(v.toU8a)) {
282
- encoded.push(v.toU8a(!isBare || (0, _util.isBoolean)(isBare) ? isBare : isBare[k]));
283
- }
275
+ encoded.push(v.toU8a(!isBare || (0, _util.isBoolean)(isBare) ? isBare : isBare[k]));
284
276
  }
285
277
  return (0, _util.u8aConcatStrict)(encoded);
286
278
  }
@@ -13,6 +13,6 @@ const packageInfo = {
13
13
  name: '@polkadot/types-codec',
14
14
  path: typeof __dirname === 'string' ? __dirname : 'auto',
15
15
  type: 'cjs',
16
- version: '9.11.2'
16
+ version: '9.12.1'
17
17
  };
18
18
  exports.packageInfo = packageInfo;
@@ -4,7 +4,8 @@ export declare class BTreeSet<V extends Codec = Codec> extends Set<V> implements
4
4
  #private;
5
5
  readonly registry: Registry;
6
6
  createdAtHash?: IU8a;
7
- readonly initialU8aLength?: number;
7
+ initialU8aLength?: number;
8
+ isStorageFallback?: boolean;
8
9
  constructor(registry: Registry, valType: CodecClass<V> | string, rawValue?: Uint8Array | string | string[] | Set<any>);
9
10
  static with<V extends Codec>(valType: CodecClass<V> | string): CodecClass<BTreeSet<V>>;
10
11
  /**
package/extended/Map.d.ts CHANGED
@@ -4,7 +4,8 @@ export declare class CodecMap<K extends Codec = Codec, V extends Codec = Codec>
4
4
  #private;
5
5
  readonly registry: Registry;
6
6
  createdAtHash?: IU8a;
7
- readonly initialU8aLength?: number;
7
+ initialU8aLength?: number;
8
+ isStorageFallback?: boolean;
8
9
  constructor(registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type?: 'BTreeMap' | 'HashMap');
9
10
  /**
10
11
  * @description The length of the value when encoded as a Uint8Array
package/native/Bool.d.ts CHANGED
@@ -9,6 +9,8 @@ import type { AnyBool, Codec, Inspect, IU8a, Registry } from '../types';
9
9
  export declare class bool extends Boolean implements Codec {
10
10
  readonly registry: Registry;
11
11
  createdAtHash?: IU8a;
12
+ initialU8aLength: number;
13
+ isStorageFallback?: boolean;
12
14
  constructor(registry: Registry, value?: bool | AnyBool | Uint8Array | number);
13
15
  /**
14
16
  * @description The length of the value when encoded as a Uint8Array
package/native/Bool.js CHANGED
@@ -10,6 +10,7 @@ import { isU8a, u8aToHex } from '@polkadot/util';
10
10
  * @noInheritDoc
11
11
  */
12
12
  export class bool extends Boolean {
13
+ initialU8aLength = 1;
13
14
  constructor(registry, value = false) {
14
15
  super(isU8a(value) ? value[0] === 1 : value instanceof Boolean ? value.valueOf() : !!value);
15
16
  this.registry = registry;
package/native/Date.d.ts CHANGED
@@ -15,6 +15,8 @@ import { BN } from '@polkadot/util';
15
15
  export declare class CodecDate extends Date implements INumber {
16
16
  readonly registry: Registry;
17
17
  createdAtHash?: IU8a;
18
+ initialU8aLength: number;
19
+ isStorageFallback?: boolean;
18
20
  constructor(registry: Registry, value?: CodecDate | Date | AnyNumber);
19
21
  /**
20
22
  * @description The length of the value when encoded as a Uint8Array
package/native/Date.js CHANGED
@@ -29,6 +29,7 @@ function decodeDate(value) {
29
29
  * @noInheritDoc
30
30
  */
31
31
  export class CodecDate extends Date {
32
+ initialU8aLength = BITLENGTH / 8;
32
33
  constructor(registry, value = 0) {
33
34
  super(decodeDate(value));
34
35
  this.registry = registry;
package/native/Float.d.ts CHANGED
@@ -13,10 +13,11 @@ interface Options {
13
13
  */
14
14
  export declare class Float extends Number implements IFloat {
15
15
  #private;
16
- createdAtHash?: IU8a;
17
16
  readonly encodedLength: number;
18
- readonly initialU8aLength?: number;
19
17
  readonly registry: Registry;
18
+ createdAtHash?: IU8a;
19
+ initialU8aLength?: number;
20
+ isStorageFallback?: boolean;
20
21
  constructor(registry: Registry, value?: AnyFloat, { bitLength }?: Options);
21
22
  static with(bitLength: 32 | 64): CodecClass<Float>;
22
23
  /**
package/native/Json.d.ts CHANGED
@@ -11,6 +11,8 @@ import type { AnyJson, Codec, Inspect, IU8a, Registry } from '../types';
11
11
  export declare class Json extends Map<string, any> implements Codec {
12
12
  readonly registry: Registry;
13
13
  createdAtHash?: IU8a;
14
+ initialU8aLength?: number;
15
+ isStorageFallback?: boolean;
14
16
  constructor(registry: Registry, value?: Record<string, unknown> | null);
15
17
  /**
16
18
  * @description Always 0, never encodes as a Uint8Array
package/native/Raw.d.ts CHANGED
@@ -10,9 +10,10 @@ import type { AnyJson, AnyU8a, Inspect, IU8a, Registry } from '../types';
10
10
  * @noInheritDoc
11
11
  */
12
12
  export declare class Raw extends Uint8Array implements IU8a {
13
- createdAtHash?: IU8a;
14
- readonly initialU8aLength?: number;
15
13
  readonly registry: Registry;
14
+ createdAtHash?: IU8a;
15
+ initialU8aLength?: number;
16
+ isStorageFallback?: boolean;
16
17
  /**
17
18
  * @description This ensures that operators such as clice, filter, map, etc. return
18
19
  * new Array instances (without this we need to apply overrides)
package/native/Set.d.ts CHANGED
@@ -13,6 +13,8 @@ export declare class CodecSet extends Set<string> implements ISet<string> {
13
13
  #private;
14
14
  readonly registry: Registry;
15
15
  createdAtHash?: IU8a;
16
+ initialU8aLength?: number;
17
+ isStorageFallback?: boolean;
16
18
  constructor(registry: Registry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength?: number);
17
19
  static with(values: SetValues, bitLength?: number): CodecClass<CodecSet>;
18
20
  /**
@@ -26,9 +26,10 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
26
26
  [K in keyof S]: string;
27
27
  }> extends Map<keyof S, Codec> implements IStruct<keyof S> {
28
28
  #private;
29
- createdAtHash?: IU8a;
30
- readonly initialU8aLength?: number;
31
29
  readonly registry: Registry;
30
+ createdAtHash?: IU8a;
31
+ initialU8aLength?: number;
32
+ isStorageFallback?: boolean;
32
33
  constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>, { definition, setDefinition }?: Options);
33
34
  static with<S extends TypesDef>(Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>>;
34
35
  /**
package/native/Struct.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Copyright 2017-2023 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
4
+ import { isBoolean, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
5
5
  import { compareMap, decodeU8aStruct, mapToTypeMap, typesToMap } from "../utils/index.js";
6
6
  function noopSetDefinition(d) {
7
7
  return d;
@@ -213,7 +213,7 @@ export class Struct extends Map {
213
213
  toHuman(isExtended) {
214
214
  const json = {};
215
215
  for (const [k, v] of this.entries()) {
216
- json[k] = v && v.toHuman(isExtended);
216
+ json[k] = v.toHuman(isExtended);
217
217
  }
218
218
  return json;
219
219
  }
@@ -224,11 +224,9 @@ export class Struct extends Map {
224
224
  toJSON() {
225
225
  const json = {};
226
226
  for (const [k, v] of this.entries()) {
227
- const jsonKey = this.#jsonMap.get(k) || k;
228
-
229
- // We actually log inside the U8a decoding and use JSON.stringify(...), which
230
- // means that the Vec may be partially populated (same applies to toHuman, same check)
231
- json[jsonKey] = v && v.toJSON();
227
+ // Here we pull out the entry against the JSON mapping (if supplied)
228
+ // since this representation goes over RPC and needs to be correct
229
+ json[this.#jsonMap.get(k) || k] = v.toJSON();
232
230
  }
233
231
  return json;
234
232
  }
@@ -239,11 +237,7 @@ export class Struct extends Map {
239
237
  toPrimitive() {
240
238
  const json = {};
241
239
  for (const [k, v] of this.entries()) {
242
- const jsonKey = this.#jsonMap.get(k) || k;
243
-
244
- // We actually log inside the U8a decoding and use JSON.stringify(...), which
245
- // means that the Vec may be partially populated (same applies to toHuman, same check)
246
- json[jsonKey] = v && v.toPrimitive();
240
+ json[k] = v.toPrimitive();
247
241
  }
248
242
  return json;
249
243
  }
@@ -269,9 +263,7 @@ export class Struct extends Map {
269
263
  toU8a(isBare) {
270
264
  const encoded = [];
271
265
  for (const [k, v] of this.entries()) {
272
- if (v && isFunction(v.toU8a)) {
273
- encoded.push(v.toU8a(!isBare || isBoolean(isBare) ? isBare : isBare[k]));
274
- }
266
+ encoded.push(v.toU8a(!isBare || isBoolean(isBare) ? isBare : isBare[k]));
275
267
  }
276
268
  return u8aConcatStrict(encoded);
277
269
  }
package/native/Text.d.ts CHANGED
@@ -10,9 +10,10 @@ import type { AnyString, AnyU8a, Inspect, IText, IU8a, Registry } from '../types
10
10
  */
11
11
  export declare class Text extends String implements IText {
12
12
  #private;
13
- createdAtHash?: IU8a;
14
- readonly initialU8aLength?: number;
15
13
  readonly registry: Registry;
14
+ createdAtHash?: IU8a;
15
+ initialU8aLength?: number;
16
+ isStorageFallback?: boolean;
16
17
  constructor(registry: Registry, value?: null | AnyString | AnyU8a | {
17
18
  toString: () => string;
18
19
  });
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "9.11.2",
23
+ "version": "9.12.1",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
@@ -434,8 +434,8 @@
434
434
  }
435
435
  },
436
436
  "dependencies": {
437
- "@babel/runtime": "^7.20.7",
438
- "@polkadot/util": "^10.2.4",
439
- "@polkadot/x-bigint": "^10.2.4"
437
+ "@babel/runtime": "^7.20.13",
438
+ "@polkadot/util": "^10.2.6",
439
+ "@polkadot/x-bigint": "^10.2.6"
440
440
  }
441
441
  }
package/packageInfo.js CHANGED
@@ -7,5 +7,5 @@ export const packageInfo = {
7
7
  name: '@polkadot/types-codec',
8
8
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
9
9
  type: 'esm',
10
- version: '9.11.2'
10
+ version: '9.12.1'
11
11
  };
package/types/codec.d.ts CHANGED
@@ -11,11 +11,35 @@ export interface Inspect {
11
11
  /**
12
12
  * @name Codec
13
13
  * @description
14
- * The base Codec interface. All types implement the interface provided here. Additionally
15
- * implementors can add their own specific interfaces and helpers with getters and functions.
16
- * The Codec Base is however required for operating as an encoding/decoding layer
14
+ * The base Codec interface. All types implement the interface provided here.
15
+ * Additionally implementors can add their own specific interfaces and helpers
16
+ * with getters and functions. The Codec Base is however required for operating
17
+ * as an encoding/decoding layer
17
18
  */
18
19
  export interface Codec {
20
+ /**
21
+ * @description
22
+ * The block at which this value was retrieved/created (set to non-empty when
23
+ * retrieved from storage)
24
+ */
25
+ createdAtHash?: IU8a;
26
+ /**
27
+ * @description
28
+ * The length of the initial encoded value (Only available when the value was
29
+ * constructed from a Uint8Array input)
30
+ */
31
+ initialU8aLength?: number;
32
+ /**
33
+ * @description
34
+ * (internal usage) Indicates that the value was created via a fallback. This
35
+ * is used when with data specified in the metadata when the storage entry is
36
+ * empty.
37
+ *
38
+ * With metadata fallback values (available as defaults on most storage entries)
39
+ * any empty storage item should erturn the default. (This is the same as the
40
+ * implementation on the Substrate runtime)
41
+ */
42
+ isStorageFallback?: boolean;
19
43
  /**
20
44
  * @description The length of the value when encoded as a Uint8Array
21
45
  */
@@ -24,10 +48,6 @@ export interface Codec {
24
48
  * @description Returns a hash of the value
25
49
  */
26
50
  readonly hash: IU8a;
27
- /**
28
- * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
29
- */
30
- readonly initialU8aLength?: number;
31
51
  /**
32
52
  * @description Checks if the value is an empty value
33
53
  */
@@ -36,10 +56,6 @@ export interface Codec {
36
56
  * @description The registry associated with this object
37
57
  */
38
58
  readonly registry: Registry;
39
- /**
40
- * @description The block at which this value was retrieved/created (set to non-empty when retrieved from storage)
41
- */
42
- createdAtHash?: IU8a;
43
59
  /**
44
60
  * @description Compares the value of the input to see if there is a match
45
61
  */
@@ -1,5 +1,6 @@
1
1
  /// <reference types="bn.js" />
2
2
  import type { BN } from '@polkadot/util';
3
+ import type { HexString } from '@polkadot/util/types';
3
4
  import type { Codec } from './codec';
4
5
  export type AnyJson = string | number | boolean | null | undefined | AnyJson[] | {
5
6
  [index: string]: AnyJson;
@@ -13,4 +14,4 @@ export type AnyTuple = Codec[];
13
14
  export type AnyU8a = Uint8Array | number[] | string;
14
15
  export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
15
16
  export type U8aBitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 264 | 512 | 520 | 1024 | 2048;
16
- export type AnyTupleValue = AnyU8a | (Codec | AnyU8a | AnyNumber | AnyString | undefined | null)[];
17
+ export type AnyTupleValue = Exclude<AnyU8a, string> | HexString | (Codec | AnyU8a | AnyNumber | AnyString | undefined | null)[];
@@ -19,6 +19,7 @@ export type RegistryTypes = Record<string, CodecClass | string | Record<string,
19
19
  }>;
20
20
  export interface CodecCreateOptions {
21
21
  blockHash?: Uint8Array | string | null;
22
+ isFallback?: boolean;
22
23
  isOptional?: boolean;
23
24
  isPedantic?: boolean;
24
25
  }