Skip to content

Commit 988edbc

Browse files
Lbqdspolarker
authored andcommitted
2.8.2: remove magic numbers
1 parent 70818ec commit 988edbc

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

app/src/handler.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ use crate::{
1212
},
1313
};
1414

15+
const MAX_TOKEN_SIZE: u8 = 5;
16+
const PATH_LENGTH: usize = 20;
17+
const HASH_LENGTH: usize = 32;
18+
const PATH_HEX_LENGTH: usize = PATH_LENGTH * 2;
19+
const FIRST_FRAME_PREFIX_LENGTH: usize = PATH_LENGTH + 1; // path + 1 byte token size
20+
const CALL_CONTRACT_FLAG: u8 = 0x01;
21+
const SCRIPT_OFFSET: usize = 3; // the encoded script offset in the tx
22+
1523
#[repr(u8)]
1624
pub enum Ins {
1725
GetVersion,
@@ -58,23 +66,24 @@ pub fn handle_apdu(
5866
}
5967
Ins::GetPubKey => {
6068
let data = comm.get_data()?;
61-
if data.len() != 21 {
69+
// 1 byte flag indicating whether address verification is needed
70+
if data.len() != PATH_LENGTH + 1 {
6271
return Err(ErrorCode::BadLen.into());
6372
}
64-
let raw_path = &data[..20];
73+
let raw_path = &data[..PATH_LENGTH];
6574
deserialize_path::<io::Reply>(
6675
raw_path,
6776
&mut path,
6877
ErrorCode::HDPathDecodingFailed.into(),
6978
)?;
7079

7180
println("raw path");
72-
println_slice::<40>(raw_path);
81+
println_slice::<PATH_HEX_LENGTH>(raw_path);
7382
let p1 = apdu_header.p1;
7483
let p2 = apdu_header.p2;
7584
let (pk, hd_index) = derive_pub_key(&mut path, p1, p2)?;
7685

77-
let need_to_display = data[20] != 0;
86+
let need_to_display = data[PATH_LENGTH] != 0;
7887
if need_to_display {
7988
review_address(&pk)?;
8089
}
@@ -84,17 +93,17 @@ pub fn handle_apdu(
8493
}
8594
Ins::SignHash => {
8695
let data = comm.get_data()?;
87-
if data.len() != 4 * 5 + 32 {
96+
if data.len() != PATH_LENGTH + HASH_LENGTH {
8897
return Err(ErrorCode::BadLen.into());
8998
}
9099
// This check can be removed, but we keep it for double checking
91100
deserialize_path::<io::Reply>(
92-
&data[..20],
101+
&data[..PATH_LENGTH],
93102
&mut path,
94103
ErrorCode::HDPathDecodingFailed.into(),
95104
)?;
96105

97-
match sign_hash_ui(&path, &data[20..]) {
106+
match sign_hash_ui(&path, &data[PATH_LENGTH..]) {
98107
Ok((signature_buf, length, _)) => comm.append(&signature_buf[..length as usize]),
99108
Err(code) => return Err(code.into()),
100109
}
@@ -127,32 +136,31 @@ pub fn handle_apdu(
127136
Ok(())
128137
}
129138

130-
const MAX_TOKEN_SIZE: u8 = 5;
131-
132139
fn handle_sign_tx(
133140
apdu_header: &ApduHeader,
134141
data: &[u8],
135142
sign_tx_context: &mut SignTxContext,
136143
tx_reviewer: &mut TxReviewer,
137144
) -> Result<(), ErrorCode> {
138145
match apdu_header.p1 {
139-
0 if data.len() < 21 => Err(ErrorCode::BadLen), // 20 bytes path + 1 byte token size
146+
0 if data.len() < FIRST_FRAME_PREFIX_LENGTH => Err(ErrorCode::BadLen),
140147
0 => {
141-
sign_tx_context.init(data)?;
142-
let token_size = data[20];
148+
sign_tx_context.init(&data[..PATH_LENGTH])?;
149+
let token_size = data[FIRST_FRAME_PREFIX_LENGTH - 1];
143150
if token_size > MAX_TOKEN_SIZE {
144151
return Err(ErrorCode::InvalidTokenSize);
145152
}
146-
let tx_data_index: usize = 21 + TOKEN_METADATA_SIZE * (token_size as usize);
147-
if data.len() < tx_data_index + 3 {
153+
let tx_data_index: usize =
154+
FIRST_FRAME_PREFIX_LENGTH + TOKEN_METADATA_SIZE * (token_size as usize);
155+
if data.len() < tx_data_index + SCRIPT_OFFSET {
148156
return Err(ErrorCode::BadLen);
149157
}
150158
let tx_data = &data[tx_data_index..];
151-
let is_tx_execute_script = tx_data[2] == 0x01;
159+
let is_tx_execute_script = tx_data[SCRIPT_OFFSET - 1] == CALL_CONTRACT_FLAG;
152160
if is_tx_execute_script {
153161
check_blind_signing()?;
154162
}
155-
let token_metadata = &data[21..tx_data_index];
163+
let token_metadata = &data[FIRST_FRAME_PREFIX_LENGTH..tx_data_index];
156164
check_token_metadata(token_size, token_metadata)?;
157165
tx_reviewer.init(is_tx_execute_script, token_metadata)?;
158166
sign_tx_context.handle_data(apdu_header, tx_data, tx_reviewer)

app/src/sign_tx_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl SignTxContext {
4444
}
4545

4646
pub fn init(&mut self, data: &[u8]) -> Result<(), ErrorCode> {
47-
deserialize_path(&data[..20], &mut self.path, ErrorCode::HDPathDecodingFailed)?;
47+
deserialize_path(data, &mut self.path, ErrorCode::HDPathDecodingFailed)?;
4848

4949
self.tx_decoder.reset();
5050
self.current_step = DecodeStep::Init;

utils/src/types/bigint.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@ use crate::buffer::{Buffer, Writable};
22
use crate::decode::*;
33
use crate::types::compact_integer::*;
44

5+
const ENCODED_SIZE: usize = 33;
6+
57
#[cfg_attr(test, derive(Debug))]
68
#[derive(Clone)]
79
pub struct BigInt {
8-
pub bytes: [u8; 33],
10+
pub bytes: [u8; ENCODED_SIZE],
911
}
1012

1113
impl Reset for BigInt {
1214
fn reset(&mut self) {
13-
self.bytes = [0; 33];
15+
self.bytes = [0; ENCODED_SIZE];
1416
}
1517
}
1618

1719
impl Default for BigInt {
1820
fn default() -> Self {
19-
BigInt { bytes: [0; 33] }
21+
BigInt {
22+
bytes: [0; ENCODED_SIZE],
23+
}
2024
}
2125
}
2226

@@ -29,8 +33,8 @@ impl PartialEq for BigInt {
2933
impl BigInt {
3034
#[cfg(test)]
3135
pub fn from_bytes(bytes: &[u8]) -> Self {
32-
assert!(bytes.len() == 33);
33-
let mut bs = [0u8; 33];
36+
assert!(bytes.len() == ENCODED_SIZE);
37+
let mut bs = [0u8; ENCODED_SIZE];
3438
bs.copy_from_slice(bytes);
3539
Self { bytes: bs }
3640
}

0 commit comments

Comments
 (0)