mirror of https://github.com/vacp2p/zerokit.git
most changes
This commit is contained in:
parent
1f983bb232
commit
c2cb067333
|
@ -8,6 +8,7 @@ rln/pmtree_db
|
|||
# will have compiled files and executables
|
||||
debug/
|
||||
target/
|
||||
wabt/
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
|
|
@ -20,6 +20,64 @@ pub struct RLNWrapper {
|
|||
instance: RLN<'static>,
|
||||
}
|
||||
|
||||
// Macro to call_with_error_msg methods with arbitrary amount of arguments,
|
||||
// First argument to the macro is context,
|
||||
// second is the actual method on `RLNWrapper`
|
||||
// rest are all other arguments to the method
|
||||
macro_rules! call_with_error_msg {
|
||||
($instance:expr, $method:ident, $error_msg:expr $(, $arg:expr)*) => {
|
||||
{
|
||||
let new_instance: &mut RLNWrapper = $instance.process();
|
||||
if new_instance.instance.$method($($arg.process()),*).is_ok() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err($error_msg)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait ProcessArg {
|
||||
type ReturnType;
|
||||
fn process(self) -> Self::ReturnType;
|
||||
}
|
||||
|
||||
impl ProcessArg for usize {
|
||||
type ReturnType = usize;
|
||||
fn process(self) -> Self::ReturnType {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ProcessArg for *const RLN<'a> {
|
||||
type ReturnType = &'a RLN<'a>;
|
||||
fn process(self) -> Self::ReturnType {
|
||||
unsafe { &*self }
|
||||
}
|
||||
}
|
||||
|
||||
trait ProcessArgRef {
|
||||
type ReturnType;
|
||||
fn process(self) -> Self::ReturnType;
|
||||
}
|
||||
|
||||
impl ProcessArgRef for *mut RLNWrapper {
|
||||
type ReturnType = &'static mut RLNWrapper;
|
||||
|
||||
fn process(self) -> Self::ReturnType {
|
||||
unsafe { &mut *self }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ProcessArgRef for &'a [u8] {
|
||||
type ReturnType = &'a [u8];
|
||||
|
||||
fn process(self) -> Self::ReturnType {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = newRLN)]
|
||||
pub fn wasm_new(
|
||||
|
@ -51,12 +109,7 @@ pub fn wasm_get_serialized_rln_witness(
|
|||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = insertMember)]
|
||||
pub fn wasm_set_next_leaf(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
|
||||
let wrapper = unsafe { &mut *ctx };
|
||||
if wrapper.instance.set_next_leaf(&input.to_vec()[..]).is_ok() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("could not insert member into merkle tree".into())
|
||||
}
|
||||
call_with_error_msg!(ctx, set_next_leaf, "could not insert member into merkle tree".to_string(), &input.to_vec()[..])
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
|
@ -66,31 +119,13 @@ pub fn wasm_set_leaves_from(
|
|||
index: usize,
|
||||
input: Uint8Array,
|
||||
) -> Result<(), String> {
|
||||
let wrapper = unsafe { &mut *ctx };
|
||||
if wrapper
|
||||
.instance
|
||||
.set_leaves_from(index as usize, &input.to_vec()[..])
|
||||
.is_ok()
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err("could not set multiple leaves".into())
|
||||
}
|
||||
call_with_error_msg!(ctx, set_leaves_from, "could not set multiple leaves".to_string(), index, &*input.to_vec())
|
||||
}
|
||||
|
||||
#[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> {
|
||||
let wrapper = unsafe { &mut *ctx };
|
||||
if wrapper
|
||||
.instance
|
||||
.init_tree_with_leaves(&input.to_vec()[..])
|
||||
.is_ok()
|
||||
{
|
||||
Ok(())
|
||||
} else {
|
||||
Err("could not init merkle tree".into())
|
||||
}
|
||||
call_with_error_msg!(ctx, init_tree_with_leaves, "could not init merkle tree".to_string(), &*input.to_vec())
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
|
|
Loading…
Reference in New Issue