Skip to content
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

Return Vec in WASM bindings natively #50

Merged
merged 1 commit into from
Nov 18, 2023
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
2 changes: 1 addition & 1 deletion synedrion-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ synedrion = { path = "../synedrion" }
js-sys = "0.3.55"
getrandom = { version = "0.2", features = ["js"] }
rand_core = { version = "0.6.4", features = ["getrandom"] }
wasm-bindgen = "0.2.85"
wasm-bindgen = "0.2.88"
wasm-bindgen-derive = "0.2"

[dev-dependencies]
Expand Down
15 changes: 3 additions & 12 deletions synedrion-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bincode::{
};
use js_sys::Error;
use rand_core::OsRng;
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
use wasm_bindgen_derive::TryFromJsValue;

use synedrion::TestParams;
Expand All @@ -19,9 +19,6 @@ const MAX_MSG_LEN: u64 = 1000 * 1000; // 1 MB

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "KeyShare[]")]
pub type KeyShareArray;

#[wasm_bindgen(typescript_type = "SigningKey | undefined")]
pub type OptionalSigningKey;
}
Expand Down Expand Up @@ -66,7 +63,7 @@ impl KeyShare {
pub fn new_centralized(
num_parties: usize,
signing_key: &OptionalSigningKey,
) -> Result<KeyShareArray, Error> {
) -> Result<Vec<KeyShare>, Error> {
let sk_js: &JsValue = signing_key.as_ref();
let typed_sk: Option<SigningKey> = if sk_js.is_undefined() {
None
Expand All @@ -81,13 +78,7 @@ impl KeyShare {
num_parties,
backend_sk.as_ref(),
);
Ok(shares
.into_vec()
.into_iter()
.map(KeyShare)
.map(JsValue::from)
.collect::<js_sys::Array>()
.unchecked_into::<KeyShareArray>())
Ok(shares.into_vec().into_iter().map(KeyShare).collect())
}
}

Expand Down
15 changes: 1 addition & 14 deletions synedrion-wasm/tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ use wasm_bindgen_test::wasm_bindgen_test;

use synedrion_wasm::{KeyShare, SigningKey};

fn try_from_js_array<T>(val: impl Into<JsValue>) -> Vec<T>
where
for<'a> T: TryFrom<&'a JsValue>,
for<'a> <T as TryFrom<&'a JsValue>>::Error: core::fmt::Debug,
{
let js_array: js_sys::Array = val.into().dyn_into().unwrap();
js_array
.iter()
.map(|js| T::try_from(&js).unwrap())
.collect()
}

fn into_js_option<T, U>(val: Option<U>) -> T
where
JsValue: From<U>,
Expand All @@ -30,8 +18,7 @@ where
#[wasm_bindgen_test]
fn test_make_key_shares() {
let sk: Option<SigningKey> = None;
let shares_js = KeyShare::new_centralized(3, &into_js_option(sk)).unwrap();
let shares = try_from_js_array::<KeyShare>(shares_js);
let shares: Vec<KeyShare> = KeyShare::new_centralized(3, &into_js_option(sk)).unwrap();
let _shares_serialized = shares
.iter()
.map(|share| share.to_bytes())
Expand Down
Loading