Skip to content

Commit

Permalink
Add tests for Cardano.Wallet.Deposit.Write.Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
HeinrichApfelmus committed Nov 28, 2024
1 parent 96fc973 commit 432901c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/customer-deposit-wallet/customer-deposit-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ test-suite unit
, base58-bytestring
, bytestring
, cardano-crypto
, cardano-crypto-class
, cardano-ledger-api
, cardano-ledger-core
, cardano-ledger-core:testlib
, cardano-wallet-read
, cardano-wallet-test-utils
Expand All @@ -225,6 +228,7 @@ test-suite unit
, customer-deposit-wallet
, customer-deposit-wallet:http
, customer-deposit-wallet:rest
, customer-deposit-wallet-pure
, directory
, hspec
, hspec-golden
Expand All @@ -247,6 +251,7 @@ test-suite unit
Cardano.Wallet.Deposit.Pure.API.AddressSpec
Cardano.Wallet.Deposit.PureSpec
Cardano.Wallet.Deposit.RESTSpec
Cardano.Wallet.Deposit.Write.KeysSpec
Paths_customer_deposit_wallet
Spec

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
{-# OPTIONS_GHC -Wno-orphans #-}

-- |
-- Copyright: © 2024 Cardano Foundation
-- License: Apache-2.0
--
-- Property tests for the deposit wallet.
module Cardano.Wallet.Deposit.Write.KeysSpec
( spec
) where

import Prelude

import Cardano.Crypto.Wallet
( generate
)
import Cardano.Wallet.Address.BIP32_Ed25519
( XPrv
, XPub
, sign
, toXPub
)
import Cardano.Wallet.Address.Encoding
( EnterpriseAddr (..)
, NetworkTag (..)
, compactAddrFromEnterpriseAddr
, credentialFromXPub
)
import Cardano.Wallet.Deposit.Write.Keys
( enterpriseAddressFromVKey
, signedDSIGNfromXSignature
, vkeyFromXPub
)
import Test.Hspec
( Spec
, describe
, it
)
import Test.QuickCheck
( Arbitrary (..)
, Blind (..)
, Property
, property
, vectorOf
, withMaxSuccess
, (===)
)

import qualified Cardano.Crypto.Hash.Blake2b as Hash
import qualified Cardano.Crypto.Hash.Class as Hash
import qualified Cardano.Ledger.BaseTypes as L
import qualified Cardano.Ledger.Hashes as L
import qualified Cardano.Ledger.Keys as L
import qualified Cardano.Wallet.Read as Read
import qualified Data.ByteString as BS

{-----------------------------------------------------------------------------
Spec
------------------------------------------------------------------------------}
spec :: Spec
spec = do
describe "commutes with ledger" $ do
it "address" $ lessCryptography $ property $
\xpub ->
enterpriseAddressFromVKey L.Mainnet (vkeyFromXPub xpub)
=== enterpriseAddressFromXPub xpub

it "verify" $ lessCryptography $ property $
\(Blind xprv) hash ->
let xpub = toXPub xprv
xsig = sign xprv (Hash.hashToBytes hash)
in
True ===
L.verifySignedDSIGN
(vkeyFromXPub xpub)
hash
(signedDSIGNfromXSignature xsig)

lessCryptography :: Property -> Property
lessCryptography = withMaxSuccess 20

{-----------------------------------------------------------------------------
Helper functions
------------------------------------------------------------------------------}
enterpriseAddressFromXPub :: XPub -> Read.CompactAddr
enterpriseAddressFromXPub =
compactAddrFromEnterpriseAddr
. EnterpriseAddrC MainnetTag
. credentialFromXPub

instance Arbitrary XPrv where
arbitrary =
generate
<$> (BS.pack <$> vectorOf 100 arbitrary)
<*> pure BS.empty

instance Arbitrary XPub where
arbitrary = toXPub <$> arbitrary

instance Arbitrary (Hash.Hash Hash.Blake2b_256 L.EraIndependentTxBody) where
arbitrary = do
bytes <- BS.pack <$> vectorOf (32) arbitrary
let Just hash = Hash.hashFromBytes bytes
pure hash

0 comments on commit 432901c

Please sign in to comment.