@polkadot/types-codec 8.5.2-1 → 8.5.2-12

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.
Files changed (58) hide show
  1. package/abstract/AbstractArray.d.ts +1 -2
  2. package/abstract/AbstractArray.js +4 -10
  3. package/base/Compact.d.ts +7 -2
  4. package/base/Compact.js +18 -3
  5. package/base/Enum.d.ts +16 -1
  6. package/base/Enum.js +29 -15
  7. package/base/Option.d.ts +7 -2
  8. package/base/Option.js +20 -3
  9. package/base/Tuple.d.ts +7 -1
  10. package/base/Tuple.js +36 -22
  11. package/base/Vec.d.ts +8 -2
  12. package/base/Vec.js +39 -22
  13. package/base/VecFixed.d.ts +7 -1
  14. package/base/VecFixed.js +20 -18
  15. package/cjs/abstract/AbstractArray.js +3 -9
  16. package/cjs/base/Compact.js +18 -3
  17. package/cjs/base/Enum.js +27 -12
  18. package/cjs/base/Option.js +20 -2
  19. package/cjs/base/Tuple.js +35 -21
  20. package/cjs/base/Vec.js +36 -21
  21. package/cjs/base/VecFixed.js +19 -17
  22. package/cjs/extended/BTreeSet.js +6 -5
  23. package/cjs/extended/BitVec.js +4 -4
  24. package/cjs/extended/Bytes.js +5 -5
  25. package/cjs/extended/Map.js +3 -4
  26. package/cjs/extended/OptionBool.js +99 -0
  27. package/cjs/extended/index.js +8 -0
  28. package/cjs/native/Struct.js +33 -19
  29. package/cjs/native/Text.js +3 -3
  30. package/cjs/packageInfo.js +1 -1
  31. package/cjs/utils/decodeU8a.js +8 -8
  32. package/cjs/utils/mapToTypeMap.js +4 -2
  33. package/cjs/utils/sanitize.js +12 -6
  34. package/cjs/utils/typesToMap.js +4 -5
  35. package/extended/BTreeSet.js +7 -6
  36. package/extended/BitVec.js +5 -5
  37. package/extended/Bytes.d.ts +1 -1
  38. package/extended/Bytes.js +6 -6
  39. package/extended/Map.js +4 -5
  40. package/extended/OptionBool.d.ts +40 -0
  41. package/extended/OptionBool.js +88 -0
  42. package/extended/index.d.ts +1 -0
  43. package/extended/index.js +1 -0
  44. package/native/Struct.d.ts +7 -2
  45. package/native/Struct.js +33 -20
  46. package/native/Text.d.ts +2 -2
  47. package/native/Text.js +4 -4
  48. package/package.json +8 -3
  49. package/packageInfo.js +1 -1
  50. package/types/codec.d.ts +1 -2
  51. package/utils/decodeU8a.d.ts +2 -4
  52. package/utils/decodeU8a.js +7 -8
  53. package/utils/mapToTypeMap.d.ts +1 -1
  54. package/utils/mapToTypeMap.js +4 -2
  55. package/utils/sanitize.d.ts +1 -0
  56. package/utils/sanitize.js +10 -6
  57. package/utils/typesToMap.d.ts +1 -1
  58. package/utils/typesToMap.js +3 -5
