From 1a08e4be6af81f2c788f8736ad661eae9f461639 Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Fri, 11 May 2018 19:32:12 +0200 Subject: [PATCH] trezor-connect response flowtype --- src/js/actions/DiscoveryActions.js | 2 +- src/js/actions/SendFormActions.js | 30 +++--------- src/js/actions/TrezorConnectActions.js | 4 +- src/js/actions/Web3Actions.js | 4 +- src/js/flowtype/trezor-connect.js | 68 ++++++++++++++++++++++---- src/js/flowtype/web3.js | 2 +- 6 files changed, 71 insertions(+), 39 deletions(-) diff --git a/src/js/actions/DiscoveryActions.js b/src/js/actions/DiscoveryActions.js index d4b23a7f..7812a006 100644 --- a/src/js/actions/DiscoveryActions.js +++ b/src/js/actions/DiscoveryActions.js @@ -135,7 +135,7 @@ const begin = (device: TrezorDevice, network: string): AsyncAction => { }); // get xpub from TREZOR - const response: Object = await TrezorConnect.getPublicKey({ + const response = await TrezorConnect.getPublicKey({ device: { path: device.path, instance: device.instance, diff --git a/src/js/actions/SendFormActions.js b/src/js/actions/SendFormActions.js index eff65ed3..5f427981 100644 --- a/src/js/actions/SendFormActions.js +++ b/src/js/actions/SendFormActions.js @@ -434,7 +434,7 @@ export const onAmountChange = (amount: string): ThunkAction => { } } -export const onCurrencyChange = (currency: any): ThunkAction => { +export const onCurrencyChange = (currency: { value: string, label: string }): ThunkAction => { return (dispatch: Dispatch, getState: GetState): void => { const accountState: ?AccountState = getState().abstractAccount; if (!accountState) return; @@ -789,7 +789,7 @@ const estimateGasPrice = (): AsyncAction => { const data: string = '0x' + (currentState.data.length % 2 === 0 ? currentState.data : '0' + currentState.data); const gasLimit = await estimateGas(web3instance.web3, { - to: '0xdb6e09ddca62d0959dc4725697e66b8152222aee', + to: '0xdb6e09ddca62d0959dc4725697e66b8152222aee', // TODO: real adress data, value: web3.toHex(web3.toWei(currentState.amount, 'ether')), gasPrice: web3.toHex( EthereumjsUnits.convert(currentState.gasPrice, 'gwei', 'wei') ), @@ -861,20 +861,6 @@ export const onSend = (): AsyncAction => { v: '' } - - // const gasOptions = { - // to: txData.to, - // data: txData.data - // } - - // const gasPrice = await getGasPrice(web3); - - - - // txData.nonce = web3.toHex(nonce); - // txData.gasLimit = web3.toHex(gasLimit); - // txData.gasPrice = web3.toHex( EthereumjsUnits.convert(gasPrice, 'gwei', 'wei') ); - const selected: ?TrezorDevice = findSelectedDevice(getState().connect); if (!selected) return; @@ -914,17 +900,13 @@ export const onSend = (): AsyncAction => { txData.s = '0x' + signedTransaction.payload.s; txData.v = web3.toHex(signedTransaction.payload.v); - // const gasLimit2 = await estimateGas(web3, txData); - - const { config } = getState().localStorage; - if (!config) return; - const selectedCoin: ?Coin = config.coins.find(c => c.network === currentState.network); - if (!selectedCoin) return; + try { const tx = new EthereumjsTx(txData); const serializedTx = '0x' + tx.serialize().toString('hex'); const txid: string = await pushTx(web3, serializedTx); + const coin: Coin = accountState.coin; dispatch({ type: SEND.TX_COMPLETE, @@ -940,13 +922,13 @@ export const onSend = (): AsyncAction => { payload: { type: 'success', title: 'Transaction success', - message: `See transaction detail`, + message: `See transaction detail`, cancelable: true, actions: [] } }); - } catch(error) { + } catch (error) { dispatch({ type: NOTIFICATION.ADD, diff --git a/src/js/actions/TrezorConnectActions.js b/src/js/actions/TrezorConnectActions.js index 8b686da3..ea16274a 100644 --- a/src/js/actions/TrezorConnectActions.js +++ b/src/js/actions/TrezorConnectActions.js @@ -132,7 +132,7 @@ export const init = (): AsyncAction => { popup: false, webusb: true }); - } catch (error) { + } catch (error) { // dispatch({ // type: CONNECT.INITIALIZATION_ERROR, // error @@ -201,7 +201,7 @@ const sortDevices = (devices: Array): Array => { }); } -export const initConnectedDevice = (device: any): ThunkAction => { +export const initConnectedDevice = (device: TrezorDevice | Device): ThunkAction => { return (dispatch: Dispatch, getState: GetState): void => { const selected = findSelectedDevice(getState().connect); diff --git a/src/js/actions/Web3Actions.js b/src/js/actions/Web3Actions.js index 7f7bac2c..bcaa2c75 100644 --- a/src/js/actions/Web3Actions.js +++ b/src/js/actions/Web3Actions.js @@ -441,9 +441,9 @@ export const estimateGas = (web3: Web3, options: EstimateGasOptions): Promise => { +export const pushTx = (web3: Web3, tx: any): Promise => { return new Promise((resolve, reject) => { - web3.eth.sendRawTransaction(tx, (error, result) => { + web3.eth.sendRawTransaction(tx, (error: Error, result: string) => { if (error) { reject(error); } else { diff --git a/src/js/flowtype/trezor-connect.js b/src/js/flowtype/trezor-connect.js index 4d647cef..6c3cbce5 100644 --- a/src/js/flowtype/trezor-connect.js +++ b/src/js/flowtype/trezor-connect.js @@ -79,10 +79,10 @@ declare module 'trezor-connect' { }; - + declare type T_RESPONSE_EVENT = 'RESPONSE_EVENT'; declare export type ResponseMessage = { - event: string; - type: string; + event: T_RESPONSE_EVENT; + type: T_RESPONSE_EVENT; id: number; success: boolean; payload: Object; @@ -163,7 +163,57 @@ declare module 'trezor-connect' { declare type TransportEventListener = (type: T_TRANSPORT_EVENT, handler: (event: TransportMessage) => void) => void; - + declare type ResponseUnsuccessful = { + success: false; + payload: { + error: string; + } + } + + declare type ResponseEthereumSignTransaction = { + success: true; + payload: { + r: string; + s: string; + v: string; + } + } | ResponseUnsuccessful; + + declare type ResponseEthereumGetAddress = { + success: true; + payload: { + address: string, + path: Array + } + } | ResponseUnsuccessful; + + declare type ResponseGetDeviceState = { + success: true; + payload: { + state: string; + } + } | ResponseUnsuccessful; + + declare type ResponseGetFeatures = { + success: true; + payload: { + // TODO + } + } | ResponseUnsuccessful; + + declare type ResponseGetPublicKey = { + success: true; + payload: { + chainCode: string, + childNum: number, + depth: number, + fingerprint: number, + path: Array , + publicKey: string, + xpub: string, + xpubFormatted: string + } + } | ResponseUnsuccessful; declare module.exports: { @@ -174,13 +224,13 @@ declare module 'trezor-connect' { getVersion: () => any; renderWebUSBButton: (className?: string) => void; - getDeviceState: (options: Object) => Promise; - getFeatures: (options: Object) => Promise; - getPublicKey: (options: Object) => Promise; - ethereumGetAddress: (options: Object) => Promise; + getDeviceState: (options: Object) => Promise; + getFeatures: (options: Object) => Promise; + getPublicKey: (options: Object) => Promise; + ethereumGetAddress: (options: Object) => Promise; uiResponse: (options: Object) => Promise; - ethereumSignTransaction: (options: Object) => Promise; + ethereumSignTransaction: (options: Object) => Promise; // export const RESPONSE_EVENT: string = 'RESPONSE_EVENT'; diff --git a/src/js/flowtype/web3.js b/src/js/flowtype/web3.js index 41006e8e..110a8e92 100644 --- a/src/js/flowtype/web3.js +++ b/src/js/flowtype/web3.js @@ -58,7 +58,7 @@ declare module 'web3' { // sign: (payload: string, signer: EthereumAddressT) => Promise, contract: (abi: Array) => ContractFactory, estimateGas: (options: EstimateGasOptions, callback: (error: ?Error, gas: ?number) => void) => void, - sendRawTransaction: (tx: any, callback: (error: Error, result: any) => void) => void, + sendRawTransaction: (tx: any, callback: (error: Error, result: string) => void) => void, filter: (type: string) => Filter; // return intance with "watch" }