Skip to content

Commit

Permalink
update account_sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Oct 2, 2024
1 parent 5ad0e98 commit bbffe04
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
54 changes: 46 additions & 8 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions slot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ tempfile = "3.10.1"
hyper.workspace = true
serde_with = "3.9.0"

# Must be synced across Dojo
account_sdk = { git = "https://github.com/cartridge-gg/controller", rev = "fea57f1" }
account_sdk = { git = "https://github.com/cartridge-gg/controller" }
base64 = "0.22.1"
21 changes: 10 additions & 11 deletions slot/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::path::Path;
use std::{fs, path::PathBuf};

use account_sdk::account::session::hash::{AllowedMethod, Session};
use account_sdk::account::session::hash::{Policy, Session};
use account_sdk::account::session::SessionAccount;
use account_sdk::signers::{HashSigner, Signer};
use anyhow::Context;
use anyhow::{anyhow, Context};
use axum::response::{IntoResponse, Response};
use axum::{extract::State, routing::post, Router};
use hyper::StatusCode;
Expand All @@ -29,8 +29,6 @@ use crate::{browser, server::LocalServer, vars};
const GUARDIAN: Felt = short_string!("CARTRIDGE_GUARDIAN");
pub const SESSION_GUARDIAN_SIGNING_KEY: SigningKey = SigningKey::from_secret_scalar(GUARDIAN);

// Taken from: https://github.com/cartridge-gg/controller/blob/b2c6ed8fcbabdc2e40176ce9955e155c662a9f1c/packages/keychain/src/const.ts#L2C1-L2C47
const DEFAULT_SESSION_EXPIRES_AT: u64 = 1727776800;
const SESSION_CREATION_PATH: &str = "/session";
const SESSION_FILE_BASE_NAME: &str = "session.json";

Expand Down Expand Up @@ -62,13 +60,11 @@ impl FullSessionInfo {
where
P: Provider + Send,
{
let session_guardian = Signer::Starknet(SESSION_GUARDIAN_SIGNING_KEY);
let session_signer = Signer::Starknet(SigningKey::from_secret_scalar(self.auth.signer));

SessionAccount::new_as_registered(
provider,
session_signer,
session_guardian,
self.auth.address,
self.chain_id,
self.auth.owner_guid,
Expand Down Expand Up @@ -137,11 +133,12 @@ pub async fn create(rpc_url: Url, policies: &[PolicyMethod]) -> Result<FullSessi

let methods = policies
.iter()
.map(AllowedMethod::try_from)
.map(Policy::try_from)
.collect::<Result<Vec<_>, _>>()
.map_err(Error::InvalidMethodName)?;

let session = Session::new(methods, DEFAULT_SESSION_EXPIRES_AT, &signer.signer())?;
let expires_at = response.expires_at.parse::<u64>().map_err(|e| anyhow!(e))?;
let session = Session::new(methods, expires_at, &signer.signer())?;
let chain_id = get_network_chain_id(rpc_url).await?;

Ok(FullSessionInfo {
Expand Down Expand Up @@ -199,7 +196,7 @@ fn store_at(

/// The response object to the session creation request.
//
// A reflection of https://github.com/cartridge-gg/controller/blob/90b767bcc6478f0e02973f7237bc2a974f745adf/packages/keychain/src/pages/session.tsx#L15-L21
// A reflection of https://github.com/cartridge-gg/controller/blob/1ac2995e4d430e9d3b88e3a62f4d3eb21a2496c3/packages/keychain/src/pages/session.tsx#L15-L22
#[cfg_attr(test, derive(PartialEq, Serialize))]
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -208,6 +205,8 @@ pub struct SessionCreationResponse {
pub username: String,
/// The address of the Controller account associated with the username.
pub address: Felt,
/// The session's expiration date.
pub expires_at: String,

pub owner_guid: Felt,
/// The hash of the session creation transaction. `None` is the session
Expand Down Expand Up @@ -380,7 +379,7 @@ async fn get_network_chain_id(url: Url) -> anyhow::Result<Felt> {
Ok(provider.chain_id().await?)
}

impl TryFrom<PolicyMethod> for AllowedMethod {
impl TryFrom<PolicyMethod> for account_sdk::account::session::hash::Policy {
type Error = NonAsciiNameError;

fn try_from(value: PolicyMethod) -> Result<Self, Self::Error> {
Expand All @@ -391,7 +390,7 @@ impl TryFrom<PolicyMethod> for AllowedMethod {
}
}

impl TryFrom<&PolicyMethod> for AllowedMethod {
impl TryFrom<&PolicyMethod> for account_sdk::account::session::hash::Policy {
type Error = NonAsciiNameError;

fn try_from(value: &PolicyMethod) -> Result<Self, Self::Error> {
Expand Down

0 comments on commit bbffe04

Please sign in to comment.