Don't abuse _raw in Base (#4332)
* Don't abuse _raw in Base * Adjust WrapperOpaque * Adjust LookupSource
This commit is contained in:
@@ -15,11 +15,11 @@ export abstract class Base<T extends Codec> implements Codec {
|
||||
|
||||
readonly initialU8aLength?: number;
|
||||
|
||||
protected readonly _raw: T;
|
||||
readonly #raw: T;
|
||||
|
||||
protected constructor (registry: CodecRegistry, value: T, initialU8aLength?: number) {
|
||||
this.registry = registry;
|
||||
this._raw = value;
|
||||
this.#raw = value;
|
||||
this.initialU8aLength = initialU8aLength;
|
||||
}
|
||||
|
||||
@@ -37,46 +37,50 @@ export abstract class Base<T extends Codec> implements Codec {
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
public get inner (): T {
|
||||
return this.#raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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?: unknown): 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): HexString {
|
||||
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 +88,7 @@ export 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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,4 +97,8 @@ export abstract class Base<T extends Codec> implements Codec {
|
||||
public toRawType (): string {
|
||||
return 'Base';
|
||||
}
|
||||
|
||||
public unwrap (): T {
|
||||
return this.#raw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,6 +233,13 @@ export class Enum implements IEnum {
|
||||
return this.#indexes[this.#entryIndex];
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The value of the enum
|
||||
*/
|
||||
public get inner (): Codec {
|
||||
return this.#raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description true if this is a basic enum (no values)
|
||||
*/
|
||||
|
||||
@@ -37,7 +37,7 @@ export class WrapperOpaque<T extends Codec> extends Base<T> {
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public override toRawType (): string {
|
||||
return `WrapperOpaque<${this._raw.toRawType()}>`;
|
||||
return `WrapperOpaque<${this.inner.toRawType()}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,11 +50,4 @@ export class WrapperOpaque<T extends Codec> extends Base<T> {
|
||||
? u8a
|
||||
: compactAddLength(u8a);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Return the inner value for the wrapped type
|
||||
*/
|
||||
public unwrap (): T {
|
||||
return this._raw;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ export class GenericEthereumLookupSource extends Base<GenericEthereumAccountId |
|
||||
/** @internal */
|
||||
private static _decodeAddress (registry: CodecRegistry, value: AnyAddress): GenericEthereumAccountId | GenericAccountIndex {
|
||||
return value instanceof GenericEthereumLookupSource
|
||||
? value._raw
|
||||
? value.inner
|
||||
: value instanceof GenericEthereumAccountId || value instanceof GenericAccountIndex
|
||||
? value
|
||||
: isU8a(value) || Array.isArray(value) || isHex(value)
|
||||
@@ -85,9 +85,9 @@ export class GenericEthereumLookupSource extends Base<GenericEthereumAccountId |
|
||||
* @description The length of the raw value, either AccountIndex or AccountId
|
||||
*/
|
||||
protected get _rawLength (): number {
|
||||
return this._raw instanceof GenericAccountIndex
|
||||
? GenericAccountIndex.calcLength(this._raw)
|
||||
: this._raw.encodedLength;
|
||||
return this.inner instanceof GenericAccountIndex
|
||||
? GenericAccountIndex.calcLength(this.inner)
|
||||
: this.inner.encodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,12 +109,12 @@ export class GenericEthereumLookupSource extends Base<GenericEthereumAccountId |
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public override toU8a (isBare?: boolean): Uint8Array {
|
||||
const encoded = this._raw.toU8a().subarray(0, this._rawLength);
|
||||
const encoded = this.inner.toU8a().subarray(0, this._rawLength);
|
||||
|
||||
return isBare
|
||||
? encoded
|
||||
: u8aConcat(
|
||||
this._raw instanceof GenericAccountIndex
|
||||
this.inner instanceof GenericAccountIndex
|
||||
? GenericAccountIndex.writeLength(encoded)
|
||||
: ACCOUNT_ID_PREFIX,
|
||||
encoded
|
||||
|
||||
@@ -36,12 +36,53 @@ const VERSIONS = [
|
||||
|
||||
export { EXTRINSIC_VERSION as LATEST_EXTRINSIC_VERSION } from './v4/Extrinsic';
|
||||
|
||||
/** @internal */
|
||||
function newFromValue (registry: CodecRegistry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown {
|
||||
if (value instanceof GenericExtrinsic) {
|
||||
return value.unwrap();
|
||||
}
|
||||
|
||||
const isSigned = (version & BIT_SIGNED) === BIT_SIGNED;
|
||||
const type = VERSIONS[version & UNMASK_VERSION] || VERSIONS[0];
|
||||
|
||||
// we cast here since the VERSION definition is incredibly broad - we don't have a
|
||||
// slice for "only add extrinsic types", and more string definitions become unwieldy
|
||||
return registry.createTypeUnsafe(type, [value, { isSigned, version }]);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeExtrinsic (registry: CodecRegistry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown {
|
||||
if (isU8a(value) || Array.isArray(value) || isHex(value)) {
|
||||
return decodeU8a(registry, u8aToU8a(value), version);
|
||||
} else if (value instanceof registry.createClassUnsafe('Call')) {
|
||||
return newFromValue(registry, { method: value }, version);
|
||||
}
|
||||
|
||||
return newFromValue(registry, value, version);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeU8a (registry: CodecRegistry, value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown {
|
||||
if (!value.length) {
|
||||
return newFromValue(registry, new Uint8Array(), version);
|
||||
}
|
||||
|
||||
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}`);
|
||||
|
||||
const data = value.subarray(offset, total);
|
||||
|
||||
return newFromValue(registry, data.subarray(1), data[0]);
|
||||
}
|
||||
|
||||
abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | ExtrinsicUnknown> {
|
||||
constructor (registry: CodecRegistry, value: ExtrinsicV4 | ExtrinsicUnknown, initialU8aLength?: number) {
|
||||
super(registry, value, initialU8aLength);
|
||||
|
||||
const signKeys = Object.keys(registry.getSignedExtensionTypes());
|
||||
const getter = (key: string) => (this._raw as ExtrinsicVx).signature[key as 'signer'];
|
||||
const getter = (key: string) => this.inner.signature[key as 'signer'];
|
||||
|
||||
// This is on the abstract class, ensuring that hasOwnProperty operates
|
||||
// correctly, i.e. it needs to be on the base class exposing it
|
||||
@@ -82,7 +123,7 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
|
||||
* @description The era for this extrinsic
|
||||
*/
|
||||
public get era (): GenericExtrinsicEra {
|
||||
return (this._raw as ExtrinsicVx).signature.era;
|
||||
return this.inner.signature.era;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,7 +137,7 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
|
||||
* @description `true` id the extrinsic is signed
|
||||
*/
|
||||
public get isSigned (): boolean {
|
||||
return (this._raw as ExtrinsicVx).signature.isSigned;
|
||||
return this.inner.signature.isSigned;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,42 +158,46 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
|
||||
* @description The [[Call]] this extrinsic wraps
|
||||
*/
|
||||
public get method (): CallBase<A> {
|
||||
return (this._raw as ExtrinsicVx).method as unknown as CallBase<A>;
|
||||
return this.inner.method as unknown as CallBase<A>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The nonce for this extrinsic
|
||||
*/
|
||||
public get nonce (): Compact<Index> {
|
||||
return (this._raw as ExtrinsicVx).signature.nonce;
|
||||
return this.inner.signature.nonce;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The actual [[EcdsaSignature]], [[Ed25519Signature]] or [[Sr25519Signature]]
|
||||
*/
|
||||
public get signature (): EcdsaSignature | Ed25519Signature | Sr25519Signature {
|
||||
return (this._raw as ExtrinsicVx).signature.signature;
|
||||
return this.inner.signature.signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Address]] that signed
|
||||
*/
|
||||
public get signer (): Address {
|
||||
return (this._raw as ExtrinsicVx).signature.signer;
|
||||
return this.inner.signature.signer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Forwards compat
|
||||
*/
|
||||
public get tip (): Compact<Balance> {
|
||||
return (this._raw as ExtrinsicVx).signature.tip;
|
||||
return this.inner.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.inner.version;
|
||||
}
|
||||
|
||||
public override get inner (): ExtrinsicVx {
|
||||
return this.unwrap();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -168,6 +213,10 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
|
||||
public is (other: IMethod<AnyTuple>): other is IMethod<A> {
|
||||
return this.method.is(other);
|
||||
}
|
||||
|
||||
public override unwrap (): ExtrinsicVx {
|
||||
return super.unwrap() as ExtrinsicVx;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,48 +235,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
|
||||
#hashCache?: CodecHash;
|
||||
|
||||
constructor (registry: CodecRegistry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, { version }: CreateOptions = {}) {
|
||||
super(registry, GenericExtrinsic._decodeExtrinsic(registry, value, version));
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
private static _newFromValue (registry: CodecRegistry, value: any, version: number): ExtrinsicVx | ExtrinsicUnknown {
|
||||
if (value instanceof GenericExtrinsic) {
|
||||
return value._raw;
|
||||
}
|
||||
|
||||
const isSigned = (version & BIT_SIGNED) === BIT_SIGNED;
|
||||
const type = VERSIONS[version & UNMASK_VERSION] || VERSIONS[0];
|
||||
|
||||
// we cast here since the VERSION definition is incredibly broad - we don't have a
|
||||
// slice for "only add extrinsic types", and more string definitions become unwieldy
|
||||
return registry.createTypeUnsafe(type, [value, { isSigned, version }]);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
private static _decodeExtrinsic (registry: CodecRegistry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, version: number = DEFAULT_VERSION): ExtrinsicVx | ExtrinsicUnknown {
|
||||
if (isU8a(value) || Array.isArray(value) || isHex(value)) {
|
||||
return GenericExtrinsic._decodeU8a(registry, u8aToU8a(value), version);
|
||||
} else if (value instanceof registry.createClassUnsafe('Call')) {
|
||||
return GenericExtrinsic._newFromValue(registry, { method: value }, version);
|
||||
}
|
||||
|
||||
return GenericExtrinsic._newFromValue(registry, value, version);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
private static _decodeU8a (registry: CodecRegistry, value: Uint8Array, version: number): ExtrinsicVx | ExtrinsicUnknown {
|
||||
if (!value.length) {
|
||||
return GenericExtrinsic._newFromValue(registry, new Uint8Array(), version);
|
||||
}
|
||||
|
||||
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}`);
|
||||
|
||||
const data = value.subarray(offset, total);
|
||||
|
||||
return GenericExtrinsic._newFromValue(registry, data.subarray(1), data[0]);
|
||||
super(registry, decodeExtrinsic(registry, value, version));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -245,7 +253,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
|
||||
* @description Injects an already-generated signature into the extrinsic
|
||||
*/
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsic<A> {
|
||||
(this._raw as ExtrinsicVx).addSignature(signer, signature, payload);
|
||||
this.inner.addSignature(signer, signature, payload);
|
||||
this.#hashCache = undefined;
|
||||
|
||||
return this;
|
||||
@@ -255,7 +263,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
|
||||
* @description Sign the extrinsic with a specific keypair
|
||||
*/
|
||||
public sign (account: IKeyringPair, options: SignatureOptions): GenericExtrinsic<A> {
|
||||
(this._raw as ExtrinsicVx).sign(account, options);
|
||||
this.inner.sign(account, options);
|
||||
this.#hashCache = undefined;
|
||||
|
||||
return this;
|
||||
@@ -265,7 +273,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
|
||||
* @describe Adds a fake signature to the extrinsic
|
||||
*/
|
||||
public signFake (signer: Address | Uint8Array | string, options: SignatureOptions): GenericExtrinsic<A> {
|
||||
(this._raw as ExtrinsicVx).signFake(signer, options);
|
||||
this.inner.signFake(signer, options);
|
||||
this.#hashCache = undefined;
|
||||
|
||||
return this;
|
||||
@@ -321,7 +329,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
|
||||
public override 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.inner.toU8a());
|
||||
|
||||
return isBare
|
||||
? encoded
|
||||
|
||||
@@ -36,6 +36,80 @@ function getTrailingZeros (period: number): number {
|
||||
return index;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeMortalEra (registry: CodecRegistry, value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue {
|
||||
if (isU8a(value) || isHex(value) || Array.isArray(value)) {
|
||||
return decodeMortalU8a(registry, u8aToU8a(value));
|
||||
} else if (!value) {
|
||||
return [new U64(registry), new U64(registry)];
|
||||
} else if (isObject(value)) {
|
||||
return decodeMortalObject(registry, value);
|
||||
}
|
||||
|
||||
throw new Error('Invalid data passed to Mortal era');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeMortalObject (registry: CodecRegistry, value: MortalMethod): MortalEraValue {
|
||||
const { current, period } = value;
|
||||
let calPeriod = Math.pow(2, Math.ceil(Math.log2(period)));
|
||||
|
||||
calPeriod = Math.min(Math.max(calPeriod, 4), 1 << 16);
|
||||
|
||||
const phase = current % calPeriod;
|
||||
const quantizeFactor = Math.max(calPeriod >> 12, 1);
|
||||
const quantizedPhase = phase / quantizeFactor * quantizeFactor;
|
||||
|
||||
return [new U64(registry, calPeriod), new U64(registry, quantizedPhase)];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeMortalU8a (registry: CodecRegistry, value: Uint8Array): MortalEraValue {
|
||||
if (value.length === 0) {
|
||||
return [new U64(registry), new U64(registry)];
|
||||
}
|
||||
|
||||
const first = u8aToBn(value.subarray(0, 1)).toNumber();
|
||||
const second = u8aToBn(value.subarray(1, 2)).toNumber();
|
||||
const encoded: number = first + (second << 8);
|
||||
const period = 2 << (encoded % (1 << 4));
|
||||
const quantizeFactor = Math.max(period >> 12, 1);
|
||||
const phase = (encoded >> 4) * quantizeFactor;
|
||||
|
||||
assert(period >= 4 && phase < period, 'Invalid data passed to Mortal era');
|
||||
|
||||
return [new U64(registry, period), new U64(registry, phase)];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
function decodeExtrinsicEra (value: IExtrinsicEra | MortalMethod | MortalEnumDef | ImmortalEnumDef | Uint8Array | string = new Uint8Array()): Uint8Array | Object | undefined {
|
||||
if (isU8a(value)) {
|
||||
return (!value.length || value[0] === 0)
|
||||
? new Uint8Array([0])
|
||||
: new Uint8Array([1, value[0], value[1]]);
|
||||
} else if (!value) {
|
||||
return new Uint8Array([0]);
|
||||
} else if (value instanceof GenericExtrinsicEra) {
|
||||
return decodeExtrinsicEra(value.toU8a());
|
||||
} else if (isHex(value)) {
|
||||
return decodeExtrinsicEra(hexToU8a(value));
|
||||
} else if (isObject(value)) {
|
||||
const entries = Object.entries(value as MortalEnumDef).map(([k, v]): [string, any] => [k.toLowerCase(), v]);
|
||||
const mortal = entries.find(([k]) => k.toLowerCase() === 'mortalera');
|
||||
const immortal = entries.find(([k]) => k.toLowerCase() === 'immortalera');
|
||||
|
||||
// this is to de-serialize from JSON
|
||||
return mortal
|
||||
? { MortalEra: mortal[1] as string }
|
||||
: immortal
|
||||
? { ImmortalEra: immortal[1] as string }
|
||||
: { MortalEra: value };
|
||||
}
|
||||
|
||||
throw new Error('Invalid data passed to Era');
|
||||
}
|
||||
|
||||
/**
|
||||
* @name ImmortalEra
|
||||
* @description
|
||||
@@ -60,52 +134,7 @@ export class MortalEra extends Tuple {
|
||||
super(registry, {
|
||||
period: U64,
|
||||
phase: U64
|
||||
}, MortalEra._decodeMortalEra(registry, value));
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
private static _decodeMortalEra (registry: CodecRegistry, value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue {
|
||||
if (isU8a(value) || isHex(value) || Array.isArray(value)) {
|
||||
return MortalEra._decodeMortalU8a(registry, u8aToU8a(value));
|
||||
} else if (!value) {
|
||||
return [new U64(registry), new U64(registry)];
|
||||
} else if (isObject(value)) {
|
||||
return MortalEra._decodeMortalObject(registry, value);
|
||||
}
|
||||
|
||||
throw new Error('Invalid data passed to Mortal era');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
private static _decodeMortalObject (registry: CodecRegistry, value: MortalMethod): MortalEraValue {
|
||||
const { current, period } = value;
|
||||
let calPeriod = Math.pow(2, Math.ceil(Math.log2(period)));
|
||||
|
||||
calPeriod = Math.min(Math.max(calPeriod, 4), 1 << 16);
|
||||
|
||||
const phase = current % calPeriod;
|
||||
const quantizeFactor = Math.max(calPeriod >> 12, 1);
|
||||
const quantizedPhase = phase / quantizeFactor * quantizeFactor;
|
||||
|
||||
return [new U64(registry, calPeriod), new U64(registry, quantizedPhase)];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
private static _decodeMortalU8a (registry: CodecRegistry, value: Uint8Array): MortalEraValue {
|
||||
if (value.length === 0) {
|
||||
return [new U64(registry), new U64(registry)];
|
||||
}
|
||||
|
||||
const first = u8aToBn(value.subarray(0, 1)).toNumber();
|
||||
const second = u8aToBn(value.subarray(1, 2)).toNumber();
|
||||
const encoded: number = first + (second << 8);
|
||||
const period = 2 << (encoded % (1 << 4));
|
||||
const quantizeFactor = Math.max(period >> 12, 1);
|
||||
const phase = (encoded >> 4) * quantizeFactor;
|
||||
|
||||
assert(period >= 4 && phase < period, 'Invalid data passed to Mortal era');
|
||||
|
||||
return [new U64(registry, period), new U64(registry, phase)];
|
||||
}, decodeMortalEra(registry, value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,36 +229,7 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
|
||||
super(registry, {
|
||||
ImmortalEra,
|
||||
MortalEra
|
||||
}, GenericExtrinsicEra._decodeExtrinsicEra(value as string));
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
private static _decodeExtrinsicEra (value: IExtrinsicEra | MortalMethod | MortalEnumDef | ImmortalEnumDef | Uint8Array | string = new Uint8Array()): Uint8Array | Object | undefined {
|
||||
if (isU8a(value)) {
|
||||
return (!value.length || value[0] === 0)
|
||||
? new Uint8Array([0])
|
||||
: new Uint8Array([1, value[0], value[1]]);
|
||||
} else if (!value) {
|
||||
return new Uint8Array([0]);
|
||||
} else if (value instanceof GenericExtrinsicEra) {
|
||||
return GenericExtrinsicEra._decodeExtrinsicEra(value.toU8a());
|
||||
} else if (isHex(value)) {
|
||||
return GenericExtrinsicEra._decodeExtrinsicEra(hexToU8a(value));
|
||||
} else if (isObject(value)) {
|
||||
const entries = Object.entries(value as MortalEnumDef).map(([k, v]): [string, any] => [k.toLowerCase(), v]);
|
||||
const mortal = entries.find(([k]) => k.toLowerCase() === 'mortalera');
|
||||
const immortal = entries.find(([k]) => k.toLowerCase() === 'immortalera');
|
||||
|
||||
// this is to de-serialize from JSON
|
||||
return mortal
|
||||
? { MortalEra: mortal[1] as string }
|
||||
: immortal
|
||||
? { ImmortalEra: immortal[1] as string }
|
||||
: { MortalEra: value };
|
||||
}
|
||||
|
||||
throw new Error('Invalid data passed to Era');
|
||||
}, decodeExtrinsicEra(value as string));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -247,7 +247,7 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
|
||||
public get asImmortalEra (): ImmortalEra {
|
||||
assert(this.isImmortalEra, () => `Cannot convert '${this.type}' via asImmortalEra`);
|
||||
|
||||
return this.value as ImmortalEra;
|
||||
return this.inner as ImmortalEra;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -256,7 +256,7 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
|
||||
public get asMortalEra (): MortalEra {
|
||||
assert(this.isMortalEra, () => `Cannot convert '${this.type}' via asMortalEra`);
|
||||
|
||||
return this.value as MortalEra;
|
||||
return this.inner as MortalEra;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,6 +28,15 @@ const VERSIONS = [
|
||||
'ExtrinsicPayloadV4'
|
||||
];
|
||||
|
||||
/** @internal */
|
||||
function decodeExtrinsicPayload (registry: CodecRegistry, value?: GenericExtrinsicPayload | ExtrinsicPayloadValue | Uint8Array | string, version: number = DEFAULT_VERSION): ExtrinsicPayloadVx {
|
||||
if (value instanceof GenericExtrinsicPayload) {
|
||||
return value.unwrap();
|
||||
}
|
||||
|
||||
return registry.createTypeUnsafe(VERSIONS[version] || VERSIONS[0], [value, { version }]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name GenericExtrinsicPayload
|
||||
* @description
|
||||
@@ -36,30 +45,21 @@ const VERSIONS = [
|
||||
*/
|
||||
export class GenericExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
|
||||
constructor (registry: CodecRegistry, value?: Partial<ExtrinsicPayloadValue> | Uint8Array | string, { version }: ExtrinsicPayloadOptions = {}) {
|
||||
super(registry, GenericExtrinsicPayload.decodeExtrinsicPayload(registry, value as ExtrinsicPayloadValue, version));
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public static decodeExtrinsicPayload (registry: CodecRegistry, value?: GenericExtrinsicPayload | ExtrinsicPayloadValue | Uint8Array | string, version: number = DEFAULT_VERSION): ExtrinsicPayloadVx {
|
||||
if (value instanceof GenericExtrinsicPayload) {
|
||||
return value._raw;
|
||||
}
|
||||
|
||||
return registry.createTypeUnsafe(VERSIONS[version] || VERSIONS[0], [value, { version }]);
|
||||
super(registry, decodeExtrinsicPayload(registry, value as ExtrinsicPayloadValue, version));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The block [[Hash]] the signature applies to (mortal/immortal)
|
||||
*/
|
||||
public get blockHash (): Hash {
|
||||
return this._raw.blockHash;
|
||||
return this.inner.blockHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[ExtrinsicEra]]
|
||||
*/
|
||||
public get era (): GenericExtrinsicEra {
|
||||
return this._raw.era;
|
||||
return this.inner.era;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,21 +67,21 @@ export class GenericExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
|
||||
*/
|
||||
public get genesisHash (): Hash {
|
||||
// NOTE only v3+
|
||||
return this._raw.genesisHash || this.registry.createTypeUnsafe('Hash', []);
|
||||
return this.inner.genesisHash || this.registry.createTypeUnsafe('Hash', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Raw]] contained in the payload
|
||||
*/
|
||||
public get method (): Raw {
|
||||
return this._raw.method;
|
||||
return this.inner.method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Index]]
|
||||
*/
|
||||
public get nonce (): Compact<Index> {
|
||||
return this._raw.nonce;
|
||||
return this.inner.nonce;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +89,7 @@ export class GenericExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
|
||||
*/
|
||||
public get specVersion (): u32 {
|
||||
// NOTE only v3+
|
||||
return this._raw.specVersion || this.registry.createTypeUnsafe('u32', []);
|
||||
return this.inner.specVersion || this.registry.createTypeUnsafe('u32', []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,7 +97,7 @@ export class GenericExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
|
||||
*/
|
||||
public get tip (): Compact<Balance> {
|
||||
// NOTE from v2+
|
||||
return this._raw.tip || this.registry.createTypeUnsafe('Compact<Balance>', []);
|
||||
return this.inner.tip || this.registry.createTypeUnsafe('Compact<Balance>', []);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,21 +105,21 @@ export class GenericExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
|
||||
*/
|
||||
public get transactionVersion (): u32 {
|
||||
// NOTE only v4+
|
||||
return this._raw.transactionVersion || this.registry.createTypeUnsafe('u32', []);
|
||||
return this.inner.transactionVersion || this.registry.createTypeUnsafe('u32', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Compares the value of the input to see if there is a match
|
||||
*/
|
||||
public override eq (other?: unknown): boolean {
|
||||
return this._raw.eq(other);
|
||||
return this.inner.eq(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Sign the payload with the keypair
|
||||
*/
|
||||
public sign (signerPair: IKeyringPair): { signature: HexString } {
|
||||
const signature = this._raw.sign(signerPair);
|
||||
const signature = this.inner.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()`
|
||||
@@ -134,7 +134,7 @@ export class GenericExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public override toHuman (isExtended?: boolean): AnyJson {
|
||||
return this._raw.toHuman(isExtended);
|
||||
return this.inner.toHuman(isExtended);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -57,7 +57,7 @@ export class GenericLookupSource extends Base<GenericAccountId | GenericAccountI
|
||||
/** @internal */
|
||||
private static _decodeAddress (registry: CodecRegistry, value: AnyAddress): GenericAccountId | GenericAccountIndex {
|
||||
return value instanceof GenericLookupSource
|
||||
? value._raw
|
||||
? value.inner
|
||||
: value instanceof GenericAccountId || value instanceof GenericAccountIndex
|
||||
? value
|
||||
: isBn(value) || isNumber(value) || isBigInt(value)
|
||||
@@ -85,9 +85,9 @@ export class GenericLookupSource extends Base<GenericAccountId | GenericAccountI
|
||||
* @description The length of the raw value, either AccountIndex or AccountId
|
||||
*/
|
||||
protected get _rawLength (): number {
|
||||
return this._raw instanceof GenericAccountIndex
|
||||
? GenericAccountIndex.calcLength(this._raw)
|
||||
: this._raw.encodedLength;
|
||||
return this.inner instanceof GenericAccountIndex
|
||||
? GenericAccountIndex.calcLength(this.inner)
|
||||
: this.inner.encodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,12 +109,12 @@ export class GenericLookupSource extends Base<GenericAccountId | GenericAccountI
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public override toU8a (isBare?: boolean): Uint8Array {
|
||||
const encoded = this._raw.toU8a().subarray(0, this._rawLength);
|
||||
const encoded = this.inner.toU8a().subarray(0, this._rawLength);
|
||||
|
||||
return isBare
|
||||
? encoded
|
||||
: u8aConcat(
|
||||
this._raw instanceof GenericAccountIndex
|
||||
this.inner instanceof GenericAccountIndex
|
||||
? GenericAccountIndex.writeLength(encoded)
|
||||
: ACCOUNT_ID_PREFIX,
|
||||
encoded
|
||||
|
||||
Reference in New Issue
Block a user