From 1dcc549bd5f59290897ce21b40a60c3d7cd4644c Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Wed, 6 Nov 2024 15:48:50 -0600 Subject: [PATCH] feat: add sapling tree anchor function --- librustzcash-wrapper/src/lib.rs | 23 +++++++++++++++++++++++ src/C/Zcash.chs | 7 +++++++ src/ZcashHaskell/Sapling.hs | 6 ++++++ 3 files changed, 36 insertions(+) diff --git a/librustzcash-wrapper/src/lib.rs b/librustzcash-wrapper/src/lib.rs index 903c256..2c1b644 100644 --- a/librustzcash-wrapper/src/lib.rs +++ b/librustzcash-wrapper/src/lib.rs @@ -1660,6 +1660,29 @@ pub extern "C" fn rust_wrapper_read_sapling_commitment_tree_parts( } } +#[no_mangle] +pub extern "C" fn rust_wrapper_read_sapling_tree_anchor( + tree: *const u8, + tree_len: usize, + out: *mut u8, + out_len: &mut usize + ){ + let tree_in: Vec = marshall_from_haskell_var(tree, tree_len, RW); + let tree_reader = Cursor::new(tree_in); + let comm_tree = read_commitment_tree(tree_reader); + match comm_tree { + Ok::, _>(c1) => { + let root = c1.root(); + let h = Hhex { bytes: root.to_bytes().to_vec() }; + marshall_to_haskell_var(&h, out, out_len, RW); + }, + Err(_) => { + 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_frontier( tree: *const u8, diff --git a/src/C/Zcash.chs b/src/C/Zcash.chs index 0bcdf6d..6d915bb 100644 --- a/src/C/Zcash.chs +++ b/src/C/Zcash.chs @@ -276,6 +276,13 @@ import ZcashHaskell.Types -> `()' #} +{# fun unsafe rust_wrapper_read_sapling_tree_anchor as rustWrapperReadSaplingTreeAnchor + { toBorshVar* `BS.ByteString'& + , getVarBuffer `Buffer HexString'& + } + -> `()' +#} + {# fun unsafe rust_wrapper_read_orchard_node as rustWrapperReadOrchardNode { toBorshVar* `BS.ByteString'& , getVarBuffer `Buffer HexString'& diff --git a/src/ZcashHaskell/Sapling.hs b/src/ZcashHaskell/Sapling.hs index f0b743c..87b21f7 100644 --- a/src/ZcashHaskell/Sapling.hs +++ b/src/ZcashHaskell/Sapling.hs @@ -26,6 +26,7 @@ import C.Zcash , rustWrapperReadSaplingFrontier , rustWrapperReadSaplingNode , rustWrapperReadSaplingPosition + , rustWrapperReadSaplingTreeAnchor , rustWrapperReadSaplingTreeParts , rustWrapperReadSaplingWitness , rustWrapperSaplingCheck @@ -231,6 +232,11 @@ getSaplingTreeParts h = withPureBorshVarBuffer $ rustWrapperReadSaplingTreeParts $ toBytes $ sapTree h +getSaplingTreeAnchor :: SaplingCommitmentTree -> HexString +getSaplingTreeAnchor tree = + withPureBorshVarBuffer $ + rustWrapperReadSaplingTreeAnchor $ toBytes $ sapTree tree + getSaplingFrontier :: SaplingCommitmentTree -> Maybe SaplingFrontier getSaplingFrontier tree = if sf_pos updatedTree > 1