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
pub(crate) mod command;
pub(crate) mod types;
pub use command::get_address::*;
pub use command::get_app_configuration::*;
pub use command::provide_erc20_token_info::*;
pub use command::sign_transaction::*;
use ledger_transport::{APDUCommand, APDUErrorCode, Exchange};
use ledger_zondax_generic::{App, LedgerAppError};
pub use types::*;
#[derive(Debug)]
pub struct EthApp<E: Exchange> {
transport: E,
}
impl<E: Exchange> App for EthApp<E> {
const CLA: u8 = 0xe0;
}
impl<E: Exchange> EthApp<E> {
pub const fn new(transport: E) -> Self {
EthApp { transport }
}
}
impl<E> EthApp<E>
where
E: Exchange + Send + Sync,
E::Error: std::error::Error,
{
pub async fn send_chunks(
&self,
mut command: APDUCommand<Vec<u8>>,
) -> Result<ledger_transport::APDUAnswer<E::AnswerType>, LedgerAppError<E::Error>> {
let chunks = command
.data
.chunks(250)
.map(|c| c.to_vec())
.collect::<Vec<Vec<u8>>>();
match chunks.len() {
0 => return Err(LedgerAppError::InvalidEmptyMessage),
n if n > 255 => return Err(LedgerAppError::InvalidMessageSize),
_ => (),
}
let (first, rest) = chunks.split_first().unwrap();
command.data = first.to_owned();
if command.p1 != ChunkPayloadType::First as u8 {
return Err(LedgerAppError::InvalidChunkPayloadType);
}
let mut response = self.transport.exchange(&command).await?;
match response.error_code() {
Ok(APDUErrorCode::NoError) => {}
Ok(err) => return Err(LedgerAppError::AppSpecific(err as _, err.description())),
Err(err) => return Err(LedgerAppError::Unknown(err as _)),
}
let p1 = ChunkPayloadType::Subsequent as u8;
for chunk in rest {
dbg!(&chunk);
let command = APDUCommand {
cla: command.cla,
ins: command.ins,
p1,
p2: 0,
data: chunk.to_vec(),
};
response = self.transport.exchange(&command).await?;
match response.error_code() {
Ok(APDUErrorCode::NoError) => {}
Ok(err) => return Err(LedgerAppError::AppSpecific(err as _, err.description())),
Err(err) => return Err(LedgerAppError::Unknown(err as _)),
}
}
Ok(response)
}
}