From 06aff8c7879bceed47393b4ca3a23354b3937d7d Mon Sep 17 00:00:00 2001 From: "Rene V. Vergara" Date: Tue, 16 Apr 2024 18:51:14 -0400 Subject: [PATCH] rvv040 - Add encodeSaplingAddress function Added new tests for encodeSaplingAddress and decodeSaplingAddress --- CHANGELOG.md | 10 +++++++ src/ZcashHaskell/Sapling.hs | 60 +++++++++++++++++++++---------------- src/ZcashHaskell/Types.hs | 4 +-- test/Spec.hs | 24 +++++++++++---- 4 files changed, 66 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f35ce1..ecf9c65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.5.5.2] + +### Added + +- Added unction to encode a Sappling Address in Human Readable Format Using a SaplingReceiver + `encodeSaplingAddress` a zcash sapling address is returned or Nothing if the function fails +- Added decoding and encoding test + + + ## [0.5.5.1] ### Added diff --git a/src/ZcashHaskell/Sapling.hs b/src/ZcashHaskell/Sapling.hs index d80abf8..2b971a2 100644 --- a/src/ZcashHaskell/Sapling.hs +++ b/src/ZcashHaskell/Sapling.hs @@ -34,32 +34,35 @@ import C.Zcash ) import Data.Aeson import qualified Data.ByteString as BS +import qualified Data.ByteString.Char8 as C +import qualified Data.Text as T import Data.HexString (HexString(..), fromText, hexString, toBytes, toText) import Data.Word import Foreign.Rust.Marshall.Variable ( withPureBorshVarBuffer , withPureBorshVarBuffer ) -import ZcashHaskell.Types - ( AccountId - , CoinType - , DecodedNote(..) - , RawData(..) - , RawTxResponse(..) - , SaplingAddress(..) - , SaplingCommitmentTree(..) - , SaplingReceiver(..) - , SaplingSpendingKey(..) - , SaplingWitness(..) - , Scope(..) - , Seed(..) - , ShieldedOutput(..) - , ToBytes(..) - , ZcashNet(..) - , decodeHexText - , getValue - ) -import ZcashHaskell.Utils (decodeBech32) +import ZcashHaskell.Types +--import ZcashHaskell.Types +-- ( AccountId +-- , CoinType +-- , DecodedNote(..) +-- , RawData(..) +-- , RawTxResponse(..) +-- , SaplingAddress(..) +-- , SaplingCommitmentTree(..) +-- , SaplingReceiver(..) +-- , SaplingSpendingKey(..) +-- , SaplingWitness(..) +-- , Scope(..) +-- , Seed(..) +-- , ShieldedOutput(..) +-- , ToBytes(..) +-- , ZcashNet(..) +-- , decodeHexText +-- , getValue +-- ) +import ZcashHaskell.Utils (decodeBech32, encodeBech32, encodeBech32m) -- | Check if given bytesting is a valid encoded shielded address isValidShieldedAddress :: BS.ByteString -> Bool @@ -208,25 +211,32 @@ getSaplingNotePosition :: SaplingWitness -> Integer getSaplingNotePosition = fromIntegral . rustWrapperReadSaplingPosition . hexBytes . sapWit +-- | Encode a SaplingReceiver into HRF text +encodeSaplingAddress :: ZcashNet -> SaplingReceiver -> Maybe T.Text +encodeSaplingAddress net sr = do + case net of + MainNet -> + Just $ encodeBech32 (C.pack sapPaymentAddressHrp) (getBytes sr) + TestNet -> + Just $ encodeBech32 (C.pack sapTestPaymentAddressHrp) (getBytes sr) + +-- | Helper to get de Nework Id from FFI response getNetId:: [Word8] -> ZcashNet getNetId [x] = do case x of 1 -> MainNet 2 -> TestNet - -- | decode a Sapling address decodeSaplingAddress :: BS.ByteString -> Maybe SaplingAddress decodeSaplingAddress sapling_address = do if BS.length sa > 1 then do let sa0 = BS.unpack sa - Just $ SaplingAddress (getNetId (take 1 sa0)) (BS.pack (drop 1 sa0)) + Just $ SaplingAddress (getNetId (take 1 sa0)) + $ SaplingReceiver (BS.pack (drop 1 sa0)) else Nothing where sa = withPureBorshVarBuffer $ rustWrapperDecodeSaplingAddress sapling_address - - - diff --git a/src/ZcashHaskell/Types.hs b/src/ZcashHaskell/Types.hs index 5bc66f4..08da042 100644 --- a/src/ZcashHaskell/Types.hs +++ b/src/ZcashHaskell/Types.hs @@ -480,9 +480,9 @@ instance ToBytes SaplingReceiver where data SaplingAddress = SaplingAddress { net_type :: !ZcashNet - , address :: !BS.ByteString + , sa_receiver :: !SaplingReceiver } deriving (Eq, Prelude.Show, Read) - + -- | Type to represent a Sapling Shielded Spend as provided by the @getrawtransaction@ RPC method data ShieldedSpend = ShieldedSpend { sp_cv :: !HexString diff --git a/test/Spec.hs b/test/Spec.hs index 37a45c5..4f5fd1e 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -58,6 +58,7 @@ import ZcashHaskell.Sapling , matchSaplingAddress , updateSaplingCommitmentTree , decodeSaplingAddress + , encodeSaplingAddress ) import ZcashHaskell.Transparent import ZcashHaskell.Types @@ -916,14 +917,27 @@ main = do describe "Decode a Sapling Address (MainNet)" $ do let sa = decodeSaplingAddress "zs1waxrpde36rlrjdwfhnvw030sn29lzwmvmeupd8x2uuqgypaafx7mqcy0ep8yf2xtg30n5424t60" it "Try to decode a valid MainNet Sapling Address" $ do - print sa - sa `shouldNotBe` Nothing + case sa of + Nothing -> assertFailure "Failed to decode MainNet SaplingAddress" + Just s -> do -- Sapling address decoded succesfully + let sh = encodeSaplingAddress (net_type s) (sa_receiver s) + case sh of + Nothing -> assertFailure "Failed to encode MainNet SaplingAddress" + Just zsh -> do + print zsh + zsh `shouldBe` "zs1waxrpde36rlrjdwfhnvw030sn29lzwmvmeupd8x2uuqgypaafx7mqcy0ep8yf2xtg30n5424t60" describe "Decode a Sapling Address (TestNet)" $ do let sa = decodeSaplingAddress "ztestsapling188csdsvhdny25am8ume03qr2026hdy03zpg5pq7jmmfxtxtct0e93p0rg80yfxvynqd4gwlwft5" it "Try to decode a valid TestNet Sapling Address " $ do - print sa - sa `shouldNotBe` Nothing - + case sa of + Nothing -> assertFailure "Failed to decode TestNet SaplingAddress" + Just s -> do -- Sapling address decoded succesfully + let sh = encodeSaplingAddress (net_type s) (sa_receiver s) + case sh of + Nothing -> assertFailure "Failed to encode TestNet SaplingAddress" + Just zsh -> do + print zsh + zsh `shouldBe` "ztestsapling188csdsvhdny25am8ume03qr2026hdy03zpg5pq7jmmfxtxtct0e93p0rg80yfxvynqd4gwlwft5" -- | Properties prop_PhraseLength :: Property