From 88b0c5cd3498adbcec0b760f15b6e7941b5b4901 Mon Sep 17 00:00:00 2001 From: Szymon Lesisz Date: Tue, 27 Mar 2018 17:12:01 +0200 Subject: [PATCH] refactoring field incomming from TrezorConnect responses --- src/js/actions/ModalActions.js | 6 +- src/js/actions/ReceiveActions.js | 4 +- src/js/actions/SendFormActions.js | 16 +++--- src/js/actions/TrezorConnectActions.js | 74 ++++++++++++++----------- src/js/reducers/ModalReducer.js | 4 +- src/js/reducers/TrezorConnectReducer.js | 21 ++++++- 6 files changed, 76 insertions(+), 49 deletions(-) diff --git a/src/js/actions/ModalActions.js b/src/js/actions/ModalActions.js index cd5b3446..d31559d9 100644 --- a/src/js/actions/ModalActions.js +++ b/src/js/actions/ModalActions.js @@ -8,7 +8,7 @@ import * as CONNECT from './constants/TrezorConnect'; export function onPinSubmit(value: string): any { - TrezorConnect.uiMessage({ type: UI.RECEIVE_PIN, data: value }); + TrezorConnect.uiResponse({ type: UI.RECEIVE_PIN, payload: value }); return { type: ACTIONS.CLOSE_MODAL } @@ -16,9 +16,9 @@ export function onPinSubmit(value: string): any { export function onPassphraseSubmit(passphrase: string): any { return async (dispatch, getState): Promise => { - const resp = await TrezorConnect.uiMessage({ + const resp = await TrezorConnect.uiResponse({ type: UI.RECEIVE_PASSPHRASE, - data: { + payload: { value: passphrase, save: true } diff --git a/src/js/actions/ReceiveActions.js b/src/js/actions/ReceiveActions.js index 33a7043e..b64bfadd 100644 --- a/src/js/actions/ReceiveActions.js +++ b/src/js/actions/ReceiveActions.js @@ -80,7 +80,7 @@ export const showAddress = (address_n: string): any => { instance: selected.instance, state: selected.checksum }, - address_n + path: address_n, }); if (response && response.success) { @@ -94,7 +94,7 @@ export const showAddress = (address_n: string): any => { payload: { type: 'error', title: 'Veryfying address error', - message: response.data.error, + message: response.payload.error, cancelable: true, actions: [ { diff --git a/src/js/actions/SendFormActions.js b/src/js/actions/SendFormActions.js index 7b9eecf3..f09a80ac 100644 --- a/src/js/actions/SendFormActions.js +++ b/src/js/actions/SendFormActions.js @@ -686,14 +686,14 @@ export const onSend = (): any => { state: selected.checksum }, //path: "m/44'/60'/0'/0/0", - address_n: txData.address_n, + path: txData.address_n, nonce: strip(txData.nonce), - gas_price: strip(txData.gasPrice), - gas_limit: strip(txData.gasLimit), + gasPrice: strip(txData.gasPrice), + gasLimit: strip(txData.gasLimit), to: strip(txData.to), value: strip(txData.value), data: strip(txData.data), - chain_id: txData.chainId + chainId: txData.chainId }); if (!signedTransaction || !signedTransaction.success) { @@ -703,7 +703,7 @@ export const onSend = (): any => { payload: { type: 'error', title: 'Transaction error', - message: signedTransaction.data.error, + message: signedTransaction.payload.error, cancelable: true, actions: [ ] } @@ -711,9 +711,9 @@ export const onSend = (): any => { return; } - txData.r = '0x' + signedTransaction.data.r; - txData.s = '0x' + signedTransaction.data.s; - txData.v = web3.toHex(signedTransaction.data.v); + txData.r = '0x' + signedTransaction.payload.r; + txData.s = '0x' + signedTransaction.payload.s; + txData.v = web3.toHex(signedTransaction.payload.v); // const gasLimit2 = await estimateGas(web3, txData); // console.log("---->GASSS", txData, gasLimit2.toString() ); diff --git a/src/js/actions/TrezorConnectActions.js b/src/js/actions/TrezorConnectActions.js index a725f742..659e6c2e 100644 --- a/src/js/actions/TrezorConnectActions.js +++ b/src/js/actions/TrezorConnectActions.js @@ -1,7 +1,7 @@ /* @flow */ 'use strict'; -import TrezorConnect, { UI, DEVICE, DEVICE_EVENT, UI_EVENT } from 'trezor-connect'; +import TrezorConnect, { UI, DEVICE, DEVICE_EVENT, UI_EVENT, TRANSPORT_EVENT } from 'trezor-connect'; import * as ACTIONS from './index'; import * as ADDRESS from './constants/Address'; import * as TOKEN from './constants/Token'; @@ -26,37 +26,46 @@ import { resolveAfter } from '../utils/promiseUtils'; import { getAccounts } from '../utils/reducerUtils'; import { findSelectedDevice, isSavedDevice } from '../reducers/TrezorConnectReducer'; - - export const init = (): any => { return async (dispatch, getState): Promise => { // set listeners TrezorConnect.on(DEVICE_EVENT, (event: DeviceMessage): void => { dispatch({ type: event.type, - device: event.data + device: event.payload }); }); const version: Object = TrezorConnect.getVersion(); - if (version.type === 'library') { - // handle UI events only if TrezorConnect isn't using popup - TrezorConnect.on(UI_EVENT, (type: string, data: any): void => { - // post event to reducers - dispatch({ - type, - data - }); + TrezorConnect.on(UI_EVENT, (event: UiMessage): void => { + // post event to reducers + dispatch({ + type: event.type, + payload: event.payload }); - } + }); + + TrezorConnect.on(TRANSPORT_EVENT, (event: UiMessage): void => { + // post event to reducers + dispatch({ + type: event.type, + payload: event.payload + }); + }); try { await TrezorConnect.init({ - transport_reconnect: true, - coins_src: './data/coins.json', - firmware_releases_src: './data/releases-1.json', - transport_config_src: './data/config_signed.bin', - latest_bridge_src: './data/latest.txt' + // transportReconnect: true, + coinsSrc: './data/coins.json', + firmwareReleasesSrc: './data/releases-1.json', + transportConfigSrc: './data/config_signed.bin', + transportReconnect: false, + latestBridgeSrc: './data/latest.txt', + connectSrc: 'https://localhost:8088/', + // connectSrc: 'https://sisyfos.trezor.io/', + debug: true, + popup: false, + // webusb: false }); setTimeout(() => { @@ -218,7 +227,7 @@ export const getSelectedDeviceState = (): any => { dispatch({ type: CONNECT.AUTH_DEVICE, device: selected, - checksum: response.data.xpub + checksum: response.payload.xpub }); } else { dispatch({ @@ -226,7 +235,7 @@ export const getSelectedDeviceState = (): any => { payload: { type: 'error', title: 'Authentification error', - message: response.data.error, + message: response.payload.error, cancelable: true, actions: [ { @@ -331,7 +340,7 @@ export function acquire(): any { payload: { type: 'error', title: 'Acquire device error', - message: response.data.error, + message: response.payload.error, cancelable: true, actions: [ { @@ -393,13 +402,12 @@ export const beginDiscoveryProcess = (device: any, coin: string): any => { // TODO: validate device checksum // const checksum = await __acquire(device.path, device.instance); // if (checksum && checksum.success) { - // if (checksum.data.xpub !== device.checksum) { + // if (checksum.payload.xpub !== device.checksum) { // console.error("Incorrect checksum!"); // return; // } // } - // acquire and hold session // get xpub from TREZOR const response = await TrezorConnect.getPublicKey({ device: { @@ -409,7 +417,7 @@ export const beginDiscoveryProcess = (device: any, coin: string): any => { }, path: coinToDiscover.bip44, confirmation: false, - keepSession: true + keepSession: true // acquire and hold session }); if (!response.success) { @@ -420,7 +428,7 @@ export const beginDiscoveryProcess = (device: any, coin: string): any => { payload: { type: 'error', title: 'Discovery error', - message: response.data.error, + message: response.payload.error, cancelable: true, actions: [ { @@ -438,17 +446,17 @@ export const beginDiscoveryProcess = (device: any, coin: string): any => { // TODO: check for interruption // TODO: handle response error - const basePath: Array = response.data.path; + const basePath: Array = response.payload.path; const hdKey = new HDKey(); - hdKey.publicKey = new Buffer(response.data.publicKey, 'hex'); - hdKey.chainCode = new Buffer(response.data.chainCode, 'hex'); + hdKey.publicKey = new Buffer(response.payload.publicKey, 'hex'); + hdKey.chainCode = new Buffer(response.payload.chainCode, 'hex'); // send data to reducer dispatch({ type: DISCOVERY.START, coin: coinToDiscover.network, device, - xpub: response.data.publicKey, + xpub: response.payload.publicKey, basePath, hdKey, }); @@ -484,14 +492,14 @@ export const discoverAddress = (device: any, discoveryProcess: Discovery): any = instance: device.instance, state: device.checksum }, - address_n: path, + path, showOnTrezor: false }); if (discoveryProcess.interrupted) return; if (verifyAddress && verifyAddress.success) { - //const trezorAddress: string = '0x' + verifyAddress.data.message.address; - const trezorAddress: string = EthereumjsUtil.toChecksumAddress(verifyAddress.data.message.address); + //const trezorAddress: string = '0x' + verifyAddress.payload.address; + const trezorAddress: string = EthereumjsUtil.toChecksumAddress(verifyAddress.payload.address); if (trezorAddress !== ethAddress) { // throw inconsistent state error console.warn("Inconsistent state", trezorAddress, ethAddress); @@ -522,7 +530,7 @@ export const discoverAddress = (device: any, discoveryProcess: Discovery): any = payload: { type: 'error', title: 'Address validation error', - message: verifyAddress.data.error, + message: verifyAddress.payload.error, cancelable: true, actions: [ { diff --git a/src/js/reducers/ModalReducer.js b/src/js/reducers/ModalReducer.js index c4a35633..b7daab3d 100644 --- a/src/js/reducers/ModalReducer.js +++ b/src/js/reducers/ModalReducer.js @@ -79,7 +79,7 @@ export default function modal(state: ModalState = initialState, action: any): an case UI.REQUEST_PASSPHRASE : return { ...state, - device: action.data.device, + device: action.payload.device, opened: true, windowType: action.type }; @@ -88,7 +88,7 @@ export default function modal(state: ModalState = initialState, action: any): an return { ...state, opened: true, - windowType: action.data.code + windowType: action.payload.code } case UI.CLOSE_UI_WINDOW : diff --git a/src/js/reducers/TrezorConnectReducer.js b/src/js/reducers/TrezorConnectReducer.js index 9c31b1da..2d30adce 100644 --- a/src/js/reducers/TrezorConnectReducer.js +++ b/src/js/reducers/TrezorConnectReducer.js @@ -30,6 +30,8 @@ type State = { selectedDevice: ?SelectedDevice; discoveryComplete: boolean; error: any; + transport: ?string; + browserState: any; } @@ -38,6 +40,8 @@ const initialState: State = { selectedDevice: null, discoveryComplete: false, error: null, + transport: null, + browserState: {} }; export const findSelectedDevice = (state: State): ?TrezorDevice => { @@ -297,6 +301,13 @@ export default function connect(state: State = initialState, action: any): any { switch (action.type) { + case 'iframe_handshake' : + return { + ...state, + browserState: action.payload.browserState + } + + case CONNECT.DUPLICATE : return duplicate(state, action.device); @@ -313,10 +324,18 @@ export default function connect(state: State = initialState, action: any): any { error: action.error }; + case TRANSPORT.START : + return { + ...state, + transport: action.payload + } + case TRANSPORT.ERROR : return { ...state, - error: action.device // message is wrapped in "device" field. It's dispatched from TrezorConnect.on(DEVICE_EVENT...) in TrezorConnectService + // error: action.payload, // message is wrapped in "device" field. It's dispatched from TrezorConnect.on(DEVICE_EVENT...) in TrezorConnectService + error: "Transport is missing", + transport: null, }; case CONNECT.DEVICE_FROM_STORAGE :