Add tests for Orchard spending key

This commit is contained in:
Rene Vergara 2024-03-05 15:09:35 -06:00
parent 53716685a8
commit 3822e9f2ff
Signed by: pitmutt
GPG key ID: 65122AD495A7F5B2
3 changed files with 19 additions and 4 deletions

View file

@ -625,7 +625,7 @@ pub extern "C" fn rust_wrapper_derive_orchard_spending_key(
let sk = SpendingKey::from_zip32_seed(&s, coin_type, zip32::AccountId::try_from(acc_id).unwrap()); let sk = SpendingKey::from_zip32_seed(&s, coin_type, zip32::AccountId::try_from(acc_id).unwrap());
match sk { match sk {
Ok(key) => { Ok(key) => {
marshall_to_haskell_var(&key.to_bytes(), out, out_len, RW); marshall_to_haskell_var(&key.to_bytes().to_vec(), out, out_len, RW);
}, },
Err(_e) => { Err(_e) => {
marshall_to_haskell_var(&vec![0], out, out_len, RW); marshall_to_haskell_var(&vec![0], out, out_len, RW);

View file

@ -34,10 +34,18 @@ import ZcashHaskell.Types
import ZcashHaskell.Utils (encodeBech32m, f4Jumble) import ZcashHaskell.Utils (encodeBech32m, f4Jumble)
-- | Derives an Orchard spending key for the given seed and account ID -- | Derives an Orchard spending key for the given seed and account ID
genOrchardSpendingKey :: Seed -> CoinType -> AccountId -> BS.ByteString genOrchardSpendingKey :: Seed -> CoinType -> AccountId -> Maybe BS.ByteString
genOrchardSpendingKey s coinType accountId = genOrchardSpendingKey s coinType accountId =
if BS.length k /= 32
then Nothing
else Just k
where
k =
withPureBorshVarBuffer $ withPureBorshVarBuffer $
rustWrapperGenOrchardSpendKey s (getValue coinType) (fromIntegral accountId) rustWrapperGenOrchardSpendKey
s
(getValue coinType)
(fromIntegral accountId)
-- | Checks if given bytestring is a valid encoded unified address -- | Checks if given bytestring is a valid encoded unified address
isValidUnifiedAddress :: BS.ByteString -> Maybe UnifiedAddress isValidUnifiedAddress :: BS.ByteString -> Maybe UnifiedAddress

View file

@ -19,6 +19,7 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
import C.Zcash (rustWrapperUADecode) import C.Zcash (rustWrapperUADecode)
import Control.Monad.IO.Class (liftIO)
import Data.Aeson import Data.Aeson
import Data.Bool (Bool(True)) import Data.Bool (Bool(True))
import qualified Data.ByteString as BS import qualified Data.ByteString as BS
@ -45,6 +46,7 @@ import ZcashHaskell.Sapling
import ZcashHaskell.Transparent (encodeTransparent) import ZcashHaskell.Transparent (encodeTransparent)
import ZcashHaskell.Types import ZcashHaskell.Types
( BlockResponse(..) ( BlockResponse(..)
, CoinType(..)
, DecodedNote(..) , DecodedNote(..)
, OrchardAction(..) , OrchardAction(..)
, RawData(..) , RawData(..)
@ -453,6 +455,11 @@ main = do
p <- generateWalletSeedPhrase p <- generateWalletSeedPhrase
let s = getWalletSeed p let s = getWalletSeed p
maybe 0 BS.length s `shouldBe` 64 maybe 0 BS.length s `shouldBe` 64
it "Generate Orchard spending key" $ do
p <- generateWalletSeedPhrase
let s = getWalletSeed p
genOrchardSpendingKey (fromMaybe "" s) MainNetCoin 1 `shouldNotBe`
Nothing
describe "Address tests" $ do describe "Address tests" $ do
it "Encode transparent" $ do it "Encode transparent" $ do
let ua = let ua =