From 5278138cc3ac0a2eb3010fdc08123bccc68a0e32 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 7 Jan 2020 09:54:04 +0100 Subject: [PATCH] Make submittable signAsync Observable-aware (#1698) * Make submittable signAsync Observable-aware * rxjs test for signAsync * Cleanup tests, pass signer in .create * Simplify Observable creation --- packages/api/src/promise/Api.spec.ts | 17 ++++++---- packages/api/src/rx/Api.spec.ts | 36 +++++++++++++++++++++ packages/api/src/submittable/createClass.ts | 27 +++++++++------- packages/api/src/submittable/types.ts | 7 +++- 4 files changed, 67 insertions(+), 20 deletions(-) diff --git a/packages/api/src/promise/Api.spec.ts b/packages/api/src/promise/Api.spec.ts index 38cc21c7d2..ed70681f5e 100644 --- a/packages/api/src/promise/Api.spec.ts +++ b/packages/api/src/promise/Api.spec.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 { ApiOptions } from '../types'; + import testingPairs from '@polkadot/keyring/testingPairs'; import Mock from '@polkadot/rpc-provider/mock/index'; import { createType, TypeRegistry } from '@polkadot/types'; -import { ApiPromise } from '..'; import { SingleAccountSigner } from '../../test/util'; -import { ApiOptions } from '../types'; +import ApiPromise from './Api'; describe('ApiPromise', (): void => { const registry = new TypeRegistry(); @@ -81,13 +82,15 @@ describe('ApiPromise', (): void => { describe('decorator.signAsync', (): void => { it('signs a transfer using an external signer', async (): Promise => { - const api = await ApiPromise.create({ provider, registry }); - api.setSigner(new SingleAccountSigner(registry, keyring.alice_session)); + const signer = new SingleAccountSigner(registry, keyring.alice_session); + const api = await ApiPromise.create({ provider, registry, signer }); const transfer = api.tx.balances.transfer(keyring.eve.address, 12345); - const dummySignature = '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'; - expect(transfer.signature.toHex()).toEqual(dummySignature); + await transfer.signAsync(keyring.alice_session, {}); - expect(transfer.signature.toHex()).not.toEqual(dummySignature); + + expect(transfer.signature.toHex()).toEqual( + '0x97f3cfe5088fcd575313e983f45d02b0f630e7b94ff9a3ac50e20cd096a8f554fda73d42ead891b5a1d3ce5607d83f20b0c6570b555e949cfb5763d0abcd590b' + ); }); }); }); diff --git a/packages/api/src/rx/Api.spec.ts b/packages/api/src/rx/Api.spec.ts index 284498c6aa..11e560e3ff 100644 --- a/packages/api/src/rx/Api.spec.ts +++ b/packages/api/src/rx/Api.spec.ts @@ -2,11 +2,28 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { SubmittableExtrinsic } from '../submittable/types'; + +import { Observable } from 'rxjs'; +import { map, switchMap } from 'rxjs/operators'; +import testingPairs from '@polkadot/keyring/testingPairs'; +import Mock from '@polkadot/rpc-provider/mock/index'; +import { TypeRegistry } from '@polkadot/types'; import { isObservable } from '@polkadot/util'; +import { SingleAccountSigner } from '../../test/util'; import ApiRx from './Api'; describe('ApiRx', (): void => { + const registry = new TypeRegistry(); + const keyring = testingPairs({ type: 'ed25519' }); + let provider: Mock; + + beforeEach((): void => { + jest.setTimeout(3000000); + provider = new Mock(registry); + }); + it('has isConnected', (): void => { const api = new ApiRx(); @@ -14,4 +31,23 @@ describe('ApiRx', (): void => { isObservable(api.isConnected) ).toBe(true); }); + + describe('decorator.signAsync', (): void => { + it('signs a transfer using an external signer', (): void => { + const signer = new SingleAccountSigner(registry, keyring.alice_session); + + ApiRx.create({ provider, registry, signer }).pipe( + switchMap((api: ApiRx): Observable> => + api.tx.balances + .transfer(keyring.eve.address, 12345) + .signAsync(keyring.alice_session, {}) + ), + map((tx: SubmittableExtrinsic<'rxjs'>): void => { + expect(tx.signature.toHex()).toEqual( + '0x97f3cfe5088fcd575313e983f45d02b0f630e7b94ff9a3ac50e20cd096a8f554fda73d42ead891b5a1d3ce5607d83f20b0c6570b555e949cfb5763d0abcd590b' + ); + }) + ); + }); + }); }); diff --git a/packages/api/src/submittable/createClass.ts b/packages/api/src/submittable/createClass.ts index 8318d7f474..0b384c5c4f 100644 --- a/packages/api/src/submittable/createClass.ts +++ b/packages/api/src/submittable/createClass.ts @@ -6,10 +6,10 @@ import { AccountId, Address, Call, ExtrinsicEra, ExtrinsicStatus, Hash, Header, Index } from '@polkadot/types/interfaces'; import { Callback, Codec, Constructor, IKeyringPair, Registry, SignatureOptions } from '@polkadot/types/types'; import { ApiInterfaceRx, ApiTypes, SignerResult } from '../types'; -import { SignerOptions, SubmittableExtrinsic, SubmittableResultImpl, SubmittableResultResult, SubmittableResultSubscription } from './types'; +import { SignerOptions, SubmittableExtrinsic, SubmittableResultImpl, SubmittableResultResult, SubmittableResultSubscription, SubmittableThis } from './types'; -import { Observable, combineLatest, of } from 'rxjs'; -import { first, map, mergeMap, switchMap, tap } from 'rxjs/operators'; +import { Observable, combineLatest, from, of } from 'rxjs'; +import { first, map, mapTo, mergeMap, switchMap, tap } from 'rxjs/operators'; import { createType, ClassOf } from '@polkadot/types'; import { isBn, isFunction, isNumber, isUndefined } from '@polkadot/util'; @@ -59,15 +59,18 @@ export default function createClass ({ api, apiType, // signs a transaction, returning `this` to allow chaining. E.g.: `sign(...).send()` // // also supports signing through external signers - public async signAsync (account: IKeyringPair, optionsOrNonce: Partial): Promise { - if (this._api.signer) { - const options = this._makeSignOptions(this._optionsOrNonce(optionsOrNonce), {}); - await this._signViaSigner(account.address, options, null); - } else { - this.sign(account, optionsOrNonce); - } + public signAsync (account: IKeyringPair, optionsOrNonce: Partial): SubmittableThis { + return this._decorateMethod( + (): Observable => { + const optionsWithNonce = this._optionsOrNonce(optionsOrNonce); - return this; + return from( + this._api.signer + ? this._signViaSigner(account.address, this._makeSignOptions(optionsWithNonce, {}), null) + : Promise.resolve(this.sign(account, optionsWithNonce)) + ).pipe(mapTo(this)); + } + )(); } // signAndSend with an immediate Hash result @@ -105,7 +108,7 @@ export default function createClass ({ api, apiType, : this._sendObservable(updateId); }) ) as Observable) // FIXME This is wrong, SubmittableResult is _not_ a codec - )(statusCb) as SubmittableResultResult | SubmittableResultSubscription; + )(statusCb); } // send with an immediate Hash result diff --git a/packages/api/src/submittable/types.ts b/packages/api/src/submittable/types.ts index 4fd72e6c30..4da5fff90d 100644 --- a/packages/api/src/submittable/types.ts +++ b/packages/api/src/submittable/types.ts @@ -34,6 +34,11 @@ export type SubmittableResultSubscription = ? Observable : Promise<() => void>; +export type SubmittableThis = + ApiType extends 'rxjs' + ? Observable + : Promise; + export interface SignerOptions { blockHash: AnyU8a; era?: IExtrinsicEra | number; @@ -48,7 +53,7 @@ export interface SubmittableExtrinsic extends IExtrins sign(account: IKeyringPair, _options: Partial): this; - signAsync(account: IKeyringPair, _options: Partial): Promise; + signAsync(account: IKeyringPair, _options: Partial): SubmittableThis; signAndSend(account: IKeyringPair | string | AccountId | Address, options?: Partial): SubmittableResultResult;