"use strict";
// Goal of this file is to inject all necessary device/signer dependency to coin-modules
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.signerContext = exports.messageSigner = exports.resolver = exports.cliTools = exports.bridge = exports.createSignInSigner = exports.createWithdrawSigner = exports.createMessageSigner = void 0;
const rxjs_1 = require("rxjs");
const index_1 = __importDefault(require("@blooo/hw-app-acre/lib/index"));
const js_1 = require("@ledgerhq/coin-bitcoin/bridge/js");
const cli_transaction_1 = __importDefault(require("@ledgerhq/coin-bitcoin/cli-transaction"));
const hw_getAddress_1 = __importDefault(require("@ledgerhq/coin-bitcoin/hw-getAddress"));
const hw_signMessage_1 = require("@ledgerhq/coin-bitcoin/hw-signMessage");
const deviceAccess_1 = require("../../hw/deviceAccess");
const config_1 = require("../../config");
const createSigner = (transport, currency) => {
    return new index_1.default({ transport, currency: currency.id });
};
const signerContext = (deviceId, crypto, fn) => (0, rxjs_1.firstValueFrom)((0, deviceAccess_1.withDevice)(deviceId)((transport) => (0, rxjs_1.from)(fn(createSigner(transport, crypto)))));
exports.signerContext = signerContext;
const getCurrencyConfig = (currency) => {
    return { info: (0, config_1.getCurrencyConfiguration)(currency) };
};
const bridge = (0, js_1.createBridges)(signerContext, getCurrencyConfig);
exports.bridge = bridge;
function createMessageSigner() {
    return (transport, account, messageData) => {
        const signerContext = (_, crypto, fn) => fn(createSigner(transport, crypto));
        return (0, hw_signMessage_1.signMessage)(signerContext)("", account, messageData);
    };
}
exports.createMessageSigner = createMessageSigner;
function createWithdrawSigner() {
    const signWithdraw = (signerContext) => async (deviceId, account, container) => {
        const path = "path" in container && container.path ? container.path : account.freshAddressPath;
        const result = (await signerContext(deviceId, account.currency, signer => signer.signWithdrawal(path, container.message)));
        const v = result["v"] + 27 + 4;
        const signature = `${v.toString(16)}${result["r"]}${result["s"]}`;
        return {
            rsv: result,
            signature,
        };
    };
    return (transport, account, messageData) => {
        const signerContext = (_, crypto, fn) => fn(createSigner(transport, crypto));
        return signWithdraw(signerContext)("", account, messageData);
    };
}
exports.createWithdrawSigner = createWithdrawSigner;
function createSignInSigner() {
    const signIn = (signerContext) => async (deviceId, account, container) => {
        const path = "path" in container && container.path ? container.path : account.freshAddressPath;
        const result = (await signerContext(deviceId, account.currency, signer => signer.signERC4361Message(path, Buffer.from(container.message).toString("hex"))));
        const v = result["v"] + 27 + 4;
        const signature = `${v.toString(16)}${result["r"]}${result["s"]}`;
        return {
            rsv: result,
            signature,
        };
    };
    return (transport, account, messageData) => {
        const signerContext = (_, crypto, fn) => fn(createSigner(transport, crypto));
        return signIn(signerContext)("", account, messageData);
    };
}
exports.createSignInSigner = createSignInSigner;
const messageSigner = {
    signMessage: createMessageSigner(),
    signWithdraw: createWithdrawSigner(),
    signIn: createSignInSigner(),
};
exports.messageSigner = messageSigner;
const resolver = (transport, addressOpt) => {
    const signerContext = (_, crypto, fn) => fn(createSigner(transport, crypto));
    return (0, hw_getAddress_1.default)(signerContext)("", addressOpt);
};
exports.resolver = resolver;
const cliTools = (0, cli_transaction_1.default)();
exports.cliTools = cliTools;
//# sourceMappingURL=ACRESetup.js.map