Make submittable signAsync Observable-aware (#1698)

* Make submittable signAsync Observable-aware

* rxjs test for signAsync

* Cleanup tests, pass signer in .create

* Simplify Observable creation
This commit is contained in:
Jaco Greeff
2020-01-07 09:54:04 +01:00
committed by GitHub
parent 98249a282e
commit 5278138cc3
4 changed files with 67 additions and 20 deletions
+10 -7
View File
@@ -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<void> => {
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'
);
});
});
});
+36
View File
@@ -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<SubmittableExtrinsic<'rxjs'>> =>
api.tx.balances
.transfer(keyring.eve.address, 12345)
.signAsync(keyring.alice_session, {})
),
map((tx: SubmittableExtrinsic<'rxjs'>): void => {
expect(tx.signature.toHex()).toEqual(
'0x97f3cfe5088fcd575313e983f45d02b0f630e7b94ff9a3ac50e20cd096a8f554fda73d42ead891b5a1d3ce5607d83f20b0c6570b555e949cfb5763d0abcd590b'
);
})
);
});
});
});
+15 -12
View File
@@ -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 <ApiType extends ApiTypes> ({ 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<SignerOptions>): Promise<this> {
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<SignerOptions>): SubmittableThis<ApiType, this> {
return this._decorateMethod(
(): Observable<this> => {
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 <ApiType extends ApiTypes> ({ api, apiType,
: this._sendObservable(updateId);
})
) as Observable<Codec>) // FIXME This is wrong, SubmittableResult is _not_ a codec
)(statusCb) as SubmittableResultResult<ApiType> | SubmittableResultSubscription<ApiType>;
)(statusCb);
}
// send with an immediate Hash result
+6 -1
View File
@@ -34,6 +34,11 @@ export type SubmittableResultSubscription<ApiType extends ApiTypes> =
? Observable<SubmittableResultImpl>
: Promise<() => void>;
export type SubmittableThis<ApiType extends ApiTypes, THIS> =
ApiType extends 'rxjs'
? Observable<THIS>
: Promise<THIS>;
export interface SignerOptions {
blockHash: AnyU8a;
era?: IExtrinsicEra | number;
@@ -48,7 +53,7 @@ export interface SubmittableExtrinsic<ApiType extends ApiTypes> extends IExtrins
sign(account: IKeyringPair, _options: Partial<SignatureOptions>): this;
signAsync(account: IKeyringPair, _options: Partial<SignatureOptions>): Promise<this>;
signAsync(account: IKeyringPair, _options: Partial<SignatureOptions>): SubmittableThis<ApiType, this>;
signAndSend(account: IKeyringPair | string | AccountId | Address, options?: Partial<SignerOptions>): SubmittableResultResult<ApiType>;