@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,99 @@
|
|
|
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, CodecObject, Inspect, IU8a, Registry, ToString } from '../types/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @name Object
|
|
9
|
+
* @description A type extends the Base class, when it holds a value
|
|
10
|
+
*/
|
|
11
|
+
export abstract class AbstractObject<T extends ToString> implements CodecObject<T> {
|
|
12
|
+
readonly registry: Registry;
|
|
13
|
+
|
|
14
|
+
public createdAtHash?: IU8a;
|
|
15
|
+
public initialU8aLength?: number | undefined;
|
|
16
|
+
public isStorageFallback?: boolean;
|
|
17
|
+
|
|
18
|
+
readonly $: T;
|
|
19
|
+
|
|
20
|
+
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
|
|
21
|
+
this.$ = value;
|
|
22
|
+
this.initialU8aLength = initialU8aLength;
|
|
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 Checks if the value is an empty value
|
|
42
|
+
*/
|
|
43
|
+
public abstract get isEmpty (): boolean;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @description Compares the value of the input to see if there is a match
|
|
47
|
+
*/
|
|
48
|
+
public abstract eq (other?: unknown): boolean;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
52
|
+
*/
|
|
53
|
+
public abstract inspect (): Inspect;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
|
57
|
+
*/
|
|
58
|
+
public abstract toHex (isLe?: boolean): HexString;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
62
|
+
*/
|
|
63
|
+
public abstract toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
67
|
+
*/
|
|
68
|
+
public abstract toJSON (): AnyJson;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @description Converts the value in a best-fit primitive form
|
|
72
|
+
*/
|
|
73
|
+
public abstract toPrimitive (disableAscii?: boolean): AnyJson;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @description Returns the string representation of the value
|
|
77
|
+
*/
|
|
78
|
+
public toString (): string {
|
|
79
|
+
return this.$.toString();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
84
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
85
|
+
*/
|
|
86
|
+
public abstract toU8a (isBare?: BareOpts): Uint8Array;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @description Returns the base runtime type name for this instance
|
|
90
|
+
*/
|
|
91
|
+
public abstract toRawType (): string;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @description Return the internal value (JS-aligned, same result as $)
|
|
95
|
+
*/
|
|
96
|
+
public valueOf (): T {
|
|
97
|
+
return this.$;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
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
|
+
import { TypeRegistry } from '@pezkuwi/types';
|
|
7
|
+
import { CodecDate, Compact, U32 } from '@pezkuwi/types-codec';
|
|
8
|
+
import { BN } from '@pezkuwi/util';
|
|
9
|
+
|
|
10
|
+
import { perf } from '../test/performance.js';
|
|
11
|
+
|
|
12
|
+
const CompactU32 = Compact.with(U32);
|
|
13
|
+
|
|
14
|
+
describe('Compact', (): void => {
|
|
15
|
+
const registry = new TypeRegistry();
|
|
16
|
+
|
|
17
|
+
describe('constructor', (): void => {
|
|
18
|
+
it('fails on > MAX_SAFE_INTEGER', (): void => {
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision, no-loss-of-precision
|
|
20
|
+
expect(() => new Compact(registry, 'u128', 9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('has support for BigInt', (): void => {
|
|
24
|
+
expect(
|
|
25
|
+
new Compact(registry, 'u128', 123456789000123456789n).toHuman()
|
|
26
|
+
).toEqual('123,456,789,000,123,456,789');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('has the correct bitLength for constructor values (BlockNumber)', (): void => {
|
|
30
|
+
expect(
|
|
31
|
+
new (Compact.with(registry.createClass('BlockNumber')))(registry, 0xfffffff9).bitLength()
|
|
32
|
+
).toEqual(32);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('has the correct encodedLength for constructor values (string BlockNumber)', (): void => {
|
|
36
|
+
expect(
|
|
37
|
+
new (Compact.with('BlockNumber'))(registry, 0xfffffff9).encodedLength
|
|
38
|
+
).toEqual(5);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('has the correct encodedLength for constructor values (class BlockNumber)', (): void => {
|
|
42
|
+
expect(
|
|
43
|
+
new (Compact.with(registry.createClass('BlockNumber')))(registry, 0xfffffff9).encodedLength
|
|
44
|
+
).toEqual(5);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('has the correct encodedLength for constructor values (u32)', (): void => {
|
|
48
|
+
expect(
|
|
49
|
+
new (Compact.with(U32))(registry, 0xffff9).encodedLength
|
|
50
|
+
).toEqual(4);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('constructs properly via Uint8Array as U32', (): void => {
|
|
54
|
+
expect(
|
|
55
|
+
new (Compact.with(U32))(registry, new Uint8Array([254, 255, 3, 0])).toNumber()
|
|
56
|
+
).toEqual(new BN(0xffff).toNumber());
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('constructs properly via number as Moment', (): void => {
|
|
60
|
+
expect(
|
|
61
|
+
new (Compact.with(CodecDate))(registry, 1537968546).toString().startsWith('Wed Sep 26 2018') // The time depends on the timezone this test is run in
|
|
62
|
+
).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('utils', (): void => {
|
|
67
|
+
it('compares against another Compact', (): void => {
|
|
68
|
+
expect(new (Compact.with(U32))(registry, 12345).eq(new (Compact.with(U32))(registry, 12345))).toBe(true);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('compares against a primitive', (): void => {
|
|
72
|
+
expect(new (Compact.with(U32))(registry, 12345).eq(12345)).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('unwraps to the wrapped value', (): void => {
|
|
76
|
+
expect(new (Compact.with(U32))(registry, 12345).unwrap() instanceof U32).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('has a valid toBn interface', (): void => {
|
|
80
|
+
expect(new (Compact.with('u128'))(registry, '12345678987654321').toBn().eq(new BN('12345678987654321'))).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('has a valid toBigInt interface', (): void => {
|
|
84
|
+
expect(
|
|
85
|
+
(new (Compact.with('u128'))(registry, 12345678987654321n).toBigInt() + 1n) === 12345678987654322n
|
|
86
|
+
).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('has a sane inspect', (): void => {
|
|
90
|
+
expect(
|
|
91
|
+
new (Compact.with(U32))(registry, 0xffff).inspect()
|
|
92
|
+
).toEqual({
|
|
93
|
+
outer: [new Uint8Array([254, 255, 3, 0])]
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
perf('Compact', 75_000, [[new Uint8Array([63 << 2])]], (v: Uint8Array) => new CompactU32(registry, v));
|
|
99
|
+
});
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { BN } from '@pezkuwi/util';
|
|
5
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
6
|
+
import type { AnyJson, AnyNumber, CodecClass, DefinitionSetter, ICompact, Inspect, INumber, IU8a, Registry } from '../types/index.js';
|
|
7
|
+
|
|
8
|
+
import { compactFromU8a, compactFromU8aLim, compactToU8a, identity, isU8a } from '@pezkuwi/util';
|
|
9
|
+
|
|
10
|
+
import { typeToConstructor } from '../utils/index.js';
|
|
11
|
+
|
|
12
|
+
function decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<T>, value: Compact<T> | AnyNumber): [T, number] {
|
|
13
|
+
if (isU8a(value)) {
|
|
14
|
+
const [decodedLength, bn] = (value[0] & 0b11) < 0b11
|
|
15
|
+
? compactFromU8aLim(value)
|
|
16
|
+
: compactFromU8a(value);
|
|
17
|
+
|
|
18
|
+
return [new Type(registry, bn), decodedLength];
|
|
19
|
+
} else if (value instanceof Compact) {
|
|
20
|
+
const raw = value.unwrap();
|
|
21
|
+
|
|
22
|
+
return raw instanceof Type
|
|
23
|
+
? [raw, 0]
|
|
24
|
+
: [new Type(registry, raw), 0];
|
|
25
|
+
} else if (value instanceof Type) {
|
|
26
|
+
return [value, 0];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [new Type(registry, value), 0];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @name Compact
|
|
34
|
+
* @description
|
|
35
|
+
* A compact length-encoding codec wrapper. It performs the same function as Length, however
|
|
36
|
+
* differs in that it uses a variable number of bytes to do the actual encoding. This is mostly
|
|
37
|
+
* used by other types to add length-prefixed encoding, or in the case of wrapped types, taking
|
|
38
|
+
* a number and making the compact representation thereof
|
|
39
|
+
*/
|
|
40
|
+
export class Compact<T extends INumber> implements ICompact<T> {
|
|
41
|
+
readonly registry: Registry;
|
|
42
|
+
|
|
43
|
+
public createdAtHash?: IU8a;
|
|
44
|
+
public initialU8aLength?: number;
|
|
45
|
+
public isStorageFallback?: boolean;
|
|
46
|
+
|
|
47
|
+
readonly #Type: CodecClass<T>;
|
|
48
|
+
readonly #raw: T;
|
|
49
|
+
|
|
50
|
+
constructor (registry: Registry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0, { definition, setDefinition = identity }: DefinitionSetter<CodecClass<T>> = {}) {
|
|
51
|
+
this.registry = registry;
|
|
52
|
+
this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
|
|
53
|
+
|
|
54
|
+
const [raw, decodedLength] = decodeCompact<T>(registry, this.#Type, value);
|
|
55
|
+
|
|
56
|
+
this.initialU8aLength = decodedLength;
|
|
57
|
+
this.#raw = raw;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public static with<O extends INumber> (Type: CodecClass<O> | string): CodecClass<Compact<O>> {
|
|
61
|
+
let definition: CodecClass<O> | undefined;
|
|
62
|
+
|
|
63
|
+
// eslint-disable-next-line no-return-assign
|
|
64
|
+
const setDefinition = <T> (d: CodecClass<T>) =>
|
|
65
|
+
(definition = d as unknown as CodecClass<O>) as unknown as CodecClass<T>;
|
|
66
|
+
|
|
67
|
+
return class extends Compact<O> {
|
|
68
|
+
constructor (registry: Registry, value?: Compact<O> | AnyNumber) {
|
|
69
|
+
super(registry, Type, value, { definition, setDefinition });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
76
|
+
*/
|
|
77
|
+
public get encodedLength (): number {
|
|
78
|
+
return this.toU8a().length;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @description returns a hash of the contents
|
|
83
|
+
*/
|
|
84
|
+
public get hash (): IU8a {
|
|
85
|
+
return this.registry.hash(this.toU8a());
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @description Checks if the value is an empty value
|
|
90
|
+
*/
|
|
91
|
+
public get isEmpty (): boolean {
|
|
92
|
+
return this.#raw.isEmpty;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* @description Returns the number of bits in the value
|
|
97
|
+
*/
|
|
98
|
+
public bitLength (): number {
|
|
99
|
+
return this.#raw.bitLength();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @description Compares the value of the input to see if there is a match
|
|
104
|
+
*/
|
|
105
|
+
public eq (other?: unknown): boolean {
|
|
106
|
+
return this.#raw.eq(
|
|
107
|
+
other instanceof Compact
|
|
108
|
+
? other.#raw
|
|
109
|
+
: other
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
115
|
+
*/
|
|
116
|
+
public inspect (): Inspect {
|
|
117
|
+
return {
|
|
118
|
+
outer: [this.toU8a()]
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @description Returns a BigInt representation of the number
|
|
124
|
+
*/
|
|
125
|
+
public toBigInt (): bigint {
|
|
126
|
+
return this.#raw.toBigInt();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @description Returns the BN representation of the number
|
|
131
|
+
*/
|
|
132
|
+
public toBn (): BN {
|
|
133
|
+
return this.#raw.toBn();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
|
138
|
+
*/
|
|
139
|
+
public toHex (isLe?: boolean): HexString {
|
|
140
|
+
return this.#raw.toHex(isLe);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
145
|
+
*/
|
|
146
|
+
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
|
147
|
+
return this.#raw.toHuman(isExtended, disableAscii);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
152
|
+
*/
|
|
153
|
+
public toJSON (): AnyJson {
|
|
154
|
+
return this.#raw.toJSON();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @description Returns the number representation for the value
|
|
159
|
+
*/
|
|
160
|
+
public toNumber (): number {
|
|
161
|
+
return this.#raw.toNumber();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* @description Converts the value in a best-fit primitive form
|
|
166
|
+
*/
|
|
167
|
+
public toPrimitive (disableAscii?: boolean): string | number {
|
|
168
|
+
return this.#raw.toPrimitive(disableAscii);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @description Returns the base runtime type name for this instance
|
|
173
|
+
*/
|
|
174
|
+
public toRawType (): string {
|
|
175
|
+
return `Compact<${this.registry.getClassName(this.#Type) || this.#raw.toRawType()}>`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @description Returns the string representation of the value
|
|
180
|
+
*/
|
|
181
|
+
public toString (): string {
|
|
182
|
+
return this.#raw.toString();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
187
|
+
*/
|
|
188
|
+
public toU8a (_isBare?: boolean): Uint8Array {
|
|
189
|
+
return compactToU8a(this.#raw.toBn());
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* @description Returns the embedded [[UInt]] or [[Moment]] value
|
|
194
|
+
*/
|
|
195
|
+
public unwrap (): T {
|
|
196
|
+
return this.#raw;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
import { TypeRegistry } from '@pezkuwi/types';
|
|
7
|
+
import { DoNotConstruct } from '@pezkuwi/types-codec';
|
|
8
|
+
|
|
9
|
+
describe('DoNotConstruct', (): void => {
|
|
10
|
+
const registry = new TypeRegistry();
|
|
11
|
+
|
|
12
|
+
it('does not allow construction', (): void => {
|
|
13
|
+
expect(
|
|
14
|
+
() => new (DoNotConstruct.with())(registry)
|
|
15
|
+
).toThrow(/Cannot construct unknown type DoNotConstruct/);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('does not allow construction (with Name)', (): void => {
|
|
19
|
+
expect(
|
|
20
|
+
() => new (DoNotConstruct.with('Something'))(registry)
|
|
21
|
+
).toThrow(/Cannot construct unknown type Something/);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
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, CodecClass, Inspect, IU8a, Registry } from '../types/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @name DoNotConstruct
|
|
9
|
+
* @description
|
|
10
|
+
* An unknown type that fails on construction with the type info
|
|
11
|
+
*/
|
|
12
|
+
export class DoNotConstruct implements Codec {
|
|
13
|
+
readonly registry: Registry;
|
|
14
|
+
|
|
15
|
+
public createdAtHash?: IU8a;
|
|
16
|
+
public isStorageFallback?: boolean;
|
|
17
|
+
|
|
18
|
+
#neverError: Error;
|
|
19
|
+
|
|
20
|
+
constructor (registry: Registry, typeName = 'DoNotConstruct') {
|
|
21
|
+
this.registry = registry;
|
|
22
|
+
this.#neverError = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`);
|
|
23
|
+
|
|
24
|
+
throw this.#neverError;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public static with (typeName?: string): CodecClass {
|
|
28
|
+
return class extends DoNotConstruct {
|
|
29
|
+
constructor (registry: Registry) {
|
|
30
|
+
super(registry, typeName);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
37
|
+
*/
|
|
38
|
+
public get encodedLength (): number {
|
|
39
|
+
throw this.#neverError;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @description returns a hash of the contents
|
|
44
|
+
*/
|
|
45
|
+
public get hash (): IU8a {
|
|
46
|
+
throw this.#neverError;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @description Checks if the value is an empty value (always true)
|
|
51
|
+
*/
|
|
52
|
+
public get isEmpty (): boolean {
|
|
53
|
+
throw this.#neverError;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @description Unimplemented
|
|
58
|
+
*/
|
|
59
|
+
eq (): boolean {
|
|
60
|
+
throw this.#neverError;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @description Unimplemented
|
|
65
|
+
*/
|
|
66
|
+
public inspect (): Inspect {
|
|
67
|
+
throw this.#neverError;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @description Unimplemented
|
|
72
|
+
*/
|
|
73
|
+
toHex (): HexString {
|
|
74
|
+
throw this.#neverError;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @description Unimplemented
|
|
79
|
+
*/
|
|
80
|
+
toHuman (): AnyJson {
|
|
81
|
+
throw this.#neverError;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @description Unimplemented
|
|
86
|
+
*/
|
|
87
|
+
toJSON (): AnyJson {
|
|
88
|
+
throw this.#neverError;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @description Unimplemented
|
|
93
|
+
*/
|
|
94
|
+
toPrimitive (): AnyJson {
|
|
95
|
+
throw this.#neverError;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @description Unimplemented
|
|
100
|
+
*/
|
|
101
|
+
toRawType (): string {
|
|
102
|
+
throw this.#neverError;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @description Unimplemented
|
|
107
|
+
*/
|
|
108
|
+
toString (): string {
|
|
109
|
+
throw this.#neverError;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @description Unimplemented
|
|
114
|
+
*/
|
|
115
|
+
toU8a (): Uint8Array {
|
|
116
|
+
throw this.#neverError;
|
|
117
|
+
}
|
|
118
|
+
}
|