From 7bc85dfa1956df4d60a5285c73c46dd4deb9798c Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 27 Apr 2023 15:56:54 -0400 Subject: [PATCH] feat: expose `hash`, `poseidon_hash` and `delete_leaf` --- rln-wasm/Cargo.toml | 2 +- rln-wasm/src/lib.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/rln-wasm/Cargo.toml b/rln-wasm/Cargo.toml index 6b61812..8d4e32e 100644 --- a/rln-wasm/Cargo.toml +++ b/rln-wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rln-wasm" -version = "0.0.7" +version = "0.0.8" edition = "2021" license = "MIT or Apache2" diff --git a/rln-wasm/src/lib.rs b/rln-wasm/src/lib.rs index c765c13..9f79cca 100644 --- a/rln-wasm/src/lib.rs +++ b/rln-wasm/src/lib.rs @@ -7,7 +7,7 @@ use std::vec::Vec; use js_sys::{BigInt as JsBigInt, Object, Uint8Array}; use num_bigint::BigInt; -use rln::public::RLN; +use rln::public::{hash, poseidon_hash, RLN}; use wasm_bindgen::prelude::*; #[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 = 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 = 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 { type 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)] #[wasm_bindgen(js_name = initTreeWithLeaves)] 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 { 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 { + 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 { + fn_call_with_output_and_error_msg!( + poseidon_hash, + "could not generate poseidon hash", + &input.to_vec()[..] + ) +}