Skip to content

feat(controller): add support for typed data policies #106

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion dojo.h
Original file line number Diff line number Diff line change
Expand Up @@ -663,10 +663,27 @@ typedef struct BlockId {
};
} BlockId;

typedef struct Policy {
typedef struct CallPolicy {
struct FieldElement target;
const char *method;
const char *description;
} CallPolicy;

typedef enum Policy_Tag {
Call,
TypedData,
} Policy_Tag;

typedef struct Policy {
Policy_Tag tag;
union {
struct {
struct CallPolicy call;
};
struct {
const char *typed_data;
};
};
} Policy;

typedef struct Controller {
Expand Down
45 changes: 44 additions & 1 deletion dojo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,55 @@ struct BlockId {
}
};

struct Policy {
struct CallPolicy {
FieldElement target;
const char *method;
const char *description;
};

struct Policy {
enum class Tag {
Call,
TypedData,
};

struct Call_Body {
CallPolicy _0;
};

struct TypedData_Body {
const char *_0;
};

Tag tag;
union {
Call_Body call;
TypedData_Body typed_data;
};

static Policy Call(const CallPolicy &_0) {
Policy result;
::new (&result.call._0) (CallPolicy)(_0);
result.tag = Tag::Call;
return result;
}

bool IsCall() const {
return tag == Tag::Call;
}

static Policy TypedData(const char *const &_0) {
Policy result;
::new (&result.typed_data._0) (const char*)(_0);
result.tag = Tag::TypedData;
return result;
}

bool IsTypedData() const {
return tag == Tag::TypedData;
}
};

struct EntityKeysClause {
enum class Tag {
HashedKeys,
Expand Down
11 changes: 10 additions & 1 deletion dojo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,20 @@ cdef extern from *:
uint64_t number;
BlockTag block_tag;

cdef struct Policy:
cdef struct CallPolicy:
FieldElement target;
const char *method;
const char *description;

cdef enum Policy_Tag:
Call,
TypedData,

cdef struct Policy:
Policy_Tag tag;
CallPolicy call;
const char *typed_data;

cdef struct Controller:
FieldElement address;
const char *username;
Expand Down
46 changes: 34 additions & 12 deletions src/c/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,53 @@ impl<T> From<COption<T>> for Option<T> {

#[derive(Debug, Clone)]
#[repr(C)]
pub struct Policy {
pub enum Policy {
Call(CallPolicy),
TypedData(*const c_char),
}

#[derive(Debug, Clone)]
#[repr(C)]
pub struct CallPolicy {
pub target: FieldElement,
pub method: *const c_char,
pub description: *const c_char,
}

impl From<Policy> for crate::types::Policy {
fn from(val: Policy) -> Self {
crate::types::Policy {
impl From<CallPolicy> for account_sdk::account::session::policy::CallPolicy {
fn from(val: CallPolicy) -> Self {
account_sdk::account::session::policy::CallPolicy {
contract_address: val.target.into(),
selector: get_selector_from_name(&unsafe { CStr::from_ptr(val.method).to_string_lossy().to_string() }).unwrap(),
authorized: Some(true),
}
}
}

impl From<CallPolicy> for crate::types::CallPolicy {
fn from(val: CallPolicy) -> Self {
crate::types::CallPolicy {
target: val.target.into(),
method: unsafe { CStr::from_ptr(val.method).to_string_lossy().to_string() },
description: unsafe { CStr::from_ptr(val.description).to_string_lossy().to_string() },
}
}
}

impl From<Policy> for account_sdk::account::session::policy::CallPolicy {
impl From<Policy> for crate::types::Policy {
fn from(val: Policy) -> Self {
account_sdk::account::session::policy::CallPolicy {
contract_address: val.target.into(),
selector: get_selector_from_name(&unsafe {
CStr::from_ptr(val.method).to_string_lossy().to_string()
})
.unwrap(),
authorized: Some(true),
match val {
Policy::Call(call) => crate::types::Policy::Call(call.into()),
Policy::TypedData(typed_data) => crate::types::Policy::TypedData(serde_json::from_str(unsafe { CStr::from_ptr(typed_data).to_string_lossy().to_string().as_str() }).unwrap()),
}
}
}

impl From<Policy> for account_sdk::account::session::policy::Policy {
fn from(val: Policy) -> Self {
match val {
Policy::Call(call) => account_sdk::account::session::policy::Policy::Call(call.into()),
Policy::TypedData(typed_data) => account_sdk::account::session::policy::Policy::TypedData(typed_data.into()),
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ use torii_client::client::Client;
use wasm_bindgen::prelude::*;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Policy {
pub enum Policy {
Call(CallPolicy),
TypedData(account_sdk::typed_data::TypedData),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallPolicy {
pub target: Felt,
pub method: String,
pub description: String,
Expand Down
Loading