@polkadot/types-codec 9.5.2 → 9.6.2

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 (152) hide show
  1. package/abstract/Array.js +15 -44
  2. package/abstract/Base.js +12 -28
  3. package/abstract/BigInt.js +3 -2
  4. package/abstract/Int.js +25 -52
  5. package/abstract/index.js +1 -0
  6. package/base/Compact.js +21 -45
  7. package/base/DoNotConstruct.js +3 -19
  8. package/base/Enum.js +37 -88
  9. package/base/Int.js +2 -5
  10. package/base/Null.js +12 -23
  11. package/base/Option.js +23 -59
  12. package/base/Result.js +8 -19
  13. package/base/Tuple.js +12 -29
  14. package/base/UInt.js +2 -4
  15. package/base/Vec.js +7 -25
  16. package/base/VecAny.js +2 -2
  17. package/base/VecFixed.js +8 -20
  18. package/base/index.js +1 -0
  19. package/bundle.js +1 -0
  20. package/cjs/abstract/Array.js +13 -47
  21. package/cjs/abstract/Base.js +12 -30
  22. package/cjs/abstract/BigInt.js +1 -4
  23. package/cjs/abstract/Int.js +25 -55
  24. package/cjs/abstract/index.js +0 -3
  25. package/cjs/base/Compact.js +21 -48
  26. package/cjs/base/DoNotConstruct.js +3 -21
  27. package/cjs/base/Enum.js +34 -92
  28. package/cjs/base/Int.js +0 -7
  29. package/cjs/base/Null.js +10 -25
  30. package/cjs/base/Option.js +23 -63
  31. package/cjs/base/Result.js +6 -21
  32. package/cjs/base/Tuple.js +12 -33
  33. package/cjs/base/UInt.js +0 -6
  34. package/cjs/base/Vec.js +7 -31
  35. package/cjs/base/VecAny.js +0 -4
  36. package/cjs/base/VecFixed.js +8 -25
  37. package/cjs/base/index.js +0 -12
  38. package/cjs/bundle.js +0 -13
  39. package/cjs/detectPackage.js +2 -4
  40. package/cjs/extended/BTreeMap.js +1 -5
  41. package/cjs/extended/BTreeSet.js +17 -57
  42. package/cjs/extended/BitVec.js +11 -25
  43. package/cjs/extended/Bytes.js +9 -20
  44. package/cjs/extended/HashMap.js +1 -5
  45. package/cjs/extended/Linkage.js +5 -20
  46. package/cjs/extended/Map.js +16 -62
  47. package/cjs/extended/OptionBool.js +8 -21
  48. package/cjs/extended/Range.js +3 -13
  49. package/cjs/extended/RangeInclusive.js +1 -6
  50. package/cjs/extended/Type.js +1 -7
  51. package/cjs/extended/U8aFixed.js +2 -15
  52. package/cjs/extended/WrapperKeepOpaque.js +8 -28
  53. package/cjs/extended/WrapperOpaque.js +2 -8
  54. package/cjs/extended/index.js +0 -14
  55. package/cjs/index.js +0 -3
  56. package/cjs/native/Bool.js +14 -32
  57. package/cjs/native/Date.js +18 -40
  58. package/cjs/native/Float.js +11 -29
  59. package/cjs/native/Json.js +14 -33
  60. package/cjs/native/Raw.js +18 -41
  61. package/cjs/native/Set.js +21 -67
  62. package/cjs/native/Struct.data.js +1 -0
  63. package/cjs/native/Struct.js +33 -83
  64. package/cjs/native/Text.js +20 -44
  65. package/cjs/native/index.js +0 -8
  66. package/cjs/packageInfo.js +3 -1
  67. package/cjs/primitive/F32.js +0 -3
  68. package/cjs/primitive/F64.js +0 -3
  69. package/cjs/primitive/I128.js +0 -3
  70. package/cjs/primitive/I16.js +0 -3
  71. package/cjs/primitive/I256.js +0 -3
  72. package/cjs/primitive/I32.js +0 -3
  73. package/cjs/primitive/I64.js +0 -3
  74. package/cjs/primitive/I8.js +0 -3
  75. package/cjs/primitive/U128.js +0 -3
  76. package/cjs/primitive/U16.js +0 -3
  77. package/cjs/primitive/U256.js +0 -3
  78. package/cjs/primitive/U32.js +0 -3
  79. package/cjs/primitive/U64.js +0 -3
  80. package/cjs/primitive/U8.js +0 -3
  81. package/cjs/primitive/USize.js +0 -4
  82. package/cjs/primitive/index.js +0 -15
  83. package/cjs/types/index.js +0 -8
  84. package/cjs/utils/compareArray.js +1 -4
  85. package/cjs/utils/compareMap.js +4 -9
  86. package/cjs/utils/compareSet.js +4 -6
  87. package/cjs/utils/decodeU8a.js +10 -18
  88. package/cjs/utils/index.js +0 -9
  89. package/cjs/utils/mapToTypeMap.js +0 -4
  90. package/cjs/utils/sanitize.js +66 -71
  91. package/cjs/utils/sortValues.js +5 -18
  92. package/cjs/utils/typeToConstructor.js +1 -2
  93. package/cjs/utils/typesToMap.js +1 -3
  94. package/cjs/utils/util.js +1 -2
  95. package/detectOther.js +2 -0
  96. package/detectPackage.js +2 -0
  97. package/extended/BTreeMap.js +1 -2
  98. package/extended/BTreeSet.js +17 -53
  99. package/extended/BitVec.js +13 -22
  100. package/extended/Bytes.js +11 -17
  101. package/extended/HashMap.js +1 -2
  102. package/extended/Linkage.js +5 -12
  103. package/extended/Map.js +16 -54
  104. package/extended/OptionBool.js +8 -17
  105. package/extended/Range.js +4 -11
  106. package/extended/RangeInclusive.js +1 -3
  107. package/extended/Type.js +3 -4
  108. package/extended/U8aFixed.js +4 -12
  109. package/extended/WrapperKeepOpaque.js +8 -23
  110. package/extended/WrapperOpaque.js +2 -5
  111. package/extended/index.js +1 -0
  112. package/index.js +1 -0
  113. package/native/Bool.js +16 -30
  114. package/native/Date.js +18 -37
  115. package/native/Float.js +12 -27
  116. package/native/Json.js +16 -30
  117. package/native/Raw.js +20 -39
  118. package/native/Set.js +21 -63
  119. package/native/Struct.data.js +1 -0
  120. package/native/Struct.js +33 -80
  121. package/native/Text.js +20 -40
  122. package/native/index.js +1 -0
  123. package/package.json +2 -2
  124. package/packageInfo.js +3 -1
  125. package/primitive/F32.js +2 -1
  126. package/primitive/F64.js +2 -1
  127. package/primitive/I128.js +2 -1
  128. package/primitive/I16.js +2 -1
  129. package/primitive/I256.js +2 -1
  130. package/primitive/I32.js +2 -1
  131. package/primitive/I64.js +2 -1
  132. package/primitive/I8.js +2 -1
  133. package/primitive/U128.js +2 -1
  134. package/primitive/U16.js +2 -1
  135. package/primitive/U256.js +2 -1
  136. package/primitive/U32.js +2 -1
  137. package/primitive/U64.js +2 -1
  138. package/primitive/U8.js +2 -1
  139. package/primitive/USize.js +2 -2
  140. package/primitive/index.js +1 -0
  141. package/types/index.js +1 -0
  142. package/utils/compareArray.js +4 -3
  143. package/utils/compareMap.js +4 -7
  144. package/utils/compareSet.js +4 -5
  145. package/utils/decodeU8a.js +12 -15
  146. package/utils/index.js +1 -0
  147. package/utils/mapToTypeMap.js +2 -3
  148. package/utils/sanitize.js +66 -60
  149. package/utils/sortValues.js +6 -15
  150. package/utils/typeToConstructor.js +1 -0
  151. package/utils/typesToMap.js +1 -2
  152. package/utils/util.js +1 -0
