Skip to content

Commit

Permalink
Send path to ipc.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Nov 10, 2020
1 parent 2b63981 commit f795a76
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
17 changes: 6 additions & 11 deletions app/actions/TrezorActions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as wallet from "wallet";
import * as selectors from "selectors";
import fs from "fs";
import { rawToHex, hexToBytes, str2utf8hex } from "helpers";
import { hexToBytes, str2utf8hex } from "helpers";
import {
walletTxToBtcjsTx,
walletTxToRefTx,
Expand Down Expand Up @@ -34,7 +33,6 @@ const NOBACKUP = "no-backup";
const TRANSPORT_ERROR = "transport-error";
const TRANSPORT_START = "transport-start";
const BOOTLOADER_MODE = "bootloader";
const DISCONNECTED_DURING_ACTION = "device disconnected during action";

let setListeners = false;

Expand Down Expand Up @@ -114,7 +112,7 @@ export const TRZ_SELECTEDDEVICE_CHANGED = "TRZ_SELECTEDDEVICE_CHANGED";
export const TRZ_NOCONNECTEDDEVICE = "TRZ_NOCONNECTEDDEVICE";

function onChange(dispatch, getState, features) {
if (features == null) return;
if (features == null) throw "no features on change";
const currentDevice = selectors.trezorDevice(getState());
// No current device handle by connect.
if (!currentDevice) return;
Expand All @@ -128,7 +126,7 @@ function onChange(dispatch, getState, features) {
};

function onConnect(dispatch, getState, features) {
if (features == null) return;
if (features == null) throw "no features on connect";
let device = features.id;
const deviceLabel = features.label;
if (features.mode == BOOTLOADER_MODE) {
Expand All @@ -139,7 +137,7 @@ function onConnect(dispatch, getState, features) {
};

function onDisconnect(dispatch, getState, features) {
if (features == null) throw "no features on change";
if (features == null) throw "no features on disconnect";
const currentDevice = selectors.trezorDevice(getState());
const device = features.id;
// If this is not the device we presume is current, ignore.
Expand Down Expand Up @@ -802,12 +800,9 @@ export const updateFirmware = (path) => async (dispatch, getState) => {
}

try {
const rawFirmware = fs.readFileSync(path);
const hexFirmware = rawToHex(rawFirmware);
// Ask main.development.js to send the firmware for us.
const errorStr = await ipcRenderer.invoke("upload-firmware", hexFirmware);
// A successful upload will end with the user unplugging the device.
if (errorStr != DISCONNECTED_DURING_ACTION) throw errorStr;
const errorStr = await ipcRenderer.invoke("upload-firmware", path);
if (errorStr) throw errorStr;
dispatch({ type: TRZ_UPDATEFIRMWARE_SUCCESS });
} catch (error) {
dispatch({ error, type: TRZ_UPDATEFIRMWARE_FAILED });
Expand Down
22 changes: 19 additions & 3 deletions app/main_dev/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import { MAINNET } from "constants";
import { initTransport } from "actions/TrezorActions.js";
import * as connect from "connect";
import { rawToHex } from "helpers";


const logger = createLogger();
let watchingOnlyWallet;
Expand Down Expand Up @@ -144,20 +146,34 @@ export const removeWallet = (testnet, walletPath) => {
};

// updateTrezorFirmware attempts to make a temporary connection to a trezor
// device and update it with the supplied firmware. It returns an error string
// device and update it with the firmware at path. It returns an error string
// in case of error.
export const updateTrezorFirmware = async ( firmware ) => {
export const updateTrezorFirmware = async ( firmwarePath ) => {
const rawFirmware = fs.readFileSync(firmwarePath);
const hexFirmware = rawToHex(rawFirmware);
let session = connect.default;
let completed = false;
try {
await initTransport(session, false);
session.on(connect.UI_EVENT, (event) => {
if (event.type == connect.UI.FIRMWARE_PROGRESS) {
logger.log("info", "Trezor update progress: " + event.payload.progress+"%");
// session.cancel() will cause an error but we ignore it if completed.
if (event.payload.progress == 100) {
completed = true;
if (session) session.cancel();
}
}
});
const res = await session.firmwareUpdate({
binary: firmware
binary: hexFirmware
});
if (res.payload && res.payload.error) {
throw res.payload.error;
}
return null;
} catch (e) {
if (completed) return null;
logger.log("error", "error uploading trezor firmware: " + e);
return e.toString();
} finally {
Expand Down

0 comments on commit f795a76

Please sign in to comment.