UNPKG

@metamask/eth-ledger-bridge-keyring

Version:
57 lines 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isDeviceExchangeError = exports.translateDmkError = void 0; const device_management_kit_1 = require("@ledgerhq/device-management-kit"); const hw_transport_1 = require("@ledgerhq/hw-transport"); const hw_wallet_sdk_1 = require("@metamask/hw-wallet-sdk"); const errors_1 = require("../errors.cjs"); const GENERIC_ERROR_STATUS_CODE = 0x6f00; /** * Translates a DMK error into an error the Ledger keyring error handler can * process. * * Resolution order: * 1. **DMK `_tag`** — connection/session-level DMK errors (e.g. * `DeviceSessionNotFound`, `DeviceLockedError`) carry a `_tag` but no hex * APDU status code. These are resolved to a {@link HardwareWalletError} * via the shared SDK mappings. * 2. **`DeviceExchangeError` hex code** — APDU-level errors that carry a * 4-character hex `errorCode` (e.g. `'6985'`). These are converted to a * {@link TransportStatusError} so the existing `LEDGER_ERROR_MAPPINGS` * lookup in the keyring error handler applies. * 3. **Generic fallback** — anything else is wrapped in a * {@link TransportStatusError} with status `0x6f00`. * * @param error - The error from a DMK device action or command. * @returns A `HardwareWalletError` (for resolved DMK tags) or a * `TransportStatusError` (for hex codes and the generic fallback). */ function translateDmkError(error) { // 1. DMK connection/session errors identified by _tag const tagResolution = (0, hw_wallet_sdk_1.getDmkErrorFromTag)(error); if (tagResolution) { return (0, errors_1.createDmkError)(tagResolution.tag); } // 2. DeviceExchangeError with hex APDU status code if (isDeviceExchangeError(error)) { const statusCode = parseHexErrorCode(error.errorCode); return new hw_transport_1.TransportStatusError(statusCode); } // 3. Generic fallback return new hw_transport_1.TransportStatusError(GENERIC_ERROR_STATUS_CODE); } exports.translateDmkError = translateDmkError; function isDeviceExchangeError(error) { return (typeof error === 'object' && error !== null && '_tag' in error && 'errorCode' in error); } exports.isDeviceExchangeError = isDeviceExchangeError; function parseHexErrorCode(errorCode) { if (typeof errorCode === 'string' && /^[0-9a-fA-F]{4}$/u.test(errorCode)) { return parseInt(errorCode, 16); } return GENERIC_ERROR_STATUS_CODE; } //# sourceMappingURL=dmk-error-translator.cjs.map