Adjust class privates via #, where applicable (#1921)

* Adjust class priates via #, where applicable

* Remove stray log

* Cleanup rpc-provider

* Small adjustments

* No private access in tests

* Submittable privates

* Use derive for nonce derivation

* Tweak observers

* Alphabetical privates
This commit is contained in:
Jaco Greeff
2020-02-22 11:29:50 +01:00
committed by GitHub
parent ccf32a6289
commit 3bf4c3fac5
32 changed files with 406 additions and 514 deletions
+12 -12
View File
@@ -30,19 +30,19 @@ export function isBigInt (value: any): value is BigInt {
export default abstract class AbstractInt extends BN implements Codec {
public readonly registry: Registry;
protected _bitLength: UIntBitLength;
readonly #bitLength: UIntBitLength;
private _isHexJson: boolean;
readonly #isHexJson: boolean;
private _isSigned: boolean;
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));
this.registry = registry;
this._bitLength = bitLength;
this._isHexJson = isHexJson;
this._isSigned = isSigned;
this.#bitLength = bitLength;
this.#isHexJson = isHexJson;
this.#isSigned = isSigned;
assert(super.bitLength() <= bitLength, `${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${bitLength}`);
}
@@ -83,7 +83,7 @@ export default abstract class AbstractInt extends BN implements Codec {
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this._bitLength / 8;
return this.#bitLength / 8;
}
/**
@@ -104,14 +104,14 @@ export default abstract class AbstractInt extends BN implements Codec {
* @description Checks if the value is an unsigned type
*/
public get isUnsigned (): boolean {
return !this._isSigned;
return !this.#isSigned;
}
/**
* @description Returns the number of bits in the value
*/
public bitLength (): UIntBitLength {
return this._bitLength;
return this.#bitLength;
}
/**
@@ -123,7 +123,7 @@ export default abstract class AbstractInt extends BN implements Codec {
// number and BN inputs (no `.eqn` needed) - numbers will be converted
return super.eq(
isHex(other)
? hexToBn(other.toString(), { isLe: false, isNegative: this._isSigned })
? hexToBn(other.toString(), { isLe: false, isNegative: this.#isSigned })
: bnToBn(other)
);
}
@@ -134,7 +134,7 @@ export default abstract class AbstractInt extends BN implements Codec {
public isMax (): boolean {
const u8a = this.toU8a().filter((byte): boolean => byte === 0xff);
return u8a.length === (this._bitLength / 8);
return u8a.length === (this.#bitLength / 8);
}
/**
@@ -164,7 +164,7 @@ export default abstract class AbstractInt extends BN implements Codec {
// FIXME this return type should by string | number, but BN's return type
// is string.
// Maximum allowed integer for JS is 2^53 - 1, set limit at 52
return this._isHexJson || (super.bitLength() > 52)
return this.#isHexJson || (super.bitLength() > 52)
? this.toHex()
: this.toNumber();
}
+2 -2
View File
@@ -15,9 +15,9 @@ import { compareMap, decodeU8a, typeToConstructor } from './utils';
export default class BTreeMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements Codec {
public readonly registry: Registry;
protected _KeyClass: Constructor<K>;
private readonly _KeyClass: Constructor<K>;
protected _ValClass: Constructor<V>;
private readonly _ValClass: Constructor<V>;
constructor (registry: Registry, keyType: Constructor<K> | InterfaceTypes, valType: Constructor<V> | InterfaceTypes, rawValue: any) {
const KeyClass = typeToConstructor(registry, keyType);
+1 -1
View File
@@ -15,7 +15,7 @@ import { compareSet, decodeU8a, typeToConstructor } from './utils';
export default class BTreeSet<V extends Codec = Codec> extends Set<V> implements Codec {
public readonly registry: Registry;
protected _ValClass: Constructor<V>;
private readonly _ValClass: Constructor<V>;
constructor (registry: Registry, valType: Constructor<V> | InterfaceTypes, rawValue: any) {
const ValClass = typeToConstructor(registry, valType);
+1 -1
View File
@@ -16,7 +16,7 @@ import Raw from './Raw';
export default abstract class Base<T extends Codec> implements Codec {
public readonly registry: Registry;
protected raw: T;
protected readonly raw: T;
protected constructor (registry: Registry, value?: any) {
this.registry = registry;
-3
View File
@@ -27,13 +27,10 @@ const BITLENGTH: UIntBitLength = 64;
export default class CodecDate extends Date implements Codec {
public readonly registry: Registry;
protected raw: Date; // FIXME Remove this once we convert all types out of Base
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
super(CodecDate.decodeDate(value));
this.registry = registry;
this.raw = this;
}
/** @internal */
+3 -3
View File
@@ -40,7 +40,7 @@ export default class Int extends AbstractInt {
*/
public toHex (isLe = false): string {
return bnToHex(this, {
bitLength: this._bitLength,
bitLength: this.bitLength(),
isLe,
isNegative: true
});
@@ -50,7 +50,7 @@ export default class Int extends AbstractInt {
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `i${this._bitLength}`;
return `i${this.bitLength()}`;
}
/**
@@ -60,7 +60,7 @@ export default class Int extends AbstractInt {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return bnToU8a(this, {
bitLength: this._bitLength,
bitLength: this.bitLength(),
isLe: true,
isNegative: true
});
+3 -3
View File
@@ -42,7 +42,7 @@ export default class UInt extends AbstractInt {
public toHex (isLe = false): string {
// For display/JSON, this is BE, for compare, use isLe
return bnToHex(this, {
bitLength: this._bitLength,
bitLength: this.bitLength(),
isLe,
isNegative: false
});
@@ -67,7 +67,7 @@ export default class UInt extends AbstractInt {
// underlying it always matches (no matter which length it actually is)
return this instanceof ClassOf(this.registry, 'Balance')
? 'Balance'
: `u${this._bitLength}`;
: `u${this.bitLength()}`;
}
/**
@@ -77,7 +77,7 @@ export default class UInt extends AbstractInt {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return bnToU8a(this, {
bitLength: this._bitLength,
bitLength: this.bitLength(),
isLe: true,
isNegative: false
});
+40 -40
View File
@@ -77,21 +77,21 @@ function decorateExtrinsics (registry: Registry, metadata: RegistryMetadata, met
}
export class TypeRegistry implements Registry {
private _chainProperties?: ChainProperties;
readonly #classes: Map<string, Constructor> = new Map();
private _classes: Map<string, Constructor> = new Map();
readonly #definitions: Map<string, string> = new Map();
private _definitions: Map<string, string> = new Map();
readonly #metadataCalls: Record<string, CallFunction> = {};
private _metadataCalls: Record<string, CallFunction> = {};
readonly #metadataErrors: Record<string, RegistryError> = {};
private _metadataErrors: Record<string, RegistryError> = {};
readonly #metadataEvents: Record<string, Constructor<EventData>> = {};
private _metadataEvents: Record<string, Constructor<EventData>> = {};
readonly #unknownTypes: Map<string, boolean> = new Map();
private _metadataExtensions: string[] = defaultExtensions;
#chainProperties?: ChainProperties;
private _unknownTypes: Map<string, boolean> = new Map();
#metadataExtensions: string[] = defaultExtensions;
constructor () {
// we only want to import these on creation, i.e. we want to avoid types
@@ -110,27 +110,27 @@ export class TypeRegistry implements Registry {
}
public get chainDecimals (): number {
return this._chainProperties?.tokenDecimals.isSome
? this._chainProperties.tokenDecimals.unwrap().toNumber()
return this.#chainProperties?.tokenDecimals.isSome
? this.#chainProperties.tokenDecimals.unwrap().toNumber()
: 12;
}
public get chainSS58 (): number | undefined {
return this._chainProperties?.ss58Format.isSome
? this._chainProperties.ss58Format.unwrap().toNumber()
return this.#chainProperties?.ss58Format.isSome
? this.#chainProperties.ss58Format.unwrap().toNumber()
: undefined;
}
public get chainToken (): string {
return this._chainProperties?.tokenSymbol.isSome
? this._chainProperties.tokenSymbol.unwrap().toString()
return this.#chainProperties?.tokenSymbol.isSome
? this.#chainProperties.tokenSymbol.unwrap().toString()
: formatBalance.getDefaults().unit;
}
// find a specific call
public findMetaCall (callIndex: Uint8Array): CallFunction {
const hexIndex = u8aToHex(callIndex);
const fn = this._metadataCalls[hexIndex];
const fn = this.#metadataCalls[hexIndex];
assert(!isUndefined(fn), `findMetaCall: Unable to find Call with index ${hexIndex}/[${callIndex}]`);
@@ -144,7 +144,7 @@ export class TypeRegistry implements Registry {
? errorIndex
: new Uint8Array([errorIndex.index.toNumber(), errorIndex.error.toNumber()])
);
const error = this._metadataErrors[hexIndex];
const error = this.#metadataErrors[hexIndex];
assert(!isUndefined(error), `findMetaError: Unable to find Error with index ${hexIndex}/[${errorIndex}]`);
@@ -153,7 +153,7 @@ export class TypeRegistry implements Registry {
public findMetaEvent (eventIndex: Uint8Array): Constructor<EventData> {
const hexIndex = u8aToHex(eventIndex);
const Event = this._metadataEvents[hexIndex];
const Event = this.#metadataEvents[hexIndex];
assert(!isUndefined(Event), `findMetaEvent: Unable to find Event with index ${hexIndex}/[${eventIndex}]`);
@@ -161,11 +161,11 @@ export class TypeRegistry implements Registry {
}
public get <T extends Codec = Codec> (name: string, withUnknown?: boolean): Constructor<T> | undefined {
let Type = this._classes.get(name);
let Type = this.#classes.get(name);
// we have not already created the type, attempt it
if (!Type) {
const definition = this._definitions.get(name);
const definition = this.#definitions.get(name);
let BaseType: Constructor<Codec> | undefined;
// we have a definition, so create the class now (lazily)
@@ -174,7 +174,7 @@ export class TypeRegistry implements Registry {
} else if (withUnknown) {
console.warn(`Unable to resolve type ${name}, it will fail on constrution`);
this._unknownTypes.set(name, true);
this.#unknownTypes.set(name, true);
BaseType = Unconstructable.with(name);
}
@@ -184,7 +184,7 @@ export class TypeRegistry implements Registry {
// Additionally, we now pass through the registry, which is a link to ourselves
Type = class extends BaseType {};
this._classes.set(name, Type);
this.#classes.set(name, Type);
}
}
@@ -192,11 +192,11 @@ export class TypeRegistry implements Registry {
}
public getChainProperties (): ChainProperties | undefined {
return this._chainProperties;
return this.#chainProperties;
}
public getDefinition (name: string): string | undefined {
return this._definitions.get(name);
return this.#definitions.get(name);
}
public getOrThrow <T extends Codec = Codec> (name: string, msg?: string): Constructor<T> {
@@ -214,23 +214,23 @@ export class TypeRegistry implements Registry {
}
public getSignedExtensionExtra (): Record<string, InterfaceTypes> {
return expandExtensionTypes(this._metadataExtensions, 'extra');
return expandExtensionTypes(this.#metadataExtensions, 'extra');
}
public getSignedExtensionTypes (): Record<string, InterfaceTypes> {
return expandExtensionTypes(this._metadataExtensions, 'types');
return expandExtensionTypes(this.#metadataExtensions, 'types');
}
public hasClass (name: string): boolean {
return this._classes.has(name);
return this.#classes.has(name);
}
public hasDef (name: string): boolean {
return this._definitions.has(name);
return this.#definitions.has(name);
}
public hasType (name: string): boolean {
return !this._unknownTypes.get(name) && (this.hasClass(name) || this.hasDef(name));
return !this.#unknownTypes.get(name) && (this.hasClass(name) || this.hasDef(name));
}
public register (type: Constructor | RegistryTypes): void;
@@ -242,11 +242,11 @@ export class TypeRegistry implements Registry {
public register (arg1: string | Constructor | RegistryTypes, arg2?: Constructor): void {
// NOTE Constructors appear as functions here
if (isFunction(arg1)) {
this._classes.set(arg1.name, arg1);
this.#classes.set(arg1.name, arg1);
} else if (isString(arg1)) {
assert(isFunction(arg2), `Expected class definition passed to '${arg1}' registration`);
this._classes.set(arg1, arg2);
this.#classes.set(arg1, arg2);
} else {
this.registerObject(arg1);
}
@@ -256,18 +256,18 @@ export class TypeRegistry implements Registry {
Object.entries(obj).forEach(([name, type]): void => {
if (isFunction(type)) {
// This _looks_ a bit funny, but `typeof Clazz === 'function'
this._classes.set(name, type);
this.#classes.set(name, type);
} else {
const def = isString(type)
? type
: JSON.stringify(type);
// we already have this type, remove the classes registered for it
if (this._classes.has(name)) {
this._classes.delete(name);
if (this.#classes.has(name)) {
this.#classes.delete(name);
}
this._definitions.set(name, def);
this.#definitions.set(name, def);
}
});
}
@@ -275,22 +275,22 @@ export class TypeRegistry implements Registry {
// sets the chain properties
public setChainProperties (properties?: ChainProperties): void {
if (properties) {
this._chainProperties = properties;
this.#chainProperties = properties;
}
}
// sets the metadata
public setMetadata (metadata: RegistryMetadata): void {
decorateExtrinsics(this, metadata, this._metadataCalls);
decorateErrors(this, metadata, this._metadataErrors);
decorateEvents(this, metadata, this._metadataEvents);
decorateExtrinsics(this, metadata, this.#metadataCalls);
decorateErrors(this, metadata, this.#metadataErrors);
decorateEvents(this, metadata, this.#metadataEvents);
// setup the available extensions
this._metadataExtensions = metadata.asLatest.extrinsic.version.gtn(0)
this.#metadataExtensions = metadata.asLatest.extrinsic.version.gtn(0)
? metadata.asLatest.extrinsic.signedExtensions.map((key): string => key.toString())
: defaultExtensions;
const unknown = findUnknownExtensions(this._metadataExtensions);
const unknown = findUnknownExtensions(this.#metadataExtensions);
if (unknown.length) {
console.warn(`Unknown signed extensions [${unknown.join(', ')}] found, treating them as no-efect`);
+12 -12
View File
@@ -17,49 +17,49 @@ import Null from '../primitive/Null';
* Wrapper for the actual data that forms part of an [[Event]]
*/
export class EventData extends Tuple {
private _meta: EventMetadataLatest;
readonly #meta: EventMetadataLatest;
private _method: string;
readonly #method: string;
private _section: string;
readonly #section: string;
private _typeDef: TypeDef[];
readonly #typeDef: TypeDef[];
constructor (registry: Registry, Types: Constructor[], value: Uint8Array, typeDef: TypeDef[], meta: RegistryMetadataEvent, section: string, method: string) {
super(registry, Types, value);
this._meta = meta as EventMetadataLatest;
this._method = method;
this._section = section;
this._typeDef = typeDef;
this.#meta = meta as EventMetadataLatest;
this.#method = method;
this.#section = section;
this.#typeDef = typeDef;
}
/**
* @description The wrapped [[EventMetadata]]
*/
public get meta (): EventMetadataLatest {
return this._meta;
return this.#meta;
}
/**
* @description The method as a string
*/
public get method (): string {
return this._method;
return this.#method;
}
/**
* @description The section as a string
*/
public get section (): string {
return this._section;
return this.#section;
}
/**
* @description The [[TypeDef]] for this event
*/
public get typeDef (): TypeDef[] {
return this._typeDef;
return this.#typeDef;
}
}
+1
View File
@@ -4,6 +4,7 @@
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'), __dirname);
+4 -3
View File
@@ -50,7 +50,8 @@ function decodeText (value: Text | string | AnyU8a | { toString: () => string })
// - Strings should probably be trimmed (docs do come through with extra padding)
export default class Text extends String implements Codec {
public readonly registry: Registry;
private _override: string | null = null;
#override: string | null = null;
constructor (registry: Registry, value: Text | string | AnyU8a | { toString: () => string } = '') {
super(decodeText(value));
@@ -100,7 +101,7 @@ export default class Text extends String implements Codec {
* @description Set an override value for this
*/
public setOverride (override: string): void {
this._override = override;
this.#override = override;
}
/**
@@ -137,7 +138,7 @@ export default class Text extends String implements Codec {
* @description Returns the string representation of the value
*/
public toString (): string {
return this._override || super.toString();
return this.#override || super.toString();
}
/**