use js_sys::Uint8Array;
use wasm_bindgen::prelude::*;
fn map_err<E: std::fmt::Display>(e: E) -> JsValue {
JsValue::from_str(&e.to_string())
}
#[wasm_bindgen]
pub struct Cmp20Signer;
#[wasm_bindgen]
impl Cmp20Signer {
#[wasm_bindgen]
pub fn keygen(&self, threshold: u32, party_count: u32) -> Result<JsValue, JsValue> {
let kg = confium_tc_cmp20::inprocess::keygen(threshold, party_count as usize)
.map_err(map_err)?;
let obj = js_sys::Object::new();
let shares = js_sys::Array::new();
for s in kg.shares {
shares.push(&Uint8Array::from(s.as_slice()));
}
js_sys::Reflect::set(&obj, &"shares".into(), &shares)?;
js_sys::Reflect::set(
&obj,
&"publicKey".into(),
&Uint8Array::from(kg.public_key.as_slice()),
)?;
Ok(obj.into())
}
#[wasm_bindgen]
pub fn sign(
&self,
shares: &js_sys::Array,
threshold: u32,
message: &[u8],
) -> Result<Uint8Array, JsValue> {
let mut blobs: Vec<Vec<u8>> = Vec::with_capacity(shares.length() as usize);
for i in 0..shares.length() {
let buf: Uint8Array = shares.get(i).into();
blobs.push(buf.to_vec());
}
let sig = confium_tc_cmp20::inprocess::sign(&blobs, threshold, message).map_err(map_err)?;
Ok(Uint8Array::from(sig.as_slice()))
}
}
impl Default for Cmp20Signer {
fn default() -> Self {
Self
}
}
#[wasm_bindgen]
pub struct Gg18Signer;
#[wasm_bindgen]
impl Gg18Signer {
#[wasm_bindgen]
pub fn keygen(&self, threshold: u32, party_count: u32) -> Result<JsValue, JsValue> {
let kg =
confium_tc_gg18::inprocess::keygen(threshold, party_count as usize).map_err(map_err)?;
let obj = js_sys::Object::new();
let shares = js_sys::Array::new();
for s in kg.shares {
shares.push(&Uint8Array::from(s.as_slice()));
}
js_sys::Reflect::set(&obj, &"shares".into(), &shares)?;
js_sys::Reflect::set(
&obj,
&"publicKey".into(),
&Uint8Array::from(kg.public_key.as_slice()),
)?;
Ok(obj.into())
}
#[wasm_bindgen]
pub fn sign(
&self,
shares: &js_sys::Array,
threshold: u32,
message: &[u8],
) -> Result<Uint8Array, JsValue> {
let mut blobs: Vec<Vec<u8>> = Vec::with_capacity(shares.length() as usize);
for i in 0..shares.length() {
let buf: Uint8Array = shares.get(i).into();
blobs.push(buf.to_vec());
}
let sig = confium_tc_gg18::inprocess::sign(&blobs, threshold, message).map_err(map_err)?;
Ok(Uint8Array::from(sig.as_slice()))
}
}
impl Default for Gg18Signer {
fn default() -> Self {
Self
}
}