Skip to content

feat(controller): start generalizing to support redirects for mobile #104

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 7 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
135 changes: 70 additions & 65 deletions dojo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
namespace dojo_bindings {
#endif // __cplusplus

struct ToriiClient;
struct CallbackState;
struct Policy;
struct ControllerAccount;
struct Call;
struct ToriiClient;
struct Controller;
struct Entity;
struct Query;
Expand Down Expand Up @@ -64,26 +65,22 @@ typedef struct Error {
char *message;
} Error;

typedef enum ResultToriiClient_Tag {
OkToriiClient,
ErrToriiClient,
} ResultToriiClient_Tag;
typedef enum Resultbool_Tag {
Okbool,
Errbool,
} Resultbool_Tag;

typedef struct ResultToriiClient {
ResultToriiClient_Tag tag;
typedef struct Resultbool {
Resultbool_Tag tag;
union {
struct {
struct ToriiClient *ok;
bool ok;
};
struct {
struct Error err;
};
};
} ResultToriiClient;

typedef struct FieldElement {
uint8_t data[32];
} FieldElement;
} Resultbool;

typedef enum ResultControllerAccount_Tag {
OkControllerAccount,
Expand All @@ -102,22 +99,9 @@ typedef struct ResultControllerAccount {
};
} ResultControllerAccount;

typedef enum Resultbool_Tag {
Okbool,
Errbool,
} Resultbool_Tag;

typedef struct Resultbool {
Resultbool_Tag tag;
union {
struct {
bool ok;
};
struct {
struct Error err;
};
};
} Resultbool;
typedef struct FieldElement {
uint8_t data[32];
} FieldElement;

typedef enum ResultFieldElement_Tag {
OkFieldElement,
Expand All @@ -136,6 +120,23 @@ typedef struct ResultFieldElement {
};
} ResultFieldElement;

typedef enum ResultToriiClient_Tag {
OkToriiClient,
ErrToriiClient,
} ResultToriiClient_Tag;

typedef struct ResultToriiClient {
ResultToriiClient_Tag tag;
union {
struct {
struct ToriiClient *ok;
};
struct {
struct Error err;
};
};
} ResultToriiClient;

typedef struct CArrayu8 {
uint8_t *data;
uintptr_t data_len;
Expand Down Expand Up @@ -785,31 +786,14 @@ typedef struct EnumOption {
extern "C" {
#endif // __cplusplus

/**
* Creates a new Torii client instance
*
* # Parameters
* * `torii_url` - URL of the Torii server
* * `libp2p_relay_url` - URL of the libp2p relay server
* * `world` - World address as a FieldElement
*
* # Returns
* Result containing pointer to new ToriiClient instance or error
*/
struct ResultToriiClient client_new(const char *torii_url,
const char *libp2p_relay_url,
struct FieldElement world);

/**
* Initiates a connection to establish a new session account
*
* This function:
* 1. Generates a new signing key pair
* 2. Starts a local HTTP server to receive the callback
* 3. Opens the keychain session URL in browser
* 4. Waits for callback with session details
* 5. Creates and stores the session
* 6. Calls the provided callback with the new session account
* 2. If redirect_uri is provided: Returns CallbackState for deep link handling
* 3. If redirect_uri is null: Starts a local HTTP server for callback
* 4. Opens the keychain session URL in browser
*
* # Safety
* This function is marked as unsafe because it:
Expand All @@ -822,25 +806,31 @@ struct ResultToriiClient client_new(const char *torii_url,
* * `policies` - Pointer to array of Policy structs defining session permissions
* * `policies_len` - Length of the policies array
* * `account_callback` - Function pointer called with the new session account when ready
* * `redirect_uri` - Optional pointer to null-terminated string containing the redirect URI. If
* provided, will be used for callback instead of starting a local server.
*
* # Returns
* If redirect_uri is provided, returns pointer to CallbackState that must be used with
* handle_deep_link_callback. If redirect_uri is null, returns null pointer.
*/
struct CallbackState *controller_connect(const char *rpc_url,
const struct Policy *policies,
uintptr_t policies_len,
void (*account_callback)(struct ControllerAccount*),
const char *redirect_uri);

/**
* Handles the deep link callback when app is reopened
*
* # Example
* ```c
* void on_account(SessionAccount* account) {
* // Handle new session account
* }
* # Parameters
* * `callback_data` - Base64 encoded callback data from the deep link
* * `state` - CallbackState pointer returned from controller_connect
*
* controller_connect(
* "https://rpc.example.com",
* policies,
* policies_length,
* on_account
* );
* ```
* # Returns
* Result containing success boolean or error
*/
void controller_connect(const char *rpc_url,
const struct Policy *policies,
uintptr_t policies_len,
void (*account_callback)(struct ControllerAccount*));
struct Resultbool controller_handle_deep_link_callback(const char *callback_data,
struct CallbackState *state);

/**
* Retrieves a stored session account if one exists and is valid
Expand Down Expand Up @@ -955,6 +945,21 @@ struct ResultFieldElement controller_execute_from_outside(struct ControllerAccou
*/
void client_set_logger(struct ToriiClient *client, void (*logger)(const char*));

/**
* Creates a new Torii client instance
*
* # Parameters
* * `torii_url` - URL of the Torii server
* * `libp2p_relay_url` - URL of the libp2p relay server
* * `world` - World address as a FieldElement
*
* # Returns
* Result containing pointer to new ToriiClient instance or error
*/
struct ResultToriiClient client_new(const char *torii_url,
const char *libp2p_relay_url,
struct FieldElement world);

/**
* Publishes a message to the network
*
Expand Down
74 changes: 38 additions & 36 deletions dojo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

namespace dojo_bindings {

struct ToriiClient;
struct CallbackState;
struct Policy;
struct ControllerAccount;
struct Call;
struct ToriiClient;
struct Ty;
struct Query;
struct Subscription;
Expand Down Expand Up @@ -907,28 +908,13 @@ struct EntityKeysClause {

extern "C" {

/// Creates a new Torii client instance
///
/// # Parameters
/// * `torii_url` - URL of the Torii server
/// * `libp2p_relay_url` - URL of the libp2p relay server
/// * `world` - World address as a FieldElement
///
/// # Returns
/// Result containing pointer to new ToriiClient instance or error
Result<ToriiClient*> client_new(const char *torii_url,
const char *libp2p_relay_url,
FieldElement world);

/// Initiates a connection to establish a new session account
///
/// This function:
/// 1. Generates a new signing key pair
/// 2. Starts a local HTTP server to receive the callback
/// 3. Opens the keychain session URL in browser
/// 4. Waits for callback with session details
/// 5. Creates and stores the session
/// 6. Calls the provided callback with the new session account
/// 2. If redirect_uri is provided: Returns CallbackState for deep link handling
/// 3. If redirect_uri is null: Starts a local HTTP server for callback
/// 4. Opens the keychain session URL in browser
///
/// # Safety
/// This function is marked as unsafe because it:
Expand All @@ -941,24 +927,27 @@ Result<ToriiClient*> client_new(const char *torii_url,
/// * `policies` - Pointer to array of Policy structs defining session permissions
/// * `policies_len` - Length of the policies array
/// * `account_callback` - Function pointer called with the new session account when ready
/// * `redirect_uri` - Optional pointer to null-terminated string containing the redirect URI. If
/// provided, will be used for callback instead of starting a local server.
///
/// # Example
/// ```c
/// void on_account(SessionAccount* account) {
/// // Handle new session account
/// }
///
/// controller_connect(
/// "https://rpc.example.com",
/// policies,
/// policies_length,
/// on_account
/// );
/// ```
void controller_connect(const char *rpc_url,
const Policy *policies,
uintptr_t policies_len,
void (*account_callback)(ControllerAccount*));
/// # Returns
/// If redirect_uri is provided, returns pointer to CallbackState that must be used with
/// handle_deep_link_callback. If redirect_uri is null, returns null pointer.
CallbackState *controller_connect(const char *rpc_url,
const Policy *policies,
uintptr_t policies_len,
void (*account_callback)(ControllerAccount*),
const char *redirect_uri);

/// Handles the deep link callback when app is reopened
///
/// # Parameters
/// * `callback_data` - Base64 encoded callback data from the deep link
/// * `state` - CallbackState pointer returned from controller_connect
///
/// # Returns
/// Result containing success boolean or error
Result<bool> controller_handle_deep_link_callback(const char *callback_data, CallbackState *state);

/// Retrieves a stored session account if one exists and is valid
///
Expand Down Expand Up @@ -1055,6 +1044,19 @@ Result<FieldElement> controller_execute_from_outside(ControllerAccount *controll
/// * `logger` - Callback function that takes a C string parameter
void client_set_logger(ToriiClient *client, void (*logger)(const char*));

/// Creates a new Torii client instance
///
/// # Parameters
/// * `torii_url` - URL of the Torii server
/// * `libp2p_relay_url` - URL of the libp2p relay server
/// * `world` - World address as a FieldElement
///
/// # Returns
/// Result containing pointer to new ToriiClient instance or error
Result<ToriiClient*> client_new(const char *torii_url,
const char *libp2p_relay_url,
FieldElement world);

/// Publishes a message to the network
///
/// # Parameters
Expand Down
Loading
Loading