Skip to content

Commit

Permalink
Bump slot ( account_sdk ) (#2493)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Oct 2, 2024
1 parent edc8840 commit 1e33eda
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 39 deletions.
168 changes: 145 additions & 23 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 @@ -230,7 +230,7 @@ alloy-sol-types = { version = "0.8.3", default-features = false }
criterion = "0.5.1"

# Slot integration. Dojo don't need to manually include `account_sdk` as dependency as `slot` already re-exports it.
slot = { git = "/~https://github.com/cartridge-gg/slot", rev = "630ed37" }
slot = { git = "/~https://github.com/cartridge-gg/slot", rev = "544cbc6" }

alloy-contract = { version = "0.3", default-features = false }
alloy-json-rpc = { version = "0.3", default-features = false }
Expand Down
29 changes: 14 additions & 15 deletions bin/sozo/src/commands/options/account/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dojo_world::contracts::naming::get_name_from_tag;
use dojo_world::manifest::{BaseManifest, Class, DojoContract, Manifest};
use dojo_world::migration::strategy::generate_salt;
use scarb::core::Config;
use slot::account_sdk::account::session::hash::{AllowedMethod, ProvedMethod};
use slot::account_sdk::account::session::hash::{Policy, ProvedPolicy};
use slot::account_sdk::account::session::merkle::MerkleTree;
use slot::account_sdk::account::session::SessionAccount;
use slot::session::{FullSessionInfo, PolicyMethod};
Expand Down Expand Up @@ -74,7 +74,7 @@ where
// Check if the session exists, if not create a new one
let session_details = match slot::session::get(chain_id)? {
Some(session) => {
trace!(expires_at = %session.session.expires_at, policies = session.session.allowed_methods.len(), "Found existing session.");
trace!(expires_at = %session.session.expires_at, policies = session.session.policies.len(), "Found existing session.");

let policies = collect_policies(world_addr_or_name, contract_address, config)?;
// check if the policies have changed
Expand All @@ -85,7 +85,7 @@ where
} else {
trace!(
new_policies = policies.len(),
existing_policies = session.session.allowed_methods.len(),
existing_policies = session.session.policies.len(),
"Policies have changed. Creating new session."
);

Expand Down Expand Up @@ -113,26 +113,25 @@ where
// This function would compute the merkle root of the new policies and compare it with the root in
// the existing SessionMetadata.
fn is_equal_to_existing(new_policies: &[PolicyMethod], session_info: &FullSessionInfo) -> bool {
let allowed_methods = new_policies
let new_policies = new_policies
.iter()
.map(|p| AllowedMethod::new(p.target, get_selector_from_name(&p.method).unwrap()))
.collect::<Vec<AllowedMethod>>();
.map(|p| Policy::new(p.target, get_selector_from_name(&p.method).unwrap()))
.collect::<Vec<Policy>>();

// Copied from somewhere
let hashes = allowed_methods.iter().map(AllowedMethod::as_merkle_leaf).collect::<Vec<Felt>>();
// Copied from Session::new
let hashes = new_policies.iter().map(Policy::as_merkle_leaf).collect::<Vec<Felt>>();

let allowed_methods = allowed_methods
let new_policies = new_policies
.into_iter()
.enumerate()
.map(|(i, method)| ProvedMethod {
method,
.map(|(i, policy)| ProvedPolicy {
policy,
proof: MerkleTree::compute_proof(hashes.clone(), i),
})
.collect::<Vec<ProvedMethod>>();
.collect::<Vec<ProvedPolicy>>();

let root = MerkleTree::compute_root(hashes[0], allowed_methods[0].proof.clone());

root == session_info.session.allowed_methods_root
let new_policies_root = MerkleTree::compute_root(hashes[0], new_policies[0].proof.clone());
new_policies_root == session_info.session.authorization_root
}

/// Policies are the building block of a session key. It's what defines what methods are allowed for
Expand Down

0 comments on commit 1e33eda

Please sign in to comment.