ergo_lib_wasm/
batchmerkleproof.rs

1//! BatchMerkleProof (compact Merkle multi-proofs)
2use ergo_lib::ergo_merkle_tree;
3use gloo_utils::format::JsValueSerdeExt;
4use serde::{Deserialize, Serialize};
5use wasm_bindgen::prelude::*;
6
7/// BatchMerkleProof type to validate root hash for multiple nodes
8#[wasm_bindgen]
9#[derive(Serialize, Deserialize)]
10pub struct BatchMerkleProof(pub(crate) ergo_merkle_tree::BatchMerkleProof);
11
12#[wasm_bindgen]
13impl BatchMerkleProof {
14    /// Creates a new [`BatchMerkleProof`] from json representation
15    pub fn from_json(json: &JsValue) -> Result<BatchMerkleProof, String> {
16        JsValueSerdeExt::into_serde(json).map_err(|err| err.to_string())
17    }
18    /// Converts [`BatchMerkleProof`] to json representation
19    pub fn to_json(&self) -> Result<JsValue, String> {
20        <JsValue as JsValueSerdeExt>::from_serde(&self).map_err(|err| err.to_string())
21    }
22
23    /// Calculates root hash for [`BatchMerkleProof`] and compares it against expected root hash
24    pub fn valid(&self, expected_root: &[u8]) -> bool {
25        self.0.valid(expected_root)
26    }
27}