// Goal of this file is to inject all necessary device/signer dependency to coin-modules
import Ada, { Networks, AddressType } from "@cardano-foundation/ledgerjs-hw-app-cardano";
import { str_to_path } from "@cardano-foundation/ledgerjs-hw-app-cardano/dist/utils/address";
import { createBridges } from "@ledgerhq/coin-cardano/bridge";
import makeCliTools from "@ledgerhq/coin-cardano/cli-transaction";
import cardanoResolver from "@ledgerhq/coin-cardano/hw-getAddress";
import { createResolver, executeWithSigner } from "../../bridge/setup";
import signerSerializer from "./signerSerializer";
import { getCurrencyConfiguration } from "../../config";
import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets/currencies";
function findNetwork(networkParams) {
    return networkParams.networkId === Networks.Mainnet.networkId
        ? Networks.Mainnet
        : Networks.Testnet;
}
const createSigner = (transport) => {
    const ada = new Ada(transport);
    return {
        getAddress: async ({ path, stakingPathString, networkParams, verify, }) => {
            const network = findNetwork(networkParams);
            const addr = await ada.deriveAddress({
                network,
                address: {
                    type: AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
                    params: {
                        spendingPath: str_to_path(path),
                        stakingPath: str_to_path(stakingPathString),
                    },
                },
            });
            if (verify) {
                await ada.showAddress({
                    network,
                    address: {
                        type: AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
                        params: {
                            spendingPath: str_to_path(path),
                            stakingPath: str_to_path(stakingPathString),
                        },
                    },
                });
            }
            return addr;
        },
        getPublicKey: async (accountPath) => {
            return ada.getExtendedPublicKey({
                path: str_to_path(accountPath),
            });
        },
        sign: async ({ transaction, networkParams }) => {
            const network = findNetwork(networkParams);
            const trxOptions = signerSerializer(network, transaction);
            return ada.signTransaction(trxOptions);
        },
    };
};
const getCurrencyConfig = () => {
    return getCurrencyConfiguration(getCryptoCurrencyById("cardano"));
};
const bridge = createBridges(executeWithSigner(createSigner), getCurrencyConfig);
const resolver = createResolver(createSigner, cardanoResolver);
const cliTools = makeCliTools();
export { bridge, cliTools, resolver };
//# sourceMappingURL=setup.js.map