Adjust interfaces for cleaner esnext usage (#2833)

* Adjust interfaces for cleaner esnext usage

* Swap out esnext
This commit is contained in:
Jaco Greeff
2020-11-09 23:17:52 +01:00
committed by GitHub
parent 8993844739
commit e6dbbfae34
82 changed files with 242 additions and 348 deletions
+3 -4
View File
@@ -4,9 +4,8 @@
import { H256 } from '../interfaces/runtime';
import { AnyJson, Codec, Registry } from '../types';
import { u8aConcat, u8aToHex } from '@polkadot/util';
import { compactToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import Compact from './Compact';
import { compareArray } from './utils';
/**
@@ -31,7 +30,7 @@ export default abstract class AbstractArray<T extends Codec> extends Array<T> im
public get encodedLength (): number {
return this.reduce((total, raw): number => {
return total + raw.encodedLength;
}, Compact.encodeU8a(this.length).length);
}, compactToU8a(this.length).length);
}
/**
@@ -124,7 +123,7 @@ export default abstract class AbstractArray<T extends Codec> extends Array<T> im
return isBare
? u8aConcat(...encoded)
: u8aConcat(
Compact.encodeU8a(this.length),
compactToU8a(this.length),
...encoded
);
}
+4 -5
View File
@@ -4,9 +4,8 @@
import { H256 } from '../interfaces/runtime';
import { AnyJson, Constructor, Codec, InterfaceTypes, Registry } from '../types';
import { isHex, hexToU8a, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { isHex, hexToU8a, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a, compactFromU8a, compactToU8a } from '@polkadot/util';
import Compact from './Compact';
import { compareSet, decodeU8a, typeToConstructor } from './utils';
const l = logger('BTreeSet');
@@ -14,7 +13,7 @@ const l = logger('BTreeSet');
/** @internal */
function decodeSetFromU8a<V extends Codec = Codec> (registry: Registry, ValClass: Constructor<V>, u8a: Uint8Array): Set<V> {
const output = new Set<V>();
const [offset, length] = Compact.decodeU8a(u8a);
const [offset, length] = compactFromU8a(u8a);
const types = [];
for (let i = 0; i < length.toNumber(); i++) {
@@ -103,7 +102,7 @@ export default class BTreeSet<V extends Codec = Codec> extends Set<V> implements
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
let len = Compact.encodeU8a(this.size).length;
let len = compactToU8a(this.size).length;
this.forEach((v: V) => {
len += v.encodedLength;
@@ -188,7 +187,7 @@ export default class BTreeSet<V extends Codec = Codec> extends Set<V> implements
const encoded = new Array<Uint8Array>();
if (!isBare) {
encoded.push(Compact.encodeU8a(this.size));
encoded.push(compactToU8a(this.size));
}
this.forEach((v: V) => {
-102
View File
@@ -7,104 +7,10 @@ import { TypeRegistry } from '../create';
import Moment from './Date';
import U32 from '../primitive/U32';
import Compact from './Compact';
import UInt from './UInt';
describe('Compact', (): void => {
const registry = new TypeRegistry();
describe('encodeU8a', (): void => {
it('encodes short u8', (): void => {
expect(
Compact.encodeU8a(18)
).toEqual(
new Uint8Array([18 << 2])
);
});
it('encodes max u8 values', (): void => {
expect(
Compact.encodeU8a(new (UInt.with(32))(registry, 63))
).toEqual(
new Uint8Array([0b11111100])
);
});
it('encodes basic u16 value', (): void => {
expect(
Compact.encodeU8a(511)
).toEqual(
new Uint8Array([0b11111101, 0b00000111])
);
});
it('encodes basic ua6 (not at edge)', (): void => {
expect(
Compact.encodeU8a(111)
).toEqual(
new Uint8Array([0xbd, 0x01])
);
});
it('encodes basic u32 values (short)', (): void => {
expect(
Compact.encodeU8a(0xffff)
).toEqual(
new Uint8Array([254, 255, 3, 0])
);
});
it('encodes basic u32 values (full)', (): void => {
expect(
Compact.encodeU8a(0xfffffff9)
).toEqual(
new Uint8Array([3, 249, 255, 255, 255])
);
});
it('encondes a large balance', (): void => {
expect(
Compact.encodeU8a(registry.createType('Balance', '0x5af3107a4000'))
).toEqual(
new Uint8Array([
3 + ((6 - 4) << 2),
0x00, 0x40, 0x7a, 0x10, 0xf3, 0x5a
])
);
});
});
describe('decodeU8a', (): void => {
it('decoded u8 value', (): void => {
expect(
Compact.decodeU8a(new Uint8Array([0b11111100]), 32)
).toEqual([1, new BN(63)]);
});
it('decodes from same u16 encoded value', (): void => {
expect(
Compact.decodeU8a(new Uint8Array([0b11111101, 0b00000111]), 32)
).toEqual([2, new BN(511)]);
});
it('decodes from same u32 encoded value (short)', (): void => {
expect(
Compact.decodeU8a(new Uint8Array([254, 255, 3, 0]), 32)
).toEqual([4, new BN(0xffff)]);
});
it('decodes from same u32 encoded value (full)', (): void => {
expect(
Compact.decodeU8a(new Uint8Array([3, 249, 255, 255, 255]), 32)
).toEqual([5, new BN(0xfffffff9)]);
});
it('decodes from same u32 as u64 encoded value (full, default)', (): void => {
expect(
Compact.decodeU8a(new Uint8Array([3, 249, 255, 255, 255]), 64)
).toEqual([5, new BN(0xfffffff9)]);
});
});
describe('constructor', (): void => {
it('has support for BigInt', (): void => {
expect(
@@ -170,12 +76,4 @@ describe('Compact', (): void => {
expect(new (Compact.with('u128'))(registry, 12345678987654321n).toBigInt() === 12345678987654321n).toBe(true);
});
});
describe('helpers', (): void => {
it('correctly adds the length prefix', (): void => {
expect(
Compact.addLengthPrefix(Uint8Array.from([12, 13]))
).toEqual(Uint8Array.from([2 << 2, 12, 13]));
});
});
});
+3 -21
View File
@@ -5,8 +5,7 @@ import { H256 } from '@polkadot/types/interfaces';
import { AnyJson, AnyNumber, Codec, Constructor, ICompact, InterfaceTypes, Registry } from '../types';
import BN from 'bn.js';
import { compactAddLength, compactFromU8a, compactStripLength, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
import { DEFAULT_BITLENGTH } from '@polkadot/util/compact/defaults';
import { compactFromU8a, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
import { typeToConstructor } from './utils';
import { UIntBitLength } from './AbstractInt';
@@ -46,23 +45,6 @@ export default class Compact<T extends CompactEncodable> implements ICompact<T>
};
}
/**
* Prepend a Uint8Array with its compact length.
*
* @param u8a - The Uint8Array to be prefixed
*/
public static addLengthPrefix = compactAddLength;
public static decodeU8a = compactFromU8a;
public static encodeU8a = compactToU8a;
public static stripLengthPrefix (u8a: Uint8Array, bitLength: UIntBitLength = DEFAULT_BITLENGTH): Uint8Array {
const [, value] = compactStripLength(u8a, bitLength);
return value;
}
/** @internal */
public static decodeCompact<T extends CompactEncodable> (registry: Registry, Type: Constructor<T>, value: Compact<T> | AnyNumber): CompactEncodable {
if (value instanceof Compact) {
@@ -71,7 +53,7 @@ export default class Compact<T extends CompactEncodable> implements ICompact<T>
return new Type(registry, value);
}
const [, _value] = Compact.decodeU8a(value, new Type(registry, 0).bitLength() as UIntBitLength);
const [, _value] = compactFromU8a(value, new Type(registry, 0).bitLength() as UIntBitLength);
return new Type(registry, _value);
}
@@ -177,7 +159,7 @@ export default class Compact<T extends CompactEncodable> implements ICompact<T>
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return Compact.encodeU8a(this.#raw.toBn());
return compactToU8a(this.#raw.toBn());
}
/**
+4 -5
View File
@@ -4,9 +4,8 @@
import { H256 } from '../interfaces/runtime';
import { AnyJson, Constructor, Codec, InterfaceTypes, Registry } from '../types';
import { isHex, hexToU8a, isObject, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { isHex, hexToU8a, isObject, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a, compactFromU8a, compactToU8a } from '@polkadot/util';
import Compact from './Compact';
import { compareMap, decodeU8a, typeToConstructor } from './utils';
const l = logger('Map');
@@ -14,7 +13,7 @@ const l = logger('Map');
/** @internal */
function decodeMapFromU8a<K extends Codec = Codec, V extends Codec = Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, u8a: Uint8Array): Map<K, V> {
const output = new Map<K, V>();
const [offset, length] = Compact.decodeU8a(u8a);
const [offset, length] = compactFromU8a(u8a);
const types = [];
for (let i = 0; i < length.toNumber(); i++) {
@@ -110,7 +109,7 @@ export default class CodecMap<K extends Codec = Codec, V extends Codec = Codec>
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
let len = Compact.encodeU8a(this.size).length;
let len = compactToU8a(this.size).length;
this.forEach((v: V, k: K) => {
len += v.encodedLength + k.encodedLength;
@@ -195,7 +194,7 @@ export default class CodecMap<K extends Codec = Codec, V extends Codec = Codec>
const encoded = new Array<Uint8Array>();
if (!isBare) {
encoded.push(Compact.encodeU8a(this.size));
encoded.push(compactToU8a(this.size));
}
this.forEach((v: V, k: K) => {
+1 -1
View File
@@ -96,7 +96,7 @@ export default class Raw extends Uint8Array implements IU8a {
}
/**
* @description Create a new subarray from the actual buffer. This is needed for compat reasons since a new Uint8Array gets returned here
* @description Create a new subarray from the actual buffer. (compat)
* @param begin The position to start at
* @param end The position to end at
*/
+2 -3
View File
@@ -3,9 +3,8 @@
import { Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { assert, logger, u8aToU8a } from '@polkadot/util';
import { assert, compactFromU8a, logger, u8aToU8a } from '@polkadot/util';
import Compact from './Compact';
import { decodeU8a, typeToConstructor } from './utils';
import AbstractArray from './AbstractArray';
@@ -49,7 +48,7 @@ export default class Vec<T extends Codec> extends AbstractArray<T> {
}
const u8a = u8aToU8a(value);
const [offset, length] = Compact.decodeU8a(u8a);
const [offset, length] = compactFromU8a(u8a);
assert(length.lten(MAX_LENGTH), `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
+2
View File
@@ -65,6 +65,8 @@ export function createTypeUnsafe<T extends Codec = Codec, K extends string = str
// it provides false warning which is more hinderance than help
return initType(registry, createClass<T, K>(registry, type), params); // , isPedantic);
} catch (error) {
l.error(error);
throw new Error(`createType(${type}):: ${(error as Error).message}`);
}
}
+2 -2
View File
@@ -6,7 +6,7 @@
import { ChainProperties, DispatchErrorModule, H256 } from '../interfaces/types';
import { CallFunction, Codec, Constructor, InterfaceTypes, RegistryError, RegistryTypes, Registry, RegistryMetadata, RegisteredTypes } from '../types';
import { extrinsicsFromMeta } from '@polkadot/metadata';
import { Metadata, extrinsicsFromMeta } from '@polkadot/metadata';
import { BN_ZERO, assert, assertReturn, formatBalance, isFunction, isString, isU8a, logger, stringCamelCase, u8aToHex } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
@@ -128,7 +128,7 @@ export class TypeRegistry implements Registry {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const definitions: Record<string, { types: RegistryTypes }> = require('../interfaces/definitions');
this.#knownDefaults = { Json, Raw, ...baseTypes };
this.#knownDefaults = { Json, Metadata, Raw, ...baseTypes };
this.#knownDefinitions = definitions;
this.init();
+8
View File
@@ -0,0 +1,8 @@
// Copyright 2017-2020 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { detectPackage } from '@polkadot/util';
// FIXME This really should be `import(...).then(...)`, but need to check rejections
// eslint-disable-next-line @typescript-eslint/no-var-requires
detectPackage(require('./package.json'), typeof __dirname !== 'undefined' && __dirname);
+3 -3
View File
@@ -6,7 +6,7 @@ import { EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV4, Sr2551
import { Address, Balance, Call, Index } from '../interfaces/runtime';
import { AnyJson, AnyU8a, ArgsDef, Codec, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, InterfaceTypes, Registry, SignatureOptions } from '../types';
import { assert, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { assert, compactAddLength, compactFromU8a, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import Base from '../codec/Base';
import Compact from '../codec/Compact';
@@ -203,7 +203,7 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
return Extrinsic._newFromValue(registry, new Uint8Array(), version);
}
const [offset, length] = Compact.decodeU8a(value);
const [offset, length] = compactFromU8a(value);
const total = offset + length.toNumber();
assert(total <= value.length, `Extrinsic: length less than remainder, expected at least ${total}, found ${value.length}`);
@@ -292,6 +292,6 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
return isBare
? encoded
: Compact.addLengthPrefix(encoded);
: compactAddLength(encoded);
}
}
+1 -5
View File
@@ -1,11 +1,7 @@
// Copyright 2017-2020 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { detectPackage } from '@polkadot/util';
// FIXME This really should be `import(...).then(...)`, but need to check rejections
// eslint-disable-next-line @typescript-eslint/no-var-requires
detectPackage(require('./package.json'), typeof __dirname !== 'undefined' && __dirname);
import './detectPackage';
export * from './codec';
export * from './create';
-2
View File
@@ -1,8 +1,6 @@
// Copyright 2017-2020 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { Metadata } from '@polkadot/metadata';
export * from './extrinsic';
export * from './generic';
export * from './primitive';
@@ -1,8 +1,8 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Enum, Option } from '@polkadot/types/codec';
import { ITuple } from '@polkadot/types/types';
import { AccountId, BlockNumber, Hash } from '@polkadot/types/interfaces/runtime';
/** @name UncleEntryItem */
+1 -1
View File
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Enum, Option, Struct, U8aFixed, Vec } from '@polkadot/types/codec';
import { u32, u64 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
import { Hash, Header } from '@polkadot/types/interfaces/runtime';
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Enum, Struct, Vec } from '@polkadot/types/codec';
import { u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AccountId, BlockNumber } from '@polkadot/types/interfaces/runtime';
/** @name CollectiveOrigin */
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Enum, Option, Struct, Vec } from '@polkadot/types/codec';
import { Bytes, bool, u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { Vote, VoteThreshold } from '@polkadot/types/interfaces/elections';
import { AccountId, Balance, BlockNumber, Call, Hash } from '@polkadot/types/interfaces/runtime';
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { BTreeSet, Enum, Struct, Vec } from '@polkadot/types/codec';
import { Bytes, u32, u64 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
import { AuthoritySignature } from '@polkadot/types/interfaces/imOnline';
import { BlockNumber, Hash } from '@polkadot/types/interfaces/runtime';
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Enum, Option, Set, Struct, Vec } from '@polkadot/types/codec';
import { Data, u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AccountId, Balance, H160 } from '@polkadot/types/interfaces/runtime';
/** @name IdentityFields */
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Struct, U8aFixed, Vec } from '@polkadot/types/codec';
import { Bytes } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AccountId, Hash, Perbill } from '@polkadot/types/interfaces/runtime';
import { IdentificationTuple, SessionIndex } from '@polkadot/types/interfaces/session';
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Enum, Option, Struct, U8aFixed, Vec } from '@polkadot/types/codec';
import { BitVec, Bytes, u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { Signature } from '@polkadot/types/interfaces/extrinsics';
import { AccountId, Balance, BlockNumber, Hash, ValidatorId } from '@polkadot/types/interfaces/runtime';
import { MembershipProof, SessionIndex } from '@polkadot/types/interfaces/session';
@@ -1,10 +1,10 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Compact, Enum, Int, Struct, U8aFixed, UInt, Vec } from '@polkadot/types/codec';
import { GenericAccountId, GenericAccountIndex, GenericBlock, GenericCall, GenericConsensusEngineId, GenericLookupSource, GenericMultiAddress } from '@polkadot/types/generic';
import { Bytes, DoNotConstruct, Null, StorageKey, u16, u32, u64, u8 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
import { Signature } from '@polkadot/types/interfaces/extrinsics';
import { SystemOrigin } from '@polkadot/types/interfaces/system';
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Option, Struct } from '@polkadot/types/codec';
import { Bytes, u32, u8 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { BlockNumber, Call, PalletsOrigin } from '@polkadot/types/interfaces/runtime';
/** @name Period */
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Struct, Vec } from '@polkadot/types/codec';
import { Bytes, u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AccountId, ValidatorId } from '@polkadot/types/interfaces/runtime';
import { Exposure } from '@polkadot/types/interfaces/staking';
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Enum, Struct } from '@polkadot/types/codec';
import { u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AccountId, Balance } from '@polkadot/types/interfaces/runtime';
/** @name Bid */
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { BTreeMap, Compact, Enum, Option, Struct, Vec } from '@polkadot/types/codec';
import { bool, u128, u16, u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AccountId, Balance, BlockNumber, Moment, PerU16, Perbill } from '@polkadot/types/interfaces/runtime';
/** @name ActiveEraInfo */
+1 -1
View File
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Option, Struct, U8aFixed, Vec } from '@polkadot/types/codec';
import { Bytes, StorageKey, Text, u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { Hash, StorageData } from '@polkadot/types/interfaces/runtime';
/** @name ApiId */
@@ -1,10 +1,10 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Compact, Enum, HashMap, Option, Result, Struct, U8aFixed, Vec } from '@polkadot/types/codec';
import { GenericEvent } from '@polkadot/types/generic';
import { Bytes, Text, bool, i32, u32, u64, u8 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AccountData } from '@polkadot/types/interfaces/balances';
import { AccountId, BlockNumber, Digest, Hash, Index, Pays, Weight } from '@polkadot/types/interfaces/runtime';
@@ -1,9 +1,9 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { ITuple } from '@polkadot/types/types';
import { Enum, Option, Struct, Vec } from '@polkadot/types/codec';
import { bool, u32 } from '@polkadot/types/primitive';
import { ITuple } from '@polkadot/types/types';
import { AccountId, Balance, BlockNumber, Hash } from '@polkadot/types/interfaces/runtime';
/** @name Bounty */
+4 -5
View File
@@ -3,9 +3,8 @@
import { AnyU8a, Registry } from '../types';
import { assert, isString, u8aConcat, u8aToU8a } from '@polkadot/util';
import { assert, compactFromU8a, compactToU8a, isString, u8aConcat, u8aToU8a } from '@polkadot/util';
import Compact from '../codec/Compact';
import Raw from '../codec/Raw';
/** @internal */
@@ -15,7 +14,7 @@ function decodeBitVecU8a (value?: Uint8Array): Uint8Array {
}
// handle all other Uint8Array inputs, these do have a length prefix which is the number of bits encoded
const [offset, length] = Compact.decodeU8a(value);
const [offset, length] = compactFromU8a(value);
const total = offset + Math.ceil(length.toNumber() / 8);
assert(total <= value.length, `BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
@@ -47,7 +46,7 @@ export default class BitVec extends Raw {
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this.length + Compact.encodeU8a(this.bitLength()).length;
return this.length + compactToU8a(this.bitLength()).length;
}
/**
@@ -66,6 +65,6 @@ export default class BitVec extends Raw {
return isBare
? bitVec
: u8aConcat(Compact.encodeU8a(this.bitLength()), bitVec);
: u8aConcat(compactToU8a(this.bitLength()), bitVec);
}
}
+4 -5
View File
@@ -3,9 +3,8 @@
import { AnyU8a, Registry } from '../types';
import { assert, isString, isU8a, u8aToU8a } from '@polkadot/util';
import { assert, compactAddLength, compactFromU8a, compactToU8a, isString, isU8a, u8aToU8a } from '@polkadot/util';
import Compact from '../codec/Compact';
import Raw from '../codec/Raw';
// Bytes are used for things like on-chain code, so it has a healthy limit
@@ -18,7 +17,7 @@ function decodeBytesU8a (value: Uint8Array): Uint8Array {
}
// handle all other Uint8Array inputs, these do have a length prefix
const [offset, length] = Compact.decodeU8a(value);
const [offset, length] = compactFromU8a(value);
const total = offset + length.toNumber();
assert(length.lten(MAX_LENGTH), `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
@@ -56,7 +55,7 @@ export default class Bytes extends Raw {
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this.length + Compact.encodeU8a(this.length).length;
return this.length + compactToU8a(this.length).length;
}
/**
@@ -73,6 +72,6 @@ export default class Bytes extends Raw {
public toU8a (isBare?: boolean): Uint8Array {
return isBare
? super.toU8a(isBare)
: Compact.addLengthPrefix(this);
: compactAddLength(this);
}
}
+1 -11
View File
@@ -3,23 +3,13 @@
import { StorageEntryMetadataLatest, StorageEntryTypeLatest, StorageHasher } from '../interfaces/metadata';
import { AnyJson, AnyU8a, Codec, InterfaceTypes, Registry } from '../types';
import { StorageEntry } from './types';
import { assert, isFunction, isString, isU8a } from '@polkadot/util';
import { AllHashers } from '@polkadot/types/interfaces/metadata/definitions';
import Bytes from './Bytes';
export interface StorageEntry {
(arg?: any): Uint8Array;
iterKey?: (arg?: any) => Uint8Array & Codec;
keyPrefix: (arg?: any) => Uint8Array;
meta: StorageEntryMetadataLatest;
method: string;
prefix: string;
section: string;
toJSON: () => any;
}
interface Decoded {
key?: Uint8Array | string;
method?: string;
+3 -4
View File
@@ -4,9 +4,8 @@
import { H256 } from '../interfaces/runtime';
import { AnyU8a, Codec, Registry } from '../types';
import { assert, hexToU8a, isHex, isString, stringToU8a, u8aToString, u8aToHex } from '@polkadot/util';
import { assert, hexToU8a, isHex, isString, stringToU8a, u8aToString, u8aToHex, compactFromU8a, compactAddLength } from '@polkadot/util';
import Compact from '../codec/Compact';
import Raw from '../codec/Raw';
const MAX_LENGTH = 128 * 1024;
@@ -26,7 +25,7 @@ function decodeText (value?: null | Text | string | AnyU8a | { toString: () => s
return u8aToString(value);
}
const [offset, length] = Compact.decodeU8a(value);
const [offset, length] = compactFromU8a(value);
const total = offset + length.toNumber();
assert(length.lten(MAX_LENGTH), `Text length ${length.toString()} exceeds ${MAX_LENGTH}`);
@@ -152,6 +151,6 @@ export default class Text extends String implements Codec {
return isBare
? encoded
: Compact.addLengthPrefix(encoded);
: compactAddLength(encoded);
}
}
+16
View File
@@ -0,0 +1,16 @@
// Copyright 2017-2020 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { StorageEntryMetadataLatest } from '../interfaces/metadata';
import { Codec } from '../types';
export interface StorageEntry {
(arg?: any): Uint8Array;
iterKey?: (arg?: any) => Uint8Array & Codec;
keyPrefix: (arg?: any) => Uint8Array;
meta: StorageEntryMetadataLatest;
method: string;
prefix: string;
section: string;
toJSON: () => any;
}