Skip to content

Commit

Permalink
Add option to run the V-App on a real device in the host program
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspider committed Sep 13, 2024
1 parent 024580b commit 1877404
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
2 changes: 2 additions & 0 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
59 changes: 42 additions & 17 deletions host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>> {
// Get the path to the ELF file from command line arguments
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
eprintln!("Usage: {} <path to elf file>", 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<dyn Transport<Error = Box<dyn std::error::Error>> + 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,
Expand All @@ -40,12 +64,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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?;
Expand Down

0 comments on commit 1877404

Please sign in to comment.