@ledgerhq/device-transport-kit-react-native-hid 1.0.0 → 1.0.2

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.
@@ -17,11 +17,14 @@ import com.ledger.devicesdk.shared.androidMain.transport.usb.controller.ACTION_U
17
17
  import com.ledger.devicesdk.shared.androidMain.transport.usb.controller.UsbAttachedReceiverController
18
18
  import com.ledger.devicesdk.shared.androidMain.transport.usb.controller.UsbDetachedReceiverController
19
19
  import com.ledger.devicesdk.shared.androidMain.transport.usb.controller.UsbPermissionReceiver
20
+ import com.ledger.devicesdk.shared.api.apdu.SendApduFailureReason
21
+ import com.ledger.devicesdk.shared.api.apdu.SendApduResult
20
22
  import com.ledger.devicesdk.shared.api.discovery.DiscoveryDevice
21
23
  import com.ledger.devicesdk.shared.internal.connection.InternalConnectedDevice
22
24
  import com.ledger.devicesdk.shared.internal.connection.InternalConnectionResult
23
25
  import com.ledger.devicesdk.shared.internal.event.SdkEventDispatcher
24
26
  import com.ledger.devicesdk.shared.internal.service.logger.LoggerService
27
+ import com.ledger.devicesdk.shared.internal.service.logger.buildSimpleDebugLogInfo
25
28
  import com.ledger.devicesdk.shared.internal.transport.TransportEvent
26
29
  import kotlinx.coroutines.CoroutineScope
27
30
  import kotlinx.coroutines.Dispatchers
@@ -34,6 +37,8 @@ import kotlin.random.Random
34
37
  import kotlin.time.Duration
35
38
  import kotlin.time.Duration.Companion.milliseconds
36
39
 
