All private prefixed with _ (#2157)

* All private prefixed with _

* Fix test
This commit is contained in:
Jaco Greeff
2020-04-11 22:21:55 +02:00
committed by GitHub
parent b58d772156
commit a833dccdaf
24 changed files with 254 additions and 257 deletions
+31 -31
View File
@@ -15,6 +15,36 @@ export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
export const DEFAULT_UINT_BITS = 64;
/** @internal */
function decodeAbstracIntU8a (value: Uint8Array, bitLength: UIntBitLength, isNegative: boolean): string {
if (!value.length) {
return '0';
}
try {
// NOTE When passing u8a in (typically from decoded data), it is always Little Endian
return u8aToBn(value.subarray(0, bitLength / 8), { isLe: true, isNegative }).toString();
} catch (error) {
throw new Error(`AbstractInt: failed on ${JSON.stringify(value)}:: ${error.message}`);
}
}
/** @internal */
function decodeAbstracInt (value: AnyNumber, bitLength: UIntBitLength, isNegative: boolean): string {
// This function returns a string, which will be passed in the BN
// constructor. It would be ideal to actually return a BN, but there's a
// bug: https://github.com/indutny/bn.js/issues/206.
if (isHex(value, -1, true)) {
return hexToBn(value, { isLe: false, isNegative }).toString();
} else if (isU8a(value)) {
return decodeAbstracIntU8a(value, bitLength, isNegative);
} else if (isString(value)) {
return new BN(value.toString(), 10).toString();
}
return bnToBn(value).toString();
}
/**
* @name AbstractInt
* @ignore
@@ -32,7 +62,7 @@ export default abstract class AbstractInt extends BN implements Codec {
readonly #isSigned: boolean;
protected constructor (registry: Registry, isSigned: boolean, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isHexJson = true) {
super(AbstractInt.decodeAbstracInt(value, bitLength, isSigned));
super(decodeAbstracInt(value, bitLength, isSigned));
this.registry = registry;
this.#bitLength = bitLength;
@@ -42,36 +72,6 @@ export default abstract class AbstractInt extends BN implements Codec {
assert(super.bitLength() <= bitLength, `${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${bitLength}`);
}
/** @internal */
public static decodeAbstracInt (value: AnyNumber, bitLength: UIntBitLength, isNegative: boolean): string {
// This function returns a string, which will be passed in the BN
// constructor. It would be ideal to actually return a BN, but there's a
// bug: https://github.com/indutny/bn.js/issues/206.
if (isHex(value, -1, true)) {
return hexToBn(value, { isLe: false, isNegative }).toString();
} else if (isU8a(value)) {
return AbstractInt.decodeAbstracIntU8a(value, bitLength, isNegative);
} else if (isString(value)) {
return new BN(value.toString(), 10).toString();
}
return bnToBn(value).toString();
}
/** @internal */
private static decodeAbstracIntU8a (value: Uint8Array, bitLength: UIntBitLength, isNegative: boolean): string {
if (!value.length) {
return '0';
}
try {
// NOTE When passing u8a in (typically from decoded data), it is always Little Endian
return u8aToBn(value.subarray(0, bitLength / 8), { isLe: true, isNegative }).toString();
} catch (error) {
throw new Error(`AbstractInt: failed on ${JSON.stringify(value)}:: ${error.message}`);
}
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
+6 -6
View File
@@ -12,6 +12,11 @@ import Raw from './Raw';
import { compareMap } from './utils';
/** @internal */
function decodeJson (value?: { [index: string]: any } | null): [string, any][] {
return Object.entries(value || {});
}
/**
* @name Json
* @description
@@ -24,7 +29,7 @@ export default class StructAny extends Map<string, any> implements Codec {
public readonly registry: Registry;
constructor (registry: Registry, value?: { [index: string]: any } | null) {
const decoded = StructAny.decodeJson(value);
const decoded = decodeJson(value);
super(decoded);
@@ -44,11 +49,6 @@ export default class StructAny extends Map<string, any> implements Codec {
});
}
/** @internal */
private static decodeJson (value?: { [index: string]: any } | null): [string, any][] {
return Object.entries(value || {});
}
/**
* @description Always 0, never encodes as a Uint8Array
*/
+24 -24
View File
@@ -17,6 +17,29 @@ type TupleTypes = (Constructor | keyof InterfaceTypes)[] | {
[index: string]: Constructor | keyof InterfaceTypes;
};
/** @internal */
function decodeTuple (registry: Registry, _Types: TupleConstructors, value: AnyU8a | string | (AnyU8a | AnyNumber | AnyString | undefined | null)[]): Codec[] {
if (isU8a(value)) {
return decodeU8a(registry, value, _Types);
} else if (isHex(value)) {
return decodeTuple(registry, _Types, hexToU8a(value));
}
const Types: Constructor[] = Array.isArray(_Types)
? _Types
: Object.values(_Types);
return Types.map((Type, index): Codec => {
try {
if (value?.[index] instanceof Type) return value[index] as any;
return new Type(registry, value?.[index]);
} catch (error) {
throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
}
});
}
/**
* @name Tuple
* @description
@@ -31,34 +54,11 @@ export default class Tuple extends AbstractArray<Codec> {
? Types.map((type): Constructor => typeToConstructor(registry, type))
: mapToTypeMap(registry, Types);
super(registry, ...Tuple.decodeTuple(registry, Clazzes, value));
super(registry, ...decodeTuple(registry, Clazzes, value));
this._Types = Clazzes;
}
/** @internal */
private static decodeTuple (registry: Registry, _Types: TupleConstructors, value: AnyU8a | string | (AnyU8a | AnyNumber | AnyString | undefined | null)[]): Codec[] {
if (isU8a(value)) {
return decodeU8a(registry, value, _Types);
} else if (isHex(value)) {
return Tuple.decodeTuple(registry, _Types, hexToU8a(value));
}
const Types: Constructor[] = Array.isArray(_Types)
? _Types
: Object.values(_Types);
return Types.map((Type, index): Codec => {
try {
if (value?.[index] instanceof Type) return value[index] as any;
return new Type(registry, value?.[index]);
} catch (error) {
throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
}
});
}
public static with (Types: TupleTypes): Constructor<Tuple> {
return class extends Tuple {
constructor (registry: Registry, value?: any) {
+23 -23
View File
@@ -13,6 +13,28 @@ import Raw from './Raw';
// as a result of the Polkadot claims module. (Technically we don't need the 520 in here)
export type BitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 512 | 520 | 1024 | 2048;
/** @internal */
function decodeU8aFixed (value: AnyU8a, bitLength: BitLength): AnyU8a {
if (Array.isArray(value) || isString(value)) {
return decodeU8aFixed(u8aToU8a(value), bitLength);
}
// ensure that we have an actual u8a with the full length as specified by
// the bitLength input (padded with zeros as required)
const byteLength = bitLength / 8;
const sub = value.subarray(0, byteLength);
if (sub.length === byteLength) {
return sub;
}
const u8a = new Uint8Array(byteLength);
u8a.set(sub, 0);
return u8a;
}
/**
* @name U8aFixed
* @description
@@ -21,29 +43,7 @@ export type BitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 512 | 520 | 1024 |
*/
export default class U8aFixed extends Raw {
constructor (registry: Registry, value: AnyU8a = new Uint8Array(), bitLength: BitLength = 256) {
super(registry, U8aFixed.decodeU8aFixed(value, bitLength));
}
/** @internal */
private static decodeU8aFixed (value: AnyU8a, bitLength: BitLength): AnyU8a {
if (Array.isArray(value) || isString(value)) {
return U8aFixed.decodeU8aFixed(u8aToU8a(value), bitLength);
}
// ensure that we have an actual u8a with the full length as specified by
// the bitLength input (padded with zeros as required)
const byteLength = bitLength / 8;
const sub = value.subarray(0, byteLength);
if (sub.length === byteLength) {
return sub;
}
const u8a = new Uint8Array(byteLength);
u8a.set(sub, 0);
return u8a;
super(registry, decodeU8aFixed(value, bitLength));
}
public static with (bitLength: BitLength, typeName?: string): Constructor<U8aFixed> {
+2 -2
View File
@@ -270,11 +270,11 @@ export class TypeRegistry implements Registry {
this.#classes.set(arg1, arg2);
} else {
this.registerObject(arg1);
this._registerObject(arg1);
}
}
private registerObject (obj: RegistryTypes): void {
private _registerObject (obj: RegistryTypes): void {
Object.entries(obj).forEach(([name, type]): void => {
if (isFunction(type)) {
// This _looks_ a bit funny, but `typeof Clazz === 'function'
+9 -9
View File
@@ -173,11 +173,11 @@ abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
*/
export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
constructor (registry: Registry, value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, { version }: CreateOptions = {}) {
super(registry, Extrinsic.decodeExtrinsic(registry, value, version));
super(registry, Extrinsic._decodeExtrinsic(registry, value, version));
}
/** @internal */
private static newFromValue (registry: Registry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown {
private static _newFromValue (registry: Registry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown {
if (value instanceof Extrinsic) {
return value._raw;
}
@@ -191,20 +191,20 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
}
/** @internal */
public static decodeExtrinsic (registry: Registry, value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown {
private static _decodeExtrinsic (registry: Registry, value: Extrinsic | ExtrinsicValue | AnyU8a | Call | undefined, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown {
if (isU8a(value) || Array.isArray(value) || isHex(value)) {
return Extrinsic.decodeU8a(registry, u8aToU8a(value), version);
return Extrinsic._decodeU8a(registry, u8aToU8a(value), version);
} else if (value instanceof registry.createClass('Call')) {
return Extrinsic.newFromValue(registry, { method: value }, version);
return Extrinsic._newFromValue(registry, { method: value }, version);
}
return Extrinsic.newFromValue(registry, value, version);
return Extrinsic._newFromValue(registry, value, version);
}
/** @internal */
private static decodeU8a (registry: Registry, value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown {
private static _decodeU8a (registry: Registry, value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown {
if (!value.length) {
return Extrinsic.newFromValue(registry, new Uint8Array(), version);
return Extrinsic._newFromValue(registry, new Uint8Array(), version);
}
const [offset, length] = Compact.decodeU8a(value);
@@ -214,7 +214,7 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
const data = value.subarray(offset, total);
return Extrinsic.newFromValue(registry, data.subarray(1), data[0]);
return Extrinsic._newFromValue(registry, data.subarray(1), data[0]);
}
/**
+24 -27
View File
@@ -28,6 +28,17 @@ interface ImmortalEnumDef {
ImmortalEra: string;
}
function getTrailingZeros (period: number): number {
const binary = period.toString(2);
let index = 0;
while (binary[binary.length - 1 - index] === '0') {
index++;
}
return index;
}
/**
* @name ImmortalEra
* @description
@@ -52,19 +63,19 @@ export class MortalEra extends Tuple {
super(registry, {
period: U64,
phase: U64
}, MortalEra.decodeMortalEra(registry, value));
}, MortalEra._decodeMortalEra(registry, value));
}
/** @internal */
private static decodeMortalEra (registry: Registry, value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue {
private static _decodeMortalEra (registry: Registry, value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue {
if (isHex(value)) {
return MortalEra.decodeMortalU8a(registry, hexToU8a(value));
return MortalEra._decodeMortalU8a(registry, hexToU8a(value));
} else if (Array.isArray(value)) {
return MortalEra.decodeMortalU8a(registry, new Uint8Array(value));
return MortalEra._decodeMortalU8a(registry, new Uint8Array(value));
} else if (isU8a(value)) {
return MortalEra.decodeMortalU8a(registry, value);
return MortalEra._decodeMortalU8a(registry, value);
} else if (isObject(value)) {
return MortalEra.decodeMortalObject(registry, value);
return MortalEra._decodeMortalObject(registry, value);
} else if (!value) {
return [new U64(registry), new U64(registry)];
}
@@ -73,7 +84,7 @@ export class MortalEra extends Tuple {
}
/** @internal */
private static decodeMortalObject (registry: Registry, value: MortalMethod): MortalEraValue {
private static _decodeMortalObject (registry: Registry, value: MortalMethod): MortalEraValue {
const { current, period } = value;
let calPeriod = Math.pow(2, Math.ceil(Math.log2(period)));
@@ -87,7 +98,7 @@ export class MortalEra extends Tuple {
}
/** @internal */
private static decodeMortalU8a (registry: Registry, value: Uint8Array): MortalEraValue {
private static _decodeMortalU8a (registry: Registry, value: Uint8Array): MortalEraValue {
if (value.length === 0) {
return [new U64(registry), new U64(registry)];
}
@@ -157,7 +168,7 @@ export class MortalEra extends Tuple {
const period = this.period.toNumber();
const phase = this.phase.toNumber();
const quantizeFactor = Math.max(period >> 12, 1);
const trailingZeros = this.getTrailingZeros(period);
const trailingZeros = getTrailingZeros(period);
const encoded = Math.min(15, Math.max(1, trailingZeros - 1)) + (((phase / quantizeFactor) << 4));
const first = encoded >> 8;
const second = encoded & 0xff;
@@ -184,20 +195,6 @@ export class MortalEra extends Tuple {
// FIXME No toNumber() here
return this.birth(current) + this.period.toNumber();
}
/**
* @description convert the number to binary and get the trailing zero's.
*/
private getTrailingZeros (period: number): number {
const binary = period.toString(2);
let index = 0;
while (binary[binary.length - 1 - index] === '0') {
index++;
}
return index;
}
}
/**
@@ -210,16 +207,16 @@ export default class ExtrinsicEra extends Enum implements IExtrinsicEra {
super(registry, {
ImmortalEra,
MortalEra
}, ExtrinsicEra.decodeExtrinsicEra(value));
}, ExtrinsicEra._decodeExtrinsicEra(value));
}
/** @internal */
// eslint-disable-next-line @typescript-eslint/ban-types
private static decodeExtrinsicEra (value: IExtrinsicEra | MortalMethod | MortalEnumDef | ImmortalEnumDef | Uint8Array | string = new Uint8Array()): Uint8Array | Object | undefined {
private static _decodeExtrinsicEra (value: IExtrinsicEra | MortalMethod | MortalEnumDef | ImmortalEnumDef | Uint8Array | string = new Uint8Array()): Uint8Array | Object | undefined {
if (value instanceof ExtrinsicEra) {
return ExtrinsicEra.decodeExtrinsicEra(value.toU8a());
return ExtrinsicEra._decodeExtrinsicEra(value.toU8a());
} else if (isHex(value)) {
return ExtrinsicEra.decodeExtrinsicEra(hexToU8a(value));
return ExtrinsicEra._decodeExtrinsicEra(hexToU8a(value));
} else if (!value || isU8a(value)) {
return (!value?.length || value[0] === 0)
? new Uint8Array([0])
@@ -99,7 +99,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
return this.registry.createType('Compact<Balance>', 0);
}
private injectSignature (signer: Address, signature: Signature, { era, nonce }: ExtrinsicPayloadV1): IExtrinsicSignature {
private _injectSignature (signer: Address, signature: Signature, { era, nonce }: ExtrinsicPayloadV1): IExtrinsicSignature {
this.set('era', era);
this.set('nonce', nonce);
this.set('signer', signer);
@@ -112,7 +112,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
* @description Adds a raw signature
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsicSignature {
return this.injectSignature(
return this._injectSignature(
this.registry.createType('Address', signer),
this.registry.createType('Signature', signature),
new ExtrinsicPayloadV1(this.registry, payload)
@@ -142,7 +142,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
const payload = this.createPayload(method, options);
const signature = this.registry.createType('Signature', payload.sign(account));
return this.injectSignature(signer, signature, payload);
return this._injectSignature(signer, signature, payload);
}
/**
@@ -153,7 +153,7 @@ export default class ExtrinsicSignatureV1 extends Struct implements IExtrinsicSi
const payload = this.createPayload(method, options);
const signature = this.registry.createType('Signature', new Uint8Array(64).fill(0x42));
return this.injectSignature(signer, signature, payload);
return this._injectSignature(signer, signature, payload);
}
/**
+2 -2
View File
@@ -50,11 +50,11 @@ function decodeU8a (registry: Registry, value: Uint8Array): AccountId | AccountI
*/
export default class Address extends Base<AccountId | AccountIndex> {
constructor (registry: Registry, value: AnyAddress = new Uint8Array()) {
super(registry, Address.decodeAddress(registry, value));
super(registry, Address._decodeAddress(registry, value));
}
/** @internal */
private static decodeAddress (registry: Registry, value: AnyAddress): AccountId | AccountIndex {
private static _decodeAddress (registry: Registry, value: AnyAddress): AccountId | AccountIndex {
if (value instanceof AccountId || value instanceof AccountIndex) {
return value;
} else if (value instanceof Address) {
@@ -57,7 +57,7 @@ export default class ConsensusEngineId extends U32 {
return this.eq(CID_GRPA);
}
private getAuraAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId {
private _getAuraAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId {
return sessionValidators[
this.registry.createType('RawAuraPreDigest', bytes.toU8a(true))
.slotNumber
@@ -66,7 +66,7 @@ export default class ConsensusEngineId extends U32 {
];
}
private getBabeAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId {
private _getBabeAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId {
const digest = this.registry.createType('RawBabePreDigestCompat', bytes.toU8a(true));
return sessionValidators[
@@ -80,9 +80,9 @@ export default class ConsensusEngineId extends U32 {
public extractAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId | undefined {
if (sessionValidators?.length) {
if (this.isAura) {
return this.getAuraAuthor(bytes, sessionValidators);
return this._getAuraAuthor(bytes, sessionValidators);
} else if (this.isBabe) {
return this.getBabeAuthor(bytes, sessionValidators);
return this._getBabeAuthor(bytes, sessionValidators);
}
}