@ledgerhq/device-transport-kit-react-native-hid 1.0.4 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/android/build.gradle +1 -0
- package/android/src/main/kotlin/com/ledger/devicesdk/shared/androidMain/transport/usb/DefaultAndroidUsbTransport.kt +43 -17
- package/android/src/main/kotlin/com/ledger/devicesdk/shared/androidMain/transport/usb/utils/UsbMapper.kt +6 -4
- package/android/src/test/kotlin/com/ledger/devicesdk/shared/androidMain/transport/usb/DefaultAndroidUsbTransportTest.kt +212 -0
- package/android/src/test/kotlin/com/ledger/devicesdk/shared/androidMainInternal/transport/deviceconnection/DeviceConnectionStateMachineTest.kt +3 -3
- package/android/src/test/kotlin/com/ledger/devicesdk/shared/androidMainInternal/transport/deviceconnection/DeviceConnectionTest.kt +1 -1
- package/lib/cjs/package.json +2 -3
- package/lib/esm/package.json +2 -3
- package/lib/types/tsconfig.prod.tsbuildinfo +1 -1
- package/package.json +6 -7
- package/lib/cjs/api/bridge/mapper.test.js +0 -3
- package/lib/cjs/api/bridge/mapper.test.js.map +0 -7
- package/lib/cjs/api/helpers/base64Utils.test.js +0 -2
- package/lib/cjs/api/helpers/base64Utils.test.js.map +0 -7
- package/lib/cjs/api/helpers/getObservableOfArraysNewItems.test.js +0 -2
- package/lib/cjs/api/helpers/getObservableOfArraysNewItems.test.js.map +0 -7
- package/lib/cjs/api/transport/RNHidTransport.test.js +0 -2
- package/lib/cjs/api/transport/RNHidTransport.test.js.map +0 -7
- package/lib/esm/api/bridge/mapper.test.js +0 -3
- package/lib/esm/api/bridge/mapper.test.js.map +0 -7
- package/lib/esm/api/helpers/base64Utils.test.js +0 -2
- package/lib/esm/api/helpers/base64Utils.test.js.map +0 -7
- package/lib/esm/api/helpers/getObservableOfArraysNewItems.test.js +0 -2
- package/lib/esm/api/helpers/getObservableOfArraysNewItems.test.js.map +0 -7
- package/lib/esm/api/transport/RNHidTransport.test.js +0 -2
- package/lib/esm/api/transport/RNHidTransport.test.js.map +0 -7
- package/lib/types/api/bridge/mapper.test.d.ts +0 -2
- package/lib/types/api/bridge/mapper.test.d.ts.map +0 -1
- package/lib/types/api/helpers/base64Utils.test.d.ts +0 -2
- package/lib/types/api/helpers/base64Utils.test.d.ts.map +0 -1
- package/lib/types/api/helpers/getObservableOfArraysNewItems.test.d.ts +0 -2
- package/lib/types/api/helpers/getObservableOfArraysNewItems.test.d.ts.map +0 -1
- package/lib/types/api/transport/RNHidTransport.test.d.ts +0 -2
- package/lib/types/api/transport/RNHidTransport.test.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
- [AndroidManifest.xml](#androidmanifestxml)
|
|
13
13
|
- [Main Features](#main-features)
|
|
14
14
|
- [How To](#how-to)
|
|
15
|
+
- [Device ids](#device-ids)
|
|
15
16
|
|
|
16
17
|
## Description
|
|
17
18
|
|
|
@@ -82,3 +83,9 @@ const dmk = new DeviceManagementKitBuilder()
|
|
|
82
83
|
|
|
83
84
|
// You can then make use of the Device Management Kit
|
|
84
85
|
```
|
|
86
|
+
|
|
87
|
+
### Device ids
|
|
88
|
+
|
|
89
|
+
A USB device has the same id in discovery and after connect, in the format `usb_<number>` (for example, `usb_1005`).
|
|
90
|
+
|
|
91
|
+
When a device re-enumerates (for example, when an app change on the device triggers a disconnection/reconnection), Android gives it a new device number. If the device has an open session, discovery continues to give it the id of that session. If it has no open session, its id changes to the new number.
|
package/android/build.gradle
CHANGED
|
@@ -98,4 +98,5 @@ dependencies {
|
|
|
98
98
|
testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
|
|
99
99
|
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$kotlin_coroutines_test_version"
|
|
100
100
|
testImplementation "junit:junit:4.13.2"
|
|
101
|
+
testImplementation "io.mockk:mockk:$mockk_version"
|
|
101
102
|
}
|
|
@@ -10,12 +10,10 @@ import android.hardware.usb.UsbDevice
|
|
|
10
10
|
import android.hardware.usb.UsbManager
|
|
11
11
|
import android.hardware.usb.UsbRequest
|
|
12
12
|
import com.ledger.devicesdk.shared.androidMain.transport.usb.connection.AndroidUsbApduSender
|
|
13
|
-
import com.ledger.devicesdk.shared.androidMain.transport.usb.model.LedgerUsbDevice
|
|
14
13
|
import com.ledger.devicesdk.shared.androidMain.transport.usb.model.UsbPermissionEvent
|
|
15
14
|
import com.ledger.devicesdk.shared.androidMain.transport.usb.model.UsbState
|
|
16
15
|
import com.ledger.devicesdk.shared.androidMain.transport.usb.utils.toLedgerUsbDevice
|
|
17
16
|
import com.ledger.devicesdk.shared.androidMain.transport.usb.utils.toScannedDevice
|
|
18
|
-
import com.ledger.devicesdk.shared.androidMain.transport.usb.utils.toUsbDevices
|
|
19
17
|
import com.ledger.devicesdk.shared.androidMainInternal.transport.deviceconnection.DeviceConnection
|
|
20
18
|
import com.ledger.devicesdk.shared.api.discovery.DiscoveryDevice
|
|
21
19
|
import com.ledger.devicesdk.shared.internal.connection.InternalConnectedDevice
|
|
@@ -42,6 +40,7 @@ import kotlinx.coroutines.flow.merge
|
|
|
42
40
|
import kotlinx.coroutines.flow.shareIn
|
|
43
41
|
import kotlinx.coroutines.isActive
|
|
44
42
|
import kotlinx.coroutines.launch
|
|
43
|
+
import java.util.concurrent.ConcurrentHashMap
|
|
45
44
|
import kotlin.time.Duration
|
|
46
45
|
import kotlin.time.Duration.Companion.milliseconds
|
|
47
46
|
import kotlin.time.Duration.Companion.seconds
|
|
@@ -62,11 +61,16 @@ internal class DefaultAndroidUsbTransport(
|
|
|
62
61
|
private val internalUsbPermissionEventFlow: MutableSharedFlow<UsbPermissionEvent> =
|
|
63
62
|
MutableSharedFlow()
|
|
64
63
|
|
|
64
|
+
// The scan loop reads the connections while other coroutines change them.
|
|
65
65
|
private val usbConnections: MutableMap<String, DeviceConnection<AndroidUsbApduSender.Dependencies>> =
|
|
66
|
-
|
|
66
|
+
ConcurrentHashMap()
|
|
67
67
|
private val usbConnectionsPendingReconnection: MutableSet<DeviceConnection<AndroidUsbApduSender.Dependencies>> =
|
|
68
68
|
mutableSetOf()
|
|
69
69
|
|
|
70
|
+
// The attached device that each waiting connection is being reconnected to.
|
|
71
|
+
private val reconnectingDevices: MutableMap<DeviceConnection<AndroidUsbApduSender.Dependencies>, UsbDevice> =
|
|
72
|
+
ConcurrentHashMap()
|
|
73
|
+
|
|
70
74
|
private var discoveryJob: Job? = null
|
|
71
75
|
|
|
72
76
|
override fun startScan(): Flow<List<DiscoveryDevice>> {
|
|
@@ -77,8 +81,7 @@ internal class DefaultAndroidUsbTransport(
|
|
|
77
81
|
scope.launch {
|
|
78
82
|
while (isActive) {
|
|
79
83
|
loggerService.log(buildSimpleDebugLogInfo(TAG, "[startScan] isActive loop"))
|
|
80
|
-
|
|
81
|
-
scanStateFlow.value = usbDevices.toScannedDevices()
|
|
84
|
+
scanStateFlow.value = scanDevices()
|
|
82
85
|
loggerService.log(buildSimpleDebugLogInfo(TAG, "[startScan] scannedDevices=${scanStateFlow.value}"))
|
|
83
86
|
delay(scanDelay)
|
|
84
87
|
}
|
|
@@ -92,6 +95,30 @@ internal class DefaultAndroidUsbTransport(
|
|
|
92
95
|
discoveryJob = null
|
|
93
96
|
}
|
|
94
97
|
|
|
98
|
+
private fun scanDevices(): List<DiscoveryDevice> =
|
|
99
|
+
usbManager.deviceList.values.mapNotNull { usbDevice ->
|
|
100
|
+
usbDevice.toLedgerUsbDevice()?.toScannedDevice(uid = getDeviceId(usbDevice))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Gives the id of a USB device. Discovery and connect use the same id.
|
|
105
|
+
*
|
|
106
|
+
* - A device with an open connection gets the session id of that connection.
|
|
107
|
+
* When the device re-enumerates (for example, when an app change on the
|
|
108
|
+
* device triggers a disconnection/reconnection), Android gives it a new
|
|
109
|
+
* device number. The reconnection gives the new
|
|
110
|
+
* [UsbDevice] to the open connection, so the device keeps its session id.
|
|
111
|
+
* While the reconnection runs, the new [UsbDevice] already gets that id.
|
|
112
|
+
* - A device without a connection gets [generateSessionId]. [connect] gives
|
|
113
|
+
* the same value as the session id.
|
|
114
|
+
*/
|
|
115
|
+
private fun getDeviceId(usbDevice: UsbDevice): String =
|
|
116
|
+
findConnection(usbDevice)?.sessionId ?: generateSessionId(usbDevice)
|
|
117
|
+
|
|
118
|
+
private fun findConnection(usbDevice: UsbDevice): DeviceConnection<AndroidUsbApduSender.Dependencies>? =
|
|
119
|
+
usbConnections.values.firstOrNull { it.getApduSender().dependencies.usbDevice == usbDevice }
|
|
120
|
+
?: reconnectingDevices.entries.firstOrNull { it.value == usbDevice }?.key
|
|
121
|
+
|
|
95
122
|
override fun updateUsbState(state: UsbState) {
|
|
96
123
|
when (state) {
|
|
97
124
|
is UsbState.Detached -> {
|
|
@@ -152,6 +179,9 @@ internal class DefaultAndroidUsbTransport(
|
|
|
152
179
|
it.getApduSender()
|
|
153
180
|
.dependencies.ledgerUsbDevice.ledgerDevice == state.ledgerUsbDevice.ledgerDevice // we just find a similar device model since there is no way to uniquely identify a device between 2 connections
|
|
154
181
|
}.let { deviceConnection ->
|
|
182
|
+
if (deviceConnection != null) {
|
|
183
|
+
reconnectingDevices[deviceConnection] = usbDevice
|
|
184
|
+
}
|
|
155
185
|
scope.launch {
|
|
156
186
|
if (deviceConnection == null) {
|
|
157
187
|
loggerService.log(
|
|
@@ -177,6 +207,7 @@ internal class DefaultAndroidUsbTransport(
|
|
|
177
207
|
"Permission denied"
|
|
178
208
|
)
|
|
179
209
|
)
|
|
210
|
+
reconnectingDevices.remove(deviceConnection, usbDevice)
|
|
180
211
|
return@launch
|
|
181
212
|
}
|
|
182
213
|
loggerService.log(
|
|
@@ -207,11 +238,13 @@ internal class DefaultAndroidUsbTransport(
|
|
|
207
238
|
* a user interaction.
|
|
208
239
|
*/
|
|
209
240
|
apduSender.release()
|
|
241
|
+
reconnectingDevices.remove(deviceConnection, usbDevice)
|
|
210
242
|
return@launch
|
|
211
243
|
}
|
|
212
244
|
deviceConnection.handleDeviceConnected(apduSender)
|
|
213
245
|
usbConnectionsPendingReconnection.remove(deviceConnection)
|
|
214
246
|
usbConnections[deviceConnection.sessionId] = deviceConnection
|
|
247
|
+
reconnectingDevices.remove(deviceConnection, usbDevice)
|
|
215
248
|
}
|
|
216
249
|
}
|
|
217
250
|
}
|
|
@@ -286,7 +319,7 @@ internal class DefaultAndroidUsbTransport(
|
|
|
286
319
|
|
|
287
320
|
override suspend fun connect(discoveryDevice: DiscoveryDevice): InternalConnectionResult {
|
|
288
321
|
val usbDevice: UsbDevice? =
|
|
289
|
-
usbManager.deviceList.values.firstOrNull { it
|
|
322
|
+
usbManager.deviceList.values.firstOrNull { getDeviceId(it) == discoveryDevice.uid }
|
|
290
323
|
|
|
291
324
|
val ledgerUsbDevice = usbDevice?.toLedgerUsbDevice()
|
|
292
325
|
|
|
@@ -294,11 +327,7 @@ internal class DefaultAndroidUsbTransport(
|
|
|
294
327
|
InternalConnectionResult.ConnectionError(error = InternalConnectionResult.Failure.DeviceNotFound)
|
|
295
328
|
} else {
|
|
296
329
|
|
|
297
|
-
val existingConnection =
|
|
298
|
-
if (it.value.getApduSender().dependencies.usbDevice == usbDevice) it.value
|
|
299
|
-
else if (it.key == generateSessionId(usbDevice)) it.value
|
|
300
|
-
else null
|
|
301
|
-
}
|
|
330
|
+
val existingConnection = findConnection(usbDevice)
|
|
302
331
|
|
|
303
332
|
if (existingConnection != null) {
|
|
304
333
|
val connectedDevice =
|
|
@@ -343,6 +372,7 @@ internal class DefaultAndroidUsbTransport(
|
|
|
343
372
|
(it.getApduSender() as AndroidUsbApduSender).release()
|
|
344
373
|
usbConnections.remove(sessionId)
|
|
345
374
|
usbConnectionsPendingReconnection.remove(it)
|
|
375
|
+
reconnectingDevices.remove(it)
|
|
346
376
|
eventDispatcher.dispatch(TransportEvent.DeviceConnectionLost(sessionId))
|
|
347
377
|
},
|
|
348
378
|
coroutineDispatcher = coroutineDispatcher,
|
|
@@ -372,12 +402,8 @@ internal class DefaultAndroidUsbTransport(
|
|
|
372
402
|
usbConnectionsPendingReconnection.find { it.sessionId == deviceId }?.requestCloseConnection()
|
|
373
403
|
}
|
|
374
404
|
|
|
405
|
+
// The "usb_" prefix keeps the id unique across transports.
|
|
375
406
|
private fun generateSessionId(usbDevice: UsbDevice): String = "usb_${usbDevice.deviceId}"
|
|
376
407
|
}
|
|
377
408
|
|
|
378
|
-
private val POST_CONNECTION_DELAY = 200.milliseconds
|
|
379
|
-
|
|
380
|
-
private fun List<LedgerUsbDevice>.toScannedDevices(): List<DiscoveryDevice> =
|
|
381
|
-
this.map {
|
|
382
|
-
it.toScannedDevice()
|
|
383
|
-
}
|
|
409
|
+
private val POST_CONNECTION_DELAY = 200.milliseconds
|
|
@@ -15,16 +15,18 @@ import com.ledger.devicesdk.shared.androidMain.transport.usb.model.VendorId
|
|
|
15
15
|
import com.ledger.devicesdk.shared.androidMain.transport.usb.model.ProductId
|
|
16
16
|
import com.ledger.devicesdk.shared.androidMain.transport.usb.model.LedgerUsbDevice
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* [uid] is the id of the device for the client. It is not [LedgerUsbDevice.uid],
|
|
20
|
+
* which is the Android device number and changes when the device re-enumerates.
|
|
21
|
+
*/
|
|
22
|
+
internal fun LedgerUsbDevice.toScannedDevice(uid: String) =
|
|
19
23
|
DiscoveryDevice(
|
|
20
|
-
uid =
|
|
24
|
+
uid = uid,
|
|
21
25
|
name = this.name,
|
|
22
26
|
ledgerDevice = this.ledgerDevice,
|
|
23
27
|
connectivityType = ConnectivityType.Usb,
|
|
24
28
|
)
|
|
25
29
|
|
|
26
|
-
internal fun List<android.hardware.usb.UsbDevice>.toUsbDevices(): List<LedgerUsbDevice> = mapNotNull { it.toLedgerUsbDevice() }
|
|
27
|
-
|
|
28
30
|
internal fun android.hardware.usb.UsbDevice.toLedgerUsbDevice(): LedgerUsbDevice? {
|
|
29
31
|
val productId = ProductId(this.productId)
|
|
30
32
|
val vendorId = VendorId(this.vendorId)
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
package com.ledger.devicesdk.shared.androidMain.transport.usb
|
|
2
|
+
|
|
3
|
+
import android.hardware.usb.UsbConstants
|
|
4
|
+
import android.hardware.usb.UsbDevice
|
|
5
|
+
import android.hardware.usb.UsbEndpoint
|
|
6
|
+
import android.hardware.usb.UsbInterface
|
|
7
|
+
import android.hardware.usb.UsbManager
|
|
8
|
+
import com.ledger.devicesdk.shared.androidMain.transport.usb.model.UsbState
|
|
9
|
+
import com.ledger.devicesdk.shared.androidMain.transport.usb.utils.toLedgerUsbDevice
|
|
10
|
+
import com.ledger.devicesdk.shared.api.discovery.DiscoveryDevice
|
|
11
|
+
import com.ledger.devicesdk.shared.internal.connection.InternalConnectionResult
|
|
12
|
+
import com.ledger.devicesdk.shared.internal.event.SdkEventDispatcher
|
|
13
|
+
import com.ledger.devicesdk.shared.internal.transport.TransportEvent
|
|
14
|
+
import io.mockk.every
|
|
15
|
+
import io.mockk.mockk
|
|
16
|
+
import io.mockk.verify
|
|
17
|
+
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
18
|
+
import kotlinx.coroutines.flow.first
|
|
19
|
+
import kotlinx.coroutines.test.StandardTestDispatcher
|
|
20
|
+
import kotlinx.coroutines.test.TestScope
|
|
21
|
+
import kotlinx.coroutines.test.advanceTimeBy
|
|
22
|
+
import kotlinx.coroutines.test.runCurrent
|
|
23
|
+
import kotlinx.coroutines.test.runTest
|
|
24
|
+
import kotlin.test.Test
|
|
25
|
+
import kotlin.test.assertEquals
|
|
26
|
+
import kotlin.test.assertIs
|
|
27
|
+
import kotlin.time.Duration.Companion.milliseconds
|
|
28
|
+
import kotlin.time.Duration.Companion.seconds
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Android gives a USB device a new device number when it re-enumerates, for
|
|
32
|
+
* example when an app change on the device triggers a disconnection/reconnection.
|
|
33
|
+
* These tests check that discovery and connect give the device the same id,
|
|
34
|
+
* before and after a re-enumeration.
|
|
35
|
+
*/
|
|
36
|
+
@OptIn(ExperimentalCoroutinesApi::class)
|
|
37
|
+
class DefaultAndroidUsbTransportTest {
|
|
38
|
+
private val androidDevices = mutableMapOf<String, UsbDevice>()
|
|
39
|
+
|
|
40
|
+
private val usbManager = mockk<UsbManager> {
|
|
41
|
+
every { deviceList } answers { HashMap(androidDevices) }
|
|
42
|
+
every { hasPermission(any<UsbDevice>()) } returns true
|
|
43
|
+
every { openDevice(any()) } answers { mockk(relaxed = true) }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
private val eventDispatcher = mockk<SdkEventDispatcher>(relaxed = true)
|
|
47
|
+
|
|
48
|
+
private val beforeAppChange = nanoX(number = 1005)
|
|
49
|
+
private val afterAppChange = nanoX(number = 1006)
|
|
50
|
+
|
|
51
|
+
@Test
|
|
52
|
+
fun `GIVEN a device without connection WHEN discovered THEN its id is usb_number`() = runTest {
|
|
53
|
+
val transport = createTransport()
|
|
54
|
+
plugIn(beforeAppChange)
|
|
55
|
+
|
|
56
|
+
assertEquals("usb_1005", scan(transport).uid)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
@Test
|
|
60
|
+
fun `GIVEN a discovered device WHEN connected THEN the session id is the discovered id`() = runTest {
|
|
61
|
+
val transport = createTransport()
|
|
62
|
+
plugIn(beforeAppChange)
|
|
63
|
+
|
|
64
|
+
assertEquals("usb_1005", connect(transport, scan(transport)).sessionId)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@Test
|
|
68
|
+
fun `GIVEN a connected device WHEN it re-enumerates THEN discovery gives the session id`() = runTest {
|
|
69
|
+
val transport = createTransport()
|
|
70
|
+
plugIn(beforeAppChange)
|
|
71
|
+
connect(transport, scan(transport))
|
|
72
|
+
|
|
73
|
+
reEnumerate(transport, from = beforeAppChange, to = afterAppChange)
|
|
74
|
+
|
|
75
|
+
assertEquals("usb_1005", scan(transport).uid)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@Test
|
|
79
|
+
fun `GIVEN a device that re-enumerates WHEN discovered before the reconnection ends THEN discovery gives the session id`() = runTest {
|
|
80
|
+
val transport = createTransport()
|
|
81
|
+
plugIn(beforeAppChange)
|
|
82
|
+
connect(transport, scan(transport))
|
|
83
|
+
|
|
84
|
+
startReEnumeration(transport, from = beforeAppChange, to = afterAppChange)
|
|
85
|
+
|
|
86
|
+
assertEquals("usb_1005", scan(transport).uid)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@Test
|
|
90
|
+
fun `GIVEN a device that re-enumerates WHEN connected before the reconnection ends THEN the waiting connection is used`() = runTest {
|
|
91
|
+
val transport = createTransport()
|
|
92
|
+
plugIn(beforeAppChange)
|
|
93
|
+
connect(transport, scan(transport))
|
|
94
|
+
startReEnumeration(transport, from = beforeAppChange, to = afterAppChange)
|
|
95
|
+
|
|
96
|
+
val result = connect(transport, scan(transport))
|
|
97
|
+
|
|
98
|
+
assertEquals("usb_1005", result.sessionId)
|
|
99
|
+
// One opening for the first connection, one for the reconnection: connect opens no other.
|
|
100
|
+
verify(exactly = 2) { usbManager.openDevice(any()) }
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
@Test
|
|
104
|
+
fun `GIVEN a re-enumerated device WHEN connected THEN the open connection is used`() = runTest {
|
|
105
|
+
val transport = createTransport()
|
|
106
|
+
plugIn(beforeAppChange)
|
|
107
|
+
connect(transport, scan(transport))
|
|
108
|
+
reEnumerate(transport, from = beforeAppChange, to = afterAppChange)
|
|
109
|
+
|
|
110
|
+
val result = connect(transport, scan(transport))
|
|
111
|
+
|
|
112
|
+
assertEquals("usb_1005", result.sessionId)
|
|
113
|
+
// One opening for the first connection, one for the reconnection.
|
|
114
|
+
verify(exactly = 2) { usbManager.openDevice(any()) }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
@Test
|
|
118
|
+
fun `GIVEN a re-enumerated device WHEN disconnected THEN its session ends`() = runTest {
|
|
119
|
+
val transport = createTransport()
|
|
120
|
+
plugIn(beforeAppChange)
|
|
121
|
+
connect(transport, scan(transport))
|
|
122
|
+
reEnumerate(transport, from = beforeAppChange, to = afterAppChange)
|
|
123
|
+
|
|
124
|
+
transport.disconnect("usb_1005")
|
|
125
|
+
runCurrent()
|
|
126
|
+
|
|
127
|
+
verify(exactly = 1) {
|
|
128
|
+
eventDispatcher.dispatch(TransportEvent.DeviceConnectionLost("usb_1005"))
|
|
129
|
+
}
|
|
130
|
+
assertEquals("usb_1006", scan(transport).uid)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private fun TestScope.createTransport() = DefaultAndroidUsbTransport(
|
|
134
|
+
application = mockk(relaxed = true),
|
|
135
|
+
usbManager = usbManager,
|
|
136
|
+
permissionRequester = { _, _, _ -> },
|
|
137
|
+
eventDispatcher = eventDispatcher,
|
|
138
|
+
loggerService = { },
|
|
139
|
+
scanDelay = 500.milliseconds,
|
|
140
|
+
coroutineDispatcher = StandardTestDispatcher(testScheduler),
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
private suspend fun TestScope.scan(transport: DefaultAndroidUsbTransport): DiscoveryDevice {
|
|
144
|
+
val devices = transport.startScan().first { it.isNotEmpty() }
|
|
145
|
+
transport.stopScan()
|
|
146
|
+
return devices.single()
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private suspend fun connect(
|
|
150
|
+
transport: DefaultAndroidUsbTransport,
|
|
151
|
+
device: DiscoveryDevice,
|
|
152
|
+
): InternalConnectionResult.Connected =
|
|
153
|
+
assertIs<InternalConnectionResult.Connected>(transport.connect(device))
|
|
154
|
+
|
|
155
|
+
/** Discovery reads the device list of Android, so plugging in a device needs no event. */
|
|
156
|
+
private fun plugIn(device: UsbDevice) {
|
|
157
|
+
androidDevices[device.deviceName] = device
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* What Android does when an app change on the device triggers a
|
|
162
|
+
* disconnection/reconnection: it detaches the device, then attaches it
|
|
163
|
+
* again with a new device number.
|
|
164
|
+
*/
|
|
165
|
+
private fun TestScope.reEnumerate(
|
|
166
|
+
transport: DefaultAndroidUsbTransport,
|
|
167
|
+
from: UsbDevice,
|
|
168
|
+
to: UsbDevice,
|
|
169
|
+
) {
|
|
170
|
+
startReEnumeration(transport, from, to)
|
|
171
|
+
// The transport gives the new device to the waiting connection after
|
|
172
|
+
// 200 ms, and would end the session after 5 s.
|
|
173
|
+
advanceTimeBy(1.seconds)
|
|
174
|
+
runCurrent()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* The first part of [reEnumerate]: Android has attached the new device, and
|
|
179
|
+
* the transport has not given it to the waiting connection yet.
|
|
180
|
+
*/
|
|
181
|
+
private fun TestScope.startReEnumeration(
|
|
182
|
+
transport: DefaultAndroidUsbTransport,
|
|
183
|
+
from: UsbDevice,
|
|
184
|
+
to: UsbDevice,
|
|
185
|
+
) {
|
|
186
|
+
androidDevices.remove(from.deviceName)
|
|
187
|
+
transport.updateUsbState(UsbState.Detached(from.toLedgerUsbDevice()!!))
|
|
188
|
+
// The open connection now waits for its device.
|
|
189
|
+
runCurrent()
|
|
190
|
+
|
|
191
|
+
androidDevices[to.deviceName] = to
|
|
192
|
+
transport.updateUsbState(UsbState.Attached(to.toLedgerUsbDevice()!!))
|
|
193
|
+
runCurrent()
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private fun nanoX(number: Int): UsbDevice {
|
|
197
|
+
val outEndpoint = mockk<UsbEndpoint> { every { direction } returns UsbConstants.USB_DIR_OUT }
|
|
198
|
+
val inEndpoint = mockk<UsbEndpoint> { every { direction } returns UsbConstants.USB_DIR_IN }
|
|
199
|
+
val usbInterface = mockk<UsbInterface> {
|
|
200
|
+
every { endpointCount } returns 2
|
|
201
|
+
every { getEndpoint(0) } returns outEndpoint
|
|
202
|
+
every { getEndpoint(1) } returns inEndpoint
|
|
203
|
+
}
|
|
204
|
+
return mockk {
|
|
205
|
+
every { deviceId } returns number
|
|
206
|
+
every { deviceName } returns "/dev/bus/usb/001/$number"
|
|
207
|
+
every { vendorId } returns 0x2c97
|
|
208
|
+
every { productId } returns 0x4011 // Nano X
|
|
209
|
+
every { getInterface(0) } returns usbInterface
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -136,7 +136,7 @@ class DeviceConnectionStateMachineTest {
|
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
@Test
|
|
139
|
-
fun `GIVEN the device connection state machine WHEN an APDU
|
|
139
|
+
fun `GIVEN the device connection state machine WHEN an APDU triggers a disconnection THEN the machine waits for reconnection and recovers`() =
|
|
140
140
|
runTest {
|
|
141
141
|
var sendApduCalled: ByteArray? = null
|
|
142
142
|
var sendApduResult: SendApduResult? = null
|
|
@@ -257,7 +257,7 @@ class DeviceConnectionStateMachineTest {
|
|
|
257
257
|
}
|
|
258
258
|
|
|
259
259
|
@Test
|
|
260
|
-
fun `GIVEN the device connection state machine WHEN
|
|
260
|
+
fun `GIVEN the device connection state machine WHEN the expected disconnection does not occur and the device is busy THEN the machine retries and moves to Connected`() =
|
|
261
261
|
runTest {
|
|
262
262
|
var sendApduCalled: ByteArray? = null
|
|
263
263
|
var sendApduResult: SendApduResult? = null
|
|
@@ -319,7 +319,7 @@ class DeviceConnectionStateMachineTest {
|
|
|
319
319
|
|
|
320
320
|
|
|
321
321
|
@Test
|
|
322
|
-
fun `GIVEN the device connection state machine WHEN an APDU
|
|
322
|
+
fun `GIVEN the device connection state machine WHEN an APDU triggers a disconnection THEN the next APDU is queued until reconnection`() =
|
|
323
323
|
runTest {
|
|
324
324
|
val sendApduCalled: MutableList<ByteArray> = mutableListOf()
|
|
325
325
|
var terminated = false
|
|
@@ -43,7 +43,7 @@ class DeviceConnectionTest {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
@Test
|
|
46
|
-
fun `GIVEN
|
|
46
|
+
fun `GIVEN an APDU that triggers disconnection WHEN a reconnection occurs THEN the second apduSender is used and the result is obtained`() =
|
|
47
47
|
runTest {
|
|
48
48
|
apdusTriggeringDisconnection.forEach { apduTriggeringDisconnection ->
|
|
49
49
|
val apduSender1 = MockedApduSender()
|
package/lib/cjs/package.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"dependencies": {
|
|
3
|
-
"@sentry/minimal": "catalog:",
|
|
4
3
|
"purify-ts": "catalog:",
|
|
5
4
|
"uuid": "catalog:"
|
|
6
5
|
},
|
|
@@ -40,7 +39,7 @@
|
|
|
40
39
|
"name": "@ledgerhq/device-transport-kit-react-native-hid",
|
|
41
40
|
"nativePackage": true,
|
|
42
41
|
"peerDependencies": {
|
|
43
|
-
"@ledgerhq/device-management-kit": "
|
|
42
|
+
"@ledgerhq/device-management-kit": "^1.13.0",
|
|
44
43
|
"react-native": ">0.74.1",
|
|
45
44
|
"rxjs": "catalog:"
|
|
46
45
|
},
|
|
@@ -66,5 +65,5 @@
|
|
|
66
65
|
"watch:builds": "pnpm ldmk-tool watch --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json",
|
|
67
66
|
"watch:types": "concurrently \"tsc --watch -p tsconfig.prod.json\" \"tsc-alias --watch -p tsconfig.prod.json\""
|
|
68
67
|
},
|
|
69
|
-
"version": "1.0
|
|
68
|
+
"version": "1.1.0"
|
|
70
69
|
}
|
package/lib/esm/package.json
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
{
|
|
2
2
|
"dependencies": {
|
|
3
|
-
"@sentry/minimal": "catalog:",
|
|
4
3
|
"purify-ts": "catalog:",
|
|
5
4
|
"uuid": "catalog:"
|
|
6
5
|
},
|
|
@@ -40,7 +39,7 @@
|
|
|
40
39
|
"name": "@ledgerhq/device-transport-kit-react-native-hid",
|
|
41
40
|
"nativePackage": true,
|
|
42
41
|
"peerDependencies": {
|
|
43
|
-
"@ledgerhq/device-management-kit": "
|
|
42
|
+
"@ledgerhq/device-management-kit": "^1.13.0",
|
|
44
43
|
"react-native": ">0.74.1",
|
|
45
44
|
"rxjs": "catalog:"
|
|
46
45
|
},
|
|
@@ -66,5 +65,5 @@
|
|
|
66
65
|
"watch:builds": "pnpm ldmk-tool watch --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json",
|
|
67
66
|
"watch:types": "concurrently \"tsc --watch -p tsconfig.prod.json\" \"tsc-alias --watch -p tsconfig.prod.json\""
|
|
68
67
|
},
|
|
69
|
-
"version": "1.0
|
|
68
|
+
"version": "1.1.0"
|
|
70
69
|
}
|