Skip to content

Commit 2b9effe

Browse files
committed
Reject --hd-path overrides on Ledger aliases.
1 parent 7965f52 commit 2b9effe

20 files changed

Lines changed: 74 additions & 91 deletions

File tree

cmd/soroban-cli/src/commands/keys/add.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ pub enum Error {
4545

4646
#[error("--hd-path is not valid with a secret key; secret keys cannot be derived")]
4747
HdPathNotSupportedForSecretKey,
48-
49-
#[error("--hd-path {0} is out of range for a Ledger account index")]
50-
HdPathOutOfRange(usize),
5148
}
5249

5350
#[derive(Debug, clap::Parser, Clone)]
@@ -94,7 +91,7 @@ pub struct Cmd {
9491
/// without re-passing the flag. Not valid with `--public-key` or a raw
9592
/// secret key.
9693
#[arg(long)]
97-
pub hd_path: Option<usize>,
94+
pub hd_path: Option<u32>,
9895
}
9996

10097
impl Cmd {
@@ -125,9 +122,10 @@ impl Cmd {
125122
}
126123

127124
async fn derive_ledger_secret(&self) -> Result<Secret, Error> {
128-
let raw = self.hd_path.unwrap_or(0);
129-
let index: u32 = raw.try_into().map_err(|_| Error::HdPathOutOfRange(raw))?;
130-
let public_key = ledger::new(index).await?.public_key().await?;
125+
let public_key = ledger::new(self.hd_path.unwrap_or_default())
126+
.await?
127+
.public_key()
128+
.await?;
131129
Ok(Secret::Ledger {
132130
hardware: HardwareKind::Ledger,
133131
public_key: public_key.to_string(),
@@ -181,7 +179,7 @@ impl Cmd {
181179
}
182180
}
183181

184-
fn build_secret(input: &str, hd_path: Option<usize>) -> Result<Secret, Error> {
182+
fn build_secret(input: &str, hd_path: Option<u32>) -> Result<Secret, Error> {
185183
let secret: Secret = input.parse()?;
186184
match (secret, hd_path) {
187185
(Secret::SecretKey { .. }, Some(_)) => Err(Error::HdPathNotSupportedForSecretKey),
@@ -248,7 +246,7 @@ mod tests {
248246

249247
fn cmd_with_public_key(
250248
public_key: &str,
251-
hd_path: Option<usize>,
249+
hd_path: Option<u32>,
252250
) -> (tempfile::TempDir, locator::Args, Cmd) {
253251
let (temp_dir, locator, mut cmd) = set_up_test();
254252
cmd.public_key = Some(public_key.to_string());

cmd/soroban-cli/src/commands/keys/generate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub struct Cmd {
5555
/// `--secure-store` or plain seed-phrase storage it is persisted on the identity
5656
/// so later commands derive the same account without re-passing the flag.
5757
#[arg(long)]
58-
pub hd_path: Option<usize>,
58+
pub hd_path: Option<u32>,
5959

6060
#[command(flatten)]
6161
pub network: network::Args,

cmd/soroban-cli/src/commands/keys/public_key.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ pub enum Error {
1111

1212
#[error(transparent)]
1313
Ledger(#[from] ledger::Error),
14-
15-
#[error("--hd-path {0} is out of range for a Ledger account index")]
16-
HdPathOutOfRange(usize),
1714
}
1815

1916
#[derive(Debug, clap::Parser, Clone)]
@@ -26,7 +23,7 @@ pub struct Cmd {
2623
/// If identity is a seed phrase use this hd path, default is 0.
2724
/// With --ledger this is the Ledger account index (default 0).
2825
#[arg(long)]
29-
pub hd_path: Option<usize>,
26+
pub hd_path: Option<u32>,
3027

3128
/// Derive the address from a connected Ledger hardware wallet at
3229
/// `m/44'/148'/N'`, where `N` defaults to 0 and can be set with
@@ -46,9 +43,10 @@ impl Cmd {
4643

4744
pub async fn public_key(&self) -> Result<stellar_strkey::ed25519::PublicKey, Error> {
4845
if self.ledger {
49-
let raw = self.hd_path.unwrap_or(0);
50-
let index: u32 = raw.try_into().map_err(|_| Error::HdPathOutOfRange(raw))?;
51-
return Ok(ledger::new(index).await?.public_key().await?);
46+
return Ok(ledger::new(self.hd_path.unwrap_or_default())
47+
.await?
48+
.public_key()
49+
.await?);
5250
}
5351
let name = self
5452
.name

cmd/soroban-cli/src/commands/keys/secret.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct Cmd {
2929

3030
/// If identity is a seed phrase use this hd path, default is 0
3131
#[arg(long, conflicts_with = "phrase")]
32-
pub hd_path: Option<usize>,
32+
pub hd_path: Option<u32>,
3333

3434
#[command(flatten)]
3535
pub locator: locator::Args,

cmd/soroban-cli/src/commands/ledger/entry/fetch/account.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct Cmd {
2020

2121
/// If identity is a seed phrase use this hd path, default is 0
2222
#[arg(long)]
23-
pub hd_path: Option<usize>,
23+
pub hd_path: Option<u32>,
2424
}
2525

2626
#[derive(thiserror::Error, Debug)]

cmd/soroban-cli/src/commands/ledger/entry/fetch/account_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct Cmd {
2424

2525
/// If identity is a seed phrase use this hd path, default is 0
2626
#[arg(long)]
27-
pub hd_path: Option<usize>,
27+
pub hd_path: Option<u32>,
2828
}
2929

3030
#[derive(thiserror::Error, Debug)]

cmd/soroban-cli/src/commands/ledger/entry/fetch/offer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct Cmd {
2424

2525
/// If identity is a seed phrase use this hd path, default is 0
2626
#[arg(long)]
27-
pub hd_path: Option<usize>,
27+
pub hd_path: Option<u32>,
2828
}
2929

3030
#[derive(thiserror::Error, Debug)]

cmd/soroban-cli/src/commands/ledger/entry/fetch/trustline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Cmd {
2727

2828
/// If account is a seed phrase use this hd path, default is 0
2929
#[arg(long)]
30-
pub hd_path: Option<usize>,
30+
pub hd_path: Option<u32>,
3131
}
3232

3333
#[derive(thiserror::Error, Debug)]

cmd/soroban-cli/src/commands/message/sign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub struct Cmd {
6262

6363
#[arg(long)]
6464
/// If using a seed phrase to sign, sets which hierarchical deterministic path to use, e.g. `m/44'/148'/{hd_path}`. Example: `--hd-path 1`. Default: `0`
65-
pub hd_path: Option<usize>,
65+
pub hd_path: Option<u32>,
6666

6767
#[command(flatten)]
6868
pub locator: locator::Args,

cmd/soroban-cli/src/commands/message/verify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub struct Cmd {
6868

6969
/// If public key identity is a seed phrase use this hd path, default is 0
7070
#[arg(long)]
71-
pub hd_path: Option<usize>,
71+
pub hd_path: Option<u32>,
7272

7373
#[command(flatten)]
7474
pub locator: locator::Args,

0 commit comments

Comments
 (0)