@polkadot/types-codec 6.12.2-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.cjs +208 -0
- package/abstract/AbstractArray.d.ts +84 -0
- package/abstract/AbstractArray.js +197 -0
- package/abstract/AbstractInt.cjs +254 -0
- package/abstract/AbstractInt.d.ts +75 -0
- package/abstract/AbstractInt.js +231 -0
- package/abstract/index.cjs +21 -0
- package/abstract/index.d.ts +2 -0
- package/abstract/index.js +4 -0
- package/base/Base.cjs +137 -0
- package/base/Base.d.ts +56 -0
- package/base/Base.js +125 -0
- package/base/Compact.cjs +201 -0
- package/base/Compact.d.ts +83 -0
- package/base/Compact.js +186 -0
- package/base/DoNotConstruct.cjs +101 -0
- package/base/DoNotConstruct.d.ts +33 -0
- package/base/DoNotConstruct.js +88 -0
- package/base/Enum.cjs +445 -0
- package/base/Enum.d.ts +105 -0
- package/base/Enum.js +421 -0
- package/base/Int.cjs +45 -0
- package/base/Int.d.ts +16 -0
- package/base/Int.js +33 -0
- package/base/Null.cjs +111 -0
- package/base/Null.d.ts +54 -0
- package/base/Null.js +101 -0
- package/base/Option.cjs +256 -0
- package/base/Option.d.ts +88 -0
- package/base/Option.js +240 -0
- package/base/Result.cjs +109 -0
- package/base/Result.d.ts +46 -0
- package/base/Result.js +98 -0
- package/base/Tuple.cjs +132 -0
- package/base/Tuple.d.ts +39 -0
- package/base/Tuple.js +116 -0
- package/base/UInt.cjs +39 -0
- package/base/UInt.d.ts +15 -0
- package/base/UInt.js +29 -0
- package/base/Vec.cjs +126 -0
- package/base/Vec.d.ts +28 -0
- package/base/Vec.js +104 -0
- package/base/VecAny.cjs +31 -0
- package/base/VecAny.d.ts +15 -0
- package/base/VecAny.js +21 -0
- package/base/VecFixed.cjs +111 -0
- package/base/VecFixed.d.ts +26 -0
- package/base/VecFixed.js +93 -0
- package/base/index.cjs +109 -0
- package/base/index.d.ts +13 -0
- package/base/index.js +15 -0
- package/bundle.cjs +100 -0
- package/bundle.d.ts +7 -0
- package/bundle.js +9 -0
- package/detectPackage.cjs +9 -0
- package/detectPackage.d.ts +1 -0
- package/detectPackage.js +5 -0
- package/extended/BTreeMap.cjs +24 -0
- package/extended/BTreeMap.d.ts +5 -0
- package/extended/BTreeMap.js +14 -0
- package/extended/BTreeSet.cjs +227 -0
- package/extended/BTreeSet.d.ts +55 -0
- package/extended/BTreeSet.js +212 -0
- package/extended/BitVec.cjs +97 -0
- package/extended/BitVec.d.ts +26 -0
- package/extended/BitVec.js +82 -0
- package/extended/Bytes.cjs +86 -0
- package/extended/Bytes.d.ts +25 -0
- package/extended/Bytes.js +75 -0
- package/extended/HashMap.cjs +24 -0
- package/extended/HashMap.d.ts +5 -0
- package/extended/HashMap.js +14 -0
- package/extended/Linkage.cjs +89 -0
- package/extended/Linkage.d.ts +31 -0
- package/extended/Linkage.js +71 -0
- package/extended/Map.cjs +244 -0
- package/extended/Map.d.ts +50 -0
- package/extended/Map.js +225 -0
- package/extended/Range.cjs +92 -0
- package/extended/Range.d.ts +30 -0
- package/extended/Range.js +75 -0
- package/extended/Type.cjs +39 -0
- package/extended/Type.d.ts +16 -0
- package/extended/Type.js +27 -0
- package/extended/U8aFixed.cjs +66 -0
- package/extended/U8aFixed.d.ts +16 -0
- package/extended/U8aFixed.js +53 -0
- package/extended/WrapperOpaque.cjs +60 -0
- package/extended/WrapperOpaque.d.ts +14 -0
- package/extended/WrapperOpaque.js +49 -0
- package/extended/index.cjs +105 -0
- package/extended/index.d.ts +11 -0
- package/extended/index.js +13 -0
- package/index.cjs +20 -0
- package/index.d.ts +2 -0
- package/index.js +4 -0
- package/native/Bool.cjs +141 -0
- package/native/Bool.d.ts +62 -0
- package/native/Bool.js +130 -0
- package/native/Date.cjs +170 -0
- package/native/Date.d.ts +76 -0
- package/native/Date.js +158 -0
- package/native/Json.cjs +137 -0
- package/native/Json.d.ts +56 -0
- package/native/Json.js +121 -0
- package/native/Raw.cjs +192 -0
- package/native/Raw.d.ts +90 -0
- package/native/Raw.js +182 -0
- package/native/Set.cjs +268 -0
- package/native/Set.d.ts +76 -0
- package/native/Set.js +251 -0
- package/native/Struct.cjs +302 -0
- package/native/Struct.d.ts +93 -0
- package/native/Struct.js +286 -0
- package/native/Text.cjs +193 -0
- package/native/Text.d.ts +71 -0
- package/native/Text.js +178 -0
- package/native/index.cjs +79 -0
- package/native/index.d.ts +7 -0
- package/native/index.js +9 -0
- package/package.json +387 -0
- package/packageInfo.cjs +14 -0
- package/packageInfo.d.ts +4 -0
- package/packageInfo.js +7 -0
- package/primitive/I128.cjs +26 -0
- package/primitive/I128.d.ts +11 -0
- package/primitive/I128.js +16 -0
- package/primitive/I16.cjs +26 -0
- package/primitive/I16.d.ts +11 -0
- package/primitive/I16.js +16 -0
- package/primitive/I256.cjs +26 -0
- package/primitive/I256.d.ts +11 -0
- package/primitive/I256.js +16 -0
- package/primitive/I32.cjs +26 -0
- package/primitive/I32.d.ts +11 -0
- package/primitive/I32.js +16 -0
- package/primitive/I64.cjs +26 -0
- package/primitive/I64.d.ts +11 -0
- package/primitive/I64.js +16 -0
- package/primitive/I8.cjs +26 -0
- package/primitive/I8.d.ts +11 -0
- package/primitive/I8.js +16 -0
- package/primitive/U128.cjs +26 -0
- package/primitive/U128.d.ts +11 -0
- package/primitive/U128.js +16 -0
- package/primitive/U16.cjs +26 -0
- package/primitive/U16.d.ts +11 -0
- package/primitive/U16.js +16 -0
- package/primitive/U256.cjs +26 -0
- package/primitive/U256.d.ts +11 -0
- package/primitive/U256.js +16 -0
- package/primitive/U32.cjs +26 -0
- package/primitive/U32.d.ts +11 -0
- package/primitive/U32.js +16 -0
- package/primitive/U64.cjs +26 -0
- package/primitive/U64.d.ts +11 -0
- package/primitive/U64.js +16 -0
- package/primitive/U8.cjs +26 -0
- package/primitive/U8.d.ts +11 -0
- package/primitive/U8.js +16 -0
- package/primitive/USize.cjs +28 -0
- package/primitive/USize.d.ts +12 -0
- package/primitive/USize.js +18 -0
- package/primitive/index.cjs +187 -0
- package/primitive/index.d.ts +13 -0
- package/primitive/index.js +15 -0
- package/types/codec.cjs +1 -0
- package/types/codec.d.ts +77 -0
- package/types/codec.js +1 -0
- package/types/helpers.cjs +1 -0
- package/types/helpers.d.ts +14 -0
- package/types/helpers.js +1 -0
- package/types/index.cjs +57 -0
- package/types/index.d.ts +4 -0
- package/types/index.js +6 -0
- package/types/interfaces.cjs +1 -0
- package/types/interfaces.d.ts +69 -0
- package/types/interfaces.js +1 -0
- package/types/registry.cjs +1 -0
- package/types/registry.d.ts +61 -0
- package/types/registry.js +1 -0
- package/utils/compareArray.cjs +22 -0
- package/utils/compareArray.d.ts +1 -0
- package/utils/compareArray.js +13 -0
- package/utils/compareMap.cjs +39 -0
- package/utils/compareMap.d.ts +1 -0
- package/utils/compareMap.js +31 -0
- package/utils/compareSet.cjs +29 -0
- package/utils/compareSet.d.ts +1 -0
- package/utils/compareSet.js +22 -0
- package/utils/decodeU8a.cjs +69 -0
- package/utils/decodeU8a.d.ts +13 -0
- package/utils/decodeU8a.js +60 -0
- package/utils/index.cjs +95 -0
- package/utils/index.d.ts +9 -0
- package/utils/index.js +11 -0
- package/utils/mapToTypeMap.cjs +25 -0
- package/utils/mapToTypeMap.d.ts +5 -0
- package/utils/mapToTypeMap.js +17 -0
- package/utils/sanitize.cjs +242 -0
- package/utils/sanitize.d.ts +16 -0
- package/utils/sanitize.js +215 -0
- package/utils/sortValues.cjs +89 -0
- package/utils/sortValues.d.ts +13 -0
- package/utils/sortValues.js +73 -0
- package/utils/typeToConstructor.cjs +14 -0
- package/utils/typeToConstructor.d.ts +2 -0
- package/utils/typeToConstructor.js +6 -0
- package/utils/typesToMap.cjs +20 -0
- package/utils/typesToMap.d.ts +2 -0
- package/utils/typesToMap.js +13 -0
- package/utils/util.cjs +15 -0
- package/utils/util.d.ts +3 -0
- package/utils/util.js +7 -0
package/native/Set.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import _classPrivateFieldLooseBase from "@babel/runtime/helpers/esm/classPrivateFieldLooseBase";
|
|
2
|
+
import _classPrivateFieldLooseKey from "@babel/runtime/helpers/esm/classPrivateFieldLooseKey";
|
|
3
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
4
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
6
|
+
import { compareArray } from "../utils/index.js";
|
|
7
|
+
|
|
8
|
+
function encodeSet(setValues, values) {
|
|
9
|
+
const encoded = new BN(0);
|
|
10
|
+
|
|
11
|
+
for (let i = 0; i < values.length; i++) {
|
|
12
|
+
encoded.ior(bnToBn(setValues[values[i]] || 0));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return encoded;
|
|
16
|
+
}
|
|
17
|
+
/** @internal */
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
function decodeSetArray(setValues, values) {
|
|
21
|
+
const result = new Array(values.length);
|
|
22
|
+
|
|
23
|
+
for (let i = 0; i < values.length; i++) {
|
|
24
|
+
const key = values[i];
|
|
25
|
+
assert(!isUndefined(setValues[key]), () => `Set: Invalid key '${key}' passed to Set, allowed ${Object.keys(setValues).join(', ')}`);
|
|
26
|
+
result[i] = key;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
/** @internal */
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
function decodeSetNumber(setValues, _value) {
|
|
35
|
+
const bn = bnToBn(_value);
|
|
36
|
+
const keys = Object.keys(setValues);
|
|
37
|
+
const result = [];
|
|
38
|
+
|
|
39
|
+
for (let i = 0; i < keys.length; i++) {
|
|
40
|
+
const key = keys[i];
|
|
41
|
+
|
|
42
|
+
if (bn.and(bnToBn(setValues[key])).eq(bnToBn(setValues[key]))) {
|
|
43
|
+
result.push(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const computed = encodeSet(setValues, result);
|
|
48
|
+
assert(bn.eq(computed), () => `Set: Mismatch decoding '${bn.toString()}', computed as '${computed.toString()}' with ${result.join(', ')}`);
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
/** @internal */
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
function decodeSet(setValues, value = 0, bitLength) {
|
|
55
|
+
assert(bitLength % 8 === 0, () => `Expected valid bitLength, power of 8, found ${bitLength}`);
|
|
56
|
+
const byteLength = bitLength / 8;
|
|
57
|
+
|
|
58
|
+
if (isU8a(value)) {
|
|
59
|
+
return value.length === 0 ? [] : decodeSetNumber(setValues, u8aToBn(value.subarray(0, byteLength), {
|
|
60
|
+
isLe: true
|
|
61
|
+
}));
|
|
62
|
+
} else if (isString(value)) {
|
|
63
|
+
return decodeSet(setValues, u8aToU8a(value), byteLength);
|
|
64
|
+
} else if (value instanceof Set || Array.isArray(value)) {
|
|
65
|
+
const input = Array.isArray(value) ? value : [...value.values()];
|
|
66
|
+
return decodeSetArray(setValues, input);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return decodeSetNumber(setValues, value);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* @name Set
|
|
73
|
+
* @description
|
|
74
|
+
* An Set is an array of string values, represented an an encoded type by
|
|
75
|
+
* a bitwise representation of the values.
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
var _allowed = /*#__PURE__*/_classPrivateFieldLooseKey("allowed");
|
|
80
|
+
|
|
81
|
+
var _byteLength = /*#__PURE__*/_classPrivateFieldLooseKey("byteLength");
|
|
82
|
+
|
|
83
|
+
export class CodecSet extends Set {
|
|
84
|
+
constructor(registry, setValues, value, bitLength = 8) {
|
|
85
|
+
super(decodeSet(setValues, value, bitLength));
|
|
86
|
+
this.registry = void 0;
|
|
87
|
+
this.createdAtHash = void 0;
|
|
88
|
+
Object.defineProperty(this, _allowed, {
|
|
89
|
+
writable: true,
|
|
90
|
+
value: void 0
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(this, _byteLength, {
|
|
93
|
+
writable: true,
|
|
94
|
+
value: void 0
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
this.add = key => {
|
|
98
|
+
// ^^^ add = () property done to assign this instance's this, otherwise Set.add creates "some" chaos
|
|
99
|
+
// we have the isUndefined(this._setValues) in here as well, add is used internally
|
|
100
|
+
// in the Set constructor (so it is undefined at this point, and should allow)
|
|
101
|
+
assert(isUndefined(_classPrivateFieldLooseBase(this, _allowed)[_allowed]) || !isUndefined(_classPrivateFieldLooseBase(this, _allowed)[_allowed][key]), () => `Set: Invalid key '${key}' on add`);
|
|
102
|
+
super.add(key);
|
|
103
|
+
return this;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
this.registry = registry;
|
|
107
|
+
_classPrivateFieldLooseBase(this, _allowed)[_allowed] = setValues;
|
|
108
|
+
_classPrivateFieldLooseBase(this, _byteLength)[_byteLength] = bitLength / 8;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static with(values, bitLength) {
|
|
112
|
+
const keys = Object.keys(values);
|
|
113
|
+
const isKeys = new Array(keys.length);
|
|
114
|
+
|
|
115
|
+
for (let i = 0; i < keys.length; i++) {
|
|
116
|
+
isKeys[i] = `is${stringPascalCase(keys[i])}`;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return class extends CodecSet {
|
|
120
|
+
constructor(registry, value) {
|
|
121
|
+
super(registry, values, value, bitLength);
|
|
122
|
+
objectProperties(this, isKeys, (_, i) => this.strings.includes(keys[i]));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
get encodedLength() {
|
|
133
|
+
return _classPrivateFieldLooseBase(this, _byteLength)[_byteLength];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @description returns a hash of the contents
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
get hash() {
|
|
141
|
+
return this.registry.hash(this.toU8a());
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* @description true is the Set contains no values
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
get isEmpty() {
|
|
149
|
+
return this.size === 0;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* @description The actual set values as a string[]
|
|
153
|
+
*/
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
get strings() {
|
|
157
|
+
return [...super.values()];
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* @description The encoded value for the set members
|
|
161
|
+
*/
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
get valueEncoded() {
|
|
165
|
+
return encodeSet(_classPrivateFieldLooseBase(this, _allowed)[_allowed], this.strings);
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* @description adds a value to the Set (extended to allow for validity checking)
|
|
169
|
+
*/
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @description Compares the value of the input to see if there is a match
|
|
174
|
+
*/
|
|
175
|
+
eq(other) {
|
|
176
|
+
if (Array.isArray(other)) {
|
|
177
|
+
// we don't actually care about the order, sort the values
|
|
178
|
+
return compareArray(this.strings.sort(), other.sort());
|
|
179
|
+
} else if (other instanceof Set) {
|
|
180
|
+
return this.eq([...other.values()]);
|
|
181
|
+
} else if (isNumber(other) || isBn(other)) {
|
|
182
|
+
return this.valueEncoded.eq(bnToBn(other));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* @description Returns a hex string representation of the value
|
|
189
|
+
*/
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
toHex() {
|
|
193
|
+
return u8aToHex(this.toU8a());
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
toHuman() {
|
|
201
|
+
return this.toJSON();
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
205
|
+
*/
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
toJSON() {
|
|
209
|
+
return this.strings;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* @description The encoded value for the set members
|
|
213
|
+
*/
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
toNumber() {
|
|
217
|
+
return this.valueEncoded.toNumber();
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* @description Returns the base runtime type name for this instance
|
|
221
|
+
*/
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
toRawType() {
|
|
225
|
+
return stringify({
|
|
226
|
+
_set: _classPrivateFieldLooseBase(this, _allowed)[_allowed]
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* @description Returns the string representation of the value
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
toString() {
|
|
235
|
+
return `[${this.strings.join(', ')}]`;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
239
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
240
|
+
*/
|
|
241
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
toU8a(isBare) {
|
|
245
|
+
return bnToU8a(this.valueEncoded, {
|
|
246
|
+
bitLength: _classPrivateFieldLooseBase(this, _byteLength)[_byteLength] * 8,
|
|
247
|
+
isLe: true
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.Struct = void 0;
|
|
9
|
+
|
|
10
|
+
var _classPrivateFieldLooseBase2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseBase"));
|
|
11
|
+
|
|
12
|
+
var _classPrivateFieldLooseKey2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseKey"));
|
|
13
|
+
|
|
14
|
+
var _util = require("@polkadot/util");
|
|
15
|
+
|
|
16
|
+
var _index = require("../utils/index.cjs");
|
|
17
|
+
|
|
18
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
19
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
20
|
+
|
|
21
|
+
/** @internal */
|
|
22
|
+
function decodeStructFromObject(registry, Types, value, jsonMap) {
|
|
23
|
+
let jsonObj;
|
|
24
|
+
const inputKeys = Object.keys(Types);
|
|
25
|
+
const typeofArray = Array.isArray(value);
|
|
26
|
+
const typeofMap = value instanceof Map;
|
|
27
|
+
(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`);
|
|
28
|
+
(0, _util.assert)(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${inputKeys.join(', ')}`);
|
|
29
|
+
const raw = new Array(inputKeys.length);
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < inputKeys.length; i++) {
|
|
32
|
+
const key = inputKeys[i];
|
|
33
|
+
const jsonKey = jsonMap.get(key) || key;
|
|
34
|
+
const Type = Types[key];
|
|
35
|
+
let assign;
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
if (typeofArray) {
|
|
39
|
+
assign = value[i];
|
|
40
|
+
} else if (typeofMap) {
|
|
41
|
+
assign = jsonKey && value.get(jsonKey);
|
|
42
|
+
} else {
|
|
43
|
+
assign = jsonKey && value[jsonKey];
|
|
44
|
+
|
|
45
|
+
if ((0, _util.isUndefined)(assign)) {
|
|
46
|
+
if ((0, _util.isUndefined)(jsonObj)) {
|
|
47
|
+
const entries = Object.entries(value);
|
|
48
|
+
jsonObj = {};
|
|
49
|
+
|
|
50
|
+
for (let e = 0; e < entries.length; e++) {
|
|
51
|
+
jsonObj[(0, _util.stringCamelCase)(entries[e][0])] = entries[e][1];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
assign = jsonKey && jsonObj[jsonKey];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
raw[i] = [key, assign instanceof Type ? assign : new Type(registry, assign)];
|
|
60
|
+
} catch (error) {
|
|
61
|
+
let type = Type.name;
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
type = new Type(registry).toRawType();
|
|
65
|
+
} catch (error) {// ignore
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
throw new Error(`Struct: failed on ${jsonKey}: ${type}:: ${error.message}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return [raw, 0];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* @name Struct
|
|
76
|
+
* @description
|
|
77
|
+
* A Struct defines an Object with key-value pairs - where the values are Codec values. It removes
|
|
78
|
+
* a lot of repetition from the actual coding, define a structure type, pass it the key/Codec
|
|
79
|
+
* values in the constructor and it manages the decoding. It is important that the constructor
|
|
80
|
+
* values matches 100% to the order in th Rust code, i.e. don't go crazy and make it alphabetical,
|
|
81
|
+
* it needs to decoded in the specific defined order.
|
|
82
|
+
* @noInheritDoc
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
var _registry = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("registry");
|
|
87
|
+
|
|
88
|
+
var _jsonMap = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("jsonMap");
|
|
89
|
+
|
|
90
|
+
var _Types = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("Types");
|
|
91
|
+
|
|
92
|
+
class Struct extends Map {
|
|
93
|
+
constructor(registry, Types, value) {
|
|
94
|
+
let jsonMap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new Map();
|
|
95
|
+
const typeMap = (0, _index.mapToTypeMap)(registry, Types);
|
|
96
|
+
const [decoded, decodedLength] = (0, _util.isU8a)(value) ? (0, _index.decodeU8a)(registry, value, typeMap, true) : (0, _util.isHex)(value) ? (0, _index.decodeU8a)(registry, (0, _util.u8aToU8a)(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
|
|
97
|
+
super(decoded);
|
|
98
|
+
Object.defineProperty(this, _registry, {
|
|
99
|
+
writable: true,
|
|
100
|
+
value: void 0
|
|
101
|
+
});
|
|
102
|
+
this.createdAtHash = void 0;
|
|
103
|
+
this.initialU8aLength = void 0;
|
|
104
|
+
Object.defineProperty(this, _jsonMap, {
|
|
105
|
+
writable: true,
|
|
106
|
+
value: void 0
|
|
107
|
+
});
|
|
108
|
+
Object.defineProperty(this, _Types, {
|
|
109
|
+
writable: true,
|
|
110
|
+
value: void 0
|
|
111
|
+
});
|
|
112
|
+
(0, _classPrivateFieldLooseBase2.default)(this, _registry)[_registry] = registry;
|
|
113
|
+
this.initialU8aLength = decodedLength;
|
|
114
|
+
(0, _classPrivateFieldLooseBase2.default)(this, _jsonMap)[_jsonMap] = jsonMap;
|
|
115
|
+
(0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types] = typeMap;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
static with(Types, jsonMap) {
|
|
119
|
+
const keys = Object.keys(Types);
|
|
120
|
+
return class extends Struct {
|
|
121
|
+
constructor(registry, value) {
|
|
122
|
+
super(registry, Types, value, jsonMap);
|
|
123
|
+
(0, _util.objectProperties)(this, keys, k => this.get(k));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* @description The available keys for this struct
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
get defKeys() {
|
|
134
|
+
return Object.keys((0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
getT(key) {
|
|
138
|
+
return this.get(key);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @description Checks if the value is an empty value
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
get isEmpty() {
|
|
146
|
+
for (const v of this.values()) {
|
|
147
|
+
if (!v.isEmpty) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* @description Returns the Type description of the structure
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
get Type() {
|
|
160
|
+
const result = {};
|
|
161
|
+
const defs = Object.entries((0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]);
|
|
162
|
+
|
|
163
|
+
for (let i = 0; i < defs.length; i++) {
|
|
164
|
+
const [key, Type] = defs[i];
|
|
165
|
+
result[key] = new Type(this.registry).toRawType();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
get encodedLength() {
|
|
176
|
+
let total = 0;
|
|
177
|
+
|
|
178
|
+
for (const v of this.values()) {
|
|
179
|
+
total += v.encodedLength;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return total;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* @description returns a hash of the contents
|
|
186
|
+
*/
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
get hash() {
|
|
190
|
+
return this.registry.hash(this.toU8a());
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
get registry() {
|
|
194
|
+
return (0, _classPrivateFieldLooseBase2.default)(this, _registry)[_registry];
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* @description Compares the value of the input to see if there is a match
|
|
198
|
+
*/
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
eq(other) {
|
|
202
|
+
return (0, _index.compareMap)(this, other);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* @description Returns a specific names entry in the structure
|
|
206
|
+
* @param name The name of the entry to retrieve
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
get(name) {
|
|
211
|
+
return super.get(name);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* @description Returns the values of a member at a specific index (Rather use get(name) for performance)
|
|
215
|
+
*/
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
getAtIndex(index) {
|
|
219
|
+
return this.toArray()[index];
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* @description Converts the Object to an standard JavaScript Array
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
toArray() {
|
|
227
|
+
return [...this.values()];
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* @description Returns a hex string representation of the value
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
toHex() {
|
|
235
|
+
return (0, _util.u8aToHex)(this.toU8a());
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
239
|
+
*/
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
toHuman(isExtended) {
|
|
243
|
+
const json = {};
|
|
244
|
+
|
|
245
|
+
for (const [k, v] of this.entries()) {
|
|
246
|
+
json[k] = v && v.toHuman(isExtended);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return json;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
253
|
+
*/
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
toJSON() {
|
|
257
|
+
const json = {};
|
|
258
|
+
|
|
259
|
+
for (const [k, v] of this.entries()) {
|
|
260
|
+
const jsonKey = (0, _classPrivateFieldLooseBase2.default)(this, _jsonMap)[_jsonMap].get(k) || k;
|
|
261
|
+
json[jsonKey] = v && v.toJSON();
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return json;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* @description Returns the base runtime type name for this instance
|
|
268
|
+
*/
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
toRawType() {
|
|
272
|
+
return (0, _util.stringify)((0, _index.typesToMap)(this.registry, (0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]));
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* @description Returns the string representation of the value
|
|
276
|
+
*/
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
toString() {
|
|
280
|
+
return (0, _util.stringify)(this.toJSON());
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
284
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
285
|
+
*/
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
toU8a(isBare) {
|
|
289
|
+
const encoded = [];
|
|
290
|
+
|
|
291
|
+
for (const [k, v] of this.entries()) {
|
|
292
|
+
if (v && (0, _util.isFunction)(v.toU8a)) {
|
|
293
|
+
encoded.push(v.toU8a(!isBare || (0, _util.isBoolean)(isBare) ? isBare : isBare[k]));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return (0, _util.u8aConcat)(...encoded);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
exports.Struct = Struct;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { HexString } from '@polkadot/util/types';
|
|
2
|
+
import type { AnyJson, BareOpts, Codec, CodecClass, CodecRegistry, IStruct, IU8a } from '../types';
|
|
3
|
+
declare type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
|
|
4
|
+
/**
|
|
5
|
+
* @name Struct
|
|
6
|
+
* @description
|
|
7
|
+
* A Struct defines an Object with key-value pairs - where the values are Codec values. It removes
|
|
8
|
+
* a lot of repetition from the actual coding, define a structure type, pass it the key/Codec
|
|
9
|
+
* values in the constructor and it manages the decoding. It is important that the constructor
|
|
10
|
+
* values matches 100% to the order in th Rust code, i.e. don't go crazy and make it alphabetical,
|
|
11
|
+
* it needs to decoded in the specific defined order.
|
|
12
|
+
* @noInheritDoc
|
|
13
|
+
*/
|
|
14
|
+
export declare class Struct<S extends TypesDef = TypesDef, V extends {
|
|
15
|
+
[K in keyof S]: any;
|
|
16
|
+
} = {
|
|
17
|
+
[K in keyof S]: any;
|
|
18
|
+
}, E extends {
|
|
19
|
+
[K in keyof S]: string;
|
|
20
|
+
} = {
|
|
21
|
+
[K in keyof S]: string;
|
|
22
|
+
}> extends Map<keyof S, Codec> implements IStruct<keyof S> {
|
|
23
|
+
#private;
|
|
24
|
+
createdAtHash?: IU8a;
|
|
25
|
+
readonly initialU8aLength?: number;
|
|
26
|
+
constructor(registry: CodecRegistry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>);
|
|
27
|
+
static with<S extends TypesDef>(Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>>;
|
|
28
|
+
/**
|
|
29
|
+
* @description The available keys for this struct
|
|
30
|
+
*/
|
|
31
|
+
get defKeys(): string[];
|
|
32
|
+
getT<T>(key: string): T;
|
|
33
|
+
/**
|
|
34
|
+
* @description Checks if the value is an empty value
|
|
35
|
+
*/
|
|
36
|
+
get isEmpty(): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* @description Returns the Type description of the structure
|
|
39
|
+
*/
|
|
40
|
+
get Type(): E;
|
|
41
|
+
/**
|
|
42
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
43
|
+
*/
|
|
44
|
+
get encodedLength(): number;
|
|
45
|
+
/**
|
|
46
|
+
* @description returns a hash of the contents
|
|
47
|
+
*/
|
|
48
|
+
get hash(): IU8a;
|
|
49
|
+
get registry(): CodecRegistry;
|
|
50
|
+
/**
|
|
51
|
+
* @description Compares the value of the input to see if there is a match
|
|
52
|
+
*/
|
|
53
|
+
eq(other?: unknown): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* @description Returns a specific names entry in the structure
|
|
56
|
+
* @param name The name of the entry to retrieve
|
|
57
|
+
*/
|
|
58
|
+
get(name: keyof S): Codec | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* @description Returns the values of a member at a specific index (Rather use get(name) for performance)
|
|
61
|
+
*/
|
|
62
|
+
getAtIndex(index: number): Codec;
|
|
63
|
+
/**
|
|
64
|
+
* @description Converts the Object to an standard JavaScript Array
|
|
65
|
+
*/
|
|
66
|
+
toArray(): Codec[];
|
|
67
|
+
/**
|
|
68
|
+
* @description Returns a hex string representation of the value
|
|
69
|
+
*/
|
|
70
|
+
toHex(): HexString;
|
|
71
|
+
/**
|
|
72
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
73
|
+
*/
|
|
74
|
+
toHuman(isExtended?: boolean): Record<string, AnyJson>;
|
|
75
|
+
/**
|
|
76
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
77
|
+
*/
|
|
78
|
+
toJSON(): Record<string, AnyJson>;
|
|
79
|
+
/**
|
|
80
|
+
* @description Returns the base runtime type name for this instance
|
|
81
|
+
*/
|
|
82
|
+
toRawType(): string;
|
|
83
|
+
/**
|
|
84
|
+
* @description Returns the string representation of the value
|
|
85
|
+
*/
|
|
86
|
+
toString(): string;
|
|
87
|
+
/**
|
|
88
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
89
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
90
|
+
*/
|
|
91
|
+
toU8a(isBare?: BareOpts): Uint8Array;
|
|
92
|
+
}
|
|
93
|
+
export {};
|