diff --git a/packages/react-api/src/Api.tsx b/packages/react-api/src/Api.tsx index 6001b11f5b..6568f1f07c 100644 --- a/packages/react-api/src/Api.tsx +++ b/packages/react-api/src/Api.tsx @@ -18,7 +18,7 @@ import keyring from '@polkadot/ui-keyring'; import { KeyringStore } from '@polkadot/ui-keyring/types'; import uiSettings from '@polkadot/ui-settings'; -import ApiSigner from '@polkadot/react-signer/ApiSigner'; +import ApiSigner from '@polkadot/react-signer/signers/ApiSigner'; import { formatBalance, isTestChain } from '@polkadot/util'; import { setSS58Format } from '@polkadot/util-crypto'; import addressDefaults from '@polkadot/util-crypto/address/defaults'; diff --git a/packages/react-signer/src/TxSigned.tsx b/packages/react-signer/src/TxSigned.tsx index d050313779..4b670d8ed6 100644 --- a/packages/react-signer/src/TxSigned.tsx +++ b/packages/react-signer/src/TxSigned.tsx @@ -2,13 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { SignerResult, SubmittableExtrinsic } from '@polkadot/api/types'; +import { SubmittableExtrinsic } from '@polkadot/api/types'; import { SignerOptions } from '@polkadot/api/submittable/types'; import { KeyringPair } from '@polkadot/keyring/types'; import { QueueTx, QueueTxMessageSetStatus } from '@polkadot/react-components/Status/types'; import { Multisig, Timepoint } from '@polkadot/types/interfaces'; -import { SignerPayloadJSON } from '@polkadot/types/types'; -import { AddressProxy } from './types'; +import { AddressProxy, QrState } from './types'; import React, { useCallback, useContext, useEffect, useState } from 'react'; import styled from 'styled-components'; @@ -20,9 +19,8 @@ import { useApi, useToggle } from '@polkadot/react-hooks'; import { Option } from '@polkadot/types'; import keyring from '@polkadot/ui-keyring'; import { BN_ZERO, assert } from '@polkadot/util'; -import { blake2AsU8a } from '@polkadot/util-crypto'; -import ledgerSigner from './LedgerSigner'; +import { LedgerSigner, QrSigner } from './signers'; import { useTranslation } from './translate'; import Address from './Address'; import Qr from './Qr'; @@ -37,15 +35,6 @@ interface Props { requestAddress: string; } -interface QrState { - isQrHashed: boolean; - isQrVisible: boolean; - qrAddress: string; - qrPayload: Uint8Array; - qrResolve?: (result: SignerResult) => void; - qrReject?: (error: Error) => void; -} - const NOOP = () => undefined; let qrId = 0; @@ -78,7 +67,11 @@ async function signAndSend (queueSetTxStatus: QueueTxMessageSetStatus, currentIt currentItem.txStartCb && currentItem.txStartCb(); try { - const unsubscribe = await tx.signAndSend(pairOrAddress, options, handleTxResults('signAndSend', queueSetTxStatus, currentItem, (): void => { + await tx.signAsync(pairOrAddress, options); + + queueSetTxStatus(currentItem.id, 'sending'); + + const unsubscribe = await tx.send(handleTxResults('signAndSend', queueSetTxStatus, currentItem, (): void => { unsubscribe(); })); } catch (error) { @@ -93,7 +86,9 @@ async function signAsync (queueSetTxStatus: QueueTxMessageSetStatus, { id, txFai txStartCb(); try { - return (await tx.signAsync(pairOrAddress, options)).toJSON(); + await tx.signAsync(pairOrAddress, options); + + return tx.toJSON(); } catch (error) { queueSetTxStatus(id, 'error', undefined, error); @@ -103,27 +98,6 @@ async function signAsync (queueSetTxStatus: QueueTxMessageSetStatus, { id, txFai return null; } -function signQrPayload (setQrState: (state: QrState) => void): (payload: SignerPayloadJSON) => Promise { - return (payload: SignerPayloadJSON): Promise => - new Promise((resolve, reject): void => { - // limit size of the transaction - const isQrHashed = (payload.method.length > 5000); - const wrapper = registry.createType('ExtrinsicPayload', payload, { version: payload.version }); - const qrPayload = isQrHashed - ? blake2AsU8a(wrapper.toU8a(true)) - : wrapper.toU8a(); - - setQrState({ - isQrHashed, - isQrVisible: true, - qrAddress: payload.address, - qrPayload, - qrReject: reject, - qrResolve: resolve - }); - }); -} - async function wrapTx (api: ApiPromise, currentItem: QueueTx, { isMultiCall, multiRoot, proxyRoot, signAddress }: AddressProxy): Promise> { let tx = currentItem.extrinsic as SubmittableExtrinsic<'promise'>; @@ -159,9 +133,9 @@ async function extractParams (address: string, options: Partial, const { meta: { isExternal, isHardware, isInjected, source } } = pair; if (isHardware) { - return ['signing', address, { ...options, signer: ledgerSigner }]; + return ['signing', address, { ...options, signer: new LedgerSigner() }]; } else if (isExternal) { - return ['qr', address, { ...options, signer: { signPayload: signQrPayload(setQrState) } }]; + return ['qr', address, { ...options, signer: new QrSigner(setQrState) }]; } else if (isInjected) { const injected = await web3FromSource(source as string); diff --git a/packages/react-signer/src/signers/AccountSigner.ts b/packages/react-signer/src/signers/AccountSigner.ts new file mode 100644 index 0000000000..030bf36290 --- /dev/null +++ b/packages/react-signer/src/signers/AccountSigner.ts @@ -0,0 +1,27 @@ +// 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 { KeyringPair } from '@polkadot/keyring/types'; +import { SignerPayloadJSON } from '@polkadot/types/types'; + +import { registry } from '@polkadot/react-api'; + +let id = 0; + +export default class AccountSigner implements Signer { + readonly #keyringPair: KeyringPair; + + constructor (keyringPair: KeyringPair) { + this.#keyringPair = keyringPair; + } + + public async signPayload (payload: SignerPayloadJSON): Promise { + return new Promise((resolve): void => { + const signed = registry.createType('ExtrinsicPayload', payload, { version: payload.version }).sign(this.#keyringPair); + + resolve({ id: ++id, ...signed }); + }); + } +} diff --git a/packages/react-signer/src/ApiSigner.ts b/packages/react-signer/src/signers/ApiSigner.ts similarity index 100% rename from packages/react-signer/src/ApiSigner.ts rename to packages/react-signer/src/signers/ApiSigner.ts diff --git a/packages/react-signer/src/LedgerSigner.ts b/packages/react-signer/src/signers/LedgerSigner.ts similarity index 69% rename from packages/react-signer/src/LedgerSigner.ts rename to packages/react-signer/src/signers/LedgerSigner.ts index ef04d5204f..605ee84b10 100644 --- a/packages/react-signer/src/LedgerSigner.ts +++ b/packages/react-signer/src/signers/LedgerSigner.ts @@ -6,19 +6,14 @@ import { Signer, SignerResult } from '@polkadot/api/types'; import { SignerPayloadJSON } from '@polkadot/types/types'; import { getLedger, registry } from '@polkadot/react-api'; -import { createType } from '@polkadot/types'; let id = 0; -export class LedgerSigner implements Signer { +export default class LedgerSigner implements Signer { public async signPayload (payload: SignerPayloadJSON): Promise { - const raw = createType(registry, 'ExtrinsicPayload', payload, { version: payload.version }); + const raw = registry.createType('ExtrinsicPayload', payload, { version: payload.version }); const { signature } = await getLedger().sign(raw.toU8a(true)); return { id: ++id, signature }; } } - -const ledgerSigner = new LedgerSigner(); - -export default ledgerSigner; diff --git a/packages/react-signer/src/signers/QrSigner.ts b/packages/react-signer/src/signers/QrSigner.ts new file mode 100644 index 0000000000..ca9c2541ab --- /dev/null +++ b/packages/react-signer/src/signers/QrSigner.ts @@ -0,0 +1,38 @@ +// 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 { SignerPayloadJSON } from '@polkadot/types/types'; +import { QrState } from '../types'; + +import { registry } from '@polkadot/react-api'; +import { blake2AsU8a } from '@polkadot/util-crypto'; + +export default class QrSigner implements Signer { + readonly #setState: (state: QrState) => void; + + constructor (setState: (state: QrState) => void) { + this.#setState = setState; + } + + public async signPayload (payload: SignerPayloadJSON): Promise { + return new Promise((resolve, reject): void => { + // limit size of the transaction + const isQrHashed = (payload.method.length > 5000); + const wrapper = registry.createType('ExtrinsicPayload', payload, { version: payload.version }); + const qrPayload = isQrHashed + ? blake2AsU8a(wrapper.toU8a(true)) + : wrapper.toU8a(); + + this.#setState({ + isQrHashed, + isQrVisible: true, + qrAddress: payload.address, + qrPayload, + qrReject: reject, + qrResolve: resolve + }); + }); + } +} diff --git a/packages/react-signer/src/signers/index.ts b/packages/react-signer/src/signers/index.ts new file mode 100644 index 0000000000..e1e584a37b --- /dev/null +++ b/packages/react-signer/src/signers/index.ts @@ -0,0 +1,8 @@ +// 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. + +export { default as AccountSigner } from './AccountSigner'; +export { default as ApiSigner } from './ApiSigner'; +export { default as LedgerSigner } from './LedgerSigner'; +export { default as QrSigner } from './QrSigner'; diff --git a/packages/react-signer/src/types.ts b/packages/react-signer/src/types.ts index 09c63650d5..e391288bfd 100644 --- a/packages/react-signer/src/types.ts +++ b/packages/react-signer/src/types.ts @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { SignerResult } from '@polkadot/api/types'; + export interface AddressFlags { hardwareType?: string; isHardware: boolean; @@ -21,6 +23,15 @@ export interface AddressProxy { signPassword: string; } +export interface QrState { + isQrHashed: boolean; + isQrVisible: boolean; + qrAddress: string; + qrPayload: Uint8Array; + qrResolve?: (result: SignerResult) => void; + qrReject?: (error: Error) => void; +} + export interface Signed { data: Uint8Array; message: Uint8Array;