"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getValidateAddress = exports.getSigner = void 0;
const getAddress_1 = __importDefault(require("@ledgerhq/coin-xrp/signer/getAddress"));
const getAddress_2 = __importDefault(require("@ledgerhq/coin-stellar/signer/getAddress"));
const hw_app_str_1 = __importDefault(require("@ledgerhq/hw-app-str"));
const signTransaction_1 = require("./signTransaction");
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const setup_1 = require("../../setup");
const hw_app_xrp_1 = __importDefault(require("@ledgerhq/hw-app-xrp"));
const ledger_signer_1 = require("@taquito/ledger-signer");
const getAddress_3 = __importDefault(require("@ledgerhq/coin-tezos/signer/getAddress"));
const hw_app_tezos_1 = __importDefault(require("@ledgerhq/hw-app-tezos"));
const Eth_1 = require("./Eth");
const validateAddress_1 = require("@ledgerhq/coin-tezos/logic/validateAddress");
const validateAddress_2 = require("@ledgerhq/coin-xrp/logic/validateAddress");
const validateAddress_3 = require("@ledgerhq/coin-stellar/logic/validateAddress");
const validateAddress_4 = require("@ledgerhq/coin-evm/logic/validateAddress");
const createSignerXrp = (transport) => {
    return new hw_app_xrp_1.default(transport);
};
const signerContextXrp = (0, setup_1.executeWithSigner)(createSignerXrp);
const createSignerStellar = (transport) => {
    const stellar = new hw_app_str_1.default(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 = stellar_sdk_1.StrKey.encodeEd25519PublicKey(rawPublicKey);
            return {
                path,
                address: publicKey,
                publicKey: publicKey,
            };
        },
    });
};
const signerContextStellar = (0, setup_1.executeWithSigner)(createSignerStellar);
const createSignerTezos = (transport) => {
    const tezos = new hw_app_tezos_1.default(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 ledger_signer_1.LedgerSigner(transport, path, !!verify, ledger_signer_1.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 = ledger_signer_1.DerivationType.ED25519;
            if (derivationType === 1)
                dt = ledger_signer_1.DerivationType.SECP256K1;
            else if (derivationType === 2)
                dt = ledger_signer_1.DerivationType.P256;
            return new ledger_signer_1.LedgerSigner(transport, path, prompt, dt);
        },
    });
};
const signerContextTezos = (0, setup_1.executeWithSigner)(createSignerTezos);
function getSigner(network) {
    switch (network) {
        case "ripple":
        case "xrp": {
            return {
                getAddress: (0, getAddress_1.default)(signerContextXrp),
                signTransaction: (0, signTransaction_1.signTransaction)(signerContextXrp),
                context: signerContextXrp,
            };
        }
        case "stellar": {
            return {
                getAddress: (0, getAddress_2.default)(signerContextStellar),
                signTransaction: (0, signTransaction_1.stellarSignTransaction)(signerContextStellar),
                context: signerContextStellar,
            };
        }
        case "tezos": {
            return {
                getAddress: (0, getAddress_3.default)(signerContextTezos),
                signTransaction: (0, signTransaction_1.tezosSignTransaction)((0, setup_1.executeWithSigner)(createSignerTezos)),
                context: signerContextTezos,
            };
        }
        case "evm": {
            return {
                getAddress: Eth_1.getAddress,
                context: Eth_1.context,
            };
        }
    }
    throw new Error(`signer for ${network} not implemented`);
}
exports.getSigner = getSigner;
const validateAddressByNetwork = new Map();
validateAddressByNetwork.set("stellar", validateAddress_3.validateAddress);
validateAddressByNetwork.set("xrp", validateAddress_2.validateAddress);
validateAddressByNetwork.set("tezos", validateAddress_1.validateAddress);
validateAddressByNetwork.set("evm", validateAddress_4.validateAddress);
validateAddressByNetwork.set("ethereum", validateAddress_4.validateAddress); // for the coin tester
validateAddressByNetwork.set("sonic", validateAddress_4.validateAddress); // for the coin tester
validateAddressByNetwork.set("polygon", validateAddress_4.validateAddress); // for the coin tester
validateAddressByNetwork.set("core", validateAddress_4.validateAddress); // for the coin tester
function getValidateAddress(network) {
    const validateAddress = validateAddressByNetwork.get(network);
    if (!validateAddress) {
        throw new Error(`No validate address function for network ${network}`);
    }
    return validateAddress;
}
exports.getValidateAddress = getValidateAddress;
//# sourceMappingURL=index.js.map