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(sozo/account): legacy bool flag to use old calldata encoding #1419

Merged
merged 3 commits into from
Jan 12, 2024
Merged
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
1 change: 1 addition & 0 deletions crates/sozo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ url.workspace = true
[dev-dependencies]
assert_fs = "1.0.10"
dojo-test-utils = { path = "../dojo-test-utils", features = [ "build-examples" ] }
katana-runner = { path = "../katana/runner" }
snapbox = "0.4.6"
65 changes: 58 additions & 7 deletions crates/sozo/src/commands/options/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub struct AccountOptions {
#[arg(help_heading = "Signer options - KEYSTORE")]
#[arg(help = "The keystore password. Used with --keystore.")]
pub keystore_password: Option<String>,

#[arg(long)]
#[arg(help = "Use legacy account (cairo0 account)")]
pub legacy: bool,
}

impl AccountOptions {
Expand All @@ -57,13 +61,9 @@ impl AccountOptions {
let chain_id =
provider.chain_id().await.with_context(|| "Failed to retrieve network chain id.")?;

Ok(SingleOwnerAccount::new(
provider,
signer,
account_address,
chain_id,
ExecutionEncoding::New,
))
let encoding = if self.legacy { ExecutionEncoding::Legacy } else { ExecutionEncoding::New };

Ok(SingleOwnerAccount::new(provider, signer, account_address, chain_id, encoding))
}

fn signer(&self, env_metadata: Option<&Environment>) -> Result<LocalWallet> {
Expand Down Expand Up @@ -118,6 +118,8 @@ mod tests {
use std::str::FromStr;

use clap::Parser;
use katana_runner::KatanaRunner;
use starknet::accounts::{Call, ExecutionEncoder};
use starknet::signers::{LocalWallet, Signer, SigningKey};
use starknet_crypto::FieldElement;

Expand Down Expand Up @@ -322,4 +324,53 @@ mod tests {
.is_err()
);
}

#[tokio::test]
async fn legacy_flag_works_as_expected() {
let cmd = Command::parse_from([
"sozo",
"--legacy",
"--account-address",
"0x0",
"--private-key",
"0x1",
]);
let (_runner, provider) = KatanaRunner::new().unwrap();
let dummy_call = vec![Call {
to: FieldElement::from_hex_be("0x0").unwrap(),
selector: FieldElement::from_hex_be("0x1").unwrap(),
calldata: vec![
FieldElement::from_hex_be("0x2").unwrap(),
FieldElement::from_hex_be("0x3").unwrap(),
],
}];

// HACK: SingleOwnerAccount doesn't expose a way to check `encoding` type used in struct, so
// checking it by encoding a dummy call and checking which method it used to encode the call
let account = cmd.account.account(provider, None).await.unwrap();
let result = account.encode_calls(&dummy_call);
// 0x0 is the data offset.
assert!(*result.get(3).unwrap() == FieldElement::from_hex_be("0x0").unwrap());
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
}

#[tokio::test]
async fn without_legacy_flag_works_as_expected() {
let cmd = Command::parse_from(["sozo", "--account-address", "0x0", "--private-key", "0x1"]);
let (_runner, provider) = KatanaRunner::new().unwrap();
let dummy_call = vec![Call {
to: FieldElement::from_hex_be("0x0").unwrap(),
selector: FieldElement::from_hex_be("0x1").unwrap(),
calldata: vec![
FieldElement::from_hex_be("0xf2").unwrap(),
FieldElement::from_hex_be("0xf3").unwrap(),
],
}];

// HACK: SingleOwnerAccount doesn't expose a way to check `encoding` type used in struct, so
// checking it by encoding a dummy call and checking which method it used to encode the call
let account = cmd.account.account(provider, None).await.unwrap();
let result = account.encode_calls(&dummy_call);
// 0x2 is the Calldata len.
assert!(*result.get(3).unwrap() == FieldElement::from_hex_be("0x2").unwrap());
lambda-0x marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading