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(sncast): generate default name when using account import #2610

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ async fn run_async_command(
account::Commands::Import(import) => {
let provider = import.rpc.get_provider(&config).await?;
let result = starknet_commands::account::import::import(
&import.name.clone(),
import.name.clone(),
&config.accounts_file,
&provider,
&import,
Expand Down
23 changes: 20 additions & 3 deletions crates/sncast/src/starknet_commands/account/import.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::{SystemTime, UNIX_EPOCH};

use crate::starknet_commands::account::{
add_created_profile_to_configuration, prepare_account_json, write_account_to_accounts_file,
AccountType,
Expand Down Expand Up @@ -26,7 +28,7 @@ use super::deploy::compute_account_address;
pub struct Import {
/// Name of the account to be imported
#[clap(short, long)]
pub name: String,
pub name: Option<String>,

/// Address of the account
#[clap(short, long)]
Expand Down Expand Up @@ -62,7 +64,7 @@ pub struct Import {
}

pub async fn import(
account: &str,
account: Option<String>,
accounts_file: &Utf8PathBuf,
provider: &JsonRpcClient<HttpTransport>,
import: &Import,
Expand All @@ -80,6 +82,12 @@ pub async fn import(
};
let private_key = &SigningKey::from_secret_scalar(*private_key);

let account = if let Some(name) = account {
name
} else {
generate_default_account_name()
};

let fetched_class_hash = match provider
.get_class_hash_at(BlockId::Tag(BlockTag::Pending), import.address)
.await
Expand Down Expand Up @@ -142,7 +150,7 @@ pub async fn import(
import.salt,
);

write_account_to_accounts_file(account, accounts_file, chain_id, account_json.clone())?;
write_account_to_accounts_file(&account, accounts_file, chain_id, account_json.clone())?;

if import.add_profile.is_some() {
let config = CastConfig {
Expand Down Expand Up @@ -196,6 +204,15 @@ fn get_private_key_from_input() -> Result<Felt> {
parse_input_to_felt(&input)
}

fn generate_default_account_name() -> String {
let start = SystemTime::now();
let since_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
let timestamp = since_epoch.as_millis();
format!("account-{}", timestamp)
}

Comment on lines +207 to +215
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's refactor this function to create account with sequential names (account-1, account-2, ...). Name with timestamp (e.g. account-1729865041172) is too complicated.
Our logic should find next available numer (and handle gaps, e.g. having account-1 and account-3 in file, imported account should receive name account-2).

You can see how accounts are read from the file here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. This was my initial plan. I'll fall back to it.

#[cfg(test)]
mod tests {
use crate::starknet_commands::account::import::parse_input_to_felt;
Expand Down
Loading