"use strict";
// Goal of this file is to inject all necessary device/signer dependency to coin-modules
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolver = exports.cliTools = exports.bridge = void 0;
const ledgerjs_hw_app_cardano_1 = __importStar(require("@cardano-foundation/ledgerjs-hw-app-cardano"));
const address_1 = require("@cardano-foundation/ledgerjs-hw-app-cardano/dist/utils/address");
const bridge_1 = require("@ledgerhq/coin-cardano/bridge");
const cli_transaction_1 = __importDefault(require("@ledgerhq/coin-cardano/cli-transaction"));
const hw_getAddress_1 = __importDefault(require("@ledgerhq/coin-cardano/hw-getAddress"));
const setup_1 = require("../../bridge/setup");
const signerSerializer_1 = __importDefault(require("./signerSerializer"));
const config_1 = require("../../config");
const currencies_1 = require("@ledgerhq/cryptoassets/currencies");
function findNetwork(networkParams) {
    return networkParams.networkId === ledgerjs_hw_app_cardano_1.Networks.Mainnet.networkId
        ? ledgerjs_hw_app_cardano_1.Networks.Mainnet
        : ledgerjs_hw_app_cardano_1.Networks.Testnet;
}
const createSigner = (transport) => {
    const ada = new ledgerjs_hw_app_cardano_1.default(transport);
    return {
        getAddress: async ({ path, stakingPathString, networkParams, verify, }) => {
            const network = findNetwork(networkParams);
            const addr = await ada.deriveAddress({
                network,
                address: {
                    type: ledgerjs_hw_app_cardano_1.AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
                    params: {
                        spendingPath: (0, address_1.str_to_path)(path),
                        stakingPath: (0, address_1.str_to_path)(stakingPathString),
                    },
                },
            });
            if (verify) {
                await ada.showAddress({
                    network,
                    address: {
                        type: ledgerjs_hw_app_cardano_1.AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
                        params: {
                            spendingPath: (0, address_1.str_to_path)(path),
                            stakingPath: (0, address_1.str_to_path)(stakingPathString),
                        },
                    },
                });
            }
            return addr;
        },
        getPublicKey: async (accountPath) => {
            return ada.getExtendedPublicKey({
                path: (0, address_1.str_to_path)(accountPath),
            });
        },
        sign: async ({ transaction, networkParams }) => {
            const network = findNetwork(networkParams);
            const trxOptions = (0, signerSerializer_1.default)(network, transaction);
            return ada.signTransaction(trxOptions);
        },
    };
};
const getCurrencyConfig = () => {
    return (0, config_1.getCurrencyConfiguration)((0, currencies_1.getCryptoCurrencyById)("cardano"));
};
const bridge = (0, bridge_1.createBridges)((0, setup_1.executeWithSigner)(createSigner), getCurrencyConfig);
exports.bridge = bridge;
const resolver = (0, setup_1.createResolver)(createSigner, hw_getAddress_1.default);
exports.resolver = resolver;
const cliTools = (0, cli_transaction_1.default)();
exports.cliTools = cliTools;
//# sourceMappingURL=setup.js.map