From 8d1f5b1e86bba6c46e5a380f4fc830c274be7a76 Mon Sep 17 00:00:00 2001 From: andrea Date: Wed, 27 Nov 2024 12:37:19 -0800 Subject: [PATCH] WASM: add decryption methods to MerkleNote --- ironfish-rust-wasm/src/merkle_note.rs | 98 ++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/ironfish-rust-wasm/src/merkle_note.rs b/ironfish-rust-wasm/src/merkle_note.rs index b19e5e30e5..60185d9e50 100644 --- a/ironfish-rust-wasm/src/merkle_note.rs +++ b/ironfish-rust-wasm/src/merkle_note.rs @@ -2,7 +2,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use crate::{errors::IronfishError, primitives::Scalar, wasm_bindgen_wrapper}; +use crate::{ + errors::IronfishError, + keys::{IncomingViewKey, OutgoingViewKey}, + note::Note, + primitives::Scalar, + wasm_bindgen_wrapper, +}; use wasm_bindgen::prelude::*; wasm_bindgen_wrapper! { @@ -30,6 +36,50 @@ impl MerkleNote { pub fn merkle_hash(&self) -> MerkleNoteHash { self.0.merkle_hash().into() } + + #[wasm_bindgen(js_name = decryptNoteForOwner)] + pub fn decrypt_note_for_owner( + &self, + owner_view_key: &IncomingViewKey, + ) -> Result { + self.0 + .decrypt_note_for_owner(owner_view_key.as_ref()) + .map(|n| n.into()) + .map_err(|e| e.into()) + } + + #[wasm_bindgen(js_name = decryptNoteForOwners)] + pub fn decrypt_note_for_owners(&self, owner_view_keys: Vec) -> Vec { + // The original `decrypt_note_for_owners` returns a `Vec>`. Here instead we + // are filtering out all errors. This likely makes this method hard to use in practice, + // because the information for mapping between the original owner and the resulting note is + // lost. However, returing a `Vec` or a `Vec