Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit c350543

Browse files
Make generate_remote_keypair more generic for potential other remote-wallets (#8274)
1 parent 5b4ecb0 commit c350543

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

keygen/src/keygen.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ use solana_clap_utils::{
1414
},
1515
};
1616
use solana_cli_config::config::{Config, CONFIG_FILE};
17-
use solana_remote_wallet::{
18-
ledger::{generate_remote_keypair, get_ledger_from_info},
19-
remote_wallet::RemoteWalletInfo,
20-
};
17+
use solana_remote_wallet::remote_keypair::generate_remote_keypair;
2118
use solana_sdk::{
2219
pubkey::write_pubkey_file,
2320
signature::{
@@ -82,14 +79,10 @@ fn get_keypair_from_matches(
8279
let mut stdin = std::io::stdin();
8380
Ok(Box::new(read_keypair(&mut stdin)?))
8481
}
85-
KeypairUrl::Usb(path) => {
86-
let (remote_wallet_info, mut derivation_path) = RemoteWalletInfo::parse_path(path)?;
87-
if let Some(derivation) = derivation_of(matches, "derivation_path") {
88-
derivation_path = derivation;
89-
}
90-
let ledger = get_ledger_from_info(remote_wallet_info)?;
91-
Ok(Box::new(generate_remote_keypair(ledger, derivation_path)))
92-
}
82+
KeypairUrl::Usb(path) => Ok(Box::new(generate_remote_keypair(
83+
path,
84+
derivation_of(matches, "derivation_path"),
85+
)?)),
9386
}
9487
}
9588

remote-wallet/src/ledger.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use crate::{
2-
remote_keypair::RemoteKeypair,
3-
remote_wallet::{
4-
initialize_wallet_manager, DerivationPath, RemoteWallet, RemoteWalletError,
5-
RemoteWalletInfo, RemoteWalletType,
6-
},
1+
use crate::remote_wallet::{
2+
initialize_wallet_manager, DerivationPath, RemoteWallet, RemoteWalletError, RemoteWalletInfo,
73
};
84
use dialoguer::{theme::ColorfulTheme, Select};
95
use log::*;
@@ -369,13 +365,3 @@ pub fn get_ledger_from_info(
369365
};
370366
wallet_manager.get_ledger(&wallet_base_pubkey)
371367
}
372-
373-
pub fn generate_remote_keypair(
374-
ledger: Arc<LedgerWallet>,
375-
derivation_path: DerivationPath,
376-
) -> RemoteKeypair {
377-
RemoteKeypair {
378-
wallet_type: RemoteWalletType::Ledger(ledger),
379-
derivation_path,
380-
}
381-
}

remote-wallet/src/remote_keypair.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use crate::remote_wallet::{DerivationPath, RemoteWallet, RemoteWalletType};
1+
use crate::{
2+
ledger::get_ledger_from_info,
3+
remote_wallet::{
4+
DerivationPath, RemoteWallet, RemoteWalletError, RemoteWalletInfo, RemoteWalletType,
5+
},
6+
};
27
use solana_sdk::{
38
pubkey::Pubkey,
49
signature::{KeypairUtil, Signature},
@@ -36,3 +41,22 @@ impl KeypairUtil for RemoteKeypair {
3641
}
3742
}
3843
}
44+
45+
pub fn generate_remote_keypair(
46+
path: String,
47+
explicit_derivation_path: Option<DerivationPath>,
48+
) -> Result<RemoteKeypair, RemoteWalletError> {
49+
let (remote_wallet_info, mut derivation_path) = RemoteWalletInfo::parse_path(path)?;
50+
if let Some(derivation) = explicit_derivation_path {
51+
derivation_path = derivation;
52+
}
53+
if remote_wallet_info.manufacturer == "ledger" {
54+
let ledger = get_ledger_from_info(remote_wallet_info)?;
55+
Ok(RemoteKeypair {
56+
wallet_type: RemoteWalletType::Ledger(ledger),
57+
derivation_path,
58+
})
59+
} else {
60+
Err(RemoteWalletError::DeviceTypeMismatch)
61+
}
62+
}

0 commit comments

Comments
 (0)