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

fix: Refactoring the storage of the access key to the keychain #439

Merged
merged 6 commits into from
Jan 27, 2025
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ keyring = { version = "3.0.5", features = [
"sync-secret-service",
"vendored",
] }

interactive-clap = "0.3"
interactive-clap-derive = "0.3"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ impl From<SaveKeypairToKeychainContext> for crate::commands::ActionContext {
}
});
let on_before_sending_transaction_callback: crate::transaction_signature_options::OnBeforeSendingTransactionCallback =
std::sync::Arc::new(
std::sync::Arc::new({
let credentials_home_dir = item.0.global_context.config.credentials_home_dir.clone();

move |transaction, network_config| {
let account_id = match transaction {
crate::transaction_signature_options::SignedTransactionOrSignedDelegateAction::SignedTransaction(
Expand All @@ -52,14 +54,15 @@ impl From<SaveKeypairToKeychainContext> for crate::commands::ActionContext {
signed_delegate_action,
) => signed_delegate_action.delegate_action.sender_id.clone()
};
crate::common::save_access_key_to_keychain(
crate::common::save_access_key_to_keychain_or_save_to_legacy_keychain(
network_config.clone(),
credentials_home_dir.clone(),
&serde_json::to_string(&item.0.key_pair_properties)?,
&item.0.key_pair_properties.public_key_str,
account_id.as_ref(),
)
},
);
}
});

Self {
global_context: item.0.global_context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ impl SaveModeContext {
SaveModeDiscriminants::SaveToKeychain => {
let key_pair_properties_buf =
serde_json::to_string(&key_pair_properties)?;
crate::common::save_access_key_to_keychain(
network_config.clone(),
&key_pair_properties_buf,
&key_pair_properties.public_key_str,
new_account_id.as_ref(),
)
}
crate::common::save_access_key_to_keychain_or_save_to_legacy_keychain(
network_config.clone(),
credentials_home_dir.clone(),
&key_pair_properties_buf,
&key_pair_properties.public_key_str,
new_account_id.as_ref(),
)
}
SaveModeDiscriminants::SaveToLegacyKeychain => {
let key_pair_properties_buf =
serde_json::to_string(&key_pair_properties)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ impl SaveModeContext {
SaveModeDiscriminants::SaveToKeychain => {
let key_pair_properties_buf =
serde_json::to_string(&key_pair_properties)?;
crate::common::save_access_key_to_keychain(
crate::common::save_access_key_to_keychain_or_save_to_legacy_keychain(
network_config.clone(),
credentials_home_dir.clone(),
&key_pair_properties_buf,
&key_pair_properties.public_key_str,
&new_account_id_str,
Expand Down
20 changes: 8 additions & 12 deletions src/commands/account/import_account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,14 @@ fn save_access_key(
)
.prompt()?;
if let SelectStorage::SaveToKeychain = selection {
let storage_message = crate::common::save_access_key_to_keychain(
network_config,
key_pair_properties_buf,
public_key_str,
account_id.as_ref(),
)
.wrap_err_with(|| {
format!(
"Failed to save the access key <{}> to the keychain",
public_key_str
)
})?;
let storage_message =
crate::common::save_access_key_to_keychain_or_save_to_legacy_keychain(
network_config,
credentials_home_dir,
key_pair_properties_buf,
public_key_str,
account_id.as_ref(),
)?;
eprintln!("{}", storage_message);
return Ok(());
}
Expand Down
35 changes: 35 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,41 @@ pub fn print_transaction_status(
return_value
}

pub fn save_access_key_to_keychain_or_save_to_legacy_keychain(
network_config: crate::config::NetworkConfig,
credentials_home_dir: std::path::PathBuf,
key_pair_properties_buf: &str,
public_key_str: &str,
account_id: &str,
) -> color_eyre::eyre::Result<String> {
match save_access_key_to_keychain(
network_config.clone(),
key_pair_properties_buf,
public_key_str,
account_id,
) {
Ok(message) => Ok(message),
Err(err) => {
eprintln!(
"\n{}\n{}\n",
format!(
"Failed to save the access key <{}> to the keychain.\n{}",
public_key_str, err
)
.red(),
"The data for the access key will be stored in the legacy keychain.".red()
);
save_access_key_to_legacy_keychain(
network_config.clone(),
credentials_home_dir,
key_pair_properties_buf,
public_key_str,
account_id,
)
}
}
}

pub fn save_access_key_to_keychain(
network_config: crate::config::NetworkConfig,
key_pair_properties_buf: &str,
Expand Down
Loading