Skip to content

Commit db1fa59

Browse files
authored
feat: get ledger app version (#623)
1 parent 50eed82 commit db1fa59

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

starknet-signers/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ thiserror = "1.0.40"
2121
crypto-bigint = { version = "0.5.1", default-features = false }
2222
rand = { version = "0.8.5", features = ["std_rng"] }
2323
coins-bip32 = { version = "0.11.1", optional = true }
24+
semver = { version = "1.0.23", optional = true }
2425

2526
# Using a fork until https://github.com/summa-tx/coins/issues/137 is fixed
2627
coins-ledger = { git = "https://github.com/xJonathanLEI/coins", rev = "0e3be5db0b18b683433de6b666556b99c726e785", default-features = false, optional = true }
@@ -37,4 +38,4 @@ wasm-bindgen-test = "0.3.34"
3738
[features]
3839
default = []
3940

40-
ledger = ["coins-bip32", "coins-ledger"]
41+
ledger = ["coins-bip32", "coins-ledger", "semver"]

starknet-signers/src/ledger.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use coins_ledger::{
55
APDUAnswer, APDUCommand, Ledger,
66
};
77
use crypto_bigint::{ArrayEncoding, U256};
8+
use semver::Version;
89
use starknet_core::{crypto::Signature, types::Felt};
910

1011
use crate::{Signer, VerifyingKey};
@@ -19,6 +20,7 @@ const EIP_2645_PURPOSE: u32 = 0x80000a55;
1920

2021
const EIP_2645_PATH_LENGTH: usize = 6;
2122

23+
const VERSION_SIZE: usize = 3;
2224
const PUBLIC_KEY_SIZE: usize = 65;
2325
const SIGNATURE_SIZE: usize = 65;
2426

@@ -49,6 +51,9 @@ pub enum LedgerError {
4951
UnexpectedResponseLength { expected: usize, actual: usize },
5052
}
5153

54+
/// The `GetPubKey` Ledger command.
55+
struct GetVersion;
56+
5257
/// The `GetPubKey` Ledger command.
5358
struct GetPubKeyCommand {
5459
display: bool,
@@ -126,6 +131,21 @@ impl LedgerStarknetApp {
126131
Ok(Self { transport })
127132
}
128133

134+
/// Gets the Ledger app version.
135+
pub async fn get_version(&self) -> Result<Version, LedgerError> {
136+
let response = self.transport.exchange(&GetVersion.into()).await?;
137+
138+
let data = get_apdu_data(&response)?;
139+
if data.len() != VERSION_SIZE {
140+
return Err(LedgerError::UnexpectedResponseLength {
141+
expected: VERSION_SIZE,
142+
actual: data.len(),
143+
});
144+
}
145+
146+
Ok(Version::new(data[0] as u64, data[1] as u64, data[2] as u64))
147+
}
148+
129149
/// Gets a public key from the app for a particular derivation path, with optional on-device
130150
/// confirmation for extra security.
131151
///
@@ -242,6 +262,19 @@ impl From<coins_ledger::LedgerError> for LedgerError {
242262
}
243263
}
244264

265+
impl From<GetVersion> for APDUCommand {
266+
fn from(_value: GetVersion) -> Self {
267+
Self {
268+
cla: CLA_STARKNET,
269+
ins: 0x00,
270+
p1: 0x00,
271+
p2: 0x00,
272+
data: APDUData::new(&[]),
273+
response_len: None,
274+
}
275+
}
276+
}
277+
245278
impl From<GetPubKeyCommand> for APDUCommand {
246279
fn from(value: GetPubKeyCommand) -> Self {
247280
let path = value

0 commit comments

Comments
 (0)