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

chore(sozo): update slot due to breaking changes #2530

Merged
merged 3 commits into from
Oct 13, 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
5 changes: 2 additions & 3 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 @@ -232,7 +232,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 = "942be15" }
slot = { git = "https://github.com/cartridge-gg/slot", rev = "1298a30" }

alloy-contract = { version = "0.3", default-features = false }
alloy-json-rpc = { version = "0.3", default-features = false }
Expand Down
38 changes: 19 additions & 19 deletions bin/sozo/src/commands/options/account/controller.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use dojo_world::contracts::naming::get_name_from_tag;
use dojo_world::manifest::{BaseManifest, Class, DojoContract, Manifest};
Expand Down Expand Up @@ -34,9 +34,9 @@

/// Create a new Catridge Controller account based on session key.
///
/// Controller guarantees that if the provided network is among one of the supported networks,
/// then the Controller account should exist. If it doesn't yet exist, it will automatically
/// be created when a session is created (ie during the session registration stage).
/// For now, Controller guarantees that if the provided network is among one of the supported
/// networks, then the Controller account should exist. If it doesn't yet exist, it will
/// automatically be created when a session is created (ie during the session registration stage).
///
/// # Supported networks
///
Expand All @@ -60,31 +60,31 @@
P: Send + Sync,
{
let chain_id = provider.chain_id().await?;
let credentials = slot::credential::Credentials::load()?;

trace!(target: "account::controller", "Loading Slot credentials.");
let credentials = slot::credential::Credentials::load()?;
let username = credentials.account.id;
let contract_address = credentials.account.contract_address;

trace!(
%username,
chain = format!("{chain_id:#x}"),
address = format!("{contract_address:#x}"),
"Creating Controller session account"
);
// Right now, the Cartridge Controller API ensures that there's always a Controller associated
// with an account, but that might change in the future.
let Some(contract_address) = credentials.account.controllers.first().map(|c| c.address) else {

Check warning on line 70 in bin/sozo/src/commands/options/account/controller.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/account/controller.rs#L70

Added line #L70 was not covered by tests
bail!("No Controller is associated with this account.");
};

// 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.policies.len(), "Found existing session.");
trace!(target: "account::controller", expires_at = %session.session.expires_at, policies = session.session.policies.len(), "Found existing session.");

// Check if the policies have changed
let policies = collect_policies(world_addr_or_name, contract_address, config)?;
// check if the policies have changed
let is_equal = is_equal_to_existing(&policies, &session);

if is_equal {
session
} else {
trace!(
target: "account::controller",
new_policies = policies.len(),
existing_policies = session.session.policies.len(),
"Policies have changed. Creating new session."
Expand All @@ -98,7 +98,7 @@

// Create a new session if not found
None => {
trace!(%username, chain = format!("{chain_id:#}"), "Creating new session.");
trace!(target: "account::controller", %username, chain = format!("{chain_id:#}"), "Creating new session.");
let policies = collect_policies(world_addr_or_name, contract_address, config)?;
let session = slot::session::create(rpc_url.clone(), &policies).await?;
slot::session::store(chain_id, &session)?;
Expand Down Expand Up @@ -149,7 +149,7 @@
let manifest = get_project_base_manifest(root_dir, config.profile().as_str())?;
let policies =
collect_policies_from_base_manifest(world_addr_or_name, user_address, root_dir, manifest)?;
trace!(policies_count = policies.len(), "Extracted policies from project.");
trace!(target: "account::controller", policies_count = policies.len(), "Extracted policies from project.");
Ok(policies)
}

Expand Down Expand Up @@ -188,14 +188,14 @@
// corresponds to [account_sdk::account::DECLARATION_SELECTOR]
let method = "__declare_transaction__".to_string();
policies.push(PolicyMethod { target: user_address, method });
trace!("Adding declare transaction policy");
trace!(target: "account::controller", "Adding declare transaction policy");

// for deploying using udc
let method = "deployContract".to_string();
const UDC_ADDRESS: Felt =
felt!("0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf");
policies.push(PolicyMethod { target: UDC_ADDRESS, method });
trace!("Adding UDC deployment policy");
trace!(target: "account::controller", "Adding UDC deployment policy");

Ok(policies)
}
Expand All @@ -215,7 +215,7 @@
if let StateMutability::External = f.state_mutability {
let policy =
PolicyMethod { target: contract_address, method: f.name.to_string() };
trace!(tag = contract_tag, target = format!("{:#x}", policy.target), method = %policy.method, "Adding policy");
trace!(target: "account::controller", tag = contract_tag, target = format!("{:#x}", policy.target), method = %policy.method, "Adding policy");
policies.push(policy);
}
}
Expand Down
43 changes: 27 additions & 16 deletions crates/katana/controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@
add_controller_account_inner(genesis, credentials.account)
}

