Skip to content

Commit

Permalink
fix: Use legacy keychain as a fallback storage when system keychain i…
Browse files Browse the repository at this point in the history
…s not supported (e.g. WSL, Codespaces, Docker containers, CI) (#439)

Resolves #435 

![Screenshot 2025-01-22 at 20 47
36](https://github.com/user-attachments/assets/42e60eef-203d-4e09-9d37-66e41e55a98c)

---------

Co-authored-by: FroVolod <frol_off@meta.ua>
  • Loading branch information
FroVolod and FroVolod authored Jan 27, 2025
1 parent 4287b2e commit 1ec213c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 24 deletions.
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

0 comments on commit 1ec213c

Please sign in to comment.