Split codec into @polkadot/types-codec (#4329)

* Split codec into @polkadot/types-codec

* Additional (test cleanups next)

* Fix tests

* Imports from '@polkadot/types-codec

* All remaining codec imports (only exports left)

* Bytes -> types-codec

* Cleanups

* Bool, BitVec, Text movements

* sanitize move

* Adjust (first build fixes)

* Adjust

* Additional

* Adjust

* Adjustments

* linting

* Adjust, not working atm

* Move {U,I}{8,16,32...} to types-codec

* createTypeUnsafe

* Fix type-only exports

* Folder exports

* Split Lookup/Create on registry interfaces

* Clanup LookupRegistry & CreateRegistry

* Combine CreateRegistry into LookupRegistry
This commit is contained in:
Jaco
2021-12-15 14:04:15 +02:00
committed by GitHub
parent e4eee7edcd
commit 7982c416e7
180 changed files with 1489 additions and 1374 deletions
+2 -2
View File
@@ -474,7 +474,7 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
const lazySection = (section: string) =>
lazyMethods({}, Object.keys(query[section]), (method: string) =>
blockHash
? this._decorateStorageEntryAt(registry, query[section][method], decorateMethod, blockHash)
? this._decorateStorageEntryAt(registry as Registry, query[section][method], decorateMethod, blockHash)
: this._decorateStorageEntry(query[section][method], decorateMethod)
);
@@ -528,7 +528,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 as Registry), blockHash))));
// .keys() & .entries() only available on map types
if (creator.iterKey && creator.meta.type.isMap) {
+1 -1
View File
@@ -14,7 +14,7 @@ import type { VersionedRegistry } from './types';
import { firstValueFrom, map, of, switchMap } from 'rxjs';
import { Metadata, TypeRegistry } from '@polkadot/types';
import { getSpecAlias, getSpecExtensions, getSpecHasher, getSpecRpc, getSpecTypes, getUpgradeVersion } from '@polkadot/types/known';
import { getSpecAlias, getSpecExtensions, getSpecHasher, getSpecRpc, getSpecTypes, getUpgradeVersion } from '@polkadot/types-known';
import { assert, BN_ZERO, isUndefined, logger, objectSpread, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';
+2 -1
View File
@@ -3,6 +3,7 @@
import type { Observable } from 'rxjs';
import type { AccountId } from '@polkadot/types/interfaces';
import type { Registry } from '@polkadot/types/types';
import type { ApiInterfaceRx } from '../../types';
import type { HeaderExtended } from '../type/types';
@@ -38,7 +39,7 @@ export function getHeader (instanceId: string, api: ApiInterfaceRx): (blockHash:
)
]).pipe(
map(([header, validators]) =>
createHeaderExtended(header.registry, header, validators)
createHeaderExtended(header.registry as Registry, header, validators)
),
catchError((): Observable<undefined> =>
// where rpc.chain.getHeader throws, we will land here - it can happen that
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { Registry } from '@polkadot/types/types';
import type { ApiInterfaceRx } from '../../types';
import type { SignedBlockExtended } from '../type/types';
@@ -32,7 +33,7 @@ export function subscribeNewBlocks (instanceId: string, api: ApiInterfaceRx): ()
]);
}),
map(([header, block, events]) =>
createSignedBlockExtended(block.registry, block, events, header.validators)
createSignedBlockExtended(block.registry as Registry, block, events, header.validators)
)
)
);
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { Registry } from '@polkadot/types/types';
import type { ApiInterfaceRx } from '../../types';
import type { HeaderExtended } from '../type/types';
@@ -34,7 +35,7 @@ export function subscribeNewHeads (instanceId: string, api: ApiInterfaceRx): ()
map(([header, validators]): HeaderExtended => {
header.createdAtHash = header.hash;
return createHeaderExtended(header.registry, header, validators);
return createHeaderExtended(header.registry as Registry, header, validators);
})
)
);
+9 -8
View File
@@ -4,8 +4,9 @@
/* eslint-disable no-dupe-class-members */
import type { Observable, OperatorFunction } from 'rxjs';
import type { Address, ApplyExtrinsicResult, Call, Extrinsic, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index, RuntimeDispatchInfo } from '@polkadot/types/interfaces';
import type { Callback, Codec, Constructor, IKeyringPair, ISubmittableResult, Registry, SignatureOptions } from '@polkadot/types/types';
import type { Address, ApplyExtrinsicResult, Call, Extrinsic, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index, RuntimeDispatchInfo, SignerPayload } from '@polkadot/types/interfaces';
import type { Callback, Codec, Constructor, IKeyringPair, ISubmittableResult, SignatureOptions } from '@polkadot/types/types';
import type { CodecRegistry } from '@polkadot/types-codec/types';
import type { ApiInterfaceRx, ApiTypes, PromiseOrObs, SignerResult } from '../types';
import type { AddressOrPair, SignerOptions, SubmittableDryRunResult, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription } from './types';
@@ -25,7 +26,7 @@ interface SubmittableOptions<ApiType extends ApiTypes> {
const identity = <T> (input: T): T => input;
function makeEraOptions (api: ApiInterfaceRx, registry: Registry, partialOptions: Partial<SignerOptions>, { header, mortalLength, nonce }: { header: Header | null; mortalLength: number; nonce: Index }): SignatureOptions {
function makeEraOptions (api: ApiInterfaceRx, registry: CodecRegistry, partialOptions: Partial<SignerOptions>, { header, mortalLength, nonce }: { header: Header | null; mortalLength: number; nonce: Index }): SignatureOptions {
if (!header) {
if (isNumber(partialOptions.era)) {
// since we have no header, it is immortal, remove any option overrides
@@ -39,10 +40,10 @@ function makeEraOptions (api: ApiInterfaceRx, registry: Registry, partialOptions
return makeSignOptions(api, partialOptions, {
blockHash: header.hash,
era: registry.createType('ExtrinsicEra', {
era: registry.createTypeUnsafe<ExtrinsicEra>('ExtrinsicEra', [{
current: header.number,
period: partialOptions.era || mortalLength
}),
}]),
nonce
});
}
@@ -83,7 +84,7 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorate
#transformResult: (input: ISubmittableResult) => ISubmittableResult = identity;
constructor (registry: Registry, extrinsic: Call | Extrinsic | Uint8Array | string) {
constructor (registry: CodecRegistry, extrinsic: Call | Extrinsic | Uint8Array | string) {
super(registry, extrinsic, { version: api.extrinsicType });
this.#ignoreStatusCb = apiType === 'rxjs';
@@ -285,11 +286,11 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorate
assert(signer, '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.createType('SignerPayload', objectSpread({}, options, {
const payload = this.registry.createTypeUnsafe<SignerPayload>('SignerPayload', [objectSpread({}, options, {
address,
blockNumber: header ? header.number : 0,
method: this.method
}));
})]);
let result: SignerResult;
if (signer.signPayload) {
+2 -1
View File
@@ -8,6 +8,7 @@
"references": [
{ "path": "../rpc-core" },
{ "path": "../rpc-provider" },
{ "path": "../types" }
{ "path": "../types" },
{ "path": "../types-known" }
]
}
+1
View File
@@ -36,6 +36,7 @@
"@polkadot/api": "6.12.2-1",
"@polkadot/rpc-provider": "6.12.2-1",
"@polkadot/types": "6.12.2-1",
"@polkadot/types-codec": "6.12.2-1",
"@polkadot/types-support": "6.12.2-1",
"@polkadot/util": "^8.1.2",
"handlebars": "^4.7.7",
@@ -20,6 +20,7 @@ try {
'^@polkadot/api(.*)': './packages/api/src/\\1',
'^@polkadot/rpc-core(.*)': './packages/rpc-core/src\\1',
'^@polkadot/rpc-provider(.*)': './packages/rpc-provider/src\\1',
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
@@ -16,6 +16,7 @@ try {
plugins: [
['module-resolver', {
alias: {
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
@@ -17,6 +17,7 @@ try {
['module-resolver', {
alias: {
'^@polkadot/typegen(.*)': './packages/typegen/src\\1',
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
@@ -16,6 +16,7 @@ try {
plugins: [
['module-resolver', {
alias: {
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
@@ -16,6 +16,7 @@ try {
plugins: [
['module-resolver', {
alias: {
'^@polkadot/types-codec(.*)': './packages/types-codec/src\\1',
'^@polkadot/types-known(.*)': './packages/types-known/src\\1',
'^@polkadot/types-support(.*)': './packages/types-support/src\\1',
'^@polkadot/types(.*)': './packages/types/src\\1'
+2 -2
View File
@@ -219,8 +219,8 @@ function generateLookup (destDir: string, entries: [string | undefined, HexStrin
const { lookup, registry } = initMeta(staticMeta).metadata.asLatest;
const filtered = getFilteredTypes(lookup, exclude);
generateLookupDefs(registry, filtered, destDir, subPath);
generateLookupTypes(registry, filtered, destDir, subPath);
generateLookupDefs(registry as Registry, filtered, destDir, subPath);
generateLookupTypes(registry as Registry, filtered, destDir, subPath);
return exclude.concat(
...filtered
+1 -3
View File
@@ -4,13 +4,11 @@
import type { TypeDef } from '@polkadot/types/create/types';
import type { Constructor, Registry } from '@polkadot/types/types';
import { Compact, Enum, Option, Struct, Tuple, UInt, Vec } from '@polkadot/types/codec';
import { AbstractInt } from '@polkadot/types/codec/AbstractInt';
import { getTypeDef } from '@polkadot/types/create';
import { TypeDefInfo } from '@polkadot/types/create/types';
import { GenericAccountId, GenericLookupSource, GenericVote } from '@polkadot/types/generic';
import { AllConvictions } from '@polkadot/types/interfaces/democracy/definitions';
import { bool, Null } from '@polkadot/types/primitive';
import { AbstractInt, bool, Compact, Enum, Null, Option, Struct, Tuple, UInt, Vec } from '@polkadot/types-codec';
import { isChildClass, stringify } from '@polkadot/util';
import { formatType } from './formatting';
+27 -26
View File
@@ -5,6 +5,7 @@
import type { TypeDef } from '@polkadot/types/create/types';
import type { Registry } from '@polkadot/types/types';
import type { CodecRegistry } from '@polkadot/types-codec/types';
import Handlebars from 'handlebars';
@@ -96,7 +97,7 @@ export function exportType (lookupIndex = -1, name = '', base: string): string {
return exportInterface(lookupIndex, name, base);
}
function singleParamNotation (registry: Registry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
function singleParamNotation (registry: CodecRegistry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
const sub = (typeDef.sub as TypeDef);
setImports(definitions, imports, [wrapper, sub.lookupName]);
@@ -104,7 +105,7 @@ function singleParamNotation (registry: Registry, wrapper: string, typeDef: Type
return paramsNotation(wrapper, sub.lookupName || formatType(registry, definitions, sub.type, imports, withShortcut));
}
function dualParamsNotation (registry: Registry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
function dualParamsNotation (registry: CodecRegistry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
const [a, b] = (typeDef.sub as TypeDef[]);
setImports(definitions, imports, [wrapper, a.lookupName, b.lookupName]);
@@ -115,18 +116,18 @@ function dualParamsNotation (registry: Registry, wrapper: string, typeDef: TypeD
]);
}
const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => string> = {
[TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
const formatters: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => string> = {
[TypeDefInfo.Compact]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'Compact', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.DoNotConstruct]: (registry: Registry, { lookupName }: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.DoNotConstruct]: (registry: CodecRegistry, { lookupName }: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
setImports(definitions, imports, ['DoNotConstruct']);
return 'DoNotConstruct';
},
[TypeDefInfo.Enum]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Enum]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
if (typeDef.lookupName) {
return typeDef.lookupName;
}
@@ -134,43 +135,43 @@ const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, def
throw new Error(`TypeDefInfo.Enum: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.Int]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Int]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
throw new Error(`TypeDefInfo.Int: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.UInt]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.UInt]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
throw new Error(`TypeDefInfo.UInt: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.Null]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Null]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
setImports(definitions, imports, ['Null']);
return 'Null';
},
[TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Option]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'Option', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.Plain]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Plain]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
setImports(definitions, imports, [typeDef.type]);
return typeDef.type;
},
[TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Range]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
throw new Error(`TypeDefInfo.Range: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.Set]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Set]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
throw new Error(`TypeDefInfo.Set: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.Si]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return formatType(registry, definitions, registry.lookup.getTypeDef(typeDef.type), imports, withShortcut);
[TypeDefInfo.Si]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return formatType(registry, definitions, (registry as Registry).lookup.getTypeDef(typeDef.type), imports, withShortcut);
},
[TypeDefInfo.Struct]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Struct]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
if (typeDef.lookupName) {
return typeDef.lookupName;
}
@@ -187,7 +188,7 @@ const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, def
}${withShortcut ? ' ' : '\n '}} & Struct`;
},
[TypeDefInfo.Tuple]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Tuple]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
const sub = typeDef.sub as TypeDef[];
setImports(definitions, imports, ['ITuple', ...sub.map(({ lookupName }) => lookupName)]);
@@ -200,11 +201,11 @@ const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, def
}]`);
},
[TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Vec]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.VecFixed]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.VecFixed]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
const sub = (typeDef.sub as TypeDef);
if (sub.type === 'u8') {
@@ -216,27 +217,27 @@ const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, def
return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.BTreeMap]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return dualParamsNotation(registry, 'BTreeMap', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.BTreeSet]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'BTreeSet', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.HashMap]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return dualParamsNotation(registry, 'HashMap', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Linkage]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'Linkage', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Result]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return dualParamsNotation(registry, 'Result', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.WrapperOpaque]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'WrapperOpaque', typeDef, definitions, imports, withShortcut);
}
};
@@ -246,7 +247,7 @@ const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, def
*/
/** @internal */
// eslint-disable-next-line @typescript-eslint/ban-types
export function formatType (registry: Registry, definitions: Record<string, ModuleTypes>, type: string | String | TypeDef, imports: TypeImports, withShortcut = false): string {
export function formatType (registry: CodecRegistry, definitions: Record<string, ModuleTypes>, type: string | String | TypeDef, imports: TypeImports, withShortcut = false): string {
let typeDef: TypeDef;
if (isString(type)) {
+1
View File
@@ -8,6 +8,7 @@
"references": [
{ "path": "../api" },
{ "path": "../rpc-provider" },
{ "path": "../types-codec" },
{ "path": "../types-support" }
]
}
+29
View File
@@ -0,0 +1,29 @@
{
"author": "Jaco Greeff <jacogr@gmail.com>",
"bugs": "https://github.com/polkadot-js/api/issues",
"contributors": [],
"description": "Implementation of the Parity codec",
"engines": {
"node": ">=14.0.0"
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/types-codec#readme",
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/types-codec",
"repository": {
"directory": "packages/types-codec",
"type": "git",
"url": "https://github.com/polkadot-js/api.git"
},
"sideEffects": [
"./detectPackage.js",
"./detectPackage.cjs"
],
"type": "module",
"version": "6.12.2-1",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.16.3",
"@polkadot/util": "^8.1.2"
}
}
@@ -1,13 +1,12 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, Codec, IVec, Registry } from '../types';
import type { AnyJson, Codec, CodecRegistry, IU8a, IVec } from '../types';
import { compactToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import { compareArray } from './utils/compareArray';
import { compareArray } from '../utils/compareArray';
/**
* @name AbstractArray
@@ -17,13 +16,13 @@ import { compareArray } from './utils/compareArray';
* @noInheritDoc
*/
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
protected constructor (registry: Registry, values: T[], initialU8aLength?: number) {
protected constructor (registry: CodecRegistry, values: T[], initialU8aLength?: number) {
super(values.length);
// explicitly set the values here - this removes the need for any extra allocations
@@ -53,7 +52,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,10 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyNumber, INumber, Registry } from '../types';
import type { UIntBitLength } from './types';
import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types';
import { assert, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isU8a, u8aToBn } from '@polkadot/util';
@@ -45,9 +43,9 @@ function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegati
* @noInheritDoc
*/
export abstract class AbstractInt extends BN implements INumber {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly encodedLength: number;
@@ -55,7 +53,7 @@ export abstract class AbstractInt extends BN implements INumber {
readonly #isSigned: boolean;
constructor (registry: Registry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
constructor (registry: CodecRegistry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
// Construct via a string, which will be passed in the BN constructor.
// It would be ideal to actually return a BN, but there is an issue:
// https://github.com/indutny/bn.js/issues/206
@@ -81,7 +79,7 @@ export abstract class AbstractInt extends BN implements INumber {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -197,7 +195,7 @@ 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.createClass('Balance')
return this instanceof this.registry.createClassUnsafe('Balance')
? 'Balance'
: `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
}
@@ -0,0 +1,5 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { AbstractArray } from './AbstractArray';
export { AbstractInt } from './AbstractInt';
@@ -1,24 +1,23 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, BareOpts, Codec, Registry } from '../types';
import type { AnyJson, BareOpts, Codec, CodecRegistry, IU8a } from '../types';
/**
* @name Base
* @description A type extends the Base class, when it holds a value
*/
export abstract class Base<T extends Codec> implements Codec {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
protected readonly _raw: T;
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
protected constructor (registry: CodecRegistry, value: T, initialU8aLength?: number) {
this.registry = registry;
this._raw = value;
this.initialU8aLength = initialU8aLength;
@@ -34,7 +33,7 @@ export abstract class Base<T extends Codec> implements Codec {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,14 +1,13 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BN } from '@polkadot/util';
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces';
import type { AnyJson, AnyNumber, Constructor, ICompact, INumber, Registry } from '../types';
import type { AnyJson, AnyNumber, CodecClass, CodecRegistry, ICompact, INumber, IU8a } from '../types';
import { compactFromU8a, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
import { typeToConstructor } from './utils';
import { typeToConstructor } from '../utils';
/**
* @name Compact
@@ -19,17 +18,17 @@ import { typeToConstructor } from './utils';
* a number and making the compact representation thereof
*/
export class Compact<T extends INumber> implements ICompact<T> {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
readonly #Type: Constructor<T>;
readonly #Type: CodecClass<T>;
readonly #raw: T;
constructor (registry: Registry, Type: Constructor<T> | string, value: Compact<T> | AnyNumber = 0) {
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0) {
this.registry = registry;
this.#Type = typeToConstructor(registry, Type);
@@ -39,16 +38,16 @@ export class Compact<T extends INumber> implements ICompact<T> {
this.#raw = raw;
}
public static with<T extends INumber> (Type: Constructor<T> | string): Constructor<Compact<T>> {
public static with<T extends INumber> (Type: CodecClass<T> | string): CodecClass<Compact<T>> {
return class extends Compact<T> {
constructor (registry: Registry, value?: Compact<T> | AnyNumber) {
constructor (registry: CodecRegistry, value?: Compact<T> | AnyNumber) {
super(registry, Type, value);
}
};
}
/** @internal */
public static decodeCompact<T extends INumber> (registry: Registry, Type: Constructor<T>, value: Compact<T> | AnyNumber): [T, number] {
public static decodeCompact<T extends INumber> (registry: CodecRegistry, Type: CodecClass<T>, value: Compact<T> | AnyNumber): [T, number] {
if (value instanceof Compact) {
return [new Type(registry, value.#raw), 0];
} else if (value instanceof Type) {
@@ -72,7 +71,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,9 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, Codec, Constructor, Registry } from '../types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, IU8a } from '../types';
/**
* @name DoNotConstruct
@@ -11,22 +10,22 @@ import type { AnyJson, Codec, Constructor, Registry } from '../types';
* An unknown type that fails on construction with the type info
*/
export class DoNotConstruct implements Codec {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
#neverError: Error;
constructor (registry: Registry, typeName = 'DoNotConstruct') {
constructor (registry: CodecRegistry, typeName = 'DoNotConstruct') {
this.registry = registry;
this.#neverError = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`);
throw this.#neverError;
}
public static with (typeName?: string): Constructor {
public static with (typeName?: string): CodecClass {
return class extends DoNotConstruct {
constructor (registry: Registry) {
constructor (registry: CodecRegistry) {
super(registry, typeName);
}
};
@@ -42,7 +41,7 @@ export class DoNotConstruct implements Codec {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
throw this.#neverError;
}
@@ -1,22 +1,21 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces';
import type { AnyJson, Codec, Constructor, IEnum, Registry } from '../types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, IEnum, IU8a } from '../types';
import { assert, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { Null } from '../primitive/Null';
import { mapToTypeMap, typesToMap } 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
export interface EnumConstructor<T = Codec> {
new(registry: Registry, value?: any, index?: number): T;
export interface EnumCodecClass<T = Codec> {
new(registry: CodecRegistry, value?: any, index?: number): T;
}
interface EntryDef {
Type: Constructor;
Type: CodecClass;
index: number;
}
@@ -27,7 +26,7 @@ interface Decoded {
value: Codec;
}
function isRustEnum (def: Record<string, string | Constructor> | Record<string, number>): def is Record<string, string | Constructor> {
function isRustEnum (def: Record<string, string | CodecClass> | Record<string, number>): def is Record<string, string | CodecClass> {
const defValues = Object.values(def);
if (defValues.some((v) => isNumber(v))) {
@@ -39,7 +38,7 @@ function isRustEnum (def: Record<string, string | Constructor> | Record<string,
return true;
}
function extractDef (registry: Registry, _def: Record<string, string | Constructor> | Record<string, number> | string[]): { def: TypesDef; isBasic: boolean; isIndexed: boolean } {
function extractDef (registry: CodecRegistry, _def: Record<string, string | CodecClass> | Record<string, number> | string[]): { def: TypesDef; isBasic: boolean; isIndexed: boolean } {
const def: TypesDef = {};
let isBasic: boolean;
let isIndexed: boolean;
@@ -82,7 +81,7 @@ function extractDef (registry: Registry, _def: Record<string, string | Construct
};
}
function createFromValue (registry: Registry, def: TypesDef, index = 0, value?: unknown): Decoded {
function createFromValue (registry: CodecRegistry, def: TypesDef, index = 0, value?: unknown): Decoded {
const entry = Object.values(def).find((e) => e.index === index);
assert(!isUndefined(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
@@ -95,7 +94,7 @@ function createFromValue (registry: Registry, def: TypesDef, index = 0, value?:
};
}
function decodeFromJSON (registry: Registry, def: TypesDef, key: string, value?: unknown): Decoded {
function decodeFromJSON (registry: CodecRegistry, def: TypesDef, key: string, value?: unknown): Decoded {
// JSON comes in the form of { "<type (camelCase)>": "<value for type>" }, here we
// additionally force to lower to ensure forward compat
const keys = Object.keys(def).map((k) => k.toLowerCase());
@@ -111,7 +110,7 @@ function decodeFromJSON (registry: Registry, def: TypesDef, key: string, value?:
}
}
function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?: number): Decoded {
function decodeEnum (registry: CodecRegistry, def: TypesDef, value?: unknown, index?: number): Decoded {
// NOTE We check the index path first, before looking at values - this allows treating
// the optional indexes before anything else, more-specific > less-specific
if (isNumber(index)) {
@@ -146,9 +145,9 @@ 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 {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly #def: TypesDef;
@@ -164,7 +163,7 @@ export class Enum implements IEnum {
readonly #raw: Codec;
constructor (registry: Registry, Types: Record<string, string | Constructor> | Record<string, number> | string[], value?: unknown, index?: number) {
constructor (registry: CodecRegistry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number) {
const { def, isBasic, isIndexed } = extractDef(registry, Types);
// shortcut isU8a as used in SCALE decoding
@@ -185,7 +184,7 @@ export class Enum implements IEnum {
}
}
public static with (Types: Record<string, string | Constructor> | Record<string, number> | string[]): EnumConstructor<Enum> {
public static with (Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum> {
const keys = Array.isArray(Types)
? Types
: Object.keys(Types);
@@ -200,7 +199,7 @@ export class Enum implements IEnum {
}
return class extends Enum {
constructor (registry: Registry, value?: unknown, index?: number) {
constructor (registry: CodecRegistry, value?: unknown, index?: number) {
super(registry, Types, value, index);
objectProperties(this, isKeys, (_, i) => this.type === keys[i]);
@@ -223,7 +222,7 @@ export class Enum implements IEnum {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -362,7 +361,7 @@ export class Enum implements IEnum {
const typeMap = Object
.entries(this.#def)
.reduce((out: Record<string, Constructor>, [key, { Type }]) => {
.reduce((out: Record<string, CodecClass>, [key, { Type }]) => {
out[key] = Type;
return out;
@@ -1,10 +1,9 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyNumber, Constructor, Registry } from '../types';
import type { UIntBitLength } from './types';
import type { AnyNumber, CodecClass, CodecRegistry, UIntBitLength } from '../types';
import { AbstractInt } from './AbstractInt';
import { AbstractInt } from '../abstract/AbstractInt';
/**
* @name Int
@@ -17,13 +16,13 @@ import { AbstractInt } from './AbstractInt';
* @noInheritDoc
*/
export class Int extends AbstractInt {
constructor (registry: Registry, value: AnyNumber = 0, bitLength?: UIntBitLength) {
constructor (registry: CodecRegistry, value: AnyNumber = 0, bitLength?: UIntBitLength) {
super(registry, value, bitLength, true);
}
public static with (bitLength: UIntBitLength, typeName?: string): Constructor<Int> {
public static with (bitLength: UIntBitLength, typeName?: string): CodecClass<Int> {
return class extends Int {
constructor (registry: Registry, value?: AnyNumber) {
constructor (registry: CodecRegistry, value?: AnyNumber) {
super(registry, value, bitLength);
}
@@ -1,9 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { Codec, Registry } from '../types';
import type { Codec, CodecRegistry, IU8a } from '../types';
import { isNull } from '@polkadot/util';
@@ -13,14 +12,14 @@ import { isNull } from '@polkadot/util';
* Implements a type that does not contain anything (apart from `null`)
*/
export class Null implements Codec {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
// Added for compatibility reasons, e.g. see Option
public readonly initialU8aLength = 0;
constructor (registry: Registry) {
constructor (registry: CodecRegistry) {
this.registry = registry;
}
@@ -34,7 +33,7 @@ export class Null implements Codec {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
throw new Error('.hash is not implemented on Null');
}
@@ -1,17 +1,16 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces';
import type { AnyJson, Codec, Constructor, IOption, Registry } from '../types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, IOption, IU8a } from '../types';
import { assert, isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
import { Null } from '../primitive/Null';
import { typeToConstructor } from './utils';
import { typeToConstructor } from '../utils';
import { Null } from './Null';
/** @internal */
function decodeOption (registry: Registry, Type: Constructor, value?: unknown): Codec {
function decodeOption (registry: CodecRegistry, Type: CodecClass, value?: unknown): Codec {
// In the case of an option, unwrap the inner
if (value instanceof Option) {
value = value.value;
@@ -42,17 +41,17 @@ function decodeOption (registry: Registry, Type: Constructor, value?: unknown):
* with a value if/as required/found.
*/
export class Option<T extends Codec> implements IOption<T> {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly #Type: Constructor<T>;
readonly #Type: CodecClass<T>;
readonly #initialU8aLength?: number;
readonly #raw: T;
constructor (registry: Registry, typeName: Constructor<T> | string, value?: unknown) {
constructor (registry: CodecRegistry, typeName: CodecClass<T> | string, value?: unknown) {
const Type = typeToConstructor(registry, typeName);
const decoded = isU8a(value) && value.length && !isCodec(value)
? value[0] === 0
@@ -69,9 +68,9 @@ export class Option<T extends Codec> implements IOption<T> {
}
}
public static with<O extends Codec> (Type: Constructor<O> | string): Constructor<Option<O>> {
public static with<O extends Codec> (Type: CodecClass<O> | string): CodecClass<Option<O>> {
return class extends Option<O> {
constructor (registry: Registry, value?: unknown) {
constructor (registry: CodecRegistry, value?: unknown) {
super(registry, Type, value);
}
};
@@ -95,7 +94,7 @@ export class Option<T extends Codec> implements IOption<T> {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, Constructor, IResult, Registry } from '../types';
import type { Codec, CodecClass, CodecRegistry, IResult } from '../types';
import { assert } from '@polkadot/util';
@@ -13,15 +13,15 @@ import { Enum } from './Enum';
* A Result maps to the Rust Result type, that can either wrap a success or error value
*/
export class Result<O extends Codec, E extends Codec> extends Enum implements IResult<O, E> {
constructor (registry: Registry, Ok: Constructor<O> | string, Err: Constructor<E> | string, value?: unknown) {
constructor (registry: CodecRegistry, Ok: CodecClass<O> | string, Err: CodecClass<E> | string, value?: unknown) {
// NOTE This is order-dependent, Ok (with index 0) needs to be first
// eslint-disable-next-line sort-keys
super(registry, { Ok, Err }, value);
}
public static override with<O extends Codec, E extends Codec> (Types: { Ok: Constructor<O> | string; Err: Constructor<E> | string }): Constructor<Result<O, E>> {
public static override with<O extends Codec, E extends Codec> (Types: { Ok: CodecClass<O> | string; Err: CodecClass<E> | string }): CodecClass<Result<O, E>> {
return class extends Result<O, E> {
constructor (registry: Registry, value?: unknown) {
constructor (registry: CodecRegistry, value?: unknown) {
super(registry, Types.Ok, Types.Err, value);
}
};
@@ -1,32 +1,30 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyNumber, AnyString, AnyU8a, Codec, Constructor, ITuple, Registry } from '../types';
import type { AnyTupleValue, Codec, CodecClass, CodecRegistry, ITuple } from '../types';
import { isFunction, isHex, isString, isU8a, stringify, u8aConcat, u8aToU8a } from '@polkadot/util';
import { AbstractArray } from './AbstractArray';
import { decodeU8a, mapToTypeMap, typeToConstructor } from './utils';
import { AbstractArray } from '../abstract/AbstractArray';
import { decodeU8a, mapToTypeMap, typeToConstructor } from '../utils';
export type AnyTupleValue = AnyU8a | (Codec | AnyU8a | AnyNumber | AnyString | undefined | null)[];
type TupleConstructors = Constructor[] | {
[index: string]: Constructor;
type TupleCodecClasss = CodecClass[] | {
[index: string]: CodecClass;
};
type TupleType = (Constructor | string);
type TupleType = (CodecClass | string);
type TupleTypes = TupleType[] | {
[index: string]: Constructor | string;
[index: string]: CodecClass | string;
};
/** @internal */
function decodeTuple (registry: Registry, Classes: TupleConstructors, value?: AnyTupleValue): [Codec[], number] {
function decodeTuple (registry: CodecRegistry, Classes: TupleCodecClasss, value?: AnyTupleValue): [Codec[], number] {
if (isU8a(value) || isHex(value)) {
return decodeU8a(registry, u8aToU8a(value), Classes);
}
const Types: Constructor[] = Array.isArray(Classes)
const Types: CodecClass[] = Array.isArray(Classes)
? Classes
: Object.values(Classes);
@@ -55,9 +53,9 @@ function decodeTuple (registry: Registry, Classes: TupleConstructors, value?: An
* own type. It extends the base JS `Array` object.
*/
export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
#Types: TupleConstructors;
#Types: TupleCodecClasss;
constructor (registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue) {
constructor (registry: CodecRegistry, Types: TupleTypes | TupleType, value?: AnyTupleValue) {
const Classes = Array.isArray(Types)
? Types.map((t) => typeToConstructor(registry, t))
: isFunction(Types) || isString(Types)
@@ -72,9 +70,9 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
this.#Types = Classes;
}
public static with (Types: TupleTypes | TupleType): Constructor<Tuple> {
public static with (Types: TupleTypes | TupleType): CodecClass<Tuple> {
return class extends Tuple {
constructor (registry: Registry, value?: AnyTupleValue) {
constructor (registry: CodecRegistry, value?: AnyTupleValue) {
super(registry, Types, value);
}
};
@@ -1,10 +1,9 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyNumber, Constructor, Registry } from '../types';
import type { UIntBitLength } from './types';
import type { AnyNumber, CodecClass, CodecRegistry, UIntBitLength } from '../types';
import { AbstractInt } from './AbstractInt';
import { AbstractInt } from '../abstract/AbstractInt';
/**
* @name UInt
@@ -17,9 +16,9 @@ import { AbstractInt } from './AbstractInt';
* @noInheritDoc
*/
export class UInt extends AbstractInt {
public static with (bitLength: UIntBitLength, typeName?: string): Constructor<UInt> {
public static with (bitLength: UIntBitLength, typeName?: string): CodecClass<UInt> {
return class extends UInt {
constructor (registry: Registry, value?: AnyNumber) {
constructor (registry: CodecRegistry, value?: AnyNumber) {
super(registry, value, bitLength);
}
@@ -1,19 +1,19 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Codec, Constructor, Registry } from '../types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import { assert, compactFromU8a, logger, u8aToU8a } from '@polkadot/util';
import { AbstractArray } from './AbstractArray';
import { decodeU8aVec, typeToConstructor } from './utils';
import { AbstractArray } from '../abstract/AbstractArray';
import { decodeU8aVec, typeToConstructor } from '../utils';
const MAX_LENGTH = 64 * 1024;
const l = logger('Vec');
export function decodeVec<T extends Codec> (registry: Registry, Type: Constructor<T>, value: Uint8Array | HexString | unknown[], length = -1): [T[], number, number] {
export function decodeVec<T extends Codec> (registry: CodecRegistry, Type: CodecClass<T>, value: Uint8Array | HexString | unknown[], length = -1): [T[], number, number] {
if (Array.isArray(value)) {
const result = new Array<T>(value.length);
@@ -57,9 +57,9 @@ export function decodeVec<T extends Codec> (registry: Registry, Type: Constructo
* specific encoding/decoding on top of the base type.
*/
export class Vec<T extends Codec> extends AbstractArray<T> {
#Type: Constructor<T>;
#Type: CodecClass<T>;
constructor (registry: Registry, Type: Constructor<T> | string, value: Uint8Array | HexString | unknown[] = []) {
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value: Uint8Array | HexString | unknown[] = []) {
const Clazz = typeToConstructor<T>(registry, Type);
const [values, decodedLength] = decodeVec(registry, Clazz, value);
@@ -68,9 +68,9 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
this.#Type = Clazz;
}
public static with<O extends Codec> (Type: Constructor<O> | string): Constructor<Vec<O>> {
public static with<O extends Codec> (Type: CodecClass<O> | string): CodecClass<Vec<O>> {
return class extends Vec<O> {
constructor (registry: Registry, value?: any[]) {
constructor (registry: CodecRegistry, value?: any[]) {
super(registry, Type, value);
}
};
@@ -1,9 +1,9 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec } from '../types';
import { AbstractArray } from './AbstractArray';
import { AbstractArray } from '../abstract/AbstractArray';
/**
* @name VecAny
@@ -1,17 +1,17 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Codec, Constructor, Registry } from '../types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import { assert, isU8a, u8aConcat } from '@polkadot/util';
import { AbstractArray } from './AbstractArray';
import { decodeU8aVec, typeToConstructor } from './utils';
import { AbstractArray } from '../abstract/AbstractArray';
import { decodeU8aVec, typeToConstructor } from '../utils';
import { decodeVec } from './Vec';
/** @internal */
function decodeVecFixed<T extends Codec> (registry: Registry, value: Uint8Array | HexString | unknown[], Type: Constructor<T>, length: number): [T[], number, number] {
function decodeVecFixed<T extends Codec> (registry: CodecRegistry, value: Uint8Array | HexString | unknown[], Type: CodecClass<T>, length: number): [T[], number, number] {
const [values, decodedLength, decodedLengthNoOffset] = decodeVec(registry, Type, value, length);
while (values.length < length) {
@@ -29,9 +29,9 @@ function decodeVecFixed<T extends Codec> (registry: Registry, value: Uint8Array
* This manages codec arrays of a fixed length
*/
export class VecFixed<T extends Codec> extends AbstractArray<T> {
#Type: Constructor<T>;
#Type: CodecClass<T>;
constructor (registry: Registry, Type: Constructor<T> | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[]) {
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[]) {
const Clazz = typeToConstructor<T>(registry, Type);
const [values,, decodedLengthNoOffset] = isU8a(value)
? decodeU8aVec(registry, value, 0, Clazz, length)
@@ -42,9 +42,9 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
this.#Type = Clazz;
}
public static with<O extends Codec> (Type: Constructor<O> | string, length: number): Constructor<VecFixed<O>> {
public static with<O extends Codec> (Type: CodecClass<O> | string, length: number): CodecClass<VecFixed<O>> {
return class extends VecFixed<O> {
constructor (registry: Registry, value?: any[]) {
constructor (registry: CodecRegistry, value?: any[]) {
super(registry, Type, length, value);
}
};
+16
View File
@@ -0,0 +1,16 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { Base } from './Base';
export { Compact } from './Compact';
export { DoNotConstruct } from './DoNotConstruct';
export { Enum } from './Enum';
export { Int } from './Int';
export { Null } from './Null';
export { Option } from './Option';
export { Result } from './Result';
export { Tuple } from './Tuple';
export { UInt } from './UInt';
export { Vec } from './Vec';
export { VecAny } from './VecAny';
export { VecFixed } from './VecFixed';
+11
View File
@@ -0,0 +1,11 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { packageInfo } from './packageInfo';
export * from './abstract';
export * from './base';
export * from './extended';
export * from './native';
export * from './primitive';
export * from './utils';
@@ -0,0 +1,8 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { detectPackage } from '@polkadot/util';
import { packageInfo } from './packageInfo';
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname, []);
@@ -1,14 +1,14 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, Constructor, Registry } from '../types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import { CodecMap } from './Map';
export class BTreeMap<K extends Codec = Codec, V extends Codec = Codec> extends CodecMap<K, V> {
public static with<K extends Codec, V extends Codec> (keyType: Constructor<K> | string, valType: Constructor<V> | string): Constructor<CodecMap<K, V>> {
public static with<K extends Codec, V extends Codec> (keyType: CodecClass<K> | string, valType: CodecClass<V> | string): CodecClass<CodecMap<K, V>> {
return class extends BTreeMap<K, V> {
constructor (registry: Registry, value?: Uint8Array | string | Map<any, any>) {
constructor (registry: CodecRegistry, value?: Uint8Array | string | Map<any, any>) {
super(registry, keyType, valType, value, 'BTreeMap');
}
};
@@ -1,18 +1,17 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, Codec, Constructor, ISet, Registry } from '../types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, ISet, IU8a } from '../types';
import { compactFromU8a, compactToU8a, isHex, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from './utils';
import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from '../utils';
const l = logger('BTreeSet');
/** @internal */
function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: Constructor<V>, u8a: Uint8Array): [Set<V>, number] {
function decodeSetFromU8a<V extends Codec> (registry: CodecRegistry, ValClass: CodecClass<V>, u8a: Uint8Array): [Set<V>, number] {
const output = new Set<V>();
const [offset, length] = compactFromU8a(u8a);
const [values, decodedLength] = decodeU8aVec(registry, u8a, offset, ValClass, length.toNumber());
@@ -25,7 +24,7 @@ function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: Constr
}
/** @internal */
function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: Constructor<V>, value: Set<any> | string[]): [Set<V>, number] {
function decodeSetFromSet<V extends Codec> (registry: CodecRegistry, ValClass: CodecClass<V>, value: Set<any> | string[]): [Set<V>, number] {
const output = new Set<V>();
value.forEach((val: any) => {
@@ -55,7 +54,7 @@ function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: Constr
* @param jsonSet
* @internal
*/
function decodeSet<V extends Codec> (registry: Registry, valType: Constructor<V> | string, value?: Uint8Array | string | string[] | Set<any>): [Set<V>, number] {
function decodeSet<V extends Codec> (registry: CodecRegistry, valType: CodecClass<V> | string, value?: Uint8Array | string | string[] | Set<any>): [Set<V>, number] {
if (!value) {
return [new Set<V>(), 0];
}
@@ -72,15 +71,15 @@ function decodeSet<V extends Codec> (registry: Registry, valType: Constructor<V>
}
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
readonly #ValClass: Constructor<V>;
readonly #ValClass: CodecClass<V>;
constructor (registry: Registry, valType: Constructor<V> | string, rawValue?: Uint8Array | string | string[] | Set<any>) {
constructor (registry: CodecRegistry, valType: CodecClass<V> | string, rawValue?: Uint8Array | string | string[] | Set<any>) {
const [values, decodedLength] = decodeSet(registry, valType, rawValue);
super(sortSet(values));
@@ -90,9 +89,9 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
this.#ValClass = typeToConstructor(registry, valType);
}
public static with<V extends Codec> (valType: Constructor<V> | string): Constructor<BTreeSet<V>> {
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>) {
constructor (registry: CodecRegistry, value?: Uint8Array | string | Set<any>) {
super(registry, valType, value);
}
};
@@ -114,7 +113,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
/**
* @description Returns a hash of the value
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,11 +1,11 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyU8a, Registry } from '../types';
import type { AnyU8a, CodecRegistry } from '../types';
import { assert, compactFromU8a, compactToU8a, isString, u8aConcat, u8aToU8a } from '@polkadot/util';
import { Raw } from '../codec/Raw';
import { Raw } from '../native/Raw';
/** @internal */
function decodeBitVecU8a (value?: Uint8Array): [number, Uint8Array] {
@@ -42,7 +42,7 @@ function decodeBitVec (value?: AnyU8a): [number, Uint8Array] {
export class BitVec extends Raw {
readonly #decodedLength: number;
constructor (registry: Registry, value?: AnyU8a) {
constructor (registry: CodecRegistry, value?: AnyU8a) {
const [decodedLength, u8a] = decodeBitVec(value);
super(registry, u8a);
@@ -1,11 +1,11 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyU8a, Registry } from '../types';
import type { AnyU8a, CodecRegistry } from '../types';
import { assert, compactAddLength, compactFromU8a, compactToU8a, isString, isU8a, u8aToU8a } from '@polkadot/util';
import { Raw } from '../codec/Raw';
import { Raw } from '../native/Raw';
// Bytes are used for things like on-chain code, so it has a healthy limit
const MAX_LENGTH = 10 * 1024 * 1024;
@@ -47,7 +47,7 @@ function decodeBytes (value?: AnyU8a): [Uint8Array | undefined, number] {
* as what is found in [[Text]] and [[Vec]])
*/
export class Bytes extends Raw {
constructor (registry: Registry, value?: AnyU8a) {
constructor (registry: CodecRegistry, value?: AnyU8a) {
const [u8a, decodedLength] = decodeBytes(value);
super(registry, u8a, decodedLength);
@@ -1,14 +1,14 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, Constructor, Registry } from '../types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import { CodecMap } from './Map';
export class HashMap<K extends Codec = Codec, V extends Codec = Codec> extends CodecMap<K, V> {
public static with<K extends Codec, V extends Codec> (keyType: Constructor<K> | string, valType: Constructor<V> | string): Constructor<CodecMap<K, V>> {
public static with<K extends Codec, V extends Codec> (keyType: CodecClass<K> | string, valType: CodecClass<V> | string): CodecClass<CodecMap<K, V>> {
return class extends HashMap<K, V> {
constructor (registry: Registry, value?: Uint8Array | string | Map<any, any>) {
constructor (registry: CodecRegistry, value?: Uint8Array | string | Map<any, any>) {
super(registry, keyType, valType, value);
}
};
@@ -1,15 +1,15 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Codec, Constructor, Registry } from '../types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import { Option } from './Option';
import { Struct } from './Struct';
import { Tuple } from './Tuple';
import { Vec } from './Vec';
import { Option } from '../base/Option';
import { Tuple } from '../base/Tuple';
import { Vec } from '../base/Vec';
import { Struct } from '../native/Struct';
type TypeWithValues = [Constructor, any[]];
type TypeWithValues = [CodecClass, any[]];
const EMPTY = new Uint8Array();
@@ -18,7 +18,7 @@ const EMPTY = new Uint8Array();
* @description The wrapper for the result from a LinkedMap
*/
export class Linkage<T extends Codec> extends Struct {
constructor (registry: Registry, Type: Constructor | string, value?: unknown) {
constructor (registry: CodecRegistry, Type: CodecClass | string, value?: unknown) {
super(registry, {
previous: Option.with(Type),
// eslint-disable-next-line sort-keys
@@ -26,9 +26,9 @@ export class Linkage<T extends Codec> extends Struct {
}, value as HexString);
}
public static withKey<O extends Codec> (Type: Constructor | string): Constructor<Linkage<O>> {
public static withKey<O extends Codec> (Type: CodecClass | string): CodecClass<Linkage<O>> {
return class extends Linkage<O> {
constructor (registry: Registry, value?: unknown) {
constructor (registry: CodecRegistry, value?: unknown) {
super(registry, Type, value);
}
};
@@ -66,7 +66,7 @@ export class Linkage<T extends Codec> extends Struct {
* @description A Linkage keys/Values tuple
*/
export class LinkageResult extends Tuple {
constructor (registry: Registry, [TypeKey, keys]: TypeWithValues, [TypeValue, values]: TypeWithValues) {
constructor (registry: CodecRegistry, [TypeKey, keys]: TypeWithValues, [TypeValue, values]: TypeWithValues) {
super(registry, {
Keys: Vec.with(TypeKey),
Values: Vec.with(TypeValue)
@@ -1,21 +1,20 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, Codec, Constructor, IMap, Registry } from '../types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, IMap, IU8a } from '../types';
import { compactFromU8a, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { AbstractArray } from './AbstractArray';
import { Enum } from './Enum';
import { Struct } from './Struct';
import { compareMap, decodeU8a, sortMap, typeToConstructor } from './utils';
import { AbstractArray } from '../abstract/AbstractArray';
import { Enum } from '../base/Enum';
import { Struct } from '../native/Struct';
import { compareMap, decodeU8a, sortMap, typeToConstructor } from '../utils';
const l = logger('Map');
/** @internal */
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, u8a: Uint8Array): [Map<K, V>, number] {
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: CodecRegistry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, u8a: Uint8Array): [Map<K, V>, number] {
const output = new Map<K, V>();
const [offset, length] = compactFromU8a(u8a);
const count = length.toNumber();
@@ -35,7 +34,7 @@ function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry,
}
/** @internal */
function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, value: Map<any, any>): [Map<K, V>, number] {
function decodeMapFromMap<K extends Codec, V extends Codec> (registry: CodecRegistry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, value: Map<any, any>): [Map<K, V>, number] {
const output = new Map<K, V>();
for (const [key, val] of value.entries()) {
@@ -77,7 +76,7 @@ function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry,
* @param jsonMap
* @internal
*/
function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyType: Constructor<K> | string, valType: Constructor<V> | string, value?: Uint8Array | string | Map<any, any>): [Map<K, V>, number] {
function decodeMap<K extends Codec, V extends Codec> (registry: CodecRegistry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, value?: Uint8Array | string | Map<any, any>): [Map<K, V>, number] {
const KeyClass = typeToConstructor(registry, keyType);
const ValClass = typeToConstructor(registry, valType);
@@ -95,19 +94,19 @@ 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> {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly #KeyClass: Constructor<K>;
readonly #KeyClass: CodecClass<K>;
readonly #ValClass: Constructor<V>;
readonly #ValClass: CodecClass<V>;
readonly initialU8aLength?: number;
readonly #type: string;
constructor (registry: Registry, keyType: Constructor<K> | string, valType: Constructor<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
constructor (registry: CodecRegistry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
const [decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
@@ -135,7 +134,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
/**
* @description Returns a hash of the value
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,9 +1,9 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyTuple, Constructor, INumber, Registry } from '../types';
import type { AnyTuple, CodecClass, CodecRegistry, INumber } from '../types';
import { Tuple } from '../codec/Tuple';
import { Tuple } from '../base/Tuple';
type RangeType = 'Range' | 'RangeInclusive';
@@ -15,15 +15,15 @@ type RangeType = 'Range' | 'RangeInclusive';
export class Range<T extends INumber> extends Tuple {
#rangeName: RangeType;
constructor (registry: Registry, Type: Constructor<T> | string, value?: AnyTuple, rangeName: RangeType = 'Range') {
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value?: AnyTuple, rangeName: RangeType = 'Range') {
super(registry, { end: Type, start: Type }, value);
this.#rangeName = rangeName;
}
public static override with <T extends INumber> (Types: Constructor<T> | string): Constructor<Range<T>> {
public static override with <T extends INumber> (Types: CodecClass<T> | string): CodecClass<Range<T>> {
return class extends Range<T> {
constructor (registry: Registry, value?: AnyTuple) {
constructor (registry: CodecRegistry, value?: AnyTuple) {
super(registry, Types, value);
}
};
@@ -52,13 +52,13 @@ export class Range<T extends INumber> extends Tuple {
}
export class RangeInclusive<T extends INumber = INumber> extends Range<T> {
constructor (registry: Registry, type: Constructor<T> | string, value?: AnyTuple) {
constructor (registry: CodecRegistry, type: CodecClass<T> | string, value?: AnyTuple) {
super(registry, type, value, 'RangeInclusive');
}
public static override with <T extends INumber> (Types: Constructor<T> | string): Constructor<RangeInclusive<T>> {
public static override with <T extends INumber> (Types: CodecClass<T> | string): CodecClass<RangeInclusive<T>> {
return class extends RangeInclusive<T> {
constructor (registry: Registry, value?: AnyTuple) {
constructor (registry: CodecRegistry, value?: AnyTuple) {
super(registry, Types, value);
}
};
+29
View File
@@ -0,0 +1,29 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecRegistry } from '../types';
import { Text } from '../native/Text';
import { sanitize } from '../utils';
/**
* @name Type
* @description
* This is a extended version of Text, specifically to handle types. Here we rely fully
* on what Text provides us, however we also adjust the types received from the runtime,
* i.e. we remove the `T::` prefixes found in some types for consistency across implementation.
*/
export class Type extends Text {
constructor (registry: CodecRegistry, value: Text | Uint8Array | string = '') {
super(registry, value);
this.setOverride(sanitize(this.toString()));
}
/**
* @description Returns the base runtime type name for this instance
*/
public override toRawType (): string {
return 'Type';
}
}
@@ -1,12 +1,11 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyU8a, Constructor, Registry } from '../types';
import type { U8aBitLength } from './types';
import type { AnyU8a, CodecClass, CodecRegistry, U8aBitLength } from '../types';
import { assert, u8aToU8a } from '@polkadot/util';
import { Raw } from './Raw';
import { Raw } from '../native/Raw';
/** @internal */
function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): [AnyU8a, number] {
@@ -29,15 +28,15 @@ function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): [AnyU8a, numbe
* to be used directly, rather is should be subclassed with the specific lengths.
*/
export class U8aFixed extends Raw {
constructor (registry: Registry, value: AnyU8a = new Uint8Array(), bitLength: U8aBitLength = 256) {
constructor (registry: CodecRegistry, value: AnyU8a = new Uint8Array(), bitLength: U8aBitLength = 256) {
const [u8a, decodedLength] = decodeU8aFixed(value, bitLength);
super(registry, u8a, decodedLength);
}
public static with (bitLength: U8aBitLength, typeName?: string): Constructor<U8aFixed> {
public static with (bitLength: U8aBitLength, typeName?: string): CodecClass<U8aFixed> {
return class extends U8aFixed {
constructor (registry: Registry, value?: AnyU8a) {
constructor (registry: CodecRegistry, value?: AnyU8a) {
super(registry, value, bitLength);
}
@@ -1,14 +1,14 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, Constructor, Registry } from '../types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import { compactAddLength, compactStripLength, isU8a } from '@polkadot/util';
import { Base } from './Base';
import { typeToConstructor } from './utils';
import { Base } from '../base/Base';
import { typeToConstructor } from '../utils';
function decodeRaw<T extends Codec> (registry: Registry, Type: Constructor<T> | string, value?: unknown): T {
function decodeRaw<T extends Codec> (registry: CodecRegistry, Type: CodecClass<T> | string, value?: unknown): T {
const Clazz = typeToConstructor<T>(registry, Type);
if (isU8a(value)) {
@@ -21,13 +21,13 @@ function decodeRaw<T extends Codec> (registry: Registry, Type: Constructor<T> |
}
export class WrapperOpaque<T extends Codec> extends Base<T> {
constructor (registry: Registry, Type: Constructor<T> | string, value?: unknown) {
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value?: unknown) {
super(registry, decodeRaw(registry, Type, value));
}
public static with<T extends Codec> (Type: Constructor<T> | string): Constructor<WrapperOpaque<T>> {
public static with<T extends Codec> (Type: CodecClass<T> | string): CodecClass<WrapperOpaque<T>> {
return class extends WrapperOpaque<T> {
constructor (registry: Registry, value?: unknown) {
constructor (registry: CodecRegistry, value?: unknown) {
super(registry, Type, value);
}
};
@@ -0,0 +1,14 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { BitVec } from './BitVec';
export { BTreeMap } from './BTreeMap';
export { BTreeSet } from './BTreeSet';
export { Bytes } from './Bytes';
export { HashMap } from './HashMap';
export { Linkage } from './Linkage';
export { CodecMap, CodecMap as Map } from './Map';
export { Range, RangeInclusive } from './Range';
export { Type } from './Type';
export { U8aFixed } from './U8aFixed';
export { WrapperOpaque } from './WrapperOpaque';
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import './detectPackage';
export * from './bundle';
@@ -1,9 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { Codec, Registry } from '../types';
import type { Codec, CodecRegistry, IU8a } from '../types';
import { isU8a, u8aToHex } from '@polkadot/util';
@@ -25,12 +24,12 @@ function decodeBool (value: any): boolean {
* @noInheritDoc
*/
export class bool extends Boolean implements Codec {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
// eslint-disable-next-line @typescript-eslint/ban-types
constructor (registry: Registry, value: bool | Boolean | Uint8Array | boolean | number = false) {
constructor (registry: CodecRegistry, value: bool | Boolean | Uint8Array | boolean | number = false) {
super(decodeBool(value));
this.registry = registry;
@@ -46,7 +45,7 @@ export class bool extends Boolean implements Codec {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,10 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyNumber, INumber, Registry } from '../types';
import type { UIntBitLength } from './types';
import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types';
import { BN, bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util';
@@ -35,11 +33,11 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date {
* @noInheritDoc
*/
export class CodecDate extends Date implements INumber {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
constructor (registry: CodecRegistry, value: CodecDate | Date | AnyNumber = 0) {
super(decodeDate(value));
this.registry = registry;
@@ -55,7 +53,7 @@ export class CodecDate extends Date implements INumber {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,13 +1,12 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, Codec, Registry } from '../types';
import type { AnyJson, Codec, CodecRegistry, IU8a } from '../types';
import { isFunction, objectProperties, stringify } from '@polkadot/util';
import { compareMap } from './utils';
import { compareMap } from '../utils';
/** @internal */
function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
@@ -23,11 +22,11 @@ function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
* @noInheritDoc
*/
export class Json extends Map<string, any> implements Codec {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
constructor (registry: Registry, value?: Record<string, unknown> | null) {
constructor (registry: CodecRegistry, value?: Record<string, unknown> | null) {
const decoded = decodeJson(value);
super(decoded);
@@ -47,7 +46,7 @@ export class Json extends Map<string, any> implements Codec {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,9 +1,8 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, AnyU8a, IU8a, Registry } from '../types';
import type { AnyJson, AnyU8a, CodecRegistry, IU8a } from '../types';
import { assert, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
@@ -17,13 +16,13 @@ import { assert, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a }
* @noInheritDoc
*/
export class Raw extends Uint8Array implements IU8a {
public readonly registry: Registry;
readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
constructor (registry: Registry, value?: AnyU8a, initialU8aLength?: number) {
constructor (registry: CodecRegistry, value?: AnyU8a, initialU8aLength?: number) {
super(u8aToU8a(value));
this.registry = registry;
@@ -40,7 +39,7 @@ export class Raw extends Uint8Array implements IU8a {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,13 +1,12 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { Constructor, ISet, Registry } from '../types';
import type { CodecClass, CodecRegistry, ISet, IU8a } from '../types';
import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
import { compareArray } from './utils';
import { compareArray } from '../utils';
type SetValues = Record<string, number | BN>;
@@ -87,15 +86,15 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
* a bitwise representation of the values.
*/
export class CodecSet extends Set<string> implements ISet<string> {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly #allowed: SetValues;
readonly #byteLength: number;
constructor (registry: Registry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength = 8) {
constructor (registry: CodecRegistry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength = 8) {
super(decodeSet(setValues, value, bitLength));
this.registry = registry;
@@ -103,7 +102,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
this.#byteLength = bitLength / 8;
}
public static with (values: SetValues, bitLength?: number): Constructor<CodecSet> {
public static with (values: SetValues, bitLength?: number): CodecClass<CodecSet> {
const keys = Object.keys(values);
const isKeys = new Array<string>(keys.length);
@@ -112,7 +111,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
}
return class extends CodecSet {
constructor (registry: Registry, value?: unknown) {
constructor (registry: CodecRegistry, value?: unknown) {
super(registry, values, value as undefined, bitLength);
objectProperties(this, isKeys, (_, i) => this.strings.includes(keys[i]));
@@ -130,7 +129,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,18 +1,17 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyJson, BareOpts, Codec, Constructor, ConstructorDef, IStruct, Registry } from '../types';
import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, CodecRegistry, IStruct, IU8a } from '../types';
import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from './utils';
import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from '../utils';
type TypesDef<T = Codec> = Record<string, string | Constructor<T>>;
type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
/** @internal */
function decodeStructFromObject (registry: Registry, Types: ConstructorDef, value: any, jsonMap: Map<string, string>): [Iterable<[string, Codec]>, number] {
function decodeStructFromObject (registry: CodecRegistry, Types: CodecClassDef, value: any, jsonMap: Map<string, string>): [Iterable<[string, Codec]>, number] {
let jsonObj: Record<string, unknown> | undefined;
const inputKeys = Object.keys(Types);
const typeofArray = Array.isArray(value);
@@ -91,17 +90,17 @@ 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> {
public readonly registry: Registry;
readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
readonly #jsonMap: Map<keyof S, string>;
readonly #Types: ConstructorDef;
readonly #Types: CodecClassDef;
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap = new Map<string, string>()) {
constructor (registry: CodecRegistry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap = new Map<string, string>()) {
const typeMap = mapToTypeMap(registry, Types);
const [decoded, decodedLength] = isU8a(value)
? decodeU8a<Codec, [string, Codec]>(registry, value, typeMap, true)
@@ -119,11 +118,11 @@ export class Struct<
this.#Types = typeMap;
}
public static with<S extends TypesDef> (Types: S, jsonMap?: Map<string, string>): Constructor<Struct<S>> {
public static with<S extends TypesDef> (Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>> {
const keys = Object.keys(Types);
return class extends Struct<S> {
constructor (registry: Registry, value?: unknown) {
constructor (registry: CodecRegistry, value?: unknown) {
super(registry, Types, value as HexString, jsonMap);
objectProperties(this, keys, (k) => this.get(k));
@@ -183,7 +182,7 @@ export class Struct<
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
@@ -1,13 +1,12 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecHash, Hash } from '../interfaces/runtime';
import type { AnyU8a, Codec, Registry } from '../types';
import type { AnyU8a, Codec, CodecRegistry, IU8a } from '../types';
import { assert, compactAddLength, compactFromU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
import { Raw } from '../codec/Raw';
import { Raw } from './Raw';
const MAX_LENGTH = 128 * 1024;
@@ -49,15 +48,15 @@ function decodeText (value?: null | Text | string | AnyU8a | { toString: () => s
// TODO
// - Strings should probably be trimmed (docs do come through with extra padding)
export class Text extends String implements Codec {
public readonly registry: Registry;
public readonly registry: CodecRegistry;
public createdAtHash?: Hash;
public createdAtHash?: IU8a;
readonly #initialU8aLength?: number;
#override: string | null = null;
constructor (registry: Registry, value?: null | Text | string | AnyU8a | { toString: () => string }) {
constructor (registry: CodecRegistry, value?: null | Text | string | AnyU8a | { toString: () => string }) {
const [str, decodedLength] = decodeText(value);
super(str);
@@ -83,7 +82,7 @@ export class Text extends String implements Codec {
/**
* @description returns a hash of the contents
*/
public get hash (): CodecHash {
public get hash (): IU8a {
return this.registry.hash(this.toU8a());
}
+10
View File
@@ -0,0 +1,10 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { bool, bool as Bool } from './Bool';
export { CodecDate, CodecDate as Date } from './Date';
export { Json } from './Json';
export { Raw } from './Raw';
export { CodecSet, CodecSet as Set } from './Set';
export { Struct } from './Struct';
export { Text } from './Text';
+6
View File
@@ -0,0 +1,6 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Auto-generated by @polkadot/dev, do not edit
export const packageInfo = { name: '@polkadot/types-codec', version: '6.12.2-1' };
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Int } from '../codec/Int';
import { Int } from '../base/Int';
/**
* @name i128
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Int } from '../codec/Int';
import { Int } from '../base/Int';
/**
* @name i16
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Int } from '../codec/Int';
import { Int } from '../base/Int';
/**
* @name i256
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Int } from '../codec/Int';
import { Int } from '../base/Int';
/**
* @name i32
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Int } from '../codec/Int';
import { Int } from '../base/Int';
/**
* @name i64
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Int } from '../codec/Int';
import { Int } from '../base/Int';
/**
* @name i8
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { UInt } from '../codec/UInt';
import { UInt } from '../base/UInt';
/**
* @name u128
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { UInt } from '../codec/UInt';
import { UInt } from '../base/UInt';
/**
* @name u16
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { UInt } from '../codec/UInt';
import { UInt } from '../base/UInt';
/**
* @name u256
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { UInt } from '../codec/UInt';
import { UInt } from '../base/UInt';
/**
* @name u32
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { UInt } from '../codec/UInt';
import { UInt } from '../base/UInt';
/**
* @name u64
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { UInt } from '../codec/UInt';
import { UInt } from '../base/UInt';
/**
* @name u8
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Registry } from '../types';
import type { CodecRegistry } from '../types';
import { u32 } from './U32';
@@ -13,7 +13,7 @@ import { u32 } from './U32';
* It is not to be used, since it created consensus mismatches.
*/
export class usize extends u32 {
constructor (registry: Registry, value?: unknown) {
constructor (registry: CodecRegistry, value?: unknown) {
super(registry, value);
throw new Error('The `usize` type should not be used. Since it is platform-specific, it creates incompatibilities between native (generally u64) and WASM (always u32) code. Use one of the `u32` or `u64` types explicitly.');
@@ -0,0 +1,16 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { i8, i8 as I8 } from './I8';
export { i16, i16 as I16 } from './I16';
export { i32, i32 as I32 } from './I32';
export { i64, i64 as I64 } from './I64';
export { i128, i128 as I128 } from './I128';
export { i256, i256 as I256 } from './I256';
export { u8, u8 as U8 } from './U8';
export { u16, u16 as U16 } from './U16';
export { u32, u32 as U32 } from './U32';
export { u64, u64 as U64 } from './U64';
export { u128, u128 as U128 } from './U128';
export { u256, u256 as U256 } from './U256';
export { usize, usize as USize } from './USize';
+101
View File
@@ -0,0 +1,101 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson } from './helpers';
import type { IU8a } from './interfaces';
import type { CodecRegistry } from './registry';
export type BareOpts = boolean | Record<string, boolean>;
/**
* @name Codec
* @description
* The base Codec interface. All types implement the interface provided here. Additionally
* implementors can add their own specific interfaces and helpers with getters and functions.
* The Codec Base is however required for operating as an encoding/decoding layer
*/
export interface Codec {
/**
* @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 The length of the initial encoded value (Only available when constructed from a Uint8Array)
*/
readonly initialU8aLength?: number;
/**
* @description Checks if the value is an empty value
*/
readonly isEmpty: boolean;
/**
* @description The registry associated with this object
*/
readonly registry: CodecRegistry;
/**
* @description The block at which this value was retrieved/created (set to non-empty when retrieved from storage)
*/
createdAtHash?: IU8a;
/**
* @description Compares the value of the input to see if there is a match
*/
eq (other?: unknown): boolean;
/**
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
*/
toHex (isLe?: boolean): HexString;
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
toHuman (isExtended?: boolean): AnyJson;
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): AnyJson;
/**
* @description Returns the base runtime type name for this instance
*/
toRawType (): string;
/**
* @description Returns the string representation of the value
*/
toString (): string;
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: BareOpts): Uint8Array;
}
export interface CodecClass<T = Codec> {
/**
* @description An internal fallback type (previous generation) if encoding fails
*/
readonly __fallbackType?: string;
// NOTE: We need the any[] here, unknown[] does not work as expected
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new(registry: CodecRegistry, ...args: any[]): T;
}
export type CodecClassDef<T = Codec> = Record<string, CodecClass<T>>;
export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';
export type ArgsDef = Record<string, CodecClass | string>;
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BN } from '@polkadot/util';
import type { Codec } from './codec';
export type AnyJson = string | number | boolean | null | undefined | AnyJson[] | { [index: string]: AnyJson };
export type AnyFunction = (...args: any[]) => any;
export type AnyNumber = BN | bigint | Uint8Array | number | string;
export type AnyString = string | string;
export type AnyTuple = Codec[];
export type AnyU8a = Uint8Array | number[] | string;
export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
// The 520 here is a weird one - it is explicitly for a [u8; 65] as found as a EcdsaSignature,
// and 264 here is explicity for a [u8; 33] as found as EcdsaPublic key.
// Likewise 160 is for [u8; 20], which is also a H160, i.e. an Ethereum address. Both these are
// as a result of the Polkadot claims module. (Technically we don't need the 520 in here)
export type U8aBitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 264 | 512 | 520 | 1024 | 2048;
export type AnyTupleValue = AnyU8a | (Codec | AnyU8a | AnyNumber | AnyString | undefined | null)[];
+7
View File
@@ -0,0 +1,7 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export * from './codec';
export * from './helpers';
export * from './interfaces';
export * from './registry';
@@ -0,0 +1,87 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
// import type { SignOptions } from '@polkadot/keyring/types';
import type { BN } from '@polkadot/util';
import type { ArgsDef, Codec } from './codec';
import type { AnyTuple } from './helpers';
export interface ICompact<T extends INumber = INumber> extends Codec {
toBigInt (): bigint;
toBn (): BN;
toNumber (): number;
unwrap (): T;
}
export interface IEnum extends Codec {
readonly defIndexes: number[];
readonly defKeys: string[];
readonly index: number;
readonly isBasic: boolean
readonly type: string;
readonly value: Codec;
toNumber (): number;
}
export interface IMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V>, Codec {}
export interface INumber extends Codec {
bitLength (): number;
toBigInt (): bigint;
toBn (): BN;
toNumber (): number;
}
export interface IOption<T extends Codec = Codec> extends Codec {
readonly isNone: boolean;
readonly isSome: boolean;
readonly value: Codec;
unwrap (): T;
unwrapOr <O>(other: O): T | O;
unwrapOrDefault (): T;
}
export interface IResult<O extends Codec = Codec, E extends Codec = Codec> extends IEnum {
readonly asErr: E;
readonly asOk: O;
readonly isErr: boolean;
readonly isOk: boolean;
}
export interface ISet<K = string> extends Set<K>, Codec {
readonly strings: string[];
}
export interface IStruct<K = string, V extends Codec = Codec> extends Map<K, V>, Codec {
readonly defKeys: string[];
getAtIndex (index: number): Codec
toArray (): Codec[];
}
export type ITuple<T extends AnyTuple = Codec[]> = T & Codec;
export interface IU8a extends Uint8Array, Codec {
readonly isAscii: boolean;
readonly isUtf8: boolean
bitLength (): number;
toHuman (isExtended?: boolean): any;
toJSON (): any;
toUtf8 (): string;
}
export interface IVec<T extends Codec = Codec> extends Array<T>, Codec {
toArray (): T[];
}
export interface IMethod<A extends AnyTuple = AnyTuple> extends Codec {
readonly args: A;
readonly argsDef: ArgsDef;
readonly callIndex: Uint8Array;
readonly data: Uint8Array;
is: (tx: IMethod<AnyTuple>) => tx is IMethod<A>;
}
@@ -0,0 +1,78 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BN } from '@polkadot/util';
import type { Codec, CodecClass } from './codec';
import type { ICompact, IEnum, IMap, IMethod, INumber, IOption, IResult, ISet, IStruct, ITuple, IU8a, IVec } from './interfaces';
export type OnlyCodec<K, T, Types> =
K extends keyof Types
? Types[K]
: T extends ICompact | IEnum | IMap | IMethod | INumber | IOption | IResult | ISet | IStruct | ITuple | IU8a | IVec
? T
: T extends Codec
? T
: never;
export type RegistryTypes =
Record<string, CodecClass | string | Record<string, string> |
{ _enum: string[] | Record<string, number> | Record<string, string | null> } |
{ _set: Record<string, number> }>;
export interface CodecCreateOptions {
blockHash?: Uint8Array | string | null;
isOptional?: boolean;
isPedantic?: boolean;
}
export interface CodecRegistry {
readonly chainDecimals: number[];
readonly chainSS58: number | undefined;
readonly chainTokens: string[];
// readonly knownTypes: RegisteredTypes;
// readonly lookup: PortableCodecRegistry;
// readonly metadata: MetadataLatest;
// readonly unknownTypes: string[];
// readonly signedExtensions: string[];
createdAtHash?: IU8a;
findMetaCall (callIndex: Uint8Array): unknown;
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN }): unknown;
findMetaEvent (eventIndex: Uint8Array): CodecClass<any>;
isLookupType (value: string): boolean;
createLookupType (lookupId: ICompact<INumber> | number): string;
// createClass <T, K extends string = string, Types = Record<K, Codec>> (type: K): CodecClass<OnlyCodec<T, K, Types>>;
createClassUnsafe <T extends Codec = Codec, K extends string = string> (type: K): CodecClass<T>;
// createType <T, K extends string = string> (type: K, ...params: unknown[]): OnlyCodec<T, K, Types>;
createTypeUnsafe <T extends Codec = Codec, K extends string = string> (type: K, params: unknown[], options?: CodecCreateOptions): T;
// get <T extends Codec = Codec, K extends string = string, R = T> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<R> | undefined;
// getChainProperties (): ChainProperties | undefined;
getClassName (clazz: CodecClass): string | undefined;
// getDefinition (typeName: string): string | undefined;
// getModuleInstances (specName: string, moduleName: string): string[] | undefined;
getOrThrow <T extends Codec = Codec, K extends string = string> (name: K, msg?: string): CodecClass<T>;
getOrUnknown <T extends Codec = Codec, K extends string = string> (name: K): CodecClass<T>;
// setKnownTypes (types: RegisteredTypes): void;
getSignedExtensionExtra (): Record<string, string>;
getSignedExtensionTypes (): Record<string, string>;
hasClass (name: string): boolean;
hasDef (name: string): boolean;
hasType (name: string): boolean;
hash (data: Uint8Array): IU8a;
// init (): CodecRegistry;
register (type: CodecClass | RegistryTypes): void;
register (name: string, type: CodecClass): void;
register (arg1: string | CodecClass | RegistryTypes, arg2?: CodecClass): void;
// setChainProperties (properties?: ChainProperties): void;
// setHasher (hasher?: IU8aer | null): void;
// setLookup (lookup: PortableCodecRegistry): void;
setMetadata (metadata: unknown, signedExtensions?: string[], userExtensions?: unknown): void;
// setSignedExtensions (signedExtensions?: string[], userExtensions?: ExtDef): void;
}
@@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isUndefined } from '@polkadot/util';
@@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isObject, isUndefined } from '@polkadot/util';
@@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isObject } from '@polkadot/util';
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, Constructor, Registry } from '../../types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import { u8aToHex } from '@polkadot/util';
@@ -9,7 +9,7 @@ function formatFailure (error: Error, type: string | null, u8a: Uint8Array, key?
return `decodeU8a: failed at ${u8aToHex(u8a)}…${key ? ` on ${key}` : ''}${type ? `: ${type}` : ''}:: ${error.message}`;
}
function getRawType (registry: Registry, Type: Constructor): string | null {
function getRawType (registry: CodecRegistry, Type: CodecClass): string | null {
try {
return new Type(registry).toRawType();
} catch {
@@ -23,10 +23,10 @@ function getRawType (registry: Registry, Type: Constructor): string | null {
*
* @param u8a - The u8a to decode.
* @param result - The result array (will be returned with values pushed)
* @param types - The array of Constructor to decode the U8a against.
* @param types - The array of CodecClass to decode the U8a against.
*/
export function decodeU8a <T extends Codec = Codec, E = T> (registry: Registry, u8a: Uint8Array, types: Constructor[] | { [index: string]: Constructor }, withZip?: boolean): [E[], number] {
const [Types, keys]: [Constructor[], string[]] = Array.isArray(types)
export function decodeU8a <T extends Codec = Codec, E = T> (registry: CodecRegistry, u8a: Uint8Array, types: CodecClass[] | { [index: string]: CodecClass }, withZip?: boolean): [E[], number] {
const [Types, keys]: [CodecClass[], string[]] = Array.isArray(types)
? [types, []]
: [Object.values(types), Object.keys(types)];
@@ -56,7 +56,7 @@ export function decodeU8a <T extends Codec = Codec, E = T> (registry: Registry,
// Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but
// since we have to do less checks (and these are intensive anyway), much faster
export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, u8a: Uint8Array, start: number, Type: Constructor<T>, count: number): [T[], number, number] {
export function decodeU8aVec <T extends Codec = Codec> (registry: CodecRegistry, u8a: Uint8Array, start: number, Type: CodecClass<T>, count: number): [T[], number, number] {
const result = new Array<T>(count);
let offset = start;
@@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { compareArray } from './compareArray';
@@ -7,5 +7,6 @@ export { compareSet } from './compareSet';
export { decodeU8a, decodeU8aVec } from './decodeU8a';
export { mapToTypeMap } from './mapToTypeMap';
export { typeToConstructor } from './typeToConstructor';
export { sanitize } from './sanitize';
export { sortAsc, sortSet, sortMap } from './sortValues';
export { typesToMap } from './typesToMap';
@@ -0,0 +1,20 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecClass, CodecRegistry } from '../types';
import { typeToConstructor } from './typeToConstructor';
/**
* @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
*/
export function mapToTypeMap (registry: CodecRegistry, input: Record<string, string | CodecClass>): Record<string, CodecClass> {
const entries = Object.entries(input);
const output: Record<string, CodecClass> = {};
for (let i = 0; i < entries.length; i++) {
output[entries[i][0]] = typeToConstructor(registry, entries[i][1]);
}
return output;
}
@@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
interface SanitizeOptions {
@@ -1,9 +1,9 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BN } from '@polkadot/util';
import type { Codec } from '../../types';
import type { Enum } from '../Enum';
import type { Enum } from '../base/Enum';
import type { Codec } from '../types';
import { bnToBn, isBigInt, isBn, isFunction, isNumber, stringify } from '@polkadot/util';
@@ -0,0 +1,14 @@
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry } from '../types';
import { isString } from '@polkadot/util';
export function typeToConstructor <T extends Codec = Codec> (registry: CodecRegistry, type: string | CodecClass<T>): CodecClass<T> {
return (
isString(type)
? registry.createClassUnsafe(type)
: type
);
}
@@ -1,9 +1,9 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Constructor, Registry } from '../../types';
import type { CodecClass, CodecRegistry } from '../types';
export function typesToMap (registry: Registry, Types: Record<string, Constructor>): Record<string, string> {
export function typesToMap (registry: CodecRegistry, Types: Record<string, CodecClass>): Record<string, string> {
const entries = Object.entries(Types);
const result: Record<string, string> = {};
@@ -1,4 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { isFunction } from '@polkadot/util';
+10
View File
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"baseUrl": "..",
"outDir": "./build",
"rootDir": "./src",
"resolveJsonModule": true
},
"references": []
}
+2 -4
View File
@@ -24,15 +24,13 @@
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.16.5",
"@polkadot/types-known": "6.12.2-1",
"@polkadot/types-codec": "6.12.2-1",
"@polkadot/util": "^8.1.2",
"@polkadot/util-crypto": "^8.1.2",
"rxjs": "^7.4.0"
},
"devDependencies": {
"@polkadot/keyring": "^8.1.2",
"@polkadot/types-support": "6.12.2-1",
"@types/bn.js": "^4.11.6",
"bn.js": "^4.12.0"
"@polkadot/types-support": "6.12.2-1"
}
}
+1 -2
View File
@@ -7,8 +7,7 @@ import { TypeRegistry } from '../create';
import { I32, Text, U32 } from '../primitive';
import { Constructor } from '../types';
import { ITuple } from '../types/interfaces';
import { Enum } from './Enum';
import { BTreeSet, Struct, Tuple } from '.';
import { BTreeSet, Enum, Struct, Tuple } from '.';
const registry = new TypeRegistry();
+1 -1
View File
@@ -1,11 +1,11 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { CodecDate } from '@polkadot/types-codec';
import { BN } from '@polkadot/util';
import { TypeRegistry } from '../create';
import { U32 } from '../primitive';
import { CodecDate } from './Date';
import { Compact } from '.';
describe('Compact', (): void => {
+1 -1
View File
@@ -3,11 +3,11 @@
import type { CodecTo } from '../types';
import { CodecDate } from '@polkadot/types-codec';
import { BN } from '@polkadot/util';
import { TypeRegistry } from '../create';
import { U64 } from '../primitive';
import { CodecDate } from './Date';
describe('Date', (): void => {
const registry = new TypeRegistry();
+6 -6
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Registry } from '../types';
import type { CodecRegistry } from '@polkadot/types-codec/types';
import { u8aToHex } from '@polkadot/util';
@@ -131,7 +131,7 @@ describe('Enum', (): void => {
class B extends Null { }
class C extends Null { }
class Test extends Enum {
constructor (registry: Registry, value?: string, index?: number) {
constructor (registry: CodecRegistry, value?: string, index?: number) {
super(registry, {
a: A,
b: B,
@@ -356,11 +356,11 @@ describe('Enum', (): void => {
});
it('re-creates via rawType (types)', (): void => {
const type = new Enum(registry, { A: Text, B: U32, C: U32 }).toRawType() as 'Raw';
const value = registry.createType(type, { B: 123 }) as unknown as { isB: true, asB: U32 };
const type = new Enum(registry, { A: Text, B: U32, C: U32 }).toRawType();
const value = registry.createType(type, { B: 123 });
expect(value.isB).toEqual(true);
expect(value.asB.toNumber()).toEqual(123);
expect((value as unknown as Record<string, unknown>).isB).toEqual(true);
expect((value as unknown as Record<string, U32>).asB.toNumber()).toEqual(123);
});
});
+2 -2
View File
@@ -1,16 +1,16 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyTupleValue } from '@polkadot/types-codec/types';
import type { VoteThreshold } from '../interfaces/elections';
import type { BlockNumber } from '../interfaces/runtime';
import type { CodecTo } from '../types';
import type { AnyTupleValue } from './Tuple';
import rpcMetadata from '@polkadot/types-support/metadata/static-substrate';
import { TypeRegistry } from '../create';
import { Metadata } from '../metadata';
import { Text, U32, u128 as U128 } from '../primitive';
import { Text, U32, U128 } from '../primitive';
import { Tuple } from '.';
describe('Tuple', (): void => {
+1 -3
View File
@@ -1,8 +1,6 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import BNOrig from 'bn.js';
import { BN, BN_TWO, isBn } from '@polkadot/util';
import { TypeRegistry } from '../create';
@@ -28,7 +26,7 @@ describe('UInt', (): void => {
const test = new UInt(registry, 16);
expect(isBn(test)).toBe(true);
expect(BNOrig.isBN(test)).toBe(true);
expect(BN.isBN(test)).toBe(true);
expect(test instanceof BN).toBe(true);
});
+1 -25
View File
@@ -6,28 +6,4 @@
// expand with slight care (for instance, Length is really only used internally to
// others, so there _should_ not be need for direct use)
// These are the base codec types, generally used for construction
export { BTreeMap } from './BTreeMap';
export { BTreeSet } from './BTreeSet';
export { Compact } from './Compact';
// export { CodecDate, CodecDate as Date } from './Date';
export { DoNotConstruct } from './DoNotConstruct';
export { Enum } from './Enum';
export { HashMap } from './HashMap';
export { Int } from './Int';
export { Json } from './Json';
export { Linkage } from './Linkage';
export { CodecMap, CodecMap as Map } from './Map';
export { Option } from './Option';
export { Range, RangeInclusive } from './Range';
export { Raw } from './Raw';
export { Result } from './Result';
export { CodecSet, CodecSet as Set } from './Set';
export { Struct } from './Struct';
export { Tuple } from './Tuple';
export { UInt } from './UInt';
export { U8aFixed } from './U8aFixed';
export { Vec } from './Vec';
// export { VecAny } from './VecAny';
export { VecFixed } from './VecFixed';
export { WrapperOpaque } from './WrapperOpaque';
export { BTreeMap, BTreeSet, Compact, DoNotConstruct, Enum, HashMap, Int, Json, Linkage, CodecMap, Map, Option, Range, RangeInclusive, Raw, Result, CodecSet, Set, Struct, Tuple, UInt, U8aFixed, Vec, VecFixed, WrapperOpaque } from '@polkadot/types-codec';
+1 -7
View File
@@ -1,10 +1,4 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
// The 520 here is a weird one - it is explicitly for a [u8; 65] as found as a EcdsaSignature,
// and 264 here is explicity for a [u8; 33] as found as EcdsaPublic key.
// Likewise 160 is for [u8; 20], which is also a H160, i.e. an Ethereum address. Both these are
// as a result of the Polkadot claims module. (Technically we don't need the 520 in here)
export type U8aBitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 264 | 512 | 520 | 1024 | 2048;
export type { U8aBitLength, UIntBitLength } from '@polkadot/types-codec/types';

Some files were not shown because too many files have changed in this diff Show More