Skip to content

Commit c2621b1

Browse files
committed
Reset the state after processing the transaction
1 parent 6c74930 commit c2621b1

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

app/src/handler.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ pub fn handle_apdu(
107107
}
108108
}
109109
Ins::SignTx => {
110-
let data = comm.get_data()?;
110+
let data = match comm.get_data() {
111+
Ok(data) => data,
112+
Err(code) => {
113+
reset(sign_tx_context, tx_reviewer);
114+
return Err(code.into());
115+
}
116+
};
111117
match handle_sign_tx(apdu_header, data, sign_tx_context, tx_reviewer) {
112118
Ok(()) if !sign_tx_context.is_complete() => {
113119
return Ok(());
@@ -125,9 +131,11 @@ pub fn handle_apdu(
125131
}
126132
Err(code) => Err(code.into()),
127133
};
134+
reset(sign_tx_context, tx_reviewer);
128135
return result;
129136
}
130137
Err(code) => {
138+
reset(sign_tx_context, tx_reviewer);
131139
return Err(code.into());
132140
}
133141
}
@@ -158,11 +166,6 @@ fn handle_sign_tx(
158166
let token_size = data[0]; // the first byte is the token size
159167
check_token_size(token_size)?;
160168
tx_reviewer.init(token_size)?;
161-
162-
// we check whether the tx is completed after processing each data frame
163-
// so we need to call `reset` to reset the status of the previous tx
164-
// we will initialize the `sign_tx_context` upon receiving the first tx data frame
165-
sign_tx_context.reset();
166169
if token_size == 0 {
167170
return Ok(());
168171
}
@@ -198,3 +201,9 @@ fn check_token_size(size: u8) -> Result<(), ErrorCode> {
198201
Ok(())
199202
}
200203
}
204+
205+
#[inline]
206+
fn reset(sign_tx_context: &mut SignTxContext, tx_reviewer: &mut TxReviewer) {
207+
sign_tx_context.reset();
208+
tx_reviewer.reset();
209+
}

app/src/sign_tx_context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ impl SignTxContext {
6161
}
6262

6363
pub fn reset(&mut self) {
64+
self.path = [0; PATH_LENGTH];
65+
self.tx_decoder.reset();
6466
self.current_step = DecodeStep::Init;
67+
self.hasher.reset();
68+
self.temp_data.reset(0);
69+
self.device_address = None;
6570
}
6671

6772
pub fn is_complete(&self) -> bool {

app/src/ui/tx_reviewer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ impl TxReviewer {
7979
Ok(())
8080
}
8181

82+
pub fn reset(&mut self) {
83+
self.reset_buffer(0);
84+
self.has_external_inputs = false;
85+
self.next_output_index = FIRST_OUTPUT_INDEX;
86+
self.tx_fee = None;
87+
self.is_tx_execute_script = false;
88+
self.token_metadata_length = 0;
89+
self.token_verifier = None;
90+
}
91+
8292
pub fn handle_token_metadata(&mut self, data: &[u8]) -> Result<(), ErrorCode> {
8393
assert!(self.token_verifier.is_none());
8494
let token_verifier = TokenVerifier::new(data)?;

0 commit comments

Comments
 (0)