@polkadot/types-codec 6.12.2-2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +109 -0
- package/base/Base.d.ts +54 -0
- package/base/Base.js +100 -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 +437 -0
- package/base/Enum.d.ts +101 -0
- package/base/Enum.js +413 -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 +68 -0
- package/extended/WrapperOpaque.d.ts +18 -0
- package/extended/WrapperOpaque.js +57 -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 +133 -0
- package/native/Json.d.ts +55 -0
- package/native/Json.js +117 -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 +289 -0
- package/native/Struct.d.ts +92 -0
- package/native/Struct.js +273 -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 +68 -0
- package/types/interfaces.js +1 -0
- package/types/registry.cjs +1 -0
- package/types/registry.d.ts +44 -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/base/Enum.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { HexString } from '@polkadot/util/types';
|
|
2
|
+
import type { AnyJson, Codec, CodecClass, CodecRegistry, IEnum, IU8a } from '../types';
|
|
3
|
+
export interface EnumCodecClass<T = Codec> {
|
|
4
|
+
new (registry: CodecRegistry, value?: any, index?: number): T;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* @name Enum
|
|
8
|
+
* @description
|
|
9
|
+
* This implements an enum, that based on the value wraps a different type. It is effectively
|
|
10
|
+
* an extension to enum where the value type is determined by the actual index.
|
|
11
|
+
*/
|
|
12
|
+
export declare class Enum implements IEnum {
|
|
13
|
+
#private;
|
|
14
|
+
readonly registry: CodecRegistry;
|
|
15
|
+
createdAtHash?: IU8a;
|
|
16
|
+
readonly initialU8aLength?: number;
|
|
17
|
+
constructor(registry: CodecRegistry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number);
|
|
18
|
+
static with(Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum>;
|
|
19
|
+
/**
|
|
20
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
21
|
+
*/
|
|
22
|
+
get encodedLength(): number;
|
|
23
|
+
/**
|
|
24
|
+
* @description returns a hash of the contents
|
|
25
|
+
*/
|
|
26
|
+
get hash(): IU8a;
|
|
27
|
+
/**
|
|
28
|
+
* @description The index of the enum value
|
|
29
|
+
*/
|
|
30
|
+
get index(): number;
|
|
31
|
+
/**
|
|
32
|
+
* @description true if this is a basic enum (no values)
|
|
33
|
+
*/
|
|
34
|
+
get isBasic(): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* @description Checks if the value is an empty value
|
|
37
|
+
*/
|
|
38
|
+
get isEmpty(): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* @description Checks if the Enum points to a [[Null]] type
|
|
41
|
+
*/
|
|
42
|
+
get isNone(): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* @description Checks if the Enum points to a [[Null]] type
|
|
45
|
+
* @deprecated use isNone
|
|
46
|
+
*/
|
|
47
|
+
get isNull(): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* @description The available keys for this enum
|
|
50
|
+
*/
|
|
51
|
+
get defIndexes(): number[];
|
|
52
|
+
/**
|
|
53
|
+
* @description The available keys for this enum
|
|
54
|
+
*/
|
|
55
|
+
get defKeys(): string[];
|
|
56
|
+
/**
|
|
57
|
+
* @description The name of the type this enum value represents
|
|
58
|
+
*/
|
|
59
|
+
get type(): string;
|
|
60
|
+
/**
|
|
61
|
+
* @description The value of the enum
|
|
62
|
+
*/
|
|
63
|
+
get value(): Codec;
|
|
64
|
+
/**
|
|
65
|
+
* @description Compares the value of the input to see if there is a match
|
|
66
|
+
*/
|
|
67
|
+
eq(other?: unknown): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* @description Returns a hex string representation of the value
|
|
70
|
+
*/
|
|
71
|
+
toHex(): HexString;
|
|
72
|
+
/**
|
|
73
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
74
|
+
*/
|
|
75
|
+
toHuman(isExtended?: boolean): AnyJson;
|
|
76
|
+
/**
|
|
77
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
78
|
+
*/
|
|
79
|
+
toJSON(): AnyJson;
|
|
80
|
+
/**
|
|
81
|
+
* @description Returns the number representation for the value
|
|
82
|
+
*/
|
|
83
|
+
toNumber(): number;
|
|
84
|
+
/**
|
|
85
|
+
* @description Returns a raw struct representation of the enum types
|
|
86
|
+
*/
|
|
87
|
+
protected _toRawStruct(): string[] | Record<string, string | number>;
|
|
88
|
+
/**
|
|
89
|
+
* @description Returns the base runtime type name for this instance
|
|
90
|
+
*/
|
|
91
|
+
toRawType(): string;
|
|
92
|
+
/**
|
|
93
|
+
* @description Returns the string representation of the value
|
|
94
|
+
*/
|
|
95
|
+
toString(): string;
|
|
96
|
+
/**
|
|
97
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
98
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
99
|
+
*/
|
|
100
|
+
toU8a(isBare?: boolean): Uint8Array;
|
|
101
|
+
}
|
package/base/Enum.js
ADDED
|
@@ -0,0 +1,413 @@
|
|
|
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, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
6
|
+
import { mapToTypeMap, typesToMap } from "../utils/index.js";
|
|
7
|
+
import { Null } from "./Null.js"; // export interface, this is used in Enum.with, so required as public by TS
|
|
8
|
+
|
|
9
|
+
function isRustEnum(def) {
|
|
10
|
+
const defValues = Object.values(def);
|
|
11
|
+
|
|
12
|
+
if (defValues.some(v => isNumber(v))) {
|
|
13
|
+
assert(defValues.every(v => isNumber(v) && v >= 0 && v <= 255), 'Invalid number-indexed enum definition');
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function extractDef(registry, _def) {
|
|
21
|
+
const def = {};
|
|
22
|
+
let isBasic;
|
|
23
|
+
let isIndexed;
|
|
24
|
+
|
|
25
|
+
if (Array.isArray(_def)) {
|
|
26
|
+
for (let i = 0; i < _def.length; i++) {
|
|
27
|
+
def[_def[i]] = {
|
|
28
|
+
Type: Null,
|
|
29
|
+
index: i
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
isBasic = true;
|
|
34
|
+
isIndexed = false;
|
|
35
|
+
} else if (isRustEnum(_def)) {
|
|
36
|
+
const entries = Object.entries(mapToTypeMap(registry, _def));
|
|
37
|
+
|
|
38
|
+
for (let i = 0; i < entries.length; i++) {
|
|
39
|
+
const [key, Type] = entries[i];
|
|
40
|
+
def[key] = {
|
|
41
|
+
Type,
|
|
42
|
+
index: i
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
isBasic = !Object.values(def).some(({
|
|
47
|
+
Type
|
|
48
|
+
}) => Type !== Null);
|
|
49
|
+
isIndexed = false;
|
|
50
|
+
} else {
|
|
51
|
+
const entries = Object.entries(_def);
|
|
52
|
+
|
|
53
|
+
for (let i = 0; i < entries.length; i++) {
|
|
54
|
+
const [key, index] = entries[i];
|
|
55
|
+
def[key] = {
|
|
56
|
+
Type: Null,
|
|
57
|
+
index
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
isBasic = true;
|
|
62
|
+
isIndexed = true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
def,
|
|
67
|
+
isBasic,
|
|
68
|
+
isIndexed
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function createFromValue(registry, def, index = 0, value) {
|
|
73
|
+
const entry = Object.values(def).find(e => e.index === index);
|
|
74
|
+
assert(!isUndefined(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
|
|
75
|
+
return {
|
|
76
|
+
index,
|
|
77
|
+
value: value instanceof entry.Type ? value : new entry.Type(registry, value)
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function decodeFromJSON(registry, def, key, value) {
|
|
82
|
+
// JSON comes in the form of { "<type (camelCase)>": "<value for type>" }, here we
|
|
83
|
+
// additionally force to lower to ensure forward compat
|
|
84
|
+
const keys = Object.keys(def).map(k => k.toLowerCase());
|
|
85
|
+
const keyLower = key.toLowerCase();
|
|
86
|
+
const index = keys.indexOf(keyLower);
|
|
87
|
+
assert(index !== -1, () => `Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
return createFromValue(registry, def, Object.values(def)[index].index, value);
|
|
91
|
+
} catch (error) {
|
|
92
|
+
throw new Error(`Enum(${key}):: ${error.message}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function decodeEnum(registry, def, value, index) {
|
|
97
|
+
// NOTE We check the index path first, before looking at values - this allows treating
|
|
98
|
+
// the optional indexes before anything else, more-specific > less-specific
|
|
99
|
+
if (isNumber(index)) {
|
|
100
|
+
return createFromValue(registry, def, index, value);
|
|
101
|
+
} else if (isU8a(value) || isHex(value)) {
|
|
102
|
+
const u8a = u8aToU8a(value); // nested, we don't want to match isObject below
|
|
103
|
+
|
|
104
|
+
if (u8a.length) {
|
|
105
|
+
return createFromValue(registry, def, u8a[0], u8a.subarray(1));
|
|
106
|
+
}
|
|
107
|
+
} else if (value instanceof Enum) {
|
|
108
|
+
return createFromValue(registry, def, value.index, value.value);
|
|
109
|
+
} else if (isNumber(value)) {
|
|
110
|
+
return createFromValue(registry, def, value);
|
|
111
|
+
} else if (isString(value)) {
|
|
112
|
+
return decodeFromJSON(registry, def, value.toString());
|
|
113
|
+
} else if (isObject(value)) {
|
|
114
|
+
const key = Object.keys(value)[0];
|
|
115
|
+
return decodeFromJSON(registry, def, key, value[key]);
|
|
116
|
+
} // Worst-case scenario, return the first with default
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
return createFromValue(registry, def, Object.values(def)[0].index);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @name Enum
|
|
123
|
+
* @description
|
|
124
|
+
* This implements an enum, that based on the value wraps a different type. It is effectively
|
|
125
|
+
* an extension to enum where the value type is determined by the actual index.
|
|
126
|
+
*/
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
var _def2 = /*#__PURE__*/_classPrivateFieldLooseKey("def");
|
|
130
|
+
|
|
131
|
+
var _entryIndex = /*#__PURE__*/_classPrivateFieldLooseKey("entryIndex");
|
|
132
|
+
|
|
133
|
+
var _indexes = /*#__PURE__*/_classPrivateFieldLooseKey("indexes");
|
|
134
|
+
|
|
135
|
+
var _isBasic = /*#__PURE__*/_classPrivateFieldLooseKey("isBasic");
|
|
136
|
+
|
|
137
|
+
var _isIndexed = /*#__PURE__*/_classPrivateFieldLooseKey("isIndexed");
|
|
138
|
+
|
|
139
|
+
var _raw = /*#__PURE__*/_classPrivateFieldLooseKey("raw");
|
|
140
|
+
|
|
141
|
+
export class Enum {
|
|
142
|
+
constructor(registry, Types, value, index) {
|
|
143
|
+
this.registry = void 0;
|
|
144
|
+
this.createdAtHash = void 0;
|
|
145
|
+
Object.defineProperty(this, _def2, {
|
|
146
|
+
writable: true,
|
|
147
|
+
value: void 0
|
|
148
|
+
});
|
|
149
|
+
Object.defineProperty(this, _entryIndex, {
|
|
150
|
+
writable: true,
|
|
151
|
+
value: void 0
|
|
152
|
+
});
|
|
153
|
+
this.initialU8aLength = void 0;
|
|
154
|
+
Object.defineProperty(this, _indexes, {
|
|
155
|
+
writable: true,
|
|
156
|
+
value: void 0
|
|
157
|
+
});
|
|
158
|
+
Object.defineProperty(this, _isBasic, {
|
|
159
|
+
writable: true,
|
|
160
|
+
value: void 0
|
|
161
|
+
});
|
|
162
|
+
Object.defineProperty(this, _isIndexed, {
|
|
163
|
+
writable: true,
|
|
164
|
+
value: void 0
|
|
165
|
+
});
|
|
166
|
+
Object.defineProperty(this, _raw, {
|
|
167
|
+
writable: true,
|
|
168
|
+
value: void 0
|
|
169
|
+
});
|
|
170
|
+
const {
|
|
171
|
+
def,
|
|
172
|
+
isBasic,
|
|
173
|
+
isIndexed
|
|
174
|
+
} = extractDef(registry, Types); // shortcut isU8a as used in SCALE decoding
|
|
175
|
+
|
|
176
|
+
const decoded = isU8a(value) && value.length && !isNumber(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
|
|
177
|
+
this.registry = registry;
|
|
178
|
+
_classPrivateFieldLooseBase(this, _def2)[_def2] = def;
|
|
179
|
+
_classPrivateFieldLooseBase(this, _isBasic)[_isBasic] = isBasic;
|
|
180
|
+
_classPrivateFieldLooseBase(this, _isIndexed)[_isIndexed] = isIndexed;
|
|
181
|
+
_classPrivateFieldLooseBase(this, _indexes)[_indexes] = Object.values(def).map(({
|
|
182
|
+
index
|
|
183
|
+
}) => index);
|
|
184
|
+
_classPrivateFieldLooseBase(this, _entryIndex)[_entryIndex] = _classPrivateFieldLooseBase(this, _indexes)[_indexes].indexOf(decoded.index) || 0;
|
|
185
|
+
_classPrivateFieldLooseBase(this, _raw)[_raw] = decoded.value;
|
|
186
|
+
|
|
187
|
+
if (_classPrivateFieldLooseBase(this, _raw)[_raw].initialU8aLength) {
|
|
188
|
+
this.initialU8aLength = 1 + _classPrivateFieldLooseBase(this, _raw)[_raw].initialU8aLength;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static with(Types) {
|
|
193
|
+
const keys = Array.isArray(Types) ? Types : Object.keys(Types);
|
|
194
|
+
const asKeys = new Array(keys.length);
|
|
195
|
+
const isKeys = new Array(keys.length);
|
|
196
|
+
|
|
197
|
+
for (let i = 0; i < keys.length; i++) {
|
|
198
|
+
const name = stringPascalCase(keys[i].replace(' ', '_'));
|
|
199
|
+
asKeys[i] = `as${name}`;
|
|
200
|
+
isKeys[i] = `is${name}`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return class extends Enum {
|
|
204
|
+
constructor(registry, value, index) {
|
|
205
|
+
super(registry, Types, value, index);
|
|
206
|
+
objectProperties(this, isKeys, (_, i) => this.type === keys[i]);
|
|
207
|
+
objectProperties(this, asKeys, (k, i) => {
|
|
208
|
+
assert(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
|
|
209
|
+
return this.value;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
217
|
+
*/
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
get encodedLength() {
|
|
221
|
+
return 1 + _classPrivateFieldLooseBase(this, _raw)[_raw].encodedLength;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* @description returns a hash of the contents
|
|
225
|
+
*/
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
get hash() {
|
|
229
|
+
return this.registry.hash(this.toU8a());
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* @description The index of the enum value
|
|
233
|
+
*/
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
get index() {
|
|
237
|
+
return _classPrivateFieldLooseBase(this, _indexes)[_indexes][_classPrivateFieldLooseBase(this, _entryIndex)[_entryIndex]];
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* @description true if this is a basic enum (no values)
|
|
241
|
+
*/
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
get isBasic() {
|
|
245
|
+
return _classPrivateFieldLooseBase(this, _isBasic)[_isBasic];
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* @description Checks if the value is an empty value
|
|
249
|
+
*/
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
get isEmpty() {
|
|
253
|
+
return _classPrivateFieldLooseBase(this, _raw)[_raw].isEmpty;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* @description Checks if the Enum points to a [[Null]] type
|
|
257
|
+
*/
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
get isNone() {
|
|
261
|
+
return _classPrivateFieldLooseBase(this, _raw)[_raw] instanceof Null;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* @description Checks if the Enum points to a [[Null]] type
|
|
265
|
+
* @deprecated use isNone
|
|
266
|
+
*/
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
get isNull() {
|
|
270
|
+
return _classPrivateFieldLooseBase(this, _raw)[_raw] instanceof Null;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* @description The available keys for this enum
|
|
274
|
+
*/
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
get defIndexes() {
|
|
278
|
+
return _classPrivateFieldLooseBase(this, _indexes)[_indexes];
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* @description The available keys for this enum
|
|
282
|
+
*/
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
get defKeys() {
|
|
286
|
+
return Object.keys(_classPrivateFieldLooseBase(this, _def2)[_def2]);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* @description The name of the type this enum value represents
|
|
290
|
+
*/
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
get type() {
|
|
294
|
+
return this.defKeys[_classPrivateFieldLooseBase(this, _entryIndex)[_entryIndex]];
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* @description The value of the enum
|
|
298
|
+
*/
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
get value() {
|
|
302
|
+
return _classPrivateFieldLooseBase(this, _raw)[_raw];
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* @description Compares the value of the input to see if there is a match
|
|
306
|
+
*/
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
eq(other) {
|
|
310
|
+
// cater for the case where we only pass the enum index
|
|
311
|
+
if (isU8a(other)) {
|
|
312
|
+
return !this.toU8a().some((entry, index) => entry !== other[index]);
|
|
313
|
+
} else if (isNumber(other)) {
|
|
314
|
+
return this.toNumber() === other;
|
|
315
|
+
} else if (_classPrivateFieldLooseBase(this, _isBasic)[_isBasic] && isString(other)) {
|
|
316
|
+
return this.type === other;
|
|
317
|
+
} else if (isHex(other)) {
|
|
318
|
+
return this.toHex() === other;
|
|
319
|
+
} else if (other instanceof Enum) {
|
|
320
|
+
return this.index === other.index && this.value.eq(other.value);
|
|
321
|
+
} else if (isObject(other)) {
|
|
322
|
+
return this.value.eq(other[this.type]);
|
|
323
|
+
} // compare the actual wrapper value
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
return this.value.eq(other);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* @description Returns a hex string representation of the value
|
|
330
|
+
*/
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
toHex() {
|
|
334
|
+
return u8aToHex(this.toU8a());
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
338
|
+
*/
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
toHuman(isExtended) {
|
|
342
|
+
return _classPrivateFieldLooseBase(this, _isBasic)[_isBasic] || this.isNone ? this.type : {
|
|
343
|
+
[this.type]: _classPrivateFieldLooseBase(this, _raw)[_raw].toHuman(isExtended)
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
348
|
+
*/
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
toJSON() {
|
|
352
|
+
return _classPrivateFieldLooseBase(this, _isBasic)[_isBasic] ? this.type : {
|
|
353
|
+
[stringCamelCase(this.type)]: _classPrivateFieldLooseBase(this, _raw)[_raw].toJSON()
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* @description Returns the number representation for the value
|
|
358
|
+
*/
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
toNumber() {
|
|
362
|
+
return this.index;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* @description Returns a raw struct representation of the enum types
|
|
366
|
+
*/
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
_toRawStruct() {
|
|
370
|
+
if (_classPrivateFieldLooseBase(this, _isBasic)[_isBasic]) {
|
|
371
|
+
return _classPrivateFieldLooseBase(this, _isIndexed)[_isIndexed] ? this.defKeys.reduce((out, key, index) => {
|
|
372
|
+
out[key] = _classPrivateFieldLooseBase(this, _indexes)[_indexes][index];
|
|
373
|
+
return out;
|
|
374
|
+
}, {}) : this.defKeys;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const typeMap = Object.entries(_classPrivateFieldLooseBase(this, _def2)[_def2]).reduce((out, [key, {
|
|
378
|
+
Type
|
|
379
|
+
}]) => {
|
|
380
|
+
out[key] = Type;
|
|
381
|
+
return out;
|
|
382
|
+
}, {});
|
|
383
|
+
return typesToMap(this.registry, typeMap);
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* @description Returns the base runtime type name for this instance
|
|
387
|
+
*/
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
toRawType() {
|
|
391
|
+
return stringify({
|
|
392
|
+
_enum: this._toRawStruct()
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* @description Returns the string representation of the value
|
|
397
|
+
*/
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
toString() {
|
|
401
|
+
return this.isNull ? this.type : stringify(this.toJSON());
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
405
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
406
|
+
*/
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
toU8a(isBare) {
|
|
410
|
+
return u8aConcat(new Uint8Array(isBare ? [] : [this.index]), _classPrivateFieldLooseBase(this, _raw)[_raw].toU8a(isBare));
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
}
|
package/base/Int.cjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Int = void 0;
|
|
7
|
+
|
|
8
|
+
var _AbstractInt = require("../abstract/AbstractInt.cjs");
|
|
9
|
+
|
|
10
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
11
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @name Int
|
|
15
|
+
* @description
|
|
16
|
+
* A generic signed integer codec. For Substrate all numbers are Little Endian encoded,
|
|
17
|
+
* this handles the encoding and decoding of those numbers. Upon construction
|
|
18
|
+
* the bitLength is provided and any additional use keeps the number to this
|
|
19
|
+
* length. This extends `BN`, so all methods available on a normal `BN` object
|
|
20
|
+
* is available here.
|
|
21
|
+
* @noInheritDoc
|
|
22
|
+
*/
|
|
23
|
+
class Int extends _AbstractInt.AbstractInt {
|
|
24
|
+
constructor(registry) {
|
|
25
|
+
let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
|
26
|
+
let bitLength = arguments.length > 2 ? arguments[2] : undefined;
|
|
27
|
+
super(registry, value, bitLength, true);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static with(bitLength, typeName) {
|
|
31
|
+
return class extends Int {
|
|
32
|
+
constructor(registry, value) {
|
|
33
|
+
super(registry, value, bitLength);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
toRawType() {
|
|
37
|
+
return typeName || super.toRawType();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
exports.Int = Int;
|
package/base/Int.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AnyNumber, CodecClass, CodecRegistry, UIntBitLength } from '../types';
|
|
2
|
+
import { AbstractInt } from '../abstract/AbstractInt';
|
|
3
|
+
/**
|
|
4
|
+
* @name Int
|
|
5
|
+
* @description
|
|
6
|
+
* A generic signed integer codec. For Substrate all numbers are Little Endian encoded,
|
|
7
|
+
* this handles the encoding and decoding of those numbers. Upon construction
|
|
8
|
+
* the bitLength is provided and any additional use keeps the number to this
|
|
9
|
+
* length. This extends `BN`, so all methods available on a normal `BN` object
|
|
10
|
+
* is available here.
|
|
11
|
+
* @noInheritDoc
|
|
12
|
+
*/
|
|
13
|
+
export declare class Int extends AbstractInt {
|
|
14
|
+
constructor(registry: CodecRegistry, value?: AnyNumber, bitLength?: UIntBitLength);
|
|
15
|
+
static with(bitLength: UIntBitLength, typeName?: string): CodecClass<Int>;
|
|
16
|
+
}
|
package/base/Int.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { AbstractInt } from "../abstract/AbstractInt.js";
|
|
4
|
+
/**
|
|
5
|
+
* @name Int
|
|
6
|
+
* @description
|
|
7
|
+
* A generic signed integer codec. For Substrate all numbers are Little Endian encoded,
|
|
8
|
+
* this handles the encoding and decoding of those numbers. Upon construction
|
|
9
|
+
* the bitLength is provided and any additional use keeps the number to this
|
|
10
|
+
* length. This extends `BN`, so all methods available on a normal `BN` object
|
|
11
|
+
* is available here.
|
|
12
|
+
* @noInheritDoc
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export class Int extends AbstractInt {
|
|
16
|
+
constructor(registry, value = 0, bitLength) {
|
|
17
|
+
super(registry, value, bitLength, true);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static with(bitLength, typeName) {
|
|
21
|
+
return class extends Int {
|
|
22
|
+
constructor(registry, value) {
|
|
23
|
+
super(registry, value, bitLength);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
toRawType() {
|
|
27
|
+
return typeName || super.toRawType();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
}
|