package/extended/Map.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { compactFromU8aLim, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
4
5
  import { AbstractArray } from "../abstract/Array.js";
5
6
  import { Enum } from "../base/Enum.js";
@@ -7,34 +8,27 @@ import { Raw } from "../native/Raw.js";
7
8
  import { Struct } from "../native/Struct.js";
8
9
  import { compareMap, decodeU8a, sortMap, typeToConstructor } from "../utils/index.js";
9
10
  const l = logger('Map');
10
- /** @internal */
11
11
 
12
+ /** @internal */
12
13
  function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
13
14
  const output = new Map();
14
15
  const [offset, count] = compactFromU8aLim(u8a);
15
16
  const types = [];
16
-
17
17
  for (let i = 0; i < count; i++) {
18
18
  types.push(KeyClass, ValClass);
19
19
  }
20
-
21
20
  const [values, decodedLength] = decodeU8a(registry, new Array(types.length), u8a.subarray(offset), [types, []]);
22
-
23
21
  for (let i = 0; i < values.length; i += 2) {
24
22
  output.set(values[i], values[i + 1]);
25
23
  }
26
-
27
24
  return [KeyClass, ValClass, output, offset + decodedLength];
28
25
  }
29
- /** @internal */
30
-
31
26
 
27
+ /** @internal */
32
28
  function decodeMapFromMap(registry, KeyClass, ValClass, value) {
33
29
  const output = new Map();
34
-
35
30
  for (const [key, val] of value.entries()) {
36
31
  const isComplex = KeyClass.prototype instanceof AbstractArray || KeyClass.prototype instanceof Struct || KeyClass.prototype instanceof Enum;
37
-
38
32
  try {
39
33
  output.set(key instanceof KeyClass ? key : new KeyClass(registry, isComplex && typeof key === 'string' ? JSON.parse(key) : key), val instanceof ValClass ? val : new ValClass(registry, val));
40
34
  } catch (error) {
@@ -42,9 +36,9 @@ function decodeMapFromMap(registry, KeyClass, ValClass, value) {
42
36
  throw error;
43
37
  }
44
38
  }
45
-
46
39
  return [KeyClass, ValClass, output, 0];
47
40
  }
41
+
48
42
  /**
49
43
  * Decode input to pass into constructor.
50
44
  *
@@ -60,12 +54,9 @@ function decodeMapFromMap(registry, KeyClass, ValClass, value) {
60
54
  * @param jsonMap
61
55
  * @internal
62
56
  */
63
-
64
-
65
57
  function decodeMap(registry, keyType, valType, value) {
66
58
  const KeyClass = typeToConstructor(registry, keyType);
67
59
  const ValClass = typeToConstructor(registry, valType);
68
-
69
60
  if (!value) {
70
61
  return [KeyClass, ValClass, new Map(), 0];
71
62
  } else if (isU8a(value) || isHex(value)) {
@@ -75,15 +66,12 @@ function decodeMap(registry, keyType, valType, value) {
75
66
  } else if (isObject(value)) {
76
67
  return decodeMapFromMap(registry, KeyClass, ValClass, new Map(Object.entries(value)));
77
68
  }
78
-
79
69
  throw new Error('Map: cannot decode type');
80
70
  }
81
-
82
71
  export class CodecMap extends Map {
83
72
  #KeyClass;
84
73
  #ValClass;
85
74
  #type;
86
-
87
75
  constructor(registry, keyType, valType, rawValue, type = 'HashMap') {
88
76
  const [KeyClass, ValClass, decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
89
77
  super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
@@ -93,146 +81,120 @@ export class CodecMap extends Map {
93
81
  this.#ValClass = ValClass;
94
82
  this.#type = type;
95
83
  }
84
+
96
85
  /**
97
86
  * @description The length of the value when encoded as a Uint8Array
98
87
  */
99
-
100
-
101
88
  get encodedLength() {
102
89
  let len = compactToU8a(this.size).length;
103
-
104
90
  for (const [k, v] of this.entries()) {
105
91
  len += k.encodedLength + v.encodedLength;
106
92
  }
107
-
108
93
  return len;
109
94
  }
95
+
110
96
  /**
111
97
  * @description Returns a hash of the value
112
98
  */
113
-
114
-
115
99
  get hash() {
116
100
  return this.registry.hash(this.toU8a());
117
101
  }
102
+
118
103
  /**
119
104
  * @description Checks if the value is an empty value
120
105
  */
121
-
122
-
123
106
  get isEmpty() {
124
107
  return this.size === 0;
125
108
  }
109
+
126
110
  /**
127
111
  * @description Compares the value of the input to see if there is a match
128
112
  */
129
-
130
-
131
113
  eq(other) {
132
114
  return compareMap(this, other);
133
115
  }
116
+
134
117
  /**
135
118
  * @description Returns a breakdown of the hex encoding for this Codec
136
119
  */
137
-
138
-
139
120
  inspect() {
140
121
  const inner = new Array();
141
-
142
122
  for (const [k, v] of this.entries()) {
143
123
  inner.push(k.inspect());
144
124
  inner.push(v.inspect());
145
125
  }
146
-
147
126
  return {
148
127
  inner,
149
128
  outer: [compactToU8a(this.size)]
150
129
  };
151
130
  }
131
+
152
132
  /**
153
133
  * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
154
134
  */
155
-
156
-
157
135
  toHex() {
158
136
  return u8aToHex(this.toU8a());
159
137
  }
138
+
160
139
  /**
161
140
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
162
141
  */
163
-
164
-
165
142
  toHuman(isExtended) {
166
143
  const json = {};
167
-
168
144
  for (const [k, v] of this.entries()) {
169
145
  json[k instanceof Raw && k.isAscii ? k.toUtf8() : k.toString()] = v.toHuman(isExtended);
170
146
  }
171
-
172
147
  return json;
173
148
  }
149
+
174
150
  /**
175
151
  * @description Converts the Object to JSON, typically used for RPC transfers
176
152
  */
177
-
178
-
179
153
  toJSON() {
180
154
  const json = {};
181
-
182
155
  for (const [k, v] of this.entries()) {
183
156
  json[k.toString()] = v.toJSON();
184
157
  }
185
-
186
158
  return json;
187
159
  }
160
+
188
161
  /**
189
162
  * @description Converts the value in a best-fit primitive form
190
163
  */
191
-
192
-
193
164
  toPrimitive() {
194
165
  const json = {};
195
-
196
166
  for (const [k, v] of this.entries()) {
197
167
  json[k instanceof Raw && k.isAscii ? k.toUtf8() : k.toString()] = v.toPrimitive();
198
168
  }
199
-
200
169
  return json;
201
170
  }
171
+
202
172
  /**
203
173
  * @description Returns the base runtime type name for this instance
204
174
  */
205
-
206
-
207
175
  toRawType() {
208
176
  return `${this.#type}<${this.registry.getClassName(this.#KeyClass) || new this.#KeyClass(this.registry).toRawType()},${this.registry.getClassName(this.#ValClass) || new this.#ValClass(this.registry).toRawType()}>`;
209
177
  }
178
+
210
179
  /**
211
180
  * @description Returns the string representation of the value
212
181
  */
213
-
214
-
215
182
  toString() {
216
183
  return stringify(this.toJSON());
217
184
  }
185
+
218
186
  /**
219
187
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
220
188
  * @param isBare true when the value has none of the type-specific prefixes (internal)
221
189
  */
222
-
223
-
224
190
  toU8a(isBare) {
225
191
  const encoded = new Array();
226
-
227
192
  if (!isBare) {
228
193
  encoded.push(compactToU8a(this.size));
229
194
  }
230
-
231
195
  for (const [k, v] of this.entries()) {
232
196
  encoded.push(k.toU8a(isBare), v.toU8a(isBare));
233
197
  }
234
-
235
198
  return u8aConcatStrict(encoded);
236
199
  }
237
-
238
200
  }
@@ -1,9 +1,9 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { isHex, isU8a, u8aToU8a } from '@polkadot/util';
4
5
  import { Option } from "../base/Option.js";
5
6
  import { bool as Bool } from "../native/Bool.js";
6
-
7
7
  function decodeU8a(registry, value) {
8
8
  // Encoded as -
9
9
  // - 0 = None
@@ -11,71 +11,62 @@ function decodeU8a(registry, value) {
11
11
  // - 2 = False
12
12
  return value[0] === 0 ? null : new Bool(registry, value[0] === 1);
13
13
  }
14
+
14
15
  /**
15
16
  * @name OptionBool
16
17
  * @description A specific implementation of Option<bool> than allows for single-byte encoding
17
18
  */
18
-
19
-
20
19
  export class OptionBool extends Option {
21
20
  constructor(registry, value) {
22
21
  super(registry, Bool, isU8a(value) || isHex(value) ? decodeU8a(registry, u8aToU8a(value)) : value);
23
22
  this.initialU8aLength = 1;
24
23
  }
24
+
25
25
  /**
26
26
  * @description The length of the value when encoded as a Uint8Array
27
27
  */
28
-
29
-
30
28
  get encodedLength() {
31
29
  return 1;
32
30
  }
31
+
33
32
  /**
34
33
  * @description Checks if the value is an empty value (always false)
35
34
  */
36
-
37
-
38
35
  get isFalse() {
39
36
  return this.isSome ? !this.value.valueOf() : false;
40
37
  }
38
+
41
39
  /**
42
40
  * @description Checks if the value is an empty value (always false)
43
41
  */
44
-
45
-
46
42
  get isTrue() {
47
43
  return this.isSome ? this.value.valueOf() : false;
48
44
  }
45
+
49
46
  /**
50
47
  * @description Returns a breakdown of the hex encoding for this Codec
51
48
  */
52
-
53
-
54
49
  inspect() {
55
50
  return {
56
51
  outer: [this.toU8a()]
57
52
  };
58
53
  }
54
+
59
55
  /**
60
56
  * @description Returns the base runtime type name for this instance
61
57
  */
62
-
63
-
64
58
  toRawType(isBare) {
65
59
  return isBare ? 'bool' : 'Option<bool>';
66
60
  }
61
+
67
62
  /**
68
63
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
69
64
  * @param isBare true when the value has none of the type-specific prefixes (internal)
70
65
  */
71
-
72
-
73
66
  toU8a(isBare) {
74
67
  if (isBare) {
75
68
  return super.toU8a(true);
76
69
  }
77
-
78
70
  return this.isSome ? new Uint8Array([this.isTrue ? 1 : 2]) : new Uint8Array([0]);
79
71
  }
80
-
81
72
  }
package/extended/Range.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { Tuple } from "../base/Tuple.js";
4
3
 
4
+ import { Tuple } from "../base/Tuple.js";
5
5
  /**
6
6
  * @name Range
7
7
  * @description
@@ -9,45 +9,38 @@ import { Tuple } from "../base/Tuple.js";
9
9
  */
10
10
  export class Range extends Tuple {
11
11
  #rangeName;
12
-
13
12
  constructor(registry, Type, value, {
14
13
  rangeName = 'Range'
15
14
  } = {}) {
16
15
  super(registry, [Type, Type], value);
17
16
  this.#rangeName = rangeName;
18
17
  }
19
-
20
18
  static with(Type) {
21
19
  return class extends Range {
22
20
  constructor(registry, value) {
23
21
  super(registry, Type, value);
24
22
  }
25
-
26
23
  };
27
24
  }
25
+
28
26
  /**
29
27
  * @description Returns the starting range value
30
28
  */
31
-
32
-
33
29
  get start() {
34
30
  return this[0];
35
31
  }
32
+
36
33
  /**
37
34
  * @description Returns the ending range value
38
35
  */
39
-
40
-
41
36
  get end() {
42
37
  return this[1];
43
38
  }
39
+
44
40
  /**
45
41
  * @description Returns the base runtime type name for this instance
46
42
  */
47
-
48
-
49
43
  toRawType() {
50
44
  return `${this.#rangeName}<${this.start.toRawType()}>`;
51
45
  }
52
-
53
46
  }
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { Range } from "./Range.js";
4
5
  export class RangeInclusive extends Range {
5
6
  constructor(registry, Type, value) {
@@ -7,14 +8,11 @@ export class RangeInclusive extends Range {
7
8
  rangeName: 'RangeInclusive'
8
9
  });
9
10
  }
10
-
11
11
  static with(Type) {
12
12
  return class extends RangeInclusive {
13
13
  constructor(registry, value) {
14
14
  super(registry, Type, value);
15
15
  }
16
-
17
16
  };
18
17
  }
19
-
20
18
  }
package/extended/Type.js CHANGED
@@ -1,7 +1,9 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { Text } from "../native/Text.js";
4
5
  import { sanitize } from "../utils/index.js";
6
+
5
7
  /**
6
8
  * @name Type
7
9
  * @description
@@ -9,19 +11,16 @@ import { sanitize } from "../utils/index.js";
9
11
  * on what Text provides us, however we also adjust the types received from the runtime,
10
12
  * i.e. we remove the `T::` prefixes found in some types for consistency across implementation.
11
13
  */
12
-
13
14
  export class Type extends Text {
14
15
  constructor(registry, value = '') {
15
16
  super(registry, value);
16
17
  this.setOverride(sanitize(this.toString()));
17
18
  }
19
+
18
20
  /**
19
21
  * @description Returns the base runtime type name for this instance
20
22
  */
21
-
22
-
23
23
  toRawType() {
24
24
  return 'Type';
25
25
  }
26
-
27
26
  }
@@ -1,56 +1,48 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { isU8a, u8aToU8a } from '@polkadot/util';
4
5
  import { Raw } from "../native/Raw.js";
5
- /** @internal */
6
6
 
7
+ /** @internal */
7
8
  function decodeU8aFixed(value, bitLength) {
8
9
  const u8a = u8aToU8a(value);
9
10
  const byteLength = bitLength / 8;
10
-
11
11
  if (!u8a.length) {
12
12
  return [new Uint8Array(byteLength), 0];
13
13
  }
14
-
15
14
  if (isU8a(value) ? u8a.length < byteLength : u8a.length !== byteLength) {
16
15
  throw new Error(`Expected input with ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
17
16
  }
18
-
19
17
  return [u8a.subarray(0, byteLength), byteLength];
20
18
  }
19
+
21
20
  /**
22
21
  * @name U8aFixed
23
22
  * @description
24
23
  * A U8a that manages a a sequence of bytes up to the specified bitLength. Not meant
25
24
  * to be used directly, rather is should be subclassed with the specific lengths.
26
25
  */
27
-
28
-
29
26
  export class U8aFixed extends Raw {
30
27
  constructor(registry, value = new Uint8Array(), bitLength = 256) {
31
28
  const [u8a, decodedLength] = decodeU8aFixed(value, bitLength);
32
29
  super(registry, u8a, decodedLength);
33
30
  }
34
-
35
31
  static with(bitLength, typeName) {
36
32
  return class extends U8aFixed {
37
33
  constructor(registry, value) {
38
34
  super(registry, value, bitLength);
39
35
  }
40
-
41
36
  toRawType() {
42
37
  return typeName || super.toRawType();
43
38
  }
44
-
45
39
  };
46
40
  }
41
+
47
42
  /**
48
43
  * @description Returns the base runtime type name for this instance
49
44
  */
50
-
51
-
52
45
  toRawType() {
53
46
  return `[u8;${this.length}]`;
54
47
  }
55
-
56
48
  }
@@ -1,13 +1,12 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { compactAddLength, compactStripLength, compactToU8a, isHex, isU8a, u8aToU8a } from '@polkadot/util';
4
5
  import { Raw } from "../native/Raw.js";
5
6
  import { typeToConstructor } from "../utils/index.js";
6
7
  import { Bytes } from "./Bytes.js";
7
-
8
8
  function decodeRaw(registry, typeName, value) {
9
9
  const Type = typeToConstructor(registry, typeName);
10
-
11
10
  if (isU8a(value) || isHex(value)) {
12
11
  try {
13
12
  const [, u8a] = isHex(value) ? [0, u8aToU8a(value)] : value instanceof Raw ? [0, value.subarray()] : compactStripLength(value);
@@ -16,16 +15,13 @@ function decodeRaw(registry, typeName, value) {
16
15
  return [Type, null, value];
17
16
  }
18
17
  }
19
-
20
18
  const instance = new Type(registry, value);
21
19
  return [Type, instance, compactAddLength(instance.toU8a())];
22
20
  }
23
-
24
21
  export class WrapperKeepOpaque extends Bytes {
25
22
  #Type;
26
23
  #decoded;
27
24
  #opaqueName;
28
-
29
25
  constructor(registry, typeName, value, {
30
26
  opaqueName = 'WrapperKeepOpaque'
31
27
  } = {}) {
@@ -35,28 +31,24 @@ export class WrapperKeepOpaque extends Bytes {
35
31
  this.#decoded = decoded;
36
32
  this.#opaqueName = opaqueName;
37
33
  }
38
-
39
34
  static with(Type) {
40
35
  return class extends WrapperKeepOpaque {
41
36
  constructor(registry, value) {
42
37
  super(registry, Type, value);
43
38
  }
44
-
45
39
  };
46
40
  }
41
+
47
42
  /**
48
43
  * @description Checks if the wrapper is decodable
49
44
  */
50
-
51
-
52
45
  get isDecoded() {
53
46
  return !!this.#decoded;
54
47
  }
48
+
55
49
  /**
56
50
  * @description Returns a breakdown of the hex encoding for this Codec
57
51
  */
58
-
59
-
60
52
  inspect() {
61
53
  return this.#decoded ? {
62
54
  inner: [this.#decoded.inspect()],
@@ -65,49 +57,42 @@ export class WrapperKeepOpaque extends Bytes {
65
57
  outer: [compactToU8a(this.length), this.toU8a(true)]
66
58
  };
67
59
  }
60
+
68
61
  /**
69
62
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
70
63
  */
71
-
72
-
73
64
  toHuman(isExtended) {
74
65
  return this.#decoded ? this.#decoded.toHuman(isExtended) : super.toHuman();
75
66
  }
67
+
76
68
  /**
77
69
  * @description Converts the value in a best-fit primitive form
78
70
  */
79
-
80
-
81
71
  toPrimitive() {
82
72
  return this.#decoded ? this.#decoded.toPrimitive() : super.toPrimitive();
83
73
  }
74
+
84
75
  /**
85
76
  * @description Returns the base runtime type name for this instance
86
77
  */
87
-
88
-
89
78
  toRawType() {
90
79
  return `${this.#opaqueName}<${this.registry.getClassName(this.#Type) || (this.#decoded ? this.#decoded.toRawType() : new this.#Type(this.registry).toRawType())}>`;
91
80
  }
81
+
92
82
  /**
93
83
  * @description Converts the Object to to a string (either decoded or bytes)
94
84
  */
95
-
96
-
97
85
  toString() {
98
86
  return this.#decoded ? this.#decoded.toString() : super.toString();
99
87
  }
88
+
100
89
  /**
101
90
  * @description Returns the decoded that the WrapperKeepOpaque represents (if available), throws if non-decodable
102
91
  */
103
-
104
-
105
92
  unwrap() {
106
93
  if (!this.#decoded) {
107
94
  throw new Error(`${this.#opaqueName}: unwrapping an undecodable value`);
108
95
  }
109
-
110
96
  return this.#decoded;
111
97
  }
112
-
113
98
  }
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { WrapperKeepOpaque } from "./WrapperKeepOpaque.js";
4
5
  export class WrapperOpaque extends WrapperKeepOpaque {
5
6
  constructor(registry, typeName, value) {
@@ -7,22 +8,18 @@ export class WrapperOpaque extends WrapperKeepOpaque {
7
8
  opaqueName: 'WrapperOpaque'
8
9
  });
9
10
  }
10
-
11
11
  static with(Type) {
12
12
  return class extends WrapperOpaque {
13
13
  constructor(registry, value) {
14
14
  super(registry, Type, value);
15
15
  }
16
-
17
16
  };
18
17
  }
18
+
19
19
  /**
20
20
  * @description The inner value for this wrapper, in all cases it _should_ be decodable (unlike KeepOpaque)
21
21
  */
22
-
23
-
24
22
  get inner() {
25
23
  return this.unwrap();
26
24
  }
27
-
28
25
  }
package/extended/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  export { BitVec } from "./BitVec.js";
4
5
  export { BTreeMap } from "./BTreeMap.js";
5
6
  export { BTreeSet } from "./BTreeSet.js";
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import "./detectPackage.js";
4
5
  export * from "./bundle.js";