-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
189 lines (168 loc) · 5.94 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use clap::Parser;
use client::TestClient;
use hidapi::HidApi;
use ledger_transport_hid::TransportNativeHID;
use sdk::transport::{Transport, TransportHID, TransportTcp, TransportWrapper};
use sdk::vanadium_client::{NativeAppClient, VanadiumAppClient};
mod commands;
mod client;
use std::io::BufRead;
use std::sync::Arc;
#[derive(Parser)]
#[command(name = "Vanadium", about = "Run a V-App on Vanadium")]
struct Args {
/// Path to the ELF file of the V-App (if not the default one)
app: Option<String>,
/// Use the HID interface for a real device, instead of Speculos
#[arg(long, group = "interface")]
hid: bool,
/// Use the native interface
#[arg(long, group = "interface")]
native: bool,
}
enum CliCommand {
Reverse(Vec<u8>),
AddNumbers(u32),
Sha256(Vec<u8>),
B58Enc(Vec<u8>),
NPrimes(u32),
Panic(String),
Exit,
}
/// Parses a hex-encoded string into a vector of bytes.
fn parse_hex_buffer(s: &str) -> Result<Vec<u8>, String> {
if s.len() % 2 != 0 {
return Err("Hex string has an odd length".to_string());
}
(0..s.len())
.step_by(2)
.map(|i| {
u8::from_str_radix(&s[i..i + 2], 16)
.map_err(|_| format!("Invalid hex character at position {}", i))
})
.collect()
}
/// Parses a string into a u32 integer.
fn parse_u32(s: &str) -> Result<u32, String> {
s.parse::<u32>()
.map_err(|_| "Invalid u32 integer".to_string())
}
fn parse_command(line: &str) -> Result<CliCommand, String> {
let mut tokens = line.split_whitespace();
if let Some(command) = tokens.next() {
match command {
"reverse" | "sha256" | "b58enc" => {
let arg = tokens.next().unwrap_or("");
let buffer = parse_hex_buffer(arg).map_err(|e| e.to_string())?;
match command {
"reverse" => Ok(CliCommand::Reverse(buffer)),
"sha256" => Ok(CliCommand::Sha256(buffer)),
"b58enc" => Ok(CliCommand::B58Enc(buffer)),
_ => unreachable!(),
}
}
"addnumbers" | "nprimes" => {
let arg = tokens
.next()
.ok_or_else(|| format!("'{}' requires a u32 integer argument", command))?;
let number = parse_u32(arg).map_err(|e| e.to_string())?;
match command {
"addnumbers" => Ok(CliCommand::AddNumbers(number)),
"nprimes" => Ok(CliCommand::NPrimes(number)),
_ => unreachable!(),
}
}
"panic" => {
// find where the word "panic" ends and the message starts
let msg = line
.find("panic")
.map(|i| line[i + 5..].trim())
.unwrap_or("");
Ok(CliCommand::Panic(msg.to_string()))
}
_ => Err(format!("Unknown command: '{}'", command)),
}
} else {
Ok(CliCommand::Exit)
}
}
#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let default_app_path = if args.native {
"../app/target/x86_64-unknown-linux-gnu/release/vnd-test"
} else {
"../app/target/riscv32i-unknown-none-elf/release/vnd-test"
};
let app_path_str = args.app.unwrap_or(default_app_path.to_string());
let mut test_client = if args.native {
TestClient::new(Box::new(
NativeAppClient::new(&app_path_str)
.await
.map_err(|_| "Failed to create client")?,
))
} else {
let transport_raw: Arc<
dyn Transport<Error = Box<dyn std::error::Error + Send + Sync>> + 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.clone());
let (client, _) = VanadiumAppClient::new(&app_path_str, Arc::new(transport), None)
.await
.map_err(|_| "Failed to create client")?;
TestClient::new(Box::new(client))
};
loop {
println!("Enter a command:");
let mut line = String::new();
std::io::stdin()
.lock()
.read_line(&mut line)
.expect("Failed to read line");
match parse_command(&line) {
Ok(cmd) => match cmd {
CliCommand::Reverse(arg) => {
println!("{}", hex::encode(test_client.reverse(&arg).await?));
}
CliCommand::AddNumbers(number) => {
println!("{}", test_client.add_numbers(number).await?);
}
CliCommand::Sha256(arg) => {
println!("{}", hex::encode(test_client.sha256(&arg).await?));
}
CliCommand::B58Enc(arg) => {
println!("{}", hex::encode(test_client.b58enc(&arg).await?));
}
CliCommand::NPrimes(n) => {
println!("{}", test_client.nprimes(n).await?);
}
CliCommand::Panic(msg) => {
test_client.panic(&msg).await?;
}
CliCommand::Exit => {
let status = test_client.exit().await?;
if status != 0 {
std::process::exit(status);
}
break;
}
},
Err(e) => {
println!("Error: {}", e);
}
}
}
Ok(())
}