ghost-portal/packages/react-signer/src/ApiSigner.ts
Jaco Greeff e97868348b
React.memo-ize more func. components (#2389)
* React.memo-ize more func. components

* Rework status display determination

* isApiReady &&

* ApiSigner private class interface

* readonly #

* Remove isPrimary on council vote
2020-03-15 12:11:59 +01:00

44 lines
1.6 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-signer 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 { Signer, SignerResult } from '@polkadot/api/types';
import { SubmittableResult } from '@polkadot/api';
import { QueueTxPayloadAdd, QueueTxMessageSetStatus, QueueTxStatus } from '@polkadot/react-components/Status/types';
import { Hash } from '@polkadot/types/interfaces';
import { SignerPayloadJSON } from '@polkadot/types/types';
import { registry } from '@polkadot/react-api';
import { ClassOf } from '@polkadot/types';
export default class ApiSigner implements Signer {
readonly #queuePayload: QueueTxPayloadAdd;
readonly #queueSetTxStatus: QueueTxMessageSetStatus;
constructor (queuePayload: QueueTxPayloadAdd, queueSetTxStatus: QueueTxMessageSetStatus) {
this.#queuePayload = queuePayload;
this.#queueSetTxStatus = queueSetTxStatus;
}
public async signPayload (payload: SignerPayloadJSON): Promise<SignerResult> {
return new Promise((resolve, reject): void => {
this.#queuePayload(payload, (id: number, result: SignerResult | null): void => {
if (result) {
resolve(result);
} else {
reject(new Error('Unable to sign'));
}
});
});
}
public update (id: number, result: Hash | SubmittableResult): void {
if (result instanceof ClassOf(registry, 'Hash')) {
this.#queueSetTxStatus(id, 'sent', result.toHex());
} else {
this.#queueSetTxStatus(id, result.status.type.toLowerCase() as QueueTxStatus, status);
}
}
}