package/native/Struct.js CHANGED
@@ -1,22 +1,26 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
3
+ import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, 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 */
7
- function decodeStructFromObject(registry, Types, value, jsonMap) {
10
+
11
+
12
+ function decodeStructFromObject(registry, [Types, keys], value, jsonMap) {
8
13
  let jsonObj;
9
- const inputKeys = Object.keys(Types);
10
14
  const typeofArray = Array.isArray(value);
11
15
  const typeofMap = value instanceof Map;
12
16
  assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
13
- assert(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${inputKeys.join(', ')}`);
14
- const raw = new Array(inputKeys.length);
17
+ assert(!typeofArray || value.length === keys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${keys.join(', ')}`);
18
+ const raw = new Array(keys.length);
15
19
 
16
- for (let i = 0; i < inputKeys.length; i++) {
17
- const key = inputKeys[i];
20
+ for (let i = 0; i < keys.length; i++) {
21
+ const key = keys[i];
18
22
  const jsonKey = jsonMap.get(key) || key;
19
- const Type = Types[key];
23
+ const Type = Types[i];
20
24
  let assign;
21
25
 
22
26
  try {
@@ -73,9 +77,12 @@ export class Struct extends Map {
73
77
  #jsonMap;
74
78
  #Types;
75
79
 
76
- constructor(registry, Types, value, jsonMap = new Map()) {
77
- const typeMap = mapToTypeMap(registry, Types);
78
- 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);
80
+ constructor(registry, Types, value, jsonMap = new Map(), {
81
+ definition,
82
+ setDefinition = noopSetDefinition
83
+ } = {}) {
84
+ const typeMap = definition || setDefinition(mapToTypeMap(registry, Types));
85
+ const [decoded, decodedLength] = isU8a(value) || isHex(value) ? decodeU8a(registry, new Array(typeMap[0].length), u8aToU8a(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
79
86
  super(decoded);
80
87
  this.#registry = registry;
81
88
  this.initialU8aLength = decodedLength;
@@ -85,9 +92,16 @@ export class Struct extends Map {
85
92
 
86
93
  static with(Types, jsonMap) {
87
94
  const keys = Object.keys(Types);
95
+ let definition; // eslint-disable-next-line no-return-assign
96
+
97
+ const setDefinition = d => definition = d;
98
+
88
99
  return class extends Struct {
89
100
  constructor(registry, value) {
90
- super(registry, Types, value, jsonMap);
101
+ super(registry, Types, value, jsonMap, {
102
+ definition,
103
+ setDefinition
104
+ });
91
105
  objectProperties(this, keys, k => this.get(k));
92
106
  }
93
107
 
@@ -99,7 +113,7 @@ export class Struct extends Map {
99
113
 
100
114
 
101
115
  get defKeys() {
102
- return Object.keys(this.#Types);
116
+ return this.#Types[1];
103
117
  }
104
118
 
105
119
  getT(key) {
@@ -126,11 +140,10 @@ export class Struct extends Map {
126
140
 
127
141
  get Type() {
128
142
  const result = {};
129
- const defs = Object.entries(this.#Types);
143
+ const [Types, keys] = this.#Types;
130
144
 
131
- for (let i = 0; i < defs.length; i++) {
132
- const [key, Type] = defs[i];
133
- result[key] = new Type(this.registry).toRawType();
145
+ for (let i = 0; i < keys.length; i++) {
146
+ result[keys[i]] = new Types[i](this.registry).toRawType();
134
147
  }
135
148
 
136
149
  return result;
@@ -191,11 +204,11 @@ export class Struct extends Map {
191
204
  */
192
205
 
193
206
 
194
- inspect() {
207
+ inspect(isBare) {
195
208
  const inner = new Array();
196
209
 
197
210
  for (const [k, v] of this.entries()) {
198
- inner.push({ ...v.inspect(),
211
+ inner.push({ ...v.inspect(!isBare || isBoolean(isBare) ? isBare : isBare[k]),
199
212
  name: stringCamelCase(k)
200
213
  });
201
214
  }
@@ -280,7 +293,7 @@ export class Struct extends Map {
280
293
  }
281
294
  }
282
295
 
283
- return u8aConcat(...encoded);
296
+ return u8aConcatStrict(encoded);
284
297
  }
285
298
 
286
299
  }
package/native/Text.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
- import type { AnyU8a, Inspect, IText, IU8a, Registry } from '../types';
2
+ import type { AnyString, AnyU8a, Inspect, IText, IU8a, Registry } from '../types';
3
3
  /**
4
4
  * @name Text
5
5
  * @description
@@ -12,7 +12,7 @@ export declare class Text extends String implements IText {
12
12
  #private;
13
13
  readonly registry: Registry;
14
14
  createdAtHash?: IU8a;
15
- constructor(registry: Registry, value?: null | Text | string | AnyU8a | {
15
+ constructor(registry: Registry, value?: null | AnyString | AnyU8a | {
16
16
  toString: () => string;
17
17
  });
18
18
  /**
package/native/Text.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { assert, compactAddLength, compactFromU8a, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
3
+ import { assert, compactAddLength, compactFromU8aLim, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
4
4
  import { Raw } from "./Raw.js";
5
5
  const MAX_LENGTH = 128 * 1024;
6
6
  /** @internal */
@@ -17,9 +17,9 @@ function decodeText(value) {
17
17
  return [u8aToString(value), 0];
18
18
  }
19
19
 
20
- const [offset, length] = compactFromU8a(value);
21
- const total = offset + length.toNumber();
22
- assert(length.lten(MAX_LENGTH), () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
20
+ const [offset, length] = compactFromU8aLim(value);
21
+ const total = offset + length;
22
+ assert(length <= MAX_LENGTH, () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
23
23
  assert(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
24
24
  return [u8aToString(value.subarray(offset, total)), total];
25
25
  } else if (isHex(value)) {
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.5.2-1",
23
+ "version": "8.5.2-12",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
@@ -172,6 +172,11 @@
172
172
  "require": "./cjs/extended/Map.js",
173
173
  "default": "./extended/Map.js"
174
174
  },
175
+ "./extended/OptionBool": {
176
+ "types": "./extended/OptionBool.d.ts",
177
+ "require": "./cjs/extended/OptionBool.js",
178
+ "default": "./extended/OptionBool.js"
179
+ },
175
180
  "./extended/Range": {
176
181
  "types": "./extended/Range.d.ts",
177
182
  "require": "./cjs/extended/Range.js",
@@ -409,7 +414,7 @@
409
414
  }
410
415
  },
411
416
  "dependencies": {
412
- "@babel/runtime": "^7.18.0",
413
- "@polkadot/util": "^9.2.1"
417
+ "@babel/runtime": "^7.18.3",
418
+ "@polkadot/util": "^9.3.1"
414
419
  }
415
420
  }
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-1'
8
+ version: '8.5.2-12'
9
9
  };
package/types/codec.d.ts CHANGED
@@ -47,7 +47,7 @@ export interface Codec {
47
47
  /**
48
48
  * @description Returns a breakdown of the hex encoding for this Codec
49
49
  */
50
- inspect(): Inspect;
50
+ inspect(isBare?: BareOpts): Inspect;
51
51
  /**
52
52
  * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
53
53
  */
@@ -81,6 +81,5 @@ export interface CodecClass<T = Codec> {
81
81
  readonly __fallbackType?: string;
82
82
  new (registry: Registry, ...args: any[]): T;
83
83
  }
84
- export declare type CodecClassDef<T = Codec> = Record<string, CodecClass<T>>;
85
84
  export declare type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';
86
85
  export declare type ArgsDef = Record<string, CodecClass | string>;
@@ -7,7 +7,5 @@ import type { Codec, CodecClass, Registry } from '../types';
7
7
  * @param result - The result array (will be returned with values pushed)
8
8
  * @param types - The array of CodecClass to decode the U8a against.
9
9
  */
10
- export declare function decodeU8a<T extends Codec = Codec, E = T>(registry: Registry, u8a: Uint8Array, types: CodecClass[] | {
11
- [index: string]: CodecClass;
12
- }, withZip?: boolean): [E[], number];
13
- export declare function decodeU8aVec<T extends Codec = Codec>(registry: Registry, u8a: Uint8Array, start: number, Type: CodecClass<T>, count: number): [T[], number, number];
10
+ export declare function decodeU8a<T extends Codec = Codec, E = T>(registry: Registry, result: unknown[], u8a: Uint8Array, [Types, keys]: [CodecClass[], string[]], withZip?: boolean): [E[], number];
11
+ export declare function decodeU8aVec<T extends Codec = Codec>(registry: Registry, result: unknown[], u8a: Uint8Array, startAt: number, Type: CodecClass<T>): [number, number];
@@ -23,12 +23,11 @@ function getRawType(registry, Type) {
23
23
  */
24
24
 
25
25
 
26
- export function decodeU8a(registry, u8a, types, withZip) {
27
- const [Types, keys] = Array.isArray(types) ? [types, []] : [Object.values(types), Object.keys(types)];
28
- const result = new Array(Types.length);
26
+ export function decodeU8a(registry, result, u8a, [Types, keys], withZip) {
27
+ const count = result.length;
29
28
  let offset = 0;
30
29
 
31
- for (let i = 0; i < Types.length; i++) {
30
+ for (let i = 0; i < count; i++) {
32
31
  try {
33
32
  const value = new Types[i](registry, u8a.subarray(offset));
34
33
  offset += value.initialU8aLength || value.encodedLength;
@@ -42,9 +41,9 @@ export function decodeU8a(registry, u8a, types, withZip) {
42
41
  } // Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but
43
42
  // since we have to do less checks (and these are intensive anyway), much faster
44
43
 
45
- export function decodeU8aVec(registry, u8a, start, Type, count) {
46
- const result = new Array(count);
47
- let offset = start;
44
+ export function decodeU8aVec(registry, result, u8a, startAt, Type) {
45
+ const count = result.length;
46
+ let offset = startAt;
48
47
 
49
48
  for (let i = 0; i < count; i++) {
50
49
  try {
@@ -56,5 +55,5 @@ export function decodeU8aVec(registry, u8a, start, Type, count) {
56
55
  }
57
56
  }
58
57
 
59
- return [result, offset, offset - start];
58
+ return [offset, offset - startAt];
60
59
  }
@@ -2,4 +2,4 @@ import type { CodecClass, Registry } from '../types';
2
2
  /**
3
3
  * @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
4
4
  */
5
- export declare function mapToTypeMap(registry: Registry, input: Record<string, string | CodecClass>): Record<string, CodecClass>;
5
+ export declare function mapToTypeMap(registry: Registry, input: Record<string, string | CodecClass>): [CodecClass[], string[]];
@@ -7,10 +7,12 @@ import { typeToConstructor } from "./typeToConstructor.js";
7
7
 
8
8
  export function mapToTypeMap(registry, input) {
9
9
  const entries = Object.entries(input);
10
- const output = {};
10
+ const count = entries.length;
11
+ const output = [new Array(count), new Array(count)];
11
12
 
12
13
  for (let i = 0; i < entries.length; i++) {
13
- output[entries[i][0]] = typeToConstructor(registry, entries[i][1]);
14
+ output[1][i] = entries[i][0];
15
+ output[0][i] = typeToConstructor(registry, entries[i][1]);
14
16
  }
15
17
 
16
18
  return output;
@@ -2,6 +2,7 @@ interface SanitizeOptions {
2
2
  allowNamespaces?: boolean;
3
3
  }
4
4
  declare type Mapper = (value: string, options?: SanitizeOptions) => string;
5
+ export declare function trim(): Mapper;
5
6
  export declare function findClosing(value: string, start: number): number;
6
7
  export declare function alias(src: string, dest: string, withChecks?: boolean): Mapper;
7
8
  export declare function cleanupCompact(): Mapper;
package/utils/sanitize.js CHANGED
@@ -19,7 +19,12 @@ alias('RawAddress', 'Address'), // lookups, mapped to Address/AccountId as appro
19
19
  alias('Lookup::Source', 'LookupSource'), alias('Lookup::Target', 'LookupTarget'), // HACK duplication between contracts & primitives, however contracts prefixed with exec
20
20
  alias('exec::StorageKey', 'ContractStorageKey'), // flattens tuples with one value, `(AccountId)` -> `AccountId`
21
21
  flattenSingleTuple(), // converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
22
- removeColons()]; // given a starting index, find the closing >
22
+ removeColons(), // remove all trailing spaces - this should always be the last
23
+ trim()]; // given a string, trim it
24
+
25
+ export function trim() {
26
+ return value => value.trim();
27
+ } // given a starting index, find the closing >
23
28
 
24
29
  export function findClosing(value, start) {
25
30
  let depth = 0;
@@ -191,10 +196,11 @@ export function removeWrap(check) {
191
196
  const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types
192
197
 
193
198
  export function sanitize(value, options) {
194
- let result = value.toString();
199
+ const startValue = value.toString();
200
+ let result = startValue;
195
201
 
196
202
  if (!options) {
197
- const memoized = sanitizeMap.get(result);
203
+ const memoized = sanitizeMap.get(startValue);
198
204
 
199
205
  if (memoized) {
200
206
  return memoized;
@@ -205,10 +211,8 @@ export function sanitize(value, options) {
205
211
  result = mappings[i](result, options);
206
212
  }
207
213
 
208
- result = result.trim();
209
-
210
214
  if (!options) {
211
- sanitizeMap.set(value.toString(), result);
215
+ sanitizeMap.set(startValue, result);
212
216
  }
213
217
 
214
218
  return result;
@@ -1,2 +1,2 @@
1
1
  import type { CodecClass, Registry } from '../types';
2
- export declare function typesToMap(registry: Registry, Types: Record<string, CodecClass>): Record<string, string>;
2
+ export declare function typesToMap(registry: Registry, [Types, keys]: [CodecClass[], string[]]): Record<string, string>;
@@ -1,12 +1,10 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- export function typesToMap(registry, Types) {
4
- const entries = Object.entries(Types);
3
+ export function typesToMap(registry, [Types, keys]) {
5
4
  const result = {};
6
5
 
7
- for (let i = 0; i < entries.length; i++) {
8
- const [key, Type] = entries[i];
9
- result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
6
+ for (let i = 0; i < keys.length; i++) {
7
+ result[keys[i]] = registry.getClassName(Types[i]) || new Types[i](registry).toRawType();
10
8
  }
11
9
 
12
10
  return result;