40
+ private val TAG = "TransportHidModule"
41
+
37
42
  class TransportHidModule(
38
43
  private val reactContext: ReactApplicationContext,
39
44
  private val coroutineScope: CoroutineScope
@@ -133,12 +138,18 @@ class TransportHidModule(
133
138
  transport?.stopScan()
134
139
  }
135
140
 
136
- private var discoveryCount = 0
141
+ private var activeScanCount = 0
137
142
 
138
143
  @ReactMethod
139
144
  fun startScan(promise: Promise) {
140
- discoveryCount += 1
141
- if (discoveryCount > 1) {
145
+ loggerService.log(
146
+ buildSimpleDebugLogInfo(TAG, "[startScan] called")
147
+ )
148
+ activeScanCount += 1
149
+ if (activeScanCount > 1) {
150
+ loggerService.log(
151
+ buildSimpleDebugLogInfo(TAG, "[startScan] already scanning")
152
+ )
142
153
  promise.resolve(null)
143
154
  return
144
155
  }
@@ -156,16 +167,34 @@ class TransportHidModule(
156
167
 
157
168
  @ReactMethod
158
169
  fun stopScan(promise: Promise) {
159
- discoveryCount -= 1
160
- if (discoveryCount > 0) {
161
- promise.resolve(null)
162
- return
163
- }
164
- try {
165
- transport!!.stopScan()
166
- promise.resolve(null)
167
- } catch (e: Exception) {
168
- promise.reject(e);
170
+ loggerService.log(
171
+ buildSimpleDebugLogInfo(TAG, "[stopScan] called, activeScanCount=$activeScanCount")
172
+ )
173
+
174
+ when(activeScanCount) {
175
+ 0 -> {
176
+ loggerService.log(buildSimpleDebugLogInfo(TAG, "[stopScan] no active scan"))
177
+ promise.resolve(null)
178
+ }
179
+ 1 -> {
180
+ try {
181
+ transport!!.stopScan()
182
+ promise.resolve(null)
183
+ } catch (e: Exception) {
184
+ promise.reject(e);
185
+ }
186
+ activeScanCount = 0
187
+ }
188
+ else -> {
189
+ loggerService.log(
190
+ buildSimpleDebugLogInfo(
191
+ TAG,
192
+ "[stopScan] still scanning because there are active listeners"
193
+ )
194
+ )
195
+ activeScanCount -= 1
196
+ promise.resolve(null)
197
+ }
169
198
  }
170
199
  }
171
200
 
@@ -217,7 +246,9 @@ class TransportHidModule(
217
246
  try {
218
247
  val device = connectedDevices.firstOrNull() { it.id == sessionId }
219
248
  if (device == null) {
220
- promise.reject(Exception("[TransportHidModule][sendApdu] Device not found"))
249
+ promise.resolve(
250
+ SendApduResult.Failure(SendApduFailureReason.DeviceNotFound).toWritableMap()
251
+ )
221
252
  return
222
253
  }
223
254
  coroutineScope.launch {
@@ -43,8 +43,11 @@ import kotlinx.coroutines.flow.shareIn
43
43
  import kotlinx.coroutines.isActive
44
44
  import kotlinx.coroutines.launch
45
45
  import kotlin.time.Duration
46
+ import kotlin.time.Duration.Companion.milliseconds
46
47
  import kotlin.time.Duration.Companion.seconds
47
48
 
49
+ private val TAG = "DefaultAndroidUsbTransport"
50
+
48
51
  internal class DefaultAndroidUsbTransport(
49
52
  private val application: Application,
50
53
  private val usbManager: UsbManager,
@@ -67,22 +70,16 @@ internal class DefaultAndroidUsbTransport(
67
70
  private var discoveryJob: Job? = null
68
71
 
69
72
  override fun startScan(): Flow<List<DiscoveryDevice>> {
73
+ loggerService.log(buildSimpleDebugLogInfo(TAG, "[startScan] called"))
70
74
  val scanStateFlow = MutableStateFlow<List<DiscoveryDevice>>(emptyList())
71
75
  discoveryJob?.cancel()
72
76
  discoveryJob =
73
77
  scope.launch {
74
78
  while (isActive) {
75
- val usbDevices = usbManager.deviceList.values.toList()
76
- val devices =
77
- usbDevices
78
- .filter { device ->
79
- usbConnections.filter {
80
- device == it.value.getApduSender().dependencies.usbDevice
81
- }.isEmpty()
82
- }.toUsbDevices()
83
-
84
- scanStateFlow.value = devices.toScannedDevices()
85
-
79
+ loggerService.log(buildSimpleDebugLogInfo(TAG, "[startScan] isActive loop"))
80
+ val usbDevices = usbManager.deviceList.values.toList().toUsbDevices()
81
+ scanStateFlow.value = usbDevices.toScannedDevices()
82
+ loggerService.log(buildSimpleDebugLogInfo(TAG, "[startScan] scannedDevices=${scanStateFlow.value}"))
86
83
  delay(scanDelay)
87
84
  }
88
85
  }
@@ -90,6 +87,7 @@ internal class DefaultAndroidUsbTransport(
90
87
  }
91
88
 
92
89
  override fun stopScan() {
90
+ loggerService.log(buildSimpleDebugLogInfo(TAG, "[stopScan] called"))
93
91
  discoveryJob?.cancel()
94
92
  discoveryJob = null
95
93
  }
@@ -199,6 +197,7 @@ internal class DefaultAndroidUsbTransport(
199
197
  request = UsbRequest(),
200
198
  loggerService = loggerService
201
199
  )
200
+ delay(POST_CONNECTION_DELAY)
202
201
 
203
202
  if (!usbConnectionsPendingReconnection.contains(deviceConnection)) {
204
203
  /**
@@ -294,6 +293,27 @@ internal class DefaultAndroidUsbTransport(
294
293
  return if (usbDevice == null || ledgerUsbDevice == null) {
295
294
  InternalConnectionResult.ConnectionError(error = InternalConnectionResult.Failure.DeviceNotFound)
296
295
  } else {
296
+
297
+ val existingConnection = usbConnections.firstNotNullOfOrNull {
298
+ if (it.value.getApduSender().dependencies.usbDevice == usbDevice) it.value
299
+ else if (it.key == generateSessionId(usbDevice)) it.value
300
+ else null
301
+ }
302
+
303
+ if (existingConnection != null) {
304
+ val connectedDevice =
305
+ InternalConnectedDevice(
306
+ existingConnection.sessionId,
307
+ discoveryDevice.name,
308
+ discoveryDevice.ledgerDevice,
309
+ discoveryDevice.connectivityType,
310
+ sendApduFn = { apdu: ByteArray, triggersDisconnection: Boolean, abortTimeoutDuration: Duration ->
311
+ existingConnection.requestSendApdu(apdu, triggersDisconnection, abortTimeoutDuration)
312
+ }
313
+ )
314
+ return InternalConnectionResult.Connected(device = connectedDevice, sessionId = existingConnection.sessionId)
315
+ }
316
+
297
317
  val permissionResult = checkOrRequestPermission(usbDevice)
298
318
  if (permissionResult is PermissionResult.Denied) {
299
319
  return permissionResult.connectionError
@@ -312,6 +332,7 @@ internal class DefaultAndroidUsbTransport(
312
332
  request = UsbRequest(),
313
333
  loggerService = loggerService,
314
334
  )
335
+ delay(POST_CONNECTION_DELAY)
315
336
 
316
337
  val deviceConnection = DeviceConnection(
317
338
  sessionId = sessionId,
@@ -354,6 +375,8 @@ internal class DefaultAndroidUsbTransport(
354
375
  private fun generateSessionId(usbDevice: UsbDevice): String = "usb_${usbDevice.deviceId}"
355
376
  }
356
377
 
378
+ private val POST_CONNECTION_DELAY = 200.milliseconds
379
+
357
380
  private fun List<LedgerUsbDevice>.toScannedDevices(): List<DiscoveryDevice> =
358
381
  this.map {
359
382
  it.toScannedDevice()
@@ -1,2 +1,2 @@
1
- "use strict";var c=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var m=(e,t)=>{for(var r in t)c(e,r,{get:t[r],enumerable:!0})},f=(e,t,r,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of l(t))!D.call(e,i)&&i!==r&&c(e,i,{get:()=>t[i],enumerable:!(s=v(t,i))||s.enumerable});return e};var y=e=>f(c({},"__esModule",{value:!0}),e);var b={};m(b,{mapNativeConnectionResultToConnectionResult:()=>R,mapNativeDeviceConnectionLostToDeviceDisconnected:()=>L,mapNativeDiscoveryDeviceToTransportDiscoveredDevice:()=>N,mapNativeLedgerDeviceToDeviceModel:()=>a,mapNativeSendApduResultToSendApduResult:()=>T,mapNativeTransportLogToLog:()=>g});module.exports=y(b);var n=require("@ledgerhq/device-management-kit"),o=require("purify-ts"),d=require("../helpers/base64Utils"),p=require("../transport/Errors"),u=require("../transport/rnHidTransportIdentifier");function a(e,t){return t.filterDeviceModels({usbProductId:Number.parseInt(e.usbProductIdMask,16)})[0]??null}function N(e,t){const r=a(e.ledgerDevice,t);return r==null?null:{id:e.uid,deviceModel:r,transport:u.TRANSPORT_IDENTIFIER,name:e.name}}function g(e){let t;switch(e.level){case"error":t=n.LogLevel.Error;break;case"warning":t=n.LogLevel.Warning;break;case"info":t=n.LogLevel.Info;break;case"debug":t=n.LogLevel.Debug;break;default:I(e.level),t=n.LogLevel.Info;break}return[t,e.message,{tag:e.tag,data:e.jsonPayload,timestamp:Number.parseInt(e.timestamp,10)}]}function I(e){throw new Error("Unexpected object: "+e)}function R(e,t){if(e.success){const r=a(e.ledgerDevice,t);return r?(0,o.Right)({sessionId:e.sessionId,transportDeviceModel:r}):(0,o.Left)(new n.OpeningConnectionError(`Could not find device model for the connected device with usbProductIdMask: ${e.ledgerDevice.usbProductIdMask}`))}else return(0,o.Left)(new n.OpeningConnectionError(e.error))}function T(e){if(e.success){const t=(0,d.base64ToUint8Array)(e.apdu),r=n.FramerUtils.getFirstBytesFrom(t,t.length-2),s=n.FramerUtils.getLastBytesFrom(t,2);return(0,o.Right)(new n.ApduResponse({data:r,statusCode:s}))}else return e.error==="SendApduTimeout"?(0,o.Left)(new n.SendApduTimeoutError("Abort timeout")):e.error==="EmptyResponse"?(0,o.Left)(new n.SendApduEmptyResponseError("Empty response")):e.error==="DeviceDisconnected"?(0,o.Left)(new n.DeviceDisconnectedWhileSendingError("Device disconnected")):(0,o.Left)(new p.HidTransportSendApduUnknownError(e.error))}function L(e){return{sessionId:e.id}}0&&(module.exports={mapNativeConnectionResultToConnectionResult,mapNativeDeviceConnectionLostToDeviceDisconnected,mapNativeDiscoveryDeviceToTransportDiscoveredDevice,mapNativeLedgerDeviceToDeviceModel,mapNativeSendApduResultToSendApduResult,mapNativeTransportLogToLog});
1
+ "use strict";var s=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var m=(e,t)=>{for(var o in t)s(e,o,{get:t[o],enumerable:!0})},f=(e,t,o,c)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of l(t))!D.call(e,i)&&i!==o&&s(e,i,{get:()=>t[i],enumerable:!(c=v(t,i))||c.enumerable});return e};var N=e=>f(s({},"__esModule",{value:!0}),e);var b={};m(b,{mapNativeConnectionResultToConnectionResult:()=>R,mapNativeDeviceConnectionLostToDeviceDisconnected:()=>L,mapNativeDiscoveryDeviceToTransportDiscoveredDevice:()=>y,mapNativeLedgerDeviceToDeviceModel:()=>a,mapNativeSendApduResultToSendApduResult:()=>T,mapNativeTransportLogToLog:()=>g});module.exports=N(b);var n=require("@ledgerhq/device-management-kit"),r=require("purify-ts"),d=require("../helpers/base64Utils"),u=require("../transport/Errors"),p=require("../transport/rnHidTransportIdentifier");function a(e,t){return t.filterDeviceModels({usbProductId:Number.parseInt(e.usbProductIdMask,16)})[0]??null}function y(e,t){const o=a(e.ledgerDevice,t);return o==null?null:{id:e.uid,deviceModel:o,transport:p.TRANSPORT_IDENTIFIER,name:e.name}}function g(e){let t;switch(e.level){case"error":t=n.LogLevel.Error;break;case"warning":t=n.LogLevel.Warning;break;case"info":t=n.LogLevel.Info;break;case"debug":t=n.LogLevel.Debug;break;default:I(e.level),t=n.LogLevel.Info;break}return[t,e.message,{tag:e.tag,data:e.jsonPayload,timestamp:Number.parseInt(e.timestamp,10)}]}function I(e){throw new Error("Unexpected object: "+e)}function R(e,t){if(e.success){const o=a(e.ledgerDevice,t);return o?(0,r.Right)({sessionId:e.sessionId,transportDeviceModel:o}):(0,r.Left)(new n.OpeningConnectionError(`Could not find device model for the connected device with usbProductIdMask: ${e.ledgerDevice.usbProductIdMask}`))}else return(0,r.Left)(new n.OpeningConnectionError(e.error))}function T(e){if(e.success){const t=(0,d.base64ToUint8Array)(e.apdu),o=n.FramerUtils.getFirstBytesFrom(t,t.length-2),c=n.FramerUtils.getLastBytesFrom(t,2);return(0,r.Right)(new n.ApduResponse({data:o,statusCode:c}))}else switch(e.error){case"SendApduTimeout":return(0,r.Left)(new n.SendApduTimeoutError("Abort timeout"));case"EmptyResponse":return(0,r.Left)(new n.SendApduEmptyResponseError("Empty response"));case"DeviceDisconnected":return(0,r.Left)(new n.DeviceDisconnectedWhileSendingError("Device disconnected"));case"DeviceNotFound":case"NoUsbEndpointFound":return(0,r.Left)(new n.DeviceDisconnectedBeforeSendingApdu);default:return(0,r.Left)(new u.HidTransportSendApduUnknownError(e.error))}}function L(e){return{sessionId:e.id}}0&&(module.exports={mapNativeConnectionResultToConnectionResult,mapNativeDeviceConnectionLostToDeviceDisconnected,mapNativeDiscoveryDeviceToTransportDiscoveredDevice,mapNativeLedgerDeviceToDeviceModel,mapNativeSendApduResultToSendApduResult,mapNativeTransportLogToLog});
2
2
  //# sourceMappingURL=mapper.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/api/bridge/mapper.ts"],
4
- "sourcesContent": ["import {\n ApduResponse,\n DeviceDisconnectedWhileSendingError,\n type DeviceModelDataSource,\n FramerUtils,\n LogLevel,\n type LogParams,\n OpeningConnectionError,\n SendApduEmptyResponseError,\n type SendApduResult,\n SendApduTimeoutError,\n type TransportDeviceModel,\n type TransportDiscoveredDevice,\n} from \"@ledgerhq/device-management-kit\";\nimport { Left, Right } from \"purify-ts\";\n\nimport { base64ToUint8Array } from \"@api/helpers/base64Utils\";\nimport { HidTransportSendApduUnknownError } from \"@api/transport/Errors\";\nimport { TRANSPORT_IDENTIFIER } from \"@api/transport/rnHidTransportIdentifier\";\nimport {\n type InternalConnectionResult,\n type InternalDeviceDisconnected,\n} from \"@api/transport/types\";\n\nimport {\n type NativeDeviceConnectionLost,\n type NativeDiscoveryDevice,\n type NativeInternalConnectionResult,\n type NativeLedgerDevice,\n type NativeLog,\n type NativeSendApduResult,\n} from \"./types\";\n\nexport function mapNativeLedgerDeviceToDeviceModel(\n nativeLedgerDevice: NativeLedgerDevice,\n deviceModelDataSource: DeviceModelDataSource,\n): TransportDeviceModel | null {\n return (\n deviceModelDataSource.filterDeviceModels({\n usbProductId: Number.parseInt(nativeLedgerDevice.usbProductIdMask, 16),\n })[0] ?? null\n );\n}\n\nexport function mapNativeDiscoveryDeviceToTransportDiscoveredDevice(\n nativeDevice: NativeDiscoveryDevice,\n deviceModelDataSource: DeviceModelDataSource,\n): TransportDiscoveredDevice | null {\n const deviceModel = mapNativeLedgerDeviceToDeviceModel(\n nativeDevice.ledgerDevice,\n deviceModelDataSource,\n );\n if (deviceModel == null) return null;\n\n return {\n id: nativeDevice.uid,\n deviceModel,\n transport: TRANSPORT_IDENTIFIER,\n name: nativeDevice.name,\n };\n}\n\nexport function mapNativeTransportLogToLog(log: NativeLog): LogParams {\n let level: LogLevel;\n switch (log.level) {\n case \"error\":\n level = LogLevel.Error;\n break;\n case \"warning\":\n level = LogLevel.Warning;\n break;\n case \"info\":\n level = LogLevel.Info;\n break;\n case \"debug\":\n level = LogLevel.Debug;\n break;\n default:\n assertNever(log.level);\n level = LogLevel.Info;\n break;\n }\n\n return [\n level,\n log.message,\n {\n tag: log.tag,\n data: log.jsonPayload,\n timestamp: Number.parseInt(log.timestamp, 10),\n },\n ];\n}\n\nfunction assertNever(x: never) {\n throw new Error(\"Unexpected object: \" + x);\n}\n\nexport function mapNativeConnectionResultToConnectionResult(\n result: NativeInternalConnectionResult,\n deviceModelDataSource: DeviceModelDataSource,\n): InternalConnectionResult {\n if (result.success) {\n const transportDeviceModel = mapNativeLedgerDeviceToDeviceModel(\n result.ledgerDevice,\n deviceModelDataSource,\n );\n if (!transportDeviceModel)\n return Left(\n new OpeningConnectionError(\n `Could not find device model for the connected device with usbProductIdMask: ${result.ledgerDevice.usbProductIdMask}`,\n ),\n );\n return Right({ sessionId: result.sessionId, transportDeviceModel });\n } else {\n return Left(new OpeningConnectionError(result.error));\n }\n}\n\nexport function mapNativeSendApduResultToSendApduResult(\n result: NativeSendApduResult,\n): SendApduResult {\n if (result.success) {\n const responseBytes = base64ToUint8Array(result.apdu);\n const data = FramerUtils.getFirstBytesFrom(\n responseBytes,\n responseBytes.length - 2,\n );\n const statusCode = FramerUtils.getLastBytesFrom(responseBytes, 2);\n return Right(new ApduResponse({ data, statusCode }));\n } else if (result.error === \"SendApduTimeout\") {\n return Left(new SendApduTimeoutError(\"Abort timeout\"));\n } else if (result.error === \"EmptyResponse\") {\n return Left(new SendApduEmptyResponseError(\"Empty response\"));\n } else if (result.error === \"DeviceDisconnected\") {\n return Left(new DeviceDisconnectedWhileSendingError(\"Device disconnected\"));\n } else {\n return Left(new HidTransportSendApduUnknownError(result.error));\n }\n}\n\nexport function mapNativeDeviceConnectionLostToDeviceDisconnected(\n nativeDeviceConnectionLost: NativeDeviceConnectionLost,\n): InternalDeviceDisconnected {\n return {\n sessionId: nativeDeviceConnectionLost.id,\n };\n}\n"],
5
- "mappings": "yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,iDAAAE,EAAA,sDAAAC,EAAA,wDAAAC,EAAA,uCAAAC,EAAA,4CAAAC,EAAA,+BAAAC,IAAA,eAAAC,EAAAR,GAAA,IAAAS,EAaO,2CACPC,EAA4B,qBAE5BC,EAAm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,EAC9CG,EAAO,cAAY,kBACvBD,EACAA,EAAc,OAAS,CACzB,EACME,EAAa,cAAY,iBAAiBF,EAAe,CAAC,EAChE,SAAO,SAAM,IAAI,eAAa,CAAE,KAAAC,EAAM,WAAAC,CAAW,CAAC,CAAC,CACrD,KAAO,QAAIJ,EAAO,QAAU,qBACnB,QAAK,IAAI,uBAAqB,eAAe,CAAC,EAC5CA,EAAO,QAAU,mBACnB,QAAK,IAAI,6BAA2B,gBAAgB,CAAC,EACnDA,EAAO,QAAU,wBACnB,QAAK,IAAI,sCAAoC,qBAAqB,CAAC,KAEnE,QAAK,IAAI,mCAAiCA,EAAO,KAAK,CAAC,CAElE,CAEO,SAASnB,EACdwB,EAC4B,CAC5B,MAAO,CACL,UAAWA,EAA2B,EACxC,CACF",
4
+ "sourcesContent": ["import {\n ApduResponse,\n DeviceDisconnectedBeforeSendingApdu,\n DeviceDisconnectedWhileSendingError,\n type DeviceModelDataSource,\n FramerUtils,\n LogLevel,\n type LogParams,\n OpeningConnectionError,\n SendApduEmptyResponseError,\n type SendApduResult,\n SendApduTimeoutError,\n type TransportDeviceModel,\n type TransportDiscoveredDevice,\n} from \"@ledgerhq/device-management-kit\";\nimport { Left, Right } from \"purify-ts\";\n\nimport { base64ToUint8Array } from \"@api/helpers/base64Utils\";\nimport { HidTransportSendApduUnknownError } from \"@api/transport/Errors\";\nimport { TRANSPORT_IDENTIFIER } from \"@api/transport/rnHidTransportIdentifier\";\nimport {\n type InternalConnectionResult,\n type InternalDeviceDisconnected,\n} from \"@api/transport/types\";\n\nimport {\n type NativeDeviceConnectionLost,\n type NativeDiscoveryDevice,\n type NativeInternalConnectionResult,\n type NativeLedgerDevice,\n type NativeLog,\n type NativeSendApduResult,\n} from \"./types\";\n\nexport function mapNativeLedgerDeviceToDeviceModel(\n nativeLedgerDevice: NativeLedgerDevice,\n deviceModelDataSource: DeviceModelDataSource,\n): TransportDeviceModel | null {\n return (\n deviceModelDataSource.filterDeviceModels({\n usbProductId: Number.parseInt(nativeLedgerDevice.usbProductIdMask, 16),\n })[0] ?? null\n );\n}\n\nexport function mapNativeDiscoveryDeviceToTransportDiscoveredDevice(\n nativeDevice: NativeDiscoveryDevice,\n deviceModelDataSource: DeviceModelDataSource,\n): TransportDiscoveredDevice | null {\n const deviceModel = mapNativeLedgerDeviceToDeviceModel(\n nativeDevice.ledgerDevice,\n deviceModelDataSource,\n );\n if (deviceModel == null) return null;\n\n return {\n id: nativeDevice.uid,\n deviceModel,\n transport: TRANSPORT_IDENTIFIER,\n name: nativeDevice.name,\n };\n}\n\nexport function mapNativeTransportLogToLog(log: NativeLog): LogParams {\n let level: LogLevel;\n switch (log.level) {\n case \"error\":\n level = LogLevel.Error;\n break;\n case \"warning\":\n level = LogLevel.Warning;\n break;\n case \"info\":\n level = LogLevel.Info;\n break;\n case \"debug\":\n level = LogLevel.Debug;\n break;\n default:\n assertNever(log.level);\n level = LogLevel.Info;\n break;\n }\n\n return [\n level,\n log.message,\n {\n tag: log.tag,\n data: log.jsonPayload,\n timestamp: Number.parseInt(log.timestamp, 10),\n },\n ];\n}\n\nfunction assertNever(x: never) {\n throw new Error(\"Unexpected object: \" + x);\n}\n\nexport function mapNativeConnectionResultToConnectionResult(\n result: NativeInternalConnectionResult,\n deviceModelDataSource: DeviceModelDataSource,\n): InternalConnectionResult {\n if (result.success) {\n const transportDeviceModel = mapNativeLedgerDeviceToDeviceModel(\n result.ledgerDevice,\n deviceModelDataSource,\n );\n if (!transportDeviceModel)\n return Left(\n new OpeningConnectionError(\n `Could not find device model for the connected device with usbProductIdMask: ${result.ledgerDevice.usbProductIdMask}`,\n ),\n );\n return Right({ sessionId: result.sessionId, transportDeviceModel });\n } else {\n return Left(new OpeningConnectionError(result.error));\n }\n}\n\nexport function mapNativeSendApduResultToSendApduResult(\n result: NativeSendApduResult,\n): SendApduResult {\n if (result.success) {\n const responseBytes = base64ToUint8Array(result.apdu);\n const data = FramerUtils.getFirstBytesFrom(\n responseBytes,\n responseBytes.length - 2,\n );\n const statusCode = FramerUtils.getLastBytesFrom(responseBytes, 2);\n return Right(new ApduResponse({ data, statusCode }));\n } else {\n switch (result.error) {\n case \"SendApduTimeout\":\n return Left(new SendApduTimeoutError(\"Abort timeout\"));\n case \"EmptyResponse\":\n return Left(new SendApduEmptyResponseError(\"Empty response\"));\n case \"DeviceDisconnected\":\n return Left(\n new DeviceDisconnectedWhileSendingError(\"Device disconnected\"),\n );\n case \"DeviceNotFound\":\n case \"NoUsbEndpointFound\":\n return Left(new DeviceDisconnectedBeforeSendingApdu());\n default:\n return Left(new HidTransportSendApduUnknownError(result.error));\n }\n }\n}\n\nexport function mapNativeDeviceConnectionLostToDeviceDisconnected(\n nativeDeviceConnectionLost: NativeDeviceConnectionLost,\n): InternalDeviceDisconnected {\n return {\n sessionId: nativeDeviceConnectionLost.id,\n };\n}\n"],
5
+ "mappings": "yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,iDAAAE,EAAA,sDAAAC,EAAA,wDAAAC,EAAA,uCAAAC,EAAA,4CAAAC,EAAA,+BAAAC,IAAA,eAAAC,EAAAR,GAAA,IAAAS,EAcO,2CACPC,EAA4B,qBAE5BC,EAAmC,oCACnCC,EAAiD,iCACjDC,EAAqC,mDAe9B,SAASR,EACdS,EACAC,EAC6B,CAC7B,OACEA,EAAsB,mBAAmB,CACvC,aAAc,OAAO,SAASD,EAAmB,iBAAkB,EAAE,CACvE,CAAC,EAAE,CAAC,GAAK,IAEb,CAEO,SAASV,EACdY,EACAD,EACkC,CAClC,MAAME,EAAcZ,EAClBW,EAAa,aACbD,CACF,EACA,OAAIE,GAAe,KAAa,KAEzB,CACL,GAAID,EAAa,IACjB,YAAAC,EACA,UAAW,uBACX,KAAMD,EAAa,IACrB,CACF,CAEO,SAAST,EAA2BW,EAA2B,CACpE,IAAIC,EACJ,OAAQD,EAAI,MAAO,CACjB,IAAK,QACHC,EAAQ,WAAS,MACjB,MACF,IAAK,UACHA,EAAQ,WAAS,QACjB,MACF,IAA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O,SAASnB,EACdwB,EAC4B,CAC5B,MAAO,CACL,UAAWA,EAA2B,EACxC,CACF",
6
6
  "names": ["mapper_exports", "__export", "mapNativeConnectionResultToConnectionResult", "mapNativeDeviceConnectionLostToDeviceDisconnected", "mapNativeDiscoveryDeviceToTransportDiscoveredDevice", "mapNativeLedgerDeviceToDeviceModel", "mapNativeSendApduResultToSendApduResult", "mapNativeTransportLogToLog", "__toCommonJS", "import_device_management_kit", "import_purify_ts", "import_base64Utils", "import_Errors", "import_rnHidTransportIdentifier", "nativeLedgerDevice", "deviceModelDataSource", "nativeDevice", "deviceModel", "log", "level", "assertNever", "x", "result", "transportDeviceModel", "responseBytes", "data", "statusCode", "nativeDeviceConnectionLost"]
7
7
  }
@@ -1,2 +1,2 @@
1
- "use strict";var e=require("@ledgerhq/device-management-kit"),a=require("purify-ts"),d=require("../transport/Errors"),c=require("../transport/rnHidTransportIdentifier"),o=require("./mapper");describe("mapper",()=>{const n=new e.StaticDeviceModelDataSource;describe("mapNativeLedgerDeviceToDeviceModel",()=>{[{nativeLedgerDevice:{name:"NanoS",usbProductIdMask:"0x10"},deviceModel:n.getDeviceModel({id:e.DeviceModelId.NANO_S})},{nativeLedgerDevice:{name:"NanoX",usbProductIdMask:"0x40"},deviceModel:n.getDeviceModel({id:e.DeviceModelId.NANO_X})},{nativeLedgerDevice:{name:"NanoSPlus",usbProductIdMask:"0x50"},deviceModel:n.getDeviceModel({id:e.DeviceModelId.NANO_SP})},{nativeLedgerDevice:{name:"Stax",usbProductIdMask:"0x60"},deviceModel:n.getDeviceModel({id:e.DeviceModelId.STAX})},{nativeLedgerDevice:{name:"Flex",usbProductIdMask:"0x70"},deviceModel:n.getDeviceModel({id:e.DeviceModelId.FLEX})},{nativeLedgerDevice:{name:"NanoX",usbProductIdMask:"0x12345678"},deviceModel:null}].forEach(({nativeLedgerDevice:t,deviceModel:i})=>{it(`should map USB device with usbProductIdMask ${t.usbProductIdMask} to ${i?.productName??"null"}`,()=>{expect((0,o.mapNativeLedgerDeviceToDeviceModel)(t,n)).toEqual(i)})})}),describe("mapNativeDiscoveryDeviceToTransportDiscoveredDevice",()=>{it("should map NativeDiscoveryDevice to TransportDiscoveredDevice",()=>{const s={name:"NanoS",uid:"abcd",ledgerDevice:{name:"NanoS",usbProductIdMask:"0x10"}},t={id:"abcd",deviceModel:n.getDeviceModel({id:e.DeviceModelId.NANO_S}),transport:c.TRANSPORT_IDENTIFIER,name:"NanoS"};expect((0,o.mapNativeDiscoveryDeviceToTransportDiscoveredDevice)(s,n)).toEqual(t)}),it("should return null if the device model is not recognized",()=>{const s={name:"NanoX",uid:"efgh",ledgerDevice:{name:"NanoX",usbProductIdMask:"0x4567890"}};expect((0,o.mapNativeDiscoveryDeviceToTransportDiscoveredDevice)(s,n)).toEqual(null)})}),describe("mapNativeTransportLogToLog",()=>{[{nativeLog:{level:"debug",tag:"tag",message:"debug message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[e.LogLevel.Debug,"debug message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"info",tag:"tag",message:"info message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[e.LogLevel.Info,"info message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"error",tag:"tag",message:"error message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[e.LogLevel.Error,"error message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"warning",tag:"tag",message:"warning message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[e.LogLevel.Warning,"warning message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]}].forEach(({nativeLog:t,log:i})=>{it(`should map NativeLog with level "${t.level}" to Log`,()=>{expect((0,o.mapNativeTransportLogToLog)(t)).toEqual(i)})})}),describe("mapNativeConnectionResultToConnectionResult",()=>{[{testTitle:"Success",nativeConnectionResult:{success:!0,sessionId:"1234",ledgerDevice:{name:"NanoS",usbProductIdMask:"0x10"},deviceName:"NanoS"},connectionResult:(0,a.Right)({sessionId:"1234",transportDeviceModel:n.getDeviceModel({id:e.DeviceModelId.NANO_S})})},{testTitle:"Failure",nativeConnectionResult:{success:!1,error:"error message"},connectionResult:(0,a.Left)(new e.OpeningConnectionError("error message"))},{testTitle:"Unknown device model",nativeConnectionResult:{success:!0,sessionId:"1234",ledgerDevice:{name:"NanoX",usbProductIdMask:"0x12345678"},deviceName:"NanoX"},connectionResult:(0,a.Left)(new e.OpeningConnectionError("Could not find device model for the connected device with usbProductIdMask: 0x12345678"))}].forEach(({testTitle:t,nativeConnectionResult:i,connectionResult:r})=>{it(t,()=>{expect((0,o.mapNativeConnectionResultToConnectionResult)(i,n)).toEqual(r)})})}),describe("mapNativeSendApduResultToSendApduResult",()=>{test("success",()=>{const t={success:!0,apdu:"AQIDkAA="},i=(0,a.Right)(new e.ApduResponse({data:new Uint8Array([1,2,3]),statusCode:new Uint8Array([144,0])}));expect((0,o.mapNativeSendApduResultToSendApduResult)(t)).toEqual(i)}),test("failure",()=>{const s={success:!1,error:"error message"},t=(0,a.Left)(new d.HidTransportSendApduUnknownError("error message"));expect((0,o.mapNativeSendApduResultToSendApduResult)(s)).toEqual(t)}),test("timeout error",()=>{const s={success:!1,error:"SendApduTimeout"},t=(0,a.Left)(new e.SendApduTimeoutError("Abort timeout"));expect((0,o.mapNativeSendApduResultToSendApduResult)(s)).toEqual(t)}),test("empty response error",()=>{const s={success:!1,error:"EmptyResponse"},t=(0,a.Left)(new e.SendApduEmptyResponseError("Empty response"));expect((0,o.mapNativeSendApduResultToSendApduResult)(s)).toEqual(t)})}),describe("mapNativeDeviceConnectionLostToDeviceDisconnected",()=>{it("should map NativeDeviceConnectionLost to DeviceDisconnected",()=>{const s={id:"1234"},t={sessionId:"1234"};expect((0,o.mapNativeDeviceConnectionLostToDeviceDisconnected)(s)).toEqual(t)})})});
1
+ "use strict";var e=require("@ledgerhq/device-management-kit"),n=require("purify-ts"),d=require("../transport/Errors"),c=require("../transport/rnHidTransportIdentifier"),s=require("./mapper");describe("mapper",()=>{const a=new e.StaticDeviceModelDataSource;describe("mapNativeLedgerDeviceToDeviceModel",()=>{[{nativeLedgerDevice:{name:"NanoS",usbProductIdMask:"0x10"},deviceModel:a.getDeviceModel({id:e.DeviceModelId.NANO_S})},{nativeLedgerDevice:{name:"NanoX",usbProductIdMask:"0x40"},deviceModel:a.getDeviceModel({id:e.DeviceModelId.NANO_X})},{nativeLedgerDevice:{name:"NanoSPlus",usbProductIdMask:"0x50"},deviceModel:a.getDeviceModel({id:e.DeviceModelId.NANO_SP})},{nativeLedgerDevice:{name:"Stax",usbProductIdMask:"0x60"},deviceModel:a.getDeviceModel({id:e.DeviceModelId.STAX})},{nativeLedgerDevice:{name:"Flex",usbProductIdMask:"0x70"},deviceModel:a.getDeviceModel({id:e.DeviceModelId.FLEX})},{nativeLedgerDevice:{name:"NanoX",usbProductIdMask:"0x12345678"},deviceModel:null}].forEach(({nativeLedgerDevice:t,deviceModel:i})=>{it(`should map USB device with usbProductIdMask ${t.usbProductIdMask} to ${i?.productName??"null"}`,()=>{expect((0,s.mapNativeLedgerDeviceToDeviceModel)(t,a)).toEqual(i)})})}),describe("mapNativeDiscoveryDeviceToTransportDiscoveredDevice",()=>{it("should map NativeDiscoveryDevice to TransportDiscoveredDevice",()=>{const o={name:"NanoS",uid:"abcd",ledgerDevice:{name:"NanoS",usbProductIdMask:"0x10"}},t={id:"abcd",deviceModel:a.getDeviceModel({id:e.DeviceModelId.NANO_S}),transport:c.TRANSPORT_IDENTIFIER,name:"NanoS"};expect((0,s.mapNativeDiscoveryDeviceToTransportDiscoveredDevice)(o,a)).toEqual(t)}),it("should return null if the device model is not recognized",()=>{const o={name:"NanoX",uid:"efgh",ledgerDevice:{name:"NanoX",usbProductIdMask:"0x4567890"}};expect((0,s.mapNativeDiscoveryDeviceToTransportDiscoveredDevice)(o,a)).toEqual(null)})}),describe("mapNativeTransportLogToLog",()=>{[{nativeLog:{level:"debug",tag:"tag",message:"debug message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[e.LogLevel.Debug,"debug message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"info",tag:"tag",message:"info message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[e.LogLevel.Info,"info message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"error",tag:"tag",message:"error message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[e.LogLevel.Error,"error message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"warning",tag:"tag",message:"warning message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[e.LogLevel.Warning,"warning message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]}].forEach(({nativeLog:t,log:i})=>{it(`should map NativeLog with level "${t.level}" to Log`,()=>{expect((0,s.mapNativeTransportLogToLog)(t)).toEqual(i)})})}),describe("mapNativeConnectionResultToConnectionResult",()=>{[{testTitle:"Success",nativeConnectionResult:{success:!0,sessionId:"1234",ledgerDevice:{name:"NanoS",usbProductIdMask:"0x10"},deviceName:"NanoS"},connectionResult:(0,n.Right)({sessionId:"1234",transportDeviceModel:a.getDeviceModel({id:e.DeviceModelId.NANO_S})})},{testTitle:"Failure",nativeConnectionResult:{success:!1,error:"error message"},connectionResult:(0,n.Left)(new e.OpeningConnectionError("error message"))},{testTitle:"Unknown device model",nativeConnectionResult:{success:!0,sessionId:"1234",ledgerDevice:{name:"NanoX",usbProductIdMask:"0x12345678"},deviceName:"NanoX"},connectionResult:(0,n.Left)(new e.OpeningConnectionError("Could not find device model for the connected device with usbProductIdMask: 0x12345678"))}].forEach(({testTitle:t,nativeConnectionResult:i,connectionResult:r})=>{it(t,()=>{expect((0,s.mapNativeConnectionResultToConnectionResult)(i,a)).toEqual(r)})})}),describe("mapNativeSendApduResultToSendApduResult",()=>{test("success",()=>{const t={success:!0,apdu:"AQIDkAA="},i=(0,n.Right)(new e.ApduResponse({data:new Uint8Array([1,2,3]),statusCode:new Uint8Array([144,0])}));expect((0,s.mapNativeSendApduResultToSendApduResult)(t)).toEqual(i)}),test("failure",()=>{const o={success:!1,error:"error message"},t=(0,n.Left)(new d.HidTransportSendApduUnknownError("error message"));expect((0,s.mapNativeSendApduResultToSendApduResult)(o)).toEqual(t)}),test("timeout error",()=>{const o={success:!1,error:"SendApduTimeout"},t=(0,n.Left)(new e.SendApduTimeoutError("Abort timeout"));expect((0,s.mapNativeSendApduResultToSendApduResult)(o)).toEqual(t)}),test("empty response error",()=>{const o={success:!1,error:"EmptyResponse"},t=(0,n.Left)(new e.SendApduEmptyResponseError("Empty response"));expect((0,s.mapNativeSendApduResultToSendApduResult)(o)).toEqual(t)}),test("device not found error",()=>{const o={success:!1,error:"DeviceNotFound"},t=(0,n.Left)(new e.DeviceDisconnectedBeforeSendingApdu);expect((0,s.mapNativeSendApduResultToSendApduResult)(o)).toEqual(t)}),test("no usb endpoint found error",()=>{const o={success:!1,error:"NoUsbEndpointFound"},t=(0,n.Left)(new e.DeviceDisconnectedBeforeSendingApdu);expect((0,s.mapNativeSendApduResultToSendApduResult)(o)).toEqual(t)}),test("unknown error",()=>{const o={success:!1,error:"unknown error"},t=(0,n.Left)(new d.HidTransportSendApduUnknownError("unknown error"));expect((0,s.mapNativeSendApduResultToSendApduResult)(o)).toEqual(t)})}),describe("mapNativeDeviceConnectionLostToDeviceDisconnected",()=>{it("should map NativeDeviceConnectionLost to DeviceDisconnected",()=>{const o={id:"1234"},t={sessionId:"1234"};expect((0,s.mapNativeDeviceConnectionLostToDeviceDisconnected)(o)).toEqual(t)})})});
2
2
  //# sourceMappingURL=mapper.test.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/api/bridge/mapper.test.ts"],
4
- "sourcesContent": ["import {\n ApduResponse,\n DeviceModelId,\n LogLevel,\n type LogParams,\n OpeningConnectionError,\n SendApduEmptyResponseError,\n type SendApduResult,\n SendApduTimeoutError,\n StaticDeviceModelDataSource,\n type TransportDeviceModel,\n type TransportDiscoveredDevice,\n} from \"@ledgerhq/device-management-kit\";\nimport { Left, Right } from \"purify-ts\";\n\nimport { HidTransportSendApduUnknownError } from \"@api/transport/Errors\";\nimport { TRANSPORT_IDENTIFIER } from \"@api/transport/rnHidTransportIdentifier\";\nimport { type InternalConnectionResult } from \"@api/transport/types\";\n\nimport {\n mapNativeConnectionResultToConnectionResult,\n mapNativeDeviceConnectionLostToDeviceDisconnected,\n mapNativeDiscoveryDeviceToTransportDiscoveredDevice,\n mapNativeLedgerDeviceToDeviceModel,\n mapNativeSendApduResultToSendApduResult,\n mapNativeTransportLogToLog,\n} from \"./mapper\";\nimport {\n type NativeDiscoveryDevice,\n type NativeInternalConnectionResult,\n type NativeLedgerDevice,\n type NativeLog,\n type NativeSendApduResult,\n} from \"./types\";\n\ndescribe(\"mapper\", () => {\n const deviceModelDataSource = new StaticDeviceModelDataSource();\n\n describe(\"mapNativeLedgerDeviceToDeviceModel\", () => {\n const testCases: Array<{\n nativeLedgerDevice: NativeLedgerDevice;\n deviceModel: TransportDeviceModel | null;\n }> = [\n {\n nativeLedgerDevice: {\n name: \"NanoS\",\n usbProductIdMask: \"0x10\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_S,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"NanoX\",\n usbProductIdMask: \"0x40\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_X,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"NanoSPlus\",\n usbProductIdMask: \"0x50\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_SP,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"Stax\",\n usbProductIdMask: \"0x60\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.STAX,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"Flex\",\n usbProductIdMask: \"0x70\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.FLEX,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"NanoX\",\n usbProductIdMask: \"0x12345678\",\n },\n deviceModel: null, // because the usbProductIdMask is not recognized\n },\n ];\n testCases.forEach(({ nativeLedgerDevice, deviceModel }) => {\n it(`should map USB device with usbProductIdMask ${nativeLedgerDevice.usbProductIdMask} to ${\n deviceModel?.productName ?? \"null\"\n }`, () => {\n expect(\n mapNativeLedgerDeviceToDeviceModel(\n nativeLedgerDevice,\n deviceModelDataSource,\n ),\n ).toEqual(deviceModel);\n });\n });\n });\n\n describe(\"mapNativeDiscoveryDeviceToTransportDiscoveredDevice\", () => {\n it(\"should map NativeDiscoveryDevice to TransportDiscoveredDevice\", () => {\n const nativeDevice: NativeDiscoveryDevice = {\n name: \"NanoS\",\n uid: \"abcd\",\n ledgerDevice: {\n name: \"NanoS\",\n usbProductIdMask: \"0x10\",\n },\n };\n const expectedDiscoveredDevice: TransportDiscoveredDevice = {\n id: \"abcd\",\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_S,\n }),\n transport: TRANSPORT_IDENTIFIER,\n name: \"NanoS\",\n };\n expect(\n mapNativeDiscoveryDeviceToTransportDiscoveredDevice(\n nativeDevice,\n deviceModelDataSource,\n ),\n ).toEqual(expectedDiscoveredDevice);\n });\n\n it(\"should return null if the device model is not recognized\", () => {\n const nativeDevice: NativeDiscoveryDevice = {\n name: \"NanoX\",\n uid: \"efgh\",\n ledgerDevice: {\n name: \"NanoX\",\n usbProductIdMask: \"0x4567890\", // some invalid value\n },\n };\n const expectedDiscoveredDevice = null; // because the usbProductIdMask is not recognized\n expect(\n mapNativeDiscoveryDeviceToTransportDiscoveredDevice(\n nativeDevice,\n deviceModelDataSource,\n ),\n ).toEqual(expectedDiscoveredDevice);\n });\n });\n\n describe(\"mapNativeTransportLogToLog\", () => {\n const testCases: Array<{\n nativeLog: NativeLog;\n log: LogParams;\n }> = [\n {\n // debug\n nativeLog: {\n level: \"debug\",\n tag: \"tag\",\n message: \"debug message\",\n jsonPayload: { key: \"value\" },\n timestamp: \"123456789\",\n },\n log: [\n LogLevel.Debug,\n \"debug message\",\n {\n timestamp: 123456789,\n tag: \"tag\",\n data: { key: \"value\" },\n },\n ],\n },\n // info\n {\n nativeLog: {\n level: \"info\",\n tag: \"tag\",\n message: \"info message\",\n jsonPayload: { key: \"value\" },\n timestamp: \"123456789\",\n },\n log: [\n LogLevel.Info,\n \"info message\",\n {\n timestamp: 123456789,\n tag: \"tag\",\n data: { key: \"value\" },\n },\n ],\n },\n // error\n {\n nativeLog: {\n level: \"error\",\n tag: \"tag\",\n message: \"error message\",\n jsonPayload: { key: \"value\" },\n timestamp: \"123456789\",\n },\n log: [\n LogLevel.Error,\n \"error message\",\n {\n timestamp: 123456789,\n tag: \"tag\",\n data: { key: \"value\" },\n },\n ],\n },\n // warning\n {\n nativeLog: {\n level: \"warning\",\n tag: \"tag\",\n message: \"warning message\",\n jsonPayload: { key: \"value\" },\n timestamp: \"123456789\",\n },\n log: [\n LogLevel.Warning,\n \"warning message\",\n {\n timestamp: 123456789,\n tag: \"tag\",\n data: { key: \"value\" },\n },\n ],\n },\n ];\n\n testCases.forEach(({ nativeLog, log }) => {\n it(`should map NativeLog with level \"${nativeLog.level}\" to Log`, () => {\n expect(mapNativeTransportLogToLog(nativeLog)).toEqual(log);\n });\n });\n });\n\n describe(\"mapNativeConnectionResultToConnectionResult\", () => {\n const testCases: Array<{\n nativeConnectionResult: NativeInternalConnectionResult;\n connectionResult: InternalConnectionResult;\n testTitle: string;\n }> = [\n {\n testTitle: \"Success\",\n nativeConnectionResult: {\n success: true,\n sessionId: \"1234\",\n ledgerDevice: {\n name: \"NanoS\",\n usbProductIdMask: \"0x10\",\n },\n deviceName: \"NanoS\",\n },\n connectionResult: Right({\n sessionId: \"1234\",\n transportDeviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_S,\n }),\n }),\n },\n {\n testTitle: \"Failure\",\n nativeConnectionResult: {\n success: false,\n error: \"error message\",\n },\n connectionResult: Left(new OpeningConnectionError(\"error message\")),\n },\n {\n testTitle: \"Unknown device model\",\n nativeConnectionResult: {\n success: true,\n sessionId: \"1234\",\n ledgerDevice: {\n name: \"NanoX\",\n usbProductIdMask: \"0x12345678\",\n },\n deviceName: \"NanoX\",\n },\n connectionResult: Left(\n new OpeningConnectionError(\n \"Could not find device model for the connected device with usbProductIdMask: 0x12345678\",\n ),\n ),\n },\n ];\n\n testCases.forEach(\n ({ testTitle, nativeConnectionResult, connectionResult }) => {\n it(testTitle, () => {\n expect(\n mapNativeConnectionResultToConnectionResult(\n nativeConnectionResult,\n deviceModelDataSource,\n ),\n ).toEqual(connectionResult);\n });\n },\n );\n });\n\n describe(\"mapNativeSendApduResultToSendApduResult\", () => {\n test(\"success\", () => {\n const resultApduString = \"AQIDkAA=\";\n const nativeSendApduResult: NativeSendApduResult = {\n success: true,\n apdu: resultApduString,\n };\n const expectedSendApduResult: SendApduResult = Right(\n new ApduResponse({\n data: new Uint8Array([0x01, 0x02, 0x03]),\n statusCode: new Uint8Array([0x90, 0x00]),\n }),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"failure\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"error message\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new HidTransportSendApduUnknownError(\"error message\"),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"timeout error\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"SendApduTimeout\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new SendApduTimeoutError(\"Abort timeout\"),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"empty response error\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"EmptyResponse\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new SendApduEmptyResponseError(\"Empty response\"),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n });\n\n describe(\"mapNativeDeviceConnectionLostToDeviceDisconnected\", () => {\n it(\"should map NativeDeviceConnectionLost to DeviceDisconnected\", () => {\n const nativeDeviceConnectionLost = {\n id: \"1234\",\n };\n const expectedDeviceDisconnected = {\n sessionId: \"1234\",\n };\n expect(\n mapNativeDeviceConnectionLostToDeviceDisconnected(\n nativeDeviceConnectionLost,\n ),\n ).toEqual(expectedDeviceDisconnected);\n });\n });\n});\n"],
5
- "mappings": "aAAA,IAAAA,EAYO,2CACPC,EAA4B,qBAE5BC,EAAiD,iCACjDC,EAAqC,mDAGrCC,EAOO,oBASP,SAAS,SAAU,IAAM,CACvB,MAAMC,EAAwB,IAAI,8BAElC,SAAS,qCAAsC,IAAM,CAI9C,CACH,CACE,mBAAoB,CAClB,KAAM,QACN,iBAAkB,MACpB,EACA,YAAaA,EAAsB,eAAe,CAChD,GAAI,gBAAc,MACpB,CAAC,CACH,EACA,CACE,mBAAoB,CAClB,KAAM,QACN,iBAAkB,MACpB,EACA,YAAaA,EAAsB,eAAe,CAChD,GAAI,gBAAc,MACpB,CAAC,CACH,EACA,CACE,mBAAoB,CAClB,KAAM,YACN,iBAAkB,MACpB,EACA,YAAaA,EAAsB,eAAe,CAChD,GAAI,gBAAc,OACpB,CAAC,CACH,EACA,CACE,mBAAoB,CAClB,KAAM,OACN,iBAAkB,MACpB,EACA,YAAaA,EAAsB,eAAe,CAChD,GAAI,gBAAc,IACpB,CAAC,CACH,EACA,CACE,mBAAoB,CAClB,KAAM,OACN,iBAAkB,MACpB,EACA,YAAaA,EAAsB,eAAe,CAChD,GAAI,gBAAc,IACpB,CAAC,CACH,EACA,CACE,mBAAoB,CAClB,KAAM,QACN,iBAAkB,YACpB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CACF,CACF,CAAC,EAED,SAAS,0CAA2C,IAAM,CACxD,KAAK,UAAW,IAAM,CAEpB,MAAMC,EAA6C,CACjD,QAAS,GACT,KAHuB,UAIzB,EACMC,KAAyC,SAC7C,IAAI,eAAa,CACf,KAAM,IAAI,WAAW,CAAC,EAAM,EAAM,CAAI,CAAC,EACvC,WAAY,IAAI,WAAW,CAAC,IAAM,CAAI,CAAC,CACzC,CAAC,CACH,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,EAED,KAAK,UAAW,IAAM,CACpB,MAAMD,EAA6C,CACjD,QAAS,GACT,MAAO,eACT,EACMC,KAAyC,QAC7C,IAAI,mCAAiC,eAAe,CACtD,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,EAED,KAAK,gBAAiB,IAAM,CAC1B,MAAMD,EAA6C,CACjD,QAAS,GACT,MAAO,iBACT,EACMC,KAAyC,QAC7C,IAAI,uBAAqB,eAAe,CAC1C,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,EAED,KAAK,uBAAwB,IAAM,CACjC,MAAMD,EAA6C,CACjD,QAAS,GACT,MAAO,eACT,EACMC,KAAyC,QAC7C,IAAI,6BAA2B,gBAAgB,CACjD,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,CACH,CAAC,EAED,SAAS,oDAAqD,IAAM,CAClE,GAAG,8DAA+D,IAAM,CACtE,MAAMC,EAA6B,CACjC,GAAI,MACN,EACMC,EAA6B,CACjC,UAAW,MACb,EACA,UACE,qDACED,CACF,CACF,EAAE,QAAQC,CAA0B,CACtC,CAAC,CACH,CAAC,CACH,CAAC",
4
+ "sourcesContent": ["import {\n ApduResponse,\n DeviceDisconnectedBeforeSendingApdu,\n DeviceModelId,\n LogLevel,\n type LogParams,\n OpeningConnectionError,\n SendApduEmptyResponseError,\n type SendApduResult,\n SendApduTimeoutError,\n StaticDeviceModelDataSource,\n type TransportDeviceModel,\n type TransportDiscoveredDevice,\n} from \"@ledgerhq/device-management-kit\";\nimport { Left, Right } from \"purify-ts\";\n\nimport { HidTransportSendApduUnknownError } from \"@api/transport/Errors\";\nimport { TRANSPORT_IDENTIFIER } from \"@api/transport/rnHidTransportIdentifier\";\nimport { type InternalConnectionResult } from \"@api/transport/types\";\n\nimport {\n mapNativeConnectionResultToConnectionResult,\n mapNativeDeviceConnectionLostToDeviceDisconnected,\n mapNativeDiscoveryDeviceToTransportDiscoveredDevice,\n mapNativeLedgerDeviceToDeviceModel,\n mapNativeSendApduResultToSendApduResult,\n mapNativeTransportLogToLog,\n} from \"./mapper\";\nimport {\n type NativeDiscoveryDevice,\n type NativeInternalConnectionResult,\n type NativeLedgerDevice,\n type NativeLog,\n type NativeSendApduResult,\n} from \"./types\";\n\ndescribe(\"mapper\", () => {\n const deviceModelDataSource = new StaticDeviceModelDataSource();\n\n describe(\"mapNativeLedgerDeviceToDeviceModel\", () => {\n const testCases: Array<{\n nativeLedgerDevice: NativeLedgerDevice;\n deviceModel: TransportDeviceModel | null;\n }> = [\n {\n nativeLedgerDevice: {\n name: \"NanoS\",\n usbProductIdMask: \"0x10\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_S,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"NanoX\",\n usbProductIdMask: \"0x40\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_X,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"NanoSPlus\",\n usbProductIdMask: \"0x50\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_SP,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"Stax\",\n usbProductIdMask: \"0x60\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.STAX,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"Flex\",\n usbProductIdMask: \"0x70\",\n },\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.FLEX,\n }),\n },\n {\n nativeLedgerDevice: {\n name: \"NanoX\",\n usbProductIdMask: \"0x12345678\",\n },\n deviceModel: null, // because the usbProductIdMask is not recognized\n },\n ];\n testCases.forEach(({ nativeLedgerDevice, deviceModel }) => {\n it(`should map USB device with usbProductIdMask ${nativeLedgerDevice.usbProductIdMask} to ${\n deviceModel?.productName ?? \"null\"\n }`, () => {\n expect(\n mapNativeLedgerDeviceToDeviceModel(\n nativeLedgerDevice,\n deviceModelDataSource,\n ),\n ).toEqual(deviceModel);\n });\n });\n });\n\n describe(\"mapNativeDiscoveryDeviceToTransportDiscoveredDevice\", () => {\n it(\"should map NativeDiscoveryDevice to TransportDiscoveredDevice\", () => {\n const nativeDevice: NativeDiscoveryDevice = {\n name: \"NanoS\",\n uid: \"abcd\",\n ledgerDevice: {\n name: \"NanoS\",\n usbProductIdMask: \"0x10\",\n },\n };\n const expectedDiscoveredDevice: TransportDiscoveredDevice = {\n id: \"abcd\",\n deviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_S,\n }),\n transport: TRANSPORT_IDENTIFIER,\n name: \"NanoS\",\n };\n expect(\n mapNativeDiscoveryDeviceToTransportDiscoveredDevice(\n nativeDevice,\n deviceModelDataSource,\n ),\n ).toEqual(expectedDiscoveredDevice);\n });\n\n it(\"should return null if the device model is not recognized\", () => {\n const nativeDevice: NativeDiscoveryDevice = {\n name: \"NanoX\",\n uid: \"efgh\",\n ledgerDevice: {\n name: \"NanoX\",\n usbProductIdMask: \"0x4567890\", // some invalid value\n },\n };\n const expectedDiscoveredDevice = null; // because the usbProductIdMask is not recognized\n expect(\n mapNativeDiscoveryDeviceToTransportDiscoveredDevice(\n nativeDevice,\n deviceModelDataSource,\n ),\n ).toEqual(expectedDiscoveredDevice);\n });\n });\n\n describe(\"mapNativeTransportLogToLog\", () => {\n const testCases: Array<{\n nativeLog: NativeLog;\n log: LogParams;\n }> = [\n {\n // debug\n nativeLog: {\n level: \"debug\",\n tag: \"tag\",\n message: \"debug message\",\n jsonPayload: { key: \"value\" },\n timestamp: \"123456789\",\n },\n log: [\n LogLevel.Debug,\n \"debug message\",\n {\n timestamp: 123456789,\n tag: \"tag\",\n data: { key: \"value\" },\n },\n ],\n },\n // info\n {\n nativeLog: {\n level: \"info\",\n tag: \"tag\",\n message: \"info message\",\n jsonPayload: { key: \"value\" },\n timestamp: \"123456789\",\n },\n log: [\n LogLevel.Info,\n \"info message\",\n {\n timestamp: 123456789,\n tag: \"tag\",\n data: { key: \"value\" },\n },\n ],\n },\n // error\n {\n nativeLog: {\n level: \"error\",\n tag: \"tag\",\n message: \"error message\",\n jsonPayload: { key: \"value\" },\n timestamp: \"123456789\",\n },\n log: [\n LogLevel.Error,\n \"error message\",\n {\n timestamp: 123456789,\n tag: \"tag\",\n data: { key: \"value\" },\n },\n ],\n },\n // warning\n {\n nativeLog: {\n level: \"warning\",\n tag: \"tag\",\n message: \"warning message\",\n jsonPayload: { key: \"value\" },\n timestamp: \"123456789\",\n },\n log: [\n LogLevel.Warning,\n \"warning message\",\n {\n timestamp: 123456789,\n tag: \"tag\",\n data: { key: \"value\" },\n },\n ],\n },\n ];\n\n testCases.forEach(({ nativeLog, log }) => {\n it(`should map NativeLog with level \"${nativeLog.level}\" to Log`, () => {\n expect(mapNativeTransportLogToLog(nativeLog)).toEqual(log);\n });\n });\n });\n\n describe(\"mapNativeConnectionResultToConnectionResult\", () => {\n const testCases: Array<{\n nativeConnectionResult: NativeInternalConnectionResult;\n connectionResult: InternalConnectionResult;\n testTitle: string;\n }> = [\n {\n testTitle: \"Success\",\n nativeConnectionResult: {\n success: true,\n sessionId: \"1234\",\n ledgerDevice: {\n name: \"NanoS\",\n usbProductIdMask: \"0x10\",\n },\n deviceName: \"NanoS\",\n },\n connectionResult: Right({\n sessionId: \"1234\",\n transportDeviceModel: deviceModelDataSource.getDeviceModel({\n id: DeviceModelId.NANO_S,\n }),\n }),\n },\n {\n testTitle: \"Failure\",\n nativeConnectionResult: {\n success: false,\n error: \"error message\",\n },\n connectionResult: Left(new OpeningConnectionError(\"error message\")),\n },\n {\n testTitle: \"Unknown device model\",\n nativeConnectionResult: {\n success: true,\n sessionId: \"1234\",\n ledgerDevice: {\n name: \"NanoX\",\n usbProductIdMask: \"0x12345678\",\n },\n deviceName: \"NanoX\",\n },\n connectionResult: Left(\n new OpeningConnectionError(\n \"Could not find device model for the connected device with usbProductIdMask: 0x12345678\",\n ),\n ),\n },\n ];\n\n testCases.forEach(\n ({ testTitle, nativeConnectionResult, connectionResult }) => {\n it(testTitle, () => {\n expect(\n mapNativeConnectionResultToConnectionResult(\n nativeConnectionResult,\n deviceModelDataSource,\n ),\n ).toEqual(connectionResult);\n });\n },\n );\n });\n\n describe(\"mapNativeSendApduResultToSendApduResult\", () => {\n test(\"success\", () => {\n const resultApduString = \"AQIDkAA=\";\n const nativeSendApduResult: NativeSendApduResult = {\n success: true,\n apdu: resultApduString,\n };\n const expectedSendApduResult: SendApduResult = Right(\n new ApduResponse({\n data: new Uint8Array([0x01, 0x02, 0x03]),\n statusCode: new Uint8Array([0x90, 0x00]),\n }),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"failure\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"error message\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new HidTransportSendApduUnknownError(\"error message\"),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"timeout error\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"SendApduTimeout\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new SendApduTimeoutError(\"Abort timeout\"),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"empty response error\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"EmptyResponse\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new SendApduEmptyResponseError(\"Empty response\"),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"device not found error\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"DeviceNotFound\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new DeviceDisconnectedBeforeSendingApdu(),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"no usb endpoint found error\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"NoUsbEndpointFound\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new DeviceDisconnectedBeforeSendingApdu(),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n\n test(\"unknown error\", () => {\n const nativeSendApduResult: NativeSendApduResult = {\n success: false,\n error: \"unknown error\",\n };\n const expectedSendApduResult: SendApduResult = Left(\n new HidTransportSendApduUnknownError(\"unknown error\"),\n );\n expect(\n mapNativeSendApduResultToSendApduResult(nativeSendApduResult),\n ).toEqual(expectedSendApduResult);\n });\n });\n\n describe(\"mapNativeDeviceConnectionLostToDeviceDisconnected\", () => {\n it(\"should map NativeDeviceConnectionLost to DeviceDisconnected\", () => {\n const nativeDeviceConnectionLost = {\n id: \"1234\",\n };\n const expectedDeviceDisconnected = {\n sessionId: \"1234\",\n };\n expect(\n mapNativeDeviceConnectionLostToDeviceDisconnected(\n nativeDeviceConnectionLost,\n ),\n ).toEqual(expectedDeviceDisconnected);\n });\n });\n});\n"],
5
+ "mappings": "aAAA,IAAAA,EAaO,2CACPC,EAA4B,qBAE5BC,EAAiD,iCACjDC,EAAqC,mDAGrCC,E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AM,CAC1B,MAAMD,EAA6C,CACjD,QAAS,GACT,MAAO,iBACT,EACMC,KAAyC,QAC7C,IAAI,uBAAqB,eAAe,CAC1C,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,EAED,KAAK,uBAAwB,IAAM,CACjC,MAAMD,EAA6C,CACjD,QAAS,GACT,MAAO,eACT,EACMC,KAAyC,QAC7C,IAAI,6BAA2B,gBAAgB,CACjD,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,EAED,KAAK,yBAA0B,IAAM,CACnC,MAAMD,EAA6C,CACjD,QAAS,GACT,MAAO,gBACT,EACMC,KAAyC,QAC7C,IAAI,qCACN,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,EAED,KAAK,8BAA+B,IAAM,CACxC,MAAMD,EAA6C,CACjD,QAAS,GACT,MAAO,oBACT,EACMC,KAAyC,QAC7C,IAAI,qCACN,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,EAED,KAAK,gBAAiB,IAAM,CAC1B,MAAMD,EAA6C,CACjD,QAAS,GACT,MAAO,eACT,EACMC,KAAyC,QAC7C,IAAI,mCAAiC,eAAe,CACtD,EACA,UACE,2CAAwCD,CAAoB,CAC9D,EAAE,QAAQC,CAAsB,CAClC,CAAC,CACH,CAAC,EAED,SAAS,oDAAqD,IAAM,CAClE,GAAG,8DAA+D,IAAM,CACtE,MAAMC,EAA6B,CACjC,GAAI,MACN,EACMC,EAA6B,CACjC,UAAW,MACb,EACA,UACE,qDACED,CACF,CACF,EAAE,QAAQC,CAA0B,CACtC,CAAC,CACH,CAAC,CACH,CAAC",
6
6
  "names": ["import_device_management_kit", "import_purify_ts", "import_Errors", "import_rnHidTransportIdentifier", "import_mapper", "deviceModelDataSource", "nativeLedgerDevice", "deviceModel", "nativeDevice", "expectedDiscoveredDevice", "nativeLog", "log", "testTitle", "nativeConnectionResult", "connectionResult", "nativeSendApduResult", "expectedSendApduResult", "nativeDeviceConnectionLost", "expectedDeviceDisconnected"]
7
7
  }
package/lib/cjs/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var a=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var R=Object.prototype.hasOwnProperty;var d=(o,r)=>{for(var T in r)a(o,T,{get:r[T],enumerable:!0})},f=(o,r,T,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of I(r))!R.call(o,t)&&t!==T&&a(o,t,{get:()=>r[t],enumerable:!(e=p(r,t))||e.enumerable});return o};var s=o=>f(a({},"__esModule",{value:!0}),o);var N={};d(N,{RNHidTransportFactory:()=>i.RNHidTransportFactory,rnHidTransportIdentifier:()=>n.TRANSPORT_IDENTIFIER});module.exports=s(N);var i=require("./api/RNHidTransportFactory"),n=require("./api/transport/rnHidTransportIdentifier");0&&(module.exports={RNHidTransportFactory,rnHidTransportIdentifier});
1
+ "use strict";var p=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var f=Object.getOwnPropertyNames;var s=Object.prototype.hasOwnProperty;var I=(o,r)=>{for(var t in r)p(o,t,{get:r[t],enumerable:!0})},R=(o,r,t,d)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of f(r))!s.call(o,n)&&n!==t&&p(o,n,{get:()=>r[n],enumerable:!(d=i(r,n))||d.enumerable});return o};var m=o=>R(p({},"__esModule",{value:!0}),o);var x={};I(x,{HidTransportSendApduUnknownError:()=>T.HidTransportSendApduUnknownError,RNHidTransportFactory:()=>e.RNHidTransportFactory,rnHidTransportIdentifier:()=>a.TRANSPORT_IDENTIFIER});module.exports=m(x);var e=require("./api/RNHidTransportFactory"),T=require("./api/transport/Errors"),a=require("./api/transport/rnHidTransportIdentifier");0&&(module.exports={HidTransportSendApduUnknownError,RNHidTransportFactory,rnHidTransportIdentifier});
2
2
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
- "sourcesContent": ["export { RNHidTransportFactory } from \"@api/RNHidTransportFactory\";\nexport { TRANSPORT_IDENTIFIER as rnHidTransportIdentifier } from \"@api/transport/rnHidTransportIdentifier\";\n"],
5
- "mappings": "yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,wHAAAE,EAAAF,GAAA,IAAAG,EAAsC,sCACtCC,EAAiE",
6
- "names": ["index_exports", "__export", "__toCommonJS", "import_RNHidTransportFactory", "import_rnHidTransportIdentifier"]
4
+ "sourcesContent": ["export { RNHidTransportFactory } from \"@api/RNHidTransportFactory\";\nexport { HidTransportSendApduUnknownError } from \"@api/transport/Errors\";\nexport { TRANSPORT_IDENTIFIER as rnHidTransportIdentifier } from \"@api/transport/rnHidTransportIdentifier\";\n"],
5
+ "mappings": "yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gMAAAE,EAAAF,GAAA,IAAAG,EAAsC,sCACtCC,EAAiD,iCACjDC,EAAiE",
6
+ "names": ["index_exports", "__export", "__toCommonJS", "import_RNHidTransportFactory", "import_Errors", "import_rnHidTransportIdentifier"]
7
7
  }
@@ -1,20 +1,30 @@
1
1
  {
2
- "name": "@ledgerhq/device-transport-kit-react-native-hid",
3
- "version": "1.0.0",
4
- "license": "Apache-2.0",
5
- "private": false,
6
- "react-native": "src/index.ts",
7
- "nativePackage": true,
2
+ "dependencies": {
3
+ "@sentry/minimal": "catalog:",
4
+ "purify-ts": "catalog:",
5
+ "uuid": "catalog:"
6
+ },
7
+ "devDependencies": {
8
+ "@ledgerhq/device-management-kit": "workspace:^",
9
+ "@ledgerhq/eslint-config-dsdk": "workspace:^",
10
+ "@ledgerhq/ldmk-tool": "workspace:^",
11
+ "@ledgerhq/prettier-config-dsdk": "workspace:^",
12
+ "@ledgerhq/tsconfig-dsdk": "workspace:^",
13
+ "@ledgerhq/vitest-config-dmk": "workspace:^",
14
+ "@types/uuid": "catalog:",
15
+ "react-native": "catalog:",
16
+ "rxjs": "catalog:"
17
+ },
8
18
  "exports": {
9
19
  ".": {
10
- "types": "./lib/types/index.d.ts",
11
20
  "import": "./lib/esm/index.js",
12
- "require": "./lib/cjs/index.js"
21
+ "require": "./lib/cjs/index.js",
22
+ "types": "./lib/types/index.d.ts"
13
23
  },
14
24
  "./*": {
15
- "types": "./lib/types/*",
16
25
  "import": "./lib/esm/*",
17
- "require": "./lib/cjs/*"
26
+ "require": "./lib/cjs/*",
27
+ "types": "./lib/types/*"
18
28
  }
19
29
  },
20
30
  "files": [
@@ -26,41 +36,35 @@
26
36
  "!android/gradlew.bat",
27
37
  "!android/local.properties"
28
38
  ],
39
+ "license": "Apache-2.0",
40
+ "name": "@ledgerhq/device-transport-kit-react-native-hid",
41
+ "nativePackage": true,
42
+ "peerDependencies": {
43
+ "@ledgerhq/device-management-kit": "workspace:^",
44
+ "react-native": ">0.74.1",
45
+ "rxjs": "catalog:"
46
+ },
47
+ "private": false,
48
+ "react-native": "src/index.ts",
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "https://github.com/LedgerHQ/device-sdk-ts.git"
52
+ },
29
53
  "scripts": {
30
- "prebuild": "rimraf lib",
31
54
  "build": "pnpm ldmk-tool build --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json",
32
55
  "dev": "concurrently \"pnpm watch:builds\" \"pnpm watch:types\"",
33
- "watch:builds": "pnpm ldmk-tool watch --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json",
34
- "watch:types": "concurrently \"tsc --watch -p tsconfig.prod.json\" \"tsc-alias --watch -p tsconfig.prod.json\"",
35
56
  "lint": "eslint src/**/*.ts",
36
57
  "lint:fix": "pnpm lint --fix",
37
58
  "postpack": "find . -name '*.tgz' -exec cp {} ../../../dist/ \\; ",
59
+ "prebuild": "rimraf lib",
38
60
  "prettier": "prettier . --check",
39
61
  "prettier:fix": "prettier . --write",
40
- "typecheck": "tsc --noEmit",
41
62
  "test": "vitest run --passWithNoTests",
63
+ "test:coverage": "vitest run --coverage --passWithNoTests",
42
64
  "test:watch": "vitest --passWithNoTests",
43
- "test:coverage": "vitest run --coverage --passWithNoTests"
44
- },
45
- "dependencies": {
46
- "@sentry/minimal": "catalog:",
47
- "purify-ts": "catalog:",
48
- "uuid": "catalog:"
49
- },
50
- "devDependencies": {
51
- "@ledgerhq/device-management-kit": "workspace:*",
52
- "@ledgerhq/eslint-config-dsdk": "workspace:*",
53
- "@ledgerhq/ldmk-tool": "workspace:*",
54
- "@ledgerhq/vitest-config-dmk": "workspace:*",
55
- "@ledgerhq/prettier-config-dsdk": "workspace:*",
56
- "@ledgerhq/tsconfig-dsdk": "workspace:*",
57
- "@types/uuid": "catalog:",
58
- "react-native": "catalog:",
59
- "rxjs": "catalog:"
65
+ "typecheck": "tsc --noEmit",
66
+ "watch:builds": "pnpm ldmk-tool watch --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json",
67
+ "watch:types": "concurrently \"tsc --watch -p tsconfig.prod.json\" \"tsc-alias --watch -p tsconfig.prod.json\""
60
68
  },
61
- "peerDependencies": {
62
- "@ledgerhq/device-management-kit": "workspace:*",
63
- "react-native": ">0.74.1",
64
- "rxjs": "catalog:"
65
- }
69
+ "version": "1.0.2"
66
70
  }
@@ -1,2 +1,2 @@
1
- import{ApduResponse as p,DeviceDisconnectedWhileSendingError as u,FramerUtils as i,LogLevel as o,OpeningConnectionError as s,SendApduEmptyResponseError as v,SendApduTimeoutError as l}from"@ledgerhq/device-management-kit";import{Left as r,Right as c}from"purify-ts";import{base64ToUint8Array as D}from"../helpers/base64Utils";import{HidTransportSendApduUnknownError as m}from"../transport/Errors";import{TRANSPORT_IDENTIFIER as f}from"../transport/rnHidTransportIdentifier";function a(e,t){return t.filterDeviceModels({usbProductId:Number.parseInt(e.usbProductIdMask,16)})[0]??null}function L(e,t){const n=a(e.ledgerDevice,t);return n==null?null:{id:e.uid,deviceModel:n,transport:f,name:e.name}}function b(e){let t;switch(e.level){case"error":t=o.Error;break;case"warning":t=o.Warning;break;case"info":t=o.Info;break;case"debug":t=o.Debug;break;default:y(e.level),t=o.Info;break}return[t,e.message,{tag:e.tag,data:e.jsonPayload,timestamp:Number.parseInt(e.timestamp,10)}]}function y(e){throw new Error("Unexpected object: "+e)}function S(e,t){if(e.success){const n=a(e.ledgerDevice,t);return n?c({sessionId:e.sessionId,transportDeviceModel:n}):r(new s(`Could not find device model for the connected device with usbProductIdMask: ${e.ledgerDevice.usbProductIdMask}`))}else return r(new s(e.error))}function A(e){if(e.success){const t=D(e.apdu),n=i.getFirstBytesFrom(t,t.length-2),d=i.getLastBytesFrom(t,2);return c(new p({data:n,statusCode:d}))}else return e.error==="SendApduTimeout"?r(new l("Abort timeout")):e.error==="EmptyResponse"?r(new v("Empty response")):e.error==="DeviceDisconnected"?r(new u("Device disconnected")):r(new m(e.error))}function w(e){return{sessionId:e.id}}export{S as mapNativeConnectionResultToConnectionResult,w as mapNativeDeviceConnectionLostToDeviceDisconnected,L as mapNativeDiscoveryDeviceToTransportDiscoveredDevice,a as mapNativeLedgerDeviceToDeviceModel,A as mapNativeSendApduResultToSendApduResult,b as mapNativeTransportLogToLog};
1
+ import{ApduResponse as u,DeviceDisconnectedBeforeSendingApdu as p,DeviceDisconnectedWhileSendingError as v,FramerUtils as i,LogLevel as r,OpeningConnectionError as c,SendApduEmptyResponseError as l,SendApduTimeoutError as D}from"@ledgerhq/device-management-kit";import{Left as o,Right as s}from"purify-ts";import{base64ToUint8Array as m}from"../helpers/base64Utils";import{HidTransportSendApduUnknownError as f}from"../transport/Errors";import{TRANSPORT_IDENTIFIER as N}from"../transport/rnHidTransportIdentifier";function a(e,t){return t.filterDeviceModels({usbProductId:Number.parseInt(e.usbProductIdMask,16)})[0]??null}function b(e,t){const n=a(e.ledgerDevice,t);return n==null?null:{id:e.uid,deviceModel:n,transport:N,name:e.name}}function S(e){let t;switch(e.level){case"error":t=r.Error;break;case"warning":t=r.Warning;break;case"info":t=r.Info;break;case"debug":t=r.Debug;break;default:y(e.level),t=r.Info;break}return[t,e.message,{tag:e.tag,data:e.jsonPayload,timestamp:Number.parseInt(e.timestamp,10)}]}function y(e){throw new Error("Unexpected object: "+e)}function w(e,t){if(e.success){const n=a(e.ledgerDevice,t);return n?s({sessionId:e.sessionId,transportDeviceModel:n}):o(new c(`Could not find device model for the connected device with usbProductIdMask: ${e.ledgerDevice.usbProductIdMask}`))}else return o(new c(e.error))}function A(e){if(e.success){const t=m(e.apdu),n=i.getFirstBytesFrom(t,t.length-2),d=i.getLastBytesFrom(t,2);return s(new u({data:n,statusCode:d}))}else switch(e.error){case"SendApduTimeout":return o(new D("Abort timeout"));case"EmptyResponse":return o(new l("Empty response"));case"DeviceDisconnected":return o(new v("Device disconnected"));case"DeviceNotFound":case"NoUsbEndpointFound":return o(new p);default:return o(new f(e.error))}}function E(e){return{sessionId:e.id}}export{w as mapNativeConnectionResultToConnectionResult,E as mapNativeDeviceConnectionLostToDeviceDisconnected,b as mapNativeDiscoveryDeviceToTransportDiscoveredDevice,a as mapNativeLedgerDeviceToDeviceModel,A as mapNativeSendApduResultToSendApduResult,S as mapNativeTransportLogToLog};
2
2
  //# sourceMappingURL=mapper.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/api/bridge/mapper.ts"],
4
- "sourcesContent": ["import {\n ApduResponse,\n DeviceDisconnectedWhileSendingError,\n type DeviceModelDataSource,\n FramerUtils,\n LogLevel,\n type LogParams,\n OpeningConnectionError,\n SendApduEmptyResponseError,\n type SendApduResult,\n SendApduTimeoutError,\n type TransportDeviceModel,\n type TransportDiscoveredDevice,\n} from \"@ledgerhq/device-management-kit\";\nimport { Left, Right } from \"purify-ts\";\n\nimport { base64ToUint8Array } from \"@api/helpers/base64Utils\";\nimport { HidTransportSendApduUnknownError } from \"@api/transport/Errors\";\nimport { TRANSPORT_IDENTIFIER } from \"@api/transport/rnHidTransportIdentifier\";\nimport {\n type InternalConnectionResult,\n type InternalDeviceDisconnected,\n} from \"@api/transport/types\";\n\nimport {\n type NativeDeviceConnectionLost,\n type NativeDiscoveryDevice,\n type NativeInternalConnectionResult,\n type NativeLedgerDevice,\n type NativeLog,\n type NativeSendApduResult,\n} from \"./types\";\n\nexport function mapNativeLedgerDeviceToDeviceModel(\n nativeLedgerDevice: NativeLedgerDevice,\n deviceModelDataSource: DeviceModelDataSource,\n): TransportDeviceModel | null {\n return (\n deviceModelDataSource.filterDeviceModels({\n usbProductId: Number.parseInt(nativeLedgerDevice.usbProductIdMask, 16),\n })[0] ?? null\n );\n}\n\nexport function mapNativeDiscoveryDeviceToTransportDiscoveredDevice(\n nativeDevice: NativeDiscoveryDevice,\n deviceModelDataSource: DeviceModelDataSource,\n): TransportDiscoveredDevice | null {\n const deviceModel = mapNativeLedgerDeviceToDeviceModel(\n nativeDevice.ledgerDevice,\n deviceModelDataSource,\n );\n if (deviceModel == null) return null;\n\n return {\n id: nativeDevice.uid,\n deviceModel,\n transport: TRANSPORT_IDENTIFIER,\n name: nativeDevice.name,\n };\n}\n\nexport function mapNativeTransportLogToLog(log: NativeLog): LogParams {\n let level: LogLevel;\n switch (log.level) {\n case \"error\":\n level = LogLevel.Error;\n break;\n case \"warning\":\n level = LogLevel.Warning;\n break;\n case \"info\":\n level = LogLevel.Info;\n break;\n case \"debug\":\n level = LogLevel.Debug;\n break;\n default:\n assertNever(log.level);\n level = LogLevel.Info;\n break;\n }\n\n return [\n level,\n log.message,\n {\n tag: log.tag,\n data: log.jsonPayload,\n timestamp: Number.parseInt(log.timestamp, 10),\n },\n ];\n}\n\nfunction assertNever(x: never) {\n throw new Error(\"Unexpected object: \" + x);\n}\n\nexport function mapNativeConnectionResultToConnectionResult(\n result: NativeInternalConnectionResult,\n deviceModelDataSource: DeviceModelDataSource,\n): InternalConnectionResult {\n if (result.success) {\n const transportDeviceModel = mapNativeLedgerDeviceToDeviceModel(\n result.ledgerDevice,\n deviceModelDataSource,\n );\n if (!transportDeviceModel)\n return Left(\n new OpeningConnectionError(\n `Could not find device model for the connected device with usbProductIdMask: ${result.ledgerDevice.usbProductIdMask}`,\n ),\n );\n return Right({ sessionId: result.sessionId, transportDeviceModel });\n } else {\n return Left(new OpeningConnectionError(result.error));\n }\n}\n\nexport function mapNativeSendApduResultToSendApduResult(\n result: NativeSendApduResult,\n): SendApduResult {\n if (result.success) {\n const responseBytes = base64ToUint8Array(result.apdu);\n const data = FramerUtils.getFirstBytesFrom(\n responseBytes,\n responseBytes.length - 2,\n );\n const statusCode = FramerUtils.getLastBytesFrom(responseBytes, 2);\n return Right(new ApduResponse({ data, statusCode }));\n } else if (result.error === \"SendApduTimeout\") {\n return Left(new SendApduTimeoutError(\"Abort timeout\"));\n } else if (result.error === \"EmptyResponse\") {\n return Left(new SendApduEmptyResponseError(\"Empty response\"));\n } else if (result.error === \"DeviceDisconnected\") {\n return Left(new DeviceDisconnectedWhileSendingError(\"Device disconnected\"));\n } else {\n return Left(new HidTransportSendApduUnknownError(result.error));\n }\n}\n\nexport function mapNativeDeviceConnectionLostToDeviceDisconnected(\n nativeDeviceConnectionLost: NativeDeviceConnectionLost,\n): InternalDeviceDisconnected {\n return {\n sessionId: nativeDeviceConnectionLost.id,\n };\n}\n"],
5
- "mappings": "AAAA,OACE,gBAAAA,EACA,uCAAAC,EAEA,eAAAC,EACA,YAAAC,EAEA,0BAAAC,EACA,8BAAAC,EAEA,wBAAAC,MAGK,kCACP,OAAS,QAAAC,EAAM,SAAAC,MAAa,YAE5B,OAAS,sBAAAC,MAA0B,2BACnC,OAAS,oCAAAC,MAAwC,wBACjD,OAAS,wBAAAC,MAA4B,0CAe9B,SAASC,EACdC,EACAC,EAC6B,CAC7B,OACEA,EAAsB,mBAAmB,CACvC,aAAc,OAAO,SAASD,EAAmB,iBAAkB,EAAE,CACvE,CAAC,EAAE,CAAC,GAAK,IAEb,CAEO,SAASE,EACdC,EACAF,EACkC,CAClC,MAAMG,EAAcL,EACl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jB,EAAK,IAAID,EAAqB,eAAe,CAAC,EAC5CkB,EAAO,QAAU,gBACnBjB,EAAK,IAAIF,EAA2B,gBAAgB,CAAC,EACnDmB,EAAO,QAAU,qBACnBjB,EAAK,IAAIN,EAAoC,qBAAqB,CAAC,EAEnEM,EAAK,IAAIG,EAAiCc,EAAO,KAAK,CAAC,CAElE,CAEO,SAASM,EACdC,EAC4B,CAC5B,MAAO,CACL,UAAWA,EAA2B,EACxC,CACF",
6
- "names": ["ApduResponse", "DeviceDisconnectedWhileSendingError", "FramerUtils", "LogLevel", "OpeningConnectionError", "SendApduEmptyResponseError", "SendApduTimeoutError", "Left", "Right", "base64ToUint8Array", "HidTransportSendApduUnknownError", "TRANSPORT_IDENTIFIER", "mapNativeLedgerDeviceToDeviceModel", "nativeLedgerDevice", "deviceModelDataSource", "mapNativeDiscoveryDeviceToTransportDiscoveredDevice", "nativeDevice", "deviceModel", "mapNativeTransportLogToLog", "log", "level", "assertNever", "x", "mapNativeConnectionResultToConnectionResult", "result", "transportDeviceModel", "mapNativeSendApduResultToSendApduResult", "responseBytes", "data", "statusCode", "mapNativeDeviceConnectionLostToDeviceDisconnected", "nativeDeviceConnectionLost"]
4
+ "sourcesContent": ["import {\n ApduResponse,\n DeviceDisconnectedBeforeSendingApdu,\n DeviceDisconnectedWhileSendingError,\n type DeviceModelDataSource,\n FramerUtils,\n LogLevel,\n type LogParams,\n OpeningConnectionError,\n SendApduEmptyResponseError,\n type SendApduResult,\n SendApduTimeoutError,\n type TransportDeviceModel,\n type TransportDiscoveredDevice,\n} from \"@ledgerhq/device-management-kit\";\nimport { Left, Right } from \"purify-ts\";\n\nimport { base64ToUint8Array } from \"@api/helpers/base64Utils\";\nimport { HidTransportSendApduUnknownError } from \"@api/transport/Errors\";\nimport { TRANSPORT_IDENTIFIER } from \"@api/transport/rnHidTransportIdentifier\";\nimport {\n type InternalConnectionResult,\n type InternalDeviceDisconnected,\n} from \"@api/transport/types\";\n\nimport {\n type NativeDeviceConnectionLost,\n type NativeDiscoveryDevice,\n type NativeInternalConnectionResult,\n type NativeLedgerDevice,\n type NativeLog,\n type NativeSendApduResult,\n} from \"./types\";\n\nexport function mapNativeLedgerDeviceToDeviceModel(\n nativeLedgerDevice: NativeLedgerDevice,\n deviceModelDataSource: DeviceModelDataSource,\n): TransportDeviceModel | null {\n return (\n deviceModelDataSource.filterDeviceModels({\n usbProductId: Number.parseInt(nativeLedgerDevice.usbProductIdMask, 16),\n })[0] ?? null\n );\n}\n\nexport function mapNativeDiscoveryDeviceToTransportDiscoveredDevice(\n nativeDevice: NativeDiscoveryDevice,\n deviceModelDataSource: DeviceModelDataSource,\n): TransportDiscoveredDevice | null {\n const deviceModel = mapNativeLedgerDeviceToDeviceModel(\n nativeDevice.ledgerDevice,\n deviceModelDataSource,\n );\n if (deviceModel == null) return null;\n\n return {\n id: nativeDevice.uid,\n deviceModel,\n transport: TRANSPORT_IDENTIFIER,\n name: nativeDevice.name,\n };\n}\n\nexport function mapNativeTransportLogToLog(log: NativeLog): LogParams {\n let level: LogLevel;\n switch (log.level) {\n case \"error\":\n level = LogLevel.Error;\n break;\n case \"warning\":\n level = LogLevel.Warning;\n break;\n case \"info\":\n level = LogLevel.Info;\n break;\n case \"debug\":\n level = LogLevel.Debug;\n break;\n default:\n assertNever(log.level);\n level = LogLevel.Info;\n break;\n }\n\n return [\n level,\n log.message,\n {\n tag: log.tag,\n data: log.jsonPayload,\n timestamp: Number.parseInt(log.timestamp, 10),\n },\n ];\n}\n\nfunction assertNever(x: never) {\n throw new Error(\"Unexpected object: \" + x);\n}\n\nexport function mapNativeConnectionResultToConnectionResult(\n result: NativeInternalConnectionResult,\n deviceModelDataSource: DeviceModelDataSource,\n): InternalConnectionResult {\n if (result.success) {\n const transportDeviceModel = mapNativeLedgerDeviceToDeviceModel(\n result.ledgerDevice,\n deviceModelDataSource,\n );\n if (!transportDeviceModel)\n return Left(\n new OpeningConnectionError(\n `Could not find device model for the connected device with usbProductIdMask: ${result.ledgerDevice.usbProductIdMask}`,\n ),\n );\n return Right({ sessionId: result.sessionId, transportDeviceModel });\n } else {\n return Left(new OpeningConnectionError(result.error));\n }\n}\n\nexport function mapNativeSendApduResultToSendApduResult(\n result: NativeSendApduResult,\n): SendApduResult {\n if (result.success) {\n const responseBytes = base64ToUint8Array(result.apdu);\n const data = FramerUtils.getFirstBytesFrom(\n responseBytes,\n responseBytes.length - 2,\n );\n const statusCode = FramerUtils.getLastBytesFrom(responseBytes, 2);\n return Right(new ApduResponse({ data, statusCode }));\n } else {\n switch (result.error) {\n case \"SendApduTimeout\":\n return Left(new SendApduTimeoutError(\"Abort timeout\"));\n case \"EmptyResponse\":\n return Left(new SendApduEmptyResponseError(\"Empty response\"));\n case \"DeviceDisconnected\":\n return Left(\n new DeviceDisconnectedWhileSendingError(\"Device disconnected\"),\n );\n case \"DeviceNotFound\":\n case \"NoUsbEndpointFound\":\n return Left(new DeviceDisconnectedBeforeSendingApdu());\n default:\n return Left(new HidTransportSendApduUnknownError(result.error));\n }\n }\n}\n\nexport function mapNativeDeviceConnectionLostToDeviceDisconnected(\n nativeDeviceConnectionLost: NativeDeviceConnectionLost,\n): InternalDeviceDisconnected {\n return {\n sessionId: nativeDeviceConnectionLost.id,\n };\n}\n"],
5
+ "mappings": "AAAA,OACE,gBAAAA,EACA,uCAAAC,EACA,uCAAAC,EAEA,eAAAC,EACA,YAAAC,EAEA,0BAAAC,EACA,8BAAAC,EAEA,wBAAAC,MAGK,kCACP,OAAS,QAAAC,EAAM,SAAAC,MAAa,YAE5B,OAAS,sBAAAC,MAA0B,2BACnC,OAAS,oCAAAC,MAAwC,wBACjD,OAAS,wBAAAC,MAA4B,0CAe9B,SAASC,EACdC,EACAC,EAC6B,CAC7B,OACEA,EAAsB,mBAAmB,CACvC,aAAc,OAAO,SAASD,EAAmB,iBAAkB,EAAE,CACvE,CAAC,EAAE,CAAC,GAAK,IAEb,CAEO,SAASE,EACdC,EACAF,EACkC,CAClC,MAAMG,EAAcL,EAClB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AK,kBACH,OAAOjB,EAAK,IAAID,EAAqB,eAAe,CAAC,EACvD,IAAK,gBACH,OAAOC,EAAK,IAAIF,EAA2B,gBAAgB,CAAC,EAC9D,IAAK,qBACH,OAAOE,EACL,IAAIN,EAAoC,qBAAqB,CAC/D,EACF,IAAK,iBACL,IAAK,qBACH,OAAOM,EAAK,IAAIP,CAAqC,EACvD,QACE,OAAOO,EAAK,IAAIG,EAAiCc,EAAO,KAAK,CAAC,CAClE,CAEJ,CAEO,SAASM,EACdC,EAC4B,CAC5B,MAAO,CACL,UAAWA,EAA2B,EACxC,CACF",
6
+ "names": ["ApduResponse", "DeviceDisconnectedBeforeSendingApdu", "DeviceDisconnectedWhileSendingError", "FramerUtils", "LogLevel", "OpeningConnectionError", "SendApduEmptyResponseError", "SendApduTimeoutError", "Left", "Right", "base64ToUint8Array", "HidTransportSendApduUnknownError", "TRANSPORT_IDENTIFIER", "mapNativeLedgerDeviceToDeviceModel", "nativeLedgerDevice", "deviceModelDataSource", "mapNativeDiscoveryDeviceToTransportDiscoveredDevice", "nativeDevice", "deviceModel", "mapNativeTransportLogToLog", "log", "level", "assertNever", "x", "mapNativeConnectionResultToConnectionResult", "result", "transportDeviceModel", "mapNativeSendApduResultToSendApduResult", "responseBytes", "data", "statusCode", "mapNativeDeviceConnectionLostToDeviceDisconnected", "nativeDeviceConnectionLost"]
7
7
  }
@@ -1,2 +1,2 @@
1
- import{ApduResponse as l,DeviceModelId as n,LogLevel as i,OpeningConnectionError as c,SendApduEmptyResponseError as p,SendApduTimeoutError as m,StaticDeviceModelDataSource as g}from"@ledgerhq/device-management-kit";import{Left as a,Right as r}from"purify-ts";import{HidTransportSendApduUnknownError as D}from"../transport/Errors";import{TRANSPORT_IDENTIFIER as N}from"../transport/rnHidTransportIdentifier";import{mapNativeConnectionResultToConnectionResult as S,mapNativeDeviceConnectionLostToDeviceDisconnected as A,mapNativeDiscoveryDeviceToTransportDiscoveredDevice as u,mapNativeLedgerDeviceToDeviceModel as R,mapNativeSendApduResultToSendApduResult as d,mapNativeTransportLogToLog as y}from"./mapper";describe("mapper",()=>{const o=new g;describe("mapNativeLedgerDeviceToDeviceModel",()=>{[{nativeLedgerDevice:{name:"NanoS",usbProductIdMask:"0x10"},deviceModel:o.getDeviceModel({id:n.NANO_S})},{nativeLedgerDevice:{name:"NanoX",usbProductIdMask:"0x40"},deviceModel:o.getDeviceModel({id:n.NANO_X})},{nativeLedgerDevice:{name:"NanoSPlus",usbProductIdMask:"0x50"},deviceModel:o.getDeviceModel({id:n.NANO_SP})},{nativeLedgerDevice:{name:"Stax",usbProductIdMask:"0x60"},deviceModel:o.getDeviceModel({id:n.STAX})},{nativeLedgerDevice:{name:"Flex",usbProductIdMask:"0x70"},deviceModel:o.getDeviceModel({id:n.FLEX})},{nativeLedgerDevice:{name:"NanoX",usbProductIdMask:"0x12345678"},deviceModel:null}].forEach(({nativeLedgerDevice:e,deviceModel:s})=>{it(`should map USB device with usbProductIdMask ${e.usbProductIdMask} to ${s?.productName??"null"}`,()=>{expect(R(e,o)).toEqual(s)})})}),describe("mapNativeDiscoveryDeviceToTransportDiscoveredDevice",()=>{it("should map NativeDiscoveryDevice to TransportDiscoveredDevice",()=>{const t={name:"NanoS",uid:"abcd",ledgerDevice:{name:"NanoS",usbProductIdMask:"0x10"}},e={id:"abcd",deviceModel:o.getDeviceModel({id:n.NANO_S}),transport:N,name:"NanoS"};expect(u(t,o)).toEqual(e)}),it("should return null if the device model is not recognized",()=>{const t={name:"NanoX",uid:"efgh",ledgerDevice:{name:"NanoX",usbProductIdMask:"0x4567890"}};expect(u(t,o)).toEqual(null)})}),describe("mapNativeTransportLogToLog",()=>{[{nativeLog:{level:"debug",tag:"tag",message:"debug message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[i.Debug,"debug message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"info",tag:"tag",message:"info message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[i.Info,"info message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"error",tag:"tag",message:"error message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[i.Error,"error message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"warning",tag:"tag",message:"warning message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[i.Warning,"warning message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]}].forEach(({nativeLog:e,log:s})=>{it(`should map NativeLog with level "${e.level}" to Log`,()=>{expect(y(e)).toEqual(s)})})}),describe("mapNativeConnectionResultToConnectionResult",()=>{[{testTitle:"Success",nativeConnectionResult:{success:!0,sessionId:"1234",ledgerDevice:{name:"NanoS",usbProductIdMask:"0x10"},deviceName:"NanoS"},connectionResult:r({sessionId:"1234",transportDeviceModel:o.getDeviceModel({id:n.NANO_S})})},{testTitle:"Failure",nativeConnectionResult:{success:!1,error:"error message"},connectionResult:a(new c("error message"))},{testTitle:"Unknown device model",nativeConnectionResult:{success:!0,sessionId:"1234",ledgerDevice:{name:"NanoX",usbProductIdMask:"0x12345678"},deviceName:"NanoX"},connectionResult:a(new c("Could not find device model for the connected device with usbProductIdMask: 0x12345678"))}].forEach(({testTitle:e,nativeConnectionResult:s,connectionResult:v})=>{it(e,()=>{expect(S(s,o)).toEqual(v)})})}),describe("mapNativeSendApduResultToSendApduResult",()=>{test("success",()=>{const e={success:!0,apdu:"AQIDkAA="},s=r(new l({data:new Uint8Array([1,2,3]),statusCode:new Uint8Array([144,0])}));expect(d(e)).toEqual(s)}),test("failure",()=>{const t={success:!1,error:"error message"},e=a(new D("error message"));expect(d(t)).toEqual(e)}),test("timeout error",()=>{const t={success:!1,error:"SendApduTimeout"},e=a(new m("Abort timeout"));expect(d(t)).toEqual(e)}),test("empty response error",()=>{const t={success:!1,error:"EmptyResponse"},e=a(new p("Empty response"));expect(d(t)).toEqual(e)})}),describe("mapNativeDeviceConnectionLostToDeviceDisconnected",()=>{it("should map NativeDeviceConnectionLost to DeviceDisconnected",()=>{const t={id:"1234"},e={sessionId:"1234"};expect(A(t)).toEqual(e)})})});
1
+ import{ApduResponse as m,DeviceDisconnectedBeforeSendingApdu as c,DeviceModelId as a,LogLevel as d,OpeningConnectionError as r,SendApduEmptyResponseError as g,SendApduTimeoutError as D,StaticDeviceModelDataSource as N}from"@ledgerhq/device-management-kit";import{Left as n,Right as u}from"purify-ts";import{HidTransportSendApduUnknownError as l}from"../transport/Errors";import{TRANSPORT_IDENTIFIER as S}from"../transport/rnHidTransportIdentifier";import{mapNativeConnectionResultToConnectionResult as A,mapNativeDeviceConnectionLostToDeviceDisconnected as R,mapNativeDiscoveryDeviceToTransportDiscoveredDevice as v,mapNativeLedgerDeviceToDeviceModel as x,mapNativeSendApduResultToSendApduResult as i,mapNativeTransportLogToLog as y}from"./mapper";describe("mapper",()=>{const o=new N;describe("mapNativeLedgerDeviceToDeviceModel",()=>{[{nativeLedgerDevice:{name:"NanoS",usbProductIdMask:"0x10"},deviceModel:o.getDeviceModel({id:a.NANO_S})},{nativeLedgerDevice:{name:"NanoX",usbProductIdMask:"0x40"},deviceModel:o.getDeviceModel({id:a.NANO_X})},{nativeLedgerDevice:{name:"NanoSPlus",usbProductIdMask:"0x50"},deviceModel:o.getDeviceModel({id:a.NANO_SP})},{nativeLedgerDevice:{name:"Stax",usbProductIdMask:"0x60"},deviceModel:o.getDeviceModel({id:a.STAX})},{nativeLedgerDevice:{name:"Flex",usbProductIdMask:"0x70"},deviceModel:o.getDeviceModel({id:a.FLEX})},{nativeLedgerDevice:{name:"NanoX",usbProductIdMask:"0x12345678"},deviceModel:null}].forEach(({nativeLedgerDevice:e,deviceModel:s})=>{it(`should map USB device with usbProductIdMask ${e.usbProductIdMask} to ${s?.productName??"null"}`,()=>{expect(x(e,o)).toEqual(s)})})}),describe("mapNativeDiscoveryDeviceToTransportDiscoveredDevice",()=>{it("should map NativeDiscoveryDevice to TransportDiscoveredDevice",()=>{const t={name:"NanoS",uid:"abcd",ledgerDevice:{name:"NanoS",usbProductIdMask:"0x10"}},e={id:"abcd",deviceModel:o.getDeviceModel({id:a.NANO_S}),transport:S,name:"NanoS"};expect(v(t,o)).toEqual(e)}),it("should return null if the device model is not recognized",()=>{const t={name:"NanoX",uid:"efgh",ledgerDevice:{name:"NanoX",usbProductIdMask:"0x4567890"}};expect(v(t,o)).toEqual(null)})}),describe("mapNativeTransportLogToLog",()=>{[{nativeLog:{level:"debug",tag:"tag",message:"debug message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[d.Debug,"debug message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"info",tag:"tag",message:"info message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[d.Info,"info message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"error",tag:"tag",message:"error message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[d.Error,"error message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]},{nativeLog:{level:"warning",tag:"tag",message:"warning message",jsonPayload:{key:"value"},timestamp:"123456789"},log:[d.Warning,"warning message",{timestamp:123456789,tag:"tag",data:{key:"value"}}]}].forEach(({nativeLog:e,log:s})=>{it(`should map NativeLog with level "${e.level}" to Log`,()=>{expect(y(e)).toEqual(s)})})}),describe("mapNativeConnectionResultToConnectionResult",()=>{[{testTitle:"Success",nativeConnectionResult:{success:!0,sessionId:"1234",ledgerDevice:{name:"NanoS",usbProductIdMask:"0x10"},deviceName:"NanoS"},connectionResult:u({sessionId:"1234",transportDeviceModel:o.getDeviceModel({id:a.NANO_S})})},{testTitle:"Failure",nativeConnectionResult:{success:!1,error:"error message"},connectionResult:n(new r("error message"))},{testTitle:"Unknown device model",nativeConnectionResult:{success:!0,sessionId:"1234",ledgerDevice:{name:"NanoX",usbProductIdMask:"0x12345678"},deviceName:"NanoX"},connectionResult:n(new r("Could not find device model for the connected device with usbProductIdMask: 0x12345678"))}].forEach(({testTitle:e,nativeConnectionResult:s,connectionResult:p})=>{it(e,()=>{expect(A(s,o)).toEqual(p)})})}),describe("mapNativeSendApduResultToSendApduResult",()=>{test("success",()=>{const e={success:!0,apdu:"AQIDkAA="},s=u(new m({data:new Uint8Array([1,2,3]),statusCode:new Uint8Array([144,0])}));expect(i(e)).toEqual(s)}),test("failure",()=>{const t={success:!1,error:"error message"},e=n(new l("error message"));expect(i(t)).toEqual(e)}),test("timeout error",()=>{const t={success:!1,error:"SendApduTimeout"},e=n(new D("Abort timeout"));expect(i(t)).toEqual(e)}),test("empty response error",()=>{const t={success:!1,error:"EmptyResponse"},e=n(new g("Empty response"));expect(i(t)).toEqual(e)}),test("device not found error",()=>{const t={success:!1,error:"DeviceNotFound"},e=n(new c);expect(i(t)).toEqual(e)}),test("no usb endpoint found error",()=>{const t={success:!1,error:"NoUsbEndpointFound"},e=n(new c);expect(i(t)).toEqual(e)}),test("unknown error",()=>{const t={success:!1,error:"unknown error"},e=n(new l("unknown error"));expect(i(t)).toEqual(e)})}),describe("mapNativeDeviceConnectionLostToDeviceDisconnected",()=>{it("should map NativeDeviceConnectionLost to DeviceDisconnected",()=>{const t={id:"1234"},e={sessionId:"1234"};expect(R(t)).toEqual(e)})})});
2
2
  //# sourceMappingURL=mapper.test.js.map