Skip to content

Commit 65d0800

Browse files
authored
Merge pull request #2860 from input-output-hk/fix/ddw-926-trezor-error-handling
2 parents 3df2fbf + e466617 commit 65d0800

File tree

14 files changed

+204
-125
lines changed

14 files changed

+204
-125
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585

8686
### Fixes
8787

88+
- Improved error handling for incorrect passphrase and incorrect hardware wallet error ([PR 2860](https://github.com/input-output-hk/daedalus/pull/2860))
8889
- Fixed rewards CSV export issues ([PR 2885](https://github.com/input-output-hk/daedalus/pull/2885))
8990
- Fixed behaviour of wallet settings option of the app menu ([PR 2838](https://github.com/input-output-hk/daedalus/pull/2838))
9091
- Fixed styling of ITN rewards feature ([PR 2861](https://github.com/input-output-hk/daedalus/pull/2861))

gulpfile.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ gulp.task(
177177
'clean:dist',
178178
'server:create:dev',
179179
'build:renderer:html',
180-
'build:main:watch',
180+
process.argv.includes('--disable-electron-reload')
181+
? 'build:main'
182+
: 'build:main:watch',
181183
'build:renderer:watch'
182184
)
183185
);

source/main/ipc/getHardwareWalletChannel.ts

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import {
2727
import { HardwareWalletChannels } from './createHardwareWalletIPCChannels';
2828
import { Device } from './hardwareWallets/ledger/deviceDetection/types';
2929
import { DeviceDetectionPayload } from './hardwareWallets/ledger/deviceDetection/deviceDetection';
30+
import { initTrezorConnect, reinitTrezorConnect } from '../trezor/connection';
31+
import { manifest } from '../trezor/manifest';
32+
import { buildTrezorDeviceParams } from '../utils/buildTrezorDeviceParams';
3033

3134
type ListenerType = {
3235
unsubscribe: (...args: Array<any>) => any;
@@ -169,6 +172,8 @@ export const handleHardwareWalletRequests = async (
169172
TrezorConnect.removeAllListeners();
170173
// Initialize new device listeners
171174
TrezorConnect.on(UI_EVENT, (event) => {
175+
logger.info('[TREZOR-CONNECT] Received UI_EVENT: ' + event.type);
176+
172177
if (event.type === UI.REQUEST_PASSPHRASE) {
173178
// ui-request_passphrase
174179
if (event.payload && event.payload.device) {
@@ -180,11 +185,20 @@ export const handleHardwareWalletRequests = async (
180185
passphraseOnDevice: true,
181186
},
182187
});
188+
189+
logger.info(
190+
'[TREZOR-CONNECT] Called TrezorConnect.uiResponse - requested to provide passphrase on device'
191+
);
183192
}
184193
}
185194
});
186195
TrezorConnect.on(TRANSPORT_EVENT, (event) => {
187196
if (event.type === TRANSPORT.ERROR) {
197+
logger.info(
198+
'[TREZOR-CONNECT] Received TRANSPORT_EVENT: transport-error',
199+
event.payload
200+
);
201+
188202
// Send Transport error to Renderer
189203
getHardwareWalletConnectionChannel.send(
190204
{
@@ -198,6 +212,8 @@ export const handleHardwareWalletRequests = async (
198212
}
199213
});
200214
TrezorConnect.on(DEVICE_EVENT, (event) => {
215+
logger.info('[TREZOR-CONNECT] Received DEVICE_EVENT: ' + event.type);
216+
201217
const connectionChanged =
202218
event.type === DEVICE.CONNECT ||
203219
event.type === DEVICE.DISCONNECT ||
@@ -250,13 +266,13 @@ export const handleHardwareWalletRequests = async (
250266
getHardwareWalletTransportChannel.onRequest(
251267
async (request: HardwareWalletTransportDeviceRequest) => {
252268
const { isTrezor, devicePath } = request;
253-
// @ts-ignore ts-migrate(2345) FIXME: Argument of type 'string' is not assignable to par... Remove this comment to see the full error message
254-
logger.info('[HW-DEBUG] getHardwareWalletTransportChannel', devicePath);
269+
logger.info('[HW-DEBUG] getHardwareWalletTransportChannel', {
270+
devicePath,
271+
});
255272
// Connected Trezor device info
256273
let deviceFeatures;
257274

258275
if (isTrezor) {
259-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
260276
logger.info('[HW-DEBUG] getHardwareWalletTransportChannel::TREZOR ');
261277

262278
try {
@@ -265,10 +281,12 @@ export const handleHardwareWalletRequests = async (
265281
path: devicePath,
266282
},
267283
});
268-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
269-
logger.info('[HW-DEBUG] Trezor connect success');
284+
285+
logger.info('[TREZOR-CONNECT] Called TrezorConnect.getFeatures()');
270286

271287
if (deviceFeatures && deviceFeatures.success) {
288+
logger.info('[HW-DEBUG] Trezor connect success');
289+
272290
const {
273291
major_version: majorVersion,
274292
minor_version: minorVersion,
@@ -291,19 +309,16 @@ export const handleHardwareWalletRequests = async (
291309

292310
throw deviceFeatures.payload; // Error is in payload
293311
} catch (e) {
294-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
295312
logger.info('[HW-DEBUG] Trezor connect error');
296313
throw e;
297314
}
298315
}
299316

300317
try {
301-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
302318
logger.info('[HW-DEBUG] getHardwareWalletTransportChannel:: LEDGER');
303319
const transportList = await TransportNodeHid.list();
304320
let hw;
305321
let lastConnectedPath;
306-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
307322
logger.info(
308323
`[HW-DEBUG] getHardwareWalletTransportChannel::transportList=${JSON.stringify(
309324
transportList
@@ -336,14 +351,12 @@ export const handleHardwareWalletRequests = async (
336351
if (transportList && !transportList.length) {
337352
// Establish connection with last device
338353
try {
339-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
340354
logger.info('[HW-DEBUG] INIT NEW transport');
341355

342356
const { device } = await waitForDevice();
343357

344358
await openTransportLayer(device.path, device);
345359
} catch (e) {
346-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
347360
logger.info('[HW-DEBUG] INIT NEW transport - ERROR');
348361
throw e;
349362
}
@@ -362,7 +375,6 @@ export const handleHardwareWalletRequests = async (
362375
throw new Error('Device not connected!');
363376
}
364377
} else {
365-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
366378
logger.info('[HW-DEBUG] USE CURRENT CONNECTION');
367379
hw = devicesMemo[devicePath].transport;
368380
deviceConnection = get(devicesMemo, [devicePath, 'AdaConnection']);
@@ -394,41 +406,19 @@ export const handleHardwareWalletRequests = async (
394406

395407
throw new Error('Missing device info');
396408
} catch (error) {
397-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
398409
logger.info('[HW-DEBUG] ERROR on getHardwareWalletTransportChannel');
399410
throw error;
400411
}
401412
}
402413
);
414+
403415
handleInitTrezorConnectChannel.onRequest(async () => {
404-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
405416
logger.info('[HW-DEBUG] INIT TREZOR');
406417
resetTrezorListeners();
407-
TrezorConnect.manifest({
408-
409-
appUrl: 'http://your.application.com',
410-
});
411-
TrezorConnect.init({
412-
popup: false,
413-
// render your own UI
414-
webusb: false,
415-
// webusb is not supported in electron
416-
debug: true,
417-
// see what's going on inside connect
418-
// lazyLoad: true, // set to "false" (default) if you want to start communication with bridge on application start (and detect connected device right away)
419-
// set it to "true", then trezor-connect will not be initialized until you call some TrezorConnect.method()
420-
// this is useful when you don't know if you are dealing with Trezor user
421-
manifest: {
422-
423-
// @TODO
424-
appUrl: 'http://your.application.com', // @TODO
425-
},
426-
})
427-
.then(() => {})
428-
.catch((error) => {
429-
throw error;
430-
});
418+
TrezorConnect.manifest(manifest);
419+
await initTrezorConnect();
431420
});
421+
432422
handleInitLedgerConnectChannel.onRequest(async () => {
433423
logger.info('[HW-DEBUG] INIT LEDGER');
434424
observer = new EventObserver({
@@ -483,16 +473,17 @@ export const handleHardwareWalletRequests = async (
483473

484474
try {
485475
deviceConnection = get(devicesMemo, [devicePath, 'AdaConnection']);
486-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
476+
487477
logger.info('[HW-DEBUG] DERIVE ADDRESS');
488478

489479
if (isTrezor) {
480+
logger.info(
481+
'[TREZOR-CONNECT] Called TrezorConnect.cardanoGetAddress()'
482+
);
483+
490484
const result = await TrezorConnect.cardanoGetAddress({
491-
device: {
492-
path: devicePath,
493-
// @ts-ignore ts-migrate(2769) FIXME: No overload matches this call.
494-
showOnTrezor: true,
495-
},
485+
showOnTrezor: true,
486+
device: buildTrezorDeviceParams(devicePath),
496487
addressParameters: {
497488
addressType,
498489
path: `m/${spendingPathStr}`,
@@ -548,7 +539,7 @@ export const handleHardwareWalletRequests = async (
548539

549540
try {
550541
deviceConnection = get(devicesMemo, [devicePath, 'AdaConnection']);
551-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
542+
552543
logger.info('[HW-DEBUG] SHOW ADDRESS');
553544

554545
if (isTrezor) {
@@ -641,14 +632,13 @@ export const handleHardwareWalletRequests = async (
641632
};
642633
}
643634

644-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
645635
logger.info(`[HW-DEBUG] GET CARDANO APP path:${path}`);
646636
deviceConnection = devicesMemo[path].AdaConnection;
647637
const { version } = await deviceConnection.getVersion();
648-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
638+
649639
logger.info('[HW-DEBUG] getCardanoAdaAppChannel:: appVersion');
650640
const { serialHex } = await deviceConnection.getSerial();
651-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
641+
652642
logger.info(
653643
`[HW-DEBUG] getCardanoAdaAppChannel:: deviceSerial: ${serialHex}`
654644
);
@@ -686,6 +676,12 @@ export const handleHardwareWalletRequests = async (
686676
const hasPathChanged = !includes(devicePaths, oldPath);
687677
const newPath = hasPathChanged ? last(devicePaths) : oldPath;
688678

679+
if (hasPathChanged) {
680+
logger.info(
681+
`[HW-DEBUG] Device path changed from ${oldPath} to ${newPath}`
682+
);
683+
}
684+
689685
if (!newPath) {
690686
logger.info(
691687
'[HW-DEBUG] ERROR in Cardano App (Device paths list is empty)',
@@ -749,20 +745,26 @@ export const handleHardwareWalletRequests = async (
749745
getExtendedPublicKeyChannel.onRequest(async (params) => {
750746
// Params example:
751747
// { path: "1852'/1815'/0'", isTrezor: false, devicePath: null }
752-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
748+
753749
logger.info('[HW-DEBUG] getExtendedPublicKeyChannel');
754750
const { path, isTrezor, devicePath } = params;
755751

756752
try {
757753
if (isTrezor) {
758-
// Check if Trezor instantiated
754+
// We re-initialize the Trezor Connect session to give the user the chance to provide
755+
// a different passphrase, in case they want to switch to a different
756+
// hidden wallet or just if they provided a wrong one.
757+
await reinitTrezorConnect();
758+
759+
logger.info('[TREZOR-CONNECT] Calling TrezorConnect.getFeatures()');
759760
const deviceFeatures = await TrezorConnect.getFeatures({
760-
device: {
761-
path: devicePath,
762-
},
761+
device: buildTrezorDeviceParams(devicePath),
763762
});
764763

765764
if (deviceFeatures.success) {
765+
logger.info(
766+
'[TREZOR-CONNECT] Calling TrezorConnect.cardanoGetPublicKey()'
767+
);
766768
const extendedPublicKeyResponse = await TrezorConnect.cardanoGetPublicKey(
767769
{
768770
path: `m/${path}`,
@@ -774,15 +776,13 @@ export const handleHardwareWalletRequests = async (
774776
throw extendedPublicKeyResponse.payload;
775777
}
776778

777-
const extendedPublicKey = get(
778-
extendedPublicKeyResponse,
779-
['payload', 'node'],
780-
{}
781-
);
779+
const extendedPublicKey = get(extendedPublicKeyResponse, [
780+
'payload',
781+
'node',
782+
]);
783+
782784
return Promise.resolve({
783-
// @ts-ignore ts-migrate(2339) FIXME: Property 'public_key' does not exist on type '{} |... Remove this comment to see the full error message
784785
publicKeyHex: extendedPublicKey.public_key,
785-
// @ts-ignore ts-migrate(2339) FIXME: Property 'chain_code' does not exist on type '{} |... Remove this comment to see the full error message
786786
chainCodeHex: extendedPublicKey.chain_code,
787787
});
788788
}
@@ -791,7 +791,7 @@ export const handleHardwareWalletRequests = async (
791791
}
792792

793793
deviceConnection = get(devicesMemo, [devicePath, 'AdaConnection']);
794-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
794+
795795
logger.info('[HW-DEBUG] EXPORT KEY');
796796

797797
// Check if Ledger instantiated
@@ -829,7 +829,7 @@ export const handleHardwareWalletRequests = async (
829829
signingMode,
830830
additionalWitnessPaths,
831831
} = params;
832-
// @ts-ignore ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
832+
833833
logger.info('[HW-DEBUG] SIGN Ledger transaction');
834834
deviceConnection = devicePath
835835
? devicesMemo[devicePath].AdaConnection
@@ -897,19 +897,25 @@ export const handleHardwareWalletRequests = async (
897897
auxiliaryData,
898898
signingMode,
899899
};
900+
901+
logger.info(
902+
'[TREZOR-CONNECT] Calling TrezorConnect.cardanoSignTransaction()'
903+
);
904+
900905
// @ts-ignore ts-migrate(2345) FIXME: Argument of type '{ inputs: TrezorSignTransactionI... Remove this comment to see the full error message
901906
const signedTransaction = await TrezorConnect.cardanoSignTransaction({
902-
device: {
903-
path: devicePath,
904-
},
907+
device: buildTrezorDeviceParams(devicePath),
905908
...dataToSign,
906909
});
910+
907911
return Promise.resolve(signedTransaction);
908912
} catch (e) {
909913
throw e;
910914
}
911915
});
916+
912917
resetTrezorActionChannel.onRequest(async () => {
918+
logger.info('[TREZOR-CONNECT] Called TrezorConnect.cancel()');
913919
TrezorConnect.cancel('Method_Cancel');
914920
});
915921
};

source/main/trezor/connection.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import TrezorConnect from 'trezor-connect';
2+
import { logger } from '../utils/logging';
3+
import { manifest } from './manifest';
4+
5+
export const initTrezorConnect = async () => {
6+
try {
7+
await TrezorConnect.init({
8+
popup: false, // render your own UI
9+
webusb: false, // webusb is not supported in electron
10+
debug: true,
11+
manifest,
12+
});
13+
logger.info('[TREZOR-CONNECT] Called TrezorConnect.init()');
14+
} catch (error) {
15+
logger.info('[TREZOR-CONNECT] Failed to call TrezorConnect.init()');
16+
throw error;
17+
}
18+
};
19+
20+
export const reinitTrezorConnect = initTrezorConnect;

source/main/trezor/manifest.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Manifest } from 'trezor-connect';
2+
3+
export const manifest: Manifest = {
4+
5+
appUrl: 'https://daedaluswallet.io/',
6+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { CommonParams } from 'trezor-connect';
2+
import { logger } from './logging';
3+
4+
export const buildTrezorDeviceParams = (
5+
path: string | null
6+
): CommonParams['device'] => {
7+
if (path !== null) {
8+
logger.info(
9+
`[HW-DEBUG] Connecting Trezor device with ${path} as device path`
10+
);
11+
12+
return {
13+
path,
14+
};
15+
}
16+
17+
logger.info('[HW-DEBUG] Connecting Trezor device with null as device path');
18+
return undefined;
19+
};

0 commit comments

Comments
 (0)