@pezkuwi/types-codec 16.5.5
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/README.md +3 -0
- package/build/abstract/Array.d.ts +89 -0
- package/build/abstract/Base.d.ts +75 -0
- package/build/abstract/Int.d.ts +80 -0
- package/build/abstract/Object.d.ts +67 -0
- package/build/abstract/index.d.ts +3 -0
- package/build/base/Compact.d.ts +88 -0
- package/build/base/DoNotConstruct.d.ts +63 -0
- package/build/base/Enum.d.ts +118 -0
- package/build/base/Int.d.ts +16 -0
- package/build/base/Null.d.ts +56 -0
- package/build/base/Option.d.ts +94 -0
- package/build/base/Result.d.ts +38 -0
- package/build/base/Tuple.d.ts +42 -0
- package/build/base/UInt.d.ts +15 -0
- package/build/base/Vec.d.ts +28 -0
- package/build/base/VecAny.d.ts +15 -0
- package/build/base/VecFixed.d.ts +30 -0
- package/build/base/index.d.ts +12 -0
- package/build/bundle.d.ts +7 -0
- package/build/extended/BTreeMap.d.ts +5 -0
- package/build/extended/BTreeSet.d.ts +64 -0
- package/build/extended/BitVec.d.ts +37 -0
- package/build/extended/Bytes.d.ts +29 -0
- package/build/extended/HashMap.d.ts +5 -0
- package/build/extended/Linkage.d.ts +37 -0
- package/build/extended/Map.d.ts +59 -0
- package/build/extended/OptionBool.d.ts +36 -0
- package/build/extended/Range.d.ts +29 -0
- package/build/extended/RangeInclusive.d.ts +6 -0
- package/build/extended/Type.d.ts +16 -0
- package/build/extended/U8aFixed.d.ts +16 -0
- package/build/extended/WrapperKeepOpaque.d.ts +40 -0
- package/build/extended/WrapperOpaque.d.ts +10 -0
- package/build/extended/index.d.ts +14 -0
- package/build/index.d.ts +2 -0
- package/build/native/Bool.d.ts +71 -0
- package/build/native/Date.d.ts +84 -0
- package/build/native/Float.d.ts +68 -0
- package/build/native/Json.d.ts +69 -0
- package/build/native/Raw.d.ts +87 -0
- package/build/native/Set.d.ts +84 -0
- package/build/native/Struct.d.ts +106 -0
- package/build/native/Text.d.ts +77 -0
- package/build/native/index.d.ts +8 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/primitive/F32.d.ts +11 -0
- package/build/primitive/F64.d.ts +11 -0
- package/build/primitive/I128.d.ts +11 -0
- package/build/primitive/I16.d.ts +11 -0
- package/build/primitive/I256.d.ts +11 -0
- package/build/primitive/I32.d.ts +11 -0
- package/build/primitive/I64.d.ts +11 -0
- package/build/primitive/I8.d.ts +11 -0
- package/build/primitive/ISize.d.ts +12 -0
- package/build/primitive/U128.d.ts +11 -0
- package/build/primitive/U16.d.ts +11 -0
- package/build/primitive/U256.d.ts +11 -0
- package/build/primitive/U32.d.ts +11 -0
- package/build/primitive/U64.d.ts +11 -0
- package/build/primitive/U8.d.ts +11 -0
- package/build/primitive/USize.d.ts +12 -0
- package/build/primitive/index.d.ts +16 -0
- package/build/types/codec.d.ts +113 -0
- package/build/types/helpers.d.ts +27 -0
- package/build/types/index.d.ts +4 -0
- package/build/types/interfaces.d.ts +74 -0
- package/build/types/registry.d.ts +67 -0
- package/build/utils/compareArray.d.ts +1 -0
- package/build/utils/compareMap.d.ts +1 -0
- package/build/utils/compareSet.d.ts +1 -0
- package/build/utils/decodeU8a.d.ts +26 -0
- package/build/utils/index.d.ts +8 -0
- package/build/utils/sanitize.d.ts +15 -0
- package/build/utils/sortValues.d.ts +12 -0
- package/build/utils/toConstructors.d.ts +16 -0
- package/build/utils/typesToMap.d.ts +2 -0
- package/build/utils/util.d.ts +3 -0
- package/package.json +34 -0
- package/src/abstract/Array.ts +213 -0
- package/src/abstract/Base.ts +129 -0
- package/src/abstract/Int.ts +271 -0
- package/src/abstract/Object.ts +99 -0
- package/src/abstract/index.ts +6 -0
- package/src/base/Compact.spec.ts +99 -0
- package/src/base/Compact.ts +198 -0
- package/src/base/DoNotConstruct.spec.ts +23 -0
- package/src/base/DoNotConstruct.ts +118 -0
- package/src/base/Enum.spec.ts +487 -0
- package/src/base/Enum.ts +460 -0
- package/src/base/Int.spec.ts +225 -0
- package/src/base/Int.ts +34 -0
- package/src/base/Null.spec.ts +41 -0
- package/src/base/Null.ts +96 -0
- package/src/base/Option.spec.ts +216 -0
- package/src/base/Option.ts +275 -0
- package/src/base/Result.spec.ts +64 -0
- package/src/base/Result.ts +79 -0
- package/src/base/Tuple.spec.ts +161 -0
- package/src/base/Tuple.ts +149 -0
- package/src/base/UInt.spec.ts +192 -0
- package/src/base/UInt.ts +30 -0
- package/src/base/Vec.spec.ts +224 -0
- package/src/base/Vec.ts +133 -0
- package/src/base/VecAny.ts +23 -0
- package/src/base/VecFixed.spec.ts +78 -0
- package/src/base/VecFixed.ts +92 -0
- package/src/base/index.ts +15 -0
- package/src/bundle.ts +13 -0
- package/src/checkTypes.manual.ts +12 -0
- package/src/extended/BTreeMap.spec.ts +245 -0
- package/src/extended/BTreeMap.ts +16 -0
- package/src/extended/BTreeSet.spec.ts +260 -0
- package/src/extended/BTreeSet.ts +233 -0
- package/src/extended/BitVec.spec.ts +97 -0
- package/src/extended/BitVec.ts +137 -0
- package/src/extended/Bytes.spec.ts +75 -0
- package/src/extended/Bytes.ts +88 -0
- package/src/extended/HashMap.spec.ts +36 -0
- package/src/extended/HashMap.ts +16 -0
- package/src/extended/Linkage.spec.ts +43 -0
- package/src/extended/Linkage.ts +81 -0
- package/src/extended/Map.spec.ts +123 -0
- package/src/extended/Map.ts +255 -0
- package/src/extended/OptionBool.spec.ts +49 -0
- package/src/extended/OptionBool.ts +93 -0
- package/src/extended/Range.spec.ts +37 -0
- package/src/extended/Range.ts +56 -0
- package/src/extended/RangeInclusive.ts +20 -0
- package/src/extended/Type.spec.ts +118 -0
- package/src/extended/Type.ts +29 -0
- package/src/extended/U8aFixed.spec.ts +117 -0
- package/src/extended/U8aFixed.ts +57 -0
- package/src/extended/WrapperKeepOpaque.spec.ts +101 -0
- package/src/extended/WrapperKeepOpaque.ts +128 -0
- package/src/extended/WrapperOpaque.spec.ts +58 -0
- package/src/extended/WrapperOpaque.ts +27 -0
- package/src/extended/index.ts +17 -0
- package/src/index.ts +6 -0
- package/src/mod.ts +4 -0
- package/src/native/Bool.spec.ts +74 -0
- package/src/native/Bool.ts +137 -0
- package/src/native/Date.spec.ts +85 -0
- package/src/native/Date.ts +169 -0
- package/src/native/Float.spec.ts +51 -0
- package/src/native/Float.ts +136 -0
- package/src/native/Json.ts +147 -0
- package/src/native/Raw.spec.ts +113 -0
- package/src/native/Raw.ts +171 -0
- package/src/native/Set.spec.ts +116 -0
- package/src/native/Set.ts +269 -0
- package/src/native/Struct.data.ts +4 -0
- package/src/native/Struct.spec.ts +411 -0
- package/src/native/Struct.ts +338 -0
- package/src/native/Text.spec.ts +85 -0
- package/src/native/Text.ts +184 -0
- package/src/native/index.ts +11 -0
- package/src/packageDetect.ts +11 -0
- package/src/packageInfo.ts +6 -0
- package/src/primitive/F32.ts +14 -0
- package/src/primitive/F64.ts +14 -0
- package/src/primitive/I128.ts +14 -0
- package/src/primitive/I16.ts +14 -0
- package/src/primitive/I256.ts +14 -0
- package/src/primitive/I32.ts +14 -0
- package/src/primitive/I64.ts +14 -0
- package/src/primitive/I8.ts +14 -0
- package/src/primitive/ISize.ts +21 -0
- package/src/primitive/U128.ts +14 -0
- package/src/primitive/U16.ts +14 -0
- package/src/primitive/U256.ts +14 -0
- package/src/primitive/U32.ts +14 -0
- package/src/primitive/U64.ts +14 -0
- package/src/primitive/U8.ts +14 -0
- package/src/primitive/USize.ts +21 -0
- package/src/primitive/index.ts +19 -0
- package/src/test/performance.ts +61 -0
- package/src/types/codec.ts +140 -0
- package/src/types/helpers.ts +50 -0
- package/src/types/index.ts +7 -0
- package/src/types/interfaces.ts +98 -0
- package/src/types/registry.ts +86 -0
- package/src/utils/compareArray.ts +22 -0
- package/src/utils/compareMap.ts +40 -0
- package/src/utils/compareSet.ts +23 -0
- package/src/utils/decodeU8a.ts +123 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/sanitize.spec.ts +89 -0
- package/src/utils/sanitize.ts +290 -0
- package/src/utils/sortValues.ts +103 -0
- package/src/utils/toConstructors.ts +46 -0
- package/src/utils/typesToMap.ts +14 -0
- package/src/utils/util.ts +8 -0
- package/tsconfig.build.json +16 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +21 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AnyString } from '../types/index.js';
|
|
2
|
+
type Mapper = (value: string) => string;
|
|
3
|
+
export declare function trim(): Mapper;
|
|
4
|
+
export declare function findClosing(value: string, start: number): number;
|
|
5
|
+
export declare function alias(src: string, dest: string, withChecks?: boolean): Mapper;
|
|
6
|
+
export declare function cleanupCompact(): Mapper;
|
|
7
|
+
export declare function flattenSingleTuple(): Mapper;
|
|
8
|
+
export declare function removeExtensions(type: string, isSized: boolean): Mapper;
|
|
9
|
+
export declare function removeColons(): Mapper;
|
|
10
|
+
export declare function removeGenerics(): Mapper;
|
|
11
|
+
export declare function removePairOf(): Mapper;
|
|
12
|
+
export declare function removeTraits(): Mapper;
|
|
13
|
+
export declare function removeWrap(check: string): Mapper;
|
|
14
|
+
export declare function sanitize(value: AnyString): string;
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BN } from '@pezkuwi/util';
|
|
2
|
+
import type { Codec } from '../types/index.js';
|
|
3
|
+
type SortArg = Codec | Codec[] | number[] | BN | bigint | number | Uint8Array;
|
|
4
|
+
/**
|
|
5
|
+
* Sort keys/values of BTreeSet/BTreeMap in ascending order for encoding compatibility with Rust's BTreeSet/BTreeMap
|
|
6
|
+
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeSet.html)
|
|
7
|
+
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeMap.html)
|
|
8
|
+
*/
|
|
9
|
+
export declare function sortAsc<V extends SortArg = Codec>(a: V, b: V): number;
|
|
10
|
+
export declare function sortSet<V extends Codec = Codec>(set: Set<V>): Set<V>;
|
|
11
|
+
export declare function sortMap<K extends Codec = Codec, V extends Codec = Codec>(map: Map<K, V>): Map<K, V>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Codec, CodecClass, Registry } from '../types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* @internal
|
|
4
|
+
* From a type string or class, return the associated type class
|
|
5
|
+
*/
|
|
6
|
+
export declare function typeToConstructor<T extends Codec = Codec>(registry: Registry, type: string | CodecClass<T>): CodecClass<T>;
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
* Takes an input array of types and returns the associated classes for it
|
|
10
|
+
*/
|
|
11
|
+
export declare function typesToConstructors<T extends Codec = Codec>(registry: Registry, types: (string | CodecClass<T>)[]): CodecClass<T>[];
|
|
12
|
+
/**
|
|
13
|
+
* @internal
|
|
14
|
+
* Takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
|
|
15
|
+
*/
|
|
16
|
+
export declare function mapToTypeMap(registry: Registry, input: Record<string, string | CodecClass>): [CodecClass[], string[]];
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "Jaco Greeff <jacogr@gmail.com>",
|
|
3
|
+
"bugs": "https://github.com/pezkuwichain/pezkuwi-api/issues",
|
|
4
|
+
"description": "Implementation of the SCALE codec",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=18"
|
|
7
|
+
},
|
|
8
|
+
"homepage": "https://github.com/pezkuwichain/pezkuwi-api/tree/master/packages/types-codec#readme",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"name": "@pezkuwi/types-codec",
|
|
11
|
+
"repository": {
|
|
12
|
+
"directory": "packages/types-codec",
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/pezkuwichain/pezkuwi-api.git"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": [
|
|
17
|
+
"./packageDetect.js",
|
|
18
|
+
"./packageDetect.cjs"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"version": "16.5.5",
|
|
22
|
+
"main": "index.js",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@pezkuwi/util": "14.0.5",
|
|
25
|
+
"@pezkuwi/x-bigint": "14.0.5",
|
|
26
|
+
"tslib": "^2.8.1"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@pezkuwi/types": "16.5.5",
|
|
30
|
+
"@pezkuwi/types-augment": "16.5.5",
|
|
31
|
+
"@pezkuwi/types-support": "16.5.5",
|
|
32
|
+
"@pezkuwi/util-crypto": "14.0.5"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
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 { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types/index.js';
|
|
6
|
+
|
|
7
|
+
import { compactToU8a, u8aConcatStrict, u8aToHex } from '@pezkuwi/util';
|
|
8
|
+
|
|
9
|
+
import { compareArray } from '../utils/compareArray.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @name AbstractArray
|
|
13
|
+
* @description
|
|
14
|
+
* This manages codec arrays. It is an extension to Array, providing
|
|
15
|
+
* specific encoding/decoding on top of the base type.
|
|
16
|
+
* @noInheritDoc
|
|
17
|
+
*/
|
|
18
|
+
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
|
|
19
|
+
readonly registry: Registry;
|
|
20
|
+
|
|
21
|
+
public createdAtHash?: IU8a;
|
|
22
|
+
public initialU8aLength?: number;
|
|
23
|
+
public isStorageFallback?: boolean;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @description This ensures that operators such as clice, filter, map, etc. return
|
|
27
|
+
* new Array instances (without this we need to apply overrides)
|
|
28
|
+
*/
|
|
29
|
+
static get [Symbol.species] (): typeof Array {
|
|
30
|
+
return Array;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
protected constructor (registry: Registry, length: number) {
|
|
34
|
+
super(length);
|
|
35
|
+
|
|
36
|
+
this.registry = registry;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
41
|
+
*/
|
|
42
|
+
public get encodedLength (): number {
|
|
43
|
+
// We need to loop through all entries since they may have a variable length themselves,
|
|
44
|
+
// e.g. when a Vec or Compact is contained withing, it has a variable length based on data
|
|
45
|
+
const count = this.length;
|
|
46
|
+
let total = compactToU8a(count).length;
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < count; i++) {
|
|
49
|
+
total += this[i].encodedLength;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return total;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @description returns a hash of the contents
|
|
57
|
+
*/
|
|
58
|
+
public get hash (): IU8a {
|
|
59
|
+
return this.registry.hash(this.toU8a());
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @description Checks if the value is an empty value
|
|
64
|
+
*/
|
|
65
|
+
public get isEmpty (): boolean {
|
|
66
|
+
return this.length === 0;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @description The length of the value
|
|
71
|
+
*/
|
|
72
|
+
public override get length (): number {
|
|
73
|
+
// only included here since we ignore inherited docs
|
|
74
|
+
return super.length;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @description Compares the value of the input to see if there is a match
|
|
79
|
+
*/
|
|
80
|
+
public eq (other?: unknown): boolean {
|
|
81
|
+
return compareArray(this, other);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
86
|
+
*/
|
|
87
|
+
public inspect (): Inspect {
|
|
88
|
+
return {
|
|
89
|
+
inner: this.inspectInner(),
|
|
90
|
+
outer: [compactToU8a(this.length)]
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @internal
|
|
96
|
+
* @description Internal per-item inspection of internal values
|
|
97
|
+
*/
|
|
98
|
+
public inspectInner (): Inspect[] {
|
|
99
|
+
const count = this.length;
|
|
100
|
+
const inner = new Array<Inspect>(count);
|
|
101
|
+
|
|
102
|
+
for (let i = 0; i < count; i++) {
|
|
103
|
+
inner[i] = this[i].inspect();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return inner;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @description Converts the Object to an standard JavaScript Array
|
|
111
|
+
*/
|
|
112
|
+
public toArray (): T[] {
|
|
113
|
+
return Array.from(this);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @description Returns a hex string representation of the value
|
|
118
|
+
*/
|
|
119
|
+
public toHex (): HexString {
|
|
120
|
+
return u8aToHex(this.toU8a());
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
125
|
+
*/
|
|
126
|
+
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
|
127
|
+
const count = this.length;
|
|
128
|
+
const result = new Array<AnyJson>(count);
|
|
129
|
+
|
|
130
|
+
for (let i = 0; i < count; i++) {
|
|
131
|
+
result[i] = this[i] && this[i].toHuman(isExtended, disableAscii);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
139
|
+
*/
|
|
140
|
+
public toJSON (): AnyJson {
|
|
141
|
+
const count = this.length;
|
|
142
|
+
const result = new Array<AnyJson>(count);
|
|
143
|
+
|
|
144
|
+
for (let i = 0; i < count; i++) {
|
|
145
|
+
// We actually log inside the U8a decoding and use JSON.stringify(...), which
|
|
146
|
+
// means that the Vec may be partially populated (same applies to toHuman, same check)
|
|
147
|
+
result[i] = this[i] && this[i].toJSON();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @description Converts the value in a best-fit primitive form
|
|
155
|
+
*/
|
|
156
|
+
public toPrimitive (disableAscii?: boolean): AnyJson {
|
|
157
|
+
const count = this.length;
|
|
158
|
+
const result = new Array<AnyJson>(count);
|
|
159
|
+
|
|
160
|
+
for (let i = 0; i < count; i++) {
|
|
161
|
+
result[i] = this[i] && this[i].toPrimitive(disableAscii);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @description Returns the base runtime type name for this instance
|
|
169
|
+
*/
|
|
170
|
+
abstract toRawType (): string;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @description Returns the string representation of the value
|
|
174
|
+
*/
|
|
175
|
+
public override toString (): string {
|
|
176
|
+
const count = this.length;
|
|
177
|
+
const result = new Array<string>(count);
|
|
178
|
+
|
|
179
|
+
for (let i = 0; i < count; i++) {
|
|
180
|
+
result[i] = this[i].toString();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return `[${result.join(', ')}]`;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
188
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
189
|
+
*/
|
|
190
|
+
public toU8a (isBare?: boolean): Uint8Array {
|
|
191
|
+
const encoded = this.toU8aInner();
|
|
192
|
+
|
|
193
|
+
return isBare
|
|
194
|
+
? u8aConcatStrict(encoded)
|
|
195
|
+
: u8aConcatStrict([compactToU8a(this.length), ...encoded]);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* @internal
|
|
200
|
+
* @description Internal per-item SCALE encoding of contained values
|
|
201
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
202
|
+
*/
|
|
203
|
+
public toU8aInner (isBare?: boolean): Uint8Array[] {
|
|
204
|
+
const count = this.length;
|
|
205
|
+
const encoded = new Array<Uint8Array>(count);
|
|
206
|
+
|
|
207
|
+
for (let i = 0; i < count; i++) {
|
|
208
|
+
encoded[i] = this[i].toU8a(isBare);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return encoded;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
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 { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @name Base
|
|
9
|
+
* @description A type extends the Base class, when it holds a value
|
|
10
|
+
*/
|
|
11
|
+
export abstract class AbstractBase<T extends Codec> implements Codec {
|
|
12
|
+
readonly registry: Registry;
|
|
13
|
+
|
|
14
|
+
public createdAtHash?: IU8a | undefined;
|
|
15
|
+
public initialU8aLength?: number | undefined;
|
|
16
|
+
public isStorageFallback?: boolean;
|
|
17
|
+
|
|
18
|
+
readonly #raw: T;
|
|
19
|
+
|
|
20
|
+
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
|
|
21
|
+
this.initialU8aLength = initialU8aLength;
|
|
22
|
+
this.#raw = value;
|
|
23
|
+
this.registry = registry;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
28
|
+
*/
|
|
29
|
+
public get encodedLength (): number {
|
|
30
|
+
return this.toU8a().length;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @description returns a hash of the contents
|
|
35
|
+
*/
|
|
36
|
+
public get hash (): IU8a {
|
|
37
|
+
return this.registry.hash(this.toU8a());
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @description returns the inner (wrapped value)
|
|
42
|
+
*/
|
|
43
|
+
public get inner (): T {
|
|
44
|
+
return this.#raw;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @description Checks if the value is an empty value
|
|
49
|
+
*/
|
|
50
|
+
public get isEmpty (): boolean {
|
|
51
|
+
return this.#raw.isEmpty;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @description Compares the value of the input to see if there is a match
|
|
56
|
+
*/
|
|
57
|
+
public eq (other?: unknown): boolean {
|
|
58
|
+
return this.#raw.eq(other);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
63
|
+
*/
|
|
64
|
+
public inspect (): Inspect {
|
|
65
|
+
return this.#raw.inspect();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
|
70
|
+
*/
|
|
71
|
+
public toHex (isLe?: boolean): HexString {
|
|
72
|
+
return this.#raw.toHex(isLe);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
77
|
+
*/
|
|
78
|
+
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
|
79
|
+
return this.#raw.toHuman(isExtended, disableAscii);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
84
|
+
*/
|
|
85
|
+
public toJSON (): AnyJson {
|
|
86
|
+
return this.#raw.toJSON();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @description Converts the value in a best-fit primitive form
|
|
91
|
+
*/
|
|
92
|
+
public toPrimitive (disableAscii?: boolean): AnyJson {
|
|
93
|
+
return this.#raw.toPrimitive(disableAscii);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @description Returns the string representation of the value
|
|
98
|
+
*/
|
|
99
|
+
public toString (): string {
|
|
100
|
+
return this.#raw.toString();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
105
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
106
|
+
*/
|
|
107
|
+
public toU8a (isBare?: BareOpts): Uint8Array {
|
|
108
|
+
return this.#raw.toU8a(isBare);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @description Returns the base runtime type name for this instance
|
|
113
|
+
*/
|
|
114
|
+
public abstract toRawType (): string;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* @description Returns the inner wrapped value (equivalent to valueOf)
|
|
118
|
+
*/
|
|
119
|
+
public unwrap (): T {
|
|
120
|
+
return this.#raw;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @description Returns the inner wrapped value
|
|
125
|
+
*/
|
|
126
|
+
public valueOf (): T {
|
|
127
|
+
return this.#raw;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
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 { AnyNumber, Inspect, INumber, IU8a, Registry, ToBn, UIntBitLength } from '../types/index.js';
|
|
6
|
+
|
|
7
|
+
import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBigInt, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@pezkuwi/util';
|
|
8
|
+
|
|
9
|
+
export const DEFAULT_UINT_BITS = 64;
|
|
10
|
+
|
|
11
|
+
// Maximum allowed integer for JS is 2^53 - 1, set limit at 52
|
|
12
|
+
// In this case however, we always print any >32 as hex
|
|
13
|
+
const MAX_NUMBER_BITS = 52;
|
|
14
|
+
const MUL_P = new BN(1_00_00);
|
|
15
|
+
|
|
16
|
+
const FORMATTERS: [string, BN][] = [
|
|
17
|
+
['Perquintill', BN_QUINTILL],
|
|
18
|
+
['Perbill', BN_BILLION],
|
|
19
|
+
['Permill', BN_MILLION],
|
|
20
|
+
['Percent', BN_HUNDRED]
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
function isToBn (value: unknown): value is ToBn {
|
|
24
|
+
return isFunction((value as ToBn).toBn);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function toPercentage (value: BN, divisor: BN): string {
|
|
28
|
+
return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** @internal */
|
|
32
|
+
function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<string, string> | ToBn | null, isNegative: boolean): string | number {
|
|
33
|
+
if (isNumber(value)) {
|
|
34
|
+
if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
|
|
35
|
+
throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return value;
|
|
39
|
+
} else if (isString(value)) {
|
|
40
|
+
if (isHex(value, -1, true)) {
|
|
41
|
+
return hexToBn(value, { isLe: false, isNegative }).toString();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (value.includes('.') || value.includes(',') || value.includes('e')) {
|
|
45
|
+
throw new Error('String should not contain decimal points or scientific notation');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return value;
|
|
49
|
+
} else if (isBn(value) || isBigInt(value)) {
|
|
50
|
+
return value.toString();
|
|
51
|
+
} else if (isObject(value)) {
|
|
52
|
+
if (isToBn(value)) {
|
|
53
|
+
return value.toBn().toString();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Allow the construction from an object with a single top-level key. This means that
|
|
57
|
+
// single key objects can be treated equivalently to numbers, assuming they meet the
|
|
58
|
+
// specific requirements. (This is useful in Weights 1.5 where Objects are compact)
|
|
59
|
+
const keys = Object.keys(value);
|
|
60
|
+
|
|
61
|
+
if (keys.length !== 1) {
|
|
62
|
+
throw new Error('Unable to construct number from multi-key object');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return decodeAbstractInt(value[keys[0]], isNegative);
|
|
66
|
+
} else if (!value) {
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
throw new Error(`Unable to create BN from unknown type ${typeof value}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @name AbstractInt
|
|
75
|
+
* @ignore
|
|
76
|
+
* @noInheritDoc
|
|
77
|
+
*/
|
|
78
|
+
export abstract class AbstractInt extends BN implements INumber {
|
|
79
|
+
readonly registry: Registry;
|
|
80
|
+
readonly encodedLength: number;
|
|
81
|
+
readonly isUnsigned: boolean;
|
|
82
|
+
|
|
83
|
+
public createdAtHash?: IU8a;
|
|
84
|
+
public initialU8aLength?: number;
|
|
85
|
+
public isStorageFallback?: boolean;
|
|
86
|
+
|
|
87
|
+
readonly #bitLength: UIntBitLength;
|
|
88
|
+
|
|
89
|
+
constructor (registry: Registry, value: AnyNumber | null = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
|
|
90
|
+
// Construct via a string/number, which will be passed in the BN constructor.
|
|
91
|
+
// It would be ideal to actually return a BN, but there is an issue:
|
|
92
|
+
// https://github.com/indutny/bn.js/issues/206
|
|
93
|
+
super(
|
|
94
|
+
// shortcut isU8a as used in SCALE decoding
|
|
95
|
+
isU8a(value)
|
|
96
|
+
? bitLength <= 48
|
|
97
|
+
? u8aToNumber(value.subarray(0, bitLength / 8), { isNegative: isSigned })
|
|
98
|
+
: u8aToBn(value.subarray(0, bitLength / 8), { isLe: true, isNegative: isSigned }).toString()
|
|
99
|
+
: decodeAbstractInt(value, isSigned)
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
this.registry = registry;
|
|
103
|
+
this.#bitLength = bitLength;
|
|
104
|
+
this.encodedLength = this.#bitLength / 8;
|
|
105
|
+
this.initialU8aLength = this.#bitLength / 8;
|
|
106
|
+
this.isUnsigned = !isSigned;
|
|
107
|
+
|
|
108
|
+
const isNegative = this.isNeg();
|
|
109
|
+
const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
|
|
110
|
+
|
|
111
|
+
if (isNegative && !isSigned) {
|
|
112
|
+
throw new Error(`${this.toRawType()}: Negative number passed to unsigned type`);
|
|
113
|
+
} else if (super.bitLength() > maxBits) {
|
|
114
|
+
throw new Error(`${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @description returns a hash of the contents
|
|
120
|
+
*/
|
|
121
|
+
public get hash (): IU8a {
|
|
122
|
+
return this.registry.hash(this.toU8a());
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @description Checks if the value is a zero value (align elsewhere)
|
|
127
|
+
*/
|
|
128
|
+
public get isEmpty (): boolean {
|
|
129
|
+
return this.isZero();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @description Returns the number of bits in the value
|
|
134
|
+
*/
|
|
135
|
+
public override bitLength (): number {
|
|
136
|
+
return this.#bitLength;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @description Compares the value of the input to see if there is a match
|
|
141
|
+
*/
|
|
142
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
143
|
+
public override eq (other?: unknown): boolean {
|
|
144
|
+
// Here we are actually overriding the built-in .eq to take care of both
|
|
145
|
+
// number and BN inputs (no `.eqn` needed) - numbers will be converted
|
|
146
|
+
return super.eq(
|
|
147
|
+
isHex(other)
|
|
148
|
+
? hexToBn(other.toString(), { isLe: false, isNegative: !this.isUnsigned })
|
|
149
|
+
: bnToBn(other as string)
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
155
|
+
*/
|
|
156
|
+
public inspect (): Inspect {
|
|
157
|
+
return {
|
|
158
|
+
outer: [this.toU8a()]
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @description True if this value is the max of the type
|
|
164
|
+
*/
|
|
165
|
+
public isMax (): boolean {
|
|
166
|
+
const u8a = this.toU8a().filter((b) => b === 0xff);
|
|
167
|
+
|
|
168
|
+
return u8a.length === (this.#bitLength / 8);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @description Returns a BigInt representation of the number
|
|
173
|
+
*/
|
|
174
|
+
public toBigInt (): bigint {
|
|
175
|
+
return BigInt(this.toString());
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @description Returns the BN representation of the number. (Compatibility)
|
|
180
|
+
*/
|
|
181
|
+
public toBn (): BN {
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @description Returns a hex string representation of the value
|
|
187
|
+
*/
|
|
188
|
+
public toHex (isLe = false): HexString {
|
|
189
|
+
// For display/JSON, this is BE, for compare, use isLe
|
|
190
|
+
return bnToHex(this, {
|
|
191
|
+
bitLength: this.bitLength(),
|
|
192
|
+
isLe,
|
|
193
|
+
isNegative: !this.isUnsigned
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
199
|
+
*/
|
|
200
|
+
public toHuman (_isExpanded?: boolean): string {
|
|
201
|
+
const rawType = this.toRawType();
|
|
202
|
+
|
|
203
|
+
if (rawType === 'Balance') {
|
|
204
|
+
return this.isMax()
|
|
205
|
+
? 'everything'
|
|
206
|
+
// FIXME In the case of multiples we need some way of detecting which instance this belongs
|
|
207
|
+
// to. as it stands we will always format (incorrectly) against the first token defined
|
|
208
|
+
: formatBalance(this, { decimals: this.registry.chainDecimals[0], withSi: true, withUnit: this.registry.chainTokens[0] });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const [, divisor] = FORMATTERS.find(([type]) => type === rawType) || [];
|
|
212
|
+
|
|
213
|
+
return divisor
|
|
214
|
+
? toPercentage(this, divisor)
|
|
215
|
+
: formatNumber(this);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
220
|
+
*/
|
|
221
|
+
public override toJSON (onlyHex = false): any {
|
|
222
|
+
// FIXME this return type should by string | number, however BN returns string
|
|
223
|
+
// Options here are
|
|
224
|
+
// - super.bitLength() - the actual used bits, use hex when close to MAX_SAFE_INTEGER
|
|
225
|
+
// - this.#bitLength - the max used bits, use hex when larger than native Rust type
|
|
226
|
+
return onlyHex || (this.#bitLength > 128) || (super.bitLength() > MAX_NUMBER_BITS)
|
|
227
|
+
? this.toHex()
|
|
228
|
+
: this.toNumber();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
|
|
233
|
+
*/
|
|
234
|
+
public toPrimitive (): number | string {
|
|
235
|
+
return super.bitLength() > MAX_NUMBER_BITS
|
|
236
|
+
? this.toString()
|
|
237
|
+
: this.toNumber();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @description Returns the base runtime type name for this instance
|
|
242
|
+
*/
|
|
243
|
+
public toRawType (): string {
|
|
244
|
+
// NOTE In the case of balances, which have a special meaning on the UI
|
|
245
|
+
// and can be interpreted differently, return a specific value for it so
|
|
246
|
+
// underlying it always matches (no matter which length it actually is)
|
|
247
|
+
return this instanceof this.registry.createClassUnsafe('Balance')
|
|
248
|
+
? 'Balance'
|
|
249
|
+
: `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* @description Returns the string representation of the value
|
|
254
|
+
* @param base The base to use for the conversion
|
|
255
|
+
*/
|
|
256
|
+
public override toString (base?: number): string {
|
|
257
|
+
// only included here since we do not inherit docs
|
|
258
|
+
return super.toString(base);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
263
|
+
*/
|
|
264
|
+
public toU8a (_isBare?: boolean): Uint8Array {
|
|
265
|
+
return bnToU8a(this, {
|
|
266
|
+
bitLength: this.bitLength(),
|
|
267
|
+
isLe: true,
|
|
268
|
+
isNegative: !this.isUnsigned
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|