From 18774040b1a04d4343cfa2ce500110f060e057b6 Mon Sep 17 00:00:00 2001 From: Salvatore Ingala <6681844+bigspider@users.noreply.github.com> Date: Fri, 13 Sep 2024 14:39:58 +0200 Subject: [PATCH] Add option to run the V-App on a real device in the host program --- host/Cargo.toml | 2 ++ host/src/main.rs | 59 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/host/Cargo.toml b/host/Cargo.toml index 528756d..77b54c9 100644 --- a/host/Cargo.toml +++ b/host/Cargo.toml @@ -5,9 +5,11 @@ edition = "2021" [dependencies] async-trait = "0.1.81" +clap = { version = "4.5.17", features = ["derive"] } common = { path = "../common" } goblin = "0.8.2" hex = "0.4.3" +hidapi = "2.6.3" ledger-apdu = "0.11.0" ledger-transport-hid = "0.11.0" postcard = { version = "1.0.8", features = ["alloc"] } diff --git a/host/src/main.rs b/host/src/main.rs index a7cdb95..f16fa6f 100644 --- a/host/src/main.rs +++ b/host/src/main.rs @@ -3,32 +3,56 @@ mod elf; mod transport; mod vanadium_client; +use clap::Parser; use common::manifest::Manifest; -use transport::{Transport, TransportTcp}; +use hidapi::HidApi; +use ledger_transport_hid::TransportNativeHID; +use transport::{Transport, TransportHID, TransportTcp, TransportWrapper}; -use std::env; use std::path::Path; +use std::sync::Arc; use vanadium_client::VanadiumClient; use elf::ElfFile; +#[derive(Parser)] +#[command(name = "Vanadium", about = "Run a V-App on Vanadium")] +struct Args { + /// Path to the ELF file of the V-App + #[arg(required = true)] + elf: String, + + /// Use the HID interface for a real device, instead of Speculos + #[arg(long)] + hid: bool, +} + #[tokio::main(flavor = "current_thread")] async fn main() -> Result<(), Box> { - // Get the path to the ELF file from command line arguments - let args: Vec = env::args().collect(); - if args.len() < 2 { - eprintln!("Usage: {} ", args[0]); - std::process::exit(1); - } - - let path = Path::new(&args[1]); + let args = Args::parse(); - let elf_file = ElfFile::new(path)?; + let elf_path = Path::new(&args.elf); + let elf_file = ElfFile::new(elf_path)?; - println!("Entrypoint: {:?}", elf_file.entrypoint); - println!("{:?}", elf_file); + // println!("Entrypoint: {:?}", elf_file.entrypoint); + // println!("{:?}", elf_file); - let transport = TransportTcp::new().await?; + let transport_raw: Arc> + Send + Sync> = + if args.hid { + Arc::new(TransportHID::new( + TransportNativeHID::new( + &HidApi::new().expect("Unable to get connect to the device"), + ) + .unwrap(), + )) + } else { + Arc::new( + TransportTcp::new() + .await + .expect("Unable to get TCP transport. Is speculos running?"), + ) + }; + let transport = TransportWrapper::new(transport_raw); let manifest = Manifest::new( 0, @@ -40,12 +64,13 @@ async fn main() -> Result<(), Box> { elf_file.code_segment.start, elf_file.code_segment.end, 0xd47a2000 - 65536, // TODO - 0xd47a2000, // TODO + 0xd47a2000, // TODO elf_file.data_segment.start, elf_file.data_segment.end, [0u8; 32], // TODO - 0 // TODO - ).unwrap(); + 0, // TODO + ) + .unwrap(); let client = VanadiumClient::new(transport); let app_hmac = client.register_vapp(&manifest).await?;