@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.
- package/abstract/AbstractArray.d.ts +1 -2
- package/abstract/AbstractArray.js +4 -10
- package/base/Compact.d.ts +7 -2
- package/base/Compact.js +18 -3
- package/base/Enum.d.ts +16 -1
- package/base/Enum.js +29 -15
- package/base/Option.d.ts +7 -2
- package/base/Option.js +20 -3
- package/base/Tuple.d.ts +7 -1
- package/base/Tuple.js +36 -22
- package/base/Vec.d.ts +8 -2
- package/base/Vec.js +39 -22
- package/base/VecFixed.d.ts +7 -1
- package/base/VecFixed.js +20 -18
- package/cjs/abstract/AbstractArray.js +3 -9
- package/cjs/base/Compact.js +18 -3
- package/cjs/base/Enum.js +27 -12
- package/cjs/base/Option.js +20 -2
- package/cjs/base/Tuple.js +35 -21
- package/cjs/base/Vec.js +36 -21
- package/cjs/base/VecFixed.js +19 -17
- package/cjs/extended/BTreeSet.js +6 -5
- package/cjs/extended/BitVec.js +4 -4
- package/cjs/extended/Bytes.js +5 -5
- package/cjs/extended/Map.js +3 -4
- package/cjs/extended/OptionBool.js +99 -0
- package/cjs/extended/index.js +8 -0
- package/cjs/native/Struct.js +33 -19
- package/cjs/native/Text.js +3 -3
- package/cjs/packageInfo.js +1 -1
- package/cjs/utils/decodeU8a.js +8 -8
- package/cjs/utils/mapToTypeMap.js +4 -2
- package/cjs/utils/sanitize.js +12 -6
- package/cjs/utils/typesToMap.js +4 -5
- package/extended/BTreeSet.js +7 -6
- package/extended/BitVec.js +5 -5
- package/extended/Bytes.d.ts +1 -1
- package/extended/Bytes.js +6 -6
- package/extended/Map.js +4 -5
- package/extended/OptionBool.d.ts +40 -0
- package/extended/OptionBool.js +88 -0
- package/extended/index.d.ts +1 -0
- package/extended/index.js +1 -0
- package/native/Struct.d.ts +7 -2
- package/native/Struct.js +33 -20
- package/native/Text.d.ts +2 -2
- package/native/Text.js +4 -4
- package/package.json +8 -3
- package/packageInfo.js +1 -1
- package/types/codec.d.ts +1 -2
- package/utils/decodeU8a.d.ts +2 -4
- package/utils/decodeU8a.js +7 -8
- package/utils/mapToTypeMap.d.ts +1 -1
- package/utils/mapToTypeMap.js +4 -2
- package/utils/sanitize.d.ts +1 -0
- package/utils/sanitize.js +10 -6
- package/utils/typesToMap.d.ts +1 -1
- package/utils/typesToMap.js +3 -5
package/cjs/extended/index.js
CHANGED
|
@@ -51,6 +51,12 @@ Object.defineProperty(exports, "Map", {
|
|
|
51
51
|
return _Map.CodecMap;
|
|
52
52
|
}
|
|
53
53
|
});
|
|
54
|
+
Object.defineProperty(exports, "OptionBool", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: function () {
|
|
57
|
+
return _OptionBool.OptionBool;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
54
60
|
Object.defineProperty(exports, "Range", {
|
|
55
61
|
enumerable: true,
|
|
56
62
|
get: function () {
|
|
@@ -102,6 +108,8 @@ var _Linkage = require("./Linkage");
|
|
|
102
108
|
|
|
103
109
|
var _Map = require("./Map");
|
|
104
110
|
|
|
111
|
+
var _OptionBool = require("./OptionBool");
|
|
112
|
+
|
|
105
113
|
var _Range = require("./Range");
|
|
106
114
|
|
|
107
115
|
var _RangeInclusive = require("./RangeInclusive");
|
package/cjs/native/Struct.js
CHANGED
|
@@ -11,21 +11,25 @@ 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 */
|
|
16
|
-
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
function decodeStructFromObject(registry, _ref, value, jsonMap) {
|
|
21
|
+
let [Types, keys] = _ref;
|
|
17
22
|
let jsonObj;
|
|
18
|
-
const inputKeys = Object.keys(Types);
|
|
19
23
|
const typeofArray = Array.isArray(value);
|
|
20
24
|
const typeofMap = value instanceof Map;
|
|
21
25
|
(0, _util.assert)(typeofArray || typeofMap || (0, _util.isObject)(value), () => `Struct: Cannot decode value ${(0, _util.stringify)(value)} (typeof ${typeof value}), expected an input object, map or array`);
|
|
22
|
-
(0, _util.assert)(!typeofArray || value.length ===
|
|
23
|
-
const raw = new Array(
|
|
26
|
+
(0, _util.assert)(!typeofArray || value.length === keys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${keys.join(', ')}`);
|
|
27
|
+
const raw = new Array(keys.length);
|
|
24
28
|
|
|
25
|
-
for (let i = 0; i <
|
|
26
|
-
const key =
|
|
29
|
+
for (let i = 0; i < keys.length; i++) {
|
|
30
|
+
const key = keys[i];
|
|
27
31
|
const jsonKey = jsonMap.get(key) || key;
|
|
28
|
-
const Type = Types[
|
|
32
|
+
const Type = Types[i];
|
|
29
33
|
let assign;
|
|
30
34
|
|
|
31
35
|
try {
|
|
@@ -84,8 +88,12 @@ 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
|
-
|
|
88
|
-
|
|
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));
|
|
96
|
+
const [decoded, decodedLength] = (0, _util.isU8a)(value) || (0, _util.isHex)(value) ? (0, _utils.decodeU8a)(registry, new Array(typeMap[0].length), (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;
|
|
91
99
|
this.initialU8aLength = decodedLength;
|
|
@@ -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
|
|
|
@@ -109,7 +124,7 @@ class Struct extends Map {
|
|
|
109
124
|
|
|
110
125
|
|
|
111
126
|
get defKeys() {
|
|
112
|
-
return
|
|
127
|
+
return this.#Types[1];
|
|
113
128
|
}
|
|
114
129
|
|
|
115
130
|
getT(key) {
|
|
@@ -136,11 +151,10 @@ class Struct extends Map {
|
|
|
136
151
|
|
|
137
152
|
get Type() {
|
|
138
153
|
const result = {};
|
|
139
|
-
const
|
|
154
|
+
const [Types, keys] = this.#Types;
|
|
140
155
|
|
|
141
|
-
for (let i = 0; i <
|
|
142
|
-
|
|
143
|
-
result[key] = new Type(this.registry).toRawType();
|
|
156
|
+
for (let i = 0; i < keys.length; i++) {
|
|
157
|
+
result[keys[i]] = new Types[i](this.registry).toRawType();
|
|
144
158
|
}
|
|
145
159
|
|
|
146
160
|
return result;
|
|
@@ -201,11 +215,11 @@ class Struct extends Map {
|
|
|
201
215
|
*/
|
|
202
216
|
|
|
203
217
|
|
|
204
|
-
inspect() {
|
|
218
|
+
inspect(isBare) {
|
|
205
219
|
const inner = new Array();
|
|
206
220
|
|
|
207
221
|
for (const [k, v] of this.entries()) {
|
|
208
|
-
inner.push({ ...v.inspect(),
|
|
222
|
+
inner.push({ ...v.inspect(!isBare || (0, _util.isBoolean)(isBare) ? isBare : isBare[k]),
|
|
209
223
|
name: (0, _util.stringCamelCase)(k)
|
|
210
224
|
});
|
|
211
225
|
}
|
|
@@ -290,7 +304,7 @@ class Struct extends Map {
|
|
|
290
304
|
}
|
|
291
305
|
}
|
|
292
306
|
|
|
293
|
-
return (0, _util.
|
|
307
|
+
return (0, _util.u8aConcatStrict)(encoded);
|
|
294
308
|
}
|
|
295
309
|
|
|
296
310
|
}
|
package/cjs/native/Text.js
CHANGED
|
@@ -26,9 +26,9 @@ function decodeText(value) {
|
|
|
26
26
|
return [(0, _util.u8aToString)(value), 0];
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
const [offset, length] = (0, _util.
|
|
30
|
-
const total = offset + length
|
|
31
|
-
(0, _util.assert)(length
|
|
29
|
+
const [offset, length] = (0, _util.compactFromU8aLim)(value);
|
|
30
|
+
const total = offset + length;
|
|
31
|
+
(0, _util.assert)(length <= MAX_LENGTH, () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
|
32
32
|
(0, _util.assert)(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
|
33
33
|
return [(0, _util.u8aToString)(value.subarray(offset, total)), total];
|
|
34
34
|
} else if ((0, _util.isHex)(value)) {
|
package/cjs/packageInfo.js
CHANGED
package/cjs/utils/decodeU8a.js
CHANGED
|
@@ -31,12 +31,12 @@ function getRawType(registry, Type) {
|
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
33
|
|
|
34
|
-
function decodeU8a(registry, u8a,
|
|
35
|
-
|
|
36
|
-
const
|
|
34
|
+
function decodeU8a(registry, result, u8a, _ref, withZip) {
|
|
35
|
+
let [Types, keys] = _ref;
|
|
36
|
+
const count = result.length;
|
|
37
37
|
let offset = 0;
|
|
38
38
|
|
|
39
|
-
for (let i = 0; i <
|
|
39
|
+
for (let i = 0; i < count; i++) {
|
|
40
40
|
try {
|
|
41
41
|
const value = new Types[i](registry, u8a.subarray(offset));
|
|
42
42
|
offset += value.initialU8aLength || value.encodedLength;
|
|
@@ -51,9 +51,9 @@ function decodeU8a(registry, u8a, types, withZip) {
|
|
|
51
51
|
// since we have to do less checks (and these are intensive anyway), much faster
|
|
52
52
|
|
|
53
53
|
|
|
54
|
-
function decodeU8aVec(registry, u8a,
|
|
55
|
-
const
|
|
56
|
-
let offset =
|
|
54
|
+
function decodeU8aVec(registry, result, u8a, startAt, Type) {
|
|
55
|
+
const count = result.length;
|
|
56
|
+
let offset = startAt;
|
|
57
57
|
|
|
58
58
|
for (let i = 0; i < count; i++) {
|
|
59
59
|
try {
|
|
@@ -65,5 +65,5 @@ function decodeU8aVec(registry, u8a, start, Type, count) {
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
return [
|
|
68
|
+
return [offset, offset - startAt];
|
|
69
69
|
}
|
|
@@ -15,10 +15,12 @@ var _typeToConstructor = require("./typeToConstructor");
|
|
|
15
15
|
*/
|
|
16
16
|
function mapToTypeMap(registry, input) {
|
|
17
17
|
const entries = Object.entries(input);
|
|
18
|
-
const
|
|
18
|
+
const count = entries.length;
|
|
19
|
+
const output = [new Array(count), new Array(count)];
|
|
19
20
|
|
|
20
21
|
for (let i = 0; i < entries.length; i++) {
|
|
21
|
-
output[
|
|
22
|
+
output[1][i] = entries[i][0];
|
|
23
|
+
output[0][i] = (0, _typeToConstructor.typeToConstructor)(registry, entries[i][1]);
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
return output;
|
package/cjs/utils/sanitize.js
CHANGED
|
@@ -14,6 +14,7 @@ exports.removePairOf = removePairOf;
|
|
|
14
14
|
exports.removeTraits = removeTraits;
|
|
15
15
|
exports.removeWrap = removeWrap;
|
|
16
16
|
exports.sanitize = sanitize;
|
|
17
|
+
exports.trim = trim;
|
|
17
18
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
18
19
|
// SPDX-License-Identifier: Apache-2.0
|
|
19
20
|
const BOUNDED = ['BTreeMap', 'BTreeSet', 'HashMap', 'Vec'];
|
|
@@ -35,7 +36,13 @@ alias('RawAddress', 'Address'), // lookups, mapped to Address/AccountId as appro
|
|
|
35
36
|
alias('Lookup::Source', 'LookupSource'), alias('Lookup::Target', 'LookupTarget'), // HACK duplication between contracts & primitives, however contracts prefixed with exec
|
|
36
37
|
alias('exec::StorageKey', 'ContractStorageKey'), // flattens tuples with one value, `(AccountId)` -> `AccountId`
|
|
37
38
|
flattenSingleTuple(), // converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
|
|
38
|
-
removeColons()
|
|
39
|
+
removeColons(), // remove all trailing spaces - this should always be the last
|
|
40
|
+
trim()]; // given a string, trim it
|
|
41
|
+
|
|
42
|
+
function trim() {
|
|
43
|
+
return value => value.trim();
|
|
44
|
+
} // given a starting index, find the closing >
|
|
45
|
+
|
|
39
46
|
|
|
40
47
|
function findClosing(value, start) {
|
|
41
48
|
let depth = 0;
|
|
@@ -218,10 +225,11 @@ function removeWrap(check) {
|
|
|
218
225
|
const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types
|
|
219
226
|
|
|
220
227
|
function sanitize(value, options) {
|
|
221
|
-
|
|
228
|
+
const startValue = value.toString();
|
|
229
|
+
let result = startValue;
|
|
222
230
|
|
|
223
231
|
if (!options) {
|
|
224
|
-
const memoized = sanitizeMap.get(
|
|
232
|
+
const memoized = sanitizeMap.get(startValue);
|
|
225
233
|
|
|
226
234
|
if (memoized) {
|
|
227
235
|
return memoized;
|
|
@@ -232,10 +240,8 @@ function sanitize(value, options) {
|
|
|
232
240
|
result = mappings[i](result, options);
|
|
233
241
|
}
|
|
234
242
|
|
|
235
|
-
result = result.trim();
|
|
236
|
-
|
|
237
243
|
if (!options) {
|
|
238
|
-
sanitizeMap.set(
|
|
244
|
+
sanitizeMap.set(startValue, result);
|
|
239
245
|
}
|
|
240
246
|
|
|
241
247
|
return result;
|
package/cjs/utils/typesToMap.js
CHANGED
|
@@ -7,13 +7,12 @@ exports.typesToMap = typesToMap;
|
|
|
7
7
|
|
|
8
8
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
9
9
|
// SPDX-License-Identifier: Apache-2.0
|
|
10
|
-
function typesToMap(registry,
|
|
11
|
-
|
|
10
|
+
function typesToMap(registry, _ref) {
|
|
11
|
+
let [Types, keys] = _ref;
|
|
12
12
|
const result = {};
|
|
13
13
|
|
|
14
|
-
for (let i = 0; i <
|
|
15
|
-
|
|
16
|
-
result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
|
|
14
|
+
for (let i = 0; i < keys.length; i++) {
|
|
15
|
+
result[keys[i]] = registry.getClassName(Types[i]) || new Types[i](registry).toRawType();
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
return result;
|
package/extended/BTreeSet.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import {
|
|
3
|
+
import { compactFromU8aLim, compactToU8a, isHex, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from "../utils/index.js";
|
|
5
5
|
const l = logger('BTreeSet');
|
|
6
6
|
/** @internal */
|
|
7
7
|
|
|
8
8
|
function decodeSetFromU8a(registry, ValClass, u8a) {
|
|
9
9
|
const output = new Set();
|
|
10
|
-
const [offset,
|
|
11
|
-
const
|
|
10
|
+
const [offset, count] = compactFromU8aLim(u8a);
|
|
11
|
+
const result = new Array(count);
|
|
12
|
+
const [decodedLength] = decodeU8aVec(registry, result, u8a, offset, ValClass);
|
|
12
13
|
|
|
13
|
-
for (let i = 0; i <
|
|
14
|
-
output.add(
|
|
14
|
+
for (let i = 0; i < count; i++) {
|
|
15
|
+
output.add(result[i]);
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
return [ValClass, output, decodedLength];
|
|
@@ -212,7 +213,7 @@ export class BTreeSet extends Set {
|
|
|
212
213
|
encoded.push(v.toU8a(isBare));
|
|
213
214
|
}
|
|
214
215
|
|
|
215
|
-
return
|
|
216
|
+
return u8aConcatStrict(encoded);
|
|
216
217
|
}
|
|
217
218
|
|
|
218
219
|
}
|
package/extended/BitVec.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,
|
|
3
|
+
import { assert, compactFromU8aLim, compactToU8a, isString, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { Raw } from "../native/Raw.js";
|
|
5
5
|
/** @internal */
|
|
6
6
|
|
|
@@ -10,10 +10,10 @@ function decodeBitVecU8a(value) {
|
|
|
10
10
|
} // handle all other Uint8Array inputs, these do have a length prefix which is the number of bits encoded
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
const [offset, length] =
|
|
14
|
-
const total = offset + Math.ceil(length
|
|
13
|
+
const [offset, length] = compactFromU8aLim(value);
|
|
14
|
+
const total = offset + Math.ceil(length / 8);
|
|
15
15
|
assert(total <= value.length, () => `BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
|
16
|
-
return [length
|
|
16
|
+
return [length, value.subarray(offset, total)];
|
|
17
17
|
}
|
|
18
18
|
/** @internal */
|
|
19
19
|
|
|
@@ -84,7 +84,7 @@ export class BitVec extends Raw {
|
|
|
84
84
|
|
|
85
85
|
toU8a(isBare) {
|
|
86
86
|
const bitVec = super.toU8a();
|
|
87
|
-
return isBare ? bitVec :
|
|
87
|
+
return isBare ? bitVec : u8aConcatStrict([compactToU8a(this.#decodedLength), bitVec]);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
}
|
package/extended/Bytes.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare class Bytes extends Raw {
|
|
|
16
16
|
/**
|
|
17
17
|
* @description Returns a breakdown of the hex encoding for this Codec
|
|
18
18
|
*/
|
|
19
|
-
inspect(): Inspect;
|
|
19
|
+
inspect(isBare?: boolean): Inspect;
|
|
20
20
|
/**
|
|
21
21
|
* @description Returns the base runtime type name for this instance
|
|
22
22
|
*/
|
package/extended/Bytes.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,
|
|
3
|
+
import { assert, compactAddLength, compactFromU8aLim, compactToU8a, isCodec, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { Raw } from "../native/Raw.js"; // Bytes are used for things like on-chain code, so it has a healthy limit
|
|
5
5
|
|
|
6
6
|
const MAX_LENGTH = 10 * 1024 * 1024;
|
|
@@ -12,9 +12,9 @@ function decodeBytesU8a(value) {
|
|
|
12
12
|
} // handle all other Uint8Array inputs, these do have a length prefix
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
const [offset, length] =
|
|
16
|
-
const total = offset + length
|
|
17
|
-
assert(length
|
|
15
|
+
const [offset, length] = compactFromU8aLim(value);
|
|
16
|
+
const total = offset + length;
|
|
17
|
+
assert(length <= MAX_LENGTH, () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
|
18
18
|
assert(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
|
19
19
|
return [value.subarray(offset, total), total];
|
|
20
20
|
}
|
|
@@ -45,10 +45,10 @@ export class Bytes extends Raw {
|
|
|
45
45
|
*/
|
|
46
46
|
|
|
47
47
|
|
|
48
|
-
inspect() {
|
|
48
|
+
inspect(isBare) {
|
|
49
49
|
const clength = compactToU8a(this.length);
|
|
50
50
|
return {
|
|
51
|
-
outer: this.length ? [clength, super.toU8a()] : [clength]
|
|
51
|
+
outer: isBare ? [super.toU8a()] : this.length ? [clength, super.toU8a()] : [clength]
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
54
|
/**
|
package/extended/Map.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 {
|
|
3
|
+
import { compactFromU8aLim, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { AbstractArray } from "../abstract/AbstractArray.js";
|
|
5
5
|
import { Enum } from "../base/Enum.js";
|
|
6
6
|
import { Struct } from "../native/Struct.js";
|
|
@@ -10,15 +10,14 @@ const l = logger('Map');
|
|
|
10
10
|
|
|
11
11
|
function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
|
|
12
12
|
const output = new Map();
|
|
13
|
-
const [offset,
|
|
14
|
-
const count = length.toNumber();
|
|
13
|
+
const [offset, count] = compactFromU8aLim(u8a);
|
|
15
14
|
const types = [];
|
|
16
15
|
|
|
17
16
|
for (let i = 0; i < count; i++) {
|
|
18
17
|
types.push(KeyClass, ValClass);
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
const [values, decodedLength] = decodeU8a(registry, u8a.subarray(offset), types);
|
|
20
|
+
const [values, decodedLength] = decodeU8a(registry, new Array(types.length), u8a.subarray(offset), [types, []]);
|
|
22
21
|
|
|
23
22
|
for (let i = 0; i < values.length; i += 2) {
|
|
24
23
|
output.set(values[i], values[i + 1]);
|
|
@@ -218,7 +217,7 @@ export class CodecMap extends Map {
|
|
|
218
217
|
encoded.push(k.toU8a(isBare), v.toU8a(isBare));
|
|
219
218
|
}
|
|
220
219
|
|
|
221
|
-
return
|
|
220
|
+
return u8aConcatStrict(encoded);
|
|
222
221
|
}
|
|
223
222
|
|
|
224
223
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { HexString } from '@polkadot/util/types';
|
|
2
|
+
import type { AnyBool, Inspect, Registry } from '../types';
|
|
3
|
+
import { Option } from '../base/Option';
|
|
4
|
+
import { bool as Bool } from '../native/Bool';
|
|
5
|
+
/**
|
|
6
|
+
* @name OptionBool
|
|
7
|
+
* @description A specific implementation of Option<bool> than allows for single-byte encoding
|
|
8
|
+
*/
|
|
9
|
+
export declare class OptionBool extends Option<Bool> {
|
|
10
|
+
constructor(registry: Registry, value?: Option<Bool> | AnyBool | Uint8Array | HexString | null);
|
|
11
|
+
/**
|
|
12
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
13
|
+
*/
|
|
14
|
+
get encodedLength(): number;
|
|
15
|
+
/**
|
|
16
|
+
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
|
17
|
+
*/
|
|
18
|
+
get initialU8aLength(): number;
|
|
19
|
+
/**
|
|
20
|
+
* @description Checks if the value is an empty value (always false)
|
|
21
|
+
*/
|
|
22
|
+
get isFalse(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* @description Checks if the value is an empty value (always false)
|
|
25
|
+
*/
|
|
26
|
+
get isTrue(): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
29
|
+
*/
|
|
30
|
+
inspect(): Inspect;
|
|
31
|
+
/**
|
|
32
|
+
* @description Returns the base runtime type name for this instance
|
|
33
|
+
*/
|
|
34
|
+
toRawType(isBare?: boolean): string;
|
|
35
|
+
/**
|
|
36
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
37
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
38
|
+
*/
|
|
39
|
+
toU8a(isBare?: boolean): Uint8Array;
|
|
40
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { isHex, isU8a, u8aToU8a } from '@polkadot/util';
|
|
4
|
+
import { Option } from "../base/Option.js";
|
|
5
|
+
import { bool as Bool } from "../native/Bool.js";
|
|
6
|
+
|
|
7
|
+
function decodeU8a(registry, value) {
|
|
8
|
+
// Encoded as -
|
|
9
|
+
// - 0 = None
|
|
10
|
+
// - 1 = True
|
|
11
|
+
// - 2 = False
|
|
12
|
+
return value[0] === 0 ? null : new Bool(registry, value[0] === 1);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @name OptionBool
|
|
16
|
+
* @description A specific implementation of Option<bool> than allows for single-byte encoding
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export class OptionBool extends Option {
|
|
21
|
+
constructor(registry, value) {
|
|
22
|
+
super(registry, Bool, isU8a(value) || isHex(value) ? decodeU8a(registry, u8aToU8a(value)) : value);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
get encodedLength() {
|
|
30
|
+
return 1;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
get initialU8aLength() {
|
|
38
|
+
return 1;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* @description Checks if the value is an empty value (always false)
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
get isFalse() {
|
|
46
|
+
return this.isSome ? !this.value.valueOf() : false;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @description Checks if the value is an empty value (always false)
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
get isTrue() {
|
|
54
|
+
return this.isSome ? this.value.valueOf() : false;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
inspect() {
|
|
62
|
+
return {
|
|
63
|
+
outer: [this.toU8a()]
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* @description Returns the base runtime type name for this instance
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
toRawType(isBare) {
|
|
72
|
+
return isBare ? 'bool' : 'Option<bool>';
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
76
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
77
|
+
*/
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
toU8a(isBare) {
|
|
81
|
+
if (isBare) {
|
|
82
|
+
return super.toU8a(true);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return this.isSome ? new Uint8Array([this.isTrue ? 1 : 2]) : new Uint8Array([0]);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
}
|
package/extended/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export { Bytes } from './Bytes';
|
|
|
5
5
|
export { HashMap } from './HashMap';
|
|
6
6
|
export { Linkage } from './Linkage';
|
|
7
7
|
export { CodecMap, CodecMap as Map } from './Map';
|
|
8
|
+
export { OptionBool } from './OptionBool';
|
|
8
9
|
export { Range } from './Range';
|
|
9
10
|
export { RangeInclusive } from './RangeInclusive';
|
|
10
11
|
export { Type } from './Type';
|
package/extended/index.js
CHANGED
|
@@ -7,6 +7,7 @@ export { Bytes } from "./Bytes.js";
|
|
|
7
7
|
export { HashMap } from "./HashMap.js";
|
|
8
8
|
export { Linkage } from "./Linkage.js";
|
|
9
9
|
export { CodecMap, CodecMap as Map } from "./Map.js";
|
|
10
|
+
export { OptionBool } from "./OptionBool.js";
|
|
10
11
|
export { Range } from "./Range.js";
|
|
11
12
|
export { RangeInclusive } from "./RangeInclusive.js";
|
|
12
13
|
export { Type } from "./Type.js";
|
package/native/Struct.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { HexString } from '@polkadot/util/types';
|
|
2
2
|
import type { AnyJson, BareOpts, Codec, CodecClass, Inspect, IStruct, IU8a, Registry } from '../types';
|
|
3
3
|
declare type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
|
|
4
|
+
declare type Definition = [CodecClass[], string[]];
|
|
5
|
+
interface Options {
|
|
6
|
+
definition?: Definition;
|
|
7
|
+
setDefinition?: (d: Definition) => Definition;
|
|
8
|
+
}
|
|
4
9
|
/**
|
|
5
10
|
* @name Struct
|
|
6
11
|
* @description
|
|
@@ -23,7 +28,7 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
|
|
|
23
28
|
#private;
|
|
24
29
|
createdAtHash?: IU8a;
|
|
25
30
|
readonly initialU8aLength?: number;
|
|
26
|
-
constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string
|
|
31
|
+
constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>, { definition, setDefinition }?: Options);
|
|
27
32
|
static with<S extends TypesDef>(Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>>;
|
|
28
33
|
/**
|
|
29
34
|
* @description The available keys for this struct
|
|
@@ -63,7 +68,7 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
|
|
|
63
68
|
/**
|
|
64
69
|
* @description Returns a breakdown of the hex encoding for this Codec
|
|
65
70
|
*/
|
|
66
|
-
inspect(): Inspect;
|
|
71
|
+
inspect(isBare?: BareOpts): Inspect;
|
|
67
72
|
/**
|
|
68
73
|
* @description Converts the Object to an standard JavaScript Array
|
|
69
74
|
*/
|