"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const invariant_1 = __importDefault(require("invariant"));
const errors_1 = require("@ledgerhq/errors");
const logs_1 = require("@ledgerhq/logs");
const hw_getAddress_1 = __importDefault(require("../../generated/hw-getAddress"));
const dispatch = (transport, opts) => {
    const { currency, verify } = opts;
    const getAddress = hw_getAddress_1.default[currency.family];
    (0, invariant_1.default)(getAddress, `getAddress is not implemented for ${currency.id}`);
    return getAddress(transport, opts)
        .then(result => {
        (0, logs_1.log)("hw", `getAddress ${currency.id} on ${opts.path}`, result);
        return result;
    })
        .catch(e => {
        (0, logs_1.log)("hw", `getAddress ${currency.id} on ${opts.path} FAILED ${String(e)}`);
        if (e && e.name === "TransportStatusError") {
            if (e.statusCode === 0x6b00 && verify) {
                throw new errors_1.DeviceAppVerifyNotSupported();
            }
            if (e.statusCode === 0x6985 || e.statusCode === 0x5501) {
                throw new errors_1.UserRefusedAddress();
            }
        }
        throw e;
    });
};
exports.default = dispatch;
//# sourceMappingURL=index.js.map