diff --git a/src/Zemu.ts b/src/Zemu.ts index a97ad14..7f388b0 100644 --- a/src/Zemu.ts +++ b/src/Zemu.ts @@ -44,12 +44,13 @@ import { DEFAULT_WAIT_TIMEOUT, DEFAULT_STAX_START_TEXT, DEFAULT_NANO_START_TEXT, + DEFAULT_PENDING_REVIEW_TEXT, } from "./constants"; import EmuContainer from "./emulator"; import GRPCRouter from "./grpc"; -import { scheduleToNavElement, TouchNavigation } from "./actions"; +import { ClickNavigation, scheduleToNavElement, TouchNavigation } from "./actions"; import { dummyButton, tapContinueButton, TouchElements } from "./buttons"; import { ActionKind, @@ -199,11 +200,33 @@ export default class Zemu { this.startOptions.startText = this.startOptions.model === "stax" ? DEFAULT_STAX_START_TEXT : DEFAULT_NANO_START_TEXT; } - await this.waitForText( - this.startOptions.startText, - this.startOptions.startTimeout, - this.startOptions.caseSensitive, - ); + const start = new Date(); + let found = false; + let reviewPendingFound = false; + const flags = !this.startOptions.caseSensitive ? "i" : ""; + const startRegex = new RegExp(this.startOptions.startText, flags); + const reviewPendingRegex = new RegExp(DEFAULT_PENDING_REVIEW_TEXT, flags); + + while (!found) { + const currentTime = new Date(); + const elapsed = currentTime.getTime() - start.getTime(); + if (elapsed > this.startOptions.startTimeout) { + throw new Error( + `Timeout (${this.startOptions.startTimeout}) waiting for text (${this.startOptions.startText})`, + ); + } + const events = await this.getEvents(); + if (!reviewPendingFound && events.some((event: IEvent) => reviewPendingRegex.test(event.text))) { + const nav = + this.startOptions.model === "stax" + ? new TouchNavigation([ButtonKind.ConfirmYesButton]) + : new ClickNavigation([0]); + await this.navigate("", "", nav.schedule, true, false, 0, true); + reviewPendingFound = true; + } + found = events.some((event: IEvent) => startRegex.test(event.text)); + await Zemu.sleep(); + } this.log(`Get initial snapshot and events`); this.mainMenuSnapshot = await this.snapshot(); diff --git a/src/constants.ts b/src/constants.ts index 2c1f6ac..c071cb3 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -20,6 +20,7 @@ export const DEFAULT_EMU_IMG = "zondax/builder-zemu:speculos-e262a0ca9d2b37810d0 export const DEFAULT_MODEL = "nanos"; export const DEFAULT_NANO_START_TEXT = "Ready"; export const DEFAULT_STAX_START_TEXT = "This application enables"; +export const DEFAULT_PENDING_REVIEW_TEXT = "Ledger review" export const DEFAULT_START_DELAY = 20000; export const DEFAULT_KEY_DELAY = 250; export const DEFAULT_HOST = "127.0.0.1";