Skip to content

Commit

Permalink
Update to new account_sdk session creation logic (#87)
Browse files Browse the repository at this point in the history
- bump `account_sdk` rev
- re-export `account_sdk` so that Dojo don't have to manually sync the `account_sdk` version
- update the session creation logic
  • Loading branch information
kariy authored Sep 12, 2024
1 parent e8e43a3 commit 43840df
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 88 deletions.
32 changes: 17 additions & 15 deletions Cargo.lock

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

21 changes: 7 additions & 14 deletions cli/src/command/auth/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use std::str::FromStr;

use anyhow::{anyhow, ensure, Result};
use clap::Parser;
use slot::session::{self, Policy};
use slot::session::{self, PolicyMethod};
use starknet::core::types::Felt;
use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider};
use url::Url;

#[derive(Debug, Parser)]
Expand All @@ -16,34 +15,28 @@ pub struct CreateSession {
rpc_url: String,

#[arg(help = "The session's policies.")]
#[arg(value_parser = parse_policy)]
#[arg(value_parser = parse_policy_method)]
#[arg(required = true)]
policies: Vec<Policy>,
policies: Vec<PolicyMethod>,
}

impl CreateSession {
pub async fn run(&self) -> Result<()> {
let url = Url::parse(&self.rpc_url)?;
let chain_id = get_network_chain_id(url.clone()).await?;
let session = session::create(url, &self.policies).await?;
session::store(chain_id, &session)?;
session::store(session.chain_id, &session)?;
Ok(())
}
}

fn parse_policy(value: &str) -> Result<Policy> {
fn parse_policy_method(value: &str) -> Result<PolicyMethod> {
let mut parts = value.split(',');

let target = parts.next().ok_or(anyhow!("missing target"))?.to_owned();
let target = Felt::from_str(&target)?;
let method = parts.next().ok_or(anyhow!("missing method"))?.to_owned();

ensure!(parts.next().is_none(), " bruh");
ensure!(parts.next().is_none());

Ok(Policy { target, method })
}

async fn get_network_chain_id(url: Url) -> Result<Felt> {
let provider = JsonRpcClient::new(HttpTransport::new(url));
Ok(provider.chain_id().await?)
Ok(PolicyMethod { target, method })
}
3 changes: 2 additions & 1 deletion slot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ hyper.workspace = true
serde_with = "3.9.0"

# Must be synced across Dojo
account_sdk = { git = "https://github.com/cartridge-gg/controller", rev = "0b5c318" }
account_sdk = { git = "https://github.com/cartridge-gg/controller", rev = "e433a45" }
base64 = "0.22.1"
9 changes: 9 additions & 0 deletions slot/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use account_sdk::signers::SignError;
use starknet::core::utils::NonAsciiNameError;

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Expand All @@ -20,4 +23,10 @@ pub enum Error {

#[error(transparent)]
Anyhow(#[from] anyhow::Error),

#[error("Invalid method name: {0}")]
InvalidMethodName(NonAsciiNameError),

#[error(transparent)]
Signing(#[from] SignError),
}
1 change: 1 addition & 0 deletions slot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ pub mod vars;
pub(crate) mod error;
pub(crate) mod utils;

pub use account_sdk;
pub use error::Error;
Loading

0 comments on commit 43840df

Please sign in to comment.