-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the ability to deploy and fetch Argent X accounts.
- Loading branch information
1 parent
2cb377d
commit abc8bf6
Showing
8 changed files
with
328 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use async_trait::async_trait; | ||
use starknet::{ | ||
accounts::{ | ||
AccountFactory, ArgentAccountFactory, OpenZeppelinAccountFactory, RawAccountDeployment, | ||
}, | ||
core::types::FieldElement, | ||
providers::Provider, | ||
signers::Signer, | ||
}; | ||
|
||
pub enum AnyAccountFactory<S, P> { | ||
OpenZeppelin(OpenZeppelinAccountFactory<S, P>), | ||
Argent(ArgentAccountFactory<S, P>), | ||
} | ||
|
||
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] | ||
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] | ||
impl<S, P> AccountFactory for AnyAccountFactory<S, P> | ||
where | ||
S: Signer + Sync + Send, | ||
P: Provider + Sync + Send, | ||
{ | ||
type Provider = P; | ||
type SignError = S::SignError; | ||
|
||
fn class_hash(&self) -> FieldElement { | ||
match self { | ||
AnyAccountFactory::OpenZeppelin(inner) => inner.class_hash(), | ||
AnyAccountFactory::Argent(inner) => inner.class_hash(), | ||
} | ||
} | ||
|
||
fn calldata(&self) -> Vec<FieldElement> { | ||
match self { | ||
AnyAccountFactory::OpenZeppelin(inner) => inner.calldata(), | ||
AnyAccountFactory::Argent(inner) => inner.calldata(), | ||
} | ||
} | ||
|
||
fn chain_id(&self) -> FieldElement { | ||
match self { | ||
AnyAccountFactory::OpenZeppelin(inner) => inner.chain_id(), | ||
AnyAccountFactory::Argent(inner) => inner.chain_id(), | ||
} | ||
} | ||
|
||
fn provider(&self) -> &Self::Provider { | ||
match self { | ||
AnyAccountFactory::OpenZeppelin(inner) => inner.provider(), | ||
AnyAccountFactory::Argent(inner) => inner.provider(), | ||
} | ||
} | ||
|
||
async fn sign_deployment( | ||
&self, | ||
deployment: &RawAccountDeployment, | ||
) -> Result<Vec<FieldElement>, Self::SignError> { | ||
match self { | ||
AnyAccountFactory::OpenZeppelin(inner) => inner.sign_deployment(deployment).await, | ||
AnyAccountFactory::Argent(inner) => inner.sign_deployment(deployment).await, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::{io::Write, path::PathBuf}; | ||
|
||
use anyhow::Result; | ||
use clap::Parser; | ||
use colored::Colorize; | ||
use starknet::{ | ||
core::types::FieldElement, | ||
macros::felt, | ||
signers::{Signer, SigningKey}, | ||
}; | ||
|
||
use crate::{ | ||
account::{ | ||
AccountConfig, AccountVariant, ArgentAccountConfig, DeploymentStatus, UndeployedStatus, | ||
}, | ||
path::ExpandedPathbufParser, | ||
signer::SignerArgs, | ||
}; | ||
|
||
/// Official hashes used as of extension version 5.7.0 | ||
const ARGENT_PROXY_CLASS_HASH: FieldElement = | ||
felt!("0x025ec026985a3bf9d0cc1fe17326b245dfdc3ff89b8fde106542a3ea56c5a918"); | ||
const ARGENT_IMPL_CLASS_HASH: FieldElement = | ||
felt!("0x033434ad846cdd5f23eb73ff09fe6fddd568284a0fb7d1be20ee482f044dabe2"); | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct Init { | ||
// TODO: allow manually specifying public key without using a signer | ||
#[clap(flatten)] | ||
signer: SignerArgs, | ||
#[clap( | ||
long, | ||
short, | ||
help = "Overwrite the account config file if it already exists" | ||
)] | ||
force: bool, | ||
#[clap( | ||
value_parser = ExpandedPathbufParser, | ||
help = "Path to save the account config file" | ||
)] | ||
output: PathBuf, | ||
} | ||
|
||
impl Init { | ||
pub async fn run(self) -> Result<()> { | ||
if self.output.exists() && !self.force { | ||
anyhow::bail!("account config file already exists"); | ||
} | ||
|
||
let signer = self.signer.into_signer()?; | ||
|
||
// Too lazy to write random salt generation | ||
let salt = SigningKey::from_random().secret_scalar(); | ||
|
||
let account_config = AccountConfig { | ||
version: 1, | ||
variant: AccountVariant::Argent(ArgentAccountConfig { | ||
version: 1, | ||
implementation: ARGENT_IMPL_CLASS_HASH, | ||
signer: signer.get_public_key().await?.scalar(), | ||
guardian: FieldElement::ZERO, | ||
}), | ||
deployment: DeploymentStatus::Undeployed(UndeployedStatus { | ||
class_hash: ARGENT_PROXY_CLASS_HASH, | ||
salt, | ||
}), | ||
}; | ||
|
||
let deployed_address = account_config.deploy_account_address()?; | ||
|
||
let mut file = std::fs::File::create(&self.output)?; | ||
serde_json::to_writer_pretty(&mut file, &account_config)?; | ||
file.write_all(b"\n")?; | ||
|
||
eprintln!( | ||
"Created new account config file: {}", | ||
std::fs::canonicalize(&self.output)?.display() | ||
); | ||
eprintln!(); | ||
eprintln!( | ||
"Once deployed, this account will be available at:\n {}", | ||
format!("{:#064x}", deployed_address).bright_yellow() | ||
); | ||
eprintln!(); | ||
eprintln!( | ||
"Deploy this account by running:\n {}", | ||
format!("starkli account deploy {}", self.output.display()).bright_yellow() | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use anyhow::Result; | ||
use clap::{Parser, Subcommand}; | ||
|
||
mod init; | ||
use init::Init; | ||
|
||
#[derive(Debug, Parser)] | ||
pub struct Argent { | ||
#[clap(subcommand)] | ||
command: Subcommands, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum Subcommands { | ||
#[clap(about = "Create a new account configuration without actually deploying")] | ||
Init(Init), | ||
} | ||
|
||
impl Argent { | ||
pub async fn run(self) -> Result<()> { | ||
match self.command { | ||
Subcommands::Init(cmd) => cmd.run().await, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.