mirror of https://github.com/vacp2p/zerokit.git
feat(rln-wasm): set/get metadata api (#179)
* feat(rln-wasm): set/get metadata api * fix(rln): imports
This commit is contained in:
parent
2793fe0e24
commit
8cfd83de54
|
@ -236,6 +236,27 @@ 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)
|
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 = setMetadata)]
|
||||||
|
pub fn wasm_set_metadata(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
|
||||||
|
call_with_error_msg!(
|
||||||
|
ctx,
|
||||||
|
set_metadata,
|
||||||
|
"could not set metadata".to_string(),
|
||||||
|
&*input.to_vec()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
|
#[wasm_bindgen(js_name = getMetadata)]
|
||||||
|
pub fn wasm_get_metadata(ctx: *mut RLNWrapper) -> Result<Uint8Array, String> {
|
||||||
|
call_with_output_and_error_msg!(
|
||||||
|
ctx,
|
||||||
|
get_metadata,
|
||||||
|
"could not get metadata".to_string()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[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> {
|
||||||
|
|
|
@ -6,8 +6,7 @@ mod tests {
|
||||||
use rln::circuit::TEST_TREE_HEIGHT;
|
use rln::circuit::TEST_TREE_HEIGHT;
|
||||||
use rln::utils::normalize_usize;
|
use rln::utils::normalize_usize;
|
||||||
use rln_wasm::*;
|
use rln_wasm::*;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::{prelude::*, JsValue};
|
||||||
use wasm_bindgen::JsValue;
|
|
||||||
use wasm_bindgen_test::wasm_bindgen_test;
|
use wasm_bindgen_test::wasm_bindgen_test;
|
||||||
|
|
||||||
#[wasm_bindgen(module = "src/utils.js")]
|
#[wasm_bindgen(module = "src/utils.js")]
|
||||||
|
@ -109,4 +108,28 @@ mod tests {
|
||||||
let is_proof_valid = wasm_verify_with_roots(rln_instance, proof_with_signal, roots);
|
let is_proof_valid = wasm_verify_with_roots(rln_instance, proof_with_signal, roots);
|
||||||
assert!(is_proof_valid.unwrap(), "verifying proof with roots failed");
|
assert!(is_proof_valid.unwrap(), "verifying proof with roots failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen_test]
|
||||||
|
fn test_metadata() {
|
||||||
|
let tree_height = TEST_TREE_HEIGHT;
|
||||||
|
let circom_path = format!("../rln/resources/tree_height_{TEST_TREE_HEIGHT}/rln.wasm");
|
||||||
|
let zkey_path = format!("../rln/resources/tree_height_{TEST_TREE_HEIGHT}/rln_final.zkey");
|
||||||
|
let vk_path =
|
||||||
|
format!("../rln/resources/tree_height_{TEST_TREE_HEIGHT}/verification_key.json");
|
||||||
|
let zkey = read_file(&zkey_path).unwrap();
|
||||||
|
let vk = read_file(&vk_path).unwrap();
|
||||||
|
|
||||||
|
// Creating an instance of RLN
|
||||||
|
let rln_instance = wasm_new(tree_height, zkey, vk).unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
let test_metadata = Uint8Array::new(&JsValue::from_str("test"));
|
||||||
|
// Inserting random metadata
|
||||||
|
wasm_set_metadata(rln_instance, test_metadata.clone()).unwrap();
|
||||||
|
|
||||||
|
// Getting metadata
|
||||||
|
let metadata = wasm_get_metadata(rln_instance).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(metadata.to_vec(), test_metadata.to_vec());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,10 +12,8 @@ use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read, Write};
|
||||||
use cfg_if::cfg_if;
|
use cfg_if::cfg_if;
|
||||||
use color_eyre::{Report, Result};
|
use color_eyre::{Report, Result};
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
use serde_json::{json, Value};
|
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::str::FromStr;
|
use utils::{ZerokitMerkleProof, ZerokitMerkleTree};
|
||||||
use utils::{Hasher, ZerokitMerkleProof, ZerokitMerkleTree};
|
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(not(target_arch = "wasm32"))] {
|
if #[cfg(not(target_arch = "wasm32"))] {
|
||||||
|
@ -23,6 +21,9 @@ cfg_if! {
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use crate::circuit::{circom_from_folder, vk_from_folder, circom_from_raw, zkey_from_folder, TEST_RESOURCES_FOLDER, TEST_TREE_HEIGHT};
|
use crate::circuit::{circom_from_folder, vk_from_folder, circom_from_raw, zkey_from_folder, TEST_RESOURCES_FOLDER, TEST_TREE_HEIGHT};
|
||||||
use ark_circom::WitnessCalculator;
|
use ark_circom::WitnessCalculator;
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use utils::{Hasher};
|
||||||
|
use std::str::FromStr;
|
||||||
} else {
|
} else {
|
||||||
use std::marker::*;
|
use std::marker::*;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue