From aa10e09595549ad3dd07a08ba459cac5a59350ae Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Thu, 21 Mar 2024 20:15:49 +0000 Subject: [PATCH 1/2] Implement Sapling spends (#42) This PR add support for Sapling spends in the communication with Zebra. Reviewed-on: https://git.vergara.tech/Vergara_Tech/zcash-haskell/pulls/42 Co-authored-by: Rene Vergara Co-committed-by: Rene Vergara --- CHANGELOG.md | 4 ++++ src/ZcashHaskell/Sapling.hs | 6 ++++-- src/ZcashHaskell/Types.hs | 25 +++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fef5e74..999e019 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.5.1.0] +### Added + +- Functionality to capture Sapling Spends + ### Changed - Modified the `makeZebraCall` function to handle errors explicitly diff --git a/src/ZcashHaskell/Sapling.hs b/src/ZcashHaskell/Sapling.hs index fb1c459..2dc7f49 100644 --- a/src/ZcashHaskell/Sapling.hs +++ b/src/ZcashHaskell/Sapling.hs @@ -90,11 +90,13 @@ instance FromJSON RawTxResponse where ht <- obj .: "height" c <- obj .: "confirmations" b <- obj .: "blocktime" + sSpend <- obj .: "vShieldedSpend" case o of - Nothing -> pure $ RawTxResponse i h (getShieldedOutputs h) [] ht c b + Nothing -> + pure $ RawTxResponse i h sSpend (getShieldedOutputs h) [] ht c b Just o' -> do a <- o' .: "actions" - pure $ RawTxResponse i h (getShieldedOutputs h) a ht c b + pure $ RawTxResponse i h sSpend (getShieldedOutputs h) a ht c b -- | Attempts to obtain a sapling SpendingKey using a HDSeed genSaplingSpendingKey :: Seed -> CoinType -> Int -> Maybe SaplingSpendingKey diff --git a/src/ZcashHaskell/Types.hs b/src/ZcashHaskell/Types.hs index 182ddc1..e7f68b3 100644 --- a/src/ZcashHaskell/Types.hs +++ b/src/ZcashHaskell/Types.hs @@ -207,6 +207,7 @@ instance FromJSON BlockResponse where data RawTxResponse = RawTxResponse { rt_id :: !HexString , rt_hex :: !HexString + , rt_shieldedSpends :: ![ShieldedSpend] , rt_shieldedOutputs :: ![BS.ByteString] , rt_orchardActions :: ![OrchardAction] , rt_blockheight :: !Integer @@ -283,6 +284,30 @@ newtype SaplingReceiver = instance ToBytes SaplingReceiver where getBytes (SaplingReceiver s) = s +-- | Type to represent a Sapling Shielded Spend as provided by the @getrawtransaction@ RPC method +data ShieldedSpend = ShieldedSpend + { sp_cv :: !HexString + , sp_anchor :: !HexString + , sp_nullifier :: !HexString + , sp_rk :: !HexString + , sp_proof :: !HexString + , sp_auth :: !HexString + } deriving stock (Eq, Prelude.Show, GHC.Generic, Read) + deriving anyclass (SOP.Generic, SOP.HasDatatypeInfo) + deriving anyclass (Data.Structured.Show) + deriving (BorshSize, ToBorsh, FromBorsh) via AsStruct ShieldedSpend + +instance FromJSON ShieldedSpend where + parseJSON = + withObject "ShieldedSpend" $ \obj -> do + cv <- obj .: "cv" + anchor <- obj .: "anchor" + nullifier <- obj .: "nullifier" + rk <- obj .: "rk" + p <- obj .: "proof" + sig <- obj .: "spendAuthSig" + pure $ ShieldedSpend cv anchor nullifier rk p sig + -- | Type to represent a Sapling Shielded Output as provided by the @getrawtransaction@ RPC method of @zcashd@. data ShieldedOutput = ShieldedOutput { s_cv :: !HexString -- ^ Value commitment to the input note From f0995441628381fee14ae1c655c3c4f8d96162e5 Mon Sep 17 00:00:00 2001 From: pitmutt Date: Fri, 22 Mar 2024 18:04:04 +0000 Subject: [PATCH 2/2] Account for missing response fields in Zebra response (#43) This PR adds code to account for the missing `time` field in the Zebra response for blocks and raw transactions. Reviewed-on: https://git.vergara.tech/Vergara_Tech/zcash-haskell/pulls/43 Co-authored-by: pitmutt Co-committed-by: pitmutt --- src/ZcashHaskell/Types.hs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ZcashHaskell/Types.hs b/src/ZcashHaskell/Types.hs index e7f68b3..612410e 100644 --- a/src/ZcashHaskell/Types.hs +++ b/src/ZcashHaskell/Types.hs @@ -7,7 +7,7 @@ -- Copyright : 2022-2024 Vergara Technologies -- License : MIT -- --- Maintainer : pitmut@vergara.tech +-- Maintainer : pitmutt@vergara.tech -- Stability : experimental -- Portability : unknown -- @@ -31,6 +31,7 @@ import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as C import Data.HexString import Data.Int +import Data.Maybe (fromMaybe) import Data.Structured import qualified Data.Text as T import qualified Data.Text.Encoding as E @@ -199,9 +200,9 @@ instance FromJSON BlockResponse where withObject "BlockResponse" $ \obj -> do c <- obj .: "confirmations" h <- obj .: "height" - t <- obj .: "time" + t <- obj .:? "time" txs <- obj .: "tx" - pure $ BlockResponse c h t txs + pure $ BlockResponse c h (fromMaybe 0 t) txs -- | Type to represent response from the `zcashd` RPC `getrawtransaction` data RawTxResponse = RawTxResponse