diff --git a/dojo.h b/dojo.h index 76e8378..689d9a5 100644 --- a/dojo.h +++ b/dojo.h @@ -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 { diff --git a/dojo.hpp b/dojo.hpp index 4ee6f67..7707d7a 100644 --- a/dojo.hpp +++ b/dojo.hpp @@ -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, diff --git a/dojo.pyx b/dojo.pyx index 6b7cfbd..ab61e31 100644 --- a/dojo.pyx +++ b/dojo.pyx @@ -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; diff --git a/src/c/types.rs b/src/c/types.rs index 7d2b886..b9b7054 100644 --- a/src/c/types.rs +++ b/src/c/types.rs @@ -81,15 +81,32 @@ impl From> for Option { #[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 for crate::types::Policy { - fn from(val: Policy) -> Self { - crate::types::Policy { +impl From 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 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() }, @@ -97,15 +114,20 @@ impl From for crate::types::Policy { } } -impl From for account_sdk::account::session::policy::CallPolicy { +impl From 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 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()), } } } diff --git a/src/types.rs b/src/types.rs index bc49c2f..720ad6e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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,