refactoring field incomming from TrezorConnect responses

pull/2/merge
Szymon Lesisz 6 years ago
parent 8ed54612e8
commit 88b0c5cd34

@ -8,7 +8,7 @@ import * as CONNECT from './constants/TrezorConnect';
export function onPinSubmit(value: string): any { export function onPinSubmit(value: string): any {
TrezorConnect.uiMessage({ type: UI.RECEIVE_PIN, data: value }); TrezorConnect.uiResponse({ type: UI.RECEIVE_PIN, payload: value });
return { return {
type: ACTIONS.CLOSE_MODAL type: ACTIONS.CLOSE_MODAL
} }
@ -16,9 +16,9 @@ export function onPinSubmit(value: string): any {
export function onPassphraseSubmit(passphrase: string): any { export function onPassphraseSubmit(passphrase: string): any {
return async (dispatch, getState): Promise<void> => { return async (dispatch, getState): Promise<void> => {
const resp = await TrezorConnect.uiMessage({ const resp = await TrezorConnect.uiResponse({
type: UI.RECEIVE_PASSPHRASE, type: UI.RECEIVE_PASSPHRASE,
data: { payload: {
value: passphrase, value: passphrase,
save: true save: true
} }

@ -80,7 +80,7 @@ export const showAddress = (address_n: string): any => {
instance: selected.instance, instance: selected.instance,
state: selected.checksum state: selected.checksum
}, },
address_n path: address_n,
}); });
if (response && response.success) { if (response && response.success) {
@ -94,7 +94,7 @@ export const showAddress = (address_n: string): any => {
payload: { payload: {
type: 'error', type: 'error',
title: 'Veryfying address error', title: 'Veryfying address error',
message: response.data.error, message: response.payload.error,
cancelable: true, cancelable: true,
actions: [ actions: [
{ {

@ -686,14 +686,14 @@ export const onSend = (): any => {
state: selected.checksum state: selected.checksum
}, },
//path: "m/44'/60'/0'/0/0", //path: "m/44'/60'/0'/0/0",
address_n: txData.address_n, path: txData.address_n,
nonce: strip(txData.nonce), nonce: strip(txData.nonce),
gas_price: strip(txData.gasPrice), gasPrice: strip(txData.gasPrice),
gas_limit: strip(txData.gasLimit), gasLimit: strip(txData.gasLimit),
to: strip(txData.to), to: strip(txData.to),
value: strip(txData.value), value: strip(txData.value),
data: strip(txData.data), data: strip(txData.data),
chain_id: txData.chainId chainId: txData.chainId
}); });
if (!signedTransaction || !signedTransaction.success) { if (!signedTransaction || !signedTransaction.success) {
@ -703,7 +703,7 @@ export const onSend = (): any => {
payload: { payload: {
type: 'error', type: 'error',
title: 'Transaction error', title: 'Transaction error',
message: signedTransaction.data.error, message: signedTransaction.payload.error,
cancelable: true, cancelable: true,
actions: [ ] actions: [ ]
} }
@ -711,9 +711,9 @@ export const onSend = (): any => {
return; return;
} }
txData.r = '0x' + signedTransaction.data.r; txData.r = '0x' + signedTransaction.payload.r;
txData.s = '0x' + signedTransaction.data.s; txData.s = '0x' + signedTransaction.payload.s;
txData.v = web3.toHex(signedTransaction.data.v); txData.v = web3.toHex(signedTransaction.payload.v);
// const gasLimit2 = await estimateGas(web3, txData); // const gasLimit2 = await estimateGas(web3, txData);
// console.log("---->GASSS", txData, gasLimit2.toString() ); // console.log("---->GASSS", txData, gasLimit2.toString() );

@ -1,7 +1,7 @@
/* @flow */ /* @flow */
'use strict'; '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 ACTIONS from './index';
import * as ADDRESS from './constants/Address'; import * as ADDRESS from './constants/Address';
import * as TOKEN from './constants/Token'; import * as TOKEN from './constants/Token';
@ -26,37 +26,46 @@ import { resolveAfter } from '../utils/promiseUtils';
import { getAccounts } from '../utils/reducerUtils'; import { getAccounts } from '../utils/reducerUtils';
import { findSelectedDevice, isSavedDevice } from '../reducers/TrezorConnectReducer'; import { findSelectedDevice, isSavedDevice } from '../reducers/TrezorConnectReducer';
export const init = (): any => { export const init = (): any => {
return async (dispatch, getState): Promise<void> => { return async (dispatch, getState): Promise<void> => {
// set listeners // set listeners
TrezorConnect.on(DEVICE_EVENT, (event: DeviceMessage): void => { TrezorConnect.on(DEVICE_EVENT, (event: DeviceMessage): void => {
dispatch({ dispatch({
type: event.type, type: event.type,
device: event.data device: event.payload
}); });
}); });
const version: Object = TrezorConnect.getVersion(); const version: Object = TrezorConnect.getVersion();
if (version.type === 'library') { TrezorConnect.on(UI_EVENT, (event: UiMessage): void => {
// handle UI events only if TrezorConnect isn't using popup // post event to reducers
TrezorConnect.on(UI_EVENT, (type: string, data: any): void => { dispatch({
// post event to reducers type: event.type,
dispatch({ payload: event.payload
type,
data
});
}); });
} });
TrezorConnect.on(TRANSPORT_EVENT, (event: UiMessage): void => {
// post event to reducers
dispatch({
type: event.type,
payload: event.payload
});
});
try { try {
await TrezorConnect.init({ await TrezorConnect.init({
transport_reconnect: true, // transportReconnect: true,
coins_src: './data/coins.json', coinsSrc: './data/coins.json',
firmware_releases_src: './data/releases-1.json', firmwareReleasesSrc: './data/releases-1.json',
transport_config_src: './data/config_signed.bin', transportConfigSrc: './data/config_signed.bin',
latest_bridge_src: './data/latest.txt' transportReconnect: false,
latestBridgeSrc: './data/latest.txt',
connectSrc: 'https://localhost:8088/',
// connectSrc: 'https://sisyfos.trezor.io/',
debug: true,
popup: false,
// webusb: false
}); });
setTimeout(() => { setTimeout(() => {
@ -218,7 +227,7 @@ export const getSelectedDeviceState = (): any => {
dispatch({ dispatch({
type: CONNECT.AUTH_DEVICE, type: CONNECT.AUTH_DEVICE,
device: selected, device: selected,
checksum: response.data.xpub checksum: response.payload.xpub
}); });
} else { } else {
dispatch({ dispatch({
@ -226,7 +235,7 @@ export const getSelectedDeviceState = (): any => {
payload: { payload: {
type: 'error', type: 'error',
title: 'Authentification error', title: 'Authentification error',
message: response.data.error, message: response.payload.error,
cancelable: true, cancelable: true,
actions: [ actions: [
{ {
@ -331,7 +340,7 @@ export function acquire(): any {
payload: { payload: {
type: 'error', type: 'error',
title: 'Acquire device error', title: 'Acquire device error',
message: response.data.error, message: response.payload.error,
cancelable: true, cancelable: true,
actions: [ actions: [
{ {
@ -393,13 +402,12 @@ export const beginDiscoveryProcess = (device: any, coin: string): any => {
// TODO: validate device checksum // TODO: validate device checksum
// const checksum = await __acquire(device.path, device.instance); // const checksum = await __acquire(device.path, device.instance);
// if (checksum && checksum.success) { // if (checksum && checksum.success) {
// if (checksum.data.xpub !== device.checksum) { // if (checksum.payload.xpub !== device.checksum) {
// console.error("Incorrect checksum!"); // console.error("Incorrect checksum!");
// return; // return;
// } // }
// } // }
// acquire and hold session
// get xpub from TREZOR // get xpub from TREZOR
const response = await TrezorConnect.getPublicKey({ const response = await TrezorConnect.getPublicKey({
device: { device: {
@ -409,7 +417,7 @@ export const beginDiscoveryProcess = (device: any, coin: string): any => {
}, },
path: coinToDiscover.bip44, path: coinToDiscover.bip44,
confirmation: false, confirmation: false,
keepSession: true keepSession: true // acquire and hold session
}); });
if (!response.success) { if (!response.success) {
@ -420,7 +428,7 @@ export const beginDiscoveryProcess = (device: any, coin: string): any => {
payload: { payload: {
type: 'error', type: 'error',
title: 'Discovery error', title: 'Discovery error',
message: response.data.error, message: response.payload.error,
cancelable: true, cancelable: true,
actions: [ actions: [
{ {
@ -438,17 +446,17 @@ export const beginDiscoveryProcess = (device: any, coin: string): any => {
// TODO: check for interruption // TODO: check for interruption
// TODO: handle response error // TODO: handle response error
const basePath: Array<number> = response.data.path; const basePath: Array<number> = response.payload.path;
const hdKey = new HDKey(); const hdKey = new HDKey();
hdKey.publicKey = new Buffer(response.data.publicKey, 'hex'); hdKey.publicKey = new Buffer(response.payload.publicKey, 'hex');
hdKey.chainCode = new Buffer(response.data.chainCode, 'hex'); hdKey.chainCode = new Buffer(response.payload.chainCode, 'hex');
// send data to reducer // send data to reducer
dispatch({ dispatch({
type: DISCOVERY.START, type: DISCOVERY.START,
coin: coinToDiscover.network, coin: coinToDiscover.network,
device, device,
xpub: response.data.publicKey, xpub: response.payload.publicKey,
basePath, basePath,
hdKey, hdKey,
}); });
@ -484,14 +492,14 @@ export const discoverAddress = (device: any, discoveryProcess: Discovery): any =
instance: device.instance, instance: device.instance,
state: device.checksum state: device.checksum
}, },
address_n: path, path,
showOnTrezor: false showOnTrezor: false
}); });
if (discoveryProcess.interrupted) return; if (discoveryProcess.interrupted) return;
if (verifyAddress && verifyAddress.success) { if (verifyAddress && verifyAddress.success) {
//const trezorAddress: string = '0x' + verifyAddress.data.message.address; //const trezorAddress: string = '0x' + verifyAddress.payload.address;
const trezorAddress: string = EthereumjsUtil.toChecksumAddress(verifyAddress.data.message.address); const trezorAddress: string = EthereumjsUtil.toChecksumAddress(verifyAddress.payload.address);
if (trezorAddress !== ethAddress) { if (trezorAddress !== ethAddress) {
// throw inconsistent state error // throw inconsistent state error
console.warn("Inconsistent state", trezorAddress, ethAddress); console.warn("Inconsistent state", trezorAddress, ethAddress);
@ -522,7 +530,7 @@ export const discoverAddress = (device: any, discoveryProcess: Discovery): any =
payload: { payload: {
type: 'error', type: 'error',
title: 'Address validation error', title: 'Address validation error',
message: verifyAddress.data.error, message: verifyAddress.payload.error,
cancelable: true, cancelable: true,
actions: [ actions: [
{ {

@ -79,7 +79,7 @@ export default function modal(state: ModalState = initialState, action: any): an
case UI.REQUEST_PASSPHRASE : case UI.REQUEST_PASSPHRASE :
return { return {
...state, ...state,
device: action.data.device, device: action.payload.device,
opened: true, opened: true,
windowType: action.type windowType: action.type
}; };
@ -88,7 +88,7 @@ export default function modal(state: ModalState = initialState, action: any): an
return { return {
...state, ...state,
opened: true, opened: true,
windowType: action.data.code windowType: action.payload.code
} }
case UI.CLOSE_UI_WINDOW : case UI.CLOSE_UI_WINDOW :

@ -30,6 +30,8 @@ type State = {
selectedDevice: ?SelectedDevice; selectedDevice: ?SelectedDevice;
discoveryComplete: boolean; discoveryComplete: boolean;
error: any; error: any;
transport: ?string;
browserState: any;
} }
@ -38,6 +40,8 @@ const initialState: State = {
selectedDevice: null, selectedDevice: null,
discoveryComplete: false, discoveryComplete: false,
error: null, error: null,
transport: null,
browserState: {}
}; };
export const findSelectedDevice = (state: State): ?TrezorDevice => { export const findSelectedDevice = (state: State): ?TrezorDevice => {
@ -297,6 +301,13 @@ export default function connect(state: State = initialState, action: any): any {
switch (action.type) { switch (action.type) {
case 'iframe_handshake' :
return {
...state,
browserState: action.payload.browserState
}
case CONNECT.DUPLICATE : case CONNECT.DUPLICATE :
return duplicate(state, action.device); return duplicate(state, action.device);
@ -313,10 +324,18 @@ export default function connect(state: State = initialState, action: any): any {
error: action.error error: action.error
}; };
case TRANSPORT.START :
return {
...state,
transport: action.payload
}
case TRANSPORT.ERROR : case TRANSPORT.ERROR :
return { return {
...state, ...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 : case CONNECT.DEVICE_FROM_STORAGE :

Loading…
Cancel
Save