Skip to content

Commit 760b719

Browse files
committed
feat(connect): emit pin request only in thp handshake with interactions
1 parent 3c555e6 commit 760b719

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

packages/connect/src/device/Device.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,11 +580,9 @@ export class Device extends TypedEmitter<DeviceEvents> {
580580

581581
if (this.protocol.name === 'v2') {
582582
const withInteraction = !!fn;
583-
await getThpChannel(this, withInteraction);
584-
if (this.getThpState()?.isAutoconnectPaired || withInteraction) {
583+
this.busy = await getThpChannel(this, withInteraction);
584+
if (!this.busy) {
585585
await this.getFeatures();
586-
} else {
587-
this.busy = 'thp-locked';
588586
}
589587
} else if (fn) {
590588
await this.initialize(!!options.useCardanoDerivation);

packages/connect/src/device/thp/index.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DEVICE } from '../../events/device';
12
import type { Device } from '../Device';
23
import { createThpChannel, thpHandshake } from './handshake';
34
import { thpPairing } from './pairing';
@@ -9,26 +10,40 @@ export { createThpSession } from './session';
910
export const getThpChannel = async (device: Device, withInteraction?: boolean) => {
1011
const thpState = device.getThpState();
1112

13+
if (!thpState) return;
14+
1215
try {
13-
if (thpState?.phase === 'handshake') {
16+
if (thpState.phase === 'handshake') {
1417
await createThpChannel(device);
1518
try {
1619
await thpHandshake(device);
1720
} catch (error) {
1821
if (error.code === 'ThpDeviceLocked') {
19-
// Device is pin-locked, retry handshake with tryToUnlock param
2022
thpState.resetState();
23+
if (!withInteraction) {
24+
return 'pin-locked';
25+
}
26+
27+
// Device is pin-locked and interactions enabled, retry handshake with tryToUnlock param
28+
device.emit(DEVICE.BUTTON, {
29+
device,
30+
payload: { code: 'ButtonRequest_PinEntry' },
31+
});
2132
await createThpChannel(device);
2233
await thpHandshake(device, true);
2334
} else {
2435
throw error;
2536
}
2637
}
2738
}
28-
if (thpState?.phase === 'pairing' && withInteraction) {
39+
if (thpState.phase === 'pairing' && withInteraction) {
2940
// start pairing with UI interaction
3041
await thpPairing(device);
3142
}
43+
44+
if (thpState.phase !== 'paired') {
45+
return 'thp-locked';
46+
}
3247
} catch (error) {
3348
thpState?.resetState();
3449

0 commit comments

Comments
 (0)