Allow passing of signer via sign options (#1734)

* Allow passing of signer via sign options

* Pass signer via api.sign
This commit is contained in:
Jaco Greeff
2020-01-21 14:45:53 +01:00
committed by GitHub
parent be3f9c8fe8
commit 20ed3bb5fe
4 changed files with 29 additions and 21 deletions
+12 -7
View File
@@ -18,6 +18,10 @@ interface KeyringSigner {
sign (message: Uint8Array): Uint8Array;
}
interface SignerRawOptions {
signer?: Signer;
}
let pkgJson: { name: string; version: string };
try {
@@ -262,20 +266,21 @@ export default abstract class ApiBase<ApiType extends ApiTypes> extends Init<Api
/**
* @description Signs a raw signer payload, string or Uint8Array
*/
public async sign (signer: KeyringSigner | string, data: SignerPayloadRawBase): Promise<string> {
// NOTE Do we really want to do this? Or turn it into an observable for rxjs?
if (isString(signer)) {
assert(this._rx.signer?.signRaw, 'No signer exists with a signRaw interface');
public async sign (address: KeyringSigner | string, data: SignerPayloadRawBase, { signer }: SignerRawOptions = {}): Promise<string> {
if (isString(address)) {
const _signer = signer || this._rx.signer;
assert(_signer?.signRaw, 'No signer exists with a signRaw interface');
return (
await this._rx.signer.signRaw({
await _signer.signRaw({
type: 'bytes',
...data,
address: signer
address
})
).signature;
}
return u8aToHex(signer.sign(u8aToU8a(data.data)));
return u8aToHex(address.sign(u8aToU8a(data.data)));
}
}
+13 -13
View File
@@ -11,7 +11,7 @@ import { SignerOptions, SubmittableExtrinsic, SubmittablePaymentResult, Submitta
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';
import { assert, isBn, isFunction, isNumber, isUndefined } from '@polkadot/util';
import { filterEvents, isKeyringPair } from '../util';
import ApiBase from '../base';
@@ -82,15 +82,15 @@ 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 signAsync (account: IKeyringPair, optionsOrNonce: Partial<SignerOptions>): SubmittableThis<ApiType, this> {
public signAsync (account: IKeyringPair | string | AccountId | Address, optionsOrNonce: Partial<SignerOptions>): SubmittableThis<ApiType, this> {
return this._decorateMethod(
(): Observable<this> => {
const optionsWithNonce = this._optionsOrNonce(optionsOrNonce);
return from(
this._api.signer
? this._signViaSigner(account.address, this._makeSignOptions(optionsWithNonce, {}), null)
: Promise.resolve(this.sign(account, optionsWithNonce))
isKeyringPair(account)
? Promise.resolve(this.sign(account, optionsWithNonce))
: this._signViaSigner(account, this._makeSignOptions(optionsWithNonce, {}), null)
).pipe(mapTo(this));
}
)();
@@ -163,10 +163,10 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
return [options, statusCb];
}
private async _signViaSigner (address: string, options: SignatureOptions, header: Header | null): Promise<number> {
if (!this._api.signer) {
throw new Error('no signer attached');
}
private async _signViaSigner (address: | string | AccountId | Address, options: SignatureOptions, header: Header | null): Promise<number> {
const signer = options.signer || this._api.signer;
assert(signer, 'No signer specified, either via api.setSigner or via sign options');
const payload = createType(this.registry, 'SignerPayload', {
...options,
@@ -176,10 +176,10 @@ export default function createClass <ApiType extends ApiTypes> ({ api, apiType,
});
let result: SignerResult;
if (this._api.signer.signPayload) {
result = await this._api.signer.signPayload(payload.toPayload());
} else if (this._api.signer.signRaw) {
result = await this._api.signer.signRaw(payload.toRaw());
if (signer.signPayload) {
result = await signer.signPayload(payload.toPayload());
} else if (signer.signRaw) {
result = await signer.signRaw(payload.toRaw());
} else {
throw new Error('Invalid signer interface, it should implement either signPayload or signRaw (or both)');
}
+2 -1
View File
@@ -4,7 +4,7 @@
import { AccountId, Address, ExtrinsicStatus, EventRecord, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces';
import { AnyNumber, AnyU8a, Callback, IExtrinsic, IExtrinsicEra, IKeyringPair, SignatureOptions } from '@polkadot/types/types';
import { ApiTypes } from '../types';
import { ApiTypes, Signer } from '../types';
import { Observable } from 'rxjs';
@@ -48,6 +48,7 @@ export interface SignerOptions {
blockHash: AnyU8a;
era?: IExtrinsicEra | number;
nonce: AnyNumber;
signer?: Signer;
tip?: AnyNumber;
}