From 432901c8eac6e9406daef2b42637b75c81dccc88 Mon Sep 17 00:00:00 2001 From: Heinrich Apfelmus Date: Wed, 27 Nov 2024 15:57:43 +0100 Subject: [PATCH] Add tests for `Cardano.Wallet.Deposit.Write.Keys` --- .../customer-deposit-wallet.cabal | 5 + .../Cardano/Wallet/Deposit/Write/KeysSpec.hs | 107 ++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/Write/KeysSpec.hs diff --git a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal index 6050f82d1d9..4145bb24565 100644 --- a/lib/customer-deposit-wallet/customer-deposit-wallet.cabal +++ b/lib/customer-deposit-wallet/customer-deposit-wallet.cabal @@ -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 @@ -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 @@ -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 diff --git a/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/Write/KeysSpec.hs b/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/Write/KeysSpec.hs new file mode 100644 index 00000000000..f01a0367c27 --- /dev/null +++ b/lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/Write/KeysSpec.hs @@ -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