call_bool_method_with_error_msg (#144)

Co-authored-by: tyshkor <tyshko1@gmail.com>
This commit is contained in:
tyshko-rostyslav 2023-04-10 16:15:16 +02:00 committed by GitHub
parent 2e868d6cbf
commit 672287b77b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -79,6 +79,15 @@ macro_rules! call_with_error_msg {
} }
} }
macro_rules! call_bool_method_with_error_msg {
($instance:expr, $method:ident, $error_msg:expr $(, $arg:expr)*) => {
{
let new_instance: &RLNWrapper = $instance.process();
new_instance.instance.$method($($arg.process()),*).map_err(|err| format!("Msg: {:#?}, Error: {:#?}", $error_msg, err))
}
}
}
trait ProcessArg { trait ProcessArg {
type ReturnType; type ReturnType;
fn process(self) -> Self::ReturnType; fn process(self) -> Self::ReturnType;
@ -294,15 +303,12 @@ pub fn wasm_recover_id_secret(
#[allow(clippy::not_unsafe_ptr_arg_deref)] #[allow(clippy::not_unsafe_ptr_arg_deref)]
#[wasm_bindgen(js_name = verifyRLNProof)] #[wasm_bindgen(js_name = verifyRLNProof)]
pub fn wasm_verify_rln_proof(ctx: *const RLNWrapper, proof: Uint8Array) -> Result<bool, String> { pub fn wasm_verify_rln_proof(ctx: *const RLNWrapper, proof: Uint8Array) -> Result<bool, String> {
let wrapper = unsafe { &*ctx }; call_bool_method_with_error_msg!(
if match wrapper.instance.verify_rln_proof(&proof.to_vec()[..]) { ctx,
Ok(verified) => verified, verify_rln_proof,
Err(_) => return Err("error while verifying rln proof".into()), "error while verifying rln proof".to_string(),
} { &proof.to_vec()[..]
return Ok(true); )
}
Ok(false)
} }
#[allow(clippy::not_unsafe_ptr_arg_deref)] #[allow(clippy::not_unsafe_ptr_arg_deref)]
@ -312,18 +318,13 @@ pub fn wasm_verify_with_roots(
proof: Uint8Array, proof: Uint8Array,
roots: Uint8Array, roots: Uint8Array,
) -> Result<bool, String> { ) -> Result<bool, String> {
let wrapper = unsafe { &*ctx }; call_bool_method_with_error_msg!(
if match wrapper ctx,
.instance verify_with_roots,
.verify_with_roots(&proof.to_vec()[..], &roots.to_vec()[..]) "error while verifying proof with roots".to_string(),
{ &proof.to_vec()[..],
Ok(verified) => verified, &roots.to_vec()[..]
Err(_) => return Err("error while verifying proof with roots".into()), )
} {
return Ok(true);
}
Ok(false)
} }
#[allow(clippy::not_unsafe_ptr_arg_deref)] #[allow(clippy::not_unsafe_ptr_arg_deref)]