Skip to content

Commit 497da46

Browse files
fix(sncast): adapt native accounts to required addresses
1 parent 4479dc4 commit 497da46

8 files changed

Lines changed: 37 additions & 21 deletions

File tree

crates/sncast/src/helpers/devnet/provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub struct PredeployedAccount {
120120
impl From<&PredeployedAccount> for AccountRecord {
121121
fn from(predeployed_account: &PredeployedAccount) -> Self {
122122
Self {
123-
address: Some(predeployed_account.address),
123+
address: predeployed_account.address,
124124
public_key: predeployed_account.public_key,
125125
class_hash: None,
126126
salt: None,

crates/sncast/src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,31 @@ pub struct AccountData {
117117
pub signer_type: SignerType,
118118
}
119119

120-
impl From<AccountData> for accounts::AccountRecord {
121-
fn from(account: AccountData) -> Self {
120+
impl TryFrom<AccountData> for accounts::AccountRecord {
121+
type Error = accounts::AccountsError;
122+
123+
fn try_from(account: AccountData) -> Result<Self, Self::Error> {
122124
let signer = match account.signer_type {
123125
SignerType::Local { private_key } => {
124126
SignerSpec::PrivateKey(PrivateKeySpec::new(private_key))
125127
}
126128
SignerType::Ledger { ledger_path } => SignerSpec::Ledger(LedgerSpec::new(ledger_path)),
127129
};
128-
Self {
130+
Ok(Self {
129131
public_key: account.public_key,
130-
address: account.address,
132+
address: account
133+
.address
134+
.ok_or(accounts::AccountsError::MissingField {
135+
field: "address",
136+
operation: "connected account use",
137+
})?,
131138
salt: account.salt,
132139
deployed: account.deployed,
133140
class_hash: account.class_hash,
134141
legacy: account.legacy,
135142
account_type: account.account_type,
136143
signer,
137-
}
144+
})
138145
}
139146
}
140147

@@ -382,7 +389,7 @@ pub async fn get_account_from_repository<'a>(
382389
let chain_id = get_chain_id(provider).await?;
383390
let (account_data, signer) = if let Some(keystore) = keystore {
384391
let account_data: accounts::AccountRecord =
385-
get_account_data_from_keystore(account, keystore)?.into();
392+
get_account_data_from_keystore(account, keystore)?.try_into()?;
386393
let private_key = account_data
387394
.signer
388395
.private_key()
@@ -442,9 +449,7 @@ pub(crate) async fn build_account(
442449
provider: &JsonRpcClient<HttpTransport>,
443450
signer: RuntimeSigner,
444451
) -> Result<RuntimeAccount<'_>> {
445-
let address = account_data
446-
.address
447-
.context("Failed to get account address")?;
452+
let address = account_data.address;
448453

449454
verify_account_address(address, chain_id, provider).await?;
450455

@@ -595,7 +600,7 @@ pub fn get_account_data_from_repository(
595600
};
596601
Ok(AccountData {
597602
public_key: account.public_key,
598-
address: account.address,
603+
address: Some(account.address),
599604
salt: account.salt,
600605
deployed: account.deployed,
601606
class_hash: account.class_hash,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub async fn create(
114114
let (account_record, estimated_fee) =
115115
generate_account(provider, signer_source, ui, generation_params).await?;
116116

117-
let address = account_record.address.context("Invalid address")?;
117+
let address = account_record.address;
118118

119119
let estimated_fee_strk = BigDecimal::new(estimated_fee.into(), 18.into());
120120
let mut message = format!(

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use camino::Utf8PathBuf;
33
use clap::Args;
44
use conversions::IntoConv;
55
use serde_json::Map;
6-
use sncast::accounts::{AccountRecord, AccountRepository};
6+
use sncast::accounts::{AccountRecord, AccountRepository, AccountsError};
77
use sncast::helpers::braavos::BraavosAccountFactory;
88
use sncast::helpers::constants::BRAAVOS_BASE_ACCOUNT_CLASS_HASH;
99
use sncast::helpers::dry_run::DryRunArgs;
@@ -129,8 +129,20 @@ async fn deploy_from_keystore(
129129
bail!("Public key and private key from keystore do not match");
130130
}
131131

132-
let account_record: AccountRecord = account_data.clone().into();
133-
let (account_type, class_hash, salt) = extract_deployment_fields(&account_record)?;
132+
let account_type = account_data
133+
.account_type
134+
.ok_or(AccountsError::MissingField {
135+
field: "type",
136+
operation: "account deployment",
137+
})?;
138+
let class_hash = account_data.class_hash.ok_or(AccountsError::MissingField {
139+
field: "class_hash",
140+
operation: "account deployment",
141+
})?;
142+
let salt = account_data.salt.ok_or(AccountsError::MissingField {
143+
field: "salt",
144+
operation: "account deployment",
145+
})?;
134146

135147
let signer = LocalWallet::from_signing_key(private_key);
136148
let (address, result) = create_factory_and_deploy(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl AccountDataRepresentationMessage {
6565
},
6666
public_key: account.public_key.into_hex_string(),
6767
network: None,
68-
address: account.address.map(IntoHexStr::into_hex_string),
68+
address: Some(account.address.into_hex_string()),
6969
salt: account.salt.map(IntoHexStr::into_hex_string),
7070
deployed: account.deployed,
7171
class_hash: account.class_hash.map(IntoHexStr::into_hex_string),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub fn prepare_account_record(
112112
) -> AccountRecord {
113113
AccountRecord {
114114
public_key,
115-
address: Some(address),
115+
address,
116116
salt,
117117
deployed: Some(deployed),
118118
class_hash,

crates/sncast/src/starknet_commands/get/balance.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::starknet_commands::utils::felt_or_id::{ContractAddress, TokenAddress};
2-
use anyhow::{Context, Error, Result, bail};
2+
use anyhow::{Error, Result, bail};
33
use clap::Args;
44
use primitive_types::U256;
55
use sncast::helpers::command::process_command_result;
@@ -110,9 +110,7 @@ async fn get_account_address(
110110
let repository = sncast::accounts::AccountRepository::new(config.accounts_file.clone());
111111
let account_data = get_account_record_from_repository(&config.account, chain_id, &repository)?;
112112

113-
account_data
114-
.address
115-
.context("Failed to get account address")
113+
Ok(account_data.address)
116114
}
117115

118116
#[expect(clippy::result_large_err)]

crates/sncast/tests/e2e/account/deploy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use conversions::string::IntoHexStr;
1212
use indoc::indoc;
1313
use shared::test_utils::output_assert::{AsOutput, assert_stderr_contains, assert_stdout_contains};
1414
use sncast::AccountType;
15+
use sncast::helpers::account::load_accounts;
1516
use sncast::helpers::constants::{
1617
BRAAVOS_CLASS_HASH, KEYSTORE_PASSWORD_ENV_VAR, OZ_CLASS_HASH, READY_CLASS_HASH,
1718
};

0 commit comments

Comments
 (0)