2023-12-20 20:03:42 +00:00
|
|
|
{- Copyright 2022-2024 Vergara Technologies LLC
|
|
|
|
|
|
|
|
This file is part of Zcash-Haskell.
|
|
|
|
|
|
|
|
Zcash-Haskell is free software: you can redistribute it and/or modify it
|
|
|
|
under the terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
later version.
|
|
|
|
|
|
|
|
Zcash-Haskell is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along with
|
|
|
|
Zcash-Haskell. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
-}
|
2023-09-27 16:18:00 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2023-12-20 20:03:42 +00:00
|
|
|
-- |
|
|
|
|
-- Module : ZcashHaskell.Sapling
|
|
|
|
-- Copyright : 2022-2024 Vergara Technologies
|
|
|
|
-- License : LGPL-3
|
|
|
|
--
|
|
|
|
-- Maintainer : rene@vergara.network
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : unknown
|
|
|
|
--
|
|
|
|
-- Functions to interact with the Sapling shielded pool of the Zcash blockchain.
|
|
|
|
--
|
2023-06-14 15:53:29 +00:00
|
|
|
module ZcashHaskell.Sapling where
|
2023-05-04 20:26:49 +00:00
|
|
|
|
2023-06-14 15:53:29 +00:00
|
|
|
import C.Zcash
|
|
|
|
( rustWrapperIsShielded
|
|
|
|
, rustWrapperSaplingCheck
|
2023-08-22 20:05:40 +00:00
|
|
|
, rustWrapperSaplingNoteDecode
|
2023-06-14 15:53:29 +00:00
|
|
|
, rustWrapperSaplingVkDecode
|
2023-09-27 15:37:53 +00:00
|
|
|
, rustWrapperTxParse
|
2023-06-14 15:53:29 +00:00
|
|
|
)
|
2023-09-27 16:18:00 +00:00
|
|
|
import Data.Aeson
|
2023-05-04 20:26:49 +00:00
|
|
|
import qualified Data.ByteString as BS
|
2023-08-22 20:05:40 +00:00
|
|
|
import Foreign.Rust.Marshall.Variable (withPureBorshVarBuffer)
|
2023-09-27 16:18:00 +00:00
|
|
|
import ZcashHaskell.Types
|
|
|
|
( DecodedNote(..)
|
2023-09-28 19:23:42 +00:00
|
|
|
, RawData(..)
|
2023-09-27 16:18:00 +00:00
|
|
|
, RawTxResponse(..)
|
|
|
|
, ShieldedOutput(..)
|
|
|
|
, decodeHexText
|
|
|
|
)
|
2023-09-28 19:23:42 +00:00
|
|
|
import ZcashHaskell.Utils (decodeBech32)
|
2023-05-04 20:26:49 +00:00
|
|
|
|
|
|
|
-- | Check if given bytesting is a valid encoded shielded address
|
|
|
|
isValidShieldedAddress :: BS.ByteString -> Bool
|
|
|
|
isValidShieldedAddress = rustWrapperIsShielded
|
2023-06-14 14:55:52 +00:00
|
|
|
|
2023-09-27 16:18:00 +00:00
|
|
|
getShieldedOutputs :: BS.ByteString -> [BS.ByteString]
|
|
|
|
getShieldedOutputs t = withPureBorshVarBuffer $ rustWrapperTxParse t
|
|
|
|
|
2023-06-14 14:55:52 +00:00
|
|
|
-- | Check if given bytestring is a valid Sapling viewing key
|
|
|
|
isValidSaplingViewingKey :: BS.ByteString -> Bool
|
2023-09-28 19:23:42 +00:00
|
|
|
isValidSaplingViewingKey k =
|
|
|
|
case hrp decodedKey of
|
|
|
|
"zxviews" -> rustWrapperSaplingVkDecode $ bytes decodedKey
|
|
|
|
_ -> False
|
|
|
|
where
|
|
|
|
decodedKey = decodeBech32 k
|
2023-06-14 15:53:29 +00:00
|
|
|
|
|
|
|
-- | Check if the given bytestring for the Sapling viewing key matches the second bytestring for the address
|
|
|
|
matchSaplingAddress :: BS.ByteString -> BS.ByteString -> Bool
|
|
|
|
matchSaplingAddress = rustWrapperSaplingCheck
|
2023-08-22 20:05:40 +00:00
|
|
|
|
2023-08-23 20:19:31 +00:00
|
|
|
-- | Attempt to decode the given raw tx with the given Sapling viewing key
|
|
|
|
decodeSaplingOutput :: BS.ByteString -> BS.ByteString -> Maybe DecodedNote
|
2023-08-22 20:05:40 +00:00
|
|
|
decodeSaplingOutput key out =
|
|
|
|
case a_value decodedAction of
|
|
|
|
0 -> Nothing
|
|
|
|
_ -> Just decodedAction
|
|
|
|
where
|
|
|
|
decodedAction =
|
|
|
|
withPureBorshVarBuffer $ rustWrapperSaplingNoteDecode key out
|
2023-09-27 16:18:00 +00:00
|
|
|
|
|
|
|
instance FromJSON RawTxResponse where
|
|
|
|
parseJSON =
|
|
|
|
withObject "RawTxResponse" $ \obj -> do
|
|
|
|
i <- obj .: "txid"
|
2023-09-28 18:56:31 +00:00
|
|
|
o <- obj .:? "orchard"
|
2023-09-27 16:18:00 +00:00
|
|
|
h <- obj .: "hex"
|
2023-10-02 20:25:44 +00:00
|
|
|
ht <- obj .: "height"
|
|
|
|
c <- obj .: "confirmations"
|
|
|
|
b <- obj .: "blocktime"
|
2023-09-28 18:56:31 +00:00
|
|
|
case o of
|
|
|
|
Nothing ->
|
|
|
|
pure $
|
|
|
|
RawTxResponse
|
|
|
|
i
|
|
|
|
(decodeHexText h)
|
|
|
|
(getShieldedOutputs (decodeHexText h))
|
|
|
|
[]
|
2023-10-02 20:25:44 +00:00
|
|
|
ht
|
|
|
|
c
|
|
|
|
b
|
2023-09-28 18:56:31 +00:00
|
|
|
Just o' -> do
|
|
|
|
a <- o' .: "actions"
|
|
|
|
pure $
|
|
|
|
RawTxResponse
|
|
|
|
i
|
|
|
|
(decodeHexText h)
|
|
|
|
(getShieldedOutputs (decodeHexText h))
|
|
|
|
a
|
2023-10-02 20:25:44 +00:00
|
|
|
ht
|
|
|
|
c
|
|
|
|
b
|