fn add_controller_account_inner(genesis: &mut Genesis, user: slot::account::Account) -> Result<()> {
let cred = user.credentials.webauthn.first().unwrap();
fn add_controller_account_inner(
genesis: &mut Genesis,
user: slot::account::AccountInfo,
) -> Result<()> {
let cred = user.credentials.first().unwrap();
let contract_address = user.controllers.first().unwrap().address;

trace!(
username = user.id,
address = format!("{:#x}", user.contract_address),
address = format!("{:#x}", contract_address),

Check warning on line 39 in crates/katana/controller/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/controller/src/lib.rs#L39

Added line #L39 was not covered by tests
"Adding Cartridge Controller account to genesis."
);

Expand All @@ -47,7 +51,7 @@
storage: Some(get_contract_storage(credential_id, public_key)?),
};

let address = ContractAddress::from(user.contract_address);
let address = ContractAddress::from(contract_address);

(address, GenesisAllocation::Contract(account))
};
Expand All @@ -56,7 +60,7 @@

trace!(
username = user.id,
address = format!("{:#x}", user.contract_address),
address = format!("{:#x}", contract_address),

Check warning on line 63 in crates/katana/controller/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/controller/src/lib.rs#L63

Added line #L63 was not covered by tests
"Cartridge Controller account added to genesis."
);

Expand All @@ -81,7 +85,8 @@
pub fn add_controller_account_json(genesis: &mut GenesisJson) -> Result<()> {
// bouncer that checks if there is an authenticated slot user
let user = Credentials::load()?;
let cred = user.account.credentials.webauthn.first().unwrap();
let cred = user.account.credentials.first().unwrap();
let contract_address = user.account.controllers.first().unwrap().address;

Check warning on line 89 in crates/katana/controller/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/controller/src/lib.rs#L88-L89

Added lines #L88 - L89 were not covered by tests

let credential_id = webauthn::credential::from_base64(&cred.id)?;
let public_key = webauthn::cose_key::from_base64(&cred.public_key)?;
Expand All @@ -96,7 +101,7 @@
storage: Some(get_contract_storage(credential_id, public_key)?),
};

let address = ContractAddress::from(user.account.contract_address);
let address = ContractAddress::from(contract_address);

Check warning on line 104 in crates/katana/controller/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/controller/src/lib.rs#L104

Added line #L104 was not covered by tests

(address, account)
};
Expand Down Expand Up @@ -190,8 +195,9 @@

#[cfg(test)]
mod tests {

use assert_matches::assert_matches;
use slot::account::WebAuthnCredential;
use slot::account::{Controller, ControllerSigner, SignerType, WebAuthnCredential};
use starknet::macros::felt;

use super::*;
Expand All @@ -216,21 +222,26 @@
fn test_add_controller_account() {
let mut genesis = Genesis::default();

let account = slot::account::Account {
let account = slot::account::AccountInfo {
id: "johnsmith".to_string(),
name: None,
contract_address: CONTROLLER_ADDRESS,
credentials: slot::account::AccountCredentials {
webauthn: vec![WebAuthnCredential {
id: WEBAUTHN_CREDENTIAL_ID.to_string(),
public_key: WEBAUTHN_PUBLIC_KEY.to_string(),
controllers: vec![Controller {
id: "controller1".to_string(),
address: CONTROLLER_ADDRESS,
signers: vec![ControllerSigner {
id: "signer1".to_string(),
r#type: SignerType::WebAuthn,
}],
},
}],
credentials: vec![WebAuthnCredential {
id: WEBAUTHN_CREDENTIAL_ID.to_string(),
public_key: WEBAUTHN_PUBLIC_KEY.to_string(),
}],
};

add_controller_account_inner(&mut genesis, account.clone()).unwrap();

let address = ContractAddress::from(account.contract_address);
let address = ContractAddress::from(account.controllers[0].address);
let allocation = genesis.allocations.get(&address).unwrap();

assert!(genesis.allocations.contains_key(&address));
Expand Down
Loading