@pezkuwi/types-codec 16.5.5 → 16.5.6
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/LICENSE +201 -0
- package/abstract/Array.js +166 -0
- package/abstract/Base.js +101 -0
- package/abstract/Int.js +226 -0
- package/abstract/Object.js +40 -0
- package/{src/abstract/index.ts → abstract/index.js} +0 -3
- package/base/Compact.js +159 -0
- package/base/DoNotConstruct.js +95 -0
- package/base/Enum.js +369 -0
- package/{build/base → base}/Int.d.ts +1 -1
- package/base/Int.js +26 -0
- package/base/Null.js +77 -0
- package/base/Option.js +232 -0
- package/base/Result.js +63 -0
- package/base/Tuple.js +110 -0
- package/{build/base → base}/UInt.d.ts +1 -1
- package/base/UInt.js +23 -0
- package/base/Vec.js +102 -0
- package/base/VecAny.js +17 -0
- package/base/VecFixed.js +67 -0
- package/{src/base/index.ts → base/index.js} +0 -3
- package/{src/bundle.ts → bundle.js} +0 -6
- package/cjs/abstract/Array.d.ts +89 -0
- package/cjs/abstract/Array.js +170 -0
- package/cjs/abstract/Base.d.ts +75 -0
- package/cjs/abstract/Base.js +105 -0
- package/cjs/abstract/Int.d.ts +80 -0
- package/cjs/abstract/Int.js +230 -0
- package/cjs/abstract/Object.d.ts +67 -0
- package/cjs/abstract/Object.js +44 -0
- package/cjs/abstract/index.d.ts +3 -0
- package/cjs/abstract/index.js +9 -0
- package/cjs/base/Compact.d.ts +88 -0
- package/cjs/base/Compact.js +163 -0
- package/cjs/base/DoNotConstruct.d.ts +63 -0
- package/cjs/base/DoNotConstruct.js +99 -0
- package/cjs/base/Enum.d.ts +118 -0
- package/cjs/base/Enum.js +373 -0
- package/cjs/base/Int.d.ts +16 -0
- package/cjs/base/Int.js +30 -0
- package/cjs/base/Null.d.ts +56 -0
- package/cjs/base/Null.js +81 -0
- package/cjs/base/Option.d.ts +94 -0
- package/cjs/base/Option.js +236 -0
- package/cjs/base/Result.d.ts +38 -0
- package/cjs/base/Result.js +67 -0
- package/cjs/base/Tuple.d.ts +42 -0
- package/cjs/base/Tuple.js +114 -0
- package/cjs/base/UInt.d.ts +15 -0
- package/cjs/base/UInt.js +27 -0
- package/cjs/base/Vec.d.ts +28 -0
- package/cjs/base/Vec.js +107 -0
- package/cjs/base/VecAny.d.ts +15 -0
- package/cjs/base/VecAny.js +21 -0
- package/cjs/base/VecFixed.d.ts +30 -0
- package/cjs/base/VecFixed.js +71 -0
- package/cjs/base/index.d.ts +12 -0
- package/cjs/base/index.js +27 -0
- package/cjs/bundle.d.ts +7 -0
- package/cjs/bundle.js +12 -0
- package/cjs/extended/BTreeMap.js +14 -0
- package/cjs/extended/BTreeSet.js +189 -0
- package/cjs/extended/BitVec.js +112 -0
- package/cjs/extended/Bytes.js +74 -0
- package/cjs/extended/HashMap.js +14 -0
- package/cjs/extended/Linkage.js +70 -0
- package/cjs/extended/Map.js +204 -0
- package/cjs/extended/OptionBool.js +76 -0
- package/cjs/extended/Range.js +42 -0
- package/cjs/extended/RangeInclusive.js +17 -0
- package/cjs/extended/Type.js +25 -0
- package/cjs/extended/U8aFixed.js +46 -0
- package/cjs/extended/WrapperKeepOpaque.js +103 -0
- package/cjs/extended/WrapperOpaque.js +23 -0
- package/cjs/extended/index.js +32 -0
- package/cjs/index.js +5 -0
- package/cjs/native/Bool.js +113 -0
- package/cjs/native/Date.js +144 -0
- package/cjs/native/Float.js +111 -0
- package/cjs/native/Json.js +121 -0
- package/cjs/native/Raw.js +144 -0
- package/cjs/native/Set.js +219 -0
- package/cjs/native/Struct.data.js +4 -0
- package/cjs/native/Struct.js +264 -0
- package/cjs/native/Text.js +154 -0
- package/cjs/native/index.js +22 -0
- package/cjs/package.json +3 -0
- package/cjs/packageDetect.js +5 -0
- package/cjs/packageInfo.js +4 -0
- package/cjs/primitive/F32.js +14 -0
- package/cjs/primitive/F64.js +14 -0
- package/cjs/primitive/I128.js +14 -0
- package/cjs/primitive/I16.js +14 -0
- package/cjs/primitive/I256.js +14 -0
- package/cjs/primitive/I32.js +14 -0
- package/cjs/primitive/I64.js +14 -0
- package/cjs/primitive/I8.js +14 -0
- package/cjs/primitive/ISize.js +18 -0
- package/cjs/primitive/U128.js +14 -0
- package/cjs/primitive/U16.js +14 -0
- package/cjs/primitive/U256.js +14 -0
- package/cjs/primitive/U32.js +14 -0
- package/cjs/primitive/U64.js +14 -0
- package/cjs/primitive/U8.js +14 -0
- package/cjs/primitive/USize.js +18 -0
- package/cjs/primitive/index.js +51 -0
- package/{build → cjs}/types/codec.d.ts +1 -1
- package/cjs/types/codec.js +2 -0
- package/cjs/types/helpers.js +2 -0
- package/cjs/types/index.js +7 -0
- package/cjs/types/interfaces.js +2 -0
- package/cjs/types/registry.js +2 -0
- package/cjs/utils/compareArray.js +13 -0
- package/cjs/utils/compareMap.js +30 -0
- package/cjs/utils/compareSet.js +20 -0
- package/cjs/utils/decodeU8a.js +105 -0
- package/cjs/utils/index.js +25 -0
- package/cjs/utils/sanitize.js +233 -0
- package/cjs/utils/sortValues.js +84 -0
- package/cjs/utils/toConstructors.js +40 -0
- package/cjs/utils/typesToMap.js +10 -0
- package/cjs/utils/util.js +7 -0
- package/extended/BTreeMap.d.ts +5 -0
- package/extended/BTreeMap.js +10 -0
- package/extended/BTreeSet.d.ts +64 -0
- package/extended/BTreeSet.js +185 -0
- package/extended/BitVec.d.ts +37 -0
- package/extended/BitVec.js +108 -0
- package/extended/Bytes.d.ts +29 -0
- package/extended/Bytes.js +70 -0
- package/extended/HashMap.d.ts +5 -0
- package/extended/HashMap.js +10 -0
- package/extended/Linkage.d.ts +37 -0
- package/extended/Linkage.js +65 -0
- package/extended/Map.d.ts +59 -0
- package/extended/Map.js +200 -0
- package/extended/OptionBool.d.ts +36 -0
- package/extended/OptionBool.js +72 -0
- package/extended/Range.d.ts +29 -0
- package/extended/Range.js +38 -0
- package/extended/RangeInclusive.d.ts +6 -0
- package/extended/RangeInclusive.js +13 -0
- package/extended/Type.d.ts +16 -0
- package/extended/Type.js +21 -0
- package/extended/U8aFixed.d.ts +16 -0
- package/extended/U8aFixed.js +42 -0
- package/extended/WrapperKeepOpaque.d.ts +40 -0
- package/extended/WrapperKeepOpaque.js +99 -0
- package/extended/WrapperOpaque.d.ts +10 -0
- package/extended/WrapperOpaque.js +19 -0
- package/{src/extended/index.ts → extended/index.d.ts} +0 -3
- package/extended/index.js +14 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/native/Bool.d.ts +71 -0
- package/native/Bool.js +109 -0
- package/native/Date.d.ts +84 -0
- package/native/Date.js +140 -0
- package/native/Float.d.ts +68 -0
- package/native/Float.js +107 -0
- package/native/Json.d.ts +69 -0
- package/native/Json.js +117 -0
- package/native/Raw.d.ts +87 -0
- package/native/Raw.js +140 -0
- package/native/Set.d.ts +84 -0
- package/native/Set.js +215 -0
- package/native/Struct.d.ts +106 -0
- package/{src/native/Struct.data.ts → native/Struct.data.js} +0 -3
- package/native/Struct.js +260 -0
- package/native/Text.d.ts +77 -0
- package/native/Text.js +150 -0
- package/{src/native/index.ts → native/index.d.ts} +0 -3
- package/native/index.js +8 -0
- package/package.json +1126 -11
- package/packageDetect.d.ts +1 -0
- package/packageDetect.js +3 -0
- package/packageInfo.d.ts +6 -0
- package/packageInfo.js +1 -0
- package/primitive/F32.d.ts +11 -0
- package/primitive/F32.js +10 -0
- package/primitive/F64.d.ts +11 -0
- package/primitive/F64.js +10 -0
- package/primitive/I128.d.ts +11 -0
- package/primitive/I128.js +10 -0
- package/primitive/I16.d.ts +11 -0
- package/primitive/I16.js +10 -0
- package/primitive/I256.d.ts +11 -0
- package/primitive/I256.js +10 -0
- package/primitive/I32.d.ts +11 -0
- package/primitive/I32.js +10 -0
- package/primitive/I64.d.ts +11 -0
- package/primitive/I64.js +10 -0
- package/primitive/I8.d.ts +11 -0
- package/primitive/I8.js +10 -0
- package/primitive/ISize.d.ts +12 -0
- package/primitive/ISize.js +14 -0
- package/primitive/U128.d.ts +11 -0
- package/primitive/U128.js +10 -0
- package/primitive/U16.d.ts +11 -0
- package/primitive/U16.js +10 -0
- package/primitive/U256.d.ts +11 -0
- package/primitive/U256.js +10 -0
- package/primitive/U32.d.ts +11 -0
- package/primitive/U32.js +10 -0
- package/primitive/U64.d.ts +11 -0
- package/primitive/U64.js +10 -0
- package/primitive/U8.d.ts +11 -0
- package/primitive/U8.js +10 -0
- package/primitive/USize.d.ts +12 -0
- package/primitive/USize.js +14 -0
- package/{src/primitive/index.ts → primitive/index.d.ts} +0 -3
- package/primitive/index.js +16 -0
- package/types/codec.d.ts +113 -0
- package/types/codec.js +1 -0
- package/{src/types/helpers.ts → types/helpers.d.ts} +8 -31
- package/types/helpers.js +1 -0
- package/{src/types/index.ts → types/index.d.ts} +0 -3
- package/types/index.js +4 -0
- package/types/interfaces.d.ts +74 -0
- package/types/interfaces.js +1 -0
- package/types/registry.d.ts +67 -0
- package/types/registry.js +1 -0
- package/utils/compareArray.d.ts +1 -0
- package/utils/compareArray.js +10 -0
- package/utils/compareMap.d.ts +1 -0
- package/utils/compareMap.js +27 -0
- package/utils/compareSet.d.ts +1 -0
- package/utils/compareSet.js +17 -0
- package/utils/decodeU8a.d.ts +26 -0
- package/utils/decodeU8a.js +100 -0
- package/{src/utils/index.ts → utils/index.d.ts} +0 -3
- package/utils/index.js +8 -0
- package/utils/sanitize.d.ts +15 -0
- package/utils/sanitize.js +219 -0
- package/utils/sortValues.d.ts +12 -0
- package/utils/sortValues.js +79 -0
- package/utils/toConstructors.d.ts +16 -0
- package/utils/toConstructors.js +35 -0
- package/utils/typesToMap.d.ts +2 -0
- package/utils/typesToMap.js +7 -0
- package/utils/util.d.ts +3 -0
- package/utils/util.js +4 -0
- package/src/abstract/Array.ts +0 -213
- package/src/abstract/Base.ts +0 -129
- package/src/abstract/Int.ts +0 -271
- package/src/abstract/Object.ts +0 -99
- package/src/base/Compact.spec.ts +0 -99
- package/src/base/Compact.ts +0 -198
- package/src/base/DoNotConstruct.spec.ts +0 -23
- package/src/base/DoNotConstruct.ts +0 -118
- package/src/base/Enum.spec.ts +0 -487
- package/src/base/Enum.ts +0 -460
- package/src/base/Int.spec.ts +0 -225
- package/src/base/Int.ts +0 -34
- package/src/base/Null.spec.ts +0 -41
- package/src/base/Null.ts +0 -96
- package/src/base/Option.spec.ts +0 -216
- package/src/base/Option.ts +0 -275
- package/src/base/Result.spec.ts +0 -64
- package/src/base/Result.ts +0 -79
- package/src/base/Tuple.spec.ts +0 -161
- package/src/base/Tuple.ts +0 -149
- package/src/base/UInt.spec.ts +0 -192
- package/src/base/UInt.ts +0 -30
- package/src/base/Vec.spec.ts +0 -224
- package/src/base/Vec.ts +0 -133
- package/src/base/VecAny.ts +0 -23
- package/src/base/VecFixed.spec.ts +0 -78
- package/src/base/VecFixed.ts +0 -92
- package/src/checkTypes.manual.ts +0 -12
- package/src/extended/BTreeMap.spec.ts +0 -245
- package/src/extended/BTreeMap.ts +0 -16
- package/src/extended/BTreeSet.spec.ts +0 -260
- package/src/extended/BTreeSet.ts +0 -233
- package/src/extended/BitVec.spec.ts +0 -97
- package/src/extended/BitVec.ts +0 -137
- package/src/extended/Bytes.spec.ts +0 -75
- package/src/extended/Bytes.ts +0 -88
- package/src/extended/HashMap.spec.ts +0 -36
- package/src/extended/HashMap.ts +0 -16
- package/src/extended/Linkage.spec.ts +0 -43
- package/src/extended/Linkage.ts +0 -81
- package/src/extended/Map.spec.ts +0 -123
- package/src/extended/Map.ts +0 -255
- package/src/extended/OptionBool.spec.ts +0 -49
- package/src/extended/OptionBool.ts +0 -93
- package/src/extended/Range.spec.ts +0 -37
- package/src/extended/Range.ts +0 -56
- package/src/extended/RangeInclusive.ts +0 -20
- package/src/extended/Type.spec.ts +0 -118
- package/src/extended/Type.ts +0 -29
- package/src/extended/U8aFixed.spec.ts +0 -117
- package/src/extended/U8aFixed.ts +0 -57
- package/src/extended/WrapperKeepOpaque.spec.ts +0 -101
- package/src/extended/WrapperKeepOpaque.ts +0 -128
- package/src/extended/WrapperOpaque.spec.ts +0 -58
- package/src/extended/WrapperOpaque.ts +0 -27
- package/src/index.ts +0 -6
- package/src/mod.ts +0 -4
- package/src/native/Bool.spec.ts +0 -74
- package/src/native/Bool.ts +0 -137
- package/src/native/Date.spec.ts +0 -85
- package/src/native/Date.ts +0 -169
- package/src/native/Float.spec.ts +0 -51
- package/src/native/Float.ts +0 -136
- package/src/native/Json.ts +0 -147
- package/src/native/Raw.spec.ts +0 -113
- package/src/native/Raw.ts +0 -171
- package/src/native/Set.spec.ts +0 -116
- package/src/native/Set.ts +0 -269
- package/src/native/Struct.spec.ts +0 -411
- package/src/native/Struct.ts +0 -338
- package/src/native/Text.spec.ts +0 -85
- package/src/native/Text.ts +0 -184
- package/src/packageDetect.ts +0 -11
- package/src/packageInfo.ts +0 -6
- package/src/primitive/F32.ts +0 -14
- package/src/primitive/F64.ts +0 -14
- package/src/primitive/I128.ts +0 -14
- package/src/primitive/I16.ts +0 -14
- package/src/primitive/I256.ts +0 -14
- package/src/primitive/I32.ts +0 -14
- package/src/primitive/I64.ts +0 -14
- package/src/primitive/I8.ts +0 -14
- package/src/primitive/ISize.ts +0 -21
- package/src/primitive/U128.ts +0 -14
- package/src/primitive/U16.ts +0 -14
- package/src/primitive/U256.ts +0 -14
- package/src/primitive/U32.ts +0 -14
- package/src/primitive/U64.ts +0 -14
- package/src/primitive/U8.ts +0 -14
- package/src/primitive/USize.ts +0 -21
- package/src/test/performance.ts +0 -61
- package/src/types/codec.ts +0 -140
- package/src/types/interfaces.ts +0 -98
- package/src/types/registry.ts +0 -86
- package/src/utils/compareArray.ts +0 -22
- package/src/utils/compareMap.ts +0 -40
- package/src/utils/compareSet.ts +0 -23
- package/src/utils/decodeU8a.ts +0 -123
- package/src/utils/sanitize.spec.ts +0 -89
- package/src/utils/sanitize.ts +0 -290
- package/src/utils/sortValues.ts +0 -103
- package/src/utils/toConstructors.ts +0 -46
- package/src/utils/typesToMap.ts +0 -14
- package/src/utils/util.ts +0 -8
- package/tsconfig.build.json +0 -16
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.spec.json +0 -21
- /package/{build/abstract → abstract}/Array.d.ts +0 -0
- /package/{build/abstract → abstract}/Base.d.ts +0 -0
- /package/{build/abstract → abstract}/Int.d.ts +0 -0
- /package/{build/abstract → abstract}/Object.d.ts +0 -0
- /package/{build/abstract → abstract}/index.d.ts +0 -0
- /package/{build/base → base}/Compact.d.ts +0 -0
- /package/{build/base → base}/DoNotConstruct.d.ts +0 -0
- /package/{build/base → base}/Enum.d.ts +0 -0
- /package/{build/base → base}/Null.d.ts +0 -0
- /package/{build/base → base}/Option.d.ts +0 -0
- /package/{build/base → base}/Result.d.ts +0 -0
- /package/{build/base → base}/Tuple.d.ts +0 -0
- /package/{build/base → base}/Vec.d.ts +0 -0
- /package/{build/base → base}/VecAny.d.ts +0 -0
- /package/{build/base → base}/VecFixed.d.ts +0 -0
- /package/{build/base → base}/index.d.ts +0 -0
- /package/{build/bundle.d.ts → bundle.d.ts} +0 -0
- /package/{build → cjs}/extended/BTreeMap.d.ts +0 -0
- /package/{build → cjs}/extended/BTreeSet.d.ts +0 -0
- /package/{build → cjs}/extended/BitVec.d.ts +0 -0
- /package/{build → cjs}/extended/Bytes.d.ts +0 -0
- /package/{build → cjs}/extended/HashMap.d.ts +0 -0
- /package/{build → cjs}/extended/Linkage.d.ts +0 -0
- /package/{build → cjs}/extended/Map.d.ts +0 -0
- /package/{build → cjs}/extended/OptionBool.d.ts +0 -0
- /package/{build → cjs}/extended/Range.d.ts +0 -0
- /package/{build → cjs}/extended/RangeInclusive.d.ts +0 -0
- /package/{build → cjs}/extended/Type.d.ts +0 -0
- /package/{build → cjs}/extended/U8aFixed.d.ts +0 -0
- /package/{build → cjs}/extended/WrapperKeepOpaque.d.ts +0 -0
- /package/{build → cjs}/extended/WrapperOpaque.d.ts +0 -0
- /package/{build → cjs}/extended/index.d.ts +0 -0
- /package/{build → cjs}/index.d.ts +0 -0
- /package/{build → cjs}/native/Bool.d.ts +0 -0
- /package/{build → cjs}/native/Date.d.ts +0 -0
- /package/{build → cjs}/native/Float.d.ts +0 -0
- /package/{build → cjs}/native/Json.d.ts +0 -0
- /package/{build → cjs}/native/Raw.d.ts +0 -0
- /package/{build → cjs}/native/Set.d.ts +0 -0
- /package/{build → cjs}/native/Struct.d.ts +0 -0
- /package/{build → cjs}/native/Text.d.ts +0 -0
- /package/{build → cjs}/native/index.d.ts +0 -0
- /package/{build → cjs}/packageDetect.d.ts +0 -0
- /package/{build → cjs}/packageInfo.d.ts +0 -0
- /package/{build → cjs}/primitive/F32.d.ts +0 -0
- /package/{build → cjs}/primitive/F64.d.ts +0 -0
- /package/{build → cjs}/primitive/I128.d.ts +0 -0
- /package/{build → cjs}/primitive/I16.d.ts +0 -0
- /package/{build → cjs}/primitive/I256.d.ts +0 -0
- /package/{build → cjs}/primitive/I32.d.ts +0 -0
- /package/{build → cjs}/primitive/I64.d.ts +0 -0
- /package/{build → cjs}/primitive/I8.d.ts +0 -0
- /package/{build → cjs}/primitive/ISize.d.ts +0 -0
- /package/{build → cjs}/primitive/U128.d.ts +0 -0
- /package/{build → cjs}/primitive/U16.d.ts +0 -0
- /package/{build → cjs}/primitive/U256.d.ts +0 -0
- /package/{build → cjs}/primitive/U32.d.ts +0 -0
- /package/{build → cjs}/primitive/U64.d.ts +0 -0
- /package/{build → cjs}/primitive/U8.d.ts +0 -0
- /package/{build → cjs}/primitive/USize.d.ts +0 -0
- /package/{build → cjs}/primitive/index.d.ts +0 -0
- /package/{build → cjs}/types/helpers.d.ts +0 -0
- /package/{build → cjs}/types/index.d.ts +0 -0
- /package/{build → cjs}/types/interfaces.d.ts +0 -0
- /package/{build → cjs}/types/registry.d.ts +0 -0
- /package/{build → cjs}/utils/compareArray.d.ts +0 -0
- /package/{build → cjs}/utils/compareMap.d.ts +0 -0
- /package/{build → cjs}/utils/compareSet.d.ts +0 -0
- /package/{build → cjs}/utils/decodeU8a.d.ts +0 -0
- /package/{build → cjs}/utils/index.d.ts +0 -0
- /package/{build → cjs}/utils/sanitize.d.ts +0 -0
- /package/{build → cjs}/utils/sortValues.d.ts +0 -0
- /package/{build → cjs}/utils/toConstructors.d.ts +0 -0
- /package/{build → cjs}/utils/typesToMap.d.ts +0 -0
- /package/{build → cjs}/utils/util.d.ts +0 -0
package/src/native/Set.ts
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
// Copyright 2017-2025 @polkadot/types-codec authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import type { HexString } from '@pezkuwi/util/types';
|
|
5
|
-
import type { CodecClass, Inspect, ISet, IU8a, Registry } from '../types/index.js';
|
|
6
|
-
|
|
7
|
-
import { BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@pezkuwi/util';
|
|
8
|
-
|
|
9
|
-
import { compareArray } from '../utils/index.js';
|
|
10
|
-
|
|
11
|
-
type SetValues = Record<string, number | BN>;
|
|
12
|
-
|
|
13
|
-
function encodeSet (setValues: SetValues, values: string[]): BN {
|
|
14
|
-
const encoded = new BN(0);
|
|
15
|
-
|
|
16
|
-
for (let i = 0, count = values.length; i < count; i++) {
|
|
17
|
-
encoded.ior(bnToBn(setValues[values[i]] || 0));
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return encoded;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** @internal */
|
|
24
|
-
function decodeSetArray (setValues: SetValues, values: string[]): string[] {
|
|
25
|
-
const count = values.length;
|
|
26
|
-
const result = new Array<string>(count);
|
|
27
|
-
|
|
28
|
-
for (let i = 0; i < count; i++) {
|
|
29
|
-
const key = values[i];
|
|
30
|
-
|
|
31
|
-
if (isUndefined(setValues[key])) {
|
|
32
|
-
throw new Error(`Set: Invalid key '${key}' passed to Set, allowed ${Object.keys(setValues).join(', ')}`);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
result[i] = key;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** @internal */
|
|
42
|
-
function decodeSetNumber (setValues: SetValues, _value: BN | number): string[] {
|
|
43
|
-
const bn = bnToBn(_value);
|
|
44
|
-
const keys = Object.keys(setValues);
|
|
45
|
-
const result: string[] = [];
|
|
46
|
-
|
|
47
|
-
for (let i = 0, count = keys.length; i < count; i++) {
|
|
48
|
-
const key = keys[i];
|
|
49
|
-
|
|
50
|
-
if (bn.and(bnToBn(setValues[key])).eq(bnToBn(setValues[key]))) {
|
|
51
|
-
result.push(key);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const computed = encodeSet(setValues, result);
|
|
56
|
-
|
|
57
|
-
if (!bn.eq(computed)) {
|
|
58
|
-
throw new Error(`Set: Mismatch decoding '${bn.toString()}', computed as '${computed.toString()}' with ${result.join(', ')}`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** @internal */
|
|
65
|
-
function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8Array | BN | number | string = 0, bitLength: number): string[] {
|
|
66
|
-
if (bitLength % 8 !== 0) {
|
|
67
|
-
throw new Error(`Expected valid bitLength, power of 8, found ${bitLength}`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const byteLength = bitLength / 8;
|
|
71
|
-
|
|
72
|
-
if (isU8a(value)) {
|
|
73
|
-
return value.length === 0
|
|
74
|
-
? []
|
|
75
|
-
: decodeSetNumber(setValues, u8aToBn(value.subarray(0, byteLength), { isLe: true }));
|
|
76
|
-
} else if (isString(value)) {
|
|
77
|
-
return decodeSet(setValues, u8aToU8a(value), byteLength);
|
|
78
|
-
} else if (value instanceof Set || Array.isArray(value)) {
|
|
79
|
-
const input = Array.isArray(value)
|
|
80
|
-
? value
|
|
81
|
-
: [...value.values()];
|
|
82
|
-
|
|
83
|
-
return decodeSetArray(setValues, input);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return decodeSetNumber(setValues, value);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @name Set
|
|
91
|
-
* @description
|
|
92
|
-
* An Set is an array of string values, represented an an encoded type by
|
|
93
|
-
* a bitwise representation of the values.
|
|
94
|
-
*/
|
|
95
|
-
export class CodecSet extends Set<string> implements ISet<string> {
|
|
96
|
-
readonly registry: Registry;
|
|
97
|
-
|
|
98
|
-
public createdAtHash?: IU8a;
|
|
99
|
-
public initialU8aLength?: number;
|
|
100
|
-
public isStorageFallback?: boolean;
|
|
101
|
-
|
|
102
|
-
readonly #allowed: SetValues;
|
|
103
|
-
readonly #byteLength: number;
|
|
104
|
-
|
|
105
|
-
constructor (registry: Registry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength = 8) {
|
|
106
|
-
super(decodeSet(setValues, value, bitLength));
|
|
107
|
-
|
|
108
|
-
this.registry = registry;
|
|
109
|
-
this.#allowed = setValues;
|
|
110
|
-
this.#byteLength = bitLength / 8;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
public static with (values: SetValues, bitLength?: number): CodecClass<CodecSet> {
|
|
114
|
-
return class extends CodecSet {
|
|
115
|
-
static {
|
|
116
|
-
const keys = Object.keys(values);
|
|
117
|
-
const count = keys.length;
|
|
118
|
-
const isKeys = new Array<string>(count);
|
|
119
|
-
|
|
120
|
-
for (let i = 0; i < count; i++) {
|
|
121
|
-
isKeys[i] = `is${stringPascalCase(keys[i])}`;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
objectProperties(this.prototype, isKeys, (_: string, i: number, self: CodecSet) =>
|
|
125
|
-
self.strings.includes(keys[i])
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
constructor (registry: Registry, value?: string[] | Set<string> | Uint8Array | BN | number | string) {
|
|
130
|
-
super(registry, values, value, bitLength);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @description The length of the value when encoded as a Uint8Array
|
|
137
|
-
*/
|
|
138
|
-
public get encodedLength (): number {
|
|
139
|
-
return this.#byteLength;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* @description returns a hash of the contents
|
|
144
|
-
*/
|
|
145
|
-
public get hash (): IU8a {
|
|
146
|
-
return this.registry.hash(this.toU8a());
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* @description true is the Set contains no values
|
|
151
|
-
*/
|
|
152
|
-
public get isEmpty (): boolean {
|
|
153
|
-
return this.size === 0;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* @description The actual set values as a string[]
|
|
158
|
-
*/
|
|
159
|
-
public get strings (): string[] {
|
|
160
|
-
return [...super.values()];
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* @description The encoded value for the set members
|
|
165
|
-
*/
|
|
166
|
-
public get valueEncoded (): BN {
|
|
167
|
-
return encodeSet(this.#allowed, this.strings);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* @description adds a value to the Set (extended to allow for validity checking)
|
|
172
|
-
*/
|
|
173
|
-
public override add = (key: string): this => {
|
|
174
|
-
// ^^^ add = () property done to assign this instance's this, otherwise Set.add creates "some" chaos
|
|
175
|
-
// we have the isUndefined(this._setValues) in here as well, add is used internally
|
|
176
|
-
// in the Set constructor (so it is undefined at this point, and should allow)
|
|
177
|
-
if (this.#allowed && isUndefined(this.#allowed[key])) {
|
|
178
|
-
throw new Error(`Set: Invalid key '${key}' on add`);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
super.add(key);
|
|
182
|
-
|
|
183
|
-
return this;
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* @description Compares the value of the input to see if there is a match
|
|
188
|
-
*/
|
|
189
|
-
public eq (other?: unknown): boolean {
|
|
190
|
-
if (Array.isArray(other)) {
|
|
191
|
-
// we don't actually care about the order, sort the values
|
|
192
|
-
return compareArray(this.strings.sort(), other.sort());
|
|
193
|
-
} else if (other instanceof Set) {
|
|
194
|
-
return this.eq([...other.values()]);
|
|
195
|
-
} else if (isNumber(other) || isBn(other as string)) {
|
|
196
|
-
return this.valueEncoded.eq(bnToBn(other as string));
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* @description Returns a breakdown of the hex encoding for this Codec
|
|
204
|
-
*/
|
|
205
|
-
public inspect (): Inspect {
|
|
206
|
-
return {
|
|
207
|
-
outer: [this.toU8a()]
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* @description Returns a hex string representation of the value
|
|
213
|
-
*/
|
|
214
|
-
public toHex (): HexString {
|
|
215
|
-
return u8aToHex(this.toU8a());
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
220
|
-
*/
|
|
221
|
-
public toHuman (): string[] {
|
|
222
|
-
return this.toJSON();
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
227
|
-
*/
|
|
228
|
-
public toJSON (): string[] {
|
|
229
|
-
return this.strings;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* @description The encoded value for the set members
|
|
234
|
-
*/
|
|
235
|
-
public toNumber (): number {
|
|
236
|
-
return this.valueEncoded.toNumber();
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* @description Converts the value in a best-fit primitive form
|
|
241
|
-
*/
|
|
242
|
-
public toPrimitive (): string[] {
|
|
243
|
-
return this.toJSON();
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* @description Returns the base runtime type name for this instance
|
|
248
|
-
*/
|
|
249
|
-
public toRawType (): string {
|
|
250
|
-
return stringify({ _set: this.#allowed });
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* @description Returns the string representation of the value
|
|
255
|
-
*/
|
|
256
|
-
public override toString (): string {
|
|
257
|
-
return `[${this.strings.join(', ')}]`;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
262
|
-
*/
|
|
263
|
-
public toU8a (_isBare?: boolean): Uint8Array {
|
|
264
|
-
return bnToU8a(this.valueEncoded, {
|
|
265
|
-
bitLength: this.#byteLength * 8,
|
|
266
|
-
isLe: true
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
}
|
|
@@ -1,411 +0,0 @@
|
|
|
1
|
-
// Copyright 2017-2025 @polkadot/types-codec authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
|
|
5
|
-
|
|
6
|
-
/* eslint-disable sort-keys */
|
|
7
|
-
|
|
8
|
-
import type { Bool, Option } from '@pezkuwi/types-codec';
|
|
9
|
-
import type { CodecTo } from '@pezkuwi/types-codec/types';
|
|
10
|
-
|
|
11
|
-
import { TypeRegistry } from '@pezkuwi/types';
|
|
12
|
-
import { Enum, Struct, Text, U32, Vec } from '@pezkuwi/types-codec';
|
|
13
|
-
import { stringify } from '@pezkuwi/util';
|
|
14
|
-
|
|
15
|
-
import { TEST_A } from './Struct.data.js';
|
|
16
|
-
|
|
17
|
-
describe('Struct', (): void => {
|
|
18
|
-
const registry = new TypeRegistry();
|
|
19
|
-
|
|
20
|
-
describe('decoding', (): void => {
|
|
21
|
-
const testDecode = (type: string, input: any): void =>
|
|
22
|
-
it(`can decode from ${type}`, (): void => {
|
|
23
|
-
const s = new Struct(registry, {
|
|
24
|
-
foo: Text,
|
|
25
|
-
bar: U32
|
|
26
|
-
}, input);
|
|
27
|
-
|
|
28
|
-
expect([...s.keys()]).toEqual(['foo', 'bar']);
|
|
29
|
-
expect(
|
|
30
|
-
[...s.values()].map((v): string =>
|
|
31
|
-
v.toString()
|
|
32
|
-
)
|
|
33
|
-
).toEqual(['bazzing', '69']);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
testDecode('array', ['bazzing', 69]);
|
|
37
|
-
testDecode('hex', '0x1c62617a7a696e6745000000');
|
|
38
|
-
testDecode('object', { foo: 'bazzing', bar: 69 });
|
|
39
|
-
testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
|
|
40
|
-
testDecode('Struct.with', new (Struct.with({ foo: 'Text', bar: 'U32' }))(registry, { foo: 'bazzing', bar: 69 }));
|
|
41
|
-
|
|
42
|
-
it('decodes null/undefined/empty correctly (& equivalent)', (): void => {
|
|
43
|
-
const Clazz = Struct.with({
|
|
44
|
-
txt: Text,
|
|
45
|
-
u32: U32
|
|
46
|
-
});
|
|
47
|
-
const expected = { txt: '', u32: '0' };
|
|
48
|
-
|
|
49
|
-
expect(new Clazz(registry, {}).toHuman()).toEqual(expected);
|
|
50
|
-
expect(new Clazz(registry, null).toHuman()).toEqual(expected);
|
|
51
|
-
expect(new Clazz(registry, undefined).toHuman()).toEqual(expected);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('decodes with Optionals', (): void => {
|
|
55
|
-
const Clazz = Struct.with({
|
|
56
|
-
a: 'Option<Bool>',
|
|
57
|
-
b: 'Option<Bool>'
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const c = new Clazz(registry, { a: false }) as unknown as { a: Option<Bool>, b: Option<Bool> };
|
|
61
|
-
|
|
62
|
-
expect(c.a.isSome).toEqual(true);
|
|
63
|
-
expect(c.a.unwrap().isTrue).toEqual(false);
|
|
64
|
-
expect(c.b.isSome).toEqual(false);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('decodes reusing instantiated inputs', (): void => {
|
|
68
|
-
const foo = new Text(registry, 'bar');
|
|
69
|
-
|
|
70
|
-
expect(
|
|
71
|
-
(new Struct(
|
|
72
|
-
registry,
|
|
73
|
-
{ foo: Text },
|
|
74
|
-
{ foo }
|
|
75
|
-
)).get('foo')
|
|
76
|
-
).toBe(foo);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
it('decodes a more complicated type', (): void => {
|
|
80
|
-
const s = new Struct(registry, {
|
|
81
|
-
foo: Vec.with(Struct.with({
|
|
82
|
-
bar: Text
|
|
83
|
-
}))
|
|
84
|
-
}, { foo: [{ bar: 1 }, { bar: 2 }] });
|
|
85
|
-
|
|
86
|
-
expect(s.toString()).toBe('{"foo":[{"bar":"1"},{"bar":"2"}]}');
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('decodes a previously problematic input', (): void => {
|
|
90
|
-
let data;
|
|
91
|
-
|
|
92
|
-
try {
|
|
93
|
-
data = new Struct(registry, {
|
|
94
|
-
a: 'u32',
|
|
95
|
-
b: 'H256',
|
|
96
|
-
c: 'H256',
|
|
97
|
-
swap: Enum.with({
|
|
98
|
-
A: 'u256',
|
|
99
|
-
B: 'u256'
|
|
100
|
-
}),
|
|
101
|
-
d: Vec.with('u8'),
|
|
102
|
-
e: 'u8'
|
|
103
|
-
}, TEST_A);
|
|
104
|
-
} catch (error) {
|
|
105
|
-
console.error(error);
|
|
106
|
-
|
|
107
|
-
throw error;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
expect(data.get('d')).toHaveLength(50000);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('decodes from a Map input', (): void => {
|
|
114
|
-
const s = new Struct(registry, {
|
|
115
|
-
txt: Text,
|
|
116
|
-
foo: U32,
|
|
117
|
-
bar: U32
|
|
118
|
-
}, new Map<string, unknown>([['a', 42], ['txt', 'fubar']]));
|
|
119
|
-
|
|
120
|
-
expect(s.toString()).toEqual('{"txt":"fubar","foo":0,"bar":0}');
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('decodes from a snake_case input', (): void => {
|
|
124
|
-
const input = new Struct(registry, {
|
|
125
|
-
snakeCaseA: U32,
|
|
126
|
-
snakeCaseB: Text,
|
|
127
|
-
other: U32
|
|
128
|
-
}, { snake_case_a: 42, snake_case_b: 'fubar', other: 69 } as any);
|
|
129
|
-
|
|
130
|
-
expect(input.toString()).toEqual('{"snakeCaseA":42,"snakeCaseB":"fubar","other":69}');
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('throws when it cannot decode', (): void => {
|
|
134
|
-
expect(
|
|
135
|
-
(): Struct<any> => new (
|
|
136
|
-
Struct.with({
|
|
137
|
-
txt: Text,
|
|
138
|
-
u32: U32
|
|
139
|
-
})
|
|
140
|
-
)(registry, 'ABC')
|
|
141
|
-
).toThrow(/Cannot decode value/);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('throws a sensical error on incorrect array values passed to structs', (): void => {
|
|
145
|
-
expect(
|
|
146
|
-
() => new Struct(registry, {
|
|
147
|
-
_: 'Vec<u32>'
|
|
148
|
-
}, [123, 456])
|
|
149
|
-
).toThrow(/array to object with known keys/);
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
describe('encoding', (): void => {
|
|
154
|
-
const testEncode = (to: CodecTo, expected: any): void =>
|
|
155
|
-
it(`can encode ${to}`, (): void => {
|
|
156
|
-
const s = new Struct(registry, {
|
|
157
|
-
foo: Text,
|
|
158
|
-
bar: U32
|
|
159
|
-
}, { foo: 'bazzing', bar: 69 });
|
|
160
|
-
|
|
161
|
-
expect(s[to]()).toEqual(expected);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
testEncode('toHex', '0x1c62617a7a696e6745000000');
|
|
165
|
-
testEncode('toJSON', { foo: 'bazzing', bar: 69 });
|
|
166
|
-
testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
|
|
167
|
-
testEncode('toString', '{"foo":"bazzing","bar":69}');
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('provides a clean toString()', (): void => {
|
|
171
|
-
expect(
|
|
172
|
-
new (
|
|
173
|
-
Struct.with({
|
|
174
|
-
txt: Text,
|
|
175
|
-
u32: U32
|
|
176
|
-
})
|
|
177
|
-
)(registry, { txt: 'foo', u32: 0x123456 }).toString()
|
|
178
|
-
).toEqual('{"txt":"foo","u32":1193046}');
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it('provides a clean toString() (string types)', (): void => {
|
|
182
|
-
expect(
|
|
183
|
-
new (
|
|
184
|
-
Struct.with({
|
|
185
|
-
txt: 'Text',
|
|
186
|
-
num: 'u32',
|
|
187
|
-
cls: U32
|
|
188
|
-
})
|
|
189
|
-
)(registry, { txt: 'foo', num: 0x123456, cls: 123 }).toString()
|
|
190
|
-
).toEqual('{"txt":"foo","num":1193046,"cls":123}');
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it('exposes the properties on the object', (): void => {
|
|
194
|
-
const struct = new (
|
|
195
|
-
Struct.with({
|
|
196
|
-
txt: Text,
|
|
197
|
-
u32: U32
|
|
198
|
-
})
|
|
199
|
-
)(registry, { txt: 'foo', u32: 0x123456 });
|
|
200
|
-
|
|
201
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
|
|
202
|
-
expect((struct as any).txt.toString()).toEqual('foo');
|
|
203
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
|
|
204
|
-
expect((struct as any).u32.toNumber()).toEqual(0x123456);
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('correctly encodes length', (): void => {
|
|
208
|
-
expect(
|
|
209
|
-
new (
|
|
210
|
-
Struct.with({
|
|
211
|
-
txt: Text,
|
|
212
|
-
u32: U32
|
|
213
|
-
})
|
|
214
|
-
)(registry, { foo: 'bazzing', bar: 69 }).encodedLength
|
|
215
|
-
).toEqual(5);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it('exposes the types', (): void => {
|
|
219
|
-
expect(
|
|
220
|
-
new Struct(registry, {
|
|
221
|
-
foo: Text,
|
|
222
|
-
bar: Text,
|
|
223
|
-
baz: U32
|
|
224
|
-
}, {
|
|
225
|
-
foo: 'foo',
|
|
226
|
-
bar: 'bar',
|
|
227
|
-
baz: 3
|
|
228
|
-
}).Type
|
|
229
|
-
).toEqual({
|
|
230
|
-
foo: 'Text',
|
|
231
|
-
bar: 'Text',
|
|
232
|
-
baz: 'u32'
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
it('gets the value at a particular index', (): void => {
|
|
237
|
-
expect(
|
|
238
|
-
new (
|
|
239
|
-
Struct.with({
|
|
240
|
-
txt: Text,
|
|
241
|
-
u32: U32
|
|
242
|
-
})
|
|
243
|
-
)(registry, { txt: 'foo', u32: 1234 })
|
|
244
|
-
.getAtIndex(1)
|
|
245
|
-
.toString()
|
|
246
|
-
).toEqual('1234');
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
describe('utils', (): void => {
|
|
250
|
-
it('compares against other objects', (): void => {
|
|
251
|
-
const test = {
|
|
252
|
-
foo: 'foo',
|
|
253
|
-
bar: 'bar',
|
|
254
|
-
baz: 3
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
expect(
|
|
258
|
-
new Struct(registry, {
|
|
259
|
-
foo: Text,
|
|
260
|
-
bar: Text,
|
|
261
|
-
baz: U32
|
|
262
|
-
}, test).eq(test)
|
|
263
|
-
).toBe(true);
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
it('has a sane toPrimitive', (): void => {
|
|
267
|
-
const S = Struct.with({
|
|
268
|
-
name: 'Text',
|
|
269
|
-
description: 'Vec<Text>',
|
|
270
|
-
fooA: 'Bytes',
|
|
271
|
-
fooB: 'Bytes',
|
|
272
|
-
fooC: Struct.with({
|
|
273
|
-
a: 'u32',
|
|
274
|
-
b: 'u128',
|
|
275
|
-
c: 'Compact<u128>',
|
|
276
|
-
d: 'bool'
|
|
277
|
-
})
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
expect(
|
|
281
|
-
new S(registry, {
|
|
282
|
-
name: 'Something',
|
|
283
|
-
description: ['One line', 'Another line'],
|
|
284
|
-
fooA: 'hello world!',
|
|
285
|
-
fooB: '0x123456',
|
|
286
|
-
fooC: {
|
|
287
|
-
a: 1234,
|
|
288
|
-
b: BigInt('1234567890111213141516'),
|
|
289
|
-
c: 123456,
|
|
290
|
-
d: true
|
|
291
|
-
}
|
|
292
|
-
}).toPrimitive()
|
|
293
|
-
).toEqual({
|
|
294
|
-
name: 'Something',
|
|
295
|
-
description: ['One line', 'Another line'],
|
|
296
|
-
fooA: 'hello world!',
|
|
297
|
-
fooB: '0x123456',
|
|
298
|
-
fooC: {
|
|
299
|
-
a: 1234,
|
|
300
|
-
b: '1234567890111213141516',
|
|
301
|
-
c: 123456,
|
|
302
|
-
d: true
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
it('generates sane toRawType', (): void => {
|
|
308
|
-
expect(
|
|
309
|
-
new Struct(registry, {
|
|
310
|
-
accountId: 'AccountId',
|
|
311
|
-
balanceCompact: registry.createClass('Compact<Balance>'),
|
|
312
|
-
blockNumber: registry.createClass('BlockNumber'),
|
|
313
|
-
compactNumber: registry.createClass('Compact<BlockNumber>'),
|
|
314
|
-
optionNumber: registry.createClass('Option<BlockNumber>'),
|
|
315
|
-
counter: U32,
|
|
316
|
-
vector: Vec.with('AccountId')
|
|
317
|
-
}).toRawType()
|
|
318
|
-
).toEqual(stringify({
|
|
319
|
-
accountId: 'AccountId',
|
|
320
|
-
balanceCompact: 'Compact<Balance>', // Override in Uint
|
|
321
|
-
blockNumber: 'BlockNumber',
|
|
322
|
-
compactNumber: 'Compact<BlockNumber>',
|
|
323
|
-
optionNumber: 'Option<BlockNumber>',
|
|
324
|
-
counter: 'u32',
|
|
325
|
-
vector: 'Vec<AccountId>'
|
|
326
|
-
}));
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
it('generates sane toRawType (via with)', (): void => {
|
|
330
|
-
const Type = Struct.with({
|
|
331
|
-
accountId: 'AccountId',
|
|
332
|
-
balance: registry.createClass('Balance')
|
|
333
|
-
});
|
|
334
|
-
|
|
335
|
-
expect(
|
|
336
|
-
new Type(registry).toRawType()
|
|
337
|
-
).toEqual(stringify({
|
|
338
|
-
accountId: 'AccountId',
|
|
339
|
-
balance: 'Balance' // Override in Uint
|
|
340
|
-
}));
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
it('allows toString with large numbers', (): void => {
|
|
345
|
-
// replicate https://github.com/polkadot-js/api/issues/640
|
|
346
|
-
expect(
|
|
347
|
-
new Struct(registry, {
|
|
348
|
-
blockNumber: registry.createClass('Option<BlockNumber>')
|
|
349
|
-
}, { blockNumber: '0x0000000010abcdef' }).toString()
|
|
350
|
-
).toEqual('{"blockNumber":279694831}');
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
describe('toU8a', (): void => {
|
|
354
|
-
const def: Record<string, any> = {
|
|
355
|
-
foo: 'Bytes',
|
|
356
|
-
method: 'Bytes',
|
|
357
|
-
bar: 'Option<u32>',
|
|
358
|
-
baz: 'bool'
|
|
359
|
-
};
|
|
360
|
-
const val = {
|
|
361
|
-
foo: '0x4269',
|
|
362
|
-
method: '0x99',
|
|
363
|
-
bar: 1,
|
|
364
|
-
baz: true
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
it('generates toU8a with undefined', (): void => {
|
|
368
|
-
expect(
|
|
369
|
-
new Struct(registry, def, val).toU8a()
|
|
370
|
-
).toEqual(new Uint8Array([2 << 2, 0x42, 0x69, 1 << 2, 0x99, 1, 1, 0, 0, 0, 1]));
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
it('generates toU8a with true', (): void => {
|
|
374
|
-
expect(
|
|
375
|
-
new Struct(registry, def, val).toU8a(true)
|
|
376
|
-
).toEqual(new Uint8Array([0x42, 0x69, 0x99, 1, 0, 0, 0, 1]));
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
it('generates toU8a with { method: true }', (): void => {
|
|
380
|
-
expect(
|
|
381
|
-
new Struct(registry, def, val).toU8a({ method: true })
|
|
382
|
-
).toEqual(new Uint8Array([2 << 2, 0x42, 0x69, 0x99, 1, 1, 0, 0, 0, 1]));
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
it('has a sane inspect', (): void => {
|
|
386
|
-
expect(
|
|
387
|
-
new Struct(registry, def, val).inspect()
|
|
388
|
-
).toEqual({
|
|
389
|
-
inner: [
|
|
390
|
-
{
|
|
391
|
-
name: 'foo',
|
|
392
|
-
outer: [new Uint8Array([2 << 2]), new Uint8Array([0x42, 0x69])]
|
|
393
|
-
},
|
|
394
|
-
{
|
|
395
|
-
name: 'method',
|
|
396
|
-
outer: [new Uint8Array([1 << 2]), new Uint8Array([0x99])]
|
|
397
|
-
},
|
|
398
|
-
{
|
|
399
|
-
inner: undefined,
|
|
400
|
-
name: 'bar',
|
|
401
|
-
outer: [new Uint8Array([1]), new Uint8Array([1, 0, 0, 0])]
|
|
402
|
-
},
|
|
403
|
-
{
|
|
404
|
-
name: 'baz',
|
|
405
|
-
outer: [new Uint8Array([1])]
|
|
406
|
-
}
|
|
407
|
-
]
|
|
408
|
-
});
|
|
409
|
-
});
|
|
410
|
-
});
|
|
411
|
-
});
|