@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/Struct.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
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, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
6
|
+
import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from "../utils/index.js";
|
|
7
|
+
|
|
8
|
+
/** @internal */
|
|
9
|
+
function decodeStructFromObject(registry, Types, value, jsonMap) {
|
|
10
|
+
let jsonObj;
|
|
11
|
+
const inputKeys = Object.keys(Types);
|
|
12
|
+
const typeofArray = Array.isArray(value);
|
|
13
|
+
const typeofMap = value instanceof Map;
|
|
14
|
+
assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
|
|
15
|
+
assert(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${inputKeys.join(', ')}`);
|
|
16
|
+
const raw = new Array(inputKeys.length);
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < inputKeys.length; i++) {
|
|
19
|
+
const key = inputKeys[i];
|
|
20
|
+
const jsonKey = jsonMap.get(key) || key;
|
|
21
|
+
const Type = Types[key];
|
|
22
|
+
let assign;
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
if (typeofArray) {
|
|
26
|
+
assign = value[i];
|
|
27
|
+
} else if (typeofMap) {
|
|
28
|
+
assign = jsonKey && value.get(jsonKey);
|
|
29
|
+
} else {
|
|
30
|
+
assign = jsonKey && value[jsonKey];
|
|
31
|
+
|
|
32
|
+
if (isUndefined(assign)) {
|
|
33
|
+
if (isUndefined(jsonObj)) {
|
|
34
|
+
const entries = Object.entries(value);
|
|
35
|
+
jsonObj = {};
|
|
36
|
+
|
|
37
|
+
for (let e = 0; e < entries.length; e++) {
|
|
38
|
+
jsonObj[stringCamelCase(entries[e][0])] = entries[e][1];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
assign = jsonKey && jsonObj[jsonKey];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
raw[i] = [key, assign instanceof Type ? assign : new Type(registry, assign)];
|
|
47
|
+
} catch (error) {
|
|
48
|
+
let type = Type.name;
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
type = new Type(registry).toRawType();
|
|
52
|
+
} catch (error) {// ignore
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
throw new Error(`Struct: failed on ${jsonKey}: ${type}:: ${error.message}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return [raw, 0];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @name Struct
|
|
63
|
+
* @description
|
|
64
|
+
* A Struct defines an Object with key-value pairs - where the values are Codec values. It removes
|
|
65
|
+
* a lot of repetition from the actual coding, define a structure type, pass it the key/Codec
|
|
66
|
+
* values in the constructor and it manages the decoding. It is important that the constructor
|
|
67
|
+
* values matches 100% to the order in th Rust code, i.e. don't go crazy and make it alphabetical,
|
|
68
|
+
* it needs to decoded in the specific defined order.
|
|
69
|
+
* @noInheritDoc
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
var _registry = /*#__PURE__*/_classPrivateFieldLooseKey("registry");
|
|
74
|
+
|
|
75
|
+
var _jsonMap = /*#__PURE__*/_classPrivateFieldLooseKey("jsonMap");
|
|
76
|
+
|
|
77
|
+
var _Types = /*#__PURE__*/_classPrivateFieldLooseKey("Types");
|
|
78
|
+
|
|
79
|
+
export class Struct extends Map {
|
|
80
|
+
constructor(registry, Types, value, jsonMap = new Map()) {
|
|
81
|
+
const typeMap = mapToTypeMap(registry, Types);
|
|
82
|
+
const [decoded, decodedLength] = isU8a(value) ? decodeU8a(registry, value, typeMap, true) : isHex(value) ? decodeU8a(registry, u8aToU8a(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
|
|
83
|
+
super(decoded);
|
|
84
|
+
Object.defineProperty(this, _registry, {
|
|
85
|
+
writable: true,
|
|
86
|
+
value: void 0
|
|
87
|
+
});
|
|
88
|
+
this.createdAtHash = void 0;
|
|
89
|
+
this.initialU8aLength = void 0;
|
|
90
|
+
Object.defineProperty(this, _jsonMap, {
|
|
91
|
+
writable: true,
|
|
92
|
+
value: void 0
|
|
93
|
+
});
|
|
94
|
+
Object.defineProperty(this, _Types, {
|
|
95
|
+
writable: true,
|
|
96
|
+
value: void 0
|
|
97
|
+
});
|
|
98
|
+
_classPrivateFieldLooseBase(this, _registry)[_registry] = registry;
|
|
99
|
+
this.initialU8aLength = decodedLength;
|
|
100
|
+
_classPrivateFieldLooseBase(this, _jsonMap)[_jsonMap] = jsonMap;
|
|
101
|
+
_classPrivateFieldLooseBase(this, _Types)[_Types] = typeMap;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static with(Types, jsonMap) {
|
|
105
|
+
const keys = Object.keys(Types);
|
|
106
|
+
return class extends Struct {
|
|
107
|
+
constructor(registry, value) {
|
|
108
|
+
super(registry, Types, value, jsonMap);
|
|
109
|
+
objectProperties(this, keys, k => this.get(k));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* @description The available keys for this struct
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
get defKeys() {
|
|
120
|
+
return Object.keys(_classPrivateFieldLooseBase(this, _Types)[_Types]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
getT(key) {
|
|
124
|
+
return this.get(key);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* @description Checks if the value is an empty value
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
get isEmpty() {
|
|
132
|
+
for (const v of this.values()) {
|
|
133
|
+
if (!v.isEmpty) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @description Returns the Type description of the structure
|
|
142
|
+
*/
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
get Type() {
|
|
146
|
+
const result = {};
|
|
147
|
+
const defs = Object.entries(_classPrivateFieldLooseBase(this, _Types)[_Types]);
|
|
148
|
+
|
|
149
|
+
for (let i = 0; i < defs.length; i++) {
|
|
150
|
+
const [key, Type] = defs[i];
|
|
151
|
+
result[key] = new Type(this.registry).toRawType();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
158
|
+
*/
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
get encodedLength() {
|
|
162
|
+
let total = 0;
|
|
163
|
+
|
|
164
|
+
for (const v of this.values()) {
|
|
165
|
+
total += v.encodedLength;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return total;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* @description returns a hash of the contents
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
get hash() {
|
|
176
|
+
return this.registry.hash(this.toU8a());
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
get registry() {
|
|
180
|
+
return _classPrivateFieldLooseBase(this, _registry)[_registry];
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @description Compares the value of the input to see if there is a match
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
eq(other) {
|
|
188
|
+
return compareMap(this, other);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @description Returns a specific names entry in the structure
|
|
192
|
+
* @param name The name of the entry to retrieve
|
|
193
|
+
*/
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
get(name) {
|
|
197
|
+
return super.get(name);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* @description Returns the values of a member at a specific index (Rather use get(name) for performance)
|
|
201
|
+
*/
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
getAtIndex(index) {
|
|
205
|
+
return this.toArray()[index];
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* @description Converts the Object to an standard JavaScript Array
|
|
209
|
+
*/
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
toArray() {
|
|
213
|
+
return [...this.values()];
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* @description Returns a hex string representation of the value
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
toHex() {
|
|
221
|
+
return u8aToHex(this.toU8a());
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
225
|
+
*/
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
toHuman(isExtended) {
|
|
229
|
+
const json = {};
|
|
230
|
+
|
|
231
|
+
for (const [k, v] of this.entries()) {
|
|
232
|
+
json[k] = v && v.toHuman(isExtended);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return json;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
239
|
+
*/
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
toJSON() {
|
|
243
|
+
const json = {};
|
|
244
|
+
|
|
245
|
+
for (const [k, v] of this.entries()) {
|
|
246
|
+
const jsonKey = _classPrivateFieldLooseBase(this, _jsonMap)[_jsonMap].get(k) || k;
|
|
247
|
+
json[jsonKey] = v && v.toJSON();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return json;
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* @description Returns the base runtime type name for this instance
|
|
254
|
+
*/
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
toRawType() {
|
|
258
|
+
return stringify(typesToMap(this.registry, _classPrivateFieldLooseBase(this, _Types)[_Types]));
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* @description Returns the string representation of the value
|
|
262
|
+
*/
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
toString() {
|
|
266
|
+
return stringify(this.toJSON());
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
270
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
271
|
+
*/
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
toU8a(isBare) {
|
|
275
|
+
const encoded = [];
|
|
276
|
+
|
|
277
|
+
for (const [k, v] of this.entries()) {
|
|
278
|
+
if (v && isFunction(v.toU8a)) {
|
|
279
|
+
encoded.push(v.toU8a(!isBare || isBoolean(isBare) ? isBare : isBare[k]));
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return u8aConcat(...encoded);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
}
|
package/native/Text.cjs
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.Text = 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 _Raw = require("./Raw.cjs");
|
|
17
|
+
|
|
18
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
19
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
20
|
+
const MAX_LENGTH = 128 * 1024;
|
|
21
|
+
/** @internal */
|
|
22
|
+
|
|
23
|
+
function decodeText(value) {
|
|
24
|
+
if ((0, _util.isU8a)(value)) {
|
|
25
|
+
if (!value.length) {
|
|
26
|
+
return ['', 0];
|
|
27
|
+
} // for Raw, the internal buffer does not have an internal length
|
|
28
|
+
// (the same applies in e.g. Bytes, where length is added at encoding-time)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
if (value instanceof _Raw.Raw) {
|
|
32
|
+
return [(0, _util.u8aToString)(value), 0];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const [offset, length] = (0, _util.compactFromU8a)(value);
|
|
36
|
+
const total = offset + length.toNumber();
|
|
37
|
+
(0, _util.assert)(length.lten(MAX_LENGTH), () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
|
38
|
+
(0, _util.assert)(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
|
39
|
+
return [(0, _util.u8aToString)(value.subarray(offset, total)), total];
|
|
40
|
+
} else if ((0, _util.isHex)(value)) {
|
|
41
|
+
return [(0, _util.u8aToString)((0, _util.hexToU8a)(value)), 0];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return [value ? value.toString() : '', 0];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* @name Text
|
|
48
|
+
* @description
|
|
49
|
+
* This is a string wrapper, along with the length. It is used both for strings as well
|
|
50
|
+
* as items such as documentation. It simply extends the standard JS `String` built-in
|
|
51
|
+
* object, inheriting all methods exposed from `String`.
|
|
52
|
+
* @noInheritDoc
|
|
53
|
+
*/
|
|
54
|
+
// TODO
|
|
55
|
+
// - Strings should probably be trimmed (docs do come through with extra padding)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
var _initialU8aLength = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("initialU8aLength");
|
|
59
|
+
|
|
60
|
+
var _override = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("override");
|
|
61
|
+
|
|
62
|
+
class Text extends String {
|
|
63
|
+
constructor(registry, value) {
|
|
64
|
+
const [str, decodedLength] = decodeText(value);
|
|
65
|
+
super(str);
|
|
66
|
+
this.registry = void 0;
|
|
67
|
+
this.createdAtHash = void 0;
|
|
68
|
+
Object.defineProperty(this, _initialU8aLength, {
|
|
69
|
+
writable: true,
|
|
70
|
+
value: void 0
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(this, _override, {
|
|
73
|
+
writable: true,
|
|
74
|
+
value: null
|
|
75
|
+
});
|
|
76
|
+
this.registry = registry;
|
|
77
|
+
(0, _classPrivateFieldLooseBase2.default)(this, _initialU8aLength)[_initialU8aLength] = decodedLength;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
get encodedLength() {
|
|
85
|
+
return this.toU8a().length;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
get initialU8aLength() {
|
|
93
|
+
return (0, _classPrivateFieldLooseBase2.default)(this, _initialU8aLength)[_initialU8aLength];
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* @description returns a hash of the contents
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
get hash() {
|
|
101
|
+
return this.registry.hash(this.toU8a());
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @description Checks if the value is an empty value
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
get isEmpty() {
|
|
109
|
+
return this.length === 0;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* @description The length of the value
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
get length() {
|
|
117
|
+
// only included here since we ignore inherited docs
|
|
118
|
+
return super.length;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* @description Compares the value of the input to see if there is a match
|
|
122
|
+
*/
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
eq(other) {
|
|
126
|
+
return (0, _util.isString)(other) ? this.toString() === other.toString() : false;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* @description Set an override value for this
|
|
130
|
+
*/
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
setOverride(override) {
|
|
134
|
+
(0, _classPrivateFieldLooseBase2.default)(this, _override)[_override] = override;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* @description Returns a hex string representation of the value
|
|
138
|
+
*/
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
toHex() {
|
|
142
|
+
// like with Vec<u8>, when we are encoding to hex, we don't actually add
|
|
143
|
+
// the length prefix (it is already implied by the actual string length)
|
|
144
|
+
return (0, _util.u8aToHex)(this.toU8a(true));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
toHuman() {
|
|
152
|
+
return this.toJSON();
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
toJSON() {
|
|
160
|
+
return this.toString();
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* @description Returns the base runtime type name for this instance
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
toRawType() {
|
|
168
|
+
return 'Text';
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* @description Returns the string representation of the value
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
toString() {
|
|
176
|
+
return (0, _classPrivateFieldLooseBase2.default)(this, _override)[_override] || super.toString();
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
180
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
181
|
+
*/
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
toU8a(isBare) {
|
|
185
|
+
// NOTE Here we use the super toString (we are not taking overrides into account,
|
|
186
|
+
// rather encoding the original value the string was constructed with)
|
|
187
|
+
const encoded = (0, _util.stringToU8a)(super.toString());
|
|
188
|
+
return isBare ? encoded : (0, _util.compactAddLength)(encoded);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
exports.Text = Text;
|
package/native/Text.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { HexString } from '@polkadot/util/types';
|
|
2
|
+
import type { AnyU8a, Codec, CodecRegistry, IU8a } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* @name Text
|
|
5
|
+
* @description
|
|
6
|
+
* This is a string wrapper, along with the length. It is used both for strings as well
|
|
7
|
+
* as items such as documentation. It simply extends the standard JS `String` built-in
|
|
8
|
+
* object, inheriting all methods exposed from `String`.
|
|
9
|
+
* @noInheritDoc
|
|
10
|
+
*/
|
|
11
|
+
export declare class Text extends String implements Codec {
|
|
12
|
+
#private;
|
|
13
|
+
readonly registry: CodecRegistry;
|
|
14
|
+
createdAtHash?: IU8a;
|
|
15
|
+
constructor(registry: CodecRegistry, value?: null | Text | string | AnyU8a | {
|
|
16
|
+
toString: () => string;
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
20
|
+
*/
|
|
21
|
+
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
|
+
/**
|
|
27
|
+
* @description returns a hash of the contents
|
|
28
|
+
*/
|
|
29
|
+
get hash(): IU8a;
|
|
30
|
+
/**
|
|
31
|
+
* @description Checks if the value is an empty value
|
|
32
|
+
*/
|
|
33
|
+
get isEmpty(): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* @description The length of the value
|
|
36
|
+
*/
|
|
37
|
+
get length(): number;
|
|
38
|
+
/**
|
|
39
|
+
* @description Compares the value of the input to see if there is a match
|
|
40
|
+
*/
|
|
41
|
+
eq(other?: unknown): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* @description Set an override value for this
|
|
44
|
+
*/
|
|
45
|
+
setOverride(override: string): void;
|
|
46
|
+
/**
|
|
47
|
+
* @description Returns a hex string representation of the value
|
|
48
|
+
*/
|
|
49
|
+
toHex(): HexString;
|
|
50
|
+
/**
|
|
51
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
52
|
+
*/
|
|
53
|
+
toHuman(): string;
|
|
54
|
+
/**
|
|
55
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
56
|
+
*/
|
|
57
|
+
toJSON(): string;
|
|
58
|
+
/**
|
|
59
|
+
* @description Returns the base runtime type name for this instance
|
|
60
|
+
*/
|
|
61
|
+
toRawType(): string;
|
|
62
|
+
/**
|
|
63
|
+
* @description Returns the string representation of the value
|
|
64
|
+
*/
|
|
65
|
+
toString(): string;
|
|
66
|
+
/**
|
|
67
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
68
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
69
|
+
*/
|
|
70
|
+
toU8a(isBare?: boolean): Uint8Array;
|
|
71
|
+
}
|