Skip to content

Commit

Permalink
Refactor APDU messages in client
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspider committed Oct 11, 2024
1 parent 7ec4395 commit f3ba9d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
32 changes: 32 additions & 0 deletions client-sdk/src/apdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,35 @@ pub fn apdu_continue(data: Vec<u8>) -> APDUCommand {
data,
}
}

pub fn apdu_continue_with_p1(data: Vec<u8>, p1: u8) -> APDUCommand {
APDUCommand {
cla: 0xE0,
ins: 0xff,
p1,
p2: 0,
data,
}
}

pub fn apdu_register_vapp(serialized_manifest: Vec<u8>) -> APDUCommand {
APDUCommand {
cla: 0xE0,
ins: 2,
p1: 0,
p2: 0,
data: serialized_manifest,
}
}

pub fn apdu_run_vapp(serialized_manifest: Vec<u8>, app_hmac: [u8; 32]) -> APDUCommand {
let mut data = serialized_manifest;
data.extend_from_slice(&app_hmac);
APDUCommand {
cla: 0xE0,
ins: 3,
p1: 0,
p2: 0,
data,
}
}
40 changes: 11 additions & 29 deletions client-sdk/src/vanadium_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use common::constants::{page_start, PAGE_SIZE};
use common::manifest::Manifest;
use sha2::{Digest, Sha256};

use crate::apdu::{apdu_continue, APDUCommand, StatusWord};
use crate::apdu::{
apdu_continue, apdu_continue_with_p1, apdu_register_vapp, apdu_run_vapp, APDUCommand,
StatusWord,
};
use crate::elf::ElfFile;
use crate::transport::Transport;

Expand Down Expand Up @@ -137,22 +140,12 @@ struct VAppEngine<E: std::fmt::Debug + Send + Sync + 'static> {

impl<E: std::fmt::Debug + Send + Sync + 'static> VAppEngine<E> {
pub async fn run(mut self, app_hmac: [u8; 32]) -> Result<(), &'static str> {
let mut data =
let serialized_manifest =
postcard::to_allocvec(&self.manifest).map_err(|_| "manifest serialization failed")?;

data.extend_from_slice(&app_hmac);

let command = APDUCommand {
cla: 0xE0,
ins: 3,
p1: 0,
p2: 0,
data,
};

let (status, result) = self
.transport
.exchange(&command)
.exchange(&apdu_run_vapp(serialized_manifest, app_hmac))
.await
.map_err(|_| "exchange failed")?;

Expand Down Expand Up @@ -206,16 +199,10 @@ impl<E: std::fmt::Debug + Send + Sync + 'static> VAppEngine<E> {
let (mut data, _) = segment.get_page(page_index)?;
let p1 = data.pop().unwrap();

// return the content of the page
// return the content of the page (the last byte is in p1)
Ok(self
.transport
.exchange(&APDUCommand {
cla: 0xE0,
ins: 0xff,
p1,
p2: 0,
data,
})
.exchange(&apdu_continue_with_p1(data, p1))
.await
.map_err(|_| "exchange failed")?)
}
Expand Down Expand Up @@ -513,16 +500,11 @@ impl GenericVanadiumClient {
transport: Arc<dyn Transport<Error = E>>,
manifest: &Manifest,
) -> Result<[u8; 32], &'static str> {
let command = APDUCommand {
cla: 0xE0,
ins: 2,
p1: 0,
p2: 0,
data: postcard::to_allocvec(manifest).map_err(|_| "manifest serialization failed")?,
};
let serialized_manifest =
postcard::to_allocvec(manifest).map_err(|_| "manifest serialization failed")?;

let (status, result) = transport
.exchange(&command)
.exchange(&apdu_register_vapp(serialized_manifest))
.await
.map_err(|_| "exchange failed")?;

Expand Down

0 comments on commit f3ba9d9

Please sign in to comment.