Revert all recent $ renames (#5429)
* Revert "[CI Skip] bump/beta 9.11.4-6-x" This reverts commit7ee4f7aaf8. * Revert "Rename Code .registry to .$registry (#5428)" This reverts commitdd6826fadd. * Revert "[CI Skip] bump/beta 9.11.4-5-x" This reverts commitabc68bf2e2. * Revert "Convert encodedLength to $encodedLength (#5426)" This reverts commitccab89304b. * Revert "[CI Skip] bump/beta 9.11.4-4-x" This reverts commit044465eee4. * Revert "Rename inspect() to inspectU8a() (#5425)" This reverts commit42af205546. * Revert "[CI Skip] bump/beta 9.11.4-3-x" This reverts commit545ad50097. * Revert "Move .isEmpty to $isEmpty (#5424)" This reverts commit6f19084a4d. * Revert "[CI Skip] bump/beta 9.11.4-2-x" This reverts commit4688291ecf. * Revert "Warn on deprecated getter usage (#5423)" This reverts commitda1cf9d64e. * Revert "[CI Skip] bump/beta 9.11.4-1-x" This reverts commit96a3ef51f0. * Revert "Revert "[CI Skip] bump/beta 9.11.4-1-x"" This reverts commit 2eca0730a630d587878f12c141e21ea631dcdd19. * Revert "Revert "[CI Skip] bump/beta 9.11.4-2-x"" This reverts commit 66284b40692071aa03a1064997c9402fc0b0987c. * Revert "Revert "[CI Skip] bump/beta 9.11.4-3-x"" This reverts commit 9b820eaefccdb7b51d893000e5480d11e5195e22. * Revert "Revert "[CI Skip] bump/beta 9.11.4-4-x"" This reverts commit cb5149adf98a5eefd8da61b8793707f85deb7c09. * Revert "Revert "[CI Skip] bump/beta 9.11.4-5-x"" This reverts commit 489a64439860fefba163695ec36b319dcb1eeddc. * Revert "Revert "[CI Skip] bump/beta 9.11.4-6-x"" This reverts commit 34a0fd4e2784a1b40ea8e06534d1f39328250e7a. * Adjust linebreaks * Adjust isStorageFallback naming * Fix build
This commit is contained in:
@@ -76,17 +76,11 @@ function parseJson (json: Record<string, unknown>, chainProperties?: ChainProper
|
||||
|
||||
export class Abi {
|
||||
readonly events: AbiEvent[];
|
||||
|
||||
readonly constructors: AbiConstructor[];
|
||||
|
||||
readonly info: ContractProjectInfo;
|
||||
|
||||
readonly json: Record<string, unknown>;
|
||||
|
||||
readonly messages: AbiMessage[];
|
||||
|
||||
readonly metadata: ContractMetadataLatest;
|
||||
|
||||
readonly registry: Registry;
|
||||
|
||||
constructor (abiJson: Record<string, unknown> | string, chainProperties?: ChainProperties) {
|
||||
@@ -239,7 +233,7 @@ export class Abi {
|
||||
return args.map(({ type: { lookupName, type } }): Codec => {
|
||||
const value = this.registry.createType(lookupName || type, data.subarray(offset));
|
||||
|
||||
offset += value.$encodedLength;
|
||||
offset += value.encodedLength;
|
||||
|
||||
return value;
|
||||
});
|
||||
|
||||
@@ -12,11 +12,9 @@ import { Abi } from '../Abi';
|
||||
|
||||
export abstract class Base<ApiType extends ApiTypes> {
|
||||
readonly abi: Abi;
|
||||
|
||||
readonly api: ApiBase<ApiType>;
|
||||
|
||||
protected readonly _decorateMethod: DecorateMethod<ApiType>;
|
||||
|
||||
protected readonly _isWeightV1: boolean;
|
||||
|
||||
constructor (api: ApiBase<ApiType>, abi: string | Record<string, unknown> | Abi, decorateMethod: DecorateMethod<ApiType>) {
|
||||
|
||||
@@ -116,7 +116,7 @@ function querySystemAccount (api: DeriveApi, accountId: AccountId): Observable<R
|
||||
|
||||
const nonce = (infoOrTuple as AccountInfo).nonce || (infoOrTuple as [Index, AccountData])[0];
|
||||
|
||||
if (!data || data.$isEmpty) {
|
||||
if (!data || data.isEmpty) {
|
||||
return [
|
||||
nonce,
|
||||
[[zeroBalance(api), zeroBalance(api), zeroBalance(api), zeroBalance(api)]]
|
||||
|
||||
@@ -38,7 +38,7 @@ export function getBlock (instanceId: string, api: DeriveApi): (hash: Uint8Array
|
||||
])
|
||||
),
|
||||
map(([signedBlock, events, [, validators, author]]) =>
|
||||
createSignedBlockExtended(events.$registry, signedBlock, events, validators, author)
|
||||
createSignedBlockExtended(events.registry, signedBlock, events, validators, author)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -35,7 +35,7 @@ export function getHeader (instanceId: string, api: DeriveApi): (blockHash: Uint
|
||||
getAuthorDetails(header, queryAt)
|
||||
),
|
||||
map(([header, validators, author]) =>
|
||||
createHeaderExtended((validators || header).$registry, header, validators, author)
|
||||
createHeaderExtended((validators || header).registry, header, validators, author)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -17,7 +17,7 @@ export function subscribeFinalizedBlocks (instanceId: string, api: DeriveApi): (
|
||||
return memo(instanceId, (): Observable<SignedBlockExtended> =>
|
||||
api.derive.chain.subscribeFinalizedHeads().pipe(
|
||||
switchMap((header) =>
|
||||
api.derive.chain.getBlock(header.$createdAtHash || header.hash)
|
||||
api.derive.chain.getBlock(header.createdAtHash || header.hash)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -41,7 +41,7 @@ export function subscribeFinalizedHeads (instanceId: string, api: DeriveApi): ()
|
||||
const endHash = prevHash;
|
||||
const startHash = header.parentHash;
|
||||
|
||||
prevHash = header.$createdAtHash = header.hash;
|
||||
prevHash = header.createdAtHash = header.hash;
|
||||
|
||||
return endHash === null || startHash.eq(endHash)
|
||||
? of(header)
|
||||
|
||||
@@ -17,7 +17,7 @@ export function subscribeNewBlocks (instanceId: string, api: DeriveApi): () => O
|
||||
return memo(instanceId, (): Observable<SignedBlockExtended> =>
|
||||
api.derive.chain.subscribeNewHeads().pipe(
|
||||
switchMap((header) =>
|
||||
api.derive.chain.getBlock(header.$createdAtHash || header.hash)
|
||||
api.derive.chain.getBlock(header.createdAtHash || header.hash)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -37,9 +37,9 @@ export function subscribeNewHeads (instanceId: string, api: DeriveApi): () => Ob
|
||||
getAuthorDetails(header, queryAt)
|
||||
),
|
||||
map(([header, validators, author]): HeaderExtended => {
|
||||
header.$createdAtHash = header.hash;
|
||||
header.createdAtHash = header.hash;
|
||||
|
||||
return createHeaderExtended(header.$registry, header, validators, author);
|
||||
return createHeaderExtended(header.registry, header, validators, author);
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
@@ -33,7 +33,7 @@ function _getUpdates (api: DeriveApi, paraId: string | number | BN): Observable<
|
||||
added = added.concat(...changes.added);
|
||||
removed = removed.concat(...changes.removed);
|
||||
|
||||
return of({ added, addedDelta: changes.added, blockHash: events.$createdAtHash?.toHex() || '-', removed, removedDelta: changes.removed });
|
||||
return of({ added, addedDelta: changes.added, blockHash: events.createdAtHash?.toHex() || '-', removed, removedDelta: changes.removed });
|
||||
}
|
||||
|
||||
return EMPTY;
|
||||
@@ -52,7 +52,7 @@ function _eventTriggerAll (api: DeriveApi, paraId: string | number | BN): Observ
|
||||
);
|
||||
|
||||
return items.length
|
||||
? of(events.$createdAtHash?.toHex() || '-')
|
||||
? of(events.createdAtHash?.toHex() || '-')
|
||||
: EMPTY;
|
||||
}),
|
||||
startWith('-')
|
||||
|
||||
@@ -29,5 +29,5 @@ export function extractContributed (paraId: string | number | BN, events: Vec<Fr
|
||||
}
|
||||
|
||||
return result;
|
||||
}, { added, blockHash: events.$createdAtHash?.toHex() || '-', removed });
|
||||
}, { added, blockHash: events.createdAtHash?.toHex() || '-', removed });
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ function isNewDepositors (depositors: ITuple<[Vec<AccountId>, Balance]> | ITuple
|
||||
function parse ([proposals, images, optDepositors]: Result): DeriveProposal[] {
|
||||
return proposals
|
||||
.filter(([, , proposer], index): boolean =>
|
||||
!!(optDepositors[index]?.isSome) && !proposer.$isEmpty
|
||||
!!(optDepositors[index]?.isSome) && !proposer.isEmpty
|
||||
)
|
||||
.map(([index, hash, proposer], proposalIndex): DeriveProposal => {
|
||||
const depositors = optDepositors[proposalIndex].unwrap();
|
||||
|
||||
@@ -19,7 +19,7 @@ function mapResult ([result, validators, heartbeats, numBlocks]: [DeriveHeartbea
|
||||
validators.forEach((validator, index): void => {
|
||||
const validatorId = validator.toString();
|
||||
const blockCount = numBlocks[index];
|
||||
const hasMessage = !heartbeats[index].$isEmpty;
|
||||
const hasMessage = !heartbeats[index].isEmpty;
|
||||
const prev = result[validatorId];
|
||||
|
||||
if (!prev || prev.hasMessage !== hasMessage || !prev.blockCount.eq(blockCount)) {
|
||||
|
||||
@@ -37,7 +37,7 @@ function signingHeader (api: DeriveApi): Observable<Header> {
|
||||
switchMap((header) =>
|
||||
// check for chains at genesis (until block 1 is produced, e.g. 6s), since
|
||||
// we do need to allow transactions at chain start (also dev/seal chains)
|
||||
header.parentHash.$isEmpty
|
||||
header.parentHash.isEmpty
|
||||
? of(header)
|
||||
// in the case of the current block, we use the parent to minimize the
|
||||
// impact of forks on the system, but not completely remove it
|
||||
|
||||
@@ -18,7 +18,7 @@ export function createHeaderExtended (registry: Registry, header?: Header, valid
|
||||
super(registry, header);
|
||||
|
||||
this.#author = author || extractAuthor(this.digest, validators || []);
|
||||
this.$createdAtHash = header?.$createdAtHash;
|
||||
this.createdAtHash = header?.createdAtHash;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,7 +46,7 @@ export function createSignedBlockExtended (registry: Registry, block?: SignedBlo
|
||||
this.#author = author || extractAuthor(this.block.header.digest, validators || []);
|
||||
this.#events = events || ([] as EventRecord[]);
|
||||
this.#extrinsics = mapExtrinsics(this.block.extrinsics, this.#events);
|
||||
this.$createdAtHash = block?.$createdAtHash;
|
||||
this.createdAtHash = block?.createdAtHash;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -712,7 +712,7 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
decorated.sizeAt = decorateMethod((blockHash: Hash | Uint8Array | string, ...args: unknown[]): Observable<u64> =>
|
||||
getQueryAt(blockHash).pipe(
|
||||
switchMap((q) =>
|
||||
this._rpcCore.state.getStorageSize(getArgs(args, q.creator.meta.$registry), blockHash))));
|
||||
this._rpcCore.state.getStorageSize(getArgs(args, q.creator.meta.registry), blockHash))));
|
||||
|
||||
// .keys() & .entries() only available on map types
|
||||
if (creator.iterKey && creator.meta.type.isMap) {
|
||||
|
||||
@@ -196,7 +196,7 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
|
||||
: await firstValueFrom(this._rpcCore.chain.getHeader.raw(blockHash))
|
||||
);
|
||||
|
||||
if (header.parentHash.$isEmpty) {
|
||||
if (header.parentHash.isEmpty) {
|
||||
throw new Error('Unable to retrieve header and parent from supplied hash');
|
||||
}
|
||||
|
||||
|
||||
@@ -160,7 +160,7 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, blockHas
|
||||
first(),
|
||||
switchMap((signingInfo): Observable<RuntimeDispatchInfo> => {
|
||||
// setup our options (same way as in signAndSend)
|
||||
const eraOptions = makeEraOptions(api, this.$registry, allOptions, signingInfo);
|
||||
const eraOptions = makeEraOptions(api, this.registry, allOptions, signingInfo);
|
||||
const signOptions = makeSignOptions(api, eraOptions, {});
|
||||
const u8a = this.isSigned
|
||||
? api.tx(this).signFake(address, signOptions).toU8a()
|
||||
@@ -244,7 +244,7 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, blockHas
|
||||
return api.derive.tx.signingInfo(address, options.nonce, options.era).pipe(
|
||||
first(),
|
||||
mergeMap(async (signingInfo): Promise<UpdateInfo> => {
|
||||
const eraOptions = makeEraOptions(api, this.$registry, options, signingInfo);
|
||||
const eraOptions = makeEraOptions(api, this.registry, options, signingInfo);
|
||||
let updateId = -1;
|
||||
|
||||
if (isKeyringPair(account)) {
|
||||
@@ -316,7 +316,7 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, blockHas
|
||||
throw new Error('No signer specified, either via api.setSigner or via sign options. You possibly need to pass through an explicit keypair for the origin so it can be used for signing.');
|
||||
}
|
||||
|
||||
const payload = this.$registry.createTypeUnsafe<SignerPayload>('SignerPayload', [objectSpread({}, options, {
|
||||
const payload = this.registry.createTypeUnsafe<SignerPayload>('SignerPayload', [objectSpread({}, options, {
|
||||
address,
|
||||
blockNumber: header ? header.number : 0,
|
||||
method: this.method
|
||||
|
||||
@@ -33,7 +33,7 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
||||
}, {});
|
||||
const { $registry: registry, lookup, pallets } = meta.asLatest;
|
||||
const { lookup, pallets, registry } = meta.asLatest;
|
||||
|
||||
const modules = pallets
|
||||
.filter(({ constants }) => constants.length > 0)
|
||||
|
||||
@@ -77,7 +77,7 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
||||
}, {});
|
||||
const { $registry: registry, lookup, pallets } = meta.asLatest;
|
||||
const { lookup, pallets, registry } = meta.asLatest;
|
||||
const modules = pallets
|
||||
.filter(({ events }) => events.isSome)
|
||||
.map(({ events, name }) => ({
|
||||
|
||||
@@ -240,7 +240,7 @@ function generateRegistry (registry: Registry, filtered: [PortableType, TypeDef]
|
||||
|
||||
function generateLookup (destDir: string, entries: [string, HexString][]): void {
|
||||
entries.reduce<string[]>((exclude, [subPath, staticMeta]): string[] => {
|
||||
const { $registry: registry, lookup } = initMeta(staticMeta).metadata.asLatest;
|
||||
const { lookup, registry } = initMeta(staticMeta).metadata.asLatest;
|
||||
const filtered = getFilteredTypes(lookup, exclude);
|
||||
|
||||
generateLookupDefs(registry, filtered, destDir, subPath);
|
||||
|
||||
@@ -83,7 +83,7 @@ export function generateCallTypes (meta: Metadata, dest: string, extraTypes: Ext
|
||||
|
||||
// find the system.Version in metadata
|
||||
let apis: Apis | null = null;
|
||||
const { $registry: registry, pallets } = meta.asLatest;
|
||||
const { pallets, registry } = meta.asLatest;
|
||||
const sysp = pallets.find(({ name }) => name.eq('System'));
|
||||
|
||||
if (sysp) {
|
||||
|
||||
@@ -232,7 +232,7 @@ function addConstants ({ lookup, pallets }: MetadataLatest): string {
|
||||
description: DESC_CONSTANTS,
|
||||
sections: pallets
|
||||
.sort(sortByName)
|
||||
.filter(({ constants }) => !constants.$isEmpty)
|
||||
.filter(({ constants }) => !constants.isEmpty)
|
||||
.map(({ constants, name }) => {
|
||||
const sectionName = stringLowerFirst(name);
|
||||
|
||||
@@ -256,7 +256,7 @@ function addConstants ({ lookup, pallets }: MetadataLatest): string {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function addStorage ({ $registry: registry, lookup, pallets }: MetadataLatest): string {
|
||||
function addStorage ({ lookup, pallets, registry }: MetadataLatest): string {
|
||||
const { substrate } = getSubstrateStorage(registry);
|
||||
const moduleSections = pallets
|
||||
.sort(sortByName)
|
||||
|
||||
@@ -67,10 +67,10 @@ export function getSimilarTypes (registry: Registry, definitions: Record<string,
|
||||
}
|
||||
}
|
||||
} else if (isChildClass(Enum, Clazz)) {
|
||||
const { $isBasic, defKeys } = new (Clazz as Constructor)(registry) as Enum;
|
||||
const { defKeys, isBasic } = new (Clazz as Constructor)(registry) as Enum;
|
||||
const keys = defKeys.filter((v) => !v.startsWith('__Unused'));
|
||||
|
||||
if ($isBasic) {
|
||||
if (isBasic) {
|
||||
possibleTypes.push(arrayToStrType(keys), 'number');
|
||||
} else {
|
||||
// TODO We don't really want any here, these should be expanded
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types';
|
||||
|
||||
import { compactToU8a, u8aConcatStrict, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { compareArray, warnGet } from '../utils';
|
||||
import { compareArray } from '../utils/compareArray';
|
||||
|
||||
/**
|
||||
* @name AbstractArray
|
||||
@@ -16,10 +16,11 @@ import { compareArray, warnGet } from '../utils';
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
/**
|
||||
* @description This ensures that operators such as clice, filter, map, etc. return
|
||||
@@ -32,44 +33,19 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
protected constructor (registry: Registry, length: number) {
|
||||
super(length);
|
||||
|
||||
this.$registry = registry;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
// We need to loop through all entries since they may have a variable length themselves,
|
||||
// e.g. when a Vec or Compact is contained withing, it has a variable length based on data
|
||||
let total = compactToU8a(this.length).length;
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
total += this[i].$encodedLength;
|
||||
total += this[i].encodedLength;
|
||||
}
|
||||
|
||||
return total;
|
||||
@@ -79,13 +55,13 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.length === 0;
|
||||
}
|
||||
|
||||
@@ -107,7 +83,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
inner: this.inspectInner(),
|
||||
outer: [compactToU8a(this.length)]
|
||||
@@ -118,7 +94,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
const inner = new Array<Inspect>(this.length);
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
inner[i] = this[i].inspectU8a();
|
||||
inner[i] = this[i].inspect();
|
||||
}
|
||||
|
||||
return inner;
|
||||
|
||||
@@ -4,55 +4,29 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
|
||||
/**
|
||||
* @name Base
|
||||
* @description A type extends the Base class, when it holds a value
|
||||
*/
|
||||
export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #raw: T;
|
||||
|
||||
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
|
||||
this.$initialU8aLength = initialU8aLength;
|
||||
this.initialU8aLength = initialU8aLength;
|
||||
this.#raw = value;
|
||||
this.$registry = registry;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
@@ -60,7 +34,7 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
public get inner (): T {
|
||||
@@ -70,8 +44,8 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
return this.#raw.$isEmpty;
|
||||
public get isEmpty (): boolean {
|
||||
return this.#raw.isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,8 +58,8 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
return this.#raw.inspectU8a();
|
||||
public inspect (): Inspect {
|
||||
return this.#raw.inspect();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,8 +6,6 @@ import type { AnyNumber, Inspect, INumber, IU8a, Registry, UIntBitLength } from
|
||||
|
||||
import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
|
||||
export const DEFAULT_UINT_BITS = 64;
|
||||
|
||||
// Maximum allowed integer for JS is 2^53 - 1, set limit at 52
|
||||
@@ -74,12 +72,13 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<strin
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export abstract class AbstractInt extends BN implements INumber {
|
||||
public $createdAtHash?: IU8a;
|
||||
readonly $encodedLength: number;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
readonly $isUnsigned: boolean;
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
readonly encodedLength: number;
|
||||
readonly isUnsigned: boolean;
|
||||
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #bitLength: UIntBitLength;
|
||||
|
||||
@@ -96,10 +95,11 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
: decodeAbstractInt(value, isSigned)
|
||||
);
|
||||
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
this.#bitLength = bitLength;
|
||||
this.$encodedLength = this.$initialU8aLength = this.#bitLength / 8;
|
||||
this.$isUnsigned = !isSigned;
|
||||
this.encodedLength = this.#bitLength / 8;
|
||||
this.initialU8aLength = this.#bitLength / 8;
|
||||
this.isUnsigned = !isSigned;
|
||||
|
||||
const isNegative = this.isNeg();
|
||||
const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
|
||||
@@ -111,47 +111,17 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
}
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isUnsigned instead. This getter will be removed in a future version */
|
||||
public get isUnsigned (): boolean {
|
||||
return warnGet(this, 'isUnsigned');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is a zero value (align elsewhere)
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.isZero();
|
||||
}
|
||||
|
||||
@@ -171,7 +141,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
// number and BN inputs (no `.eqn` needed) - numbers will be converted
|
||||
return super.eq(
|
||||
isHex(other)
|
||||
? hexToBn(other.toString(), { isLe: false, isNegative: !this.$isUnsigned })
|
||||
? hexToBn(other.toString(), { isLe: false, isNegative: !this.isUnsigned })
|
||||
: bnToBn(other as string)
|
||||
);
|
||||
}
|
||||
@@ -179,7 +149,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
outer: [this.toU8a()]
|
||||
};
|
||||
@@ -216,7 +186,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
return bnToHex(this, {
|
||||
bitLength: this.bitLength(),
|
||||
isLe,
|
||||
isNegative: !this.$isUnsigned
|
||||
isNegative: !this.isUnsigned
|
||||
});
|
||||
}
|
||||
|
||||
@@ -232,7 +202,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
? 'everything'
|
||||
// FIXME In the case of multiples we need some way of detecting which instance this belongs
|
||||
// to. as it stands we will always format (incorrectly) against the first token defined
|
||||
: formatBalance(this, { decimals: this.$registry.chainDecimals[0], withSi: true, withUnit: this.$registry.chainTokens[0] });
|
||||
: formatBalance(this, { decimals: this.registry.chainDecimals[0], withSi: true, withUnit: this.registry.chainTokens[0] });
|
||||
}
|
||||
|
||||
const [, divisor] = FORMATTERS.find(([type]) => type === rawType) || [];
|
||||
@@ -271,9 +241,9 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
// NOTE In the case of balances, which have a special meaning on the UI
|
||||
// and can be interpreted differently, return a specific value for it so
|
||||
// underlying it always matches (no matter which length it actually is)
|
||||
return this instanceof this.$registry.createClassUnsafe('Balance')
|
||||
return this instanceof this.registry.createClassUnsafe('Balance')
|
||||
? 'Balance'
|
||||
: `${this.$isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
|
||||
: `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -294,7 +264,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
return bnToU8a(this, {
|
||||
bitLength: this.bitLength(),
|
||||
isLe: true,
|
||||
isNegative: !this.$isUnsigned
|
||||
isNegative: !this.isUnsigned
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,18 +12,6 @@ const CompactU32 = Compact.with(U32);
|
||||
describe('Compact', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
describe('@deprecated', (): void => {
|
||||
it('warns on createdAtHash usage', () => {
|
||||
// eslint-disable-next-line deprecation/deprecation
|
||||
expect(new Compact(registry, 'u128').createdAtHash).not.toBeDefined();
|
||||
});
|
||||
|
||||
it('warns on initialU8aLength usage', () => {
|
||||
// eslint-disable-next-line deprecation/deprecation
|
||||
expect(new Compact(registry, 'u128').initialU8aLength).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('constructor', (): void => {
|
||||
it('fails on > MAX_SAFE_INTEGER', (): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
|
||||
@@ -44,19 +32,19 @@ describe('Compact', (): void => {
|
||||
|
||||
it('has the correct encodedLength for constructor values (string BlockNumber)', (): void => {
|
||||
expect(
|
||||
new (Compact.with('BlockNumber'))(registry, 0xfffffff9).$encodedLength
|
||||
new (Compact.with('BlockNumber'))(registry, 0xfffffff9).encodedLength
|
||||
).toEqual(5);
|
||||
});
|
||||
|
||||
it('has the correct encodedLength for constructor values (class BlockNumber)', (): void => {
|
||||
expect(
|
||||
new (Compact.with(registry.createClass('BlockNumber')))(registry, 0xfffffff9).$encodedLength
|
||||
new (Compact.with(registry.createClass('BlockNumber')))(registry, 0xfffffff9).encodedLength
|
||||
).toEqual(5);
|
||||
});
|
||||
|
||||
it('has the correct encodedLength for constructor values (u32)', (): void => {
|
||||
expect(
|
||||
new (Compact.with(U32))(registry, 0xffff9).$encodedLength
|
||||
new (Compact.with(U32))(registry, 0xffff9).encodedLength
|
||||
).toEqual(4);
|
||||
});
|
||||
|
||||
@@ -98,7 +86,7 @@ describe('Compact', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new (Compact.with(U32))(registry, 0xffff).inspectU8a()
|
||||
new (Compact.with(U32))(registry, 0xffff).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([254, 255, 3, 0])]
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { AnyJson, AnyNumber, CodecClass, ICompact, Inspect, INumber, IU8a,
|
||||
|
||||
import { compactFromU8a, compactFromU8aLim, compactToU8a, isU8a } from '@polkadot/util';
|
||||
|
||||
import { typeToConstructor, warnGet } from '../utils';
|
||||
import { typeToConstructor } from '../utils';
|
||||
|
||||
interface Options<T> {
|
||||
definition?: CodecClass<T>;
|
||||
@@ -47,49 +47,25 @@ function decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<
|
||||
* a number and making the compact representation thereof
|
||||
*/
|
||||
export class Compact<T extends INumber> implements ICompact<T> {
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #Type: CodecClass<T>;
|
||||
readonly #raw: T;
|
||||
|
||||
constructor (registry: Registry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0, { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
|
||||
|
||||
const [raw, decodedLength] = decodeCompact<T>(registry, this.#Type, value);
|
||||
|
||||
this.$initialU8aLength = decodedLength;
|
||||
this.initialU8aLength = decodedLength;
|
||||
this.#raw = raw;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
public static with<O extends INumber> (Type: CodecClass<O> | string): CodecClass<Compact<O>> {
|
||||
let definition: CodecClass<O> | undefined;
|
||||
|
||||
@@ -107,7 +83,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
@@ -115,14 +91,14 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
return this.#raw.$isEmpty;
|
||||
public get isEmpty (): boolean {
|
||||
return this.#raw.isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +122,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
outer: [this.toU8a()]
|
||||
};
|
||||
@@ -205,7 +181,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return `Compact<${this.$registry.getClassName(this.#Type) || this.#raw.toRawType()}>`;
|
||||
return `Compact<${this.registry.getClassName(this.#Type) || this.#raw.toRawType()}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,53 +4,26 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
|
||||
/**
|
||||
* @name DoNotConstruct
|
||||
* @description
|
||||
* An unknown type that fails on construction with the type info
|
||||
*/
|
||||
export class DoNotConstruct implements Codec {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
#neverError: Error;
|
||||
|
||||
constructor (registry: Registry, typeName = 'DoNotConstruct') {
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
this.#neverError = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`);
|
||||
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
public static with (typeName?: string): CodecClass {
|
||||
return class extends DoNotConstruct {
|
||||
constructor (registry: Registry) {
|
||||
@@ -62,7 +35,7 @@ export class DoNotConstruct implements Codec {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
@@ -76,7 +49,7 @@ export class DoNotConstruct implements Codec {
|
||||
/**
|
||||
* @description Checks if the value is an empty value (always true)
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
@@ -84,7 +57,7 @@ export class DoNotConstruct implements Codec {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
|
||||
@@ -293,7 +293,7 @@ describe('Enum', (): void => {
|
||||
it('has isNone set, with correct index (i.e. no values are used)', (): void => {
|
||||
const test = new Enum(registry, ['foo', 'bar'], 1);
|
||||
|
||||
expect(test.$isNone).toBe(true);
|
||||
expect(test.isNone).toBe(true);
|
||||
expect(test.index).toEqual(1);
|
||||
});
|
||||
|
||||
@@ -303,7 +303,7 @@ describe('Enum', (): void => {
|
||||
registry,
|
||||
{ Text, U32 },
|
||||
'0x0134120000'
|
||||
).inspectU8a()
|
||||
).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0x01]), new Uint8Array([0x34, 0x12, 0x00, 0x00])]
|
||||
});
|
||||
@@ -430,7 +430,7 @@ describe('Enum', (): void => {
|
||||
const test = new Test(registry, 123, 1);
|
||||
|
||||
expect(test.toHex()).toEqual('0x017b000000');
|
||||
expect(test.$encodedLength).toEqual(1 + 4);
|
||||
expect(test.encodedLength).toEqual(1 + 4);
|
||||
});
|
||||
|
||||
it('encodes a single entry correctly', (): void => {
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, CodecClass, IEnum, Inspect, IU8a, Registry } from
|
||||
|
||||
import { isHex, isNumber, isObject, isString, isU8a, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { mapToTypeMap, typesToMap, warnGet } from '../utils';
|
||||
import { mapToTypeMap, typesToMap } from '../utils';
|
||||
import { Null } from './Null';
|
||||
|
||||
// export interface, this is used in Enum.with, so required as public by TS
|
||||
@@ -183,16 +183,16 @@ function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?:
|
||||
* an extension to enum where the value type is determined by the actual index.
|
||||
*/
|
||||
export class Enum implements IEnum {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
readonly $isBasic: boolean;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #def: TypesDef;
|
||||
readonly #entryIndex: number;
|
||||
readonly #indexes: number[];
|
||||
readonly #isBasic: boolean;
|
||||
readonly #isIndexed: boolean;
|
||||
readonly #raw: Codec;
|
||||
|
||||
@@ -204,55 +204,19 @@ export class Enum implements IEnum {
|
||||
? createFromU8a(registry, def, value[0], value.subarray(1))
|
||||
: decodeEnum(registry, def, value, index);
|
||||
|
||||
this.$isBasic = isBasic;
|
||||
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
this.#def = def;
|
||||
this.#isBasic = isBasic;
|
||||
this.#isIndexed = isIndexed;
|
||||
this.#indexes = Object.values(def).map(({ index }) => index);
|
||||
this.#entryIndex = this.#indexes.indexOf(decoded.index);
|
||||
this.#raw = decoded.value;
|
||||
|
||||
if (this.#raw.$initialU8aLength) {
|
||||
this.$initialU8aLength = 1 + this.#raw.$initialU8aLength;
|
||||
if (this.#raw.initialU8aLength) {
|
||||
this.initialU8aLength = 1 + this.#raw.initialU8aLength;
|
||||
}
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isBasic instead. This getter will be removed in a future version */
|
||||
public get isBasic (): boolean {
|
||||
return warnGet(this, 'isBasic');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isNone instead. This getter will be removed in a future version */
|
||||
public get isNone (): boolean {
|
||||
return warnGet(this, 'isNone');
|
||||
}
|
||||
|
||||
public static with (Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum> {
|
||||
let definition: Definition | undefined;
|
||||
|
||||
@@ -297,15 +261,15 @@ export class Enum implements IEnum {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
return 1 + this.#raw.$encodedLength;
|
||||
public get encodedLength (): number {
|
||||
return 1 + this.#raw.encodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -322,17 +286,24 @@ export class Enum implements IEnum {
|
||||
return this.#raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description true if this is a basic enum (no values)
|
||||
*/
|
||||
public get isBasic (): boolean {
|
||||
return this.#isBasic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
return this.#raw.$isEmpty;
|
||||
public get isEmpty (): boolean {
|
||||
return this.#raw.isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the Enum points to a [[Null]] type
|
||||
*/
|
||||
public get $isNone (): boolean {
|
||||
public get isNone (): boolean {
|
||||
return this.#raw instanceof Null;
|
||||
}
|
||||
|
||||
@@ -373,7 +344,7 @@ export class Enum implements IEnum {
|
||||
return !this.toU8a().some((entry, index) => entry !== other[index]);
|
||||
} else if (isNumber(other)) {
|
||||
return this.toNumber() === other;
|
||||
} else if (this.$isBasic && isString(other)) {
|
||||
} else if (this.#isBasic && isString(other)) {
|
||||
return this.type === other;
|
||||
} else if (isHex(other)) {
|
||||
return this.toHex() === other;
|
||||
@@ -390,12 +361,12 @@ export class Enum implements IEnum {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
if (this.$isBasic) {
|
||||
public inspect (): Inspect {
|
||||
if (this.#isBasic) {
|
||||
return { outer: [new Uint8Array([this.index])] };
|
||||
}
|
||||
|
||||
const { inner, outer = [] } = this.#raw.inspectU8a();
|
||||
const { inner, outer = [] } = this.#raw.inspect();
|
||||
|
||||
return {
|
||||
inner,
|
||||
@@ -414,7 +385,7 @@ export class Enum implements IEnum {
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public toHuman (isExtended?: boolean): AnyJson {
|
||||
return this.$isBasic || this.$isNone
|
||||
return this.#isBasic || this.isNone
|
||||
? this.type
|
||||
: { [this.type]: this.#raw.toHuman(isExtended) };
|
||||
}
|
||||
@@ -423,7 +394,7 @@ export class Enum implements IEnum {
|
||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||
*/
|
||||
public toJSON (): AnyJson {
|
||||
return this.$isBasic
|
||||
return this.#isBasic
|
||||
? this.type
|
||||
: { [stringCamelCase(this.type)]: this.#raw.toJSON() };
|
||||
}
|
||||
@@ -439,7 +410,7 @@ export class Enum implements IEnum {
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
return this.$isBasic
|
||||
return this.#isBasic
|
||||
? this.type
|
||||
: { [stringCamelCase(this.type)]: this.#raw.toPrimitive() };
|
||||
}
|
||||
@@ -448,7 +419,7 @@ export class Enum implements IEnum {
|
||||
* @description Returns a raw struct representation of the enum types
|
||||
*/
|
||||
protected _toRawStruct (): string[] | Record<string, string | number> {
|
||||
if (this.$isBasic) {
|
||||
if (this.#isBasic) {
|
||||
return this.#isIndexed
|
||||
? this.defKeys.reduce((out: Record<string, number>, key, index): Record<string, number> => {
|
||||
out[key] = this.#indexes[index];
|
||||
@@ -460,7 +431,7 @@ export class Enum implements IEnum {
|
||||
|
||||
const entries = Object.entries(this.#def);
|
||||
|
||||
return typesToMap(this.$registry, entries.reduce<[CodecClass[], string[]]>((out, [key, { Type }], i) => {
|
||||
return typesToMap(this.registry, entries.reduce<[CodecClass[], string[]]>((out, [key, { Type }], i) => {
|
||||
out[0][i] = Type;
|
||||
out[1][i] = key;
|
||||
|
||||
@@ -479,7 +450,7 @@ export class Enum implements IEnum {
|
||||
* @description Returns the string representation of the value
|
||||
*/
|
||||
public toString (): string {
|
||||
return this.$isNone
|
||||
return this.isNone
|
||||
? this.type
|
||||
: stringify(this.toJSON());
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ describe('Int', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Int(registry, '0x12', 16).inspectU8a()
|
||||
new Int(registry, '0x12', 16).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0x12, 0x00])]
|
||||
});
|
||||
|
||||
@@ -26,7 +26,7 @@ describe('Null', (): void => {
|
||||
});
|
||||
|
||||
it('isEmpty', (): void => {
|
||||
expect(new Null(registry).$isEmpty).toBe(true);
|
||||
expect(new Null(registry).isEmpty).toBe(true);
|
||||
});
|
||||
|
||||
it('has an empty hex', (): void => {
|
||||
|
||||
@@ -6,48 +6,22 @@ import type { Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { isNull } from '@polkadot/util';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
|
||||
/**
|
||||
* @name Null
|
||||
* @description
|
||||
* Implements a type that does not contain anything (apart from `null`)
|
||||
*/
|
||||
export class Null implements Codec {
|
||||
public $createdAtHash?: IU8a;
|
||||
readonly $encodedLength = 0;
|
||||
public $initialU8aLength = 0;
|
||||
public $isStorageFallback?: boolean;
|
||||
readonly $isEmpty = true;
|
||||
readonly $registry: Registry;
|
||||
readonly encodedLength = 0;
|
||||
readonly isEmpty = true;
|
||||
readonly registry: Registry;
|
||||
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength = 0;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
constructor (registry: Registry) {
|
||||
this.$registry = registry;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +41,7 @@ export class Null implements Codec {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ describe('Option', (): void => {
|
||||
].forEach((test): void => {
|
||||
expect(test.isSome).toBe(true);
|
||||
expect(test.isNone).toBe(false);
|
||||
expect(test.$isEmpty).toBe(false);
|
||||
expect(test.isEmpty).toBe(false);
|
||||
expect(test.toU8a()).toEqual(new Uint8Array([1]));
|
||||
expect(test.unwrap().toHex()).toEqual('0x');
|
||||
});
|
||||
@@ -198,7 +198,7 @@ describe('Option', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Option(registry, U32, '1234').inspectU8a()
|
||||
new Option(registry, U32, '1234').inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0x01]), new Uint8Array([210, 4, 0, 0])]
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, CodecClass, Inspect, IOption, IU8a, Registry } fro
|
||||
|
||||
import { isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { typeToConstructor, warnGet } from '../utils';
|
||||
import { typeToConstructor } from '../utils';
|
||||
import { Null } from './Null';
|
||||
|
||||
interface Options<T> {
|
||||
@@ -66,11 +66,11 @@ function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): C
|
||||
* with a value if/as required/found.
|
||||
*/
|
||||
export class Option<T extends Codec> implements IOption<T> {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #Type: CodecClass<T>;
|
||||
readonly #raw: T;
|
||||
@@ -83,40 +83,15 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
: new Type(registry, value.subarray(1))
|
||||
: decodeOption(registry, Type, value);
|
||||
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
this.#Type = Type;
|
||||
this.#raw = decoded as T;
|
||||
|
||||
if (decoded?.$initialU8aLength) {
|
||||
this.$initialU8aLength = 1 + decoded.$initialU8aLength;
|
||||
if (decoded?.initialU8aLength) {
|
||||
this.initialU8aLength = 1 + decoded.initialU8aLength;
|
||||
}
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
public static with<O extends Codec> (Type: CodecClass<O> | string): CodecClass<Option<O>> {
|
||||
let definition: CodecClass<O> | undefined;
|
||||
|
||||
@@ -136,22 +111,22 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
// boolean byte (has value, doesn't have) along with wrapped length
|
||||
return 1 + this.#raw.$encodedLength;
|
||||
return 1 + this.#raw.encodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the Option has no value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.isNone;
|
||||
}
|
||||
|
||||
@@ -190,12 +165,12 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
if (this.isNone) {
|
||||
return { outer: [new Uint8Array([0])] };
|
||||
}
|
||||
|
||||
const { inner, outer = [] } = this.#raw.inspectU8a();
|
||||
const { inner, outer = [] } = this.#raw.inspect();
|
||||
|
||||
return {
|
||||
inner,
|
||||
@@ -243,7 +218,7 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (isBare?: boolean): string {
|
||||
const wrapped = this.$registry.getClassName(this.#Type) || new this.#Type(this.$registry).toRawType();
|
||||
const wrapped = this.registry.getClassName(this.#Type) || new this.#Type(this.registry).toRawType();
|
||||
|
||||
return isBare
|
||||
? wrapped
|
||||
@@ -266,7 +241,7 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
return this.#raw.toU8a(true);
|
||||
}
|
||||
|
||||
const u8a = new Uint8Array(this.$encodedLength);
|
||||
const u8a = new Uint8Array(this.encodedLength);
|
||||
|
||||
if (this.isSome) {
|
||||
u8a.set([1]);
|
||||
@@ -304,6 +279,6 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
public unwrapOrDefault (): T {
|
||||
return this.isSome
|
||||
? this.unwrap()
|
||||
: new this.#Type(this.$registry);
|
||||
: new this.#Type(this.registry);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ export class Result<O extends Codec, E extends Codec> extends Enum implements IR
|
||||
* @description Checks if the Result has no value
|
||||
*/
|
||||
public override get isEmpty (): boolean {
|
||||
return this.isOk && this.value.$isEmpty;
|
||||
return this.isOk && this.value.isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -50,7 +50,7 @@ describe('Tuple', (): void => {
|
||||
const INPUT = '0xcc0200000000';
|
||||
const test = registry.createType('(u32, [u32; 0], u16)', INPUT);
|
||||
|
||||
expect(test.$encodedLength).toEqual(4 + 0 + 2);
|
||||
expect(test.encodedLength).toEqual(4 + 0 + 2);
|
||||
expect(test.toHex()).toEqual(INPUT);
|
||||
});
|
||||
});
|
||||
@@ -123,7 +123,7 @@ describe('Tuple', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
tuple.inspectU8a()
|
||||
tuple.inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{ outer: [new Uint8Array([7 << 2]), stringToU8a('bazzing')] },
|
||||
|
||||
@@ -68,7 +68,7 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
|
||||
super(registry, Classes[0].length);
|
||||
|
||||
this.$initialU8aLength = (
|
||||
this.initialU8aLength = (
|
||||
isU8a(value)
|
||||
? decodeU8a(registry, this, value, Classes)
|
||||
: decodeTuple(registry, this, value, Classes)
|
||||
@@ -93,11 +93,11 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
let total = 0;
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
total += this[i].$encodedLength;
|
||||
total += this[i].encodedLength;
|
||||
}
|
||||
|
||||
return total;
|
||||
@@ -109,13 +109,13 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
public get Types (): string[] {
|
||||
return this.#Types[1].length
|
||||
? this.#Types[1]
|
||||
: this.#Types[0].map((T) => new T(this.$registry).toRawType());
|
||||
: this.#Types[0].map((T) => new T(this.registry).toRawType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public override inspectU8a (): Inspect {
|
||||
public override inspect (): Inspect {
|
||||
return {
|
||||
inner: this.inspectInner()
|
||||
};
|
||||
@@ -126,7 +126,7 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
*/
|
||||
public toRawType (): string {
|
||||
const types = this.#Types[0].map((T) =>
|
||||
this.$registry.getClassName(T) || new T(this.$registry).toRawType()
|
||||
this.registry.getClassName(T) || new T(this.registry).toRawType()
|
||||
);
|
||||
|
||||
return `(${types.join(',')})`;
|
||||
|
||||
@@ -127,7 +127,7 @@ describe('UInt', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new UInt(registry, '0x12', 16).inspectU8a()
|
||||
new UInt(registry, '0x12', 16).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0x12, 0x00])]
|
||||
});
|
||||
|
||||
@@ -185,7 +185,7 @@ describe('Vec', (): void => {
|
||||
];
|
||||
const vec = registry.createType('Vec<AccountId>', addrs);
|
||||
|
||||
expect(vec.inspectU8a()).toEqual({
|
||||
expect(vec.inspect()).toEqual({
|
||||
inner: addrs.map((a) => ({
|
||||
outer: [decodeAddress(a)]
|
||||
})),
|
||||
|
||||
@@ -80,7 +80,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
|
||||
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
|
||||
|
||||
this.$initialU8aLength = (
|
||||
this.initialU8aLength = (
|
||||
isU8a(decodeFrom)
|
||||
? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type)
|
||||
: decodeVec(registry, this, decodeFrom, startAt, this.#Type)
|
||||
@@ -115,7 +115,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
// convert type first, this removes overhead from the eq
|
||||
const other = _other instanceof this.#Type
|
||||
? _other
|
||||
: new this.#Type(this.$registry, _other);
|
||||
: new this.#Type(this.registry, _other);
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
if (other.eq(this[i])) {
|
||||
@@ -130,6 +130,6 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return `Vec<${this.$registry.getClassName(this.#Type) || new this.#Type(this.$registry).toRawType()}>`;
|
||||
return `Vec<${this.registry.getClassName(this.#Type) || new this.#Type(this.registry).toRawType()}>`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,19 +50,19 @@ describe('VecFixed', (): void => {
|
||||
it('has empty Uint8Array when length is 0', (): void => {
|
||||
const test = new (VecFixed.with(Text, 0))(registry);
|
||||
|
||||
expect(test.$encodedLength).toEqual(0);
|
||||
expect(test.encodedLength).toEqual(0);
|
||||
expect(test.toU8a()).toEqual(new Uint8Array([]));
|
||||
});
|
||||
|
||||
it('has equivalent to 1 Uint8Array when length is 1', (): void => {
|
||||
const test = new (VecFixed.with(Text, 1))(registry, ['hello']);
|
||||
|
||||
expect(test.$encodedLength).toEqual(1 + 5);
|
||||
expect(test.encodedLength).toEqual(1 + 5);
|
||||
expect(test.toU8a()).toEqual(new Uint8Array([20, 104, 101, 108, 108, 111]));
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(test.inspectU8a()).toEqual({
|
||||
expect(test.inspect()).toEqual({
|
||||
inner: [
|
||||
{ outer: [new Uint8Array([1 << 2]), stringToU8a('1')] },
|
||||
{ outer: [new Uint8Array([1 << 2]), stringToU8a('2')] },
|
||||
|
||||
@@ -32,7 +32,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
|
||||
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
|
||||
|
||||
this.$initialU8aLength = (
|
||||
this.initialU8aLength = (
|
||||
isU8a(value)
|
||||
? decodeU8aVec(registry, this, value, 0, this.#Type)
|
||||
: decodeVec(registry, this, value, 0, this.#Type)
|
||||
@@ -57,17 +57,17 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
* @description The type for the items
|
||||
*/
|
||||
public get Type (): string {
|
||||
return new this.#Type(this.$registry).toRawType();
|
||||
return new this.#Type(this.registry).toRawType();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
let total = 0;
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
total += this[i].$encodedLength;
|
||||
total += this[i].encodedLength;
|
||||
}
|
||||
|
||||
return total;
|
||||
@@ -76,7 +76,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public override inspectU8a (): Inspect {
|
||||
public override inspect (): Inspect {
|
||||
return {
|
||||
inner: this.inspectInner()
|
||||
};
|
||||
|
||||
@@ -87,7 +87,7 @@ describe('BTreeMap', (): void => {
|
||||
it('correctly encodes length', (): void => {
|
||||
expect(
|
||||
new (
|
||||
BTreeMap.with(Text, U32))(registry, mockU32TextMap).$encodedLength
|
||||
BTreeMap.with(Text, U32))(registry, mockU32TextMap).encodedLength
|
||||
).toEqual(13);
|
||||
});
|
||||
|
||||
@@ -195,7 +195,7 @@ describe('BTreeMap', (): void => {
|
||||
[new Text(registry, '1'), new Text(registry, 'foo')],
|
||||
[new Text(registry, '2'), new Text(registry, 'bar')],
|
||||
[new Text(registry, '3'), new Text(registry, 'baz')]
|
||||
])).inspectU8a()
|
||||
])).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{ outer: [new Uint8Array([1 << 2]), stringToU8a('1')] },
|
||||
|
||||
@@ -130,7 +130,7 @@ describe('BTreeSet', (): void => {
|
||||
it('correctly encodes length', (): void => {
|
||||
expect(
|
||||
new (
|
||||
BTreeSet.with(U32))(registry, mockU32Set).$encodedLength
|
||||
BTreeSet.with(U32))(registry, mockU32Set).encodedLength
|
||||
).toEqual(17);
|
||||
});
|
||||
|
||||
@@ -139,10 +139,10 @@ describe('BTreeSet', (): void => {
|
||||
|
||||
// only the length byte
|
||||
expect(none.toHex()).toEqual('0x00');
|
||||
expect(none.$encodedLength).toEqual(1);
|
||||
expect(none.encodedLength).toEqual(1);
|
||||
expect(
|
||||
(new (BTreeSet.with(U32))(registry, none.toHex())).$initialU8aLength
|
||||
).toEqual(none.$encodedLength);
|
||||
(new (BTreeSet.with(U32))(registry, none.toHex())).initialU8aLength
|
||||
).toEqual(none.encodedLength);
|
||||
});
|
||||
|
||||
it('correctly encodes/decodes filled', (): void => {
|
||||
@@ -150,10 +150,10 @@ describe('BTreeSet', (): void => {
|
||||
|
||||
// length byte + 2 values, 2 << 2 with u32 values
|
||||
expect(some.toHex()).toEqual('0x080100000002000000');
|
||||
expect(some.$encodedLength).toEqual(1 + (4 * 2));
|
||||
expect(some.encodedLength).toEqual(1 + (4 * 2));
|
||||
expect(
|
||||
(new (BTreeSet.with(U32))(registry, some.toHex())).$initialU8aLength
|
||||
).toEqual(some.$encodedLength);
|
||||
(new (BTreeSet.with(U32))(registry, some.toHex())).initialU8aLength
|
||||
).toEqual(some.encodedLength);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -209,7 +209,7 @@ describe('BTreeSet', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new (BTreeSet.with(U32))(registry, [1, 2, 3, 4]).inspectU8a()
|
||||
new (BTreeSet.with(U32))(registry, [1, 2, 3, 4]).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{ outer: [new Uint8Array([1, 0, 0, 0])] },
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, CodecClass, Inspect, ISet, IU8a, Registry } from '
|
||||
|
||||
import { compactFromU8aLim, compactToU8a, isHex, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { compareSet, decodeU8aVec, sortSet, typeToConstructor, warnGet } from '../utils';
|
||||
import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from '../utils';
|
||||
|
||||
const l = logger('BTreeSet');
|
||||
|
||||
@@ -70,11 +70,11 @@ function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V>
|
||||
}
|
||||
|
||||
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #ValClass: CodecClass<V>;
|
||||
|
||||
@@ -83,36 +83,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
|
||||
super(sortSet(values));
|
||||
|
||||
this.$registry = registry;
|
||||
this.$initialU8aLength = decodedLength;
|
||||
this.registry = registry;
|
||||
this.initialU8aLength = decodedLength;
|
||||
this.#ValClass = ValClass;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
public static with<V extends Codec> (valType: CodecClass<V> | string): CodecClass<BTreeSet<V>> {
|
||||
return class extends BTreeSet<V> {
|
||||
constructor (registry: Registry, value?: Uint8Array | string | Set<any>) {
|
||||
@@ -124,11 +99,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
let len = compactToU8a(this.size).length;
|
||||
|
||||
for (const v of this.values()) {
|
||||
len += v.$encodedLength;
|
||||
len += v.encodedLength;
|
||||
}
|
||||
|
||||
return len;
|
||||
@@ -138,13 +113,13 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
* @description Returns a hash of the value
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
@@ -165,11 +140,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
const inner = new Array<Inspect>();
|
||||
|
||||
for (const v of this.values()) {
|
||||
inner.push(v.inspectU8a());
|
||||
inner.push(v.inspect());
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -215,7 +190,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return `BTreeSet<${this.$registry.getClassName(this.#ValClass) || new this.#ValClass(this.$registry).toRawType()}>`;
|
||||
return `BTreeSet<${this.registry.getClassName(this.#ValClass) || new this.#ValClass(this.registry).toRawType()}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,7 @@ describe('BitVec', (): void => {
|
||||
|
||||
it('has the right encodedLength', (): void => {
|
||||
expect(
|
||||
bitvec.$encodedLength
|
||||
bitvec.encodedLength
|
||||
).toEqual((test.length - 2) / 2);
|
||||
});
|
||||
|
||||
@@ -48,7 +48,7 @@ describe('BitVec', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new BitVec(registry, '0x0837').inspectU8a()
|
||||
new BitVec(registry, '0x0837').inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([0]), new Uint8Array([0x08, 0x37])]
|
||||
});
|
||||
|
||||
@@ -60,14 +60,14 @@ export class BitVec extends Raw {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
return this.length + compactToU8a(this.#decodedLength).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public override inspectU8a (): Inspect {
|
||||
public override inspect (): Inspect {
|
||||
return {
|
||||
outer: [compactToU8a(this.#decodedLength), super.toU8a()]
|
||||
};
|
||||
|
||||
@@ -65,7 +65,7 @@ describe('Bytes', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Bytes(registry, '0x12345678').inspectU8a()
|
||||
new Bytes(registry, '0x12345678').inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([4 << 2]), new Uint8Array([0x12, 0x34, 0x56, 0x78])]
|
||||
});
|
||||
|
||||
@@ -50,14 +50,14 @@ export class Bytes extends Raw {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
return this.length + compactToU8a(this.length).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public override inspectU8a (isBare?: boolean): Inspect {
|
||||
public override inspect (isBare?: boolean): Inspect {
|
||||
const clength = compactToU8a(this.length);
|
||||
|
||||
return {
|
||||
|
||||
@@ -20,7 +20,7 @@ describe('HashMap', (): void => {
|
||||
new (HashMap.with(Text, Text))(registry, new Map([
|
||||
[new Text(registry, '1'), new Text(registry, 'foo')],
|
||||
[new Text(registry, '2'), new Text(registry, 'bar')]
|
||||
])).inspectU8a()
|
||||
])).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{ outer: [new Uint8Array([1 << 2]), stringToU8a('1')] },
|
||||
|
||||
@@ -55,7 +55,7 @@ export class Linkage<T extends Codec> extends Struct {
|
||||
public override toU8a (): 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
|
||||
return this.isEmpty
|
||||
? EMPTY
|
||||
: super.toU8a();
|
||||
}
|
||||
|
||||
@@ -81,10 +81,10 @@ describe('CodecMap', (): void => {
|
||||
|
||||
// only the length byte
|
||||
expect(none.toHex()).toEqual('0x00');
|
||||
expect(none.$encodedLength).toEqual(1);
|
||||
expect(none.encodedLength).toEqual(1);
|
||||
expect(
|
||||
new CodecMap(registry, Text, Text, none.toHex()).$initialU8aLength
|
||||
).toEqual(none.$encodedLength);
|
||||
new CodecMap(registry, Text, Text, none.toHex()).initialU8aLength
|
||||
).toEqual(none.encodedLength);
|
||||
});
|
||||
|
||||
it('correctly encodes/decodes filled', (): void => {
|
||||
@@ -95,10 +95,10 @@ describe('CodecMap', (): void => {
|
||||
|
||||
// length byte + 2 values, 2 << 2 with Text values
|
||||
expect(some.toHex()).toEqual('0x0804310c666f6f04320c626172');
|
||||
expect(some.$encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2));
|
||||
expect(some.encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2));
|
||||
expect(
|
||||
new CodecMap(registry, Text, Text, some.toHex()).$initialU8aLength
|
||||
).toEqual(some.$encodedLength);
|
||||
new CodecMap(registry, Text, Text, some.toHex()).initialU8aLength
|
||||
).toEqual(some.encodedLength);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -107,7 +107,7 @@ describe('CodecMap', (): void => {
|
||||
new CodecMap(registry, Text, Text, new Map([
|
||||
[new Text(registry, '1'), new Text(registry, 'foo')],
|
||||
[new Text(registry, '2'), new Text(registry, 'bar')]
|
||||
])).inspectU8a()
|
||||
])).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{ outer: [new Uint8Array([1 << 2]), stringToU8a('1')] },
|
||||
|
||||
@@ -10,7 +10,7 @@ import { AbstractArray } from '../abstract/Array';
|
||||
import { Enum } from '../base/Enum';
|
||||
import { Raw } from '../native/Raw';
|
||||
import { Struct } from '../native/Struct';
|
||||
import { compareMap, decodeU8a, sortMap, typeToConstructor, warnGet } from '../utils';
|
||||
import { compareMap, decodeU8a, sortMap, typeToConstructor } from '../utils';
|
||||
|
||||
const l = logger('Map');
|
||||
|
||||
@@ -94,11 +94,11 @@ function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyTyp
|
||||
}
|
||||
|
||||
export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements IMap<K, V> {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #KeyClass: CodecClass<K>;
|
||||
readonly #ValClass: CodecClass<V>;
|
||||
@@ -109,46 +109,21 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
|
||||
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
|
||||
|
||||
this.$registry = registry;
|
||||
this.$initialU8aLength = decodedLength;
|
||||
this.registry = registry;
|
||||
this.initialU8aLength = decodedLength;
|
||||
this.#KeyClass = KeyClass;
|
||||
this.#ValClass = ValClass;
|
||||
this.#type = type;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
let len = compactToU8a(this.size).length;
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
len += k.$encodedLength + v.$encodedLength;
|
||||
len += k.encodedLength + v.encodedLength;
|
||||
}
|
||||
|
||||
return len;
|
||||
@@ -158,13 +133,13 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
* @description Returns a hash of the value
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
@@ -178,12 +153,12 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
const inner = new Array<Inspect>();
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
inner.push(k.inspectU8a());
|
||||
inner.push(v.inspectU8a());
|
||||
inner.push(k.inspect());
|
||||
inner.push(v.inspect());
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -250,7 +225,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return `${this.#type}<${this.$registry.getClassName(this.#KeyClass) || new this.#KeyClass(this.$registry).toRawType()},${this.$registry.getClassName(this.#ValClass) || new this.#ValClass(this.$registry).toRawType()}>`;
|
||||
return `${this.#type}<${this.registry.getClassName(this.#KeyClass) || new this.#KeyClass(this.registry).toRawType()},${this.registry.getClassName(this.#ValClass) || new this.#ValClass(this.registry).toRawType()}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,7 +40,7 @@ describe('OptionBool', (): void => {
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(new OptionBool(registry, true).inspectU8a()).toEqual({
|
||||
expect(new OptionBool(registry, true).inspect()).toEqual({
|
||||
outer: [new Uint8Array([1])]
|
||||
});
|
||||
});
|
||||
|
||||
@@ -33,13 +33,13 @@ export class OptionBool extends Option<Bool> {
|
||||
: value
|
||||
);
|
||||
|
||||
this.$initialU8aLength = 1;
|
||||
this.initialU8aLength = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ export class OptionBool extends Option<Bool> {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public override inspectU8a (): Inspect {
|
||||
public override inspect (): Inspect {
|
||||
return { outer: [this.toU8a()] };
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ describe('Range', (): void => {
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(range.inspectU8a()).toEqual({
|
||||
expect(range.inspect()).toEqual({
|
||||
inner: [
|
||||
{ outer: [new Uint8Array([1, 0, 0, 0])] },
|
||||
{ outer: [new Uint8Array([2, 0, 0, 0])] }
|
||||
|
||||
@@ -106,7 +106,7 @@ describe('Type', (): void => {
|
||||
const u8a = new Type(registry, original).toU8a();
|
||||
const decoded = new Type(registry, u8a);
|
||||
|
||||
expect(decoded.$encodedLength).toEqual(original.length + 1); // extra byte for length
|
||||
expect(decoded.encodedLength).toEqual(original.length + 1); // extra byte for length
|
||||
expect(decoded.toString()).toEqual(expected);
|
||||
});
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ describe('U8aFixed', (): void => {
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(u8a.inspectU8a()).toEqual({
|
||||
expect(u8a.inspect()).toEqual({
|
||||
outer: [new Uint8Array([1, 2, 3, 4])]
|
||||
});
|
||||
});
|
||||
|
||||
@@ -52,7 +52,7 @@ describe('WrapperKeepOpaque', (): void => {
|
||||
|
||||
it('has a sane inspect (non-decodable)', (): void => {
|
||||
expect(
|
||||
new ClazzUSize(registry, u8au32).inspectU8a()
|
||||
new ClazzUSize(registry, u8au32).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([4 << 2]), new Uint8Array([0x87, 0xd6, 0x12, 0x00])]
|
||||
});
|
||||
@@ -60,7 +60,7 @@ describe('WrapperKeepOpaque', (): void => {
|
||||
|
||||
it('has a sane inspect (decodable)', (): void => {
|
||||
expect(
|
||||
new ClazzU32(registry, u8au32).inspectU8a()
|
||||
new ClazzU32(registry, u8au32).inspect()
|
||||
).toEqual({
|
||||
inner: [{ outer: [new Uint8Array([0x87, 0xd6, 0x12, 0x00])] }],
|
||||
outer: [new Uint8Array([4 << 2])]
|
||||
|
||||
@@ -70,10 +70,10 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public override inspectU8a (): Inspect {
|
||||
public override inspect (): Inspect {
|
||||
return this.#decoded
|
||||
? {
|
||||
inner: [this.#decoded.inspectU8a()],
|
||||
inner: [this.#decoded.inspect()],
|
||||
outer: [compactToU8a(this.length)]
|
||||
}
|
||||
: {
|
||||
@@ -103,7 +103,7 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public override toRawType (): string {
|
||||
return `${this.#opaqueName}<${this.$registry.getClassName(this.#Type) || (this.#decoded ? this.#decoded.toRawType() : new this.#Type(this.$registry).toRawType())}>`;
|
||||
return `${this.#opaqueName}<${this.registry.getClassName(this.#Type) || (this.#decoded ? this.#decoded.toRawType() : new this.#Type(this.registry).toRawType())}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,7 +47,7 @@ describe('WrapperOpaque', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new WrapperOpaque(registry, 'u32', '0x78563412').inspectU8a()
|
||||
new WrapperOpaque(registry, 'u32', '0x78563412').inspect()
|
||||
).toEqual({
|
||||
inner: [{ outer: [new Uint8Array([0x78, 0x56, 0x34, 0x12])] }],
|
||||
outer: [new Uint8Array([4 << 2])]
|
||||
|
||||
@@ -37,7 +37,7 @@ describe('Bool', (): void => {
|
||||
});
|
||||
|
||||
it('correctly encodes length', (): void => {
|
||||
expect(new Bool(registry, true).$encodedLength).toEqual(1);
|
||||
expect(new Bool(registry, true).encodedLength).toEqual(1);
|
||||
});
|
||||
|
||||
describe('utils', (): void => {
|
||||
@@ -58,13 +58,13 @@ describe('Bool', (): void => {
|
||||
});
|
||||
|
||||
it('has sane isEmpty aligning with the rest', (): void => {
|
||||
expect(new Bool(registry).$isEmpty).toBe(true);
|
||||
expect(new Bool(registry, false).$isEmpty).toBe(true);
|
||||
expect(new Bool(registry, true).$isEmpty).toBe(false);
|
||||
expect(new Bool(registry).isEmpty).toBe(true);
|
||||
expect(new Bool(registry, false).isEmpty).toBe(true);
|
||||
expect(new Bool(registry, true).isEmpty).toBe(false);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(new Bool(registry, true).inspectU8a()).toEqual({
|
||||
expect(new Bool(registry, true).inspect()).toEqual({
|
||||
outer: [new Uint8Array([1])]
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,8 +6,6 @@ import type { AnyBool, Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { isU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
|
||||
/**
|
||||
* @name bool
|
||||
* @description
|
||||
@@ -15,12 +13,11 @@ import { warnGet } from '../utils';
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class bool extends Boolean implements Codec {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
readonly $encodedLength = 1;
|
||||
public $initialU8aLength = 1;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength = 1;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
constructor (registry: Registry, value: bool | AnyBool | Uint8Array | number = false) {
|
||||
super(
|
||||
@@ -31,45 +28,27 @@ export class bool extends Boolean implements Codec {
|
||||
: !!value
|
||||
);
|
||||
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value (true when it wraps false/default)
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.isFalse;
|
||||
}
|
||||
|
||||
@@ -101,7 +80,7 @@ export class bool extends Boolean implements Codec {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
outer: [this.toU8a()]
|
||||
};
|
||||
|
||||
@@ -75,7 +75,7 @@ describe('Date', (): void => {
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(new CodecDate(registry, 3).inspectU8a()).toEqual({
|
||||
expect(new CodecDate(registry, 3).inspect()).toEqual({
|
||||
outer: [new Uint8Array([3, 0, 0, 0, 0, 0, 0, 0])]
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,8 +6,6 @@ import type { AnyNumber, Inspect, INumber, IU8a, Registry, UIntBitLength } from
|
||||
|
||||
import { BN, bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
|
||||
const BITLENGTH: UIntBitLength = 64;
|
||||
const U8A_OPTS = { bitLength: BITLENGTH, isLe: true };
|
||||
|
||||
@@ -36,55 +34,36 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date {
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class CodecDate extends Date implements INumber {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
readonly $encodedLength = BITLENGTH / 8;
|
||||
public $initialU8aLength = BITLENGTH / 8;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength = BITLENGTH / 8;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
|
||||
super(decodeDate(value));
|
||||
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
return BITLENGTH / 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.getTime() === 0;
|
||||
}
|
||||
|
||||
@@ -105,7 +84,7 @@ export class CodecDate extends Date implements INumber {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
outer: [this.toU8a()]
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ describe('Float', (): void => {
|
||||
|
||||
it('triggers isEmpty on 0', (): void => {
|
||||
expect(
|
||||
new F32(registry, 0).$isEmpty
|
||||
new F32(registry, 0).isEmpty
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
|
||||
@@ -6,8 +6,6 @@ import type { AnyFloat, CodecClass, IFloat, Inspect, IU8a, Registry } from '../t
|
||||
|
||||
import { floatToU8a, isHex, isU8a, u8aToFloat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
|
||||
interface Options {
|
||||
bitLength?: 32 | 64;
|
||||
}
|
||||
@@ -21,11 +19,12 @@ interface Options {
|
||||
* in some eth_* RPCs
|
||||
*/
|
||||
export class Float extends Number implements IFloat {
|
||||
public $createdAtHash?: IU8a;
|
||||
readonly $encodedLength: number;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
readonly $registry: Registry;
|
||||
readonly encodedLength: number;
|
||||
readonly registry: Registry;
|
||||
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #bitLength: 32 | 64;
|
||||
|
||||
@@ -39,34 +38,9 @@ export class Float extends Number implements IFloat {
|
||||
);
|
||||
|
||||
this.#bitLength = bitLength;
|
||||
this.$encodedLength = bitLength / 8;
|
||||
this.$initialU8aLength = this.$encodedLength;
|
||||
this.$registry = registry;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
this.encodedLength = bitLength / 8;
|
||||
this.initialU8aLength = this.encodedLength;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public static with (bitLength: 32 | 64): CodecClass<Float> {
|
||||
@@ -81,13 +55,13 @@ export class Float extends Number implements IFloat {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns true if the type wraps an empty/default all-0 value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
get isEmpty (): boolean {
|
||||
return this.valueOf() === 0;
|
||||
}
|
||||
|
||||
@@ -101,7 +75,7 @@ export class Float extends Number implements IFloat {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
outer: [this.toU8a()]
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { isFunction, objectProperties, stringify } from '@polkadot/util';
|
||||
|
||||
import { compareMap, warnGet } from '../utils';
|
||||
import { compareMap } from '../utils';
|
||||
|
||||
/** @internal */
|
||||
function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
|
||||
@@ -22,59 +22,40 @@ function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class Json extends Map<string, any> implements Codec {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
readonly $encodedLength = 0;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
constructor (registry: Registry, value?: Record<string, unknown> | null) {
|
||||
const decoded = decodeJson(value);
|
||||
|
||||
super(decoded);
|
||||
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
|
||||
objectProperties(this, decoded.map(([k]) => k), (k) => this.get(k));
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
/**
|
||||
* @description Always 0, never encodes as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return [...this.keys()].length === 0;
|
||||
}
|
||||
|
||||
@@ -95,7 +76,7 @@ export class Json extends Map<string, any> implements Codec {
|
||||
/**
|
||||
* @description Unimplemented, will throw
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
throw new Error('Unimplemented');
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ describe('Raw', (): void => {
|
||||
});
|
||||
|
||||
it('correctly encodes length', (): void => {
|
||||
expect(u8a.$encodedLength).toEqual(5);
|
||||
expect(u8a.encodedLength).toEqual(5);
|
||||
});
|
||||
|
||||
it('allows wrapping of a pre-existing instance', (): void => {
|
||||
@@ -98,7 +98,7 @@ describe('Raw', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Raw(registry, [1, 2, 3, 4, 5]).inspectU8a()
|
||||
new Raw(registry, [1, 2, 3, 4, 5]).inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([1, 2, 3, 4, 5])]
|
||||
});
|
||||
|
||||
@@ -6,8 +6,6 @@ import type { AnyJson, AnyU8a, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
|
||||
/**
|
||||
* @name Raw
|
||||
* @description
|
||||
@@ -18,11 +16,11 @@ import { warnGet } from '../utils';
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class Raw extends Uint8Array implements IU8a {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
/**
|
||||
* @description This ensures that operators such as clice, filter, map, etc. return
|
||||
@@ -35,39 +33,14 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
constructor (registry: Registry, value?: AnyU8a, initialU8aLength?: number) {
|
||||
super(u8aToU8a(value));
|
||||
|
||||
this.$registry = registry;
|
||||
this.$initialU8aLength = initialU8aLength;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
this.registry = registry;
|
||||
this.initialU8aLength = initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
@@ -75,7 +48,7 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,7 +61,7 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
/**
|
||||
* @description Returns true if the type wraps an empty/default all-0 value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return !this.length || isUndefined(this.find((b) => !!b));
|
||||
}
|
||||
|
||||
@@ -121,7 +94,7 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
outer: [this.toU8a()]
|
||||
};
|
||||
|
||||
@@ -33,7 +33,7 @@ describe('Set', (): void => {
|
||||
it('constructs via an string[]', (): void => {
|
||||
const set = new CodecSet(registry, SET_ROLES, ['full', 'authority']);
|
||||
|
||||
expect(set.$isEmpty).toEqual(false);
|
||||
expect(set.isEmpty).toEqual(false);
|
||||
expect(set.toString()).toEqual(
|
||||
'[full, authority]'
|
||||
);
|
||||
@@ -97,7 +97,7 @@ describe('Set', (): void => {
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(set.inspectU8a()).toEqual({
|
||||
expect(set.inspect()).toEqual({
|
||||
outer: [new Uint8Array([SET_ROLES.full | SET_ROLES.authority])]
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { CodecClass, Inspect, ISet, IU8a, Registry } from '../types';
|
||||
|
||||
import { BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { compareArray, warnGet } from '../utils';
|
||||
import { compareArray } from '../utils';
|
||||
|
||||
type SetValues = Record<string, number | BN>;
|
||||
|
||||
@@ -92,11 +92,11 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
|
||||
* a bitwise representation of the values.
|
||||
*/
|
||||
export class CodecSet extends Set<string> implements ISet<string> {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #allowed: SetValues;
|
||||
readonly #byteLength: number;
|
||||
@@ -104,36 +104,11 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
constructor (registry: Registry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength = 8) {
|
||||
super(decodeSet(setValues, value, bitLength));
|
||||
|
||||
this.$registry = registry;
|
||||
this.registry = registry;
|
||||
this.#allowed = setValues;
|
||||
this.#byteLength = bitLength / 8;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
public static with (values: SetValues, bitLength?: number): CodecClass<CodecSet> {
|
||||
return class extends CodecSet {
|
||||
static {
|
||||
@@ -158,7 +133,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
return this.#byteLength;
|
||||
}
|
||||
|
||||
@@ -166,13 +141,13 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description true is the Set contains no values
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
@@ -225,7 +200,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
outer: [this.toU8a()]
|
||||
};
|
||||
|
||||
@@ -207,7 +207,7 @@ describe('Struct', (): void => {
|
||||
txt: Text,
|
||||
u32: U32
|
||||
})
|
||||
)(registry, { foo: 'bazzing', bar: 69 }).$encodedLength
|
||||
)(registry, { foo: 'bazzing', bar: 69 }).encodedLength
|
||||
).toEqual(5);
|
||||
});
|
||||
|
||||
@@ -380,7 +380,7 @@ describe('Struct', (): void => {
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Struct(registry, def, val).inspectU8a()
|
||||
new Struct(registry, def, val).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@ import type { AnyJson, BareOpts, Codec, CodecClass, Inspect, IStruct, IU8a, Regi
|
||||
|
||||
import { isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { compareMap, decodeU8aStruct, mapToTypeMap, typesToMap, warnGet } from '../utils';
|
||||
import { compareMap, decodeU8aStruct, mapToTypeMap, typesToMap } from '../utils';
|
||||
|
||||
type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
|
||||
|
||||
@@ -103,11 +103,11 @@ export class Struct<
|
||||
V extends { [K in keyof S]: any } = { [K in keyof S]: any },
|
||||
// type names, mapped by key, name of Class in S
|
||||
E extends { [K in keyof S]: string } = { [K in keyof S]: string }> extends Map<keyof S, Codec> implements IStruct<keyof S> {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
readonly #jsonMap: Map<keyof S, string>;
|
||||
readonly #Types: Definition;
|
||||
@@ -122,37 +122,12 @@ export class Struct<
|
||||
|
||||
super(decoded);
|
||||
|
||||
this.$initialU8aLength = decodedLength;
|
||||
this.$registry = registry;
|
||||
this.initialU8aLength = decodedLength;
|
||||
this.registry = registry;
|
||||
this.#jsonMap = jsonMap;
|
||||
this.#Types = typeMap;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
}
|
||||
|
||||
public static with<S extends TypesDef> (Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>> {
|
||||
let definition: Definition | undefined;
|
||||
|
||||
@@ -185,9 +160,9 @@ export class Struct<
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
for (const v of this.values()) {
|
||||
if (!v.$isEmpty) {
|
||||
if (!v.isEmpty) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -198,11 +173,11 @@ export class Struct<
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
let total = 0;
|
||||
|
||||
for (const v of this.values()) {
|
||||
total += v.$encodedLength;
|
||||
total += v.encodedLength;
|
||||
}
|
||||
|
||||
return total;
|
||||
@@ -212,7 +187,7 @@ export class Struct<
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,7 +198,7 @@ export class Struct<
|
||||
const [Types, keys] = this.#Types;
|
||||
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
result[keys[i]] = new Types[i](this.$registry).toRawType();
|
||||
result[keys[i]] = new Types[i](this.registry).toRawType();
|
||||
}
|
||||
|
||||
return result as E;
|
||||
@@ -261,12 +236,12 @@ export class Struct<
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (isBare?: BareOpts): Inspect {
|
||||
public inspect (isBare?: BareOpts): Inspect {
|
||||
const inner = new Array<Inspect>();
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
inner.push({
|
||||
...v.inspectU8a(
|
||||
...v.inspect(
|
||||
!isBare || isBoolean(isBare)
|
||||
? isBare
|
||||
: isBare[k]
|
||||
@@ -345,7 +320,7 @@ export class Struct<
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return stringify(typesToMap(this.$registry, this.#Types));
|
||||
return stringify(typesToMap(this.registry, this.#Types));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -59,20 +59,20 @@ describe('Text', (): void => {
|
||||
it('calulates the length & encoded length correctly for ASCII', (): void => {
|
||||
const test = new Text(registry, 'abcde');
|
||||
|
||||
expect(test.$encodedLength).toEqual(6);
|
||||
expect(test.encodedLength).toEqual(6);
|
||||
expect(test).toHaveLength(5);
|
||||
});
|
||||
|
||||
it('calulates the length & encoded length correctly for non-ASCII', (): void => {
|
||||
const test = new Text(registry, '中文');
|
||||
|
||||
expect(test.$encodedLength).toEqual(7);
|
||||
expect(test.encodedLength).toEqual(7);
|
||||
expect(test).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Text(registry, 'abcde').inspectU8a()
|
||||
new Text(registry, 'abcde').inspect()
|
||||
).toEqual({
|
||||
outer: [new Uint8Array([5 << 2]), stringToU8a('abcde')]
|
||||
});
|
||||
|
||||
@@ -6,7 +6,6 @@ import type { AnyString, AnyU8a, Inspect, IText, IU8a, Registry } from '../types
|
||||
|
||||
import { compactAddLength, compactFromU8aLim, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
|
||||
|
||||
import { warnGet } from '../utils';
|
||||
import { Raw } from './Raw';
|
||||
|
||||
const MAX_LENGTH = 128 * 1024;
|
||||
@@ -50,11 +49,11 @@ function decodeText (value?: null | AnyString | AnyU8a | { toString: () => strin
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class Text extends String implements IText {
|
||||
readonly $registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
public createdAtHash?: IU8a;
|
||||
public initialU8aLength?: number;
|
||||
public isStorageFallback?: boolean;
|
||||
|
||||
#override: string | null = null;
|
||||
|
||||
@@ -63,39 +62,14 @@ export class Text extends String implements IText {
|
||||
|
||||
super(str);
|
||||
|
||||
this.$registry = registry;
|
||||
this.$initialU8aLength = decodedLength;
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
public get encodedLength (): number {
|
||||
return warnGet(this, 'encodedLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version */
|
||||
public get registry (): Registry {
|
||||
return warnGet(this, 'registry');
|
||||
this.registry = registry;
|
||||
this.initialU8aLength = decodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get $encodedLength (): number {
|
||||
public get encodedLength (): number {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
@@ -103,13 +77,13 @@ export class Text extends String implements IText {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.$registry.hash(this.toU8a());
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get $isEmpty (): boolean {
|
||||
public get isEmpty (): boolean {
|
||||
return this.length === 0;
|
||||
}
|
||||
|
||||
@@ -133,7 +107,7 @@ export class Text extends String implements IText {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspectU8a (): Inspect {
|
||||
public inspect (): Inspect {
|
||||
const value = stringToU8a(super.toString());
|
||||
|
||||
return {
|
||||
|
||||
@@ -14,23 +14,6 @@ export interface Inspect {
|
||||
outer?: Uint8Array[];
|
||||
}
|
||||
|
||||
interface CodecDeprecated {
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
readonly createdAtHash: IU8a | undefined;
|
||||
|
||||
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
|
||||
readonly encodedLength: number;
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
readonly initialU8aLength: number | undefined;
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version. */
|
||||
readonly isEmpty: boolean;
|
||||
|
||||
/** @deprecated Use $registry instead. This getter will be removed in a future version. */
|
||||
readonly registry: Registry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name Codec
|
||||
* @description
|
||||
@@ -39,32 +22,20 @@ interface CodecDeprecated {
|
||||
* with getters and functions. The Codec Base is however required for operating
|
||||
* as an encoding/decoding layer
|
||||
*/
|
||||
export interface Codec extends CodecDeprecated {
|
||||
export interface Codec {
|
||||
/**
|
||||
* @description
|
||||
* The block at which this value was retrieved/created (set to non-empty when
|
||||
* retrieved from storage)
|
||||
*/
|
||||
$createdAtHash?: IU8a;
|
||||
|
||||
/**
|
||||
* @description
|
||||
* The length of the value when encoded as a Uint8Array (which is the SCALE representation)
|
||||
*/
|
||||
readonly $encodedLength: number;
|
||||
createdAtHash?: IU8a;
|
||||
|
||||
/**
|
||||
* @description
|
||||
* The length of the initial encoded value (Only available when the value was
|
||||
* constructed from a Uint8Array input)
|
||||
*/
|
||||
$initialU8aLength?: number;
|
||||
|
||||
/**
|
||||
* @description
|
||||
* Checks if the value is an empty value, with the internal bytes all-zero
|
||||
*/
|
||||
$isEmpty: boolean;
|
||||
initialU8aLength?: number;
|
||||
|
||||
/**
|
||||
* @description
|
||||
@@ -76,20 +47,28 @@ export interface Codec extends CodecDeprecated {
|
||||
* any empty storage item should erturn the default. (This is the same as the
|
||||
* implementation on the Substrate runtime)
|
||||
*/
|
||||
$isStorageFallback?: boolean;
|
||||
isStorageFallback?: boolean;
|
||||
|
||||
/**
|
||||
* @description
|
||||
* The type registry associated with this object, as available when this type
|
||||
* was initially created.
|
||||
*/
|
||||
readonly $registry: Registry;
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
readonly encodedLength: number;
|
||||
|
||||
/**
|
||||
* @description Returns a hash of the value
|
||||
*/
|
||||
readonly hash: IU8a;
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
readonly isEmpty: boolean;
|
||||
|
||||
/**
|
||||
* @description The registry associated with this object
|
||||
*/
|
||||
readonly registry: Registry;
|
||||
|
||||
/**
|
||||
* @description Compares the value of the input to see if there is a match
|
||||
*/
|
||||
@@ -98,7 +77,7 @@ export interface Codec extends CodecDeprecated {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspectU8a (isBare?: BareOpts): Inspect;
|
||||
inspect (isBare?: BareOpts): Inspect;
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
|
||||
@@ -14,11 +14,10 @@ export interface ICompact<T extends INumber = INumber> extends Codec {
|
||||
}
|
||||
|
||||
export interface IEnum extends Codec {
|
||||
readonly $isBasic: boolean;
|
||||
|
||||
readonly defIndexes: number[];
|
||||
readonly defKeys: string[];
|
||||
readonly index: number;
|
||||
readonly isBasic: boolean;
|
||||
readonly type: string;
|
||||
readonly value: Codec;
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ export interface Registry {
|
||||
readonly chainSS58: number | undefined;
|
||||
readonly chainTokens: string[];
|
||||
|
||||
$createdAtHash?: IU8a;
|
||||
createdAtHash?: IU8a;
|
||||
|
||||
findMetaCall (callIndex: Uint8Array): CallFunction;
|
||||
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN } | { error: BN | Uint8Array, index: BN }): RegistryError;
|
||||
|
||||
@@ -41,7 +41,7 @@ export function decodeU8a <T extends Codec = Codec> (registry: Registry, result:
|
||||
while (i < count) {
|
||||
const value = new Types[i](registry, u8a.subarray(offset));
|
||||
|
||||
offset += value.$initialU8aLength || value.$encodedLength;
|
||||
offset += value.initialU8aLength || value.encodedLength;
|
||||
result[i] = value;
|
||||
i++;
|
||||
}
|
||||
@@ -67,7 +67,7 @@ export function decodeU8aStruct (registry: Registry, result: [string, Codec][],
|
||||
while (i < count) {
|
||||
const value = new Types[i](registry, u8a.subarray(offset));
|
||||
|
||||
offset += value.$initialU8aLength || value.$encodedLength;
|
||||
offset += value.initialU8aLength || value.encodedLength;
|
||||
result[i] = [keys[i], value];
|
||||
i++;
|
||||
}
|
||||
@@ -93,7 +93,7 @@ export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, resu
|
||||
while (i < count) {
|
||||
const value = new Type(registry, u8a.subarray(offset));
|
||||
|
||||
offset += value.$initialU8aLength || value.$encodedLength;
|
||||
offset += value.initialU8aLength || value.encodedLength;
|
||||
result[i] = value;
|
||||
i++;
|
||||
}
|
||||
|
||||
@@ -10,4 +10,3 @@ export { typeToConstructor } from './typeToConstructor';
|
||||
export { sanitize } from './sanitize';
|
||||
export { sortAsc, sortSet, sortMap } from './sortValues';
|
||||
export { typesToMap } from './typesToMap';
|
||||
export { warnGet } from './warnGet';
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
// Copyright 2017-2023 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
type Getters = 'createdAtHash' | 'encodedLength' | 'hash' | 'initialU8aLength' | 'isBasic' | 'isEmpty' | 'isNone' | 'isUnsigned' | 'registry';
|
||||
|
||||
const hasWarned: Record<string, boolean> = {};
|
||||
|
||||
/** @description Warns (once, per class) on the use of a deprecated interface */
|
||||
export function warnGet <T> (obj: Record<string, any>, name: Getters): T {
|
||||
const clazz = `${obj.constructor?.name || 'Codec'}`;
|
||||
const check = `${clazz}.${name}`;
|
||||
const pass = `$${name}`;
|
||||
|
||||
if (!hasWarned[check]) {
|
||||
hasWarned[check] = true;
|
||||
|
||||
console.warn(`Use ${clazz}.${pass} instead. The use of ${check} is deprecated and will be removed in a future version.`);
|
||||
}
|
||||
|
||||
return obj[pass] as T;
|
||||
}
|
||||
@@ -24,7 +24,7 @@ function checkInstance (created: Codec, matcher: Uint8Array): void {
|
||||
) ||
|
||||
(
|
||||
// when the created is empty and matcher is also empty, let it slide...
|
||||
created.$isEmpty &&
|
||||
created.isEmpty &&
|
||||
matcher.every((v) => !v)
|
||||
)
|
||||
);
|
||||
@@ -54,11 +54,11 @@ function initType<T extends Codec> (registry: Registry, Type: CodecClass, params
|
||||
isPedantic && checkPedantic(created, params);
|
||||
|
||||
if (blockHash) {
|
||||
created.$createdAtHash = createTypeUnsafe<IU8a>(registry, 'BlockHash', [blockHash]);
|
||||
created.createdAtHash = createTypeUnsafe<IU8a>(registry, 'BlockHash', [blockHash]);
|
||||
}
|
||||
|
||||
if (isFallback) {
|
||||
created.$isStorageFallback = true;
|
||||
created.isStorageFallback = true;
|
||||
}
|
||||
|
||||
return created as T;
|
||||
|
||||
@@ -98,4 +98,4 @@ const tt9 = registry.createType('(u32, (u32, u64), Vec<u8>, Vec<(u32, u64)>, [u8
|
||||
// tuple with nested fixed
|
||||
const tta = registry.createType('([u8;32], [u16;5])');
|
||||
|
||||
assert(tt1[2].bitLength() && tt2.bitLength() && tt4[3].bitLength() && tt5.$isEmpty && tt6[1].toHuman() && tt7.toHuman() && tt8.toHuman() && tt9.toHuman() && tta[1][1].bitLength(), 'All ok');
|
||||
assert(tt1[2].bitLength() && tt2.bitLength() && tt4[3].bitLength() && tt5.isEmpty && tt6[1].toHuman() && tt7.toHuman() && tt8.toHuman() && tt9.toHuman() && tta[1][1].bitLength(), 'All ok');
|
||||
|
||||
@@ -22,7 +22,7 @@ describe('createType', (): void => {
|
||||
|
||||
expect(f64.toRawType()).toEqual('Fixed64');
|
||||
expect(f64.bitLength()).toEqual(64);
|
||||
expect(f64.$isUnsigned).toBe(false);
|
||||
expect(f64.isUnsigned).toBe(false);
|
||||
expect(f64 instanceof Int).toBe(true);
|
||||
});
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { ExtDef } from '../extrinsic/signedExtensions/types';
|
||||
import type { ChainProperties, DispatchErrorModule, DispatchErrorModuleU8, DispatchErrorModuleU8a, EventMetadataLatest, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant } from '../interfaces/types';
|
||||
import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types';
|
||||
|
||||
import { DoNotConstruct, Json, Raw, warnGet } from '@polkadot/types-codec';
|
||||
import { DoNotConstruct, Json, Raw } from '@polkadot/types-codec';
|
||||
import { constructTypeClass, createClassUnsafe, createTypeUnsafe } from '@polkadot/types-create';
|
||||
import { assertReturn, BN_ZERO, formatBalance, isFunction, isNumber, isString, isU8a, lazyMethod, logger, objectSpread, stringCamelCase, stringify } from '@polkadot/util';
|
||||
import { blake2AsU8a } from '@polkadot/util-crypto';
|
||||
@@ -186,7 +186,7 @@ export class TypeRegistry implements Registry {
|
||||
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>>> = {};
|
||||
readonly #moduleMap: Record<string, string[]> = {};
|
||||
|
||||
public $createdAtHash?: Hash;
|
||||
public createdAtHash?: Hash;
|
||||
|
||||
constructor (createdAtHash?: Hash | Uint8Array | string) {
|
||||
this.#knownDefaults = objectSpread({ Json, Metadata, PortableRegistry, Raw }, baseTypes);
|
||||
@@ -199,15 +199,10 @@ export class TypeRegistry implements Registry {
|
||||
}
|
||||
|
||||
if (createdAtHash) {
|
||||
this.$createdAtHash = this.createType('BlockHash', createdAtHash);
|
||||
this.createdAtHash = this.createType('BlockHash', createdAtHash);
|
||||
}
|
||||
}
|
||||
|
||||
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
|
||||
public get createdAtHash (): IU8a | undefined {
|
||||
return warnGet(this, 'createdAtHash');
|
||||
}
|
||||
|
||||
public get chainDecimals (): number[] {
|
||||
if (this.#chainProperties?.tokenDecimals.isSome) {
|
||||
const allDecimals = this.#chainProperties.tokenDecimals.unwrap();
|
||||
|
||||
@@ -19,7 +19,7 @@ describe('EthereumAccountId', (): void => {
|
||||
});
|
||||
|
||||
it('is empty by default', (): void => {
|
||||
expect(id.$isEmpty).toBe(true);
|
||||
expect(id.isEmpty).toBe(true);
|
||||
});
|
||||
|
||||
it('equals the empty address', (): void => {
|
||||
@@ -77,7 +77,7 @@ describe('EthereumAccountId', (): void => {
|
||||
]));
|
||||
|
||||
it('decodes to a non-empty value', (): void => {
|
||||
expect(registry.createType('EthereumAccountId', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887').$isEmpty).toBe(false);
|
||||
expect(registry.createType('EthereumAccountId', '0x4119b2e6c3Cb618F4f0B93ac77f9BeeC7FF02887').isEmpty).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -69,7 +69,7 @@ export class GenericEthereumLookupSource extends AbstractBase<GenericEthereumAcc
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
const rawLength = this._rawLength;
|
||||
|
||||
return rawLength + (
|
||||
@@ -86,7 +86,7 @@ export class GenericEthereumLookupSource extends AbstractBase<GenericEthereumAcc
|
||||
protected get _rawLength (): number {
|
||||
return this.inner instanceof GenericAccountIndex
|
||||
? GenericAccountIndex.calcLength(this.inner)
|
||||
: this.inner.$encodedLength;
|
||||
: this.inner.encodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -131,7 +131,7 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends AbstractBase<ExtrinsicV
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
@@ -266,13 +266,13 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
override inspectU8a (): Inspect {
|
||||
override inspect (): Inspect {
|
||||
const encoded = u8aConcat(...this.toU8aInner());
|
||||
|
||||
return {
|
||||
inner: this.isSigned
|
||||
? this.inner.inspectU8a().inner
|
||||
: this.inner.method.inspectU8a().inner,
|
||||
? this.inner.inspect().inner
|
||||
: this.inner.method.inspect().inner,
|
||||
outer: [compactToU8a(encoded.length), new Uint8Array([this.version])]
|
||||
};
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ export class MortalEra extends Tuple {
|
||||
/**
|
||||
* @description Encoded length for mortals occupy 2 bytes, different from the actual Tuple since it is encoded. This is a shortcut fro `toU8a().length`
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@@ -247,10 +247,10 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
|
||||
/**
|
||||
* @description Override the encoded length method
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
return this.isImmortalEra
|
||||
? this.asImmortalEra.$encodedLength
|
||||
: this.asMortalEra.$encodedLength;
|
||||
? this.asImmortalEra.encodedLength
|
||||
: this.asMortalEra.encodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,7 +23,7 @@ describe('ExtrinsicPayload', (): void => {
|
||||
const a = new ExtrinsicPayload(registry, TEST, { version: 4 });
|
||||
const b = new ExtrinsicPayload(registry, a.toU8a(), { version: 4 });
|
||||
|
||||
expect(a.inspectU8a()).toEqual(b.inspectU8a());
|
||||
expect(a.inspect()).toEqual(b.inspect());
|
||||
expect(a.toJSON()).toEqual(b.toJSON());
|
||||
});
|
||||
|
||||
@@ -31,7 +31,7 @@ describe('ExtrinsicPayload', (): void => {
|
||||
const a = new ExtrinsicPayload(registry, TEST, { version: 4 });
|
||||
const b = new ExtrinsicPayload(registry, a.toHex(), { version: 4 });
|
||||
|
||||
expect(a.inspectU8a()).toEqual(b.inspectU8a());
|
||||
expect(a.inspect()).toEqual(b.inspect());
|
||||
expect(a.toJSON()).toEqual(b.toJSON());
|
||||
});
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ export class GenericExtrinsicPayload extends AbstractBase<ExtrinsicPayloadVx> {
|
||||
*/
|
||||
public get genesisHash (): BlockHash {
|
||||
// NOTE only v3+
|
||||
return this.inner.genesisHash || this.$registry.createTypeUnsafe('Hash', []);
|
||||
return this.inner.genesisHash || this.registry.createTypeUnsafe('Hash', []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +89,7 @@ export class GenericExtrinsicPayload extends AbstractBase<ExtrinsicPayloadVx> {
|
||||
*/
|
||||
public get specVersion (): INumber {
|
||||
// NOTE only v3+
|
||||
return this.inner.specVersion || this.$registry.createTypeUnsafe('u32', []);
|
||||
return this.inner.specVersion || this.registry.createTypeUnsafe('u32', []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,7 +97,7 @@ export class GenericExtrinsicPayload extends AbstractBase<ExtrinsicPayloadVx> {
|
||||
*/
|
||||
public get tip (): ICompact<INumber> {
|
||||
// NOTE from v2+
|
||||
return this.inner.tip || this.$registry.createTypeUnsafe('Compact<Balance>', []);
|
||||
return this.inner.tip || this.registry.createTypeUnsafe('Compact<Balance>', []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,7 +105,7 @@ export class GenericExtrinsicPayload extends AbstractBase<ExtrinsicPayloadVx> {
|
||||
*/
|
||||
public get transactionVersion (): INumber {
|
||||
// NOTE only v4+
|
||||
return this.inner.transactionVersion || this.$registry.createTypeUnsafe('u32', []);
|
||||
return this.inner.transactionVersion || this.registry.createTypeUnsafe('u32', []);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -150,7 +150,7 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign
|
||||
public toRaw (): SignerPayloadRaw {
|
||||
const payload = this.toPayload();
|
||||
const data = u8aToHex(
|
||||
this.$registry
|
||||
this.registry
|
||||
.createTypeUnsafe('ExtrinsicPayload', [payload, { version: payload.version }])
|
||||
// NOTE Explicitly pass the bare flag so the method is encoded un-prefixed (non-decodable, for signing only)
|
||||
.toU8a({ method: true })
|
||||
|
||||
@@ -40,7 +40,7 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
|
||||
} else if (isU8a(value)) {
|
||||
// here we decode manually since we need to pull through the version information
|
||||
const signature = registry.createTypeUnsafe<ExtrinsicSignatureV4>('ExtrinsicSignatureV4', [value, { isSigned }]);
|
||||
const method = registry.createTypeUnsafe<Call>('Call', [value.subarray(signature.$encodedLength)]);
|
||||
const method = registry.createTypeUnsafe<Call>('Call', [value.subarray(signature.encodedLength)]);
|
||||
|
||||
return {
|
||||
method,
|
||||
@@ -54,7 +54,7 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public override get $encodedLength (): number {
|
||||
public override get encodedLength (): number {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ describe('ExtrinsicPayload', (): void => {
|
||||
it('has a sane inspect', (): void => {
|
||||
// we don't expect this to fail, however it is actually a good
|
||||
// reference for the ordering in base Substrate
|
||||
expect(new ExtrinsicPayload(registry, { method: tx.timestamp.set(0).toHex() } as never).inspectU8a()).toEqual({
|
||||
expect(new ExtrinsicPayload(registry, { method: tx.timestamp.set(0).toHex() } as never).inspect()).toEqual({
|
||||
inner: [
|
||||
{ name: 'method', outer: [new Uint8Array([3, 0, 0])] },
|
||||
{ name: 'era', outer: [new Uint8Array([0]), new Uint8Array([0])] },
|
||||
|
||||
@@ -40,8 +40,8 @@ export class GenericExtrinsicPayloadV4 extends Struct {
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public override inspectU8a (): Inspect {
|
||||
return super.inspectU8a({ method: true });
|
||||
public override inspect (): Inspect {
|
||||
return super.inspect({ method: true });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,6 +116,6 @@ export class GenericExtrinsicPayloadV4 extends Struct {
|
||||
// means that the data-as-signed is un-decodable, but is also doesn't need
|
||||
// the extra information, only the pure data (and is not decoded) ...
|
||||
// The same applies to V1..V3, if we have a V5, carry this comment
|
||||
return sign(this.$registry, signerPair, this.toU8a({ method: true }), this.#signOptions);
|
||||
return sign(this.registry, signerPair, this.toU8a({ method: true }), this.#signOptions);
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user