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-08-17 15:02:32 +00:00
|
|
|
-- |
|
|
|
|
-- Module : ZcashHaskell.Orchard
|
2023-12-20 20:03:42 +00:00
|
|
|
-- Copyright : 2022-2024 Vergara Technologies
|
|
|
|
-- License : LGPL-3
|
2023-08-17 15:02:32 +00:00
|
|
|
--
|
|
|
|
-- Maintainer : rene@vergara.network
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : unknown
|
|
|
|
--
|
|
|
|
-- Functions to interact with the Orchard shielded pool of the Zcash blockchain.
|
|
|
|
--
|
2023-06-14 15:53:29 +00:00
|
|
|
module ZcashHaskell.Orchard where
|
2023-05-04 20:26:49 +00:00
|
|
|
|
|
|
|
import C.Zcash
|
2024-01-12 15:46:26 +00:00
|
|
|
( rustWrapperOrchardCheck
|
2023-05-04 20:26:49 +00:00
|
|
|
, rustWrapperOrchardNoteDecode
|
2024-01-12 15:46:26 +00:00
|
|
|
, rustWrapperUADecode
|
2023-05-04 20:26:49 +00:00
|
|
|
, rustWrapperUfvkDecode
|
|
|
|
)
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import Foreign.Rust.Marshall.Variable
|
2023-06-14 15:53:29 +00:00
|
|
|
import ZcashHaskell.Types
|
2023-05-04 20:26:49 +00:00
|
|
|
|
2023-08-17 15:02:32 +00:00
|
|
|
-- | Checks if given bytestring is a valid encoded unified address
|
2024-01-12 15:46:26 +00:00
|
|
|
isValidUnifiedAddress :: BS.ByteString -> Maybe UnifiedAddress
|
|
|
|
isValidUnifiedAddress str =
|
|
|
|
case raw_net decodedAddress of
|
|
|
|
0 -> Nothing
|
|
|
|
_ -> Just $ makeUA decodedAddress
|
|
|
|
where
|
|
|
|
decodedAddress = (withPureBorshVarBuffer . rustWrapperUADecode) str
|
|
|
|
whichNet =
|
|
|
|
case raw_net decodedAddress of
|
|
|
|
1 -> MainNet
|
|
|
|
2 -> TestNet
|
|
|
|
3 -> RegTestNet
|
|
|
|
makeUA x =
|
|
|
|
UnifiedAddress
|
|
|
|
whichNet
|
|
|
|
(raw_o x)
|
|
|
|
(raw_s x)
|
|
|
|
(if not (BS.null (raw_t x))
|
|
|
|
then Just $ TransparentAddress P2PKH whichNet (raw_t x)
|
|
|
|
else if not (BS.null (raw_to x))
|
|
|
|
then Just $ TransparentAddress P2SH whichNet (raw_to x)
|
|
|
|
else Nothing)
|
2023-05-04 20:26:49 +00:00
|
|
|
|
2023-08-17 15:02:32 +00:00
|
|
|
-- | Attempts to decode the given bytestring into a Unified Full Viewing Key
|
2023-05-04 20:26:49 +00:00
|
|
|
decodeUfvk :: BS.ByteString -> Maybe UnifiedFullViewingKey
|
|
|
|
decodeUfvk str =
|
|
|
|
case net decodedKey of
|
|
|
|
0 -> Nothing
|
|
|
|
_ -> Just decodedKey
|
|
|
|
where
|
|
|
|
decodedKey = (withPureBorshVarBuffer . rustWrapperUfvkDecode) str
|
|
|
|
|
2023-10-04 16:12:30 +00:00
|
|
|
-- | Check if the given UVK matches the UA given
|
|
|
|
matchOrchardAddress :: BS.ByteString -> BS.ByteString -> Bool
|
|
|
|
matchOrchardAddress = rustWrapperOrchardCheck
|
|
|
|
|
2023-08-17 15:02:32 +00:00
|
|
|
-- | Attempts to decode the given @OrchardAction@ using the given @UnifiedFullViewingKey@.
|
2023-05-04 20:26:49 +00:00
|
|
|
decryptOrchardAction ::
|
2023-09-26 20:24:18 +00:00
|
|
|
UnifiedFullViewingKey -> OrchardAction -> Maybe DecodedNote
|
|
|
|
decryptOrchardAction key encAction =
|
2023-05-04 20:26:49 +00:00
|
|
|
case a_value decodedAction of
|
|
|
|
0 -> Nothing
|
|
|
|
_ -> Just decodedAction
|
|
|
|
where
|
|
|
|
decodedAction =
|
|
|
|
withPureBorshVarBuffer $
|
|
|
|
rustWrapperOrchardNoteDecode (o_key key) encAction
|