Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: get ledger app version #623

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion starknet-signers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ thiserror = "1.0.40"
crypto-bigint = { version = "0.5.1", default-features = false }
rand = { version = "0.8.5", features = ["std_rng"] }
coins-bip32 = { version = "0.11.1", optional = true }
semver = { version = "1.0.23", optional = true }

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

ledger = ["coins-bip32", "coins-ledger"]
ledger = ["coins-bip32", "coins-ledger", "semver"]
33 changes: 33 additions & 0 deletions starknet-signers/src/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use coins_ledger::{
APDUAnswer, APDUCommand, Ledger,
};
use crypto_bigint::{ArrayEncoding, U256};
use semver::Version;
use starknet_core::{crypto::Signature, types::Felt};

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

const EIP_2645_PATH_LENGTH: usize = 6;

const VERSION_SIZE: usize = 3;
const PUBLIC_KEY_SIZE: usize = 65;
const SIGNATURE_SIZE: usize = 65;

Expand Down Expand Up @@ -49,6 +51,9 @@ pub enum LedgerError {
UnexpectedResponseLength { expected: usize, actual: usize },
}

/// The `GetPubKey` Ledger command.
struct GetVersion;

/// The `GetPubKey` Ledger command.
struct GetPubKeyCommand {
display: bool,
Expand Down Expand Up @@ -126,6 +131,21 @@ impl LedgerStarknetApp {
Ok(Self { transport })
}

/// Gets the Ledger app version.
pub async fn get_version(&self) -> Result<Version, LedgerError> {
let response = self.transport.exchange(&GetVersion.into()).await?;

let data = get_apdu_data(&response)?;
if data.len() != VERSION_SIZE {
return Err(LedgerError::UnexpectedResponseLength {
expected: VERSION_SIZE,
actual: data.len(),
});
}

Ok(Version::new(data[0] as u64, data[1] as u64, data[2] as u64))
}

/// Gets a public key from the app for a particular derivation path, with optional on-device
/// confirmation for extra security.
///
Expand Down Expand Up @@ -242,6 +262,19 @@ impl From<coins_ledger::LedgerError> for LedgerError {
}
}

impl From<GetVersion> for APDUCommand {
fn from(_value: GetVersion) -> Self {
Self {
cla: CLA_STARKNET,
ins: 0x00,
p1: 0x00,
p2: 0x00,
data: APDUData::new(&[]),
response_len: None,
}
}
}

impl From<GetPubKeyCommand> for APDUCommand {
fn from(value: GetPubKeyCommand) -> Self {
let path = value
Expand Down
Loading