import xrpGetAddress from "@ledgerhq/coin-xrp/signer/getAddress";
import stellarGetAddress from "@ledgerhq/coin-stellar/signer/getAddress";
import Stellar from "@ledgerhq/hw-app-str";
import { signTransaction, stellarSignTransaction, tezosSignTransaction } from "./signTransaction";
import { StrKey } from "@stellar/stellar-sdk";
import { executeWithSigner } from "../../setup";
import Xrp from "@ledgerhq/hw-app-xrp";
import { DerivationType, LedgerSigner as TaquitoLedgerSigner } from "@taquito/ledger-signer";
import tezosGetAddress from "@ledgerhq/coin-tezos/signer/getAddress";
import Tezos from "@ledgerhq/hw-app-tezos";
import { context as evmContext, getAddress as evmGetAddress } from "./Eth";
import { validateAddress as tezosValidateAddress } from "@ledgerhq/coin-tezos/logic/validateAddress";
import { validateAddress as xrpValidateAddress } from "@ledgerhq/coin-xrp/logic/validateAddress";
import { validateAddress as stellarValidateAddress } from "@ledgerhq/coin-stellar/logic/validateAddress";
import { validateAddress as evmValidateAddress } from "@ledgerhq/coin-evm/logic/validateAddress";
const createSignerXrp = (transport) => {
    return new Xrp(transport);
};
const signerContextXrp = executeWithSigner(createSignerXrp);
const createSignerStellar = (transport) => {
    const stellar = new Stellar(transport);
    const originalSignTransaction = stellar.signTransaction;
    // Return the original Stellar instance with overridden methods
    return Object.assign(stellar, {
        signTransaction: async (path, transaction) => {
            const unsignedPayload = Buffer.from(transaction, "base64");
            const { signature } = await originalSignTransaction(path, unsignedPayload);
            return signature.toString("base64");
        },
        getAddress: async (path, verify) => {
            const { rawPublicKey } = await stellar.getPublicKey(path, verify);
            const publicKey = StrKey.encodeEd25519PublicKey(rawPublicKey);
            return {
                path,
                address: publicKey,
                publicKey: publicKey,
            };
        },
    });
};
const signerContextStellar = executeWithSigner(createSignerStellar);
const createSignerTezos = (transport) => {
    const tezos = new Tezos(transport);
    // align with genericSignOperation that calls signer.signTransaction
    return Object.assign(tezos, {
        async signTransaction(path, rawTxHex) {
            const { signature } = await tezos.signOperation(path, rawTxHex, {});
            return signature;
        },
        async getAddress(path, { verify } = {}) {
            // Use Taquito LedgerSigner to retrieve base58 public key and matching pkh (like oldbridge)
            const ledgerSigner = new TaquitoLedgerSigner(transport, path, !!verify, DerivationType.ED25519);
            const address = await ledgerSigner.publicKeyHash();
            const publicKey = await ledgerSigner.publicKey();
            return { path, address, publicKey };
        },
        createLedgerSigner(path, prompt, derivationType) {
            // Map 0 -> ED25519, 1 -> SECP256K1, 2 -> P256 by convention
            let dt = DerivationType.ED25519;
            if (derivationType === 1)
                dt = DerivationType.SECP256K1;
            else if (derivationType === 2)
                dt = DerivationType.P256;
            return new TaquitoLedgerSigner(transport, path, prompt, dt);
        },
    });
};
const signerContextTezos = executeWithSigner(createSignerTezos);
export function getSigner(network) {
    switch (network) {
        case "ripple":
        case "xrp": {
            return {
                getAddress: xrpGetAddress(signerContextXrp),
                signTransaction: signTransaction(signerContextXrp),
                context: signerContextXrp,
            };
        }
        case "stellar": {
            return {
                getAddress: stellarGetAddress(signerContextStellar),
                signTransaction: stellarSignTransaction(signerContextStellar),
                context: signerContextStellar,
            };
        }
        case "tezos": {
            return {
                getAddress: tezosGetAddress(signerContextTezos),
                signTransaction: tezosSignTransaction(executeWithSigner(createSignerTezos)),
                context: signerContextTezos,
            };
        }
        case "evm": {
            return {
                getAddress: evmGetAddress,
                context: evmContext,
            };
        }
    }
    throw new Error(`signer for ${network} not implemented`);
}
const validateAddressByNetwork = new Map();
validateAddressByNetwork.set("stellar", stellarValidateAddress);
validateAddressByNetwork.set("xrp", xrpValidateAddress);
validateAddressByNetwork.set("tezos", tezosValidateAddress);
validateAddressByNetwork.set("evm", evmValidateAddress);
validateAddressByNetwork.set("ethereum", evmValidateAddress); // for the coin tester
validateAddressByNetwork.set("sonic", evmValidateAddress); // for the coin tester
validateAddressByNetwork.set("polygon", evmValidateAddress); // for the coin tester
validateAddressByNetwork.set("core", evmValidateAddress); // for the coin tester
export function getValidateAddress(network) {
    const validateAddress = validateAddressByNetwork.get(network);
    if (!validateAddress) {
        throw new Error(`No validate address function for network ${network}`);
    }
    return validateAddress;
}
//# sourceMappingURL=index.js.map