From 96ee5f417944b1f1719b5613d2d7cdb9a6f8160b Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Wed, 30 Oct 2024 15:24:06 -0500 Subject: [PATCH] feat: new function to test tree with one node --- librustzcash-wrapper/src/lib.rs | 22 ++++++++++++++++++++++ src/C/Zcash.chs | 8 ++++++++ src/ZcashHaskell/Orchard.hs | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/librustzcash-wrapper/src/lib.rs b/librustzcash-wrapper/src/lib.rs index d011e65..3b060fd 100644 --- a/librustzcash-wrapper/src/lib.rs +++ b/librustzcash-wrapper/src/lib.rs @@ -1631,6 +1631,28 @@ pub extern "C" fn rust_wrapper_get_orchard_root( marshall_to_haskell_var(&h, out, out_len, RW); } +#[no_mangle] +pub extern "C" fn rust_wrapper_orchard_add_node( + node: *const u8, + node_len: usize, + out: *mut u8, + out_len: &mut usize + ){ + let mut tree: CommitmentTree = CommitmentTree::empty(); + let node_in: Vec = marshall_from_haskell_var(node, node_len, RW); + let orchard_note_comm = ExtractedNoteCommitment::from_bytes(&to_array(node_in)); + if orchard_note_comm.is_some().into() { + let n = MerkleHashOrchard::from_cmx(&orchard_note_comm.unwrap()); + tree.append(n); + let root = tree.root(); + let h = Hhex { bytes: root.to_bytes().to_vec() }; + marshall_to_haskell_var(&h, out, out_len, RW); + } else { + let h0 = Hhex { bytes: vec![0] }; + marshall_to_haskell_var(&h0, out, out_len, RW); + } +} + #[no_mangle] pub extern "C" fn rust_wrapper_read_orchard_commitment_tree( tree: *const u8, diff --git a/src/C/Zcash.chs b/src/C/Zcash.chs index abb7435..915058f 100644 --- a/src/C/Zcash.chs +++ b/src/C/Zcash.chs @@ -311,6 +311,14 @@ import ZcashHaskell.Types -> `Word64' #} +{# fun unsafe rust_wrapper_orchard_add_node as rustWrapperOrchardAddNodeTest + { toBorshVar* `BS.ByteString'& + , getVarBuffer `Buffer HexString'& + } + -> `()' + +#} + {# fun unsafe rust_wrapper_update_sapling_witness as rustWrapperUpdateSaplingWitness { toBorshVar* `BS.ByteString'& , toBorshVar* `[BS.ByteString]'& diff --git a/src/ZcashHaskell/Orchard.hs b/src/ZcashHaskell/Orchard.hs index 4cb8b9e..7e30158 100644 --- a/src/ZcashHaskell/Orchard.hs +++ b/src/ZcashHaskell/Orchard.hs @@ -22,6 +22,7 @@ import C.Zcash , rustWrapperGenOrchardReceiver , rustWrapperGenOrchardSpendKey , rustWrapperGetOrchardRootTest + , rustWrapperOrchardAddNodeTest , rustWrapperOrchardCheck , rustWrapperOrchardNoteDecode , rustWrapperOrchardNoteDecodeSK @@ -223,6 +224,10 @@ getOrchardRootTest :: Int -> HexString getOrchardRootTest level = withPureBorshVarBuffer $ rustWrapperGetOrchardRootTest $ fromIntegral level +addOrchardNodeGetRoot :: BS.ByteString -> HexString +addOrchardNodeGetRoot n = + withPureBorshVarBuffer $ rustWrapperOrchardAddNodeTest n + -- | Update a Orchard commitment tree updateOrchardCommitmentTree :: OrchardFrontier -- ^ the base tree