// Goal of this file is to inject all necessary device/signer dependency to coin-modules
import { firstValueFrom, from } from "rxjs";
import Acre from "@blooo/hw-app-acre/lib/index";
import { createBridges } from "@ledgerhq/coin-bitcoin/bridge/js";
import makeCliTools from "@ledgerhq/coin-bitcoin/cli-transaction";
import bitcoinResolver from "@ledgerhq/coin-bitcoin/hw-getAddress";
import { signMessage } from "@ledgerhq/coin-bitcoin/hw-signMessage";
import { withDevice } from "../../hw/deviceAccess";
import { getCurrencyConfiguration } from "../../config";
const createSigner = (transport, currency) => {
    return new Acre({ transport, currency: currency.id });
};
const signerContext = (deviceId, crypto, fn) => firstValueFrom(withDevice(deviceId)((transport) => from(fn(createSigner(transport, crypto)))));
const getCurrencyConfig = (currency) => {
    return { info: getCurrencyConfiguration(currency) };
};
const bridge = createBridges(signerContext, getCurrencyConfig);
export function createMessageSigner() {
    return (transport, account, messageData) => {
        const signerContext = (_, crypto, fn) => fn(createSigner(transport, crypto));
        return signMessage(signerContext)("", account, messageData);
    };
}
export 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);
    };
}
export 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);
    };
}
const messageSigner = {
    signMessage: createMessageSigner(),
    signWithdraw: createWithdrawSigner(),
    signIn: createSignInSigner(),
};
const resolver = (transport, addressOpt) => {
    const signerContext = (_, crypto, fn) => fn(createSigner(transport, crypto));
    return bitcoinResolver(signerContext)("", addressOpt);
};
const cliTools = makeCliTools();
export { bridge, cliTools, resolver, messageSigner, signerContext };
//# sourceMappingURL=ACRESetup.js.map