Adjust private members to JS privates (#4297)

This commit is contained in:
Jaco
2021-12-08 11:28:19 +02:00
committed by GitHub
parent 2463b52381
commit 93ce2210da
5 changed files with 56 additions and 56 deletions
@@ -48,20 +48,20 @@ const knownTypes: Record<string, string> = {
* A generic signer payload that can be used for serialization between API and signer
*/
export class GenericSignerPayload extends Struct implements ISignerPayload, SignerPayloadType {
private readonly _extraTypes: Record<string, string>;
readonly #extraTypes: Record<string, string>;
constructor (registry: Registry, value?: HexString | { [x: string]: unknown; } | Map<unknown, unknown> | unknown[]) {
const extensionTypes = objectSpread<Record<string, string>>({}, registry.getSignedExtensionTypes(), registry.getSignedExtensionExtra());
super(registry, objectSpread<Record<string, string>>({}, extensionTypes, knownTypes), value);
this._extraTypes = {};
this.#extraTypes = {};
const getter = (key: string) => this.get(key);
// add all extras that are not in the base types
for (const [key, type] of Object.entries(extensionTypes)) {
if (!knownTypes[key]) {
this._extraTypes[key] = type;
this.#extraTypes[key] = type;
}
objectProperty(this, key, getter);
@@ -117,7 +117,7 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign
*/
public toPayload (): SignerPayloadJSON {
const result: Record<string, string> = {};
const keys = Object.keys(this._extraTypes);
const keys = Object.keys(this.#extraTypes);
// add any explicit overrides we may have
for (let i = 0; i < keys.length; i++) {
+24 -24
View File
@@ -15,6 +15,27 @@ export const CID_BABE = stringToU8a('BABE');
export const CID_GRPA = stringToU8a('FRNK');
export const CID_POW = stringToU8a('pow_');
function getAuraAuthor (registry: Registry, bytes: Bytes, sessionValidators: AccountId[]): AccountId {
return sessionValidators[
registry.createType('RawAuraPreDigest', bytes.toU8a(true))
.slotNumber
.mod(new BN(sessionValidators.length))
.toNumber()
];
}
function getBabeAuthor (registry: Registry, bytes: Bytes, sessionValidators: AccountId[]): AccountId {
const digest = registry.createType('RawBabePreDigestCompat', bytes.toU8a(true));
return sessionValidators[
(digest.value as u32).toNumber()
];
}
function getBytesAsAuthor (registry: Registry, bytes: Bytes): AccountId {
return registry.createType('AccountId', bytes);
}
/**
* @name GenericConsensusEngineId
* @description
@@ -59,42 +80,21 @@ export class GenericConsensusEngineId extends U8aFixed {
return this.eq(CID_POW);
}
private _getAuraAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId {
return sessionValidators[
this.registry.createType('RawAuraPreDigest', bytes.toU8a(true))
.slotNumber
.mod(new BN(sessionValidators.length))
.toNumber()
];
}
private _getBabeAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId {
const digest = this.registry.createType('RawBabePreDigestCompat', bytes.toU8a(true));
return sessionValidators[
(digest.value as u32).toNumber()
];
}
private _getBytesAsAuthor (bytes: Bytes): AccountId {
return this.registry.createType('AccountId', bytes);
}
/**
* @description From the input bytes, decode into an author
*/
public extractAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId | undefined {
if (sessionValidators?.length) {
if (this.isAura) {
return this._getAuraAuthor(bytes, sessionValidators);
return getAuraAuthor(this.registry, bytes, sessionValidators);
} else if (this.isBabe) {
return this._getBabeAuthor(bytes, sessionValidators);
return getBabeAuthor(this.registry, bytes, sessionValidators);
}
}
// For pow & Moonbeam, the bytes are the actual author
if (this.isPow || bytes.length === 20) {
return this._getBytesAsAuthor(bytes);
return getBytesAsAuthor(this.registry, bytes);
}
return undefined;
+6 -6
View File
@@ -65,9 +65,9 @@ function decodeVote (registry: Registry, value?: InputTypes): Uint8Array {
* A number of lock periods, plus a vote, one way or the other.
*/
export class GenericVote extends U8aFixed {
private _aye: boolean;
#aye: boolean;
private _conviction: Conviction;
#conviction: Conviction;
constructor (registry: Registry, value?: InputTypes) {
// decoded is just 1 byte
@@ -77,22 +77,22 @@ export class GenericVote extends U8aFixed {
super(registry, decoded, 8);
this._aye = (decoded[0] & AYE_BITS) === AYE_BITS;
this._conviction = this.registry.createType('Conviction', decoded[0] & CON_MASK);
this.#aye = (decoded[0] & AYE_BITS) === AYE_BITS;
this.#conviction = this.registry.createType('Conviction', decoded[0] & CON_MASK);
}
/**
* @description returns a V2 conviction
*/
public get conviction (): Conviction {
return this._conviction;
return this.#conviction;
}
/**
* @description true if the wrapped value is a positive vote
*/
public get isAye (): boolean {
return this._aye;
return this.#aye;
}
/**
+4 -4
View File
@@ -40,21 +40,21 @@ function decodeBitVec (value?: AnyU8a): [number, Uint8Array] {
* and a normal Bytes would be that the length prefix indicates the number of bits encoded, not the bytes
*/
export class BitVec extends Raw {
private readonly _decodedLength: number;
readonly #decodedLength: number;
constructor (registry: Registry, value?: AnyU8a) {
const [decodedLength, u8a] = decodeBitVec(value);
super(registry, u8a);
this._decodedLength = decodedLength;
this.#decodedLength = decodedLength;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get encodedLength (): number {
return this.length + compactToU8a(this._decodedLength).length;
return this.length + compactToU8a(this.#decodedLength).length;
}
public override toHuman (): string {
@@ -77,6 +77,6 @@ export class BitVec extends Raw {
return isBare
? bitVec
: u8aConcat(compactToU8a(this._decodedLength), bitVec);
: u8aConcat(compactToU8a(this.#decodedLength), bitVec);
}
}
+18 -18
View File
@@ -164,22 +164,22 @@ function getType (registry: Registry, value: StorageKey | StorageEntry | [Storag
export class StorageKey<A extends AnyTuple = AnyTuple> extends Bytes implements IStorageKey<A> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore This is assigned via this.decodeArgsFromMeta()
private _args: A;
#args: A;
private _meta?: StorageEntryMetadataLatest;
#meta?: StorageEntryMetadataLatest;
private _outputType: string;
#outputType: string;
private _method?: string;
#method?: string;
private _section?: string;
#section?: string;
constructor (registry: Registry, value?: string | Uint8Array | StorageKey | StorageEntry | [StorageEntry, unknown[]?], override: Partial<StorageKeyExtra> = {}) {
const { key, method, section } = decodeStorageKey(value);
super(registry, key);
this._outputType = getType(registry, value as StorageKey);
this.#outputType = getType(registry, value as StorageKey);
// decode the args (as applicable based on the key and the hashers, after all init)
this.setMeta(getMeta(value as StorageKey), override.section || section, override.method || method);
@@ -189,35 +189,35 @@ export class StorageKey<A extends AnyTuple = AnyTuple> extends Bytes implements
* @description Return the decoded arguments (applicable to map with decodable values)
*/
public get args (): A {
return this._args;
return this.#args;
}
/**
* @description The metadata or `undefined` when not available
*/
public get meta (): StorageEntryMetadataLatest | undefined {
return this._meta;
return this.#meta;
}
/**
* @description The key method or `undefined` when not specified
*/
public get method (): string | undefined {
return this._method;
return this.#method;
}
/**
* @description The output type
*/
public get outputType (): string {
return this._outputType;
return this.#outputType;
}
/**
* @description The key section or `undefined` when not specified
*/
public get section (): string | undefined {
return this._section;
return this.#section;
}
public is (key: IStorageKey<AnyTuple>): key is IStorageKey<A> {
@@ -228,16 +228,16 @@ export class StorageKey<A extends AnyTuple = AnyTuple> extends Bytes implements
* @description Sets the meta for this key
*/
public setMeta (meta?: StorageEntryMetadataLatest, section?: string, method?: string): this {
this._meta = meta;
this._method = method || this._method;
this._section = section || this._section;
this.#meta = meta;
this.#method = method || this.#method;
this.#section = section || this.#section;
if (meta) {
this._outputType = unwrapStorageType(this.registry, meta.type);
this.#outputType = unwrapStorageType(this.registry, meta.type);
}
try {
this._args = decodeArgsFromMeta(this.registry, this.toU8a(true), this.meta);
this.#args = decodeArgsFromMeta(this.registry, this.toU8a(true), meta);
} catch (error) {
// ignore...
}
@@ -249,8 +249,8 @@ export class StorageKey<A extends AnyTuple = AnyTuple> extends Bytes implements
* @description Returns the Human representation for this type
*/
public override toHuman (): AnyJson {
return this._args.length
? this._args.map((arg) => arg.toHuman())
return this.#args.length
? this.#args.map((a) => a.toHuman())
: super.toHuman();
}