Skip to content

Commit

Permalink
Merge pull request #3 from yogh333/feat/apdu_stream
Browse files Browse the repository at this point in the history
read apdu data from file
  • Loading branch information
ryankurte committed Jul 16, 2023
2 parents 38ea55f + 641f157 commit 70e705f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tokio = { version = "1.27.0", features = ["full"] }
time = { version = "0.3.21", features = [ "macros" ] }
humantime = "2.1.0"
hex = "0.4.3"
serde_json = "1.0.100"

ledger-lib = { version = "0.1.0", features = [ "clap" ] }
ledger-proto = { version = "0.1.0" }
32 changes: 31 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ pub enum Command {
#[clap(default_value = "")]
data: ApduData,
},
/// Exchange raw data with the device
File {
#[clap(help = "file to read APDU data from (header + data)")]
filename: Option<String>,
},
}

#[derive(Clone, Debug, Default, PartialEq)]
Expand Down Expand Up @@ -156,8 +161,33 @@ async fn main() -> anyhow::Result<()> {

println!("Response: {}", resp.data.encode_hex::<String>());
}
Command::File { filename } => match filename {
Some(path) => {
let data = std::fs::read_to_string(path)?;
let mut d = connect(&mut p, &devices, args.index).await?;
let mut buff = [0u8; 256];

let apdu_seq: Vec<GenericApdu> = serde_json::from_str(data.as_str()).unwrap();

for apdu_input in apdu_seq {
let resp = d
.request::<GenericApdu>(apdu_input, &mut buff, args.timeout.into())
.await;

match resp {
Ok(apdu_output) => {
println!("Response: {}", apdu_output.data.encode_hex::<String>())
}
Err(Error::Response(0x90, 0x00)) => println!("App OK"),
Err(e) => println!("Command failed: {e:?}"),
}
}
}
None => {
error!("please provide an input file");
}
},
}

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ default = [ "std" ]
encdec = { version = "0.9.0", default_features = false }
bitflags = { version = "2.1.0", default_features = false }
displaydoc = { version = "0.2.3", default_features = false }
serde = { version = "1.0.166", features = ["derive"] }
hex = { version = "0.4.3", features = ["serde"] }

thiserror = { version = "1.0.40", optional = true }
7 changes: 5 additions & 2 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ pub use encdec::{Decode, DecodeOwned, EncDec, Encode};
mod error;
pub use error::ApduError;

pub use serde::Deserialize;

pub mod apdus;

/// APDU command header
#[derive(Copy, Clone, PartialEq, Debug, Default, Encode, DecodeOwned)]
#[derive(Copy, Clone, PartialEq, Debug, Default, Encode, DecodeOwned, Deserialize)]
#[encdec(error = "ApduError")]
pub struct ApduHeader {
/// Class ID
Expand Down Expand Up @@ -191,12 +193,13 @@ pub trait ApduBase<'a>: EncDec<'a, ApduError> {}
impl<'a, T: EncDec<'a, ApduError>> ApduBase<'a> for T {}

/// Generic APDU object (enabled with `alloc` feature), prefer use of strict APDU types where possible
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[cfg(feature = "alloc")]
pub struct GenericApdu {
/// Request APDU Header (uses [Default] for incoming / response APDUs)
pub header: ApduHeader,
/// APDU data
#[serde(with = "hex::serde")]
pub data: Vec<u8>,
}

Expand Down

0 comments on commit 70e705f

Please sign in to comment.