ghost-portal/packages/react-signer/src/signers/ApiSigner.ts
2020-09-17 16:46:46 +02:00

43 lines
1.5 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-signer authors & contributors
// SPDX-License-Identifier: Apache-2.0
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);
}
}
}