@polkadot/types-codec 8.8.3-1 → 8.8.3-10
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 → Array.d.ts} +0 -0
- package/abstract/{AbstractArray.js → Array.js} +0 -0
- package/{base → abstract}/Base.d.ts +2 -2
- package/{base → abstract}/Base.js +2 -7
- package/abstract/{AbstractInt.d.ts → Int.d.ts} +1 -4
- package/abstract/{AbstractInt.js → Int.js} +7 -14
- package/abstract/index.d.ts +3 -2
- package/abstract/index.js +3 -2
- package/base/Compact.d.ts +0 -2
- package/base/Compact.js +15 -16
- package/base/Enum.js +6 -6
- package/base/Int.d.ts +1 -1
- package/base/Int.js +1 -1
- package/base/Null.d.ts +2 -8
- package/base/Null.js +2 -16
- package/base/Option.d.ts +1 -4
- package/base/Option.js +1 -10
- package/base/Tuple.d.ts +1 -1
- package/base/Tuple.js +3 -4
- package/base/UInt.d.ts +1 -1
- package/base/UInt.js +1 -1
- package/base/Vec.d.ts +1 -1
- package/base/Vec.js +2 -3
- package/base/VecAny.d.ts +1 -1
- package/base/VecAny.js +1 -1
- package/base/VecFixed.d.ts +1 -1
- package/base/VecFixed.js +2 -3
- package/base/index.d.ts +0 -1
- package/base/index.js +0 -1
- package/cjs/abstract/{AbstractArray.js → Array.js} +0 -0
- package/cjs/{base → abstract}/Base.js +4 -9
- package/cjs/abstract/{AbstractInt.js → Int.js} +6 -13
- package/cjs/abstract/index.js +12 -4
- package/cjs/base/Compact.js +15 -16
- package/cjs/base/Enum.js +5 -5
- package/cjs/base/Int.js +2 -2
- package/cjs/base/Null.js +2 -16
- package/cjs/base/Option.js +1 -10
- package/cjs/base/Tuple.js +4 -5
- package/cjs/base/UInt.js +2 -2
- package/cjs/base/Vec.js +3 -4
- package/cjs/base/VecAny.js +2 -2
- package/cjs/base/VecFixed.js +3 -4
- package/cjs/base/index.js +0 -8
- package/cjs/extended/Bytes.js +1 -1
- package/cjs/extended/Map.js +2 -2
- package/cjs/extended/OptionBool.js +1 -8
- package/cjs/native/Struct.js +2 -7
- package/cjs/native/Text.js +1 -10
- package/cjs/packageInfo.js +1 -1
- package/cjs/utils/decodeU8a.js +63 -20
- package/cjs/utils/index.js +6 -0
- package/cjs/utils/mapToTypeMap.js +1 -1
- package/cjs/utils/sanitize.js +17 -37
- package/extended/Bytes.js +2 -2
- package/extended/Map.js +1 -1
- package/extended/OptionBool.d.ts +0 -4
- package/extended/OptionBool.js +1 -8
- package/native/Raw.d.ts +1 -1
- package/native/Struct.d.ts +1 -1
- package/native/Struct.js +3 -8
- package/native/Text.d.ts +2 -5
- package/native/Text.js +1 -10
- package/package.json +15 -15
- package/packageInfo.js +1 -1
- package/utils/decodeU8a.d.ts +16 -1
- package/utils/decodeU8a.js +57 -19
- package/utils/index.d.ts +1 -1
- package/utils/index.js +1 -1
- package/utils/mapToTypeMap.js +1 -1
- package/utils/sanitize.d.ts +3 -5
- package/utils/sanitize.js +17 -37
package/cjs/utils/sanitize.js
CHANGED
|
@@ -140,10 +140,7 @@ function removeExtensions(type, isSized) {
|
|
|
140
140
|
}
|
|
141
141
|
|
|
142
142
|
function removeColons() {
|
|
143
|
-
return
|
|
144
|
-
let {
|
|
145
|
-
allowNamespaces
|
|
146
|
-
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
143
|
+
return value => {
|
|
147
144
|
let index = 0;
|
|
148
145
|
|
|
149
146
|
while (index !== -1) {
|
|
@@ -152,10 +149,6 @@ function removeColons() {
|
|
|
152
149
|
if (index === 0) {
|
|
153
150
|
value = value.substring(2);
|
|
154
151
|
} else if (index !== -1) {
|
|
155
|
-
if (allowNamespaces) {
|
|
156
|
-
return value;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
152
|
let start = index;
|
|
160
153
|
|
|
161
154
|
while (start !== -1 && !BOX_PRECEDING.includes(value[start])) {
|
|
@@ -177,7 +170,7 @@ function removeGenerics() {
|
|
|
177
170
|
// check against the allowed wrappers, be it Vec<..>, Option<...> ...
|
|
178
171
|
const box = ALLOWED_BOXES.find(box => {
|
|
179
172
|
const start = index - box.length;
|
|
180
|
-
return start >= 0 && value.substring(start,
|
|
173
|
+
return start >= 0 && value.substring(start, index) === box && ( // make sure it is stand-alone, i.e. don't catch ElectionResult<...> as Result<...>
|
|
181
174
|
start === 0 || BOX_PRECEDING.includes(value[start - 1]));
|
|
182
175
|
}); // we have not found anything, unwrap generic innards
|
|
183
176
|
|
|
@@ -190,17 +183,13 @@ function removeGenerics() {
|
|
|
190
183
|
|
|
191
184
|
return value;
|
|
192
185
|
};
|
|
193
|
-
}
|
|
194
|
-
/** @internal */
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
function pairOfReplacer(v) {
|
|
198
|
-
return `(${v},${v})`;
|
|
199
186
|
} // remove the PairOf wrappers
|
|
200
187
|
|
|
201
188
|
|
|
202
189
|
function removePairOf() {
|
|
203
|
-
|
|
190
|
+
const replacer = v => `(${v},${v})`;
|
|
191
|
+
|
|
192
|
+
return value => replaceTagWith(value, 'PairOf<', replacer);
|
|
204
193
|
} // remove the type traits
|
|
205
194
|
|
|
206
195
|
|
|
@@ -226,40 +215,31 @@ function removeTraits() {
|
|
|
226
215
|
.replace(from5, 'Lookup') // replace `<...>::Type`
|
|
227
216
|
.replace(from6, '');
|
|
228
217
|
};
|
|
229
|
-
}
|
|
230
|
-
/** @internal */
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
function wrapReplacer(v) {
|
|
234
|
-
return v;
|
|
235
218
|
} // remove wrapping values, i.e. Box<Proposal> -> Proposal
|
|
236
219
|
|
|
237
220
|
|
|
238
221
|
function removeWrap(check) {
|
|
239
|
-
|
|
222
|
+
const replacer = v => v;
|
|
223
|
+
|
|
224
|
+
return value => replaceTagWith(value, check, replacer);
|
|
240
225
|
}
|
|
241
226
|
|
|
242
|
-
const sanitizeMap = new Map();
|
|
227
|
+
const sanitizeMap = new Map();
|
|
243
228
|
|
|
244
|
-
function sanitize(value
|
|
229
|
+
function sanitize(value) {
|
|
245
230
|
const startValue = value.toString();
|
|
246
|
-
|
|
231
|
+
const memoized = sanitizeMap.get(startValue);
|
|
247
232
|
|
|
248
|
-
if (
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if (memoized) {
|
|
252
|
-
return memoized;
|
|
253
|
-
}
|
|
233
|
+
if (memoized) {
|
|
234
|
+
return memoized;
|
|
254
235
|
}
|
|
255
236
|
|
|
256
|
-
|
|
257
|
-
result = mappings[i](result, options);
|
|
258
|
-
}
|
|
237
|
+
let result = startValue;
|
|
259
238
|
|
|
260
|
-
|
|
261
|
-
|
|
239
|
+
for (let i = 0; i < mappings.length; i++) {
|
|
240
|
+
result = mappings[i](result);
|
|
262
241
|
}
|
|
263
242
|
|
|
243
|
+
sanitizeMap.set(startValue, result);
|
|
264
244
|
return result;
|
|
265
245
|
}
|
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, compactFromU8aLim, compactToU8a,
|
|
3
|
+
import { assert, compactAddLength, compactFromU8aLim, compactToU8a, 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;
|
|
@@ -29,7 +29,7 @@ function decodeBytesU8a(value) {
|
|
|
29
29
|
|
|
30
30
|
export class Bytes extends Raw {
|
|
31
31
|
constructor(registry, value) {
|
|
32
|
-
const [u8a, decodedLength] = isU8a(value) && !(
|
|
32
|
+
const [u8a, decodedLength] = isU8a(value) && !(value instanceof Raw) ? decodeBytesU8a(value) : Array.isArray(value) || isString(value) ? [u8aToU8a(value), 0] : [value, 0];
|
|
33
33
|
super(registry, u8a, decodedLength);
|
|
34
34
|
}
|
|
35
35
|
/**
|
package/extended/Map.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
3
|
import { compactFromU8aLim, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
|
-
import { AbstractArray } from "../abstract/
|
|
4
|
+
import { AbstractArray } from "../abstract/Array.js";
|
|
5
5
|
import { Enum } from "../base/Enum.js";
|
|
6
6
|
import { Struct } from "../native/Struct.js";
|
|
7
7
|
import { compareMap, decodeU8a, sortMap, typeToConstructor } from "../utils/index.js";
|
package/extended/OptionBool.d.ts
CHANGED
|
@@ -12,10 +12,6 @@ export declare class OptionBool extends Option<Bool> {
|
|
|
12
12
|
* @description The length of the value when encoded as a Uint8Array
|
|
13
13
|
*/
|
|
14
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
15
|
/**
|
|
20
16
|
* @description Checks if the value is an empty value (always false)
|
|
21
17
|
*/
|
package/extended/OptionBool.js
CHANGED
|
@@ -20,6 +20,7 @@ function decodeU8a(registry, value) {
|
|
|
20
20
|
export class OptionBool extends Option {
|
|
21
21
|
constructor(registry, value) {
|
|
22
22
|
super(registry, Bool, isU8a(value) || isHex(value) ? decodeU8a(registry, u8aToU8a(value)) : value);
|
|
23
|
+
this.initialU8aLength = 1;
|
|
23
24
|
}
|
|
24
25
|
/**
|
|
25
26
|
* @description The length of the value when encoded as a Uint8Array
|
|
@@ -29,14 +30,6 @@ export class OptionBool extends Option {
|
|
|
29
30
|
get encodedLength() {
|
|
30
31
|
return 1;
|
|
31
32
|
}
|
|
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
33
|
/**
|
|
41
34
|
* @description Checks if the value is an empty value (always false)
|
|
42
35
|
*/
|
package/native/Raw.d.ts
CHANGED
|
@@ -10,9 +10,9 @@ 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
|
-
readonly registry: Registry;
|
|
14
13
|
createdAtHash?: IU8a;
|
|
15
14
|
readonly initialU8aLength?: number;
|
|
15
|
+
readonly registry: Registry;
|
|
16
16
|
constructor(registry: Registry, value?: AnyU8a, initialU8aLength?: number);
|
|
17
17
|
/**
|
|
18
18
|
* @description The length of the value when encoded as a Uint8Array
|
package/native/Struct.d.ts
CHANGED
|
@@ -28,6 +28,7 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
|
|
|
28
28
|
#private;
|
|
29
29
|
createdAtHash?: IU8a;
|
|
30
30
|
readonly initialU8aLength?: number;
|
|
31
|
+
readonly registry: Registry;
|
|
31
32
|
constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>, { definition, setDefinition }?: Options);
|
|
32
33
|
static with<S extends TypesDef>(Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>>;
|
|
33
34
|
/**
|
|
@@ -51,7 +52,6 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
|
|
|
51
52
|
* @description returns a hash of the contents
|
|
52
53
|
*/
|
|
53
54
|
get hash(): IU8a;
|
|
54
|
-
get registry(): Registry;
|
|
55
55
|
/**
|
|
56
56
|
* @description Compares the value of the input to see if there is a match
|
|
57
57
|
*/
|
package/native/Struct.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
3
|
import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
|
-
import { compareMap,
|
|
4
|
+
import { compareMap, decodeU8aStruct, mapToTypeMap, typesToMap } from "../utils/index.js";
|
|
5
5
|
|
|
6
6
|
function noopSetDefinition(d) {
|
|
7
7
|
return d;
|
|
@@ -73,7 +73,6 @@ function decodeStructFromObject(registry, [Types, keys], value, jsonMap) {
|
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
export class Struct extends Map {
|
|
76
|
-
#registry;
|
|
77
76
|
#jsonMap;
|
|
78
77
|
#Types;
|
|
79
78
|
|
|
@@ -82,10 +81,10 @@ export class Struct extends Map {
|
|
|
82
81
|
setDefinition = noopSetDefinition
|
|
83
82
|
} = {}) {
|
|
84
83
|
const typeMap = definition || setDefinition(mapToTypeMap(registry, Types));
|
|
85
|
-
const [decoded, decodedLength] = isU8a(value) || isHex(value) ?
|
|
84
|
+
const [decoded, decodedLength] = isU8a(value) || isHex(value) ? decodeU8aStruct(registry, new Array(typeMap[0].length), u8aToU8a(value), typeMap) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
|
|
86
85
|
super(decoded);
|
|
87
|
-
this.#registry = registry;
|
|
88
86
|
this.initialU8aLength = decodedLength;
|
|
87
|
+
this.registry = registry;
|
|
89
88
|
this.#jsonMap = jsonMap;
|
|
90
89
|
this.#Types = typeMap;
|
|
91
90
|
}
|
|
@@ -170,10 +169,6 @@ export class Struct extends Map {
|
|
|
170
169
|
get hash() {
|
|
171
170
|
return this.registry.hash(this.toU8a());
|
|
172
171
|
}
|
|
173
|
-
|
|
174
|
-
get registry() {
|
|
175
|
-
return this.#registry;
|
|
176
|
-
}
|
|
177
172
|
/**
|
|
178
173
|
* @description Compares the value of the input to see if there is a match
|
|
179
174
|
*/
|
package/native/Text.d.ts
CHANGED
|
@@ -10,8 +10,9 @@ 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
|
-
readonly registry: Registry;
|
|
14
13
|
createdAtHash?: IU8a;
|
|
14
|
+
readonly initialU8aLength?: number;
|
|
15
|
+
readonly registry: Registry;
|
|
15
16
|
constructor(registry: Registry, value?: null | AnyString | AnyU8a | {
|
|
16
17
|
toString: () => string;
|
|
17
18
|
});
|
|
@@ -19,10 +20,6 @@ export declare class Text extends String implements IText {
|
|
|
19
20
|
* @description The length of the value when encoded as a Uint8Array
|
|
20
21
|
*/
|
|
21
22
|
get encodedLength(): number;
|
|
22
|
-
/**
|
|
23
|
-
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
|
24
|
-
*/
|
|
25
|
-
get initialU8aLength(): number | undefined;
|
|
26
23
|
/**
|
|
27
24
|
* @description returns a hash of the contents
|
|
28
25
|
*/
|
package/native/Text.js
CHANGED
|
@@ -39,14 +39,13 @@ function decodeText(value) {
|
|
|
39
39
|
|
|
40
40
|
|
|
41
41
|
export class Text extends String {
|
|
42
|
-
#initialU8aLength;
|
|
43
42
|
#override = null;
|
|
44
43
|
|
|
45
44
|
constructor(registry, value) {
|
|
46
45
|
const [str, decodedLength] = decodeText(value);
|
|
47
46
|
super(str);
|
|
48
47
|
this.registry = registry;
|
|
49
|
-
this
|
|
48
|
+
this.initialU8aLength = decodedLength;
|
|
50
49
|
}
|
|
51
50
|
/**
|
|
52
51
|
* @description The length of the value when encoded as a Uint8Array
|
|
@@ -56,14 +55,6 @@ export class Text extends String {
|
|
|
56
55
|
get encodedLength() {
|
|
57
56
|
return this.toU8a().length;
|
|
58
57
|
}
|
|
59
|
-
/**
|
|
60
|
-
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
get initialU8aLength() {
|
|
65
|
-
return this.#initialU8aLength;
|
|
66
|
-
}
|
|
67
58
|
/**
|
|
68
59
|
* @description returns a hash of the contents
|
|
69
60
|
*/
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"./cjs/detectPackage.js"
|
|
21
21
|
],
|
|
22
22
|
"type": "module",
|
|
23
|
-
"version": "8.8.3-
|
|
23
|
+
"version": "8.8.3-10",
|
|
24
24
|
"main": "./cjs/index.js",
|
|
25
25
|
"module": "./index.js",
|
|
26
26
|
"types": "./index.d.ts",
|
|
@@ -37,26 +37,26 @@
|
|
|
37
37
|
"require": "./cjs/abstract/index.js",
|
|
38
38
|
"default": "./abstract/index.js"
|
|
39
39
|
},
|
|
40
|
-
"./abstract/
|
|
41
|
-
"types": "./abstract/
|
|
42
|
-
"require": "./cjs/abstract/
|
|
43
|
-
"default": "./abstract/
|
|
40
|
+
"./abstract/Array": {
|
|
41
|
+
"types": "./abstract/Array.d.ts",
|
|
42
|
+
"require": "./cjs/abstract/Array.js",
|
|
43
|
+
"default": "./abstract/Array.js"
|
|
44
44
|
},
|
|
45
|
-
"./abstract/
|
|
46
|
-
"types": "./abstract/
|
|
47
|
-
"require": "./cjs/abstract/
|
|
48
|
-
"default": "./abstract/
|
|
45
|
+
"./abstract/Base": {
|
|
46
|
+
"types": "./abstract/Base.d.ts",
|
|
47
|
+
"require": "./cjs/abstract/Base.js",
|
|
48
|
+
"default": "./abstract/Base.js"
|
|
49
|
+
},
|
|
50
|
+
"./abstract/Int": {
|
|
51
|
+
"types": "./abstract/Int.d.ts",
|
|
52
|
+
"require": "./cjs/abstract/Int.js",
|
|
53
|
+
"default": "./abstract/Int.js"
|
|
49
54
|
},
|
|
50
55
|
"./base": {
|
|
51
56
|
"types": "./base/index.d.ts",
|
|
52
57
|
"require": "./cjs/base/index.js",
|
|
53
58
|
"default": "./base/index.js"
|
|
54
59
|
},
|
|
55
|
-
"./base/Base": {
|
|
56
|
-
"types": "./base/Base.d.ts",
|
|
57
|
-
"require": "./cjs/base/Base.js",
|
|
58
|
-
"default": "./base/Base.js"
|
|
59
|
-
},
|
|
60
60
|
"./base/Compact": {
|
|
61
61
|
"types": "./base/Compact.d.ts",
|
|
62
62
|
"require": "./cjs/base/Compact.js",
|
|
@@ -415,6 +415,6 @@
|
|
|
415
415
|
},
|
|
416
416
|
"dependencies": {
|
|
417
417
|
"@babel/runtime": "^7.18.3",
|
|
418
|
-
"@polkadot/util": "^9.4.
|
|
418
|
+
"@polkadot/util": "^9.4.2-22"
|
|
419
419
|
}
|
|
420
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.8.3-
|
|
8
|
+
version: '8.8.3-10'
|
|
9
9
|
};
|
package/utils/decodeU8a.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Codec, CodecClass, Registry } from '../types';
|
|
2
2
|
/**
|
|
3
|
+
* @internal
|
|
4
|
+
*
|
|
3
5
|
* Given an u8a, and an array of Type constructors, decode the u8a against the
|
|
4
6
|
* types, and return an array of decoded values.
|
|
5
7
|
*
|
|
@@ -7,5 +9,18 @@ import type { Codec, CodecClass, Registry } from '../types';
|
|
|
7
9
|
* @param result - The result array (will be returned with values pushed)
|
|
8
10
|
* @param types - The array of CodecClass to decode the U8a against.
|
|
9
11
|
*/
|
|
10
|
-
export declare function decodeU8a<T extends Codec = Codec
|
|
12
|
+
export declare function decodeU8a<T extends Codec = Codec>(registry: Registry, result: T[], u8a: Uint8Array, [Types, keys]: [CodecClass<T>[], string[]]): [T[], number];
|
|
13
|
+
/**
|
|
14
|
+
* @internal
|
|
15
|
+
*
|
|
16
|
+
* Split from decodeU8a since this is specialized to zip returns ... while we duplicate, this
|
|
17
|
+
* is all on the hot-path, so it is not great, however there is (some) method behind the madness
|
|
18
|
+
*/
|
|
19
|
+
export declare function decodeU8aStruct(registry: Registry, result: [string, Codec][], u8a: Uint8Array, [Types, keys]: [CodecClass[], string[]]): [[string, Codec][], number];
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*
|
|
23
|
+
* Split from decodeU8a since this is specialized to 1 instance ... while we duplicate, this
|
|
24
|
+
* is all on the hot-path, so it is not great, however there is (some) method behind the madness
|
|
25
|
+
*/
|
|
11
26
|
export declare function decodeU8aVec<T extends Codec = Codec>(registry: Registry, result: unknown[], u8a: Uint8Array, startAt: number, Type: CodecClass<T>): [number, number];
|
package/utils/decodeU8a.js
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { u8aToHex } from '@polkadot/util';
|
|
4
|
+
/** @internal */
|
|
4
5
|
|
|
5
|
-
function formatFailure(
|
|
6
|
-
|
|
7
|
-
}
|
|
6
|
+
function formatFailure(registry, {
|
|
7
|
+
message
|
|
8
|
+
}, u8a, i, count, Type, key) {
|
|
9
|
+
let type = '';
|
|
8
10
|
|
|
9
|
-
function getRawType(registry, Type) {
|
|
10
11
|
try {
|
|
11
|
-
|
|
12
|
-
} catch {
|
|
13
|
-
return null;
|
|
12
|
+
type = `: ${new Type(registry).toRawType()}`;
|
|
13
|
+
} catch {// ignore
|
|
14
14
|
}
|
|
15
|
+
|
|
16
|
+
return `decodeU8a: failed at ${u8aToHex(u8a.subarray(0, 16))}…${key ? ` on ${key}` : ''} (index ${i}/${count})${type}:: ${message}`;
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
19
|
+
* @internal
|
|
20
|
+
*
|
|
17
21
|
* Given an u8a, and an array of Type constructors, decode the u8a against the
|
|
18
22
|
* types, and return an array of decoded values.
|
|
19
23
|
*
|
|
@@ -23,36 +27,70 @@ function getRawType(registry, Type) {
|
|
|
23
27
|
*/
|
|
24
28
|
|
|
25
29
|
|
|
26
|
-
export function decodeU8a(registry, result, u8a, [Types, keys]
|
|
30
|
+
export function decodeU8a(registry, result, u8a, [Types, keys]) {
|
|
27
31
|
const count = result.length;
|
|
28
32
|
let offset = 0;
|
|
33
|
+
let i = 0;
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
|
|
35
|
+
try {
|
|
36
|
+
while (i < count) {
|
|
32
37
|
const value = new Types[i](registry, u8a.subarray(offset));
|
|
33
38
|
offset += value.initialU8aLength || value.encodedLength;
|
|
34
|
-
result[i] =
|
|
35
|
-
|
|
36
|
-
throw new Error(formatFailure(error, getRawType(registry, Types[i]), u8a.subarray(offset, offset + 16), keys[i]));
|
|
39
|
+
result[i] = value;
|
|
40
|
+
i++;
|
|
37
41
|
}
|
|
42
|
+
} catch (error) {
|
|
43
|
+
throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
return [result, offset];
|
|
41
|
-
}
|
|
42
|
-
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*
|
|
51
|
+
* Split from decodeU8a since this is specialized to zip returns ... while we duplicate, this
|
|
52
|
+
* is all on the hot-path, so it is not great, however there is (some) method behind the madness
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
export function decodeU8aStruct(registry, result, u8a, [Types, keys]) {
|
|
56
|
+
const count = result.length;
|
|
57
|
+
let offset = 0;
|
|
58
|
+
let i = 0;
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
while (i < count) {
|
|
62
|
+
const value = new Types[i](registry, u8a.subarray(offset));
|
|
63
|
+
offset += value.initialU8aLength || value.encodedLength;
|
|
64
|
+
result[i] = [keys[i], value];
|
|
65
|
+
i++;
|
|
66
|
+
}
|
|
67
|
+
} catch (error) {
|
|
68
|
+
throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return [result, offset];
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @internal
|
|
75
|
+
*
|
|
76
|
+
* Split from decodeU8a since this is specialized to 1 instance ... while we duplicate, this
|
|
77
|
+
* is all on the hot-path, so it is not great, however there is (some) method behind the madness
|
|
78
|
+
*/
|
|
43
79
|
|
|
44
80
|
export function decodeU8aVec(registry, result, u8a, startAt, Type) {
|
|
45
81
|
const count = result.length;
|
|
46
82
|
let offset = startAt;
|
|
83
|
+
let i = 0;
|
|
47
84
|
|
|
48
|
-
|
|
49
|
-
|
|
85
|
+
try {
|
|
86
|
+
while (i < count) {
|
|
50
87
|
const value = new Type(registry, u8a.subarray(offset));
|
|
51
88
|
offset += value.initialU8aLength || value.encodedLength;
|
|
52
89
|
result[i] = value;
|
|
53
|
-
|
|
54
|
-
throw new Error(formatFailure(error, getRawType(registry, Type), u8a.subarray(offset, offset + 16)));
|
|
90
|
+
i++;
|
|
55
91
|
}
|
|
92
|
+
} catch (error) {
|
|
93
|
+
throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Type));
|
|
56
94
|
}
|
|
57
95
|
|
|
58
96
|
return [offset, offset - startAt];
|
package/utils/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { compareArray } from './compareArray';
|
|
2
2
|
export { compareMap } from './compareMap';
|
|
3
3
|
export { compareSet } from './compareSet';
|
|
4
|
-
export { decodeU8a, decodeU8aVec } from './decodeU8a';
|
|
4
|
+
export { decodeU8a, decodeU8aStruct, decodeU8aVec } from './decodeU8a';
|
|
5
5
|
export { mapToTypeMap } from './mapToTypeMap';
|
|
6
6
|
export { typeToConstructor } from './typeToConstructor';
|
|
7
7
|
export { sanitize } from './sanitize';
|
package/utils/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
export { compareArray } from "./compareArray.js";
|
|
4
4
|
export { compareMap } from "./compareMap.js";
|
|
5
5
|
export { compareSet } from "./compareSet.js";
|
|
6
|
-
export { decodeU8a, decodeU8aVec } from "./decodeU8a.js";
|
|
6
|
+
export { decodeU8a, decodeU8aStruct, decodeU8aVec } from "./decodeU8a.js";
|
|
7
7
|
export { mapToTypeMap } from "./mapToTypeMap.js";
|
|
8
8
|
export { typeToConstructor } from "./typeToConstructor.js";
|
|
9
9
|
export { sanitize } from "./sanitize.js";
|
package/utils/mapToTypeMap.js
CHANGED
|
@@ -10,7 +10,7 @@ export function mapToTypeMap(registry, input) {
|
|
|
10
10
|
const count = entries.length;
|
|
11
11
|
const output = [new Array(count), new Array(count)];
|
|
12
12
|
|
|
13
|
-
for (let i = 0; i <
|
|
13
|
+
for (let i = 0; i < count; i++) {
|
|
14
14
|
output[1][i] = entries[i][0];
|
|
15
15
|
output[0][i] = typeToConstructor(registry, entries[i][1]);
|
|
16
16
|
}
|
package/utils/sanitize.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
}
|
|
4
|
-
declare type Mapper = (value: string, options?: SanitizeOptions) => string;
|
|
1
|
+
import { AnyString } from '../types';
|
|
2
|
+
declare type Mapper = (value: string) => string;
|
|
5
3
|
export declare function trim(): Mapper;
|
|
6
4
|
export declare function findClosing(value: string, start: number): number;
|
|
7
5
|
export declare function alias(src: string, dest: string, withChecks?: boolean): Mapper;
|
|
@@ -13,5 +11,5 @@ export declare function removeGenerics(): Mapper;
|
|
|
13
11
|
export declare function removePairOf(): Mapper;
|
|
14
12
|
export declare function removeTraits(): Mapper;
|
|
15
13
|
export declare function removeWrap(check: string): Mapper;
|
|
16
|
-
export declare function sanitize(value:
|
|
14
|
+
export declare function sanitize(value: AnyString): string;
|
|
17
15
|
export {};
|
package/utils/sanitize.js
CHANGED
|
@@ -117,9 +117,7 @@ export function removeExtensions(type, isSized) {
|
|
|
117
117
|
};
|
|
118
118
|
}
|
|
119
119
|
export function removeColons() {
|
|
120
|
-
return
|
|
121
|
-
allowNamespaces
|
|
122
|
-
} = {}) => {
|
|
120
|
+
return value => {
|
|
123
121
|
let index = 0;
|
|
124
122
|
|
|
125
123
|
while (index !== -1) {
|
|
@@ -128,10 +126,6 @@ export function removeColons() {
|
|
|
128
126
|
if (index === 0) {
|
|
129
127
|
value = value.substring(2);
|
|
130
128
|
} else if (index !== -1) {
|
|
131
|
-
if (allowNamespaces) {
|
|
132
|
-
return value;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
129
|
let start = index;
|
|
136
130
|
|
|
137
131
|
while (start !== -1 && !BOX_PRECEDING.includes(value[start])) {
|
|
@@ -152,7 +146,7 @@ export function removeGenerics() {
|
|
|
152
146
|
// check against the allowed wrappers, be it Vec<..>, Option<...> ...
|
|
153
147
|
const box = ALLOWED_BOXES.find(box => {
|
|
154
148
|
const start = index - box.length;
|
|
155
|
-
return start >= 0 && value.substring(start,
|
|
149
|
+
return start >= 0 && value.substring(start, index) === box && ( // make sure it is stand-alone, i.e. don't catch ElectionResult<...> as Result<...>
|
|
156
150
|
start === 0 || BOX_PRECEDING.includes(value[start - 1]));
|
|
157
151
|
}); // we have not found anything, unwrap generic innards
|
|
158
152
|
|
|
@@ -165,16 +159,12 @@ export function removeGenerics() {
|
|
|
165
159
|
|
|
166
160
|
return value;
|
|
167
161
|
};
|
|
168
|
-
}
|
|
169
|
-
/** @internal */
|
|
170
|
-
|
|
171
|
-
function pairOfReplacer(v) {
|
|
172
|
-
return `(${v},${v})`;
|
|
173
162
|
} // remove the PairOf wrappers
|
|
174
163
|
|
|
175
|
-
|
|
176
164
|
export function removePairOf() {
|
|
177
|
-
|
|
165
|
+
const replacer = v => `(${v},${v})`;
|
|
166
|
+
|
|
167
|
+
return value => replaceTagWith(value, 'PairOf<', replacer);
|
|
178
168
|
} // remove the type traits
|
|
179
169
|
|
|
180
170
|
export function removeTraits() {
|
|
@@ -199,38 +189,28 @@ export function removeTraits() {
|
|
|
199
189
|
.replace(from5, 'Lookup') // replace `<...>::Type`
|
|
200
190
|
.replace(from6, '');
|
|
201
191
|
};
|
|
202
|
-
}
|
|
203
|
-
/** @internal */
|
|
204
|
-
|
|
205
|
-
function wrapReplacer(v) {
|
|
206
|
-
return v;
|
|
207
192
|
} // remove wrapping values, i.e. Box<Proposal> -> Proposal
|
|
208
193
|
|
|
209
|
-
|
|
210
194
|
export function removeWrap(check) {
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types
|
|
195
|
+
const replacer = v => v;
|
|
214
196
|
|
|
215
|
-
|
|
197
|
+
return value => replaceTagWith(value, check, replacer);
|
|
198
|
+
}
|
|
199
|
+
const sanitizeMap = new Map();
|
|
200
|
+
export function sanitize(value) {
|
|
216
201
|
const startValue = value.toString();
|
|
217
|
-
|
|
202
|
+
const memoized = sanitizeMap.get(startValue);
|
|
218
203
|
|
|
219
|
-
if (
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
if (memoized) {
|
|
223
|
-
return memoized;
|
|
224
|
-
}
|
|
204
|
+
if (memoized) {
|
|
205
|
+
return memoized;
|
|
225
206
|
}
|
|
226
207
|
|
|
227
|
-
|
|
228
|
-
result = mappings[i](result, options);
|
|
229
|
-
}
|
|
208
|
+
let result = startValue;
|
|
230
209
|
|
|
231
|
-
|
|
232
|
-
|
|
210
|
+
for (let i = 0; i < mappings.length; i++) {
|
|
211
|
+
result = mappings[i](result);
|
|
233
212
|
}
|
|
234
213
|
|
|
214
|
+
sanitizeMap.set(startValue, result);
|
|
235
215
|
return result;
|
|
236
216
|
}
|