Update raw Tx parsing to extract shielded outputs

This commit is contained in:
Rene Vergara 2023-09-27 11:18:00 -05:00
parent 489d3d632f
commit d78c269d96
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
3 changed files with 29 additions and 15 deletions

View file

@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- `makeZcashCall` function moved into this library
- `RpcResponse`, `RpcCall` types moved into this library
- Functions to decode Sapling transactions - Functions to decode Sapling transactions
- Tests for Sapling decoding - Tests for Sapling decoding
- Type for block response - Type for block response

View file

@ -1,3 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}
module ZcashHaskell.Sapling where module ZcashHaskell.Sapling where
import C.Zcash import C.Zcash
@ -7,14 +9,23 @@ import C.Zcash
, rustWrapperSaplingVkDecode , rustWrapperSaplingVkDecode
, rustWrapperTxParse , rustWrapperTxParse
) )
import Data.Aeson
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
import Foreign.Rust.Marshall.Variable (withPureBorshVarBuffer) import Foreign.Rust.Marshall.Variable (withPureBorshVarBuffer)
import ZcashHaskell.Types (DecodedNote(..), ShieldedOutput(..)) import ZcashHaskell.Types
( DecodedNote(..)
, RawTxResponse(..)
, ShieldedOutput(..)
, decodeHexText
)
-- | Check if given bytesting is a valid encoded shielded address -- | Check if given bytesting is a valid encoded shielded address
isValidShieldedAddress :: BS.ByteString -> Bool isValidShieldedAddress :: BS.ByteString -> Bool
isValidShieldedAddress = rustWrapperIsShielded isValidShieldedAddress = rustWrapperIsShielded
getShieldedOutputs :: BS.ByteString -> [BS.ByteString]
getShieldedOutputs t = withPureBorshVarBuffer $ rustWrapperTxParse t
-- | Check if given bytestring is a valid Sapling viewing key -- | Check if given bytestring is a valid Sapling viewing key
isValidSaplingViewingKey :: BS.ByteString -> Bool isValidSaplingViewingKey :: BS.ByteString -> Bool
isValidSaplingViewingKey = rustWrapperSaplingVkDecode isValidSaplingViewingKey = rustWrapperSaplingVkDecode
@ -23,9 +34,6 @@ isValidSaplingViewingKey = rustWrapperSaplingVkDecode
matchSaplingAddress :: BS.ByteString -> BS.ByteString -> Bool matchSaplingAddress :: BS.ByteString -> BS.ByteString -> Bool
matchSaplingAddress = rustWrapperSaplingCheck matchSaplingAddress = rustWrapperSaplingCheck
getShieldedOutputs :: BS.ByteString -> [BS.ByteString]
getShieldedOutputs tx = withPureBorshVarBuffer $ rustWrapperTxParse tx
-- | Attempt to decode the given raw tx with the given Sapling viewing key -- | Attempt to decode the given raw tx with the given Sapling viewing key
decodeSaplingOutput :: BS.ByteString -> BS.ByteString -> Maybe DecodedNote decodeSaplingOutput :: BS.ByteString -> BS.ByteString -> Maybe DecodedNote
decodeSaplingOutput key out = decodeSaplingOutput key out =
@ -35,3 +43,17 @@ decodeSaplingOutput key out =
where where
decodedAction = decodedAction =
withPureBorshVarBuffer $ rustWrapperSaplingNoteDecode key out withPureBorshVarBuffer $ rustWrapperSaplingNoteDecode key out
instance FromJSON RawTxResponse where
parseJSON =
withObject "RawTxResponse" $ \obj -> do
i <- obj .: "txid"
o <- obj .: "orchard"
h <- obj .: "hex"
a <- o .: "actions"
pure $
RawTxResponse
i
(decodeHexText h)
(getShieldedOutputs (decodeHexText h))
a

View file

@ -103,20 +103,10 @@ instance FromJSON BlockResponse where
data RawTxResponse = RawTxResponse data RawTxResponse = RawTxResponse
{ rt_id :: T.Text { rt_id :: T.Text
, rt_hex :: BS.ByteString , rt_hex :: BS.ByteString
, rt_shieldedOutputs :: [ShieldedOutput] , rt_shieldedOutputs :: [BS.ByteString]
, rt_orchardActions :: [OrchardAction] , rt_orchardActions :: [OrchardAction]
} deriving (Prelude.Show, Eq) } deriving (Prelude.Show, Eq)
instance FromJSON RawTxResponse where
parseJSON =
withObject "RawTxResponse" $ \obj -> do
i <- obj .: "txid"
s <- obj .: "vShieldedOutput"
o <- obj .: "orchard"
h <- obj .: "hex"
a <- o .: "actions"
pure $ RawTxResponse i (decodeHexText h) s a
-- * Sapling -- * Sapling
-- | Type to represent a Sapling Shielded Output as provided by the @getrawtransaction@ RPC method of @zcashd@. -- | Type to represent a Sapling Shielded Output as provided by the @getrawtransaction@ RPC method of @zcashd@.
data ShieldedOutput = ShieldedOutput data ShieldedOutput = ShieldedOutput