"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAddress = exports.context = exports.createSigner = void 0;
const rxjs_1 = require("rxjs");
const setup_1 = require("../../setup");
const live_signer_evm_1 = require("@ledgerhq/live-signer-evm");
const live_env_1 = require("@ledgerhq/live-env");
const ethers_1 = require("ethers");
const hw_getAddress_1 = __importDefault(require("@ledgerhq/coin-evm/hw-getAddress"));
const isDmkTransport = (transport) => {
    return ("dmk" in transport &&
        transport.dmk !== undefined &&
        "sessionId" in transport &&
        transport.sessionId !== undefined);
};
const createLiveSigner = (transport) => {
    if (isDmkTransport(transport)) {
        return new live_signer_evm_1.DmkSignerEth(transport.dmk, transport.sessionId);
    }
    return new live_signer_evm_1.LegacySignerEth(transport);
};
const createSigner = (transport) => {
    const signer = createLiveSigner(transport);
    return {
        getAddress: signer.getAddress.bind(signer),
        signTransaction: async (path, tx, domain) => {
            // Configure type of resolutions necessary for the clear signing
            const resolutionConfig = {
                externalPlugins: true,
                erc20: true,
                nft: false,
                uniswapV3: true,
                domains: domain && [domain],
            };
            const loadConfig = {
                cryptoassetsBaseURL: (0, live_env_1.getEnv)("DYNAMIC_CAL_BASE_URL"),
                nftExplorerBaseURL: (0, live_env_1.getEnv)("NFT_METADATA_SERVICE") + "/v1/ethereum",
            };
            signer.setLoadConfig(loadConfig);
            const observable = signer.clearSignTransaction(path, tx.substring(2), resolutionConfig, true);
            const event = observable.pipe((0, rxjs_1.filter)((event) => {
                return event.type === "signer.evm.signed";
            }));
            const { value } = await (0, rxjs_1.firstValueFrom)(event);
            return ethers_1.Signature.from({
                r: "0x" + value.r,
                s: "0x" + value.s,
                v: typeof value.v === "number" ? value.v : parseInt(value.v, 16),
            }).serialized;
        },
    };
};
exports.createSigner = createSigner;
exports.context = (0, setup_1.executeWithSigner)(exports.createSigner);
exports.getAddress = (0, hw_getAddress_1.default)(exports.context);
//# sourceMappingURL=Eth.js.map