Skip to content

Commit a51d1e2

Browse files
authored
Remove account type enum redundancy (#2946)
<!-- Reference any GitHub issues resolved by this PR --> Closes #2571 ## Introduced changes <!-- A brief description of the changes --> - ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [ ] Updated relevant documentation - [ ] Added relevant tests - [x] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md`
1 parent 30a7b69 commit a51d1e2

File tree

4 files changed

+31
-57
lines changed

4 files changed

+31
-57
lines changed

crates/sncast/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use conversions::byte_array::ByteArray;
4747

4848
pub type NestedMap<T> = HashMap<String, HashMap<String, T>>;
4949

50-
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq)]
50+
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
5151
#[serde(rename_all = "lowercase")]
5252
pub enum AccountType {
5353
#[serde(rename = "open_zeppelin")]
@@ -61,7 +61,7 @@ impl FromStr for AccountType {
6161

6262
fn from_str(s: &str) -> Result<Self, Self::Err> {
6363
match s {
64-
"open_zeppelin" | "oz" => Ok(AccountType::OpenZeppelin),
64+
"open_zeppelin" | "open-zeppelin" | "oz" => Ok(AccountType::OpenZeppelin),
6565
"argent" => Ok(AccountType::Argent),
6666
"braavos" => Ok(AccountType::Braavos),
6767
account_type => Err(anyhow!("Invalid account type = {account_type}")),

crates/sncast/src/starknet_commands/account/create.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::starknet_commands::account::{
2-
AccountType, add_created_profile_to_configuration, prepare_account_json,
3-
write_account_to_accounts_file,
2+
add_created_profile_to_configuration, prepare_account_json, write_account_to_accounts_file,
43
};
54
use anyhow::{Context, Result, anyhow, bail};
65
use camino::Utf8PathBuf;
@@ -16,8 +15,8 @@ use sncast::helpers::constants::{
1615
use sncast::helpers::rpc::RpcArgs;
1716
use sncast::response::structs::AccountCreateResponse;
1817
use sncast::{
19-
Network, check_class_hash_exists, check_if_legacy_contract, extract_or_generate_salt,
20-
get_chain_id, get_keystore_password, handle_account_factory_error,
18+
AccountType, Network, check_class_hash_exists, check_if_legacy_contract,
19+
extract_or_generate_salt, get_chain_id, get_keystore_password, handle_account_factory_error,
2120
};
2221
use starknet::accounts::{
2322
AccountDeploymentV1, AccountFactory, ArgentAccountFactory, OpenZeppelinAccountFactory,
@@ -27,12 +26,13 @@ use starknet::providers::JsonRpcClient;
2726
use starknet::providers::jsonrpc::HttpTransport;
2827
use starknet::signers::{LocalWallet, SigningKey};
2928
use starknet_types_core::felt::Felt;
29+
use std::str::FromStr;
3030

3131
#[derive(Args, Debug)]
3232
#[command(about = "Create an account with all important secrets")]
3333
pub struct Create {
3434
/// Type of the account
35-
#[clap(value_enum, short = 't', long = "type", default_value_t = AccountType::Oz)]
35+
#[clap(value_enum, short = 't', long = "type", value_parser = AccountType::from_str, default_value_t = AccountType::OpenZeppelin)]
3636
pub account_type: AccountType,
3737

3838
/// Account name under which account information is going to be saved
@@ -70,14 +70,14 @@ pub async fn create(
7070
let add_profile = create.add_profile.clone();
7171
let salt = extract_or_generate_salt(create.salt);
7272
let class_hash = create.class_hash.unwrap_or(match create.account_type {
73-
AccountType::Oz => OZ_CLASS_HASH,
73+
AccountType::OpenZeppelin => OZ_CLASS_HASH,
7474
AccountType::Argent => ARGENT_CLASS_HASH,
7575
AccountType::Braavos => BRAAVOS_CLASS_HASH,
7676
});
7777
check_class_hash_exists(provider, class_hash).await?;
7878

7979
let (account_json, max_fee) =
80-
generate_account(provider, salt, class_hash, &create.account_type).await?;
80+
generate_account(provider, salt, class_hash, create.account_type).await?;
8181

8282
let address: Felt = account_json["address"]
8383
.as_str()
@@ -104,7 +104,7 @@ pub async fn create(
104104
private_key,
105105
salt,
106106
class_hash,
107-
&create.account_type,
107+
create.account_type,
108108
&keystore,
109109
&account_path,
110110
legacy,
@@ -167,14 +167,14 @@ async fn generate_account(
167167
provider: &JsonRpcClient<HttpTransport>,
168168
salt: Felt,
169169
class_hash: Felt,
170-
account_type: &AccountType,
170+
account_type: AccountType,
171171
) -> Result<(serde_json::Value, Felt)> {
172172
let chain_id = get_chain_id(provider).await?;
173173
let private_key = SigningKey::from_random();
174174
let signer = LocalWallet::from_signing_key(private_key.clone());
175175

176176
let (address, fee_estimate) = match account_type {
177-
AccountType::Oz => {
177+
AccountType::OpenZeppelin => {
178178
let factory =
179179
OpenZeppelinAccountFactory::new(class_hash, chain_id, signer, provider).await?;
180180
get_address_and_deployment_fee(factory, salt).await?
@@ -245,7 +245,7 @@ fn create_to_keystore(
245245
private_key: Felt,
246246
salt: Felt,
247247
class_hash: Felt,
248-
account_type: &AccountType,
248+
account_type: AccountType,
249249
keystore_path: &Utf8PathBuf,
250250
account_path: &Utf8PathBuf,
251251
legacy: bool,
@@ -259,13 +259,12 @@ fn create_to_keystore(
259259
let password = get_keystore_password(CREATE_KEYSTORE_PASSWORD_ENV_VAR)?;
260260
let private_key = SigningKey::from_secret_scalar(private_key);
261261
private_key.save_as_keystore(keystore_path, &password)?;
262-
263262
let account_json = match account_type {
264-
AccountType::Oz => {
263+
AccountType::OpenZeppelin => {
265264
json!({
266265
"version": 1,
267266
"variant": {
268-
"type": format!("{account_type}"),
267+
"type": AccountType::OpenZeppelin,
269268
"version": 1,
270269
"public_key": format!("{:#x}", private_key.verifying_key().scalar()),
271270
"legacy": legacy,
@@ -281,7 +280,7 @@ fn create_to_keystore(
281280
json!({
282281
"version": 1,
283282
"variant": {
284-
"type": format!("{account_type}"),
283+
"type": AccountType::Argent,
285284
"version": 1,
286285
"owner": format!("{:#x}", private_key.verifying_key().scalar()),
287286
"guardian": "0x0",
@@ -298,7 +297,7 @@ fn create_to_keystore(
298297
{
299298
"version": 1,
300299
"variant": {
301-
"type": format!("{account_type}"),
300+
"type": AccountType::Braavos,
302301
"version": 1,
303302
"multisig": {
304303
"status": "off"

crates/sncast/src/starknet_commands/account/import.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use std::str::FromStr;
2+
13
use super::deploy::compute_account_address;
24
use crate::starknet_commands::account::{
3-
AccountType, add_created_profile_to_configuration, prepare_account_json,
4-
write_account_to_accounts_file,
5+
add_created_profile_to_configuration, prepare_account_json, write_account_to_accounts_file,
56
};
67
use anyhow::{Context, Result, bail, ensure};
78
use camino::Utf8PathBuf;
@@ -12,9 +13,7 @@ use sncast::helpers::account::generate_account_name;
1213
use sncast::helpers::configuration::CastConfig;
1314
use sncast::helpers::rpc::RpcArgs;
1415
use sncast::response::structs::AccountImportResponse;
15-
use sncast::{
16-
AccountType as SNCastAccountType, check_class_hash_exists, get_chain_id, handle_rpc_error,
17-
};
16+
use sncast::{AccountType, check_class_hash_exists, get_chain_id, handle_rpc_error};
1817
use starknet::core::types::{BlockId, BlockTag, StarknetError};
1918
use starknet::providers::jsonrpc::{HttpTransport, JsonRpcClient};
2019
use starknet::providers::{Provider, ProviderError};
@@ -33,7 +32,7 @@ pub struct Import {
3332
pub address: Felt,
3433

3534
/// Type of the account
36-
#[clap(short = 't', long = "type")]
35+
#[clap(short = 't', long = "type", value_parser = AccountType::from_str)]
3736
pub account_type: AccountType,
3837

3938
/// Class hash of the account
@@ -121,12 +120,7 @@ pub async fn import(
121120

122121
let chain_id = get_chain_id(provider).await?;
123122
if let Some(salt) = import.salt {
124-
// TODO(#2571)
125-
let sncast_account_type = match import.account_type {
126-
AccountType::Argent => SNCastAccountType::Argent,
127-
AccountType::Braavos => SNCastAccountType::Braavos,
128-
AccountType::Oz => SNCastAccountType::OpenZeppelin,
129-
};
123+
let sncast_account_type = import.account_type;
130124
let computed_address =
131125
compute_account_address(salt, private_key, class_hash, sncast_account_type, chain_id);
132126
ensure!(
@@ -144,7 +138,7 @@ pub async fn import(
144138
import.address,
145139
deployed,
146140
legacy,
147-
&import.account_type,
141+
import.account_type,
148142
Some(class_hash),
149143
import.salt,
150144
);

crates/sncast/src/starknet_commands/account/mod.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ use crate::starknet_commands::account::import::Import;
55
use crate::starknet_commands::account::list::List;
66
use anyhow::{Context, Result, anyhow, bail};
77
use camino::Utf8PathBuf;
8-
use clap::{Args, Subcommand, ValueEnum};
8+
use clap::{Args, Subcommand};
99
use configuration::{
1010
CONFIG_FILENAME, find_config_file, load_config, search_config_upwards_relative_to,
1111
};
1212
use serde_json::json;
13-
use sncast::{chain_id_to_network_name, decode_chain_id, helpers::configuration::CastConfig};
13+
use sncast::{
14+
AccountType, chain_id_to_network_name, decode_chain_id, helpers::configuration::CastConfig,
15+
};
1416
use starknet::signers::SigningKey;
1517
use starknet_types_core::felt::Felt;
16-
use std::{fmt, fs::OpenOptions, io::Write};
18+
use std::{fs::OpenOptions, io::Write};
1719
use toml::Value;
1820

1921
pub mod create;
@@ -38,41 +40,20 @@ pub enum Commands {
3840
List(List),
3941
}
4042

41-
#[expect(clippy::doc_markdown)]
42-
#[derive(ValueEnum, Clone, Debug)]
43-
pub enum AccountType {
44-
/// OpenZeppelin account implementation
45-
Oz,
46-
/// Argent account implementation
47-
Argent,
48-
/// Braavos account implementation
49-
Braavos,
50-
}
51-
52-
impl fmt::Display for AccountType {
53-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54-
match *self {
55-
AccountType::Oz => write!(f, "open_zeppelin"),
56-
AccountType::Argent => write!(f, "argent"),
57-
AccountType::Braavos => write!(f, "braavos"),
58-
}
59-
}
60-
}
61-
6243
pub fn prepare_account_json(
6344
private_key: &SigningKey,
6445
address: Felt,
6546
deployed: bool,
6647
legacy: bool,
67-
account_type: &AccountType,
48+
account_type: AccountType,
6849
class_hash: Option<Felt>,
6950
salt: Option<Felt>,
7051
) -> serde_json::Value {
7152
let mut account_json = json!({
7253
"private_key": format!("{:#x}", private_key.secret_scalar()),
7354
"public_key": format!("{:#x}", private_key.verifying_key().scalar()),
7455
"address": format!("{address:#x}"),
75-
"type": format!("{account_type}"),
56+
"type": format!("{account_type}").to_lowercase().replace("openzeppelin", "open_zeppelin"),
7657
"deployed": deployed,
7758
"legacy": legacy,
7859
});

0 commit comments

Comments
 (0)