Skip to content

Commit dc95e90

Browse files
committed
WASM: add bindings for Witness
1 parent 22ddf34 commit dc95e90

File tree

3 files changed

+92
-9
lines changed

3 files changed

+92
-9
lines changed

ironfish-rust-wasm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub mod merkle_note;
2222
pub mod note;
2323
pub mod primitives;
2424
pub mod transaction;
25+
pub mod witness;
2526

2627
/// Creates a [`wasm_bindgen`] wrapper for an existing type.
2728
///

ironfish-rust-wasm/src/witness.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}

ironfish-rust/src/witness.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,13 @@ pub trait WitnessTrait {
3535

3636
/// A Rust implementation of a WitnessTrait, used for testing Witness-related
3737
/// code within Rust.
38+
#[derive(Clone, PartialEq, Eq)]
3839
pub struct Witness {
3940
pub tree_size: usize,
4041
pub root_hash: Scalar,
4142
pub auth_path: Vec<WitnessNode<Scalar>>,
4243
}
4344

44-
/// Implement partial equality, ignoring the Sapling Arc
45-
impl PartialEq for Witness {
46-
fn eq(&self, other: &Witness) -> bool {
47-
self.tree_size == other.tree_size
48-
&& self.root_hash == other.root_hash
49-
&& self.auth_path == other.auth_path
50-
}
51-
}
52-
5345
impl WitnessTrait for Witness {
5446
fn verify(&self, my_hash: &MerkleNoteHash) -> bool {
5547
let mut cur_hash = my_hash.0;

0 commit comments

Comments
 (0)