Skip to content

Commit

Permalink
feat: support Cairo 1 account contract
Browse files Browse the repository at this point in the history
Adds support for using Cairo 1 style of `__execute__` calldata encoding.
However, currently the only way to enable it is to manually edit an OZ
account file to set the `legacy` field to `false`.
  • Loading branch information
xJonathanLEI committed Sep 1, 2023
1 parent aa62521 commit afc85bc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 14 deletions.
23 changes: 12 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ serde = { version = "1.0.164", features = ["derive"] }
serde_json = { version = "1.0.99", features = ["preserve_order"] }
serde_with = "2.3.3"
shellexpand = "3.1.0"
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "e9bdd7efeae9c76ef62a130ccef3f4355a864797" }
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs", rev = "4cbf0d66c213e4d2f01c796f642916af7675c4dd" }
thiserror = "1.0.40"
tokio = { version = "1.28.2", default-features = false, features = ["macros", "rt-multi-thread"] }
url = "2.4.0"
Expand Down
32 changes: 30 additions & 2 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clap::Parser;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use starknet::{
accounts::SingleOwnerAccount,
accounts::{ExecutionEncoding, SingleOwnerAccount},
core::{
serde::unsigned_field_element::UfeHex,
types::{BlockId, BlockTag, FieldElement},
Expand Down Expand Up @@ -89,6 +89,8 @@ pub struct OzAccountConfig {
pub version: u64,
#[serde_as(as = "UfeHex")]
pub public_key: FieldElement,
#[serde(default = "true_as_default")]
pub legacy: bool,
}

#[serde_as]
Expand Down Expand Up @@ -192,7 +194,13 @@ impl AccountArgs {

let chain_id = provider.chain_id().await?;

let mut account = SingleOwnerAccount::new(provider, signer, account_address, chain_id);
let mut account = SingleOwnerAccount::new(
provider,
signer,
account_address,
chain_id,
account_config.variant.execution_encoding(),
);
account.set_block_id(BlockId::Tag(BlockTag::Pending));

Ok(account)
Expand Down Expand Up @@ -261,6 +269,22 @@ impl AccountConfig {
}
}

impl AccountVariant {
pub fn execution_encoding(&self) -> ExecutionEncoding {
match self {
AccountVariant::OpenZeppelin(oz) => {
if oz.legacy {
ExecutionEncoding::Legacy
} else {
ExecutionEncoding::New
}
}
AccountVariant::Argent(_) => ExecutionEncoding::Legacy,
AccountVariant::Braavos(_) => ExecutionEncoding::Legacy,
}
}
}

impl BraavosSigner {
pub fn decode(raw_signer_model: &[FieldElement]) -> Result<Self> {
let raw_signer_type = raw_signer_model
Expand All @@ -287,3 +311,7 @@ impl Display for AccountVariantType {
}
}
}

fn true_as_default() -> bool {
true
}
1 change: 1 addition & 0 deletions src/subcommands/account/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Fetch {
AccountVariant::OpenZeppelin(OzAccountConfig {
version: 1,
public_key,
legacy: true,
})
}
AccountVariantType::Argent => {
Expand Down
1 change: 1 addition & 0 deletions src/subcommands/account/oz/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl Init {
variant: AccountVariant::OpenZeppelin(OzAccountConfig {
version: 1,
public_key: signer.get_public_key().await?.scalar(),
legacy: true,
}),
deployment: DeploymentStatus::Undeployed(UndeployedStatus {
class_hash: OZ_ACCOUNT_CLASS_HASH,
Expand Down

0 comments on commit afc85bc

Please sign in to comment.