diff --git a/packages/api-contract/src/base/Blueprint.ts b/packages/api-contract/src/base/Blueprint.ts index d035ca6047..e0890526d4 100644 --- a/packages/api-contract/src/base/Blueprint.ts +++ b/packages/api-contract/src/base/Blueprint.ts @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ApiTypes, DecorateMethod, SubmittableResultImpl } from '@polkadot/api/types'; +import { ApiTypes, DecorateMethod } from '@polkadot/api/types'; import { AccountId, Address, Hash } from '@polkadot/types/interfaces'; -import { IKeyringPair } from '@polkadot/types/types'; +import { IKeyringPair, ISubmittableResult } from '@polkadot/types/types'; import { ApiObject, ContractABIPre } from '../types'; import BN from 'bn.js'; @@ -27,7 +27,7 @@ export interface BlueprintCreate { class BlueprintCreateResult extends SubmittableResult { public readonly contract?: Contract; - constructor (result: SubmittableResultImpl, contract?: Contract) { + constructor (result: ISubmittableResult, contract?: Contract) { super(result); this.contract = contract; diff --git a/packages/api-contract/src/base/Code.ts b/packages/api-contract/src/base/Code.ts index 36d98959da..7c40054450 100644 --- a/packages/api-contract/src/base/Code.ts +++ b/packages/api-contract/src/base/Code.ts @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ApiTypes, DecorateMethod, SubmittableResultImpl } from '@polkadot/api/types'; +import { ApiTypes, DecorateMethod } from '@polkadot/api/types'; import { AccountId, Address, Hash } from '@polkadot/types/interfaces'; -import { IKeyringPair } from '@polkadot/types/types'; +import { IKeyringPair, ISubmittableResult } from '@polkadot/types/types'; import { ApiObject, ContractABIPre } from '../types'; import BN from 'bn.js'; @@ -27,7 +27,7 @@ export interface CodePutCode { class CodePutCodeResult extends SubmittableResult { public readonly blueprint?: Blueprint; - constructor (result: SubmittableResultImpl, blueprint?: Blueprint) { + constructor (result: ISubmittableResult, blueprint?: Blueprint) { super(result); this.blueprint = blueprint; @@ -55,7 +55,7 @@ export default class Code extends BaseWithTx }; } - private createResult = (result: SubmittableResultImpl): CodePutCodeResult => { + private createResult = (result: ISubmittableResult): CodePutCodeResult => { let blueprint: Blueprint | undefined; if (result.isInBlock) { diff --git a/packages/api/src/submittable/Result.ts b/packages/api/src/submittable/Result.ts index 035d78a7fa..a042a322d0 100644 --- a/packages/api/src/submittable/Result.ts +++ b/packages/api/src/submittable/Result.ts @@ -3,9 +3,10 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ExtrinsicStatus, EventRecord } from '@polkadot/types/interfaces'; -import { SubmittableResultImpl, SubmittableResultValue } from './types'; +import { ISubmittableResult } from '@polkadot/types/types'; +import { SubmittableResultValue } from './types'; -export default class SubmittableResult implements SubmittableResultImpl { +export default class SubmittableResult implements ISubmittableResult { public readonly events: EventRecord[]; public readonly status: ExtrinsicStatus; diff --git a/packages/api/src/submittable/createClass.ts b/packages/api/src/submittable/createClass.ts index 5dee3db60c..2da25936e3 100644 --- a/packages/api/src/submittable/createClass.ts +++ b/packages/api/src/submittable/createClass.ts @@ -4,9 +4,9 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { AccountData, Address, Call, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index, RuntimeDispatchInfo } from '@polkadot/types/interfaces'; -import { Callback, Codec, Constructor, IKeyringPair, ITuple, Registry, SignatureOptions } from '@polkadot/types/types'; +import { Callback, Codec, Constructor, IKeyringPair, ITuple, Registry, SignatureOptions, ISubmittableResult } from '@polkadot/types/types'; import { ApiInterfaceRx, ApiTypes, SignerResult } from '../types'; -import { AddressOrPair, SignerOptions, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultImpl, SubmittableResultResult, SubmittableResultSubscription, SubmittableThis } from './types'; +import { AddressOrPair, SignerOptions, SubmittableExtrinsic, SubmittablePaymentResult, SubmittableResultResult, SubmittableResultSubscription, SubmittableThis } from './types'; import { Observable, combineLatest, of } from 'rxjs'; import { first, map, mapTo, mergeMap, switchMap, tap } from 'rxjs/operators'; @@ -93,20 +93,20 @@ export default function createClass ({ api, apiType, public signAndSend (account: AddressOrPair, options?: Partial): SubmittableResultResult; // signAndSend with a subscription, i.e. callback provided - public signAndSend (account: AddressOrPair, statusCb: Callback): SubmittableResultSubscription; + public signAndSend (account: AddressOrPair, statusCb: Callback): SubmittableResultSubscription; // signAndSend with options and a callback - public signAndSend (account: AddressOrPair, options: Partial, statusCb?: Callback): SubmittableResultSubscription; + public signAndSend (account: AddressOrPair, options: Partial, statusCb?: Callback): SubmittableResultSubscription; // signAndSend implementation for all 3 cases above - public signAndSend (account: AddressOrPair, optionsOrStatus?: Partial | Callback, optionalStatusCb?: Callback): SubmittableResultResult | SubmittableResultSubscription { + public signAndSend (account: AddressOrPair, optionsOrStatus?: Partial | Callback, optionalStatusCb?: Callback): SubmittableResultResult | SubmittableResultSubscription { const [options, statusCb] = this._makeSignAndSendOptions(optionsOrStatus, optionalStatusCb); const isSubscription = this._api.hasSubscriptions && (this._ignoreStatusCb || !!statusCb); return this._decorateMethod( (): Observable => ( this._signObservable(account, options).pipe( - switchMap((updateId: number | undefined): Observable | Observable => + switchMap((updateId: number | undefined): Observable | Observable => isSubscription ? this._subscribeObservable(updateId) : this._sendObservable(updateId) @@ -119,10 +119,10 @@ export default function createClass ({ api, apiType, public send (): SubmittableResultResult; // send with a status callback - public send (statusCb: Callback): SubmittableResultSubscription; + public send (statusCb: Callback): SubmittableResultSubscription; // send implementation for both immediate Hash and statusCb variants - public send (statusCb?: Callback): SubmittableResultResult | SubmittableResultSubscription { + public send (statusCb?: Callback): SubmittableResultResult | SubmittableResultSubscription { const isSubscription = this._api.hasSubscriptions && (this._ignoreStatusCb || !!statusCb); return this._decorateMethod( @@ -132,7 +132,7 @@ export default function createClass ({ api, apiType, )(statusCb); } - private _makeSignAndSendOptions (optionsOrStatus?: Partial | Callback, statusCb?: Callback): [Partial, Callback?] { + private _makeSignAndSendOptions (optionsOrStatus?: Partial | Callback, statusCb?: Callback): [Partial, Callback?] { let options: Partial = {}; if (isFunction(optionsOrStatus)) { @@ -249,13 +249,13 @@ export default function createClass ({ api, apiType, ]); } - private _updateSigner (updateId: number, status: Hash | SubmittableResultImpl): void { + private _updateSigner (updateId: number, status: Hash | ISubmittableResult): void { if ((updateId !== -1) && this._api.signer && this._api.signer.update) { this._api.signer.update(updateId, status); } } - private _statusObservable (status: ExtrinsicStatus): Observable { + private _statusObservable (status: ExtrinsicStatus): Observable { if (!status.isFinalized && !status.isInBlock) { return of(new SubmittableResult({ status })); } @@ -268,7 +268,7 @@ export default function createClass ({ api, apiType, this._api.rpc.chain.getBlock(blockHash), this._api.query.system.events.at(blockHash) ]).pipe( - map(([signedBlock, allEvents]): SubmittableResultImpl => + map(([signedBlock, allEvents]): ISubmittableResult => new SubmittableResult({ events: filterEvents(this.hash, signedBlock, allEvents), status @@ -287,11 +287,11 @@ export default function createClass ({ api, apiType, ); } - private _subscribeObservable = (updateId = -1): Observable => { + private _subscribeObservable = (updateId = -1): Observable => { return this._api.rpc.author .submitAndWatchExtrinsic(this) .pipe( - switchMap((status): Observable => + switchMap((status): Observable => this._statusObservable(status) ), tap((status): void => { diff --git a/packages/api/src/submittable/types.ts b/packages/api/src/submittable/types.ts index 8fc17bba08..a746239e07 100644 --- a/packages/api/src/submittable/types.ts +++ b/packages/api/src/submittable/types.ts @@ -3,23 +3,11 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { AccountId, Address, ExtrinsicStatus, EventRecord, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces'; -import { AnyNumber, AnyU8a, Callback, Codec, IExtrinsic, IExtrinsicEra, IKeyringPair, SignatureOptions } from '@polkadot/types/types'; -import { ApiTypes, Signer } from '../types'; +import { AnyNumber, AnyU8a, Callback, Codec, IExtrinsic, IExtrinsicEra, IKeyringPair, SignatureOptions, Signer, ISubmittableResult } from '@polkadot/types/types'; +import { ApiTypes } from '../types'; import { Observable } from 'rxjs'; -export interface SubmittableResultImpl { - readonly events: EventRecord[]; - readonly status: ExtrinsicStatus; - readonly isCompleted: boolean; - readonly isError: boolean; - readonly isFinalized: boolean; - readonly isInBlock: boolean; - - filterRecords (section: string, method: string): EventRecord[]; - findRecord (section: string, method: string): EventRecord | undefined; -} - export interface SubmittableResultValue { events?: EventRecord[]; status: ExtrinsicStatus; @@ -32,12 +20,12 @@ export type SubmittablePaymentResult = export type SubmittableResultResult = ApiType extends 'rxjs' - ? Observable + ? Observable : Promise; export type SubmittableResultSubscription = ApiType extends 'rxjs' - ? Observable + ? Observable : Promise<() => void>; export type SubmittableThis = @@ -60,7 +48,7 @@ export interface SubmittableExtrinsic extends IExtrins send(): SubmittableResultResult; - send(statusCb: Callback): SubmittableResultSubscription; + send(statusCb: Callback): SubmittableResultSubscription; sign(account: IKeyringPair, _options: Partial): this; @@ -68,7 +56,7 @@ export interface SubmittableExtrinsic extends IExtrins signAndSend(account: AddressOrPair, options?: Partial): SubmittableResultResult; - signAndSend(account: AddressOrPair, statusCb: Callback): SubmittableResultSubscription; + signAndSend(account: AddressOrPair, statusCb: Callback): SubmittableResultSubscription; - signAndSend(account: AddressOrPair, options: Partial, statusCb?: Callback): SubmittableResultSubscription; + signAndSend(account: AddressOrPair, options: Partial, statusCb?: Callback): SubmittableResultSubscription; } diff --git a/packages/api/src/types/index.ts b/packages/api/src/types/index.ts index 7a6d07e2f2..bb7c526a87 100644 --- a/packages/api/src/types/index.ts +++ b/packages/api/src/types/index.ts @@ -11,10 +11,9 @@ import { Constants } from '@polkadot/metadata/Decorated/types'; import { RpcInterface, UserRpc } from '@polkadot/rpc-core/types'; import { Metadata } from '@polkadot/types'; import { Hash, RuntimeVersion } from '@polkadot/types/interfaces'; -import { RegistryTypes, SignatureOptions, SignerPayloadJSON, SignerPayloadRaw, Registry } from '@polkadot/types/types'; +import { RegistryTypes, Signer, SignerResult, SignatureOptions, Registry } from '@polkadot/types/types'; import ApiBase from '../base'; -import { SubmittableResultImpl } from '../submittable/types'; import { DeriveAllSections } from '../util/decorate'; import { DecoratedRpc } from './rpc'; import { QueryableStorage, QueryableStorageMulti } from './storage'; @@ -98,33 +97,4 @@ export interface SignerOptions extends SignatureOptions { genesisHash: Hash; } -export interface SignerResult { - /** - * @description The id for this request - */ - id: number; - - /** - * @description The resulting signature in hex - */ - signature: string; -} - -export interface Signer { - /** - * @description signs an extrinsic payload from a serialized form - */ - signPayload?: (payload: SignerPayloadJSON) => Promise; - - /** - * @description signs a raw payload, only the bytes data as supplied - */ - signRaw?: (raw: SignerPayloadRaw) => Promise; - - /** - * @description Receives an update for the extrinsic signed by a `signer.sign` - */ - update?: (id: number, status: Hash | SubmittableResultImpl) => void; -} - -export { ApiBase }; +export { ApiBase, Signer, SignerResult }; diff --git a/packages/rpc-core/src/formatting.spec-FIXME.ts b/packages/rpc-core/src/formatting.spec.FIXME-ts similarity index 100% rename from packages/rpc-core/src/formatting.spec-FIXME.ts rename to packages/rpc-core/src/formatting.spec.FIXME-ts diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts deleted file mode 100644 index a0d2fb9c17..0000000000 --- a/packages/types/src/types.ts +++ /dev/null @@ -1,431 +0,0 @@ -// Copyright 2017-2020 @polkadot/types authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import { SignOptions } from '@polkadot/keyring/types'; -import { FunctionMetadataLatest } from './interfaces/metadata'; -import { ChainProperties } from './interfaces/rpc'; -import { Address, Balance, Call, EcdsaSignature, Ed25519Signature, Index, Sr25519Signature } from './interfaces/runtime'; - -import BN from 'bn.js'; - -import { InterfaceRegistry } from './interfaceRegistry'; -import { Signer } from '@polkadot/api/types'; - -export * from './codec/types'; - -// helper to xtract keys from an array -export type ArrayElementType> = T extends ReadonlyArray - ? ElementType - : never; - -export type BareOpts = boolean | Record; - -export type InterfaceTypes = keyof InterfaceRegistry; - -export interface CallBase { - callIndex: Uint8Array; - meta: FunctionMetadataLatest; - method: string; - section: string; - toJSON: () => any; -} - -export interface CallFunction extends CallBase { - (...args: any[]): Call; -} - -export type Calls = Record; - -export type ModulesWithCalls = Record; - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IKeyringPair { - address: string; - publicKey: Uint8Array; - sign: (data: Uint8Array, options?: SignOptions) => Uint8Array; -} - -// eslint-disable-next-line @typescript-eslint/no-empty-interface -interface CodecArgArray extends Array {} -export type CodecArg = Codec | BN | boolean | string | Uint8Array | boolean | number | string | undefined | CodecArgArray | CodecArgObject; - -export type Callback = (result: T) => void | Promise; - -interface CodecArgObject { - [index: string]: CodecArg; -} - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export type AnyFunction = (...args: any[]) => any; - -export type AnyNumber = BN | Uint8Array | number | string; - -export type AnyString = string | string; - -export type AnyU8a = Uint8Array | number[] | string; - -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface AnyJsonObject extends Record {} -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface AnyJsonArray extends Array {} -export type AnyJson = string | number | boolean | null | undefined | AnyJsonObject | AnyJsonArray; - -/** - * @name Codec - * @description - * The base Codec interface. All types implement the interface provided here. Additionally - * implementors can add their own specific interfaces and helpers with getters and functions. - * The Codec Base is however required for operating as an encoding/decoding layer - */ -export interface Codec { - /** - * @description The length of the value when encoded as a Uint8Array - */ - readonly encodedLength: number; - - /** - * @description Returns a hash of the value - */ - readonly hash: IHash; - - /** - * @description Checks if the value is an empty value - */ - readonly isEmpty: boolean; - - /** - * @description The registry associated with this object - */ - readonly registry: Registry; - - /** - * @description Compares the value of the input to see if there is a match - */ - eq (other?: any): boolean; - - /** - * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation - */ - toHex (isLe?: boolean): string; - - /** - * @description Converts the Object to JSON, typically used for RPC transfers - */ - toJSON (): AnyJson; - - /** - * @description Returns the base runtime type name for this instance - */ - toRawType (): string; - - /** - * @description Returns the string representation of the value - */ - toString (): string; - - /** - * @description Encodes the value as a Uint8Array as per the SCALE specifications - * @param isBare true when the value has none of the type-specific prefixes (internal) - */ - toU8a (isBare?: BareOpts): Uint8Array; -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IU8a extends Uint8Array, Codec { - bitLength (): number; - toJSON (): any; -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix,@typescript-eslint/no-empty-interface -export interface IHash extends IU8a { } - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface ICompact extends Codec { - toBn (): BN; - toNumber (): number; - unwrap (): T; -} - -export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a'; - -export interface Constructor { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - new(registry: Registry, ...value: any[]): T; -} - -export type ConstructorDef = Record>; - -export type RegistryTypes = Record | { _enum: string[] | Record } | { _set: Record }>; - -export interface RuntimeVersionInterface { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - readonly apis: any[]; - readonly authoringVersion: BN; - // eslint-disable-next-line @typescript-eslint/ban-types - readonly implName: String; - readonly implVersion: BN; - // eslint-disable-next-line @typescript-eslint/ban-types - readonly specName: String; - readonly specVersion: BN; -} - -export interface SignatureOptions { - blockHash: AnyU8a; - era?: IExtrinsicEra; - genesisHash: AnyU8a; - nonce: AnyNumber; - runtimeVersion: RuntimeVersionInterface; - signer?: Signer; - tip?: AnyNumber; -} - -export type ArgsDef = Record; - -// A type alias for [Type1, Type2] & Codec, representing a tuple (Type1, Type2) -// FIXME Implement this generic on Tuple.ts itself. -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export type ITuple = Sub & Codec - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IMethod extends Codec { - readonly args: Codec[]; - readonly argsDef: ArgsDef; - readonly callIndex: Uint8Array; - readonly data: Uint8Array; - readonly hash: IHash; - readonly hasOrigin: boolean; - readonly meta: FunctionMetadataLatest; -} - -interface ExtrinsicSignatureBase { - readonly isSigned: boolean; - readonly era: IExtrinsicEra; - readonly nonce: ICompact; - readonly signature: EcdsaSignature | Ed25519Signature | Sr25519Signature; - readonly signer: Address; - readonly tip: ICompact; -} - -export interface ExtrinsicPayloadValue { - blockHash: AnyU8a; - era: AnyU8a | IExtrinsicEra; - genesisHash: AnyU8a; - method: AnyU8a | IMethod; - nonce: AnyNumber; - specVersion: AnyNumber; - tip: AnyNumber; -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IExtrinsicSignature extends ExtrinsicSignatureBase, Codec { - addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: Uint8Array | string): IExtrinsicSignature; - sign (method: Call, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature; - signFake (method: Call, address: Address | Uint8Array | string, options: SignatureOptions): IExtrinsicSignature; -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IExtrinsicEra extends Codec { - asImmortalEra: Codec; - asMortalEra: Codec; -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -interface IExtrinsicSignable { - addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): T; - sign (account: IKeyringPair, options: SignatureOptions): T; - signFake (address: Address | Uint8Array | string, options: SignatureOptions): T; -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IExtrinsicImpl extends IExtrinsicSignable, Codec { - readonly method: Call; - readonly signature: IExtrinsicSignature; - readonly version: number; -} - -// eslint-disable-next-line @typescript-eslint/interface-name-prefix -export interface IExtrinsic extends IExtrinsicSignable, ExtrinsicSignatureBase, IMethod { - readonly length: number; - readonly method: Call; - 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; -} - -export interface RegistryMetadataText extends String, Codec { - setOverride (override: string): void; -} - -export interface RegistryMetadataCallArg { - name: RegistryMetadataText; - type: RegistryMetadataText; -} - -export interface RegistryMetadataCall { - args: RegistryMetadataCallArg[]; - name: RegistryMetadataText; - - toJSON (): string | AnyJsonObject; -} - -export interface RegistryMetadataCalls { - isSome: boolean; - unwrap (): RegistryMetadataCall[]; -} - -export interface RegistryError { - documentation: string[]; - index: number; - name: string; - section: string; -} - -export interface RegistryMetadataError { - // eslint-disable-next-line @typescript-eslint/ban-types - name: String; - // eslint-disable-next-line @typescript-eslint/ban-types - documentation: String[]; -} - -export type RegistryMetadataErrors = RegistryMetadataError[]; - -export interface RegistryMetadataEvent { - args: any[]; - name: RegistryMetadataText; -} - -export interface RegistryMetadataEvents { - isSome: boolean; - unwrap (): RegistryMetadataEvent[]; -} - -export interface RegistryMetadataExtrinsic { - version: BN; - // eslint-disable-next-line @typescript-eslint/ban-types - signedExtensions: String[]; -} - -export interface RegistryMetadataModule { - calls: RegistryMetadataCalls; - errors: RegistryMetadataErrors; - events: RegistryMetadataEvents; - name: RegistryMetadataText; -} - -export interface RegistryMetadataLatest { - modules: RegistryMetadataModule[]; - extrinsic: RegistryMetadataExtrinsic; -} - -export interface RegistryMetadata { - asLatest: RegistryMetadataLatest; -} - -export interface Registry { - readonly chainDecimals: number; - readonly chainSS58: number | undefined; - readonly chainToken: string; - - findMetaCall (callIndex: Uint8Array): CallFunction; - findMetaError (errorIndex: Uint8Array): any; - // due to same circular imports where types don't really want to import from EventData, - // keep this as a generic Codec, however the actual impl. returns the correct - findMetaEvent (eventIndex: Uint8Array): Constructor; - - get (name: string): Constructor | undefined; - getChainProperties (): ChainProperties | undefined; - getDefinition (name: string): string | undefined; - getOrThrow (name: string, msg?: string): Constructor; - getSignedExtensionExtra (): Record; - getSignedExtensionTypes (): Record; - hasClass (name: string): boolean; - hasDef (name: string): boolean; - hasType (name: string): boolean; - register (type: Constructor | RegistryTypes): void; - register (name: string, type: Constructor): void; - register (arg1: string | Constructor | RegistryTypes, arg2?: Constructor): void; - setChainProperties (properties?: ChainProperties): void; - setMetadata (metadata: RegistryMetadata): void; -} diff --git a/packages/types/src/types/calls.ts b/packages/types/src/types/calls.ts new file mode 100644 index 0000000000..69198031d9 --- /dev/null +++ b/packages/types/src/types/calls.ts @@ -0,0 +1,22 @@ +// Copyright 2017-2020 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { FunctionMetadataLatest } from '../interfaces/metadata'; +import { Call } from '../interfaces/runtime'; + +export interface CallBase { + callIndex: Uint8Array; + meta: FunctionMetadataLatest; + method: string; + section: string; + toJSON: () => any; +} + +export interface CallFunction extends CallBase { + (...args: any[]): Call; +} + +export type Calls = Record; + +export type ModulesWithCalls = Record; diff --git a/packages/types/src/types/codec.ts b/packages/types/src/types/codec.ts new file mode 100644 index 0000000000..049bd2e9b9 --- /dev/null +++ b/packages/types/src/types/codec.ts @@ -0,0 +1,89 @@ +// Copyright 2017-2020 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { AnyJson, BareOpts } from './helpers'; +import { IHash } from './interfaces'; +import { Registry } from './registry'; + +import BN from 'bn.js'; + +export type ArgsDef = Record; + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +interface CodecArgArray extends Array {} + +interface CodecArgObject { + [index: string]: CodecArg; +} + +export type CodecArg = Codec | BN | boolean | string | Uint8Array | boolean | number | string | undefined | CodecArgArray | CodecArgObject; + +export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a'; + +export interface Constructor { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + new(registry: Registry, ...value: any[]): T; +} + +export type ConstructorDef = Record>; + +/** + * @name Codec + * @description + * The base Codec interface. All types implement the interface provided here. Additionally + * implementors can add their own specific interfaces and helpers with getters and functions. + * The Codec Base is however required for operating as an encoding/decoding layer + */ +export interface Codec { + /** + * @description The length of the value when encoded as a Uint8Array + */ + readonly encodedLength: number; + + /** + * @description Returns a hash of the value + */ + readonly hash: IHash; + + /** + * @description Checks if the value is an empty value + */ + readonly isEmpty: boolean; + + /** + * @description The registry associated with this object + */ + readonly registry: Registry; + + /** + * @description Compares the value of the input to see if there is a match + */ + eq (other?: any): boolean; + + /** + * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation + */ + toHex (isLe?: boolean): string; + + /** + * @description Converts the Object to JSON, typically used for RPC transfers + */ + toJSON (): AnyJson; + + /** + * @description Returns the base runtime type name for this instance + */ + toRawType (): string; + + /** + * @description Returns the string representation of the value + */ + toString (): string; + + /** + * @description Encodes the value as a Uint8Array as per the SCALE specifications + * @param isBare true when the value has none of the type-specific prefixes (internal) + */ + toU8a (isBare?: BareOpts): Uint8Array; +} diff --git a/packages/types/src/types/extrinsic.ts b/packages/types/src/types/extrinsic.ts new file mode 100644 index 0000000000..88abf21082 --- /dev/null +++ b/packages/types/src/types/extrinsic.ts @@ -0,0 +1,198 @@ +// Copyright 2017-2020 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { ExtrinsicStatus } from '../interfaces/rpc'; +import { Address, Balance, Call, EcdsaSignature, Ed25519Signature, Index, Sr25519Signature } from '../interfaces/runtime'; +import { EventRecord } from '../interfaces/system'; +import { Codec } from './codec'; +import { AnyNumber, AnyU8a } from './helpers'; +import { ICompact, IHash, IKeyringPair, IMethod, IRuntimeVersion } from './interfaces'; + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface ISubmittableResult { + readonly events: EventRecord[]; + readonly status: ExtrinsicStatus; + readonly isCompleted: boolean; + readonly isError: boolean; + readonly isFinalized: boolean; + readonly isInBlock: boolean; + + filterRecords (section: string, method: string): EventRecord[]; + findRecord (section: string, method: string): EventRecord | undefined; +} + +export interface SignerResult { + /** + * @description The id for this request + */ + id: number; + + /** + * @description The resulting signature in hex + */ + signature: string; +} + +export interface Signer { + /** + * @description signs an extrinsic payload from a serialized form + */ + signPayload?: (payload: SignerPayloadJSON) => Promise; + + /** + * @description signs a raw payload, only the bytes data as supplied + */ + signRaw?: (raw: SignerPayloadRaw) => Promise; + + /** + * @description Receives an update for the extrinsic signed by a `signer.sign` + */ + update?: (id: number, status: IHash | ISubmittableResult) => void; +} + +export interface SignatureOptions { + blockHash: AnyU8a; + era?: IExtrinsicEra; + genesisHash: AnyU8a; + nonce: AnyNumber; + runtimeVersion: IRuntimeVersion; + signer?: Signer; + tip?: AnyNumber; +} + +interface ExtrinsicSignatureBase { + readonly isSigned: boolean; + readonly era: IExtrinsicEra; + readonly nonce: ICompact; + readonly signature: EcdsaSignature | Ed25519Signature | Sr25519Signature; + readonly signer: Address; + readonly tip: ICompact; +} + +export interface ExtrinsicPayloadValue { + blockHash: AnyU8a; + era: AnyU8a | IExtrinsicEra; + genesisHash: AnyU8a; + method: AnyU8a | IMethod; + nonce: AnyNumber; + specVersion: AnyNumber; + tip: AnyNumber; +} + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface IExtrinsicSignature extends ExtrinsicSignatureBase, Codec { + addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: Uint8Array | string): IExtrinsicSignature; + sign (method: Call, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature; + signFake (method: Call, address: Address | Uint8Array | string, options: SignatureOptions): IExtrinsicSignature; +} + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface IExtrinsicEra extends Codec { + asImmortalEra: Codec; + asMortalEra: Codec; +} + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +interface IExtrinsicSignable { + addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): T; + sign (account: IKeyringPair, options: SignatureOptions): T; + signFake (address: Address | Uint8Array | string, options: SignatureOptions): T; +} + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface IExtrinsicImpl extends IExtrinsicSignable, Codec { + readonly method: Call; + readonly signature: IExtrinsicSignature; + readonly version: number; +} + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface IExtrinsic extends IExtrinsicSignable, ExtrinsicSignatureBase, IMethod { + readonly length: number; + readonly method: Call; + 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; +} diff --git a/packages/types/src/types/helpers.ts b/packages/types/src/types/helpers.ts new file mode 100644 index 0000000000..f23fc4a482 --- /dev/null +++ b/packages/types/src/types/helpers.ts @@ -0,0 +1,35 @@ +// Copyright 2017-2020 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import BN from 'bn.js'; + +import { InterfaceRegistry } from '../interfaceRegistry'; + +export type InterfaceTypes = keyof InterfaceRegistry; + +// helper to extract keys from an array +export type ArrayElementType> = T extends ReadonlyArray + ? ElementType + : never; + +export type BareOpts = boolean | Record; + +export type Callback = (result: T) => void | Promise; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export type AnyFunction = (...args: any[]) => any; + +export type AnyNumber = BN | Uint8Array | number | string; + +export type AnyString = string | string; + +export type AnyU8a = Uint8Array | number[] | string; + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface AnyJsonObject extends Record {} + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface AnyJsonArray extends Array {} + +export type AnyJson = string | number | boolean | null | undefined | AnyJsonObject | AnyJsonArray; diff --git a/packages/types/src/types/index.ts b/packages/types/src/types/index.ts new file mode 100644 index 0000000000..5d638ed666 --- /dev/null +++ b/packages/types/src/types/index.ts @@ -0,0 +1,11 @@ +// Copyright 2017-2020 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +export * from '../codec/types'; +export * from './calls'; +export * from './codec'; +export * from './extrinsic'; +export * from './helpers'; +export * from './interfaces'; +export * from './registry'; diff --git a/packages/types/src/types/interfaces.ts b/packages/types/src/types/interfaces.ts new file mode 100644 index 0000000000..3040764e58 --- /dev/null +++ b/packages/types/src/types/interfaces.ts @@ -0,0 +1,59 @@ +// Copyright 2017-2020 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. +/* eslint-disable @typescript-eslint/interface-name-prefix */ + +import { SignOptions } from '@polkadot/keyring/types'; +import { FunctionMetadataLatest } from '../interfaces/metadata'; + +import BN from 'bn.js'; + +import { ArgsDef, Codec } from './codec'; + +export interface ICompact extends Codec { + toBn (): BN; + toNumber (): number; + unwrap (): T; +} + +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface IHash extends IU8a { } + +export interface IKeyringPair { + address: string; + publicKey: Uint8Array; + sign: (data: Uint8Array, options?: SignOptions) => Uint8Array; +} + +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export interface IMethod extends Codec { + readonly args: Codec[]; + readonly argsDef: ArgsDef; + readonly callIndex: Uint8Array; + readonly data: Uint8Array; + readonly hash: IHash; + readonly hasOrigin: boolean; + readonly meta: FunctionMetadataLatest; +} + +export interface IRuntimeVersion { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + readonly apis: any[]; + readonly authoringVersion: BN; + // eslint-disable-next-line @typescript-eslint/ban-types + readonly implName: String; + readonly implVersion: BN; + // eslint-disable-next-line @typescript-eslint/ban-types + readonly specName: String; + readonly specVersion: BN; +} + +// A type alias for [Type1, Type2] & Codec, representing a tuple (Type1, Type2) +// FIXME Implement this generic on Tuple.ts itself. +// eslint-disable-next-line @typescript-eslint/interface-name-prefix +export type ITuple = Sub & Codec + +export interface IU8a extends Uint8Array, Codec { + bitLength (): number; + toJSON (): any; +} diff --git a/packages/types/src/types/registry.ts b/packages/types/src/types/registry.ts new file mode 100644 index 0000000000..87e4e8648c --- /dev/null +++ b/packages/types/src/types/registry.ts @@ -0,0 +1,109 @@ +// Copyright 2017-2020 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { ChainProperties } from '../interfaces/rpc'; + +import BN from 'bn.js'; + +import { CallFunction } from './calls'; +import { Codec, Constructor } from './codec'; +import { AnyJsonObject, InterfaceTypes } from './helpers'; + +export type RegistryTypes = Record | { _enum: string[] | Record } | { _set: Record }>; + +export interface RegistryMetadataText extends String, Codec { + setOverride (override: string): void; +} + +export interface RegistryMetadataCallArg { + name: RegistryMetadataText; + type: RegistryMetadataText; +} + +export interface RegistryMetadataCall { + args: RegistryMetadataCallArg[]; + name: RegistryMetadataText; + + toJSON (): string | AnyJsonObject; +} + +export interface RegistryMetadataCalls { + isSome: boolean; + unwrap (): RegistryMetadataCall[]; +} + +export interface RegistryError { + documentation: string[]; + index: number; + name: string; + section: string; +} + +export interface RegistryMetadataError { + // eslint-disable-next-line @typescript-eslint/ban-types + name: String; + // eslint-disable-next-line @typescript-eslint/ban-types + documentation: String[]; +} + +export type RegistryMetadataErrors = RegistryMetadataError[]; + +export interface RegistryMetadataEvent { + args: any[]; + name: RegistryMetadataText; +} + +export interface RegistryMetadataEvents { + isSome: boolean; + unwrap (): RegistryMetadataEvent[]; +} + +export interface RegistryMetadataExtrinsic { + version: BN; + // eslint-disable-next-line @typescript-eslint/ban-types + signedExtensions: String[]; +} + +export interface RegistryMetadataModule { + calls: RegistryMetadataCalls; + errors: RegistryMetadataErrors; + events: RegistryMetadataEvents; + name: RegistryMetadataText; +} + +export interface RegistryMetadataLatest { + modules: RegistryMetadataModule[]; + extrinsic: RegistryMetadataExtrinsic; +} + +export interface RegistryMetadata { + asLatest: RegistryMetadataLatest; +} + +export interface Registry { + readonly chainDecimals: number; + readonly chainSS58: number | undefined; + readonly chainToken: string; + + findMetaCall (callIndex: Uint8Array): CallFunction; + findMetaError (errorIndex: Uint8Array): any; + // due to same circular imports where types don't really want to import from EventData, + // keep this as a generic Codec, however the actual impl. returns the correct + findMetaEvent (eventIndex: Uint8Array): Constructor; + + get (name: string): Constructor | undefined; + getChainProperties (): ChainProperties | undefined; + getDefinition (name: string): string | undefined; + getOrThrow (name: string, msg?: string): Constructor; + getSignedExtensionExtra (): Record; + getSignedExtensionTypes (): Record; + hasClass (name: string): boolean; + hasDef (name: string): boolean; + hasType (name: string): boolean; + register (type: Constructor | RegistryTypes): void; + register (name: string, type: Constructor): void; + register (arg1: string | Constructor | RegistryTypes, arg2?: Constructor): void; + setChainProperties (properties?: ChainProperties): void; + setMetadata (metadata: RegistryMetadata): void; +}