Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions wallet/src/signer/ledger_signer/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use crypto::key::{
use logging::log;
use randomness::make_true_rng;
use serialization::hex::HexEncode;
use speculos::{Button, ButtonAction, Device, Handle, TouchScreenElement};
use speculos::{Device, Handle};
use test_utils::random::{make_seedable_rng, Seed};
use utils::env_utils::{bool_from_env, get_from_env};
use wallet_storage::WalletStorageReadLocked;
Expand Down Expand Up @@ -95,29 +95,16 @@ async fn auto_confirmer(mut control_msg_rx: mpsc::Receiver<ControlMessage>, hand
loop {
tokio::select! {
_ = sleep(Duration::from_millis(500)) => {
// Logic depends on whether we are using a touch screen or buttons
if handle.device().is_touch() {
// TOUCH DEVICE STRATEGY
// On Speculos, blindly tapping coordinates is safe.
// 1. Try to go to the next page (Tap the "Next/Tap" zone)
// 2. Try to confirm (Tap the "Confirm" zone)

// Attempt to advance review
let _ = handle.tap(TouchScreenElement::ReviewTap).await;
sleep(Duration::from_millis(100)).await;

// Attempt to confirm review
let _ = handle.hold(TouchScreenElement::ReviewConfirm).await;
sleep(Duration::from_millis(100)).await;
} else {
// BUTTON DEVICE STRATEGY (Nano S/S+/X)
// 1. Press Right to scroll
// 2. Press Both to confirm
let _ = handle.button(Button::Right, ButtonAction::PressAndRelease).await;
sleep(Duration::from_millis(100)).await;
let _ = handle.button(Button::Both, ButtonAction::PressAndRelease).await;
sleep(Duration::from_millis(100)).await;
let _ = handle.button(Button::Both, ButtonAction::PressAndRelease).await;
if let Ok(events) = handle.current_screen_events().await {
let to_sign = events.iter().any(|e|
e.contains("Sign transaction") || e.contains("Sign message")
);

if to_sign {
let _ = handle.confirm().await;
} else {
let _ = handle.navigate_next().await;
}
}
}
msg = control_msg_rx.recv() => {
Expand Down
65 changes: 59 additions & 6 deletions wallet/src/signer/ledger_signer/tests/speculos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,22 @@ impl TouchScreenElement {
}
}

// -----------------------------------------------------------------
// Screen Events
// -----------------------------------------------------------------

/// Response format for the /events endpoint
#[derive(Debug, Deserialize)]
struct EventsResponse {
events: Vec<Event>,
}

/// A single event inside the events array
#[derive(Debug, Deserialize)]
struct Event {
text: String,
}

// -----------------------------------------------------------------
// RUNTIME HANDLE
// -----------------------------------------------------------------
Expand Down Expand Up @@ -154,14 +170,14 @@ impl Handle {

log::debug!("Sending button request: {}:{}", button, action);

let r = Client::new()
let response = Client::new()
.post(format!("http://{}/button/{}", self.addr(), button))
.json(&ButtonPayload { action })
.send()
.await?;

if !r.status().is_success() {
anyhow::bail!("Button request failed: {}", r.status());
if !response.status().is_success() {
anyhow::bail!("Button request failed: {}", response.status());
}

Ok(())
Expand All @@ -178,14 +194,14 @@ impl Handle {
delay: None,
};

let r = Client::new()
let response = Client::new()
.post(format!("http://{}/finger", self.addr()))
.json(&payload)
.send()
.await?;

if !r.status().is_success() {
anyhow::bail!("Finger request failed: {}", r.status());
if !response.status().is_success() {
anyhow::bail!("Finger request failed: {}", response.status());
}

Ok(())
Expand All @@ -204,6 +220,43 @@ impl Handle {
self.finger(x, y, ButtonAction::PressAndRelease).await?;
Ok(())
}

pub async fn navigate_next(&self) -> anyhow::Result<()> {
if self.device().is_touch() {
self.tap(TouchScreenElement::ReviewTap).await
} else {
self.button(Button::Right, ButtonAction::PressAndRelease).await
}
}

pub async fn confirm(&self) -> anyhow::Result<()> {
if self.device().is_touch() {
self.hold(TouchScreenElement::ReviewConfirm).await
} else {
self.button(Button::Both, ButtonAction::PressAndRelease).await
}
}

/// Fetch the current screen events from the emulator and extract their text
pub async fn current_screen_events(&self) -> anyhow::Result<Vec<String>> {
log::debug!("Fetching current screen events");

let response = Client::new()
.get(format!("http://{}/events", self.addr()))
.query(&[("currentscreenonly", "true")])
.send()
.await?;

if !response.status().is_success() {
anyhow::bail!("Events request failed: {}", response.status());
}

let parsed_response: EventsResponse = response.json().await?;

let texts = parsed_response.events.into_iter().map(|event| event.text).collect();

Ok(texts)
}
}

#[cfg(test)]
Expand Down