feat: expose `hash`, `poseidon_hash` and `delete_leaf`

This commit is contained in:
Richard Ramos 2023-04-27 15:56:54 -04:00
parent 4f98fd8028
commit 7bc85dfa19
No known key found for this signature in database
GPG Key ID: 1CE87DB518195760
2 changed files with 56 additions and 2 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rln-wasm" name = "rln-wasm"
version = "0.0.7" version = "0.0.8"
edition = "2021" edition = "2021"
license = "MIT or Apache2" license = "MIT or Apache2"

View File

@ -7,7 +7,7 @@ use std::vec::Vec;
use js_sys::{BigInt as JsBigInt, Object, Uint8Array}; use js_sys::{BigInt as JsBigInt, Object, Uint8Array};
use num_bigint::BigInt; use num_bigint::BigInt;
use rln::public::RLN; use rln::public::{hash, poseidon_hash, RLN};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
#[wasm_bindgen] #[wasm_bindgen]
@ -97,6 +97,40 @@ macro_rules! call_bool_method_with_error_msg {
} }
} }
// Macro to execute a function with arbitrary amount of arguments,
// First argument is the function to execute
// Rest are all other arguments to the method
macro_rules! fn_call_with_output_and_error_msg {
// this variant is needed for the case when
// there are zero other arguments
($func:ident, $error_msg:expr) => {
{
let mut output_data: Vec<u8> = Vec::new();
if let Err(err) = $func(&mut output_data) {
std::mem::forget(output_data);
Err(format!("Msg: {:#?}, Error: {:#?}", $error_msg, err))
} else {
let result = Uint8Array::from(&output_data[..]);
std::mem::forget(output_data);
Ok(result)
}
}
};
($func:ident, $error_msg:expr, $( $arg:expr ),* ) => {
{
let mut output_data: Vec<u8> = Vec::new();
if let Err(err) = $func($($arg.process()),*, &mut output_data) {
std::mem::forget(output_data);
Err(format!("Msg: {:#?}, Error: {:#?}", $error_msg, err))
} else {
let result = Uint8Array::from(&output_data[..]);
std::mem::forget(output_data);
Ok(result)
}
}
};
}
trait ProcessArg { trait ProcessArg {
type ReturnType; type ReturnType;
fn process(self) -> Self::ReturnType; fn process(self) -> Self::ReturnType;
@ -196,6 +230,12 @@ pub fn wasm_set_leaves_from(
) )
} }
#[allow(clippy::not_unsafe_ptr_arg_deref)]
#[wasm_bindgen(js_name = deleteLeaf)]
pub fn wasm_delete_leaf(ctx: *mut RLNWrapper, index: usize) -> Result<(), String> {
call_with_error_msg!(ctx, delete_leaf, "could not delete leaf".to_string(), index)
}
#[allow(clippy::not_unsafe_ptr_arg_deref)] #[allow(clippy::not_unsafe_ptr_arg_deref)]
#[wasm_bindgen(js_name = initTreeWithLeaves)] #[wasm_bindgen(js_name = initTreeWithLeaves)]
pub fn wasm_init_tree_with_leaves(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> { pub fn wasm_init_tree_with_leaves(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
@ -333,3 +373,17 @@ pub fn wasm_verify_with_roots(
pub fn wasm_get_root(ctx: *const RLNWrapper) -> Result<Uint8Array, String> { pub fn wasm_get_root(ctx: *const RLNWrapper) -> Result<Uint8Array, String> {
call_with_output_and_error_msg!(ctx, get_root, "could not obtain root") call_with_output_and_error_msg!(ctx, get_root, "could not obtain root")
} }
#[wasm_bindgen(js_name = hash)]
pub fn wasm_hash(input: Uint8Array) -> Result<Uint8Array, String> {
fn_call_with_output_and_error_msg!(hash, "could not generate hash", &input.to_vec()[..])
}
#[wasm_bindgen(js_name = poseidonHash)]
pub fn wasm_poseidon_hash(input: Uint8Array) -> Result<Uint8Array, String> {
fn_call_with_output_and_error_msg!(
poseidon_hash,
"could not generate poseidon hash",
&input.to_vec()[..]
)
}