{- 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 . -} -- | -- Module : ZcashHaskell.Orchard -- Copyright : 2022-2024 Vergara Technologies -- License : LGPL-3 -- -- Maintainer : rene@vergara.network -- Stability : experimental -- Portability : unknown -- -- Functions to interact with the Orchard shielded pool of the Zcash blockchain. -- module ZcashHaskell.Orchard where import C.Zcash ( rustWrapperOrchardCheck , rustWrapperOrchardNoteDecode , rustWrapperUADecode , rustWrapperUfvkDecode ) import qualified Data.ByteString as BS import Foreign.Rust.Marshall.Variable import ZcashHaskell.Types -- | Checks if given bytestring is a valid encoded unified address 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) -- | Attempts to decode the given bytestring into a Unified Full Viewing Key decodeUfvk :: BS.ByteString -> Maybe UnifiedFullViewingKey decodeUfvk str = case net decodedKey of 0 -> Nothing _ -> Just decodedKey where decodedKey = (withPureBorshVarBuffer . rustWrapperUfvkDecode) str -- | Check if the given UVK matches the UA given matchOrchardAddress :: BS.ByteString -> BS.ByteString -> Bool matchOrchardAddress = rustWrapperOrchardCheck -- | Attempts to decode the given @OrchardAction@ using the given @UnifiedFullViewingKey@. decryptOrchardAction :: UnifiedFullViewingKey -> OrchardAction -> Maybe DecodedNote decryptOrchardAction key encAction = case a_value decodedAction of 0 -> Nothing _ -> Just decodedAction where decodedAction = withPureBorshVarBuffer $ rustWrapperOrchardNoteDecode (o_key key) encAction