Add ISize type (same as USize, not-to-be used) (#5556)
This commit is contained in:
@@ -90,6 +90,10 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Internal per-item inspection of internal values
|
||||
*/
|
||||
public inspectInner (): Inspect[] {
|
||||
const inner = new Array<Inspect>(this.length);
|
||||
|
||||
@@ -185,6 +189,11 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
: u8aConcatStrict([compactToU8a(this.length), ...encoded]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @description Internal per-item SCALE encoding of contained values
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8aInner (isBare?: boolean): Uint8Array[] {
|
||||
const encoded = new Array<Uint8Array>(this.length);
|
||||
|
||||
|
||||
@@ -260,7 +260,6 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (_isBare?: boolean): Uint8Array {
|
||||
return bnToU8a(this, {
|
||||
|
||||
@@ -193,7 +193,6 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (_isBare?: boolean): Uint8Array {
|
||||
return compactToU8a(this.#raw.toBn());
|
||||
|
||||
@@ -53,38 +53,65 @@ export class DoNotConstruct implements Codec {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
eq (): boolean {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
public inspect (): Inspect {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
toHex (): HexString {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
toHuman (): AnyJson {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
toJSON (): AnyJson {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
toPrimitive (): AnyJson {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
toRawType (): string {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
toString (): string {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented
|
||||
*/
|
||||
toU8a (): Uint8Array {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
@@ -460,5 +460,28 @@ describe('Enum', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('toU8a', (): void => {
|
||||
const Test = Enum.with({
|
||||
A: Text,
|
||||
B: U32
|
||||
});
|
||||
|
||||
it('has a correct toU8a() output', (): void => {
|
||||
expect(
|
||||
new Test(registry, { B: 69 }).toU8a()
|
||||
).toEqual(
|
||||
new Uint8Array([1, 69, 0, 0, 0])
|
||||
);
|
||||
});
|
||||
|
||||
it('has a correct toU8a(true) output', (): void => {
|
||||
expect(
|
||||
new Test(registry, { B: 69 }).toU8a(true)
|
||||
).toEqual(
|
||||
new Uint8Array([69, 0, 0, 0])
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
perf('Enum', 20_000, [[new Uint8Array([0, 31, 32, 33, 34])]], (v: Uint8Array) => new PEnum(registry, v));
|
||||
});
|
||||
|
||||
@@ -89,7 +89,6 @@ export class Null implements Codec {
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (_isBare?: boolean): Uint8Array {
|
||||
return new Uint8Array();
|
||||
|
||||
@@ -147,6 +147,12 @@ describe('Option', (): void => {
|
||||
).toEqual(new Uint8Array([1, 4 << 2, 49, 50, 51, 52]));
|
||||
});
|
||||
|
||||
it('allows bare specifiers on toU8a', (): void => {
|
||||
expect(
|
||||
new Option(registry, Text, '1234').toU8a(true)
|
||||
).toEqual(new Uint8Array([49, 50, 51, 52]));
|
||||
});
|
||||
|
||||
it('converts toU8a() without', (): void => {
|
||||
expect(
|
||||
new Option(registry, Text).toU8a()
|
||||
|
||||
@@ -86,7 +86,7 @@ describe('Vec', (): void => {
|
||||
expect(vector.toString()).toEqual('[1, 23, 345, 4567, 56789]');
|
||||
});
|
||||
|
||||
it('encodes with length prefix', (): void => {
|
||||
it('encodes with length prefix on toU8a()', (): void => {
|
||||
expect(vector.toU8a()).toEqual(new Uint8Array([
|
||||
5 << 2,
|
||||
1 << 2, 49,
|
||||
@@ -97,6 +97,16 @@ describe('Vec', (): void => {
|
||||
]));
|
||||
});
|
||||
|
||||
it('encodes without length prefix on toU8a(true)', (): void => {
|
||||
expect(vector.toU8a(true)).toEqual(new Uint8Array([
|
||||
1 << 2, 49,
|
||||
2 << 2, 50, 51,
|
||||
3 << 2, 51, 52, 53,
|
||||
4 << 2, 52, 53, 54, 55,
|
||||
5 << 2, 53, 54, 55, 56, 57
|
||||
]));
|
||||
});
|
||||
|
||||
it('exposes the type', (): void => {
|
||||
expect(vector.Type).toEqual('Text');
|
||||
});
|
||||
|
||||
@@ -73,6 +73,9 @@ export class BitVec extends Raw {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public override toHuman (): string {
|
||||
return `0b${
|
||||
[...this.toU8a(true)]
|
||||
@@ -94,7 +97,7 @@ export class BitVec extends Raw {
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public override toU8a (isBare?: boolean): Uint8Array {
|
||||
const bitVec = super.toU8a();
|
||||
const bitVec = super.toU8a(isBare);
|
||||
|
||||
return isBare
|
||||
? bitVec
|
||||
|
||||
@@ -34,10 +34,16 @@ export class Linkage<T extends Codec> extends Struct {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the next item the Linkage is pointing to
|
||||
*/
|
||||
public get previous (): Option<T> {
|
||||
return this.get('previous') as Option<T>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the previous item the Linkage is pointing to
|
||||
*/
|
||||
public get next (): Option<T> {
|
||||
return this.get('next') as Option<T>;
|
||||
}
|
||||
@@ -52,12 +58,12 @@ export class Linkage<T extends Codec> extends Struct {
|
||||
/**
|
||||
* @description Custom toU8a which with bare mode does not return the linkage if empty
|
||||
*/
|
||||
public override toU8a (): Uint8Array {
|
||||
public override toU8a (isBare?: boolean): Uint8Array {
|
||||
// As part of a storage query (where these appear), in the case of empty, the values
|
||||
// are NOT populated by the node - follow the same logic, leaving it empty
|
||||
return this.isEmpty
|
||||
? EMPTY
|
||||
: super.toU8a();
|
||||
: super.toU8a(isBare);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -130,7 +130,6 @@ export class bool extends Boolean implements Codec {
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (_isBare?: boolean): Uint8Array {
|
||||
return new Uint8Array([this.valueOf() ? 1 : 0]);
|
||||
|
||||
@@ -162,7 +162,6 @@ export class CodecDate extends Date implements INumber {
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (_isBare?: boolean): Uint8Array {
|
||||
return bnToU8a(this.toNumber(), U8A_OPTS);
|
||||
|
||||
@@ -128,7 +128,9 @@ export class Float extends Number implements IFloat {
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
*/
|
||||
public toU8a (): Uint8Array {
|
||||
return floatToU8a(this, { bitLength: this.#bitLength });
|
||||
public toU8a (_isBare?: boolean): Uint8Array {
|
||||
return floatToU8a(this, {
|
||||
bitLength: this.#bitLength
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +153,6 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (_isBare?: boolean): Uint8Array {
|
||||
return Uint8Array.from(this);
|
||||
|
||||
@@ -257,7 +257,6 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (_isBare?: boolean): Uint8Array {
|
||||
return bnToU8a(this.valueEncoded, {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2023 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Registry } from '../types/index.js';
|
||||
|
||||
import { i32 } from './I32.js';
|
||||
|
||||
/**
|
||||
* @name ISize
|
||||
* @description
|
||||
* A System default signed number, typically used in RPC to report non-consensus
|
||||
* data. It is a wrapper for [[I32]] as a WASM default (as generated by Rust bindings).
|
||||
* It is not to be used, since it creates consensus mismatches.
|
||||
*/
|
||||
export class isize extends i32 {
|
||||
constructor (registry: Registry, value?: unknown) {
|
||||
super(registry, value);
|
||||
|
||||
throw new Error('The `osize` type should not be used. Since it is platform-specific, it creates incompatibilities between native (generally i64) and WASM (always i32) code. Use one of the `i32` or `i64` types explicitly.');
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ export { i32, i32 as I32 } from './I32.js';
|
||||
export { i64, i64 as I64 } from './I64.js';
|
||||
export { i128, i128 as I128 } from './I128.js';
|
||||
export { i256, i256 as I256 } from './I256.js';
|
||||
export { isize, isize as ISize } from './ISize.js';
|
||||
export { u8, u8 as U8 } from './U8.js';
|
||||
export { u16, u16 as U16 } from './U16.js';
|
||||
export { u32, u32 as U32 } from './U32.js';
|
||||
|
||||
@@ -111,7 +111,8 @@ export interface Codec {
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal use, only available on
|
||||
* some Codec types, specifically those that add encodings such as length of indexes)
|
||||
*/
|
||||
toU8a (isBare?: BareOpts): Uint8Array;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user