Indicate protected raw with _raw in Base codec (#2153)

This commit is contained in:
Jaco Greeff
2020-04-10 13:54:15 +02:00
committed by GitHub
parent e0dde35621
commit 8aa1bd5bd0
10 changed files with 77 additions and 78 deletions
+10 -11
View File
@@ -10,8 +10,8 @@ import { SubmittableExtrinsic } from '../submittable/types';
import { ApiInterfaceRx, ApiOptions, ApiTypes, DecorateMethod, DecoratedRpc, DecoratedRpcSection, QueryableModuleStorage, QueryableStorage, QueryableStorageEntry, QueryableStorageMulti, QueryableStorageMultiArg, SubmittableExtrinsicFunction, SubmittableExtrinsics, SubmittableModuleExtrinsics } from '../types';
import BN from 'bn.js';
import { BehaviorSubject, Observable, forkJoin, combineLatest, of } from 'rxjs';
import { map, switchMap, take, tap, toArray } from 'rxjs/operators';
import { BehaviorSubject, Observable, combineLatest, from, of } from 'rxjs';
import { concatMap, map, switchMap, take, tap, toArray } from 'rxjs/operators';
import decorateDerive, { ExactDerive } from '@polkadot/api-derive';
import { memo } from '@polkadot/api-derive/util';
import DecoratedMeta from '@polkadot/metadata/Decorated';
@@ -500,16 +500,15 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
switchMap((keys): Observable<[StorageKey[], Option<Raw>[][]]> =>
combineLatest([
of(keys),
forkJoin(
Array(Math.ceil(keys.length / PAGE_SIZE_VALS))
.fill(0)
.map((_, index): Observable<Option<Raw>[]> => {
const keyset = keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS);
from(Array(Math.ceil(keys.length / PAGE_SIZE_VALS)).fill(0)).pipe(
concatMap((_, index): Observable<Option<Raw>[]> => {
const keyset = keys.slice(index * PAGE_SIZE_VALS, (index * PAGE_SIZE_VALS) + PAGE_SIZE_VALS);
return this._rpcCore.state.queryStorageAt
? this._rpcCore.state.queryStorageAt<Option<Raw>[]>(keyset)
: this._rpcCore.state.subscribeStorage<Option<Raw>[]>(keyset).pipe(take(1));
})
return this._rpcCore.state.queryStorageAt
? this._rpcCore.state.queryStorageAt<Option<Raw>[]>(keyset)
: this._rpcCore.state.subscribeStorage<Option<Raw>[]>(keyset).pipe(take(1));
}),
toArray()
)
])
),
+9 -9
View File
@@ -16,11 +16,11 @@ import Raw from './Raw';
export default abstract class Base<T extends Codec> implements Codec {
public readonly registry: Registry;
protected readonly raw: T;
protected readonly _raw: T;
protected constructor (registry: Registry, value?: any) {
this.registry = registry;
this.raw = value;
this._raw = value;
}
/**
@@ -41,42 +41,42 @@ export default abstract class Base<T extends Codec> implements Codec {
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.raw.isEmpty;
return this._raw.isEmpty;
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: any): boolean {
return this.raw.eq(other);
return this._raw.eq(other);
}
/**
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
*/
public toHex (isLe?: boolean): string {
return this.raw.toHex(isLe);
return this._raw.toHex(isLe);
}
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExtended?: boolean): AnyJson {
return this.raw.toHuman(isExtended);
return this._raw.toHuman(isExtended);
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this.raw.toJSON();
return this._raw.toJSON();
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.raw.toString();
return this._raw.toString();
}
/**
@@ -84,7 +84,7 @@ export default abstract class Base<T extends Codec> implements Codec {
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (isBare?: BareOpts): Uint8Array {
return this.raw.toU8a(isBare);
return this._raw.toU8a(isBare);
}
/**
+9 -9
View File
@@ -59,7 +59,7 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
/** @internal */
public static decodeCompact<T extends CompactEncodable> (registry: Registry, Type: Constructor<T>, value: Compact<T> | AnyNumber): CompactEncodable {
if (value instanceof Compact) {
return new Type(registry, value.raw);
return new Type(registry, value._raw);
} else if (isString(value) || isNumber(value) || isBn(value) || isBigInt(value)) {
return new Type(registry, value);
}
@@ -73,16 +73,16 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
* @description Returns the number of bits in the value
*/
public bitLength (): number {
return this.raw.bitLength();
return this._raw.bitLength();
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: any): boolean {
return this.raw.eq(
return this._raw.eq(
other instanceof Compact
? other.raw
? other._raw
: other
);
}
@@ -91,21 +91,21 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
* @description Returns the BN representation of the number
*/
public toBn (): BN {
return this.raw.toBn();
return this._raw.toBn();
}
/**
* @description Returns the number representation for the value
*/
public toNumber (): number {
return this.raw.toNumber();
return this._raw.toNumber();
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `Compact<${this.raw.toRawType()}>`;
return `Compact<${this._raw.toRawType()}>`;
}
/**
@@ -114,13 +114,13 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return Compact.encodeU8a(this.raw.toBn());
return Compact.encodeU8a(this._raw.toBn());
}
/**
* @description Returns the embedded [[UInt]] or [[Moment]] value
*/
public unwrap (): T {
return this.raw;
return this._raw;
}
}
+6 -6
View File
@@ -172,7 +172,7 @@ export default class Enum extends Base<Codec> {
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return 1 + this.raw.encodedLength;
return 1 + this._raw.encodedLength;
}
/**
@@ -200,7 +200,7 @@ export default class Enum extends Base<Codec> {
* @description Checks if the Enum points to a [[Null]] type (deprecated, use isNone)
*/
public get isNull (): boolean {
return this.raw instanceof Null;
return this._raw instanceof Null;
}
/**
@@ -228,7 +228,7 @@ export default class Enum extends Base<Codec> {
* @description The value of the enum
*/
public get value (): Codec {
return this.raw;
return this._raw;
}
/**
@@ -267,7 +267,7 @@ export default class Enum extends Base<Codec> {
public toHuman (isExtended?: boolean): AnyJson {
return this._isBasic
? this.type
: { [this.type]: this.raw.toHuman(isExtended) };
: { [this.type]: this._raw.toHuman(isExtended) };
}
/**
@@ -276,7 +276,7 @@ export default class Enum extends Base<Codec> {
public toJSON (): AnyJson {
return this._isBasic
? this.type
: { [this.type]: this.raw.toJSON() };
: { [this.type]: this._raw.toJSON() };
}
/**
@@ -320,7 +320,7 @@ export default class Enum extends Base<Codec> {
return u8aConcat(
new Uint8Array([index]),
this.raw.toU8a(isBare)
this._raw.toU8a(isBare)
);
}
}
+6 -6
View File
@@ -70,7 +70,7 @@ export default class Option<T extends Codec> extends Base<T> {
*/
public get encodedLength (): number {
// boolean byte (has value, doesn't have) along with wrapped length
return 1 + this.raw.encodedLength;
return 1 + this._raw.encodedLength;
}
/**
@@ -84,7 +84,7 @@ export default class Option<T extends Codec> extends Base<T> {
* @description Checks if the Option has no value
*/
public get isNone (): boolean {
return this.raw instanceof Null;
return this._raw instanceof Null;
}
/**
@@ -98,7 +98,7 @@ export default class Option<T extends Codec> extends Base<T> {
* @description The actual value for the Option
*/
public get value (): Codec {
return this.raw;
return this._raw;
}
/**
@@ -140,14 +140,14 @@ export default class Option<T extends Codec> extends Base<T> {
*/
public toU8a (isBare?: boolean): Uint8Array {
if (isBare) {
return this.raw.toU8a(true);
return this._raw.toU8a(true);
}
const u8a = new Uint8Array(this.encodedLength);
if (this.isSome) {
u8a.set([1]);
u8a.set(this.raw.toU8a(), 1);
u8a.set(this._raw.toU8a(), 1);
}
return u8a;
@@ -161,7 +161,7 @@ export default class Option<T extends Codec> extends Base<T> {
throw new Error('Option: unwrapping a None value');
}
return this.raw;
return this._raw;
}
/**
+1 -1
View File
@@ -50,7 +50,7 @@ export default class Result<O extends Codec, E extends Codec> extends Enum {
* @description Checks if the Result has no value
*/
public get isEmpty (): boolean {
return this.isOk && this.raw.isEmpty;
return this.isOk && this._raw.isEmpty;
}
/**
+13 -13
View File
@@ -71,7 +71,7 @@ abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
* @description The era for this extrinsic
*/
public get era (): ExtrinsicEra {
return (this.raw as ExtrinsicVx).signature.era;
return (this._raw as ExtrinsicVx).signature.era;
}
/**
@@ -92,7 +92,7 @@ abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
* @description `true` id the extrinsic is signed
*/
public get isSigned (): boolean {
return (this.raw as ExtrinsicVx).signature.isSigned;
return (this._raw as ExtrinsicVx).signature.isSigned;
}
/**
@@ -113,42 +113,42 @@ abstract class ExtrinsicBase extends Base<ExtrinsicVx | ExtrinsicUnknown> {
* @description The [[Call]] this extrinsic wraps
*/
public get method (): Call {
return (this.raw as ExtrinsicVx).method;
return (this._raw as ExtrinsicVx).method;
}
/**
* @description The nonce for this extrinsic
*/
public get nonce (): Compact<Index> {
return (this.raw as ExtrinsicVx).signature.nonce;
return (this._raw as ExtrinsicVx).signature.nonce;
}
/**
* @description The actual [[EcdsaSignature]], [[Ed25519Signature]] or [[Sr25519Signature]]
*/
public get signature (): EcdsaSignature | Ed25519Signature | Sr25519Signature {
return (this.raw as ExtrinsicVx).signature.signature;
return (this._raw as ExtrinsicVx).signature.signature;
}
/**
* @description The [[Address]] that signed
*/
public get signer (): Address {
return (this.raw as ExtrinsicVx).signature.signer;
return (this._raw as ExtrinsicVx).signature.signer;
}
/**
* @description Forwards compat
*/
public get tip (): Compact<Balance> {
return (this.raw as ExtrinsicVx).signature.tip;
return (this._raw as ExtrinsicVx).signature.tip;
}
/**
* @description Returns the raw transaction version (not flagged with signing information)
*/
public get type (): number {
return (this.raw as ExtrinsicVx).version;
return (this._raw as ExtrinsicVx).version;
}
/**
@@ -179,7 +179,7 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
/** @internal */
private static newFromValue (registry: Registry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown {
if (value instanceof Extrinsic) {
return value.raw;
return value._raw;
}
const isSigned = (version & BIT_SIGNED) === BIT_SIGNED;
@@ -221,7 +221,7 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
* @description Injects an already-generated signature into the extrinsic
*/
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): Extrinsic {
(this.raw as ExtrinsicVx).addSignature(signer, signature, payload);
(this._raw as ExtrinsicVx).addSignature(signer, signature, payload);
return this;
}
@@ -230,7 +230,7 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
* @description Sign the extrinsic with a specific keypair
*/
public sign (account: IKeyringPair, options: SignatureOptions): Extrinsic {
(this.raw as ExtrinsicVx).sign(account, options);
(this._raw as ExtrinsicVx).sign(account, options);
return this;
}
@@ -239,7 +239,7 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
* @describe Adds a fake signature to the extrinsic
*/
public signFake (signer: Address | Uint8Array | string, options: SignatureOptions): Extrinsic {
(this.raw as ExtrinsicVx).signFake(signer, options);
(this._raw as ExtrinsicVx).signFake(signer, options);
return this;
}
@@ -292,7 +292,7 @@ export default class Extrinsic extends ExtrinsicBase implements IExtrinsic {
public toU8a (isBare?: boolean): Uint8Array {
// we do not apply bare to the internal values, rather this only determines out length addition,
// where we strip all lengths this creates an extrinsic that cannot be decoded
const encoded = u8aConcat(new Uint8Array([this.version]), this.raw.toU8a());
const encoded = u8aConcat(new Uint8Array([this.version]), this._raw.toU8a());
return isBare
? encoded
@@ -44,7 +44,7 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
/** @internal */
public static decodeExtrinsicPayload (registry: Registry, value: ExtrinsicPayload | ExtrinsicPayloadValue | Uint8Array | string | undefined, version: number = DEFAULT_VERSION): ExtrinsicPayloadVx {
if (value instanceof ExtrinsicPayload) {
return value.raw;
return value._raw;
}
return registry.createType(VERSIONS[version] || VERSIONS[0], value, { version }) as ExtrinsicPayloadVx;
@@ -54,14 +54,14 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
* @description The block [[Hash]] the signature applies to (mortal/immortal)
*/
public get blockHash (): Hash {
return this.raw.blockHash;
return this._raw.blockHash;
}
/**
* @description The [[ExtrinsicEra]]
*/
public get era (): ExtrinsicEra {
return this.raw.era;
return this._raw.era;
}
/**
@@ -69,21 +69,21 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
*/
public get genesisHash (): Hash {
// NOTE only v3+
return (this.raw as ExtrinsicPayloadV3).genesisHash || this.registry.createType('Hash');
return (this._raw as ExtrinsicPayloadV3).genesisHash || this.registry.createType('Hash');
}
/**
* @description The [[Raw]] contained in the payload
*/
public get method (): Raw {
return this.raw.method;
return this._raw.method;
}
/**
* @description The [[Index]]
*/
public get nonce (): Compact<Index> {
return this.raw.nonce;
return this._raw.nonce;
}
/**
@@ -91,7 +91,7 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
*/
public get specVersion (): u32 {
// NOTE only v3+
return (this.raw as ExtrinsicPayloadV3).specVersion || this.registry.createType('u32');
return (this._raw as ExtrinsicPayloadV3).specVersion || this.registry.createType('u32');
}
/**
@@ -99,21 +99,21 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
*/
public get tip (): Compact<Balance> {
// NOTE from v2+
return (this.raw as ExtrinsicPayloadV2).tip || this.registry.createType('Compact<Balance>');
return (this._raw as ExtrinsicPayloadV2).tip || this.registry.createType('Compact<Balance>');
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: any): boolean {
return this.raw.eq(other);
return this._raw.eq(other);
}
/**
* @description Sign the payload with the keypair
*/
public sign (signerPair: IKeyringPair): { signature: string } {
const signature = this.raw.sign(signerPair);
const signature = this._raw.sign(signerPair);
// This is extensible, so we could quite readily extend to send back extra
// information, such as for instance the payload, i.e. `payload: this.toHex()`
@@ -128,7 +128,7 @@ export default class ExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExtended?: boolean): AnyJson {
return this.raw.toHuman(isExtended);
return this._raw.toHuman(isExtended);
}
/**
+8 -8
View File
@@ -58,7 +58,7 @@ export default class Address extends Base<AccountId | AccountIndex> {
if (value instanceof AccountId || value instanceof AccountIndex) {
return value;
} else if (value instanceof Address) {
return value.raw;
return value._raw;
} else if (isBn(value) || isNumber(value)) {
return registry.createType('AccountIndex', value);
} else if (Array.isArray(value) || isHex(value) || isU8a(value)) {
@@ -72,7 +72,7 @@ export default class Address extends Base<AccountId | AccountIndex> {
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
const rawLength = this.rawLength;
const rawLength = this._rawLength;
return rawLength + (
// for 1 byte AccountIndexes, we are not adding a specific prefix
@@ -85,10 +85,10 @@ export default class Address extends Base<AccountId | AccountIndex> {
/**
* @description The length of the raw value, either AccountIndex or AccountId
*/
public get rawLength (): number {
return this.raw instanceof AccountIndex
? AccountIndex.calcLength(this.raw)
: this.raw.encodedLength;
protected get _rawLength (): number {
return this._raw instanceof AccountIndex
? AccountIndex.calcLength(this._raw)
: this._raw.encodedLength;
}
/**
@@ -110,12 +110,12 @@ export default class Address extends Base<AccountId | AccountIndex> {
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (isBare?: boolean): Uint8Array {
const encoded = this.raw.toU8a().subarray(0, this.rawLength);
const encoded = this._raw.toU8a().subarray(0, this._rawLength);
return isBare
? encoded
: u8aConcat(
this.raw instanceof AccountIndex
this._raw instanceof AccountIndex
? AccountIndex.writeLength(encoded)
: ACCOUNT_ID_PREFIX,
encoded
+4 -4
View File
@@ -67,11 +67,11 @@ export default class Data extends Enum {
}
get asRaw (): Bytes {
return this.raw as Bytes;
return this._raw as Bytes;
}
get asSha256 (): H256 {
return this.raw as H256;
return this._raw as H256;
}
get isRaw (): boolean {
@@ -97,7 +97,7 @@ export default class Data extends Enum {
return new Uint8Array(1);
} else if (this.index === 1) {
// don't add the length, just the data
const data = this.raw.toU8a(true);
const data = this._raw.toU8a(true);
const length = Math.min(data.length, 32);
const u8a = new Uint8Array(length + 1);
@@ -111,7 +111,7 @@ export default class Data extends Enum {
const u8a = new Uint8Array(33);
u8a.set([this.index + 32], 0);
u8a.set(this.raw.toU8a(), 1);
u8a.set(this._raw.toU8a(), 1);
return u8a;
}