1+ import { DEVICE } from '../../events/device' ;
12import type { Device } from '../Device' ;
23import { createThpChannel , thpHandshake } from './handshake' ;
34import { thpPairing } from './pairing' ;
@@ -9,26 +10,40 @@ export { createThpSession } from './session';
910export 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