UNPKG

@ledgerhq/live-common

Version:
55 lines 3.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGenuineCheckFromDeviceId = void 0; const rxjs_1 = require("rxjs"); const operators_1 = require("rxjs/operators"); const errors_1 = require("@ledgerhq/errors"); const getDeviceInfo_1 = __importDefault(require("./getDeviceInfo")); const deviceAccess_1 = require("./deviceAccess"); const genuineCheck_1 = __importDefault(require("./genuineCheck")); /** * Get a genuine check for a device only from its id * @param deviceId A device id, or an empty string if device is usb plugged * @param lockedDeviceTimeoutMs Time of no response from device after which the device is considered locked, in ms. Default 1000ms. * @returns An Observable pushing objects containing: * - socketEvent: a SocketEvent giving the current status of the genuine check, * null if the genuine check process did not reach any state yet * - lockedDevice: a boolean set to true if the device is currently locked, false otherwise */ const getGenuineCheckFromDeviceId = ({ deviceId, lockedDeviceTimeoutMs = 1000, }) => { return new rxjs_1.Observable(subscriber => { // In order to know if a device is locked or not. // As we're not timing out inside the genuineCheckObservable flow (with rxjs timeout for ex) // once the device is unlock, getDeviceInfo should return the device info and // the flow will continue. No need to handle a retry strategy const lockedDeviceTimeout = setTimeout(() => { subscriber.next({ socketEvent: null, lockedDevice: true }); }, lockedDeviceTimeoutMs); // Returns a Subscription that can be unsubscribed/cleaned return ((0, deviceAccess_1.withDevice)(deviceId)(t => (0, rxjs_1.from)((0, getDeviceInfo_1.default)(t)).pipe((0, operators_1.mergeMap)(deviceInfo => { clearTimeout(lockedDeviceTimeout); subscriber.next({ socketEvent: null, lockedDevice: false }); return (0, genuineCheck_1.default)(t, deviceInfo); }))) // Needs to retry with withDevice .pipe((0, operators_1.retryWhen)((0, deviceAccess_1.retryWhileErrors)((e) => { // Cancels the locked-device unresponsive/timeout strategy if received any response/error clearTimeout(lockedDeviceTimeout); if (e instanceof errors_1.LockedDeviceError) { subscriber.next({ socketEvent: null, lockedDevice: true }); return true; } return false; }))) .subscribe({ next: (socketEvent) => subscriber.next({ socketEvent, lockedDevice: false }), error: e => subscriber.error(e), complete: () => subscriber.complete(), })); }); }; exports.getGenuineCheckFromDeviceId = getGenuineCheckFromDeviceId; //# sourceMappingURL=getGenuineCheckFromDeviceId.js.map