diff --git a/packages/api/src/SubmittableExtrinsic.ts b/packages/api/src/SubmittableExtrinsic.ts index c54aa66a92..54b7bd0ed2 100644 --- a/packages/api/src/SubmittableExtrinsic.ts +++ b/packages/api/src/SubmittableExtrinsic.ts @@ -3,9 +3,8 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { KeyringPair } from '@polkadot/keyring/types'; -import { AccountId, Address, Extrinsic, ExtrinsicStatus, EventRecord, Hash, Index, Method, RuntimeVersion, SignedBlock, Struct, Text, Vector } from '@polkadot/types/index'; -import { SignatureOptions } from '@polkadot/types/ExtrinsicSignature'; -import { AnyNumber, AnyU8a, CodecCallback } from '@polkadot/types/types'; +import { AccountId, Address, Extrinsic, ExtrinsicStatus, EventRecord, Hash, Index, Method, SignedBlock, Struct, Text, Vector } from '@polkadot/types/index'; +import { CodecCallback, SignatureOptions } from '@polkadot/types/types'; import { ApiInterface$Rx, ApiType, OnCallDefinition, Signer } from './types'; import { Observable, of, combineLatest } from 'rxjs'; @@ -14,13 +13,6 @@ import { assert, isBn, isFunction, isNumber, isUndefined } from '@polkadot/util' import filterEvents from './util/filterEvents'; -type SignatureOptionsPartial = { - blockHash?: AnyU8a, - era?: Uint8Array, - nonce: AnyNumber, - version?: RuntimeVersion -}; - type StatusCb = (result: SubmittableResult) => any; type SumbitableResultResult = @@ -132,12 +124,12 @@ export default class SubmittableExtrinsic exten ); } - private expandOptions (options: SignatureOptionsPartial): SignatureOptions { + private expandOptions (options: Partial): SignatureOptions { return { blockHash: this._api.genesisHash, version: this._api.runtimeVersion, ...options - }; + } as SignatureOptions; } send (): SumbitableResultResult; @@ -158,10 +150,10 @@ export default class SubmittableExtrinsic exten // NOTE with the nonce hack, we don't add a signature overload for 2 reasons - // - it makes it incompatible with the base Extrinsic // - for those using TS, we would like to move them towards the new version - sign (account: KeyringPair, _options: SignatureOptionsPartial): SubmittableExtrinsic { + sign (account: KeyringPair, _options: Partial): SubmittableExtrinsic { // HACK here we actually override nonce if it was specified (backwards compat for // the previous signature - don't let userspace break, but allow then time to upgrade) - const options: SignatureOptionsPartial = isBn(_options) || isNumber(_options) + const options: Partial = isBn(_options) || isNumber(_options) ? { nonce: _options as any as number } : _options; @@ -170,10 +162,10 @@ export default class SubmittableExtrinsic exten return this; } - signAndSend (account: KeyringPair | string | AccountId | Address, options?: Partial): SumbitableResultResult; + signAndSend (account: KeyringPair | string | AccountId | Address, options?: Partial>): SumbitableResultResult; signAndSend (account: KeyringPair | string | AccountId | Address, statusCb: StatusCb): SumbitableResultSubscription; - signAndSend (account: KeyringPair | string | AccountId | Address, _options?: Partial | StatusCb, statusCb?: StatusCb): SumbitableResultResult | SumbitableResultSubscription { - let options: Partial = {}; + signAndSend (account: KeyringPair | string | AccountId | Address, _options?: Partial> | StatusCb, statusCb?: StatusCb): SumbitableResultResult | SumbitableResultSubscription { + let options: Partial> = {}; if (isFunction(_options)) { statusCb = _options; diff --git a/packages/api/src/types.ts b/packages/api/src/types.ts index 31ca24483b..1a75105c5a 100644 --- a/packages/api/src/types.ts +++ b/packages/api/src/types.ts @@ -6,8 +6,7 @@ import { Observable } from 'rxjs'; import { ProviderInterface } from '@polkadot/rpc-provider/types'; import { RpcRxInterface$Events } from '@polkadot/rpc-rx/types'; import { Extrinsic, Hash, Metadata, RuntimeVersion, u64 as U64 } from '@polkadot/types/index'; -import { SignatureOptions } from '@polkadot/types/ExtrinsicSignature'; -import { CodecArg, CodecCallback, RegistryTypes } from '@polkadot/types/types'; +import { CodecArg, CodecCallback, RegistryTypes, SignatureOptions } from '@polkadot/types/types'; import { MethodFunction } from '@polkadot/types/Method'; import { StorageFunction } from '@polkadot/types/StorageKey'; diff --git a/packages/api/test/util/SingleAccountSigner.ts b/packages/api/test/util/SingleAccountSigner.ts index 379eb39ceb..b2d79c16b6 100644 --- a/packages/api/test/util/SingleAccountSigner.ts +++ b/packages/api/test/util/SingleAccountSigner.ts @@ -3,7 +3,8 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { KeyringPair } from '@polkadot/keyring/types'; -import { SignatureOptions } from '@polkadot/types/ExtrinsicSignature'; +import { SignatureOptions } from '@polkadot/types/types'; + import { Extrinsic } from '@polkadot/types/index'; let id = 0; diff --git a/packages/types/src/Extrinsic.ts b/packages/types/src/Extrinsic.ts index a5e310b3d4..d340c6b10b 100644 --- a/packages/types/src/Extrinsic.ts +++ b/packages/types/src/Extrinsic.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { KeyringPair } from '@polkadot/keyring/types'; -import { AnyNumber, AnyU8a, Codec } from './types'; +import { AnyNumber, AnyU8a, Codec, SignatureOptions } from './types'; import { isHex, isU8a, u8aToHex, u8aToU8a } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; @@ -11,7 +11,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto'; import Compact from './codec/Compact'; import Struct from './codec/Struct'; import Address from './Address'; -import ExtrinsicSignature, { SignatureOptions } from './ExtrinsicSignature'; +import ExtrinsicSignature from './ExtrinsicSignature'; import Hash from './Hash'; import { FunctionMetadata } from './Metadata/v0/Modules'; import Method from './Method'; diff --git a/packages/types/src/ExtrinsicSignature.ts b/packages/types/src/ExtrinsicSignature.ts index 3d7bbfde5b..deab5bfa7b 100644 --- a/packages/types/src/ExtrinsicSignature.ts +++ b/packages/types/src/ExtrinsicSignature.ts @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { KeyringPair } from '@polkadot/keyring/types'; -import { AnyNumber, AnyU8a } from './types'; +import { AnyNumber, SignatureOptions } from './types'; import Struct from './codec/Struct'; import Address from './Address'; @@ -15,13 +15,6 @@ import Signature from './Signature'; import SignaturePayload from './SignaturePayload'; import U8 from './U8'; -export type SignatureOptions = { - blockHash: AnyU8a, - era?: Uint8Array, - nonce: AnyNumber, - version?: RuntimeVersion -}; - export const IMMORTAL_ERA = new Uint8Array([0]); const BIT_SIGNED = 0b10000000; @@ -155,7 +148,7 @@ export default class ExtrinsicSignature extends Struct { era: era || IMMORTAL_ERA, blockHash }); - const signature = new Signature(signingPayload.sign(account, version)); + const signature = new Signature(signingPayload.sign(account, version as RuntimeVersion)); return this.injectSignature(signature, signer, signingPayload.nonce, signingPayload.era); } diff --git a/packages/types/src/Header.ts b/packages/types/src/Header.ts index 311908514d..096782ecd4 100644 --- a/packages/types/src/Header.ts +++ b/packages/types/src/Header.ts @@ -28,14 +28,14 @@ export type HeaderValue = { * A [[Block]] header */ export default class Header extends Struct { - constructor (value?: HeaderValue | Uint8Array) { + constructor (value?: HeaderValue | Uint8Array | null) { super({ parentHash: Hash, number: Compact.with(BlockNumber), stateRoot: Hash, extrinsicsRoot: Hash, digest: Digest - }, value); + }, value || {}); } /** @@ -91,11 +91,10 @@ export default class Header extends Struct { export class HeaderExtended extends Header { private _author?: AccountId; - constructor (header: Header, sessionValidators: Array = []) { + constructor (header: Header | null = null, sessionValidators: Array = []) { super(header); - const { digest: { logs } } = header; - const digestItem = logs.find(({ type }) => type === 'Seal'); + const digestItem = header && header.digest && header.digest.logs.find(({ type }) => type === 'Seal'); this._author = digestItem && sessionValidators.length ? sessionValidators[digestItem.asSeal.slot.toNumber() % sessionValidators.length] diff --git a/packages/types/src/RuntimeVersion.ts b/packages/types/src/RuntimeVersion.ts index 599626a196..169b4ee0d9 100644 --- a/packages/types/src/RuntimeVersion.ts +++ b/packages/types/src/RuntimeVersion.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AnyNumber, AnyU8a } from './types'; +import { AnyNumber, AnyU8a, RuntimeVersionInterface } from './types'; import Struct from './codec/Struct'; import Tuple from './codec/Tuple'; @@ -69,7 +69,7 @@ type RuntimeVersionValue = { * @description * A defintion of the runtime and the associated versions thereof */ -export default class RuntimeVersion extends Struct { +export default class RuntimeVersion extends Struct implements RuntimeVersionInterface { constructor (value?: RuntimeVersionValue | Uint8Array) { super({ specName: Text, diff --git a/packages/types/src/index.classes.ts b/packages/types/src/index.classes.ts index 70e55d2ddc..1cc105731d 100644 --- a/packages/types/src/index.classes.ts +++ b/packages/types/src/index.classes.ts @@ -24,7 +24,7 @@ export { default as Bytes } from './Bytes'; export { default as ChainProperties } from './ChainProperties'; export { default as CodeHash } from './CodeHash'; export { default as Data } from './Data'; -export { default as Digest } from './Digest'; +export { default as Digest, DigestItem } from './Digest'; export { default as Event } from './Event'; export { default as EventRecord } from './EventRecord'; export { default as Extrinsic } from './Extrinsic'; @@ -37,7 +37,7 @@ export { default as Gas } from './Gas'; export { default as H256 } from './H256'; export { default as H512 } from './H512'; export { default as Hash } from './Hash'; -export { default as Header } from './Header'; +export { default as Header, HeaderExtended } from './Header'; export { default as Health } from './Health'; export { default as i8 } from './I8'; export { default as i16 } from './I16'; diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index c3fc8bfa96..a3d8156945 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -71,3 +71,19 @@ export type TypeDef = { [index: string]: Codec }; export type RegistryTypes = { [name: string]: Constructor | string | { [name: string]: string } }; + +export interface RuntimeVersionInterface { + readonly apis: Array; + readonly authoringVersion: BN; + readonly implName: String; + readonly implVersion: BN; + readonly specName: String; + readonly specVersion: BN; +} + +export type SignatureOptions = { + blockHash: AnyU8a, + era?: Uint8Array, + nonce: AnyNumber, + version?: RuntimeVersionInterface +};