Split sign/send for UI updates (#2971)
* Add ProxySigner (update wrapper) * setTimeout(1) * Split sign & send parts
This commit is contained in:
parent
2ab0de39a3
commit
0973bf2aa5
@ -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';
|
||||
|
||||
@ -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<SignerResult> {
|
||||
return (payload: SignerPayloadJSON): Promise<SignerResult> =>
|
||||
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<SubmittableExtrinsic<'promise'>> {
|
||||
let tx = currentItem.extrinsic as SubmittableExtrinsic<'promise'>;
|
||||
|
||||
@ -159,9 +133,9 @@ async function extractParams (address: string, options: Partial<SignerOptions>,
|
||||
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);
|
||||
|
||||
|
||||
27
packages/react-signer/src/signers/AccountSigner.ts
Normal file
27
packages/react-signer/src/signers/AccountSigner.ts
Normal file
@ -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<SignerResult> {
|
||||
return new Promise((resolve): void => {
|
||||
const signed = registry.createType('ExtrinsicPayload', payload, { version: payload.version }).sign(this.#keyringPair);
|
||||
|
||||
resolve({ id: ++id, ...signed });
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -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<SignerResult> {
|
||||
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;
|
||||
38
packages/react-signer/src/signers/QrSigner.ts
Normal file
38
packages/react-signer/src/signers/QrSigner.ts
Normal file
@ -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<SignerResult> {
|
||||
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
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
8
packages/react-signer/src/signers/index.ts
Normal file
8
packages/react-signer/src/signers/index.ts
Normal file
@ -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';
|
||||
@ -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;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user