Skip to content
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
91 changes: 78 additions & 13 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ futures = "0.3.30"
futures-channel = "0.3.30"
wasm-bindgen = "0.2.92"
stream-cancel = "0.8.2"
cainome ={ git = "https://github.com/cartridge-gg/cainome" }

# WASM
[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
55 changes: 38 additions & 17 deletions dojo.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,40 @@ typedef struct ResultSubscription {
};
} ResultSubscription;

typedef enum ResultCArrayFieldElement_Tag {
OkCArrayFieldElement,
ErrCArrayFieldElement,
} ResultCArrayFieldElement_Tag;

typedef struct ResultCArrayFieldElement {
ResultCArrayFieldElement_Tag tag;
union {
struct {
struct CArrayFieldElement ok;
};
struct {
struct Error err;
};
};
} ResultCArrayFieldElement;

typedef enum Resultc_char_Tag {
Okc_char,
Errc_char,
} Resultc_char_Tag;

typedef struct Resultc_char {
Resultc_char_Tag tag;
union {
struct {
const char *ok;
};
struct {
struct Error err;
};
};
} Resultc_char;

typedef enum ResultFieldElement_Tag {
OkFieldElement,
ErrFieldElement,
Expand Down Expand Up @@ -532,23 +566,6 @@ typedef struct ResultAccount {
};
} ResultAccount;

typedef enum ResultCArrayFieldElement_Tag {
OkCArrayFieldElement,
ErrCArrayFieldElement,
} ResultCArrayFieldElement_Tag;

typedef struct ResultCArrayFieldElement {
ResultCArrayFieldElement_Tag tag;
union {
struct {
struct CArrayFieldElement ok;
};
struct {
struct Error err;
};
};
} ResultCArrayFieldElement;

typedef struct Call {
const char *to;
const char *selector;
Expand Down Expand Up @@ -629,6 +646,10 @@ struct Resultbool client_remove_models_to_sync(struct ToriiClient *client,
const struct KeysClause *models,
uintptr_t models_len);

struct ResultCArrayFieldElement bytearray_serialize(const char *str);

struct Resultc_char bytearray_deserialize(const struct FieldElement *felts, uintptr_t felts_len);

struct ResultFieldElement typed_data_encode(const char *typed_data, struct FieldElement address);

struct FieldElement signing_key_new(void);
Expand Down
4 changes: 4 additions & 0 deletions dojo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,10 @@ Result<bool> client_remove_models_to_sync(ToriiClient *client,
const KeysClause *models,
uintptr_t models_len);

Result<CArray<FieldElement>> bytearray_serialize(const char *str);

Result<const char*> bytearray_deserialize(const FieldElement *felts, uintptr_t felts_len);

Result<FieldElement> typed_data_encode(const char *typed_data, FieldElement address);

FieldElement signing_key_new();
Expand Down
44 changes: 44 additions & 0 deletions src/c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use self::types::{
use crate::constants;
use crate::types::{Account, Provider, Subscription};
use crate::utils::watch_tx;
use cainome::cairo_serde::{self, ByteArray, CairoSerde};
use starknet::accounts::{Account as StarknetAccount, ExecutionEncoding, SingleOwnerAccount};
use starknet::core::types::FunctionCall;
use starknet::core::utils::{
Expand Down Expand Up @@ -329,6 +330,49 @@ pub unsafe extern "C" fn client_remove_models_to_sync(
}
}

#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn bytearray_serialize(
str: *const c_char,
) -> Result<CArray<types::FieldElement>> {
let str = unsafe { CStr::from_ptr(str).to_string_lossy().into_owned() };
let bytearray = match ByteArray::from_string(str.as_str()) {
Ok(bytearray) => bytearray,
Err(e) => return Result::Err(e.into()),
};

let felts = cairo_serde::ByteArray::cairo_serialize(&bytearray);
let felts = felts
.iter()
.map(|f| f.into())
.collect::<Vec<types::FieldElement>>();
Result::Ok(felts.into())
}

#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn bytearray_deserialize(
felts: *const types::FieldElement,
felts_len: usize,
) -> Result<*const c_char> {
let felts = unsafe { std::slice::from_raw_parts(felts, felts_len) };
let felts = felts
.iter()
.map(|f| (&f.clone()).into())
.collect::<Vec<FieldElement>>();
let bytearray = match cairo_serde::ByteArray::cairo_deserialize(&felts, 0) {
Ok(bytearray) => bytearray,
Err(e) => return Result::Err(e.into()),
};

let bytearray = match bytearray.to_string() {
Ok(bytearray) => bytearray,
Err(e) => return Result::Err(e.into()),
};

Result::Ok(CString::new(bytearray).unwrap().into_raw())
}

#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn typed_data_encode(
Expand Down
35 changes: 35 additions & 0 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod utils;
use std::str::FromStr;
use std::sync::Arc;

use cainome::cairo_serde::{self, ByteArray, CairoSerde};
use crypto_bigint::U256;
use futures::StreamExt;
use js_sys::Array;
Expand Down Expand Up @@ -658,6 +659,40 @@ pub fn hash_get_contract_address(
Ok(format!("{:#x}", address))
}

#[wasm_bindgen(js_name = byteArraySerialize)]
pub fn bytearray_serialize(str: &str) -> Result<Vec<String>, JsValue> {
let bytearray = match ByteArray::from_string(str) {
Ok(bytearray) => bytearray,
Err(e) => return Err(JsValue::from(format!("failed to parse bytearray: {e}"))),
};
let felts = cairo_serde::ByteArray::cairo_serialize(&bytearray);

Ok(felts.iter().map(|f| format!("{:#x}", f)).collect())
}

#[wasm_bindgen(js_name = byteArrayDeserialize)]
pub fn bytearray_deserialize(felts: Vec<String>) -> Result<String, JsValue> {
let felts = felts
.into_iter()
.map(|f| FieldElement::from_str(f.as_str()))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| JsValue::from(format!("failed to parse felts: {e}")))?;

let bytearray = match cairo_serde::ByteArray::cairo_deserialize(&felts, 0) {
Ok(bytearray) => bytearray,
Err(e) => {
return Err(JsValue::from(format!(
"failed to deserialize bytearray: {e}"
)))
}
};

match bytearray.to_string() {
Ok(s) => Ok(s),
Err(e) => Err(JsValue::from(format!("failed to serialize bytearray: {e}"))),
}
}

#[wasm_bindgen]
impl Client {
#[wasm_bindgen(js_name = getEntities)]
Expand Down