1
+ import { DEVICE } from '../../events/device' ;
1
2
import type { Device } from '../Device' ;
2
3
import { createThpChannel , thpHandshake } from './handshake' ;
3
4
import { thpPairing } from './pairing' ;
@@ -9,26 +10,40 @@ export { createThpSession } from './session';
9
10
export const getThpChannel = async ( device : Device , withInteraction ?: boolean ) => {
10
11
const thpState = device . getThpState ( ) ;
11
12
13
+ if ( ! thpState ) return ;
14
+
12
15
try {
13
- if ( thpState ? .phase === 'handshake' ) {
16
+ if ( thpState . phase === 'handshake' ) {
14
17
await createThpChannel ( device ) ;
15
18
try {
16
19
await thpHandshake ( device ) ;
17
20
} catch ( error ) {
18
21
if ( error . code === 'ThpDeviceLocked' ) {
19
- // Device is pin-locked, retry handshake with tryToUnlock param
20
22
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
+ } ) ;
21
32
await createThpChannel ( device ) ;
22
33
await thpHandshake ( device , true ) ;
23
34
} else {
24
35
throw error ;
25
36
}
26
37
}
27
38
}
28
- if ( thpState ? .phase === 'pairing' && withInteraction ) {
39
+ if ( thpState . phase === 'pairing' && withInteraction ) {
29
40
// start pairing with UI interaction
30
41
await thpPairing ( device ) ;
31
42
}
43
+
44
+ if ( thpState . phase !== 'paired' ) {
45
+ return 'thp-locked' ;
46
+ }
32
47
} catch ( error ) {
33
48
thpState ?. resetState ( ) ;
34
49
0 commit comments