@ledgerhq/device-transport-kit-web-ble 0.0.0-try-to-fix-20250429171448 → 0.0.0-web-ble-29-08---20250829104351
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/lib/esm/api/data/WebBleConfig.js +1 -1
- package/lib/esm/api/data/WebBleConfig.js.map +3 -3
- package/lib/esm/api/transport/WebBleApduSender.js +2 -0
- package/lib/esm/api/transport/WebBleApduSender.js.map +7 -0
- package/lib/esm/api/transport/WebBleApduSender.test.js +2 -0
- package/lib/esm/api/transport/WebBleApduSender.test.js.map +7 -0
- package/lib/esm/api/transport/WebBleTransport.js +1 -1
- package/lib/esm/api/transport/WebBleTransport.js.map +3 -3
- package/lib/esm/api/transport/WebBleTransport.test.js +0 -1
- package/lib/esm/api/transport/WebBleTransport.test.js.map +4 -4
- package/lib/esm/package.json +11 -11
- package/lib/types/api/data/WebBleConfig.d.ts +3 -1
- package/lib/types/api/data/WebBleConfig.d.ts.map +1 -1
- package/lib/types/api/model/BleDevice.stub.d.ts.map +1 -1
- package/lib/types/api/transport/WebBleApduSender.d.ts +41 -0
- package/lib/types/api/transport/WebBleApduSender.d.ts.map +1 -0
- package/lib/types/api/transport/WebBleApduSender.test.d.ts +2 -0
- package/lib/types/api/transport/WebBleApduSender.test.d.ts.map +1 -0
- package/lib/types/api/transport/WebBleTransport.d.ts +26 -75
- package/lib/types/api/transport/WebBleTransport.d.ts.map +1 -1
- package/lib/types/api/transport/WebBleTransport.test.d.ts +0 -1
- package/lib/types/tsconfig.prod.tsbuildinfo +1 -1
- package/package.json +14 -14
- package/lib/esm/api/transport/BleDeviceConnection.js +0 -2
- package/lib/esm/api/transport/BleDeviceConnection.js.map +0 -7
- package/lib/esm/api/transport/BleDeviceConnection.test.js +0 -2
- package/lib/esm/api/transport/BleDeviceConnection.test.js.map +0 -7
- package/lib/types/api/transport/BleDeviceConnection.d.ts +0 -98
- package/lib/types/api/transport/BleDeviceConnection.d.ts.map +0 -1
- package/lib/types/api/transport/BleDeviceConnection.test.d.ts +0 -2
- package/lib/types/api/transport/BleDeviceConnection.test.d.ts.map +0 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const E=
|
|
1
|
+
const E=15e3,N=5e3,T=5;export{T as RECONNECTION_RETRY_COUNT,E as RECONNECT_DEVICE_TIMEOUT,N as SINGLE_RECONNECTION_TIMEOUT};
|
|
2
2
|
//# sourceMappingURL=WebBleConfig.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/api/data/WebBleConfig.ts"],
|
|
4
|
-
"sourcesContent": ["export const RECONNECT_DEVICE_TIMEOUT =
|
|
5
|
-
"mappings": "AAAO,MAAMA,EAA2B",
|
|
6
|
-
"names": ["RECONNECT_DEVICE_TIMEOUT"]
|
|
4
|
+
"sourcesContent": ["export const RECONNECT_DEVICE_TIMEOUT = 15000;\nexport const SINGLE_RECONNECTION_TIMEOUT = 5000;\nexport const RECONNECTION_RETRY_COUNT = 5;\n"],
|
|
5
|
+
"mappings": "AAAO,MAAMA,EAA2B,KAC3BC,EAA8B,IAC9BC,EAA2B",
|
|
6
|
+
"names": ["RECONNECT_DEVICE_TIMEOUT", "SINGLE_RECONNECTION_TIMEOUT", "RECONNECTION_RETRY_COUNT"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{DeviceDisconnectedWhileSendingError as c,DeviceNotInitializedError as u,SendApduTimeoutError as _}from"@ledgerhq/device-management-kit";import{Left as d,Maybe as n,Right as f}from"purify-ts";import{BehaviorSubject as y}from"rxjs";const l=8;class R{_deps;_apduSender=n.empty();_apduSenderFactory;_apduReceiverFactory;_apduReceiver;_logger;_isDeviceReady=new y(!1);_notificationsActive=!1;_mtuHandshakeInFlight=!1;_sendResolver=n.empty();_writeMode=null;constructor(e,t){this._deps={writeCharacteristic:e.writeCharacteristic,notifyCharacteristic:e.notifyCharacteristic},this._apduSenderFactory=e.apduSenderFactory,this._apduReceiverFactory=e.apduReceiverFactory,this._apduReceiver=e.apduReceiverFactory(),this._logger=t("WebBleApduSender")}_gattConnected(){try{return!!this._deps.notifyCharacteristic.service.device.gatt?.connected}catch{return!1}}_looksDisconnected(e){const t=e,i=(t?.name??"").toString(),r=(t?.message??"").toString().toLowerCase();return i==="NetworkError"||r.includes("gatt server is disconnected")||r.includes("not connected")||r.includes("cannot perform gatt operations")}_failPendingSend(e){this._sendResolver.map(t=>t(d(e))),this._sendResolver=n.empty()}_markLinkDown(){try{this._notificationsActive&&(this._deps.notifyCharacteristic.removeEventListener("characteristicvaluechanged",this._handleNotify),this._deps.notifyCharacteristic.stopNotifications().catch(()=>{}),this._notificationsActive=!1)}catch{}this._isDeviceReady.next(!1),this._apduSender=n.empty(),this._sendResolver=n.empty(),this._writeMode=null}async _sleep(e){return new Promise(t=>setTimeout(t,e))}_handleNotify=e=>{const t=e.target;if(!t.value)return;const i=new Uint8Array(t.value.buffer);if(!this._isDeviceReady.value){if(!this._mtuHandshakeInFlight){this._logger.debug("Dropping pre-handshake frame",{data:{data:i}});return}if(i.length<6||i[0]!==l){this._logger.debug("Non-MTU frame during handshake; dropping",{data:{data:i}});return}this._onReceiveSetup(i);return}this._onReceiveApdu(i)};_onReceiveSetup(e){const t=e[5];if(t===void 0||!Number.isFinite(t)||t<=0)throw new Error("MTU negotiation failed: invalid MTU");const i=Math.max(t-0,t);this._apduSender=n.of(this._apduSenderFactory({frameSize:i})),this._isDeviceReady.next(!0)}_onReceiveApdu(e){this._apduReceiver.handleFrame(e).map(t=>t.map(i=>{this._logger.debug("Received APDU",{data:{resp:i}}),this._sendResolver.map(r=>r(f(i))),this._sendResolver=n.empty()})).mapLeft(t=>{this._sendResolver.map(i=>i(d(t))),this._sendResolver=n.empty()})}_chooseWriteMode(){if(this._writeMode)return this._writeMode;const e=this._deps.writeCharacteristic;return e.properties.writeWithoutResponse?this._writeMode="withoutResponse":e.properties.write?this._writeMode="withResponse":this._writeMode="legacy",this._writeMode}async _write(e){const t=this._deps.writeCharacteristic;if(!this._gattConnected())throw this._markLinkDown(),new c("GATT not connected");const i=typeof t.writeValueWithResponse=="function",r=typeof t.writeValueWithoutResponse=="function",a=typeof t.writeValue=="function",o=async()=>t.properties.writeWithoutResponse&&r?(await t.writeValueWithoutResponse(e),this._writeMode="withoutResponse",!0):!1,s=async()=>t.properties.write&&i?(await t.writeValueWithResponse(e),this._writeMode="withResponse",!0):t.properties.write&&a?(await t.writeValue(e),this._writeMode="legacy",!0):!1,h=this._chooseWriteMode();try{if(h==="withoutResponse"){if(await o()||await s())return}else if(await s()||await o())return;throw new Error("No supported write method for characteristic")}catch(p){throw this._looksDisconnected(p)||!this._gattConnected()?(this._markLinkDown(),new c("Write failed")):p}}async _awaitReady(e=2e3){if(!(this._notificationsActive&&this._isDeviceReady.value&&this._gattConnected()))return new Promise((t,i)=>{const r=this._isDeviceReady.subscribe(o=>{o&&this._notificationsActive&&this._gattConnected()&&(clearTimeout(a),r.unsubscribe(),t())}),a=setTimeout(()=>{r.unsubscribe(),i(new u("Link not ready"))},e);this._notificationsActive&&this._isDeviceReady.value&&this._gattConnected()&&(clearTimeout(a),r.unsubscribe(),t())})}_isLegacyPing(e){return e.length===5&&e[0]===176&&e[1]===1&&e[2]===0&&e[3]===0&&e[4]===0}async setupConnection(){const e=this._deps.notifyCharacteristic;this._notificationsActive||(await e.startNotifications(),this._logger.debug("Notify armed",{data:{notifyUuid:this._deps.notifyCharacteristic.uuid,writeUuid:this._deps.writeCharacteristic.uuid,props:this._deps.writeCharacteristic.properties}}),this._notificationsActive=!0,e.addEventListener("characteristicvaluechanged",this._handleNotify)),await this._sleep(120),this._mtuHandshakeInFlight=!0,this._isDeviceReady.next(!1),this._apduSender=n.empty(),this._writeMode=null;const t=new Uint8Array([l,0,0,0,0]);try{await this._write(t.buffer),await Promise.race([new Promise((i,r)=>{const a=setTimeout(()=>r(new Error("MTU negotiation timeout")),2e3),o=this._isDeviceReady.subscribe(s=>{s&&(clearTimeout(a),o.unsubscribe(),i())})}),this._sleep(2300).then(()=>{if(!this._gattConnected())throw new c("Link dropped during MTU")})])}catch(i){try{e.removeEventListener("characteristicvaluechanged",this._handleNotify),await e.stopNotifications().catch(()=>{})}finally{this._notificationsActive=!1,this._isDeviceReady.next(!1),this._apduSender=n.empty(),this._writeMode=null}throw i}finally{this._mtuHandshakeInFlight=!1}}async sendApdu(e,t,i){try{const s=Math.max(1800,i??0);await this._awaitReady(s)}catch(s){return d(s)}if(this._isLegacyPing(e)&&await this._sleep(160),!this._gattConnected())return this._markLinkDown(),d(new c("GATT not connected"));if(this._apduSender.isNothing())return d(new u("Unknown MTU / sender not ready"));let r;const a=new Promise(s=>{this._sendResolver=n.of(h=>{r&&clearTimeout(r),s(h)})}),o=this._apduSender.map(s=>s.getFrames(e)).orDefault([]);for(const s of o)try{await this._write(s.getRawData().slice().buffer)}catch(h){const p=t?"Frame write failed during expected drop":"Frame write failed";this._logger[t?"debug":"error"](p,{data:{e:h}}),this._failPendingSend(new c("Write failed"));break}return i&&(r=setTimeout(()=>{this._logger.debug("[sendApdu] Abort timeout triggered"),this._sendResolver.map(s=>s(d(new _("Abort timeout"))))},i)),a}closeConnection(){try{this._failPendingSend(new c("Connection closed")),this._notificationsActive&&(this._deps.notifyCharacteristic.removeEventListener("characteristicvaluechanged",this._handleNotify),this._deps.notifyCharacteristic.stopNotifications().catch(()=>{}),this._notificationsActive=!1),this._deps.notifyCharacteristic.service.device.gatt?.disconnect()}catch{this._logger.error("Failed to disconnect from device")}finally{this._isDeviceReady.next(!1),this._apduSender=n.empty(),this._writeMode=null}}getDependencies(){return this._deps}setDependencies(e){this._failPendingSend(new c("Link changed"));try{this._notificationsActive&&(this._deps.notifyCharacteristic.removeEventListener("characteristicvaluechanged",this._handleNotify),this._deps.notifyCharacteristic.stopNotifications().catch(()=>{}))}catch{}this._notificationsActive=!1,this._isDeviceReady.next(!1),this._apduSender=n.empty(),this._sendResolver=n.empty(),this._writeMode=null,this._deps=e,this._apduReceiver=this._apduReceiverFactory()}}export{R as WebBleApduSender};
|
|
2
|
+
//# sourceMappingURL=WebBleApduSender.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/api/transport/WebBleApduSender.ts"],
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-unsafe-return */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-call */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\nimport {\n type ApduReceiverServiceFactory,\n type ApduResponse,\n type ApduSenderServiceFactory,\n type DeviceApduSender,\n DeviceDisconnectedWhileSendingError,\n DeviceNotInitializedError,\n type DmkError,\n type LoggerPublisherService,\n SendApduTimeoutError,\n} from \"@ledgerhq/device-management-kit\";\nimport { type Either, Left, Maybe, Right } from \"purify-ts\";\nimport { BehaviorSubject } from \"rxjs\";\n\nexport type WebBleApduSenderDependencies = {\n writeCharacteristic: BluetoothRemoteGATTCharacteristic;\n notifyCharacteristic: BluetoothRemoteGATTCharacteristic;\n};\n\nconst MTU_OP = 0x08;\n\ntype WriteMode = \"withoutResponse\" | \"withResponse\" | \"legacy\";\n\nexport class WebBleApduSender\n implements DeviceApduSender<WebBleApduSenderDependencies>\n{\n private _deps: WebBleApduSenderDependencies;\n private _apduSender: Maybe<ReturnType<ApduSenderServiceFactory>> =\n Maybe.empty();\n private _apduSenderFactory: ApduSenderServiceFactory;\n private _apduReceiverFactory: ApduReceiverServiceFactory;\n private _apduReceiver: ReturnType<ApduReceiverServiceFactory>;\n private _logger: LoggerPublisherService;\n\n private _isDeviceReady = new BehaviorSubject<boolean>(false);\n private _notificationsActive = false;\n private _mtuHandshakeInFlight = false;\n\n private _sendResolver: Maybe<(r: Either<DmkError, ApduResponse>) => void> =\n Maybe.empty();\n\n private _writeMode: WriteMode | null = null;\n\n constructor(\n deps: WebBleApduSenderDependencies & {\n apduSenderFactory: ApduSenderServiceFactory;\n apduReceiverFactory: ApduReceiverServiceFactory;\n },\n loggerFactory: (tag: string) => LoggerPublisherService,\n ) {\n this._deps = {\n writeCharacteristic: deps.writeCharacteristic,\n notifyCharacteristic: deps.notifyCharacteristic,\n };\n this._apduSenderFactory = deps.apduSenderFactory;\n this._apduReceiverFactory = deps.apduReceiverFactory;\n this._apduReceiver = deps.apduReceiverFactory();\n this._logger = loggerFactory(\"WebBleApduSender\");\n }\n\n private _gattConnected(): boolean {\n try {\n return !!this._deps.notifyCharacteristic.service.device.gatt?.connected;\n } catch {\n return false;\n }\n }\n\n private _looksDisconnected(e: unknown): boolean {\n const err = e as any;\n const name = (err?.name ?? \"\").toString();\n const msg = (err?.message ?? \"\").toString().toLowerCase();\n return (\n name === \"NetworkError\" ||\n msg.includes(\"gatt server is disconnected\") ||\n msg.includes(\"not connected\") ||\n msg.includes(\"cannot perform gatt operations\")\n );\n }\n\n private _failPendingSend(err: DmkError) {\n this._sendResolver.map((r) => r(Left(err)));\n this._sendResolver = Maybe.empty();\n }\n\n private _markLinkDown(): void {\n try {\n if (this._notificationsActive) {\n this._deps.notifyCharacteristic.removeEventListener(\n \"characteristicvaluechanged\",\n this._handleNotify,\n );\n this._deps.notifyCharacteristic.stopNotifications().catch(() => {});\n this._notificationsActive = false;\n }\n } catch {\n //fill\n }\n this._isDeviceReady.next(false);\n this._apduSender = Maybe.empty();\n this._sendResolver = Maybe.empty();\n this._writeMode = null;\n }\n\n private async _sleep(ms: number) {\n return new Promise((r) => setTimeout(r, ms));\n }\n\n private _handleNotify = (event: Event) => {\n const characteristic = event.target as BluetoothRemoteGATTCharacteristic;\n if (!characteristic.value) return;\n const data = new Uint8Array(characteristic.value.buffer);\n\n if (!this._isDeviceReady.value) {\n if (!this._mtuHandshakeInFlight) {\n this._logger.debug(\"Dropping pre-handshake frame\", { data: { data } });\n return;\n }\n if (data.length < 6 || data[0] !== MTU_OP) {\n this._logger.debug(\"Non-MTU frame during handshake; dropping\", {\n data: { data },\n });\n return;\n }\n this._onReceiveSetup(data);\n return;\n }\n\n this._onReceiveApdu(data);\n };\n\n private _onReceiveSetup(mtuResponseBuffer: Uint8Array) {\n const ledgerMtu = mtuResponseBuffer[5];\n if (\n ledgerMtu === undefined ||\n !Number.isFinite(ledgerMtu) ||\n ledgerMtu <= 0\n ) {\n throw new Error(\"MTU negotiation failed: invalid MTU\");\n }\n\n const frameSize = Math.max(ledgerMtu - 0, ledgerMtu);\n this._apduSender = Maybe.of(this._apduSenderFactory({ frameSize }));\n this._isDeviceReady.next(true);\n }\n\n private _onReceiveApdu(incomingFrame: Uint8Array) {\n this._apduReceiver\n .handleFrame(incomingFrame)\n .map((respOpt) =>\n respOpt.map((resp) => {\n this._logger.debug(\"Received APDU\", { data: { resp } });\n this._sendResolver.map((r) => r(Right(resp)));\n this._sendResolver = Maybe.empty();\n }),\n )\n .mapLeft((err) => {\n this._sendResolver.map((r) => r(Left(err)));\n this._sendResolver = Maybe.empty();\n });\n }\n\n private _chooseWriteMode(): WriteMode {\n if (this._writeMode) return this._writeMode;\n const ch = this._deps.writeCharacteristic;\n\n if (ch.properties.writeWithoutResponse) {\n this._writeMode = \"withoutResponse\";\n } else if (ch.properties.write) {\n this._writeMode = \"withResponse\";\n } else {\n this._writeMode = \"legacy\";\n }\n return this._writeMode;\n }\n\n private async _write(buf: ArrayBuffer) {\n const ch = this._deps.writeCharacteristic;\n\n if (!this._gattConnected()) {\n this._markLinkDown();\n throw new DeviceDisconnectedWhileSendingError(\"GATT not connected\");\n }\n\n const hasWithResp =\n typeof (ch as any).writeValueWithResponse === \"function\";\n const hasWithout =\n typeof (ch as any).writeValueWithoutResponse === \"function\";\n const hasLegacy = typeof (ch as any).writeValue === \"function\";\n\n const tryWithout = async () => {\n if (ch.properties.writeWithoutResponse && hasWithout) {\n await (ch as any).writeValueWithoutResponse(buf);\n this._writeMode = \"withoutResponse\";\n return true;\n }\n return false;\n };\n\n const tryWith = async () => {\n if (ch.properties.write && hasWithResp) {\n await (ch as any).writeValueWithResponse(buf);\n this._writeMode = \"withResponse\";\n return true;\n }\n if (ch.properties.write && hasLegacy) {\n await (ch as any).writeValue(buf);\n this._writeMode = \"legacy\";\n return true;\n }\n return false;\n };\n\n const preferred = this._chooseWriteMode();\n\n try {\n if (preferred === \"withoutResponse\") {\n if (await tryWithout()) return;\n if (await tryWith()) return;\n } else {\n if (await tryWith()) return;\n if (await tryWithout()) return;\n }\n throw new Error(\"No supported write method for characteristic\");\n } catch (e) {\n if (this._looksDisconnected(e) || !this._gattConnected()) {\n this._markLinkDown();\n throw new DeviceDisconnectedWhileSendingError(\"Write failed\");\n }\n\n throw e;\n }\n }\n\n private async _awaitReady(maxMs = 2000): Promise<void> {\n if (\n this._notificationsActive &&\n this._isDeviceReady.value &&\n this._gattConnected()\n )\n return;\n\n return new Promise<void>((resolve, reject) => {\n const sub = this._isDeviceReady.subscribe((ready) => {\n if (!ready) return;\n if (this._notificationsActive && this._gattConnected()) {\n clearTimeout(t);\n sub.unsubscribe();\n resolve();\n }\n });\n\n const t = setTimeout(() => {\n sub.unsubscribe();\n reject(new DeviceNotInitializedError(\"Link not ready\"));\n }, maxMs);\n\n if (\n this._notificationsActive &&\n this._isDeviceReady.value &&\n this._gattConnected()\n ) {\n clearTimeout(t);\n sub.unsubscribe();\n resolve();\n }\n });\n }\n\n private _isLegacyPing(apdu: Uint8Array): boolean {\n return (\n apdu.length === 5 &&\n apdu[0] === 0xb0 &&\n apdu[1] === 0x01 &&\n apdu[2] === 0x00 &&\n apdu[3] === 0x00 &&\n apdu[4] === 0x00\n );\n }\n\n public async setupConnection(): Promise<void> {\n const notifyChar = this._deps.notifyCharacteristic;\n if (!this._notificationsActive) {\n await notifyChar.startNotifications();\n this._logger.debug(\"Notify armed\", {\n data: {\n notifyUuid: this._deps.notifyCharacteristic.uuid,\n writeUuid: this._deps.writeCharacteristic.uuid,\n props: this._deps.writeCharacteristic.properties,\n },\n });\n this._notificationsActive = true;\n notifyChar.addEventListener(\n \"characteristicvaluechanged\",\n this._handleNotify,\n );\n }\n\n await this._sleep(120);\n\n this._mtuHandshakeInFlight = true;\n this._isDeviceReady.next(false);\n this._apduSender = Maybe.empty();\n this._writeMode = null;\n\n const mtuReq = new Uint8Array([MTU_OP, 0, 0, 0, 0]);\n\n try {\n await this._write(mtuReq.buffer);\n\n await Promise.race([\n new Promise<void>((res, rej) => {\n const t = setTimeout(\n () => rej(new Error(\"MTU negotiation timeout\")),\n 2000,\n );\n const sub = this._isDeviceReady.subscribe((ready) => {\n if (ready) {\n clearTimeout(t);\n sub.unsubscribe();\n res();\n }\n });\n }),\n\n this._sleep(2300).then(() => {\n if (!this._gattConnected()) {\n throw new DeviceDisconnectedWhileSendingError(\n \"Link dropped during MTU\",\n );\n }\n }),\n ]);\n } catch (e) {\n try {\n notifyChar.removeEventListener(\n \"characteristicvaluechanged\",\n this._handleNotify,\n );\n await notifyChar.stopNotifications().catch(() => {});\n } finally {\n this._notificationsActive = false;\n this._isDeviceReady.next(false);\n this._apduSender = Maybe.empty();\n this._writeMode = null;\n }\n throw e;\n } finally {\n this._mtuHandshakeInFlight = false;\n }\n }\n\n public async sendApdu(\n apdu: Uint8Array,\n triggersDisconnection?: boolean,\n abortTimeout?: number,\n ): Promise<Either<DmkError, ApduResponse>> {\n try {\n const waitBudget = Math.max(1800, abortTimeout ?? 0);\n await this._awaitReady(waitBudget);\n } catch (e) {\n return Left(e as DmkError);\n }\n\n if (this._isLegacyPing(apdu)) {\n await this._sleep(160);\n }\n\n if (!this._gattConnected()) {\n this._markLinkDown();\n return Left(\n new DeviceDisconnectedWhileSendingError(\n \"GATT not connected\",\n ) as unknown as DmkError,\n );\n }\n\n if (this._apduSender.isNothing()) {\n return Left(\n new DeviceNotInitializedError(\n \"Unknown MTU / sender not ready\",\n ) as unknown as DmkError,\n );\n }\n\n let to: ReturnType<typeof setTimeout> | undefined;\n const promise = new Promise<Either<DmkError, ApduResponse>>((resolve) => {\n this._sendResolver = Maybe.of((r) => {\n if (to) clearTimeout(to);\n resolve(r);\n });\n });\n\n const frames = this._apduSender.map((s) => s.getFrames(apdu)).orDefault([]);\n\n for (const frame of frames) {\n try {\n await this._write(frame.getRawData().slice().buffer);\n } catch (e) {\n const msg = triggersDisconnection\n ? \"Frame write failed during expected drop\"\n : \"Frame write failed\";\n this._logger[triggersDisconnection ? \"debug\" : \"error\"](msg, {\n data: { e },\n });\n this._failPendingSend(\n new DeviceDisconnectedWhileSendingError(\"Write failed\"),\n );\n break;\n }\n }\n\n if (abortTimeout) {\n to = setTimeout(() => {\n this._logger.debug(\"[sendApdu] Abort timeout triggered\");\n this._sendResolver.map((resolve) =>\n resolve(Left(new SendApduTimeoutError(\"Abort timeout\"))),\n );\n }, abortTimeout);\n }\n\n return promise;\n }\n\n public closeConnection(): void {\n try {\n this._failPendingSend(\n new DeviceDisconnectedWhileSendingError(\"Connection closed\"),\n );\n if (this._notificationsActive) {\n this._deps.notifyCharacteristic.removeEventListener(\n \"characteristicvaluechanged\",\n this._handleNotify,\n );\n this._deps.notifyCharacteristic.stopNotifications().catch(() => {});\n this._notificationsActive = false;\n }\n this._deps.notifyCharacteristic.service.device.gatt?.disconnect();\n } catch {\n this._logger.error(\"Failed to disconnect from device\");\n } finally {\n this._isDeviceReady.next(false);\n this._apduSender = Maybe.empty();\n this._writeMode = null;\n }\n }\n\n public getDependencies(): WebBleApduSenderDependencies {\n return this._deps;\n }\n\n public setDependencies(deps: WebBleApduSenderDependencies): void {\n this._failPendingSend(\n new DeviceDisconnectedWhileSendingError(\"Link changed\"),\n );\n\n try {\n if (this._notificationsActive) {\n this._deps.notifyCharacteristic.removeEventListener(\n \"characteristicvaluechanged\",\n this._handleNotify,\n );\n this._deps.notifyCharacteristic.stopNotifications().catch(() => {});\n }\n } catch {\n //fill\n }\n\n this._notificationsActive = false;\n this._isDeviceReady.next(false);\n this._apduSender = Maybe.empty();\n this._sendResolver = Maybe.empty();\n this._writeMode = null;\n\n this._deps = deps;\n this._apduReceiver = this._apduReceiverFactory();\n }\n}\n"],
|
|
5
|
+
"mappings": "AAKA,OAKE,uCAAAA,EACA,6BAAAC,EAGA,wBAAAC,MACK,kCACP,OAAsB,QAAAC,EAAM,SAAAC,EAAO,SAAAC,MAAa,YAChD,OAAS,mBAAAC,MAAuB,OAOhC,MAAMC,EAAS,EAIR,MAAMC,CAEb,CACU,MACA,YACNJ,EAAM,MAAM,EACN,mBACA,qBACA,cACA,QAEA,eAAiB,IAAIE,EAAyB,EAAK,EACnD,qBAAuB,GACvB,sBAAwB,GAExB,cACNF,EAAM,MAAM,EAEN,WAA+B,KAEvC,YACEK,EAIAC,EACA,CACA,KAAK,MAAQ,CACX,oBAAqBD,EAAK,oBAC1B,qBAAsBA,EAAK,oBAC7B,EACA,KAAK,mBAAqBA,EAAK,kBAC/B,KAAK,qBAAuBA,EAAK,oBACjC,KAAK,cAAgBA,EAAK,oBAAoB,EAC9C,KAAK,QAAUC,EAAc,kBAAkB,CACjD,CAEQ,gBAA0B,CAChC,GAAI,CACF,MAAO,CAAC,CAAC,KAAK,MAAM,qBAAqB,QAAQ,OAAO,MAAM,SAChE,MAAQ,CACN,MAAO,EACT,CACF,CAEQ,mBAAmB,EAAqB,CAC9C,MAAMC,EAAM,EACNC,GAAQD,GAAK,MAAQ,IAAI,SAAS,EAClCE,GAAOF,GAAK,SAAW,IAAI,SAAS,EAAE,YAAY,EACxD,OACEC,IAAS,gBACTC,EAAI,SAAS,6BAA6B,GAC1CA,EAAI,SAAS,eAAe,GAC5BA,EAAI,SAAS,gCAAgC,CAEjD,CAEQ,iBAAiBF,EAAe,CACtC,KAAK,cAAc,IAAKG,GAAMA,EAAEX,EAAKQ,CAAG,CAAC,CAAC,EAC1C,KAAK,cAAgBP,EAAM,MAAM,CACnC,CAEQ,eAAsB,CAC5B,GAAI,CACE,KAAK,uBACP,KAAK,MAAM,qBAAqB,oB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iBAAgD,CACrD,OAAO,KAAK,KACd,CAEO,gBAAgBK,EAA0C,CAC/D,KAAK,iBACH,IAAIT,EAAoC,cAAc,CACxD,EAEA,GAAI,CACE,KAAK,uBACP,KAAK,MAAM,qBAAqB,oBAC9B,6BACA,KAAK,aACP,EACA,KAAK,MAAM,qBAAqB,kBAAkB,EAAE,MAAM,IAAM,CAAC,CAAC,EAEtE,MAAQ,CAER,CAEA,KAAK,qBAAuB,GAC5B,KAAK,eAAe,KAAK,EAAK,EAC9B,KAAK,YAAcI,EAAM,MAAM,EAC/B,KAAK,cAAgBA,EAAM,MAAM,EACjC,KAAK,WAAa,KAElB,KAAK,MAAQK,EACb,KAAK,cAAgB,KAAK,qBAAqB,CACjD,CACF",
|
|
6
|
+
"names": ["DeviceDisconnectedWhileSendingError", "DeviceNotInitializedError", "SendApduTimeoutError", "Left", "Maybe", "Right", "BehaviorSubject", "MTU_OP", "WebBleApduSender", "deps", "loggerFactory", "err", "name", "msg", "r", "ms", "event", "characteristic", "data", "mtuResponseBuffer", "ledgerMtu", "frameSize", "incomingFrame", "respOpt", "resp", "ch", "buf", "hasWithResp", "hasWithout", "hasLegacy", "tryWithout", "tryWith", "preferred", "e", "maxMs", "resolve", "reject", "sub", "ready", "t", "apdu", "notifyChar", "mtuReq", "res", "rej", "triggersDisconnection", "abortTimeout", "waitBudget", "to", "promise", "frames", "frame"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{Maybe as A,Right as R}from"purify-ts";import{afterEach as E,beforeEach as k,describe as B,expect as n,it as o,vi as t}from"vitest";import{WebBleApduSender as V}from"./WebBleApduSender";class L{subscribers=[];tag;constructor(r,i){this.subscribers=r,this.tag=i}debug=t.fn();info=t.fn();warn=t.fn();error=t.fn()}function d(){const e=t.fn(),r=t.fn().mockResolvedValue(void 0);return{service:{device:{gatt:{connected:!0,disconnect:e}}},uuid:"mock-uuid",properties:{broadcast:!1,read:!1,writeWithoutResponse:!1,write:!1,notify:!1,indicate:!1,authenticatedSignedWrites:!1,reliableWrite:!1,writableAuxiliaries:!1},getDescriptor:t.fn().mockResolvedValue(void 0),getDescriptors:t.fn().mockResolvedValue([]),startNotifications:t.fn().mockResolvedValue(void 0),addEventListener:t.fn(),removeEventListener:t.fn(),stopNotifications:r,writeValueWithResponse:t.fn().mockResolvedValue(void 0),writeValueWithoutResponse:t.fn().mockResolvedValue(void 0),readValue:t.fn().mockResolvedValue(new DataView(new ArrayBuffer(0))),writeValue:t.fn().mockResolvedValue(void 0),dispatchEvent:t.fn(),oncharacteristicvaluechanged:null}}let u,a,g,m,y,s;const h=()=>new Promise(e=>setImmediate(e));k(()=>{u=d(),a=d(),g=t.fn().mockReturnValue({getFrames:e=>[{getRawData:()=>new DataView(e.buffer)}]}),m=t.fn().mockReturnValue({handleFrame:t.fn(e=>R(A.of({data:new Uint8Array([144,0])})))}),y=e=>new L([],e),s=new V({writeCharacteristic:u,notifyCharacteristic:a,apduSenderFactory:g,apduReceiverFactory:m},y)});E(()=>{t.restoreAllMocks()});B("WebBleApduSender",()=>{o("getDependencies returns initial chars",()=>{const e=s.getDependencies();n(e.writeCharacteristic).toBe(u),n(e.notifyCharacteristic).toBe(a)}),o("setupConnection negotiates MTU and listens",async()=>{const e=s.setupConnection();n(a.startNotifications).toHaveBeenCalled(),await h();const i=a.addEventListener.mock.calls.filter(([l])=>l==="characteristicvaluechanged")[0];if(!i)throw new Error("No event registered for 'characteristicvaluechanged'");const c=i[1],f=new Uint8Array([0,0,0,0,0,32]).buffer;c({target:{value:{buffer:f}}}),await n(e).resolves.toBeUndefined()}),o("sendApdu writes frames and resolves on notification",async()=>{const e=s.setupConnection();await h();const r=a.addEventListener.mock.calls.find(([v])=>v==="characteristicvaluechanged");if(!r)throw new Error("No event registered for 'characteristicvaluechanged'");const i=r[1];i({target:{value:{buffer:new Uint8Array([0,0,0,0,0,32]).buffer}}}),await e;const c=new Uint8Array([1,2,3]),f=s.sendApdu(c);n(u.writeValueWithResponse).toHaveBeenCalledWith(c.buffer),await h();const l=a.addEventListener.mock.calls.filter(([v])=>v==="characteristicvaluechanged"),p=l[l.length-1];if(!p)throw new Error("No APDU handler registered.");const[,C]=p,b=new Uint8Array([144,0]).buffer;C({target:{value:{buffer:b}}});const w=await f;n(w.isRight()).toBe(!0),n(w.extract().data).toEqual(new Uint8Array([144,0]))}),o("closeConnection calls disconnect",()=>{s.closeConnection(),n(a.service.device.gatt.disconnect).toHaveBeenCalled()}),o("setDependencies swaps characteristics",async()=>{const e=d(),r=d();a.service.device.gatt.connected=!0,await s.setDependencies({writeCharacteristic:r,notifyCharacteristic:e}),n(a.removeEventListener).toHaveBeenCalled(),n(e.startNotifications).toHaveBeenCalled(),n(e.addEventListener).toHaveBeenCalledWith("characteristicvaluechanged",n.any(Function));const i=s.getDependencies();n(i.notifyCharacteristic).toBe(e),n(i.writeCharacteristic).toBe(r)})});
|
|
2
|
+
//# sourceMappingURL=WebBleApduSender.test.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/api/transport/WebBleApduSender.test.ts"],
|
|
4
|
+
"sourcesContent": ["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport {\n type ApduReceiverServiceFactory,\n type ApduResponse,\n type ApduSenderServiceFactory,\n type LoggerPublisherService,\n} from \"@ledgerhq/device-management-kit\";\nimport { Maybe, Right } from \"purify-ts\";\nimport { afterEach, beforeEach, describe, expect, it, vi } from \"vitest\";\n\nimport { WebBleApduSender } from \"./WebBleApduSender\";\n\ntype EventListener = (event: Event) => void;\nclass LoggerStub implements LoggerPublisherService {\n subscribers: any[] = [];\n tag: string;\n constructor(subs: any[], tag: string) {\n this.subscribers = subs;\n this.tag = tag;\n }\n debug = vi.fn();\n info = vi.fn();\n warn = vi.fn();\n error = vi.fn();\n}\n\nfunction makeCharacteristic() {\n const disconnect = vi.fn();\n const stopNotifications = vi.fn().mockResolvedValue(undefined);\n return {\n service: { device: { gatt: { connected: true, disconnect } } },\n uuid: \"mock-uuid\",\n properties: {\n broadcast: false,\n read: false,\n writeWithoutResponse: false,\n write: false,\n notify: false,\n indicate: false,\n authenticatedSignedWrites: false,\n reliableWrite: false,\n writableAuxiliaries: false,\n },\n getDescriptor: vi.fn().mockResolvedValue(undefined),\n getDescriptors: vi.fn().mockResolvedValue([]),\n startNotifications: vi.fn().mockResolvedValue(undefined),\n addEventListener: vi.fn(),\n removeEventListener: vi.fn(),\n stopNotifications,\n writeValueWithResponse: vi.fn().mockResolvedValue(undefined),\n writeValueWithoutResponse: vi.fn().mockResolvedValue(undefined),\n readValue: vi.fn().mockResolvedValue(new DataView(new ArrayBuffer(0))),\n writeValue: vi.fn().mockResolvedValue(undefined),\n dispatchEvent: vi.fn(),\n oncharacteristicvaluechanged: null,\n };\n}\n\nlet writeChar: any;\nlet notifyChar: any;\nlet apduSenderFactory: ApduSenderServiceFactory;\nlet apduReceiverFactory: ApduReceiverServiceFactory;\nlet loggerFactory: (tag: string) => LoggerPublisherService;\nlet sender: WebBleApduSender;\n\nconst flushPromises = () =>\n new Promise<void>((resolve) => setImmediate(resolve));\n\nbeforeEach(() => {\n writeChar = makeCharacteristic();\n notifyChar = makeCharacteristic();\n\n apduSenderFactory = vi.fn().mockReturnValue({\n getFrames: (apdu: Uint8Array) => [\n { getRawData: () => new DataView(apdu.buffer) },\n ],\n });\n\n apduReceiverFactory = vi.fn().mockReturnValue({\n handleFrame: vi.fn((_frame: Uint8Array) =>\n Right(Maybe.of({ data: new Uint8Array([0x90, 0x00]) } as ApduResponse)),\n ),\n });\n\n loggerFactory = (tag: string) => new LoggerStub([], tag);\n\n sender = new WebBleApduSender(\n {\n writeCharacteristic: writeChar,\n notifyCharacteristic: notifyChar,\n apduSenderFactory,\n apduReceiverFactory,\n },\n loggerFactory,\n );\n});\n\nafterEach(() => {\n vi.restoreAllMocks();\n});\n\ndescribe(\"WebBleApduSender\", () => {\n it(\"getDependencies returns initial chars\", () => {\n // when\n const deps = sender.getDependencies();\n\n // then\n expect(deps.writeCharacteristic).toBe(writeChar);\n expect(deps.notifyCharacteristic).toBe(notifyChar);\n });\n\n it(\"setupConnection negotiates MTU and listens\", async () => {\n // when\n const promise = sender.setupConnection();\n\n // then\n expect(notifyChar.startNotifications).toHaveBeenCalled();\n await flushPromises();\n\n const filteredCalls = (\n notifyChar.addEventListener.mock.calls as [string, EventListener][]\n ).filter(([event]) => event === \"characteristicvaluechanged\");\n\n const firstCall = filteredCalls[0];\n\n if (!firstCall) {\n throw new Error(\"No event registered for 'characteristicvaluechanged'\");\n }\n\n const handler = firstCall[1];\n\n // given\n const mtuBuf = new Uint8Array([0, 0, 0, 0, 0, 0x20]).buffer;\n\n // when\n handler({ target: { value: { buffer: mtuBuf } } } as unknown as Event);\n\n // then\n await expect(promise).resolves.toBeUndefined();\n });\n\n it(\"sendApdu writes frames and resolves on notification\", async () => {\n // given\n const setupPromise = sender.setupConnection();\n await flushPromises();\n\n const mtuCall = (\n notifyChar.addEventListener.mock.calls as [string, EventListener][]\n ).find(([event]) => event === \"characteristicvaluechanged\");\n\n if (!mtuCall)\n throw new Error(\"No event registered for 'characteristicvaluechanged'\");\n\n const mtuHandler = mtuCall[1];\n\n mtuHandler({\n target: {\n value: { buffer: new Uint8Array([0, 0, 0, 0, 0, 0x20]).buffer },\n },\n } as unknown as Event);\n\n await setupPromise;\n\n const apduCmd = new Uint8Array([0x01, 0x02, 0x03]);\n\n // when\n const promise = sender.sendApdu(apduCmd);\n\n // then\n expect(writeChar.writeValueWithResponse).toHaveBeenCalledWith(\n apduCmd.buffer,\n );\n await flushPromises();\n\n const filteredCalls = (\n notifyChar.addEventListener.mock.calls as [string, EventListener][]\n ).filter(([event]) => event === \"characteristicvaluechanged\");\n\n const lastCall = filteredCalls[filteredCalls.length - 1];\n if (!lastCall) throw new Error(\"No APDU handler registered.\");\n\n const [, apduHandler] = lastCall;\n\n // when\n const respBuf = new Uint8Array([0x90, 0x00]).buffer;\n apduHandler({ target: { value: { buffer: respBuf } } } as unknown as Event);\n\n // then\n const result = await promise;\n\n expect(result.isRight()).toBe(true);\n expect((result.extract() as ApduResponse).data).toEqual(\n new Uint8Array([0x90, 0x00]),\n );\n });\n\n it(\"closeConnection calls disconnect\", () => {\n // when\n sender.closeConnection();\n\n // then\n expect(notifyChar.service.device.gatt.disconnect).toHaveBeenCalled();\n });\n\n it(\"setDependencies swaps characteristics\", async () => {\n // given\n const newNotify = makeCharacteristic();\n const newWrite = makeCharacteristic();\n notifyChar.service.device.gatt.connected = true;\n\n // when\n await sender.setDependencies({\n writeCharacteristic:\n newWrite as unknown as BluetoothRemoteGATTCharacteristic,\n notifyCharacteristic:\n newNotify as unknown as BluetoothRemoteGATTCharacteristic,\n });\n\n // then\n expect(notifyChar.removeEventListener).toHaveBeenCalled();\n expect(newNotify.startNotifications).toHaveBeenCalled();\n expect(newNotify.addEventListener).toHaveBeenCalledWith(\n \"characteristicvaluechanged\",\n expect.any(Function),\n );\n\n const deps = sender.getDependencies();\n expect(deps.notifyCharacteristic).toBe(newNotify);\n expect(deps.writeCharacteristic).toBe(newWrite);\n });\n});\n"],
|
|
5
|
+
"mappings": "AAOA,OAAS,SAAAA,EAAO,SAAAC,MAAa,YAC7B,OAAS,aAAAC,EAAW,cAAAC,EAAY,YAAAC,EAAU,UAAAC,EAAQ,MAAAC,EAAI,MAAAC,MAAU,SAEhE,OAAS,oBAAAC,MAAwB,qBAGjC,MAAMC,CAA6C,CACjD,YAAqB,CAAC,EACtB,IACA,YAAYC,EAAaC,EAAa,CACpC,KAAK,YAAcD,EACnB,KAAK,IAAMC,CACb,CACA,MAAQJ,EAAG,GAAG,EACd,KAAOA,EAAG,GAAG,EACb,KAAOA,EAAG,GAAG,EACb,MAAQA,EAAG,GAAG,CAChB,CAEA,SAASK,GAAqB,CAC5B,MAAMC,EAAaN,EAAG,GAAG,EACnBO,EAAoBP,EAAG,GAAG,EAAE,kBAAkB,MAAS,EAC7D,MAAO,CACL,QAA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kBAAkB,EAAE,iBAAiB,EACtDnC,EAAOmC,EAAU,gBAAgB,EAAE,qBACjC,6BACAnC,EAAO,IAAI,QAAQ,CACrB,EAEA,MAAMoB,EAAOL,EAAO,gBAAgB,EACpCf,EAAOoB,EAAK,oBAAoB,EAAE,KAAKe,CAAS,EAChDnC,EAAOoB,EAAK,mBAAmB,EAAE,KAAKgB,CAAQ,CAChD,CAAC,CACH,CAAC",
|
|
6
|
+
"names": ["Maybe", "Right", "afterEach", "beforeEach", "describe", "expect", "it", "vi", "WebBleApduSender", "LoggerStub", "subs", "tag", "makeCharacteristic", "disconnect", "stopNotifications", "writeChar", "notifyChar", "apduSenderFactory", "apduReceiverFactory", "loggerFactory", "sender", "flushPromises", "resolve", "apdu", "_frame", "deps", "promise", "firstCall", "event", "handler", "mtuBuf", "setupPromise", "mtuCall", "mtuHandler", "apduCmd", "filteredCalls", "lastCall", "apduHandler", "respBuf", "result", "newNotify", "newWrite"]
|
|
7
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{DeviceConnectionStateMachine as m,GeneralDmkError as _,OpeningConnectionError as l,TransportConnectedDevice as w,UnknownDeviceError as g}from"@ledgerhq/device-management-kit";import{Left as y,Right as p}from"purify-ts";import{BehaviorSubject as D,from as T}from"rxjs";import{switchMap as S}from"rxjs/operators";import{RECONNECT_DEVICE_TIMEOUT as C}from"../data/WebBleConfig";import{WebBleApduSender as b}from"./WebBleApduSender";const h="WEB-BLE-RN-STYLE";class B{constructor(e,i,t,r){this.loggerFactory=i;this._deviceModelDataSource=e,this._apduSenderFactory=t,this._apduReceiverFactory=r,this._logger=i("WebBleTransportRnStyle")}_logger;_deviceModelDataSource;_apduSenderFactory;_apduReceiverFactory;_deviceConnectionsById=new Map;_registry=new Map;_devices$=new D([]);isSupported(){return typeof navigator<"u"&&!!navigator.bluetooth}getIdentifier(){return h}startDiscovering(){const e=this._deviceModelDataSource.getBluetoothServices().map(t=>({services:[t]})),i=this._deviceModelDataSource.getBluetoothServices();return T(navigator.bluetooth.requestDevice({filters:e,optionalServices:i})).pipe(S(async t=>{const{serviceUuid:r,infos:n}=await this._identifyLedgerService(t),o={id:t.id,deviceModel:n.deviceModel,transport:h};return this._registry.set(t.id,{device:t,serviceUuid:r,infos:n,discovered:o}),this._emitDevices(),o}))}stopDiscovering(){}listenToAvailableDevices(){return this._emitDevices(),this._devices$.asObservable()}async connect(e){const i=this._deviceConnectionsById.get(e.deviceId),t=this._registry.get(e.deviceId);if(!t)return y(new g(`Unknown device ${e.deviceId}`));if(i)return p(new w({id:e.deviceId,deviceModel:t.infos.deviceModel,type:"BLE",transport:h,sendApdu:(...r)=>i.sendApdu(...r)}));try{const r=t.device;if(!r.gatt)throw new l("No GATT server available on device");r.gatt.connected||(await this._withTimeout(r.gatt.connect(),6e3,"GATT connect timed out"),await this._delay(150));const{service:n,infos:o}=await this._getLiveLedgerService(r),{writeCharacteristic:s,notifyCharacteristic:c}=await this._resolveCharacteristics(n,o),a=new b({writeCharacteristic:s,notifyCharacteristic:c,apduSenderFactory:this._apduSenderFactory,apduReceiverFactory:this._apduReceiverFactory},this.loggerFactory),d=new m({deviceId:e.deviceId,deviceApduSender:a,timeoutDuration:C,tryToReconnect:()=>{this.tryToReconnect(e.deviceId,e.onReconnect).catch(v=>this._logger.error("tryToReconnect() threw",{data:{e:v}}))},onTerminated:()=>{try{this._deviceConnectionsById.get(e.deviceId)?.closeConnection(),e.onDisconnect(e.deviceId)}finally{this._deviceConnectionsById.delete(e.deviceId);const v=this._registry.get(e.deviceId);if(v?.listener){try{v.device.removeEventListener("gattserverdisconnected",v.listener)}catch{}v.listener=void 0}this._emitDevices()}}});await a.setupConnection(),this._deviceConnectionsById.set(e.deviceId,d),t.serviceUuid=n.uuid,t.infos=o;const u=v=>this._handleDeviceDisconnected(e.deviceId);return t.listener=u,r.addEventListener("gattserverdisconnected",u),this._emitDevices(),p(new w({id:e.deviceId,deviceModel:o.deviceModel,type:"BLE",transport:h,sendApdu:(...v)=>d.sendApdu(...v)}))}catch(r){return this._logger.error("connect() error",{data:{e:r}}),y(new l(r))}}async disconnect(e){const i=e.connectedDevice.id,t=this._deviceConnectionsById.get(i),r=this._registry.get(i);if(!t)return y(new g(`Unknown device ${i}`));try{if(t.closeConnection(),this._deviceConnectionsById.delete(i),r?.listener){try{r.device.removeEventListener("gattserverdisconnected",r.listener)}catch{}r.listener=void 0}return await this._safeCancel(i),this._emitDevices(),p(void 0)}catch(n){return y(new _({originalError:n}))}}_handleDeviceDisconnected(e){this._logger.debug(`[${e}] gattserverdisconnected`);const i=this._deviceConnectionsById.get(e);i&&i.eventDeviceDisconnected()}async _waitForInRange(e,i={}){const{startTimeoutMs:t=500,advTimeoutMs:r=1500}=i;if(typeof e.watchAdvertisements!="function")return!1;const n=new AbortController,o=setTimeout(()=>n.abort(),t),s=await e.watchAdvertisements({signal:n.signal}).then(()=>!0).catch(()=>!1);if(clearTimeout(o),!s)return!1;const c=await new Promise(a=>{const d=()=>{e.removeEventListener("advertisementreceived",d),a(!0)};e.addEventListener("advertisementreceived",d),setTimeout(()=>{e.removeEventListener("advertisementreceived",d),a(!1)},r)});try{n.abort()}catch{}return c}async _rediscoverDevice(e){if(typeof navigator.bluetooth.getDevices!="function")return null;try{this._logger.debug(`Attempting to rediscover device ${e}`);const i=await navigator.bluetooth.getDevices();let t=i.find(r=>r.id===e)??null;if(!t){const r=this._registry.get(e)?.device?.name;r&&(t=i.find(n=>n.name===r)??null)}if(t){const r=await this._waitForInRange(t,{startTimeoutMs:400,advTimeoutMs:1200});this._logger.debug(`Rediscovered ${t.id}, inRange=${r}`)}return t}catch{return null}}async tryToReconnect(e,i){for(await this._safeCancel(e);;){const t=this._registry.get(e),r=this._deviceConnectionsById.get(e);if(!t||!r){this._logger.debug(`[${e}] aborting reconnect: registry or SM missing`);return}try{const n=await this._withTimeout(this._rediscoverDevice(e),3e3,"rediscovery timeout");if(!n)throw new Error("Device not found");if(!n.gatt)throw new Error("No GATT on device");try{n.gatt.connect()}catch(u){this._logger.error(`[${e}] gatt.connect() failed`,{data:{e:u}}),n.gatt?.connected&&n.gatt.disconnect()}const{service:o,infos:s}=await this._getLiveLedgerService(n),{writeCharacteristic:c,notifyCharacteristic:a}=await this._resolveCharacteristics(o,s);if(r.setDependencies({writeCharacteristic:c,notifyCharacteristic:a}),await r.setupConnection(),r.eventDeviceConnected(),t.serviceUuid=o.uuid,t.infos=s,t.listener)try{n.removeEventListener("gattserverdisconnected",t.listener)}catch{}const d=u=>this._handleDeviceDisconnected(e);n.addEventListener("gattserverdisconnected",d),t.listener=d;try{await i?.(e)}catch(u){this._logger.error("onReconnect callback threw",{data:{e:u}})}this._emitDevices();return}catch(n){this._logger.error(`[${e}] reconnect attempt failed`,{data:{e:n}});try{t?.device.gatt?.connected&&t.device.gatt.disconnect()}catch{}await this._delay(500);continue}}}async _safeCancel(e){const i=this._registry.get(e);if(i){try{i.device.gatt?.connected&&i.device.gatt.disconnect()}catch{}await this._delay(100)}}async _identifyLedgerService(e){if(!e.gatt)throw new l("No GATT server available on device");try{await this._withTimeout(e.gatt.connect(),6e3,"connect timeout");const{service:i,infos:t}=await this._getLiveLedgerService(e);return{serviceUuid:i.uuid,infos:t}}finally{try{e.gatt?.disconnect()}catch{}await this._delay(200)}}async _getLiveLedgerService(e){const i=this._deviceModelDataSource.getBluetoothServices(),t=this._registry.get(e.id)?.serviceUuid,r=t?[t,...i.filter(n=>n!==t)]:i.slice();for(const n of r)try{const o=await e.gatt.getPrimaryService(n),s=this._deviceModelDataSource.getBluetoothServicesInfos()[o.uuid];if(!s)throw new g(e.name||"");return{service:o,infos:s}}catch(o){if(o?.name==="SecurityError")throw new l(`Missing Web Bluetooth permission for service ${n}. Add it to optionalServices in requestDevice().`);try{const c=(await e.gatt.getPrimaryServices()).find(a=>a.uuid.toLowerCase()===n.toLowerCase());if(c){const a=this._deviceModelDataSource.getBluetoothServicesInfos()[c.uuid];if(!a)throw new g(e.name||"");return{service:c,infos:a}}}catch{}}throw new l("Ledger GATT service not found")}async _resolveCharacteristics(e,i){const t=await e.getCharacteristic(i.notifyUuid);return{writeCharacteristic:await this._pluckWriteCharacteristic(e,i),notifyCharacteristic:t}}async _pluckWriteCharacteristic(e,i){const t=[i.writeCmdUuid,i.writeUuid].filter(Boolean),r=[];for(const n of t)try{const o=await e.getCharacteristic(n);if(r.push(o),o.properties.writeWithoutResponse||o.properties.write)return o}catch{}if(r.length)return r[0];throw new l("No write characteristic available")}_emitDevices(){const e=[];for(const[t]of this._deviceConnectionsById){const r=this._registry.get(t);r?.infos&&e.push({id:t,deviceModel:r.infos.deviceModel,transport:h})}const i=Array.from(this._registry.values()).map(t=>t.discovered).filter(t=>!this._deviceConnectionsById.has(t.id));this._devices$.next([...e,...i])}_withTimeout(e,i,t,r){return new Promise((n,o)=>{const s=setTimeout(()=>{o(new l(t));try{r?.()}catch{this._logger.debug("Cancellation function threw")}},i);e.then(c=>{clearTimeout(s),n(c)},c=>{this._logger.error("_withTimeout() promise rejected",{data:{e:c}}),clearTimeout(s),o(c)})})}_delay(e){return new Promise(i=>setTimeout(i,e))}}const F=({deviceModelDataSource:f,loggerServiceFactory:e,apduSenderServiceFactory:i,apduReceiverServiceFactory:t})=>new B(f,e,i,t);export{B as WebBleTransport,h as webBleIdentifier,F as webBleTransportFactory};
|
|
2
2
|
//# sourceMappingURL=WebBleTransport.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/api/transport/WebBleTransport.ts"],
|
|
4
|
-
"sourcesContent": ["import {\n type ApduReceiverServiceFactory,\n type ApduSenderServiceFactory,\n type BleDeviceInfos,\n type ConnectError,\n type ConnectionType,\n DeviceAlreadyConnectedError,\n type DeviceId,\n type DeviceModelDataSource,\n DeviceNotRecognizedError,\n type DisconnectHandler,\n type DmkError,\n type LoggerPublisherService,\n NoAccessibleDeviceError,\n OpeningConnectionError,\n type Transport,\n TransportConnectedDevice,\n type TransportDiscoveredDevice,\n type TransportFactory,\n type TransportIdentifier,\n UnknownDeviceError,\n} from \"@ledgerhq/device-management-kit\";\nimport { type Either, EitherAsync, Left, Maybe, Right } from \"purify-ts\";\nimport { from, type Observable, switchMap, timer } from \"rxjs\";\nimport { v4 as uuid } from \"uuid\";\n\nimport { RECONNECT_DEVICE_TIMEOUT } from \"@api/data/WebBleConfig\";\nimport {\n BleDeviceGattServerError,\n BleTransportNotSupportedError,\n} from \"@api/model/Errors\";\nimport { BleDeviceConnection } from \"@api/transport/BleDeviceConnection\";\n\ntype PromptDeviceAccessError =\n | NoAccessibleDeviceError\n | BleTransportNotSupportedError;\n\n// An attempt to manage the state of several devices with one transport. Not final.\ntype WebBleInternalDevice = {\n id: DeviceId;\n bleDevice: BluetoothDevice;\n bleDeviceInfos: BleDeviceInfos;\n bleGattService: BluetoothRemoteGATTService;\n discoveredDevice: TransportDiscoveredDevice;\n};\n\nexport const webBleIdentifier: TransportIdentifier = \"WEB-BLE\";\n\nexport class WebBleTransport implements Transport {\n private readonly _connectedDevices: Array<BluetoothDevice>;\n private readonly _internalDevicesById: Map<DeviceId, WebBleInternalDevice>;\n private _deviceConnectionById: Map<DeviceId, BleDeviceConnection>;\n private _disconnectionHandlersById: Map<DeviceId, () => void>;\n private _logger: LoggerPublisherService;\n private readonly connectionType: ConnectionType = \"BLE\";\n private readonly identifier: TransportIdentifier = webBleIdentifier;\n\n constructor(\n private readonly _deviceModelDataSource: DeviceModelDataSource,\n private readonly _loggerServiceFactory: (\n tag: string,\n ) => LoggerPublisherService,\n private readonly _apduSenderFactory: ApduSenderServiceFactory,\n private readonly _apduReceiverFactory: ApduReceiverServiceFactory,\n ) {\n this._connectedDevices = [];\n this._internalDevicesById = new Map();\n this._deviceConnectionById = new Map();\n this._disconnectionHandlersById = new Map();\n this._logger = _loggerServiceFactory(\"WebBleTransport\");\n }\n\n /**\n * Get the Bluetooth API if supported or error\n * @returns `Either<BleTransportNotSupportedError, Bluetooth>`\n */\n private getBluetoothApi(): Either<BleTransportNotSupportedError, Bluetooth> {\n if (this.isSupported()) {\n return Right(navigator.bluetooth);\n }\n\n return Left(new BleTransportNotSupportedError(\"WebBle not supported\"));\n }\n\n isSupported(): boolean {\n try {\n const result = !!navigator?.bluetooth;\n return result;\n } catch {\n return false;\n }\n }\n\n getIdentifier(): TransportIdentifier {\n return this.identifier;\n }\n\n listenToAvailableDevices(): Observable<TransportDiscoveredDevice[]> {\n return from([]);\n }\n\n /**\n * Get Bluetooth GATT Primary service that is used to get writeCharacteristic and notifyCharacteristic\n * @param bleDevice\n * @private\n */\n private async getBleGattService(\n bleDevice: BluetoothDevice,\n ): Promise<Either<BleDeviceGattServerError, BluetoothRemoteGATTService>> {\n if (!bleDevice.gatt) {\n return Left(new BleDeviceGattServerError(\"Device gatt not found\"));\n }\n try {\n const [bleGattService] = await bleDevice.gatt.getPrimaryServices();\n if (!bleGattService) {\n return Left(\n new BleDeviceGattServerError(\"bluetooth service not found\"),\n );\n }\n return Right(bleGattService);\n } catch (e) {\n return Left(new BleDeviceGattServerError(e));\n }\n }\n\n /**\n * BleDeviceInfos to map primary service uuid to device model & characteristics uuid\n * @param bleGattService\n * @private\n */\n private getBleDeviceInfos(\n bleGattService: BluetoothRemoteGATTService,\n ): Either<DeviceNotRecognizedError, BleDeviceInfos> {\n const serviceToBleInfos =\n this._deviceModelDataSource.getBluetoothServicesInfos();\n const bleDeviceInfos = serviceToBleInfos[bleGattService.uuid];\n\n if (!bleDeviceInfos) {\n this._logger.error(\n `Device not recognized: ${bleGattService.device.name}`,\n );\n return Left(\n new DeviceNotRecognizedError(\n `Device not recognized: ${bleGattService.device.name}`,\n ),\n );\n }\n return Right(bleDeviceInfos);\n }\n\n /**\n * Prompt device selection in navigator\n *\n * @private\n */\n private promptDeviceAccess(): EitherAsync<\n PromptDeviceAccessError,\n BluetoothDevice\n > {\n return EitherAsync(async ({ liftEither, throwE }) => {\n const bluetoothApi = await liftEither(this.getBluetoothApi());\n let bleDevice: BluetoothDevice;\n\n try {\n bleDevice = await bluetoothApi.requestDevice({\n filters: this._deviceModelDataSource\n .getBluetoothServices()\n .map((serviceUuid) => ({\n services: [serviceUuid],\n })),\n });\n } catch (error) {\n return throwE(new NoAccessibleDeviceError(error));\n }\n\n return bleDevice;\n });\n }\n\n /**\n * Generate a discovered device from BluetoothDevice, BleGATT primary service and BLE device infos\n * @param bleDeviceInfos\n * @private\n */\n private getDiscoveredDeviceFrom(\n bleDeviceInfos: BleDeviceInfos,\n ): TransportDiscoveredDevice {\n return {\n id: uuid(),\n deviceModel: bleDeviceInfos.deviceModel,\n transport: this.identifier,\n };\n }\n\n /**\n * Generate an InternalDevice from a unique id, a BluetoothDevice, BleGATT primary service and BLE device infos\n * @param discoveredDevice\n * @param bleDevice\n * @param bleDeviceInfos\n * @param bleGattService\n * @private\n */\n private setInternalDeviceFrom(\n discoveredDevice: TransportDiscoveredDevice,\n bleDevice: BluetoothDevice,\n bleDeviceInfos: BleDeviceInfos,\n bleGattService: BluetoothRemoteGATTService,\n ) {\n const internalDevice: WebBleInternalDevice = {\n id: discoveredDevice.id,\n bleDevice,\n bleGattService,\n bleDeviceInfos,\n discoveredDevice,\n };\n\n this._logger.debug(\n `Discovered device ${internalDevice.id} ${discoveredDevice.deviceModel.productName}`,\n );\n this._internalDevicesById.set(internalDevice.id, internalDevice);\n }\n\n /**\n * Main method to get a device from a button click handler\n * The GATT connection is done here in order to populate TransportDiscoveredDevice with deviceModel\n */\n startDiscovering(): Observable<TransportDiscoveredDevice> {\n this._logger.debug(\"startDiscovering\");\n\n return from(this.promptDeviceAccess()).pipe(\n switchMap(async (errorOrBleDevice) =>\n EitherAsync(async ({ liftEither, fromPromise }) => {\n const bleDevice = await liftEither(errorOrBleDevice);\n if (bleDevice.gatt) {\n try {\n await bleDevice.gatt.connect();\n } catch (error) {\n throw new OpeningConnectionError(error);\n }\n }\n try {\n const bleGattService = await fromPromise(\n this.getBleGattService(bleDevice),\n );\n const bleDeviceInfos = await liftEither(\n this.getBleDeviceInfos(bleGattService),\n );\n const discoveredDevice =\n this.getDiscoveredDeviceFrom(bleDeviceInfos);\n this.setInternalDeviceFrom(\n discoveredDevice,\n bleDevice,\n bleDeviceInfos,\n bleGattService,\n );\n return discoveredDevice;\n } catch (error) {\n this._logger.error(\"Error while discovering device\", {\n data: { error, bleDevice },\n });\n if (bleDevice.forget) {\n await bleDevice.forget();\n }\n throw error;\n }\n }).caseOf({\n Right: (discoveredDevice) => discoveredDevice,\n Left: (error) => {\n this._logger.error(\"Error while getting accessible device\", {\n data: { error },\n });\n throw error;\n },\n }),\n ),\n );\n }\n\n stopDiscovering(): void {\n this._logger.debug(\"stopDiscovering\");\n }\n\n /**\n * Connect to a BLE device and update the internal state of the associated device\n * Handle ondisconnect event on the device in order to try a reconnection\n */\n async connect({\n deviceId,\n onDisconnect,\n }: {\n deviceId: DeviceId;\n onDisconnect: DisconnectHandler;\n }): Promise<Either<ConnectError, TransportConnectedDevice>> {\n const internalDevice = this._internalDevicesById.get(deviceId);\n\n if (!internalDevice) {\n this._logger.error(`Unknown device ${deviceId}`, {\n data: { internalDevices: this._internalDevicesById },\n });\n this._logger.debug(\"Available devices\", {\n data: { devices: this._internalDevicesById },\n });\n return Left(new UnknownDeviceError(`Unknown device ${deviceId}`));\n }\n // if device already connected, remove device id from internal state and remove error\n if (this._connectedDevices.includes(internalDevice.bleDevice)) {\n this._internalDevicesById.delete(deviceId);\n return Left(new DeviceAlreadyConnectedError(\"Device already connected\"));\n }\n\n const {\n discoveredDevice: { deviceModel },\n } = internalDevice;\n\n try {\n const [writeCharacteristic, notifyCharacteristic] = await Promise.all([\n internalDevice.bleGattService.getCharacteristic(\n internalDevice.bleDeviceInfos.writeCmdUuid,\n ),\n internalDevice.bleGattService.getCharacteristic(\n internalDevice.bleDeviceInfos.notifyUuid,\n ),\n ]);\n\n const deviceConnection = new BleDeviceConnection(\n {\n writeCharacteristic,\n notifyCharacteristic,\n apduReceiverFactory: this._apduReceiverFactory,\n apduSenderFactory: this._apduSenderFactory,\n },\n this._loggerServiceFactory,\n );\n\n await deviceConnection.setup();\n\n const connectedDevice = new TransportConnectedDevice({\n sendApdu: (apdu, triggersDisconnection) =>\n deviceConnection.sendApdu(apdu, triggersDisconnection),\n deviceModel,\n id: deviceId,\n type: this.connectionType,\n transport: this.identifier,\n });\n\n internalDevice.bleDevice.ongattserverdisconnected =\n this._getDeviceDisconnectedHandler(internalDevice, deviceConnection);\n\n this._deviceConnectionById.set(internalDevice.id, deviceConnection);\n this._disconnectionHandlersById.set(internalDevice.id, () => {\n this.disconnect({ connectedDevice }).then(() => onDisconnect(deviceId));\n });\n\n this._connectedDevices.push(internalDevice.bleDevice);\n\n return Right(connectedDevice);\n } catch (error) {\n if (internalDevice.bleDevice.forget) {\n await internalDevice.bleDevice.forget();\n }\n\n this._internalDevicesById.delete(deviceId);\n\n this._logger.error(\"Error while getting characteristics\", {\n data: { error },\n });\n\n return Left(new OpeningConnectionError(error));\n }\n }\n\n /**\n * Get the device disconnected handler\n * @param internalDevice WebBleInternalDevice\n * @param deviceConnection BleDeviceConnection\n * @returns async () => void\n * @private\n */\n private _getDeviceDisconnectedHandler(\n internalDevice: WebBleInternalDevice,\n deviceConnection: BleDeviceConnection,\n ) {\n return async () => {\n // start a timer to disconnect the device if it does not reconnect\n const disconnectObserver = timer(RECONNECT_DEVICE_TIMEOUT).subscribe(\n () => {\n this._logger.debug(\"disconnection timer over\");\n // retrieve the disconnect handler and call it\n const disconnectHandler = Maybe.fromNullable(\n this._disconnectionHandlersById.get(internalDevice.id),\n );\n disconnectHandler.map((handler) => handler());\n },\n );\n\n // connect to the navigator device\n await internalDevice.bleDevice.gatt?.connect();\n\n // cancel disconnection timeout\n disconnectObserver.unsubscribe();\n\n // retrieve new ble characteristics\n const service = await this.getBleGattService(internalDevice.bleDevice);\n\n if (service.isRight()) {\n const [writeC, notifyC] = await Promise.all([\n service\n .extract()\n .getCharacteristic(internalDevice.bleDeviceInfos.writeCmdUuid),\n service\n .extract()\n .getCharacteristic(internalDevice.bleDeviceInfos.notifyUuid),\n ]);\n\n // reconnect device connection\n await deviceConnection.reconnect(writeC, notifyC);\n }\n };\n }\n\n /**\n * Disconnect from a BLE device and delete its handlers\n *\n * @param params { connectedDevice: TransportConnectedDevice }\n */\n async disconnect(params: {\n connectedDevice: TransportConnectedDevice;\n }): Promise<Either<DmkError, void>> {\n // retrieve internal device\n const maybeInternalDevice = Maybe.fromNullable(\n this._internalDevicesById.get(params.connectedDevice.id),\n );\n\n this._logger.debug(\"disconnect device\", {\n data: { connectedDevice: params.connectedDevice },\n });\n\n if (maybeInternalDevice.isNothing()) {\n this._logger.error(`Unknown device ${params.connectedDevice.id}`);\n\n return Promise.resolve(\n Left(\n new UnknownDeviceError(`Unknown device ${params.connectedDevice.id}`),\n ),\n );\n }\n\n maybeInternalDevice.map((device) => {\n const { bleDevice } = device;\n\n // retrieve device connection and disconnect it\n const maybeDeviceConnection = Maybe.fromNullable(\n this._deviceConnectionById.get(device.id),\n );\n\n maybeDeviceConnection.map((dConnection) => dConnection.disconnect());\n\n // disconnect device gatt server\n if (bleDevice.gatt?.connected) {\n bleDevice.gatt.disconnect();\n }\n // clean up objects\n this._internalDevicesById.delete(device.id);\n this._deviceConnectionById.delete(device.id);\n this._disconnectionHandlersById.delete(device.id);\n\n if (this._connectedDevices.includes(bleDevice)) {\n delete this._connectedDevices[\n this._connectedDevices.indexOf(bleDevice)\n ];\n }\n });\n\n return Promise.resolve(Right(undefined));\n }\n}\n\nexport const webBleTransportFactory: TransportFactory = ({\n deviceModelDataSource,\n loggerServiceFactory,\n apduSenderServiceFactory,\n apduReceiverServiceFactory,\n}) =>\n new WebBleTransport(\n deviceModelDataSource,\n loggerServiceFactory,\n apduSenderServiceFactory,\n apduReceiverServiceFactory,\n );\n"],
|
|
5
|
-
"mappings": "AAAA,
|
|
6
|
-
"names": ["
|
|
4
|
+
"sourcesContent": ["import {\n type ApduReceiverServiceFactory,\n type ApduSenderServiceFactory,\n type ConnectError,\n DeviceConnectionStateMachine,\n type DeviceModelDataSource,\n type DmkError,\n GeneralDmkError,\n type LoggerPublisherService,\n OpeningConnectionError,\n type Transport,\n TransportConnectedDevice,\n type TransportDeviceModel,\n type TransportDiscoveredDevice,\n type TransportFactory,\n type TransportIdentifier,\n UnknownDeviceError,\n} from \"@ledgerhq/device-management-kit\";\nimport { type Either, Left, Right } from \"purify-ts\";\nimport { BehaviorSubject, from, type Observable } from \"rxjs\";\nimport { switchMap } from \"rxjs/operators\";\n\nimport { RECONNECT_DEVICE_TIMEOUT } from \"@api/data/WebBleConfig\";\n\nimport {\n WebBleApduSender,\n type WebBleApduSenderDependencies,\n} from \"./WebBleApduSender\";\n\nexport const webBleIdentifier: TransportIdentifier = \"WEB-BLE-RN-STYLE\";\n\ntype RegistryEntry = {\n device: BluetoothDevice;\n serviceUuid?: string;\n infos?: {\n writeCmdUuid: string;\n writeUuid?: string;\n notifyUuid: string;\n deviceModel: TransportDeviceModel;\n };\n discovered: TransportDiscoveredDevice;\n listener?: (ev: Event) => void;\n};\n\nexport class WebBleTransport implements Transport {\n private _logger: LoggerPublisherService;\n private _deviceModelDataSource: DeviceModelDataSource;\n private _apduSenderFactory: ApduSenderServiceFactory;\n private _apduReceiverFactory: ApduReceiverServiceFactory;\n\n private _deviceConnectionsById = new Map<\n string,\n DeviceConnectionStateMachine<WebBleApduSenderDependencies>\n >();\n private _registry = new Map<string, RegistryEntry>();\n private _devices$ = new BehaviorSubject<TransportDiscoveredDevice[]>([]);\n\n constructor(\n deviceModelDataSource: DeviceModelDataSource,\n private loggerFactory: (tag: string) => LoggerPublisherService,\n apduSenderFactory: ApduSenderServiceFactory,\n apduReceiverFactory: ApduReceiverServiceFactory,\n ) {\n this._deviceModelDataSource = deviceModelDataSource;\n this._apduSenderFactory = apduSenderFactory;\n this._apduReceiverFactory = apduReceiverFactory;\n this._logger = loggerFactory(\"WebBleTransportRnStyle\");\n }\n\n isSupported(): boolean {\n return typeof navigator !== \"undefined\" && !!navigator.bluetooth;\n }\n\n getIdentifier(): TransportIdentifier {\n return webBleIdentifier;\n }\n\n startDiscovering(): Observable<TransportDiscoveredDevice> {\n const filters = this._deviceModelDataSource\n .getBluetoothServices()\n .map((s) => ({ services: [s] }));\n const allUuids = this._deviceModelDataSource.getBluetoothServices();\n\n return from(\n navigator.bluetooth.requestDevice({\n filters,\n optionalServices: allUuids,\n }),\n ).pipe(\n switchMap(async (device) => {\n const { serviceUuid, infos } =\n await this._identifyLedgerService(device);\n\n const discovered: TransportDiscoveredDevice = {\n id: device.id,\n deviceModel: infos.deviceModel,\n transport: webBleIdentifier,\n };\n\n this._registry.set(device.id, {\n device,\n serviceUuid,\n infos,\n discovered,\n });\n\n this._emitDevices();\n return discovered;\n }),\n );\n }\n\n stopDiscovering(): void {\n /* no-op on Web Bluetooth */\n }\n\n listenToAvailableDevices(): Observable<TransportDiscoveredDevice[]> {\n this._emitDevices();\n return this._devices$.asObservable();\n }\n\n async connect(params: {\n deviceId: string;\n onDisconnect: (deviceId: string) => void;\n onReconnect?: (deviceId: string) => Promise<void> | void;\n }): Promise<Either<ConnectError, TransportConnectedDevice>> {\n const existing = this._deviceConnectionsById.get(params.deviceId);\n const entry = this._registry.get(params.deviceId);\n if (!entry)\n return Left(new UnknownDeviceError(`Unknown device ${params.deviceId}`));\n\n if (existing) {\n return Right(\n new TransportConnectedDevice({\n id: params.deviceId,\n deviceModel: entry.infos!.deviceModel,\n type: \"BLE\",\n transport: webBleIdentifier,\n sendApdu: (...a) => existing.sendApdu(...a),\n }),\n );\n }\n\n try {\n const device = entry.device;\n if (!device.gatt) {\n throw new OpeningConnectionError(\"No GATT server available on device\");\n }\n\n if (!device.gatt.connected) {\n await this._withTimeout(\n device.gatt.connect(),\n 6000,\n \"GATT connect timed out\",\n );\n await this._delay(150);\n }\n\n const { service, infos } = await this._getLiveLedgerService(device);\n const { writeCharacteristic, notifyCharacteristic } =\n await this._resolveCharacteristics(service, infos);\n\n const apduSender = new WebBleApduSender(\n {\n writeCharacteristic,\n notifyCharacteristic,\n apduSenderFactory: this._apduSenderFactory,\n apduReceiverFactory: this._apduReceiverFactory,\n },\n this.loggerFactory,\n );\n\n const machine =\n new DeviceConnectionStateMachine<WebBleApduSenderDependencies>({\n deviceId: params.deviceId,\n deviceApduSender: apduSender,\n timeoutDuration: RECONNECT_DEVICE_TIMEOUT,\n tryToReconnect: () => {\n this.tryToReconnect(params.deviceId, params.onReconnect).catch(\n (e) =>\n this._logger.error(\"tryToReconnect() threw\", { data: { e } }),\n );\n },\n onTerminated: () => {\n try {\n this._deviceConnectionsById\n .get(params.deviceId)\n ?.closeConnection();\n params.onDisconnect(params.deviceId);\n } finally {\n this._deviceConnectionsById.delete(params.deviceId);\n const reg = this._registry.get(params.deviceId);\n if (reg?.listener) {\n try {\n reg.device.removeEventListener(\n \"gattserverdisconnected\",\n reg.listener,\n );\n } catch {\n // fill\n }\n reg.listener = undefined;\n }\n this._emitDevices();\n }\n },\n });\n\n await apduSender.setupConnection();\n\n this._deviceConnectionsById.set(params.deviceId, machine);\n entry.serviceUuid = service.uuid;\n entry.infos = infos;\n\n const onDisc = (_ev: Event) =>\n this._handleDeviceDisconnected(params.deviceId);\n entry.listener = onDisc;\n device.addEventListener(\"gattserverdisconnected\", onDisc);\n\n this._emitDevices();\n\n return Right(\n new TransportConnectedDevice({\n id: params.deviceId,\n deviceModel: infos.deviceModel,\n type: \"BLE\",\n transport: webBleIdentifier,\n sendApdu: (...a) => machine.sendApdu(...a),\n }),\n );\n } catch (e) {\n this._logger.error(\"connect() error\", { data: { e } });\n return Left(new OpeningConnectionError(e));\n }\n }\n\n async disconnect(params: {\n connectedDevice: TransportConnectedDevice;\n }): Promise<Either<DmkError, void>> {\n const id = params.connectedDevice.id;\n const sm = this._deviceConnectionsById.get(id);\n const reg = this._registry.get(id);\n if (!sm) return Left(new UnknownDeviceError(`Unknown device ${id}`));\n\n try {\n sm.closeConnection();\n this._deviceConnectionsById.delete(id);\n if (reg?.listener) {\n try {\n reg.device.removeEventListener(\n \"gattserverdisconnected\",\n reg.listener,\n );\n } catch {\n //fill\n }\n reg.listener = undefined;\n }\n await this._safeCancel(id);\n this._emitDevices();\n return Right(undefined);\n } catch (e) {\n return Left(new GeneralDmkError({ originalError: e }));\n }\n }\n\n private _handleDeviceDisconnected(deviceId: string) {\n this._logger.debug(`[${deviceId}] gattserverdisconnected`);\n const sm = this._deviceConnectionsById.get(deviceId);\n if (!sm) return;\n sm.eventDeviceDisconnected();\n }\n\n private async _waitForInRange(\n device: BluetoothDevice,\n opts: { startTimeoutMs?: number; advTimeoutMs?: number } = {},\n ): Promise<boolean> {\n const { startTimeoutMs = 500, advTimeoutMs = 1500 } = opts;\n if (typeof device.watchAdvertisements !== \"function\") return false;\n\n const controller = new AbortController();\n const startTimer = setTimeout(() => controller.abort(), startTimeoutMs);\n const started = await device\n .watchAdvertisements({ signal: controller.signal })\n .then(() => true)\n .catch(() => false);\n clearTimeout(startTimer);\n if (!started) return false;\n\n const seen = await new Promise<boolean>((resolve) => {\n const onAdv = () => {\n device.removeEventListener(\"advertisementreceived\", onAdv);\n resolve(true);\n };\n device.addEventListener(\"advertisementreceived\", onAdv);\n setTimeout(() => {\n device.removeEventListener(\"advertisementreceived\", onAdv);\n resolve(false);\n }, advTimeoutMs);\n });\n\n try {\n controller.abort();\n } catch {\n // fill\n }\n return seen;\n }\n\n private async _rediscoverDevice(\n deviceId: string,\n ): Promise<BluetoothDevice | null> {\n if (typeof navigator.bluetooth.getDevices !== \"function\") return null;\n try {\n this._logger.debug(`Attempting to rediscover device ${deviceId}`);\n const permitted = await navigator.bluetooth.getDevices();\n let fresh = permitted.find((d) => d.id === deviceId) ?? null;\n\n if (!fresh) {\n const name = this._registry.get(deviceId)?.device?.name;\n if (name) fresh = permitted.find((d) => d.name === name) ?? null;\n }\n\n if (fresh) {\n const inRange = await this._waitForInRange(fresh, {\n startTimeoutMs: 400,\n advTimeoutMs: 1200,\n });\n this._logger.debug(`Rediscovered ${fresh.id}, inRange=${inRange}`);\n }\n return fresh;\n } catch {\n return null;\n }\n }\n\n private async tryToReconnect(\n deviceId: string,\n onReconnect?: (id: string) => Promise<void> | void,\n ) {\n await this._safeCancel(deviceId);\n\n while (true) {\n const reg = this._registry.get(deviceId);\n const sm = this._deviceConnectionsById.get(deviceId);\n if (!reg || !sm) {\n this._logger.debug(\n `[${deviceId}] aborting reconnect: registry or SM missing`,\n );\n return;\n }\n\n try {\n const device = await this._withTimeout(\n this._rediscoverDevice(deviceId),\n 3000,\n \"rediscovery timeout\",\n );\n\n if (!device) throw new Error(\"Device not found\");\n\n if (!device.gatt) throw new Error(\"No GATT on device\");\n try {\n device.gatt.connect();\n } catch (e) {\n this._logger.error(`[${deviceId}] gatt.connect() failed`, {\n data: { e },\n });\n if (device.gatt?.connected) device.gatt.disconnect();\n }\n\n const { service, infos } = await this._getLiveLedgerService(device);\n const { writeCharacteristic, notifyCharacteristic } =\n await this._resolveCharacteristics(service, infos);\n\n sm.setDependencies({ writeCharacteristic, notifyCharacteristic });\n await sm.setupConnection();\n sm.eventDeviceConnected();\n\n reg.serviceUuid = service.uuid;\n reg.infos = infos;\n\n if (reg.listener) {\n try {\n device.removeEventListener(\"gattserverdisconnected\", reg.listener);\n } catch {\n //fill\n }\n }\n const onDisc = (_: Event) => this._handleDeviceDisconnected(deviceId);\n device.addEventListener(\"gattserverdisconnected\", onDisc);\n reg.listener = onDisc;\n\n try {\n await onReconnect?.(deviceId);\n } catch (e) {\n this._logger.error(\"onReconnect callback threw\", { data: { e } });\n }\n\n this._emitDevices();\n return;\n } catch (e) {\n this._logger.error(`[${deviceId}] reconnect attempt failed`, {\n data: { e },\n });\n try {\n if (reg?.device.gatt?.connected) reg.device.gatt.disconnect();\n } catch {\n //fill\n }\n await this._delay(500);\n continue;\n }\n }\n }\n\n private async _safeCancel(deviceId: string) {\n const reg = this._registry.get(deviceId);\n if (!reg) return;\n try {\n if (reg.device.gatt?.connected) reg.device.gatt.disconnect();\n } catch {\n // fill\n }\n await this._delay(100);\n }\n\n private async _identifyLedgerService(device: BluetoothDevice): Promise<{\n serviceUuid: string;\n infos: NonNullable<RegistryEntry[\"infos\"]>;\n }> {\n if (!device.gatt) {\n throw new OpeningConnectionError(\"No GATT server available on device\");\n }\n try {\n await this._withTimeout(device.gatt.connect(), 6000, \"connect timeout\");\n const { service, infos } = await this._getLiveLedgerService(device);\n return { serviceUuid: service.uuid, infos };\n } finally {\n try {\n device.gatt?.disconnect();\n } catch {\n // fill\n }\n await this._delay(200);\n }\n }\n\n private async _getLiveLedgerService(device: BluetoothDevice): Promise<{\n service: BluetoothRemoteGATTService;\n infos: NonNullable<RegistryEntry[\"infos\"]>;\n }> {\n const knownUuids = this._deviceModelDataSource.getBluetoothServices();\n const last = this._registry.get(device.id)?.serviceUuid;\n const order = last\n ? [last, ...knownUuids.filter((u) => u !== last)]\n : knownUuids.slice();\n\n for (const uuid of order) {\n try {\n const s = await device.gatt!.getPrimaryService(uuid);\n const infos =\n this._deviceModelDataSource.getBluetoothServicesInfos()[s.uuid];\n if (!infos) throw new UnknownDeviceError(device.name || \"\");\n return { service: s, infos };\n } catch (e: unknown) {\n if ((e as Error)?.name === \"SecurityError\") {\n throw new OpeningConnectionError(\n `Missing Web Bluetooth permission for service ${uuid}. ` +\n `Add it to optionalServices in requestDevice().`,\n );\n }\n try {\n const all = await device.gatt!.getPrimaryServices();\n const found = all.find(\n (s) => s.uuid.toLowerCase() === uuid.toLowerCase(),\n );\n if (found) {\n const infos =\n this._deviceModelDataSource.getBluetoothServicesInfos()[\n found.uuid\n ];\n if (!infos) throw new UnknownDeviceError(device.name || \"\");\n return { service: found, infos };\n }\n } catch {\n // continue\n }\n }\n }\n throw new OpeningConnectionError(\"Ledger GATT service not found\");\n }\n\n private async _resolveCharacteristics(\n service: BluetoothRemoteGATTService,\n infos: NonNullable<RegistryEntry[\"infos\"]>,\n ): Promise<{\n writeCharacteristic: BluetoothRemoteGATTCharacteristic;\n notifyCharacteristic: BluetoothRemoteGATTCharacteristic;\n }> {\n const notifyCharacteristic = await service.getCharacteristic(\n infos.notifyUuid,\n );\n const writeCharacteristic = await this._pluckWriteCharacteristic(\n service,\n infos,\n );\n return { writeCharacteristic, notifyCharacteristic };\n }\n\n private async _pluckWriteCharacteristic(\n svc: BluetoothRemoteGATTService,\n infos: NonNullable<RegistryEntry[\"infos\"]>,\n ): Promise<BluetoothRemoteGATTCharacteristic> {\n const order = [infos.writeCmdUuid, infos.writeUuid].filter(\n Boolean,\n ) as string[];\n const tried: BluetoothRemoteGATTCharacteristic[] = [];\n\n for (const uuid of order) {\n try {\n const ch = await svc.getCharacteristic(uuid);\n tried.push(ch);\n\n if (ch.properties.writeWithoutResponse) return ch;\n if (ch.properties.write) return ch;\n } catch {\n // fill\n }\n }\n\n if (tried.length) return tried[0]!;\n throw new OpeningConnectionError(\"No write characteristic available\");\n }\n\n private _emitDevices() {\n const connected: TransportDiscoveredDevice[] = [];\n for (const [id] of this._deviceConnectionsById) {\n const reg = this._registry.get(id);\n if (reg?.infos) {\n connected.push({\n id,\n deviceModel: reg.infos.deviceModel,\n transport: webBleIdentifier,\n });\n }\n }\n const scanned = Array.from(this._registry.values())\n .map((r) => r.discovered)\n .filter((d) => !this._deviceConnectionsById.has(d.id));\n\n this._devices$.next([...connected, ...scanned]);\n }\n\n private _withTimeout<T>(\n p: Promise<T>,\n ms: number,\n msg: string,\n cancellationFn?: () => void,\n ): Promise<T> {\n return new Promise<T>((resolve, reject) => {\n const t = setTimeout(() => {\n reject(new OpeningConnectionError(msg));\n try {\n cancellationFn?.();\n } catch {\n this._logger.debug(\"Cancellation function threw\");\n }\n }, ms);\n p.then(\n (v) => {\n clearTimeout(t);\n resolve(v);\n },\n (e) => {\n this._logger.error(\"_withTimeout() promise rejected\", {\n data: { e },\n });\n clearTimeout(t);\n reject(e);\n },\n );\n });\n }\n\n private _delay(ms: number) {\n return new Promise((r) => setTimeout(r, ms));\n }\n}\n\nexport const webBleTransportFactory: TransportFactory = ({\n deviceModelDataSource,\n loggerServiceFactory,\n apduSenderServiceFactory,\n apduReceiverServiceFactory,\n}) =>\n new WebBleTransport(\n deviceModelDataSource,\n loggerServiceFactory,\n apduSenderServiceFactory,\n apduReceiverServiceFactory,\n );\n"],
|
|
5
|
+
"mappings": "AAAA,OAIE,gCAAAA,EAGA,mBAAAC,EAEA,0BAAAC,EAEA,4BAAAC,EAKA,sBAAAC,MACK,kCACP,OAAsB,QAAAC,EAAM,SAAAC,MAAa,YACzC,OAAS,mBAAAC,EAAiB,QAAAC,MAA6B,OACvD,OAAS,aAAAC,MAAiB,iBAE1B,OAAS,4BAAAC,MAAgC,yBAEzC,OACE,oBAAAC,MAEK,qBAEA,MAAMC,EAAwC,mBAe9C,MAAMC,CAAqC,CAahD,YACEC,EACQC,EACRC,EACAC,EACA,CAHQ,mBAAAF,EAIR,KAAK,uBAAyBD,EAC9B,KAAK,mBAAqBE,EAC1B,KAAK,qBAAuBC,EAC5B,KAAK,QAAUF,EAAc,wBAAwB,CACvD,CAtBQ,QACA,uBACA,mBACA,qBAEA,uBAAyB,IAAI,IAI7B,UAAY,IAAI,IAChB,UAAY,IAAIR,EAA6C,CAAC,CAAC,EAcvE,aAAuB,CACrB,OAAO,OAAO,UAAc,KAAe,CAAC,CAAC,UAAU,SACzD,CAEA,eAAqC,CACnC,OAAOK,CACT,CAEA,kBAA0D,CACxD,MAAMM,EAAU,KAAK,uBAClB,qBAAqB,EACrB,IAAKC,IAAO,CAAE,SAAU,CAACA,CAAC,CAAE,EAAE,EAC3BC,EAAW,KAAK,uBAAuB,qBAAqB,EAElE,OAAOZ,EACL,UAAU,UAAU,cAAc,CAChC,QAAAU,EACA,iBAAkBE,CACpB,CAAC,CACH,EAAE,KACAX,EAAU,MAAOY,GAAW,CAC1B,KAAM,CAAE,YAAAC,EAAa,MAAAC,CAAM,EACzB,MAAM,KAAK,uBAAuBF,CAAM,EAEpCG,EAAwC,CAC5C,GAAIH,EAAO,GACX,YAA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AC,CAChD,CAEQ,aACNE,EACAC,EACAC,EACAC,EACY,CACZ,OAAO,IAAI,QAAW,CAACxB,EAASyB,IAAW,CACzC,MAAMC,EAAI,WAAW,IAAM,CACzBD,EAAO,IAAIvE,EAAuBqE,CAAG,CAAC,EACtC,GAAI,CACFC,IAAiB,CACnB,MAAQ,CACN,KAAK,QAAQ,MAAM,6BAA6B,CAClD,CACF,EAAGF,CAAE,EACLD,EAAE,KACCM,GAAM,CACL,aAAaD,CAAC,EACd1B,EAAQ2B,CAAC,CACX,EACCzC,GAAM,CACL,KAAK,QAAQ,MAAM,kCAAmC,CACpD,KAAM,CAAE,EAAAA,CAAE,CACZ,CAAC,EACD,aAAawC,CAAC,EACdD,EAAOvC,CAAC,CACV,CACF,CACF,CAAC,CACH,CAEQ,OAAOoC,EAAY,CACzB,OAAO,IAAI,QAASF,GAAM,WAAWA,EAAGE,CAAE,CAAC,CAC7C,CACF,CAEO,MAAMM,EAA2C,CAAC,CACvD,sBAAA9D,EACA,qBAAA+D,EACA,yBAAAC,EACA,2BAAAC,CACF,IACE,IAAIlE,EACFC,EACA+D,EACAC,EACAC,CACF",
|
|
6
|
+
"names": ["DeviceConnectionStateMachine", "GeneralDmkError", "OpeningConnectionError", "TransportConnectedDevice", "UnknownDeviceError", "Left", "Right", "BehaviorSubject", "from", "switchMap", "RECONNECT_DEVICE_TIMEOUT", "WebBleApduSender", "webBleIdentifier", "WebBleTransport", "deviceModelDataSource", "loggerFactory", "apduSenderFactory", "apduReceiverFactory", "filters", "s", "allUuids", "device", "serviceUuid", "infos", "discovered", "params", "existing", "entry", "a", "service", "writeCharacteristic", "notifyCharacteristic", "apduSender", "machine", "e", "reg", "onDisc", "_ev", "id", "sm", "deviceId", "opts", "startTimeoutMs", "advTimeoutMs", "controller", "startTimer", "started", "seen", "resolve", "onAdv", "permitted", "fresh", "d", "name", "inRange", "onReconnect", "_", "knownUuids", "last", "order", "u", "uuid", "found", "svc", "tried", "ch", "connected", "scanned", "r", "p", "ms", "msg", "cancellationFn", "reject", "t", "v", "webBleTransportFactory", "loggerServiceFactory", "apduSenderServiceFactory", "apduReceiverServiceFactory"]
|
|
7
7
|
}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
import{NoAccessibleDeviceError as p,OpeningConnectionError as b,StaticDeviceModelDataSource as w,UnknownDeviceError as h}from"@ledgerhq/device-management-kit";import{Left as f,Right as S}from"purify-ts";import{RECONNECT_DEVICE_TIMEOUT as m}from"../data/WebBleConfig";import{bleDeviceStubBuilder as E}from"../model/BleDevice.stub";import{BleTransportNotSupportedError as D}from"../model/Errors";import{BleDeviceGattServerError as g}from"../model/Errors";import{WebBleTransport as y}from"./WebBleTransport";class k{subscribers=[];tag;constructor(d,v){this.subscribers=d,this.tag=v}error=vi.fn();warn=vi.fn();info=vi.fn();debug=vi.fn()}const x=new w,O=new k([],"web-ble"),c=E();describe("WebBleTransport",()=>{let i,d,v;beforeEach(()=>{d=vi.fn(),v=vi.fn(),i=new y(x,()=>O,v,d),vi.useFakeTimers()}),afterEach(()=>{vi.restoreAllMocks()});const s=(r,t)=>{i.startDiscovering().subscribe({next:r,error:t})};describe("When Web bluetooth API is not supported",()=>{it("should not support the transport",()=>{expect(i.isSupported()).toBe(!1)}),it("should emit a startDiscovering error",()=>new Promise((r,t)=>{s(()=>{t("Should not emit any value")},e=>{expect(e).toBeInstanceOf(D),r()})}))}),describe("When Web Bluetooth API is supported",()=>{const r=vi.fn();beforeAll(()=>{global.navigator={bluetooth:{requestDevice:r}}}),afterAll(()=>{vi.restoreAllMocks(),global.navigator=void 0}),it("should support the transport",()=>{expect(i.isSupported()).toBe(!0)}),describe("startDiscovering",()=>{it("should emit device if one new grant access",()=>new Promise((t,e)=>{r.mockResolvedValueOnce(c),s(o=>{try{expect(o).toEqual(expect.objectContaining({deviceModel:expect.objectContaining({id:"nanoX",productName:"Ledger Nano X"})})),t()}catch(n){e(n)}},o=>{e(o)})})),it("should throw DeviceNotRecognizedError if the device is not recognized",()=>new Promise((t,e)=>{r.mockResolvedValueOnce({...c,gatt:{...c.gatt,getPrimaryServices:vi.fn(()=>Promise.resolve([]))},productId:16962}),s(()=>{e("should not return a device")},o=>{expect(o).toBeInstanceOf(g),t()})})),it("should emit an error if the request device is in error",()=>new Promise((t,e)=>{const o="request device error";r.mockImplementationOnce(()=>{throw new Error(o)}),s(()=>{e("should not return a device")},n=>{expect(n).toBeInstanceOf(p),expect(n).toStrictEqual(new p(new Error(o))),t()})})),it("should emit an error if the user did not grant us access to a device (clicking on cancel on the browser popup for ex)",()=>new Promise((t,e)=>{r.mockResolvedValueOnce({forget:vi.fn()}),s(o=>{e(`Should not emit any value, but emitted ${JSON.stringify(o)}`)},o=>{try{expect(o).toBeInstanceOf(g),t()}catch(n){e(n)}})}))}),describe("connect",()=>{it("should throw UnknownDeviceError if no internal device",async()=>{const t={deviceId:"fake",onDisconnect:vi.fn()},e=await i.connect(t);expect(e).toStrictEqual(f(new h("Unknown device fake")))}),it("should throw OpeningConnectionError if the device is already opened",async()=>{const t={deviceId:"fake",onDisconnect:vi.fn()},e=await i.connect(t);expect(e).toStrictEqual(f(new h("Unknown device fake")))}),it("should throw OpeningConnectionError if the device cannot be opened",()=>new Promise((t,e)=>{const o="cannot be opened";r.mockResolvedValueOnce({...c,gatt:{connect:()=>{throw new Error(o)}}}),s(()=>{e("Should not emit any value")},n=>{expect(n).toBeInstanceOf(b),t()})})),it("should return the opened device",()=>new Promise((t,e)=>{r.mockResolvedValueOnce({...c,gatt:{...c.gatt,connected:!0}}),s(o=>{i.connect({deviceId:o.id,onDisconnect:vi.fn()}).then(n=>{n.ifRight(a=>{expect(a).toEqual(expect.objectContaining({id:o.id})),t()}).ifLeft(()=>{e(n)})}).catch(n=>{e(n)})},o=>{e(o)})})),it("should return a device if available",()=>new Promise((t,e)=>{r.mockResolvedValueOnce(c),s(o=>{i.connect({deviceId:o.id,onDisconnect:vi.fn()}).then(n=>{n.ifRight(a=>{expect(a).toEqual(expect.objectContaining({id:o.id})),t()}).ifLeft(()=>{e(n)})}).catch(n=>{e(n)})},o=>{e(o)})}))}),describe("disconnect",()=>{it("should disconnect the device",()=>new Promise((t,e)=>{r.mockResolvedValueOnce(c);const o=vi.fn();s(n=>{i.connect({deviceId:n.id,onDisconnect:o}).then(a=>{a.ifRight(l=>{i.disconnect({connectedDevice:l}).then(u=>{expect(u).toStrictEqual(S(void 0)),t()}).catch(u=>{e(u)})})})},n=>{e(n)})})),it("should call disconnect handler if device is hardware disconnected",()=>new Promise((t,e)=>{const o=vi.fn(),n=vi.spyOn(i,"disconnect");r.mockResolvedValueOnce(c),s(a=>{i.connect({deviceId:a.id,onDisconnect:o}).then(()=>{c.ongattserverdisconnected(new Event("")),vi.advanceTimersByTime(m),expect(n).toHaveBeenCalled(),t()})},a=>{e(a)})}))}),describe("reconnect",()=>{it("should not call disconnection if reconnection happen",()=>new Promise((t,e)=>{const o=vi.fn(),n=vi.spyOn(i,"disconnect");r.mockResolvedValueOnce(c),s(a=>{i.connect({deviceId:a.id,onDisconnect:o}).then(()=>{c.ongattserverdisconnected(new Event("")),vi.advanceTimersByTime(m/3),expect(n).toHaveBeenCalledTimes(0),t()}).catch(l=>{e(l)})})}))})})});
|
|
2
1
|
//# sourceMappingURL=WebBleTransport.test.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": [
|
|
4
|
-
"sourcesContent": ["import {\n type ApduReceiverServiceFactory,\n type ApduSenderServiceFactory,\n type DeviceModel,\n type LoggerPublisherService,\n type LoggerSubscriberService,\n NoAccessibleDeviceError,\n OpeningConnectionError,\n StaticDeviceModelDataSource,\n type TransportDiscoveredDevice,\n UnknownDeviceError,\n} from \"@ledgerhq/device-management-kit\";\nimport { Left, Right } from \"purify-ts\";\n\nimport { RECONNECT_DEVICE_TIMEOUT } from \"@api/data/WebBleConfig\";\nimport { bleDeviceStubBuilder } from \"@api/model/BleDevice.stub\";\nimport { BleTransportNotSupportedError } from \"@api/model/Errors\";\nimport { BleDeviceGattServerError } from \"@api/model/Errors\";\n\nimport { WebBleTransport } from \"./WebBleTransport\";\n\nclass LoggerPublisherServiceStub implements LoggerPublisherService {\n subscribers: LoggerSubscriberService[] = [];\n tag: string;\n constructor(subscribers: LoggerSubscriberService[], tag: string) {\n this.subscribers = subscribers;\n this.tag = tag;\n }\n error = vi.fn();\n warn = vi.fn();\n info = vi.fn();\n debug = vi.fn();\n}\n\n// Our StaticDeviceModelDataSource can directly be used in our unit tests\nconst bleDeviceModelDataSource = new StaticDeviceModelDataSource();\nconst logger = new LoggerPublisherServiceStub([], \"web-ble\");\n\nconst stubDevice: BluetoothDevice = bleDeviceStubBuilder();\n\ndescribe(\"WebBleTransport\", () => {\n let transport: WebBleTransport;\n let apduReceiverServiceFactoryStub: ApduReceiverServiceFactory;\n let apduSenderServiceFactoryStub: ApduSenderServiceFactory;\n\n beforeEach(() => {\n apduReceiverServiceFactoryStub = vi.fn();\n apduSenderServiceFactoryStub = vi.fn();\n transport = new WebBleTransport(\n bleDeviceModelDataSource,\n () => logger,\n apduSenderServiceFactoryStub,\n apduReceiverServiceFactoryStub,\n );\n vi.useFakeTimers();\n });\n\n afterEach(() => {\n vi.restoreAllMocks();\n });\n\n const discoverDevice = (\n onSuccess: (discoveredDevice: TransportDiscoveredDevice) => void,\n onError?: (error: unknown) => void,\n ) => {\n transport.startDiscovering().subscribe({\n next: onSuccess,\n error: onError,\n });\n };\n\n describe(\"When Web bluetooth API is not supported\", () => {\n it(\"should not support the transport\", () => {\n expect(transport.isSupported()).toBe(false);\n });\n\n it(\"should emit a startDiscovering error\", () =>\n new Promise<void>((resolve, reject) => {\n discoverDevice(\n () => {\n reject(\"Should not emit any value\");\n },\n (error) => {\n expect(error).toBeInstanceOf(BleTransportNotSupportedError);\n resolve();\n },\n );\n }));\n });\n\n describe(\"When Web Bluetooth API is supported\", () => {\n const mockedRequestDevice = vi.fn();\n\n beforeAll(() => {\n global.navigator = {\n bluetooth: {\n requestDevice: mockedRequestDevice,\n },\n } as unknown as Navigator;\n });\n\n afterAll(() => {\n vi.restoreAllMocks();\n global.navigator = undefined as unknown as Navigator;\n });\n\n it(\"should support the transport\", () => {\n expect(transport.isSupported()).toBe(true);\n });\n\n describe(\"startDiscovering\", () => {\n it(\"should emit device if one new grant access\", () =>\n new Promise<void>((resolve, reject) => {\n mockedRequestDevice.mockResolvedValueOnce(stubDevice);\n\n discoverDevice(\n (discoveredDevice) => {\n try {\n expect(discoveredDevice).toEqual(\n expect.objectContaining({\n deviceModel: expect.objectContaining({\n id: \"nanoX\",\n productName: \"Ledger Nano X\",\n }) as DeviceModel,\n }),\n );\n\n resolve();\n } catch (expectError) {\n reject(expectError as Error);\n }\n },\n (error) => {\n reject(error as Error);\n },\n );\n }));\n\n it(\"should throw DeviceNotRecognizedError if the device is not recognized\", () =>\n new Promise<void>((resolve, reject) => {\n mockedRequestDevice.mockResolvedValueOnce({\n ...stubDevice,\n gatt: {\n ...stubDevice.gatt,\n getPrimaryServices: vi.fn(() => Promise.resolve([])),\n },\n productId: 0x4242,\n });\n\n discoverDevice(\n () => {\n reject(\"should not return a device\");\n },\n (error) => {\n expect(error).toBeInstanceOf(BleDeviceGattServerError);\n resolve();\n },\n );\n }));\n\n it(\"should emit an error if the request device is in error\", () =>\n new Promise<void>((resolve, reject) => {\n const message = \"request device error\";\n mockedRequestDevice.mockImplementationOnce(() => {\n throw new Error(message);\n });\n\n discoverDevice(\n () => {\n reject(\"should not return a device\");\n },\n (error) => {\n expect(error).toBeInstanceOf(NoAccessibleDeviceError);\n expect(error).toStrictEqual(\n new NoAccessibleDeviceError(new Error(message)),\n );\n resolve();\n },\n );\n }));\n\n it(\"should emit an error if the user did not grant us access to a device (clicking on cancel on the browser popup for ex)\", () =>\n new Promise<void>((resolve, reject) => {\n mockedRequestDevice.mockResolvedValueOnce({ forget: vi.fn() });\n\n discoverDevice(\n (discoveredDevice) => {\n reject(\n `Should not emit any value, but emitted ${JSON.stringify(\n discoveredDevice,\n )}`,\n );\n },\n (error) => {\n try {\n expect(error).toBeInstanceOf(BleDeviceGattServerError);\n resolve();\n } catch (expectError) {\n reject(expectError as Error);\n }\n },\n );\n }));\n });\n\n describe(\"connect\", () => {\n it(\"should throw UnknownDeviceError if no internal device\", async () => {\n const connectParams = {\n deviceId: \"fake\",\n onDisconnect: vi.fn(),\n };\n\n const connect = await transport.connect(connectParams);\n\n expect(connect).toStrictEqual(\n Left(new UnknownDeviceError(\"Unknown device fake\")),\n );\n });\n\n it(\"should throw OpeningConnectionError if the device is already opened\", async () => {\n const device = {\n deviceId: \"fake\",\n onDisconnect: vi.fn(),\n };\n\n const connect = await transport.connect(device);\n\n expect(connect).toStrictEqual(\n Left(new UnknownDeviceError(\"Unknown device fake\")),\n );\n });\n\n it(\"should throw OpeningConnectionError if the device cannot be opened\", () =>\n new Promise<void>((resolve, reject) => {\n const message = \"cannot be opened\";\n mockedRequestDevice.mockResolvedValueOnce({\n ...stubDevice,\n gatt: {\n connect: () => {\n throw new Error(message);\n },\n },\n });\n\n discoverDevice(\n () => {\n reject(\"Should not emit any value\");\n },\n (error) => {\n expect(error).toBeInstanceOf(OpeningConnectionError);\n resolve();\n },\n );\n }));\n\n it(\"should return the opened device\", () =>\n new Promise<void>((resolve, reject) => {\n mockedRequestDevice.mockResolvedValueOnce({\n ...stubDevice,\n gatt: {\n ...stubDevice.gatt,\n connected: true,\n },\n });\n\n discoverDevice(\n (discoveredDevice) => {\n transport\n .connect({\n deviceId: discoveredDevice.id,\n onDisconnect: vi.fn(),\n })\n .then((connectedDevice) => {\n connectedDevice\n .ifRight((device) => {\n expect(device).toEqual(\n expect.objectContaining({ id: discoveredDevice.id }),\n );\n resolve();\n })\n .ifLeft(() => {\n reject(connectedDevice);\n });\n })\n .catch((error) => {\n reject(error);\n });\n },\n (error) => {\n reject(error as Error);\n },\n );\n }));\n\n it(\"should return a device if available\", () =>\n new Promise<void>((resolve, reject) => {\n mockedRequestDevice.mockResolvedValueOnce(stubDevice);\n\n discoverDevice(\n (discoveredDevice) => {\n transport\n .connect({\n deviceId: discoveredDevice.id,\n onDisconnect: vi.fn(),\n })\n .then((connectedDevice) => {\n connectedDevice\n .ifRight((device) => {\n expect(device).toEqual(\n expect.objectContaining({ id: discoveredDevice.id }),\n );\n resolve();\n })\n .ifLeft(() => {\n reject(connectedDevice);\n });\n })\n .catch((error) => {\n reject(error);\n });\n },\n (error) => {\n reject(error as Error);\n },\n );\n }));\n });\n\n describe(\"disconnect\", () => {\n it(\"should disconnect the device\", () =>\n new Promise<void>((resolve, reject) => {\n mockedRequestDevice.mockResolvedValueOnce(stubDevice);\n\n const onDisconnect = vi.fn();\n\n discoverDevice(\n (discoveredDevice) => {\n transport\n .connect({\n deviceId: discoveredDevice.id,\n onDisconnect,\n })\n .then((connectedDevice) => {\n connectedDevice.ifRight((device) => {\n transport\n .disconnect({ connectedDevice: device })\n .then((value) => {\n expect(value).toStrictEqual(Right(void 0));\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n });\n },\n (error) => {\n reject(error as Error);\n },\n );\n }));\n\n it(\"should call disconnect handler if device is hardware disconnected\", () =>\n new Promise<void>((resolve, reject) => {\n const onDisconnect = vi.fn();\n const disconnectSpy = vi.spyOn(transport, \"disconnect\");\n mockedRequestDevice.mockResolvedValueOnce(stubDevice);\n\n discoverDevice(\n (discoveredDevice) => {\n transport\n .connect({\n deviceId: discoveredDevice.id,\n onDisconnect,\n })\n .then(() => {\n stubDevice.ongattserverdisconnected(new Event(\"\"));\n vi.advanceTimersByTime(RECONNECT_DEVICE_TIMEOUT);\n expect(disconnectSpy).toHaveBeenCalled();\n resolve();\n });\n },\n (error) => {\n reject(error as Error);\n },\n );\n }));\n });\n\n describe(\"reconnect\", () => {\n it(\"should not call disconnection if reconnection happen\", () =>\n new Promise<void>((resolve, reject) => {\n // given\n const onDisconnect = vi.fn();\n const disconnectSpy = vi.spyOn(transport, \"disconnect\");\n mockedRequestDevice.mockResolvedValueOnce(stubDevice);\n\n // when\n discoverDevice((discoveredDevice) => {\n transport\n .connect({\n deviceId: discoveredDevice.id,\n onDisconnect,\n })\n .then(() => {\n stubDevice.ongattserverdisconnected(new Event(\"\"));\n\n vi.advanceTimersByTime(RECONNECT_DEVICE_TIMEOUT / 3);\n\n // then\n expect(disconnectSpy).toHaveBeenCalledTimes(0);\n resolve();\n })\n .catch((error) => {\n reject(error);\n });\n });\n }));\n });\n });\n});\n"],
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": [
|
|
3
|
+
"sources": [],
|
|
4
|
+
"sourcesContent": [],
|
|
5
|
+
"mappings": "",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
package/lib/esm/package.json
CHANGED
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
],
|
|
16
16
|
"scripts": {
|
|
17
17
|
"prebuild": "rimraf lib",
|
|
18
|
-
"build": "pnpm
|
|
18
|
+
"build": "pnpm ldmk-tool build --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json --platform web",
|
|
19
19
|
"dev": "concurrently \"pnpm watch:builds\" \"pnpm watch:types\"",
|
|
20
|
-
"watch:builds": "pnpm
|
|
20
|
+
"watch:builds": "pnpm ldmk-tool watch --entryPoints src/index.ts,src/**/*.ts --tsconfig tsconfig.prod.json --platform web",
|
|
21
21
|
"watch:types": "concurrently \"tsc --watch -p tsconfig.prod.json\" \"tsc-alias --watch -p tsconfig.prod.json\"",
|
|
22
22
|
"lint": "eslint",
|
|
23
23
|
"lint:fix": "pnpm lint --fix",
|
|
@@ -30,24 +30,24 @@
|
|
|
30
30
|
"test:coverage": "vitest run --coverage"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@sentry/minimal": "
|
|
34
|
-
"purify-ts": "
|
|
35
|
-
"uuid": "
|
|
33
|
+
"@sentry/minimal": "catalog:",
|
|
34
|
+
"purify-ts": "catalog:",
|
|
35
|
+
"uuid": "catalog:"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@ledgerhq/device-management-kit": "workspace:*",
|
|
39
|
-
"@ledgerhq/
|
|
39
|
+
"@ledgerhq/ldmk-tool": "workspace:*",
|
|
40
40
|
"@ledgerhq/eslint-config-dsdk": "workspace:*",
|
|
41
41
|
"@ledgerhq/prettier-config-dsdk": "workspace:*",
|
|
42
42
|
"@ledgerhq/tsconfig-dsdk": "workspace:*",
|
|
43
43
|
"@ledgerhq/vitest-config-dmk": "workspace:*",
|
|
44
|
-
"@types/uuid": "
|
|
45
|
-
"@types/web-bluetooth": "
|
|
46
|
-
"rxjs": "
|
|
47
|
-
"ts-node": "
|
|
44
|
+
"@types/uuid": "catalog:",
|
|
45
|
+
"@types/web-bluetooth": "catalog:",
|
|
46
|
+
"rxjs": "catalog:",
|
|
47
|
+
"ts-node": "catalog:"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"@ledgerhq/device-management-kit": "workspace:*",
|
|
51
|
-
"rxjs": "
|
|
51
|
+
"rxjs": "catalog:"
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebBleConfig.d.ts","sourceRoot":"","sources":["../../../../src/api/data/WebBleConfig.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,wBAAwB,OAAO,CAAC"}
|
|
1
|
+
{"version":3,"file":"WebBleConfig.d.ts","sourceRoot":"","sources":["../../../../src/api/data/WebBleConfig.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,wBAAwB,QAAQ,CAAC;AAC9C,eAAO,MAAM,2BAA2B,OAAO,CAAC;AAChD,eAAO,MAAM,wBAAwB,IAAI,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BleDevice.stub.d.ts","sourceRoot":"","sources":["../../../../src/api/model/BleDevice.stub.ts"],"names":[],"mappings":"AAoCA,eAAO,MAAM,4BAA4B,
|
|
1
|
+
{"version":3,"file":"BleDevice.stub.d.ts","sourceRoot":"","sources":["../../../../src/api/model/BleDevice.stub.ts"],"names":[],"mappings":"AAoCA,eAAO,MAAM,4BAA4B,GACvC,QAAO,OAAO,CAAC,iCAAiC,CAAM,KACrD,iCAQsC,CAAC;AAE1C,eAAO,MAAM,oBAAoB,GAC/B,QAAO,OAAO,CAAC,eAAe,CAAM,KACnC,eAaD,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type ApduReceiverServiceFactory, type ApduResponse, type ApduSenderServiceFactory, type DeviceApduSender, type DmkError, type LoggerPublisherService } from "@ledgerhq/device-management-kit";
|
|
2
|
+
import { type Either } from "purify-ts";
|
|
3
|
+
export type WebBleApduSenderDependencies = {
|
|
4
|
+
writeCharacteristic: BluetoothRemoteGATTCharacteristic;
|
|
5
|
+
notifyCharacteristic: BluetoothRemoteGATTCharacteristic;
|
|
6
|
+
};
|
|
7
|
+
export declare class WebBleApduSender implements DeviceApduSender<WebBleApduSenderDependencies> {
|
|
8
|
+
private _deps;
|
|
9
|
+
private _apduSender;
|
|
10
|
+
private _apduSenderFactory;
|
|
11
|
+
private _apduReceiverFactory;
|
|
12
|
+
private _apduReceiver;
|
|
13
|
+
private _logger;
|
|
14
|
+
private _isDeviceReady;
|
|
15
|
+
private _notificationsActive;
|
|
16
|
+
private _mtuHandshakeInFlight;
|
|
17
|
+
private _sendResolver;
|
|
18
|
+
private _writeMode;
|
|
19
|
+
constructor(deps: WebBleApduSenderDependencies & {
|
|
20
|
+
apduSenderFactory: ApduSenderServiceFactory;
|
|
21
|
+
apduReceiverFactory: ApduReceiverServiceFactory;
|
|
22
|
+
}, loggerFactory: (tag: string) => LoggerPublisherService);
|
|
23
|
+
private _gattConnected;
|
|
24
|
+
private _looksDisconnected;
|
|
25
|
+
private _failPendingSend;
|
|
26
|
+
private _markLinkDown;
|
|
27
|
+
private _sleep;
|
|
28
|
+
private _handleNotify;
|
|
29
|
+
private _onReceiveSetup;
|
|
30
|
+
private _onReceiveApdu;
|
|
31
|
+
private _chooseWriteMode;
|
|
32
|
+
private _write;
|
|
33
|
+
private _awaitReady;
|
|
34
|
+
private _isLegacyPing;
|
|
35
|
+
setupConnection(): Promise<void>;
|
|
36
|
+
sendApdu(apdu: Uint8Array, triggersDisconnection?: boolean, abortTimeout?: number): Promise<Either<DmkError, ApduResponse>>;
|
|
37
|
+
closeConnection(): void;
|
|
38
|
+
getDependencies(): WebBleApduSenderDependencies;
|
|
39
|
+
setDependencies(deps: WebBleApduSenderDependencies): void;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=WebBleApduSender.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebBleApduSender.d.ts","sourceRoot":"","sources":["../../../../src/api/transport/WebBleApduSender.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,KAAK,0BAA0B,EAC/B,KAAK,YAAY,EACjB,KAAK,wBAAwB,EAC7B,KAAK,gBAAgB,EAGrB,KAAK,QAAQ,EACb,KAAK,sBAAsB,EAE5B,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,KAAK,MAAM,EAAsB,MAAM,WAAW,CAAC;AAG5D,MAAM,MAAM,4BAA4B,GAAG;IACzC,mBAAmB,EAAE,iCAAiC,CAAC;IACvD,oBAAoB,EAAE,iCAAiC,CAAC;CACzD,CAAC;AAMF,qBAAa,gBACX,YAAW,gBAAgB,CAAC,4BAA4B,CAAC;IAEzD,OAAO,CAAC,KAAK,CAA+B;IAC5C,OAAO,CAAC,WAAW,CACH;IAChB,OAAO,CAAC,kBAAkB,CAA2B;IACrD,OAAO,CAAC,oBAAoB,CAA6B;IACzD,OAAO,CAAC,aAAa,CAAyC;IAC9D,OAAO,CAAC,OAAO,CAAyB;IAExC,OAAO,CAAC,cAAc,CAAuC;IAC7D,OAAO,CAAC,oBAAoB,CAAS;IACrC,OAAO,CAAC,qBAAqB,CAAS;IAEtC,OAAO,CAAC,aAAa,CACL;IAEhB,OAAO,CAAC,UAAU,CAA0B;gBA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