"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ledgerjs_hw_app_cardano_1 = require("@cardano-foundation/ledgerjs-hw-app-cardano");
const address_1 = require("@cardano-foundation/ledgerjs-hw-app-cardano/dist/utils/address");
/**
 * Convert a CoinModule transaction format into a transaction to sign with Cardano Foundation signer.
 * @param network
 * @param transaction coming form CoinModule and will be converted to `SignTransactionRequest`
 * @returns
 */
function default_1(network, transaction) {
    return {
        signingMode: ledgerjs_hw_app_cardano_1.TransactionSigningMode.ORDINARY_TRANSACTION,
        tx: {
            network,
            inputs: transaction.inputs.map(prepareLedgerInput),
            outputs: transaction.outputs.map(prepareLedgerOutput),
            certificates: transaction.certificates.map(prepareCertificate),
            withdrawals: transaction.withdrawals.map(prepareWithdrawal),
            fee: transaction.fee,
            ttl: transaction.ttl,
            validityIntervalStart: null,
            auxiliaryData: transaction.auxiliaryData
                ? {
                    type: ledgerjs_hw_app_cardano_1.TxAuxiliaryDataType.ARBITRARY_HASH,
                    params: {
                        hashHex: transaction.auxiliaryData,
                    },
                }
                : null,
        },
        additionalWitnessPaths: [],
    };
}
exports.default = default_1;
/**
 * returns the formatted transactionInput for ledger cardano app
 *
 * @param {TyphonTypes.Input} input
 * @param {number} accountIndex
 * @returns {TxInput}
 */
function prepareLedgerInput({ txHashHex, outputIndex, path }) {
    return {
        txHashHex,
        outputIndex,
        path: path ? (0, address_1.str_to_path)(path) : null,
    };
}
/**
 * returns the formatted transactionOutput for ledger cardano app
 *
 * @param output
 * @param accountIndex
 * @returns {TxOutput}
 */
function prepareLedgerOutput(output) {
    const { amount, tokenBundle } = output;
    const destination = convertDestination(output);
    return {
        format: ledgerjs_hw_app_cardano_1.TxOutputFormat.MAP_BABBAGE,
        amount,
        destination,
        tokenBundle,
    };
}
function convertDestination({ destination }) {
    if (destination.isDeviceOwnedAddress) {
        return {
            type: ledgerjs_hw_app_cardano_1.TxOutputDestinationType.DEVICE_OWNED,
            params: {
                type: ledgerjs_hw_app_cardano_1.AddressType.BASE_PAYMENT_KEY_STAKE_KEY,
                params: {
                    spendingPath: (0, address_1.str_to_path)(destination.params.spendingPath),
                    stakingPath: (0, address_1.str_to_path)(destination.params.stakingPath),
                },
            },
        };
    }
    else {
        return {
            type: ledgerjs_hw_app_cardano_1.TxOutputDestinationType.THIRD_PARTY,
            params: {
                addressHex: destination.params.addressHex,
            },
        };
    }
}
function prepareCertificate(cert) {
    const stakeCredential = {
        type: ledgerjs_hw_app_cardano_1.CredentialParamsType.KEY_PATH,
        keyPath: (0, address_1.str_to_path)(cert.params.stakeCredential.keyPath),
    };
    switch (cert.type) {
        case "REGISTRATION":
            return {
                type: ledgerjs_hw_app_cardano_1.CertificateType.STAKE_REGISTRATION_CONWAY,
                params: {
                    stakeCredential,
                    deposit: cert.params.deposit,
                },
            };
        case "DELEGATION":
            return {
                type: ledgerjs_hw_app_cardano_1.CertificateType.STAKE_DELEGATION,
                params: {
                    stakeCredential,
                    poolKeyHashHex: cert.params.poolKeyHashHex,
                },
            };
        case "DEREGISTRATION":
            return {
                type: ledgerjs_hw_app_cardano_1.CertificateType.STAKE_DEREGISTRATION_CONWAY,
                params: {
                    stakeCredential,
                    deposit: cert.params.deposit,
                },
            };
        case "VOTE_DELEGATION_ABSTAIN":
            return {
                type: ledgerjs_hw_app_cardano_1.CertificateType.VOTE_DELEGATION,
                params: {
                    stakeCredential,
                    dRep: {
                        type: ledgerjs_hw_app_cardano_1.DRepParamsType.ABSTAIN,
                    },
                },
            };
        default:
            throw new Error("Invalid Certificate type");
    }
}
function prepareWithdrawal({ stakeCredential, amount }) {
    return {
        stakeCredential: {
            type: ledgerjs_hw_app_cardano_1.CredentialParamsType.KEY_PATH,
            keyPath: (0, address_1.str_to_path)(stakeCredential.keyPath),
        },
        amount,
    };
}
//# sourceMappingURL=signerSerializer.js.map