diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bb88f3ce7..951bf7c351 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,10 @@ - The `getRuntimeVersion` and `subscribeRuntimeVersion` RPCs are now only available on the `rpc.state.*` endpoints. This aligns with the Substrate implementation. - Support for extrinsics with versions that is not in the base Substrate implementation (V1-V3) can now be done by providing an implementation for `ExtrinsicUnknown` - +- **Breaking change** SignerPayload is registered and can be overrode now. + - SignerPayload is renamed to SignerPayloadJSON + - SignerPayloadJSON, SignerPayloadRawBase and SignerPayloadRaw are all moved to `@polkadot/types` + # 0.90.1 diff --git a/packages/api/src/base/index.ts b/packages/api/src/base/index.ts index 2662006a08..af80fb6bd9 100644 --- a/packages/api/src/base/index.ts +++ b/packages/api/src/base/index.ts @@ -4,8 +4,8 @@ import { RpcInterface } from '@polkadot/rpc-core/jsonrpc.types'; import { Hash, RuntimeVersion } from '@polkadot/types/interfaces'; -import { CallFunction, RegistryTypes } from '@polkadot/types/types'; -import { ApiOptions, ApiTypes, DecoratedRpc, DecorateMethod, QueryableStorage, QueryableStorageMulti, SignerPayloadRawBase, SubmittableExtrinsics, Signer } from '../types'; +import { CallFunction, RegistryTypes, SignerPayloadRawBase } from '@polkadot/types/types'; +import { ApiOptions, ApiTypes, DecoratedRpc, DecorateMethod, QueryableStorage, QueryableStorageMulti, SubmittableExtrinsics, Signer } from '../types'; import { Constants } from '@polkadot/api-metadata/consts/types'; import { GenericCall, Metadata, getTypeRegistry } from '@polkadot/types'; diff --git a/packages/api/src/submittable/Extrinsic.ts b/packages/api/src/submittable/Extrinsic.ts index b5d4e63bcb..9ed2db5901 100644 --- a/packages/api/src/submittable/Extrinsic.ts +++ b/packages/api/src/submittable/Extrinsic.ts @@ -15,7 +15,6 @@ import { isBn, isFunction, isNumber, isUndefined } from '@polkadot/util'; import { filterEvents, isKeyringPair } from '../util'; import ApiBase from '../base'; -import SignerPayload from '../SignerPayload'; import SubmittableResult from './Result'; type Creator = (extrinsic: Call | Uint8Array | string) => SubmittableExtrinsic; @@ -130,13 +129,13 @@ class Submittable extends _Extrinsic implements SubmittableExtrinsic { + private async _signViaSigner (address: string, optionsWithEra: SignatureOptions, header: Header | null): Promise { if (!this._api.signer) { throw new Error('no signer attached'); } - const payload = new SignerPayload({ - ...eraOptions, + const payload = createType('SignerPayload', { + ...optionsWithEra, address, method: this.method, blockNumber: header ? header.number : 0 diff --git a/packages/api/src/types.ts b/packages/api/src/types.ts index dc537b5864..651258c87a 100644 --- a/packages/api/src/types.ts +++ b/packages/api/src/types.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { Hash, RuntimeVersion } from '@polkadot/types/interfaces'; -import { AnyFunction, Callback, CallFunction, Codec, CodecArg, RegistryTypes, SignatureOptions } from '@polkadot/types/types'; +import { AnyFunction, Callback, CallFunction, Codec, CodecArg, RegistryTypes, SignatureOptions, SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types'; import { SubmittableResultImpl, SubmittableExtrinsic } from './submittable/types'; import BN from 'bn.js'; @@ -222,82 +222,6 @@ export interface SignerOptions extends SignatureOptions { genesisHash: Hash; } -export interface SignerPayload { - /** - * @description The ss-58 encoded address - */ - address: string; - - /** - * @description The checkpoint hash of the block, in hex - */ - blockHash: string; - - /** - * @description The checkpoint block number, in hex - */ - blockNumber: string; - - /** - * @description The era for this transaction, in hex - */ - era: string; - - /** - * @description The genesis hash of the chain, in hex - */ - genesisHash: string; - - /** - * @description The encoded method (with arguments) in hex - */ - method: string; - - /** - * @description The nonce for this transaction, in hex - */ - nonce: string; - - /** - * @description The current spec version for the runtime - */ - specVersion: string; - - /** - * @description The tip for this transaction, in hex - */ - tip: string; - - /** - * @description The version of the extrinsic we are dealing with - */ - version: number; -} - -export interface SignerPayloadRawBase { - /** - * @description The hex-encoded data for this request - */ - data: string; - - /** - * @description The type of the contained data - */ - type?: 'bytes' | 'payload'; -} - -export interface SignerPayloadRaw extends SignerPayloadRawBase { - /** - * @description The ss-58 encoded address - */ - address: string; - - /** - * @description The type of the contained data - */ - type: 'bytes' | 'payload'; -} - export interface SignerResult { /** * @description The id for this request @@ -314,7 +238,7 @@ export interface Signer { /** * @description signs an extrinsic payload from a serialized form */ - signPayload?: (payload: SignerPayload) => Promise; + signPayload?: (payload: SignerPayloadJSON) => Promise; /** * @description signs a raw payload, only the bytes data as supplied diff --git a/packages/api/test/util/SingleAccountSigner.ts b/packages/api/test/util/SingleAccountSigner.ts index 0c15c44aad..e6efa0148d 100644 --- a/packages/api/test/util/SingleAccountSigner.ts +++ b/packages/api/test/util/SingleAccountSigner.ts @@ -2,11 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Signer, SignerPayload, SignerResult, SignerPayloadRaw } from '@polkadot/api/types'; +import { Signer, SignerResult } from '@polkadot/api/types'; import { KeyringPair } from '@polkadot/keyring/types'; import { createType } from '@polkadot/types'; import { hexToU8a, u8aToHex } from '@polkadot/util'; +import { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types'; let id = 0; @@ -20,7 +21,7 @@ export class SingleAccountSigner implements Signer { this.signDelay = signDelay; } - public async signPayload (payload: SignerPayload): Promise { + public async signPayload (payload: SignerPayloadJSON): Promise { if (payload.address !== this.keyringPair.address) { throw new Error('does not have the keyringPair'); } diff --git a/packages/types/src/interfaceRegistry.ts b/packages/types/src/interfaceRegistry.ts index da277428c5..410020c669 100644 --- a/packages/types/src/interfaceRegistry.ts +++ b/packages/types/src/interfaceRegistry.ts @@ -3,7 +3,7 @@ import { Compact, Option, Vec } from './codec'; import { Bytes, Data, Fixed64, H160, H256, H512, Null, StorageData, StorageHasher, StorageKey, Text, Type, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive'; -import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, ExtrinsicPayloadUnknown, ExtrinsicPayloadV1, ExtrinsicPayloadV2, ExtrinsicPayloadV3, ExtrinsicUnknown, ExtrinsicV1, ExtrinsicV2, ExtrinsicV3, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Moment, MortalEra, Origin, Perbill, Permill, Phantom, PhantomData, PreRuntime, Seal, SealV0, Signature, SignedBlock, Sr25519Signature, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime'; +import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, ExtrinsicPayloadUnknown, ExtrinsicPayloadV1, ExtrinsicPayloadV2, ExtrinsicPayloadV3, ExtrinsicUnknown, ExtrinsicV1, ExtrinsicV2, ExtrinsicV3, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Moment, MortalEra, Origin, Perbill, Permill, Phantom, PhantomData, PreRuntime, Seal, SealV0, Signature, SignedBlock, SignerPayload, Sr25519Signature, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime'; import { InclusionHeight, Uncle, UncleEntryItem } from './interfaces/authorship'; import { RawAuraPreDigest } from './interfaces/aura'; import { BabeAuthorityWeight, BabeBlockWeight, BabeWeight, RawBabePreDigest, RawBabePreDigestPrimary, RawBabePreDigestSecondary, SlotNumber } from './interfaces/babe'; @@ -241,6 +241,9 @@ export interface InterfaceRegistry { SignedBlock: SignedBlock; 'Option': Option; 'Vec': Vec; + SignerPayload: SignerPayload; + 'Option': Option; + 'Vec': Vec; ValidatorId: ValidatorId; 'Option': Option; 'Vec': Vec; diff --git a/packages/types/src/interfaces/runtime/definitions.ts b/packages/types/src/interfaces/runtime/definitions.ts index deef9e72b5..aab00d9c35 100644 --- a/packages/types/src/interfaces/runtime/definitions.ts +++ b/packages/types/src/interfaces/runtime/definitions.ts @@ -52,6 +52,7 @@ export default { block: 'Block', justification: 'Justification' }, + SignerPayload: 'GenericSignerPayload', ValidatorId: 'AccountId', Weight: 'u32', WeightMultiplier: 'Fixed64', diff --git a/packages/types/src/interfaces/runtime/types.ts b/packages/types/src/interfaces/runtime/types.ts index 72027e5e6d..284af89bf1 100644 --- a/packages/types/src/interfaces/runtime/types.ts +++ b/packages/types/src/interfaces/runtime/types.ts @@ -3,7 +3,7 @@ import { Codec } from '../../types'; import { Compact, Struct } from '../../codec'; -import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericExtrinsicPayloadUnknown, GenericExtrinsicPayloadV1, GenericExtrinsicPayloadV2, GenericExtrinsicPayloadV3, GenericExtrinsicUnknown, GenericExtrinsicV1, GenericExtrinsicV2, GenericExtrinsicV3, GenericImmortalEra, GenericMortalEra, GenericOrigin, H256, H512, Null, StorageData, StorageKey, u128, u32, u64 } from '../../primitive'; +import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericExtrinsicPayloadUnknown, GenericExtrinsicPayloadV1, GenericExtrinsicPayloadV2, GenericExtrinsicPayloadV3, GenericExtrinsicUnknown, GenericExtrinsicV1, GenericExtrinsicV2, GenericExtrinsicV3, GenericImmortalEra, GenericMortalEra, GenericOrigin, GenericSignerPayload, H256, H512, Null, StorageData, StorageKey, u128, u32, u64 } from '../../primitive'; /** GenericAccountId */ export type AccountId = GenericAccountId; @@ -159,6 +159,9 @@ export interface SignedBlock extends Struct { readonly justification: Justification; } +/** GenericSignerPayload */ +export type SignerPayload = GenericSignerPayload; + /** Signature */ export type Sr25519Signature = Signature; diff --git a/packages/api/src/SignerPayload.spec.ts b/packages/types/src/primitive/Extrinsic/SignerPayload.spec.ts similarity index 100% rename from packages/api/src/SignerPayload.spec.ts rename to packages/types/src/primitive/Extrinsic/SignerPayload.spec.ts diff --git a/packages/api/src/SignerPayload.ts b/packages/types/src/primitive/Extrinsic/SignerPayload.ts similarity index 79% rename from packages/api/src/SignerPayload.ts rename to packages/types/src/primitive/Extrinsic/SignerPayload.ts index b3dee00c57..770df26e91 100644 --- a/packages/api/src/SignerPayload.ts +++ b/packages/types/src/primitive/Extrinsic/SignerPayload.ts @@ -2,13 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Address, Balance, BlockNumber, Call, ExtrinsicEra, Hash, Index, RuntimeVersion } from '@polkadot/types/interfaces'; -import { Constructor } from '@polkadot/types/types'; -import { SignerPayload, SignerPayloadRaw } from './types'; +import { Address, Balance, BlockNumber, Call, ExtrinsicEra, Hash, Index, RuntimeVersion } from '../../interfaces'; +import Compact from '../../codec/Compact'; +import Struct from '../../codec/Struct'; +import { createType } from '../../codec'; +import { Codec, Constructor, ISignerPayload, SignerPayloadJSON, SignerPayloadRaw } from '../../types'; +import u8 from '../U8'; -import { createType, Compact, Struct, u8 } from '@polkadot/types'; - -export interface SignerPayloadType { +export interface SignerPayloadType extends Codec { address: Address; blockHash: Hash; blockNumber: BlockNumber; @@ -33,14 +34,14 @@ const _Payload: Constructor = Struct.with({ nonce: 'Compact', runtimeVersion: 'RuntimeVersion', tip: 'Compact', - version: u8 + version: 'u8' }) as any; -export default class Payload extends _Payload { +export default class SignerPayload extends _Payload implements ISignerPayload { /** * @description Creates an representation of the structure as an ISignerPayload JSON */ - public toPayload (): SignerPayload { + public toPayload (): SignerPayloadJSON { const { address, blockHash, blockNumber, era, genesisHash, method, nonce, runtimeVersion: { specVersion }, tip, version } = this; return { diff --git a/packages/types/src/primitive/Extrinsic/index.ts b/packages/types/src/primitive/Extrinsic/index.ts index 6dcd6f6a82..ca0241dbbf 100644 --- a/packages/types/src/primitive/Extrinsic/index.ts +++ b/packages/types/src/primitive/Extrinsic/index.ts @@ -13,3 +13,4 @@ export { default as GenericExtrinsicV2 } from './v2/Extrinsic'; export { default as GenericExtrinsicPayloadV2 } from './v2/ExtrinsicPayload'; export { default as GenericExtrinsicV3 } from './v3/Extrinsic'; export { default as GenericExtrinsicPayloadV3 } from './v3/ExtrinsicPayload'; +export { default as GenericSignerPayload } from './SignerPayload'; diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index 416abf2a78..8583c25a48 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -216,3 +216,85 @@ export interface IExtrinsic extends IExtrinsicSignable, ExtrinsicSig readonly type: number; readonly version: number; } + +export interface SignerPayloadJSON { + /** + * @description The ss-58 encoded address + */ + address: string; + + /** + * @description The checkpoint hash of the block, in hex + */ + blockHash: string; + + /** + * @description The checkpoint block number, in hex + */ + blockNumber: string; + + /** + * @description The era for this transaction, in hex + */ + era: string; + + /** + * @description The genesis hash of the chain, in hex + */ + genesisHash: string; + + /** + * @description The encoded method (with arguments) in hex + */ + method: string; + + /** + * @description The nonce for this transaction, in hex + */ + nonce: string; + + /** + * @description The current spec version for the runtime + */ + specVersion: string; + + /** + * @description The tip for this transaction, in hex + */ + tip: string; + + /** + * @description The version of the extrinsic we are dealing with + */ + version: number; +} + +export interface SignerPayloadRawBase { + /** + * @description The hex-encoded data for this request + */ + data: string; + + /** + * @description The type of the contained data + */ + type?: 'bytes' | 'payload'; +} + +export interface SignerPayloadRaw extends SignerPayloadRawBase { + /** + * @description The ss-58 encoded address + */ + address: string; + + /** + * @description The type of the contained data + */ + type: 'bytes' | 'payload'; +} + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface ISignerPayload { + toPayload (): SignerPayloadJSON; + toRaw (): SignerPayloadRaw; +}