Skip to content

Commit 143ee54

Browse files
authored
feat: hash encoding for Ledger app v2.0.0+ (#696)
1 parent 171b0c6 commit 143ee54

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

starknet-signers/src/ledger.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,21 @@ impl LedgerStarknetApp {
238238
.await?,
239239
)?;
240240

241+
// The Ledger app prior to version 2.0.0 expects the input to be left shifted by 4 bits:
242+
let app_version = self.get_version().await?;
243+
let adjusted_bytes: [u8; 32] = if app_version < Version::new(2, 0, 0) {
244+
(U256::from_be_slice(&hash.to_bytes_be()) << 4)
245+
.to_be_byte_array()
246+
.into()
247+
} else {
248+
hash.to_bytes_be()
249+
};
250+
241251
let response = self
242252
.transport
243253
.exchange(
244254
&SignHashCommand2 {
245-
hash: hash.to_bytes_be(),
255+
hash: adjusted_bytes,
246256
}
247257
.into(),
248258
)
@@ -326,17 +336,12 @@ impl From<SignHashCommand1> for APDUCommand {
326336

327337
impl From<SignHashCommand2> for APDUCommand {
328338
fn from(value: SignHashCommand2) -> Self {
329-
// For some reasons, the Ledger app expects the input to be left shifted by 4 bits...
330-
let shifted_bytes: [u8; 32] = (U256::from_be_slice(&value.hash) << 4)
331-
.to_be_byte_array()
332-
.into();
333-
334339
Self {
335340
cla: CLA_STARKNET,
336341
ins: 0x02,
337342
p1: 0x01,
338343
p2: 0x00,
339-
data: APDUData::new(&shifted_bytes),
344+
data: APDUData::new(&value.hash),
340345
response_len: None,
341346
}
342347
}

0 commit comments

Comments
 (0)