|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +use crate::{merkle_note::MerkleNoteHash, primitives::Scalar, wasm_bindgen_wrapper}; |
| 6 | +use ironfish::witness::WitnessTrait; |
| 7 | +use wasm_bindgen::prelude::*; |
| 8 | + |
| 9 | +wasm_bindgen_wrapper! { |
| 10 | + #[derive(Clone, PartialEq, Eq, Debug)] |
| 11 | + pub struct Witness(ironfish::witness::Witness); |
| 12 | +} |
| 13 | + |
| 14 | +#[wasm_bindgen] |
| 15 | +impl Witness { |
| 16 | + #[wasm_bindgen(constructor)] |
| 17 | + pub fn new(tree_size: usize, root_hash: Scalar, auth_path: Vec<WitnessNode>) -> Self { |
| 18 | + Self(ironfish::witness::Witness { |
| 19 | + tree_size, |
| 20 | + root_hash: root_hash.into(), |
| 21 | + auth_path: auth_path.into_iter().map(WitnessNode::into).collect(), |
| 22 | + }) |
| 23 | + } |
| 24 | + |
| 25 | + #[wasm_bindgen(getter)] |
| 26 | + pub fn tree_size(&self) -> usize { |
| 27 | + self.0.tree_size |
| 28 | + } |
| 29 | + |
| 30 | + #[wasm_bindgen(getter)] |
| 31 | + pub fn root_hash(&self) -> Scalar { |
| 32 | + self.0.root_hash.into() |
| 33 | + } |
| 34 | + |
| 35 | + #[wasm_bindgen(getter)] |
| 36 | + pub fn auth_path(&self) -> Vec<WitnessNode> { |
| 37 | + self.0 |
| 38 | + .auth_path |
| 39 | + .iter() |
| 40 | + .cloned() |
| 41 | + .map(WitnessNode::from) |
| 42 | + .collect() |
| 43 | + } |
| 44 | + |
| 45 | + #[wasm_bindgen] |
| 46 | + pub fn verify(&self, hash: &MerkleNoteHash) -> bool { |
| 47 | + self.0.verify(hash.as_ref()) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +wasm_bindgen_wrapper! { |
| 52 | + #[derive(Clone, PartialEq, Eq, Debug)] |
| 53 | + pub struct WitnessNode(ironfish::witness::WitnessNode<blstrs::Scalar>); |
| 54 | +} |
| 55 | + |
| 56 | +#[wasm_bindgen] |
| 57 | +impl WitnessNode { |
| 58 | + #[wasm_bindgen] |
| 59 | + pub fn left(hash: Scalar) -> Self { |
| 60 | + Self(ironfish::witness::WitnessNode::Left(hash.into())) |
| 61 | + } |
| 62 | + |
| 63 | + #[wasm_bindgen] |
| 64 | + pub fn right(hash: Scalar) -> Self { |
| 65 | + Self(ironfish::witness::WitnessNode::Right(hash.into())) |
| 66 | + } |
| 67 | + |
| 68 | + #[wasm_bindgen(getter)] |
| 69 | + pub fn is_left(&self) -> bool { |
| 70 | + match self.0 { |
| 71 | + ironfish::witness::WitnessNode::Left(_) => true, |
| 72 | + ironfish::witness::WitnessNode::Right(_) => false, |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + #[wasm_bindgen(getter)] |
| 77 | + pub fn is_right(&self) -> bool { |
| 78 | + !self.is_left() |
| 79 | + } |
| 80 | + |
| 81 | + #[wasm_bindgen(getter)] |
| 82 | + pub fn hash(&self) -> Scalar { |
| 83 | + match self.0 { |
| 84 | + ironfish::witness::WitnessNode::Left(ref hash) => hash, |
| 85 | + ironfish::witness::WitnessNode::Right(ref hash) => hash, |
| 86 | + } |
| 87 | + .to_owned() |
| 88 | + .into() |
| 89 | + } |
| 90 | +} |
0 commit comments