Skip to content

Commit

Permalink
Revamp account import (#2565)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #2218

## Introduced changes

<!-- A brief description of the changes -->

- Update `account add` to `account import`
- Remove `--public-key` flag
- Add address computation and validation based on provided salt, class
hash, private key and account type
- Allow to pass private key in interactive mode when neither
`--private-key` nor `--private-key-file` is passed
- Add "Importing Accounts" guide section

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
franciszekjob authored Oct 14, 2024
1 parent 49551b6 commit 0e21418
Show file tree
Hide file tree
Showing 25 changed files with 578 additions and 285 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Short option for `--accounts-file` flag has been removed.
- Short option for `--contract-address` is now `-d` instead of `-a`.
- `account add` is renamed to `account import`.
- `account import` can be now used without specifying `--private-key` or `--private-key-file` flags. Instead private key will be read interactively from the user.

#### Fixed
- `account delete` command: It is no longer necessary to provide the `--url` argument each time. Either the `--url` or `--network` argument must be provided, but not both, as they are mutually exclusive.
Expand Down
12 changes: 6 additions & 6 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,17 +349,17 @@ async fn run_async_command(
}

Commands::Account(account) => match account.command {
account::Commands::Add(add) => {
let provider = add.rpc.get_provider(&config).await?;
let result = starknet_commands::account::add::add(
&add.name.clone(),
account::Commands::Import(import) => {
let provider = import.rpc.get_provider(&config).await?;
let result = starknet_commands::account::import::import(
&import.name.clone(),
&config.accounts_file,
&provider,
&add,
&import,
)
.await;

print_command_result("account add", &result, numbers_format, output_format)?;
print_command_result("account import", &result, numbers_format, output_format)?;
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions crates/sncast/src/response/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ pub struct AccountCreateResponse {
impl CommandResponse for AccountCreateResponse {}

#[derive(Serialize)]
pub struct AccountAddResponse {
pub struct AccountImportResponse {
pub add_profile: String,
}

impl CommandResponse for AccountAddResponse {}
impl CommandResponse for AccountImportResponse {}

#[derive(Serialize)]
pub struct AccountDeleteResponse {
Expand Down
141 changes: 0 additions & 141 deletions crates/sncast/src/starknet_commands/account/add.rs

This file was deleted.

50 changes: 30 additions & 20 deletions crates/sncast/src/starknet_commands/account/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,7 @@ async fn deploy_from_keystore(
.class_hash
.context("Failed to get class hash from keystore")?;

let address = match account_type {
AccountType::Argent => get_contract_address(
salt,
class_hash,
&[private_key.verifying_key().scalar(), Felt::ZERO],
Felt::ZERO,
),
AccountType::OpenZeppelin => get_contract_address(
salt,
class_hash,
&[private_key.verifying_key().scalar()],
chain_id,
),
AccountType::Braavos => get_contract_address(
salt,
BRAAVOS_BASE_ACCOUNT_CLASS_HASH,
&[private_key.verifying_key().scalar()],
chain_id,
),
};
let address = compute_account_address(salt, &private_key, class_hash, account_type, chain_id);

let result = if provider
.get_class_hash_at(BlockId::Tag(Pending), address)
Expand Down Expand Up @@ -370,3 +351,32 @@ fn update_keystore_account(account: &str, address: Felt) -> Result<()> {

Ok(())
}

pub(crate) fn compute_account_address(
salt: Felt,
private_key: &SigningKey,
class_hash: Felt,
account_type: AccountType,
chain_id: Felt,
) -> Felt {
match account_type {
AccountType::Argent => get_contract_address(
salt,
class_hash,
&[private_key.verifying_key().scalar(), Felt::ZERO],
Felt::ZERO,
),
AccountType::OpenZeppelin => get_contract_address(
salt,
class_hash,
&[private_key.verifying_key().scalar()],
chain_id,
),
AccountType::Braavos => get_contract_address(
salt,
BRAAVOS_BASE_ACCOUNT_CLASS_HASH,
&[private_key.verifying_key().scalar()],
chain_id,
),
}
}
Loading

0 comments on commit 0e21418

Please sign in to comment.