Skip to content

Commit

Permalink
Revert "unfinished Preamble Class"
Browse files Browse the repository at this point in the history
This reverts commit 0b1c350.
  • Loading branch information
bee344 committed Sep 24, 2024
1 parent 0b1c350 commit 072f69a
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 182 deletions.
7 changes: 1 addition & 6 deletions packages/types-augment/src/registry/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type { ApprovalFlag, DefunctVoter, Renouncing, SetIndex, Vote, VoteIndex,
import type { CreatedBlock, ImportedAux } from '@polkadot/types/interfaces/engine';
import type { BlockV0, BlockV1, BlockV2, EIP1559Transaction, EIP2930Transaction, EthAccessList, EthAccessListItem, EthAccount, EthAddress, EthBlock, EthBloom, EthCallRequest, EthFeeHistory, EthFilter, EthFilterAddress, EthFilterChanges, EthFilterTopic, EthFilterTopicEntry, EthFilterTopicInner, EthHeader, EthLog, EthReceipt, EthReceiptV0, EthReceiptV3, EthRichBlock, EthRichHeader, EthStorageProof, EthSubKind, EthSubParams, EthSubResult, EthSyncInfo, EthSyncStatus, EthTransaction, EthTransactionAction, EthTransactionCondition, EthTransactionRequest, EthTransactionSignature, EthTransactionStatus, EthWork, EthereumAccountId, EthereumAddress, EthereumLookupSource, EthereumSignature, LegacyTransaction, TransactionV0, TransactionV1, TransactionV2 } from '@polkadot/types/interfaces/eth';
import type { EvmAccount, EvmCallInfo, EvmCallInfoV2, EvmCreateInfo, EvmCreateInfoV2, EvmLog, EvmVicinity, EvmWeightInfo, ExitError, ExitFatal, ExitReason, ExitRevert, ExitSucceed } from '@polkadot/types/interfaces/evm';
import type { AnySignature, EcdsaSignature, Ed25519Signature, Era, Extension, ExtensionVersion, Extrinsic, ExtrinsicEra, ExtrinsicPayload, ExtrinsicPayloadUnknown, ExtrinsicPayloadV4, ExtrinsicPayloadV5, ExtrinsicSignature, ExtrinsicSignatureV4, ExtrinsicSignatureV5, ExtrinsicUnknown, ExtrinsicV4, ExtrinsicV5, ExtrinsicVersion, ImmortalEra, MortalEra, MultiSignature, Preamble, Signature, SignerPayload, Sr25519Signature, TransactionExtension } from '@polkadot/types/interfaces/extrinsics';
import type { AnySignature, EcdsaSignature, Ed25519Signature, Era, Extrinsic, ExtrinsicEra, ExtrinsicPayload, ExtrinsicPayloadUnknown, ExtrinsicPayloadV4, ExtrinsicPayloadV5, ExtrinsicSignature, ExtrinsicSignatureV4, ExtrinsicSignatureV5, ExtrinsicUnknown, ExtrinsicV4, ExtrinsicV5, ImmortalEra, MortalEra, MultiSignature, Signature, SignerPayload, Sr25519Signature } from '@polkadot/types/interfaces/extrinsics';
import type { FungiblesAccessError } from '@polkadot/types/interfaces/fungibles';
import type { AssetOptions, Owner, PermissionLatest, PermissionVersions, PermissionsV1 } from '@polkadot/types/interfaces/genericAsset';
import type { GenesisBuildErr } from '@polkadot/types/interfaces/genesisBuilder';
Expand Down Expand Up @@ -491,8 +491,6 @@ declare module '@polkadot/types/types/registry' {
ExplicitDisputeStatement: ExplicitDisputeStatement;
Exposure: Exposure;
ExtendedBalance: ExtendedBalance;
Extension: Extension;
ExtensionVersion: ExtensionVersion;
Extrinsic: Extrinsic;
ExtrinsicEra: ExtrinsicEra;
ExtrinsicInclusionMode: ExtrinsicInclusionMode;
Expand All @@ -515,7 +513,6 @@ declare module '@polkadot/types/types/registry' {
ExtrinsicUnknown: ExtrinsicUnknown;
ExtrinsicV4: ExtrinsicV4;
ExtrinsicV5: ExtrinsicV5;
ExtrinsicVersion: ExtrinsicVersion;
f32: f32;
F32: F32;
f64: f64;
Expand Down Expand Up @@ -893,7 +890,6 @@ declare module '@polkadot/types/types/registry' {
PortableType: PortableType;
PortableTypeV14: PortableTypeV14;
PostDispatchInfo: PostDispatchInfo;
Preamble: Preamble;
Precommits: Precommits;
PrefabWasmModule: PrefabWasmModule;
PrefixedStorageKey: PrefixedStorageKey;
Expand Down Expand Up @@ -1182,7 +1178,6 @@ declare module '@polkadot/types/types/registry' {
TraceBlockResponse: TraceBlockResponse;
TraceError: TraceError;
TransactionalError: TransactionalError;
TransactionExtension: TransactionExtension;
TransactionInfo: TransactionInfo;
TransactionLongevity: TransactionLongevity;
TransactionPriority: TransactionPriority;
Expand Down
35 changes: 17 additions & 18 deletions packages/types/src/extrinsic/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import type { Address, Call, CodecHash, Hash } from '../interfaces/runtime/index
import type { MultiLocation } from '../interfaces/types.js';
import type { CallBase, ExtrinsicPayloadValue, ICompact, IExtrinsic, IKeyringPair, INumber, Registry, SignatureOptions } from '../types/index.js';
import type { GenericExtrinsicEra } from './ExtrinsicEra.js';
import type { Preamble } from './types.js';
import type { ExtrinsicValueV5 } from './v5/Extrinsic.js';

import { AbstractBase } from '@polkadot/types-codec';
import { compactAddLength, compactFromU8a, compactToU8a, isHex, isU8a, objectProperty, objectSpread, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';

import { BARE_EXTRINSIC, BIT_SIGNED, BIT_UNSIGNED, DEFAULT_PREAMBLE_KIND, GENERAL_EXTRINSIC, LATEST_EXTRINSIC_VERSION, LOWEST_SUPPORTED_EXTRINSIC_FORMAT_VERSION, SIGNED_EXTRINSIC, UNMASK_VERSION } from './constants.js';
import type { PreambleKind } from './types.js';
import { BARE_EXTRINSIC, BIT_SIGNED, BIT_UNSIGNED, DEFAULT_PREAMBLE, GENERAL_EXTRINSIC, LATEST_EXTRINSIC_VERSION, LOWEST_SUPPORTED_EXTRINSIC_FORMAT_VERSION, SIGNED_EXTRINSIC, UNMASK_VERSION } from './constants.js';

interface CreateOptions {
version?: number;
preambleKind?: PreambleKind;
preamble?: Preamble;
}

// NOTE The following 2 types, as well as the VERSION structure and the latest export
Expand Down Expand Up @@ -50,38 +50,37 @@ const PreambleMask = {
signed: SIGNED_EXTRINSIC
};


export { LATEST_EXTRINSIC_VERSION };

/** @internal */
function newFromValue (registry: Registry, value: any, version: number, preambleKind: PreambleKind): ExtrinsicVx | ExtrinsicUnknown {
function newFromValue (registry: Registry, value: any, version: number, preamble: Preamble): ExtrinsicVx | ExtrinsicUnknown {
if (value instanceof GenericExtrinsic) {
return value.unwrap();
}

const isSigned = (version & BIT_SIGNED) === BIT_SIGNED;
const type = (version & UNMASK_VERSION) === 5 ? PREAMBLE[preambleKind] : VERSIONS[version & UNMASK_VERSION] || VERSIONS[0];
const type = (version & UNMASK_VERSION) === 5 ? PREAMBLE[preamble] : 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: Registry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, version: number = LOWEST_SUPPORTED_EXTRINSIC_FORMAT_VERSION, preambleKind: PreambleKind = 'signed'): ExtrinsicVx | ExtrinsicUnknown {
function decodeExtrinsic (registry: Registry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, version: number = LOWEST_SUPPORTED_EXTRINSIC_FORMAT_VERSION, preamble: Preamble = DEFAULT_PREAMBLE): ExtrinsicVx | ExtrinsicUnknown {
if (isU8a(value) || Array.isArray(value) || isHex(value)) {
return decodeU8a(registry, u8aToU8a(value), version, preambleKind);
return decodeU8a(registry, u8aToU8a(value), version, preamble);
} else if (value instanceof registry.createClassUnsafe('Call')) {
return newFromValue(registry, { method: value }, version, preambleKind);
return newFromValue(registry, { method: value }, version, preamble);
}

return newFromValue(registry, value, version, preambleKind);
return newFromValue(registry, value, version, preamble);
}

/** @internal */
function decodeU8a (registry: Registry, value: Uint8Array, version: number, preambleKind: PreambleKind): ExtrinsicVx | ExtrinsicUnknown {
function decodeU8a (registry: Registry, value: Uint8Array, version: number, preamble: Preamble): ExtrinsicVx | ExtrinsicUnknown {
if (!value.length) {
return newFromValue(registry, new Uint8Array(), version, preambleKind);
return newFromValue(registry, new Uint8Array(), version, preamble);
}

const [offset, length] = compactFromU8a(value);
Expand All @@ -93,13 +92,13 @@ function decodeU8a (registry: Registry, value: Uint8Array, version: number, prea

const data = value.subarray(offset, total);

return newFromValue(registry, data.subarray(1), data[0], preambleKind);
return newFromValue(registry, data.subarray(1), data[0], preamble);
}

abstract class ExtrinsicBase<A extends AnyTuple> extends AbstractBase<ExtrinsicVx | ExtrinsicUnknown> {
readonly #preamble: PreambleKind;
readonly #preamble: Preamble;

constructor (registry: Registry, value: ExtrinsicV5 | ExtrinsicUnknown, initialU8aLength?: number, preambleKind?: PreambleKind) {
constructor (registry: Registry, value: ExtrinsicV5 | ExtrinsicUnknown, initialU8aLength?: number, preamble?: Preamble) {
super(registry, value, initialU8aLength);

const signKeys = Object.keys(registry.getSignedExtensionTypes());
Expand All @@ -111,7 +110,7 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends AbstractBase<ExtrinsicV
objectProperty(this, signKeys[i], getter);
}

this.#preamble = preambleKind || DEFAULT_PREAMBLE_KIND;
this.#preamble = preamble || DEFAULT_PREAMBLE;
}

/**
Expand Down Expand Up @@ -284,8 +283,8 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa

static LATEST_EXTRINSIC_VERSION = LATEST_EXTRINSIC_VERSION;

constructor (registry: Registry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, { preambleKind, version }: CreateOptions = {}) {
super(registry, decodeExtrinsic(registry, value, version || registry.metadata.extrinsic.version?.toNumber(), preambleKind), undefined, preambleKind);
constructor (registry: Registry, value?: GenericExtrinsic | ExtrinsicValue | AnyU8a | Call, { preamble, version }: CreateOptions = {}) {
super(registry, decodeExtrinsic(registry, value, version || registry.metadata.extrinsic.version?.toNumber(), preamble), undefined, preamble);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/types/src/extrinsic/ExtrinsicPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import type { ExtrinsicPayloadV5 } from '../interfaces/extrinsics/index.js';
import type { Hash, MultiLocation } from '../interfaces/types.js';
import type { ExtrinsicPayloadValue, ICompact, IKeyringPair, INumber, IOption } from '../types/index.js';
import type { GenericExtrinsicEra } from './ExtrinsicEra.js';
import type { Preamble } from './types.js';

import { AbstractBase } from '@polkadot/types-codec';
import { hexToU8a, isHex, u8aToHex } from '@polkadot/util';

import { DEFAULT_PREAMBLE_KIND, LATEST_EXTRINSIC_VERSION } from './constants.js';
import type { PreambleKind } from './types.js';
import { DEFAULT_PREAMBLE, LATEST_EXTRINSIC_VERSION } from './constants.js';

interface ExtrinsicPayloadOptions {
version?: number;
pramble?: PreambleKind;
pramble?: Preamble;
}

// all our known types that can be returned
Expand All @@ -41,12 +41,12 @@ const PREAMBLES = {
};

/** @internal */
function decodeExtrinsicPayload (registry: Registry, value?: GenericExtrinsicPayload | ExtrinsicPayloadValue | Uint8Array | string, version = LATEST_EXTRINSIC_VERSION, preambleKind: PreambleKind = DEFAULT_PREAMBLE_KIND): ExtrinsicPayloadVx {
function decodeExtrinsicPayload (registry: Registry, value?: GenericExtrinsicPayload | ExtrinsicPayloadValue | Uint8Array | string, version = LATEST_EXTRINSIC_VERSION, preamble: Preamble = DEFAULT_PREAMBLE): ExtrinsicPayloadVx {
if (value instanceof GenericExtrinsicPayload) {
return value.unwrap();
}

const extVersion = version === 5 ? PREAMBLES[preambleKind] : VERSIONS[version] || VERSIONS[0];
const extVersion = version === 5 ? PREAMBLES[preamble] : VERSIONS[version] || VERSIONS[0];

/**
* HACK: In order to change the assetId from `number | object` to HexString (While maintaining the true type ie Option<TAssetConversion>),
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/extrinsic/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const IMMORTAL_ERA = new Uint8Array([0]);

export const UNMASK_VERSION = 0b01111111;

export const DEFAULT_PREAMBLE_KIND = 'signed';
export const DEFAULT_PREAMBLE = 'signed';

// Latest extrinsic version is v5, which has backwards compatibility for v4 signed extrinsics
export const LATEST_EXTRINSIC_VERSION = 5;
Expand Down
4 changes: 1 addition & 3 deletions packages/types/src/extrinsic/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

import type { AnyNumber } from '@polkadot/types-codec/types';
import type { Preamble } from '../interfaces/types.js';

export interface ExtrinsicOptions {
isSigned: boolean;
version: number;
preambleKind: PreambleKind;
}

export interface ExtrinsicPayloadOptions {
Expand All @@ -24,4 +22,4 @@ export interface ExtrinsicExtraValue {
tip?: AnyNumber;
}

export type PreambleKind = 'bare' | 'general' | 'signed';
export type Preamble = 'signed' | 'bare' | 'general';
54 changes: 18 additions & 36 deletions packages/types/src/extrinsic/v5/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
// SPDX-License-Identifier: Apache-2.0

import type { HexString } from '@polkadot/util/types';
import type { ExtrinsicSignatureV5, Preamble } from '../../interfaces/extrinsics/index.js';
import type { ExtrinsicSignatureV5 } from '../../interfaces/extrinsics/index.js';
import type { Address, Call } from '../../interfaces/runtime/index.js';
import type { ExtrinsicPayloadValue, IExtrinsicV5Impl, IKeyringPair, Registry, SignatureOptions } from '../../types/index.js';
import type { ExtrinsicOptions, PreambleKind } from '../types.js';
import type { ExtrinsicOptions, Preamble } from '../types.js';

import { Struct } from '@polkadot/types-codec';
import { isU8a } from '@polkadot/util';
import { DEFAULT_PREAMBLE_KIND } from '../constants.js';

export const EXTRINSIC_VERSION = 5;

export interface ExtrinsicValueV5 {
method?: Call;
preamble?: Preamble;
signature?: ExtrinsicSignatureV5;
}

/**
Expand All @@ -24,45 +23,28 @@ export interface ExtrinsicValueV5 {
* The third generation of compact extrinsics
*/
export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl {
constructor(registry: Registry, value?: Uint8Array | ExtrinsicValueV5 | Call, { preambleKind }: Partial<ExtrinsicOptions> = {}) {

constructor (registry: Registry, value?: Uint8Array | ExtrinsicValueV5 | Call, { isSigned }: Partial<ExtrinsicOptions> = {}) {
super(registry, {
preamble: 'Preamble',
signature: 'ExtrinsicSignatureV5',
// eslint-disable-next-line sort-keys
method: 'Call'
}, GenericExtrinsicV5.decodeExtrinsic(registry, value, preambleKind));
}, GenericExtrinsicV5.decodeExtrinsic(registry, value, isSigned));
}

/** @internal */
public static decodeExtrinsic(registry: Registry, value?: Call | Uint8Array | ExtrinsicValueV5, preambleKind = DEFAULT_PREAMBLE_KIND): ExtrinsicValueV5 {
public static decodeExtrinsic (registry: Registry, value?: Call | Uint8Array | ExtrinsicValueV5, isSigned = false): ExtrinsicValueV5 {
if (value instanceof GenericExtrinsicV5) {
return value;
} else if (value instanceof registry.createClassUnsafe<Call>('Call')) {
return { method: value };
} else if (isU8a(value)) {
let preamble: Preamble;
// here we decode manually since we need to pull through the version information
if (preambleKind === 'bare') {
preamble = registry.createTypeUnsafe<Preamble>('Preamble', [{
type: 'Bare',
asBare: EXTRINSIC_VERSION
}])
} else if (preambleKind === 'signed') {
preamble = registry.createTypeUnsafe<Preamble>('Preamble', [{
type: 'Signed',
asSigned: value
}])
} else {
preamble = registry.createTypeUnsafe<Preamble>('Preamble', [{
type: 'General',
asSigned: value
}])
}
const method = registry.createTypeUnsafe<Call>('Call', [value.subarray(preamble.encodedLength)]);
const signature = registry.createTypeUnsafe<ExtrinsicSignatureV5>('ExtrinsicSignatureV5', [value, { isSigned }]);
const method = registry.createTypeUnsafe<Call>('Call', [value.subarray(signature.encodedLength)]);

return {
method,
preamble
signature
};
}

Expand All @@ -72,39 +54,39 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get encodedLength(): number {
public override get encodedLength (): number {
return this.toU8a().length;
}

/**
* @description The [[Call]] this extrinsic wraps
*/
public get method(): Call {
public get method (): Call {
return this.getT('method');
}

/**
* @description The [[ExtrinsicSignatureV5]]
*/
public get signature(): ExtrinsicSignatureV5 {
public get signature (): ExtrinsicSignatureV5 {
return this.getT('signature');
}

/**
* @description The version for the signature
*/
public get version(): number {
public get version (): number {
return EXTRINSIC_VERSION;
}

public get preamble(): Preamble {
public get preamble (): Preamble {
return this.getT('preamble');
}

/**
* @description Add an [[ExtrinsicSignatureV5]] to the extrinsic (already generated)
*/
public addSignature(signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV5 {
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV5 {
this.signature.addSignature(signer, signature, payload);

return this;
Expand All @@ -113,7 +95,7 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl {
/**
* @description Sign the extrinsic with a specific keypair
*/
public sign(account: IKeyringPair, options: SignatureOptions): GenericExtrinsicV5 {
public sign (account: IKeyringPair, options: SignatureOptions): GenericExtrinsicV5 {
this.signature.sign(this.method, account, options);

return this;
Expand All @@ -122,7 +104,7 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl {
/**
* @describe Adds a fake signature to the extrinsic
*/
public signFake(signer: Address | Uint8Array | string, options: SignatureOptions): GenericExtrinsicV5 {
public signFake (signer: Address | Uint8Array | string, options: SignatureOptions): GenericExtrinsicV5 {
this.signature.signFake(this.method, signer, options);

return this;
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/extrinsic/v5/ExtrinsicSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class GenericExtrinsicSignatureV5 extends Struct implements IExtrinsicSig
registry,
objectSpread(
// eslint-disable-next-line sort-keys
{ extrinsicFormatVersion: 'ExtrinsicVersion', signer: 'Address', signature: 'ExtrinsicSignature', transactionExtensionVersion: 'ExtensionVersion' },
{ signer: 'Address', signature: 'ExtrinsicSignature', transactionExtensionVersion: 'u8' },
signTypes
),
GenericExtrinsicSignatureV5.decodeExtrinsicSignature(value, isSigned)
Expand Down
Loading

0 comments on commit 072f69a

Please sign in to comment.