Skip to content

Commit

Permalink
fix: wait and skip Ledger Pending review if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosala committed Jan 25, 2024
1 parent ac8bea5 commit 5b0177f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/Zemu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 5b0177f

Please sign in to comment.