1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-25 00:18:07 +00:00

refactoring field incomming from TrezorConnect responses

This commit is contained in:
Szymon Lesisz 2018-03-27 17:12:01 +02:00
parent 8ed54612e8
commit 88b0c5cd34
6 changed files with 76 additions and 49 deletions

View File

@ -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<void> => {
const resp = await TrezorConnect.uiMessage({
const resp = await TrezorConnect.uiResponse({
type: UI.RECEIVE_PASSPHRASE,
data: {
payload: {
value: passphrase,
save: true
}

View File

@ -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: [
{

View File

@ -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() );

View File

@ -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<void> => {
// 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<number> = response.data.path;
const basePath: Array<number> = 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: [
{

View File

@ -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 :

View File

@ -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 :