"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEVM = exports.sendEvmNanoS = exports.sendEvmButtonDevice = void 0;
const expect_1 = __importDefault(require("expect"));
const speculos_1 = require("../speculos");
const speculosAppVersion_1 = require("../speculosAppVersion");
const TouchDeviceSimulator_1 = require("../deviceInteraction/TouchDeviceSimulator");
const DeviceLabels_1 = require("../enum/DeviceLabels");
const Device_1 = require("../enum/Device");
const types_devices_1 = require("@ledgerhq/types-devices");
const DeviceController_1 = require("../deviceInteraction/DeviceController");
function validateTransactionData(tx, events) {
    const isAmountCorrect = (0, speculos_1.containsSubstringInEvent)(tx.amount, events);
    (0, expect_1.default)(isAmountCorrect).toBeTruthy();
    if (tx.accountToCredit.ensName && process.env.SPECULOS_DEVICE !== Device_1.Device.LNS.name) {
        const isENSNameCorrect = (0, speculos_1.containsSubstringInEvent)(tx.accountToCredit.ensName, events);
        (0, expect_1.default)(isENSNameCorrect).toBeTruthy();
    }
    else {
        const isAddressCorrect = (0, speculos_1.containsSubstringInEvent)(tx.accountToCredit.address, events);
        (0, expect_1.default)(isAddressCorrect).toBeTruthy();
    }
}
async function sendEvmTouchDevices(tx) {
    const events = await (0, speculos_1.pressUntilTextFound)(DeviceLabels_1.DeviceLabels.HOLD_TO_SIGN);
    validateTransactionData(tx, events);
    await (0, TouchDeviceSimulator_1.longPressAndRelease)(DeviceLabels_1.DeviceLabels.HOLD_TO_SIGN, 3);
}
exports.sendEvmButtonDevice = (0, DeviceController_1.withDeviceController)(({ getButtonsController }) => async (tx) => {
    const buttons = getButtonsController();
    const events = await (0, speculos_1.pressUntilTextFound)(DeviceLabels_1.DeviceLabels.SIGN_TRANSACTION);
    validateTransactionData(tx, events);
    await buttons.both();
});
exports.sendEvmNanoS = (0, DeviceController_1.withDeviceController)(({ getButtonsController }) => async (tx) => {
    const buttons = getButtonsController();
    const events = await (0, speculos_1.pressUntilTextFound)(DeviceLabels_1.DeviceLabels.ACCEPT);
    validateTransactionData(tx, events);
    await buttons.both();
});
async function sendEVM(tx) {
    const speculosModel = (0, speculosAppVersion_1.getSpeculosModel)();
    if ((0, speculosAppVersion_1.isTouchDevice)()) {
        return sendEvmTouchDevices(tx);
    }
    if (speculosModel === types_devices_1.DeviceModelId.nanoS) {
        return (0, exports.sendEvmNanoS)(tx);
    }
    return (0, exports.sendEvmButtonDevice)(tx);
}
exports.sendEVM = sendEVM;
//# sourceMappingURL=evm.js.map