-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This pull request adds unit tests for * The mapping `XPub` → `Address` * Verification of `XSignature` by comparing `Cardano.Wallet.Address.Encoding` against the implementation in `Cardano.Ledger`. We adopt the fix of the mapping `XPub` → `Address` by adjusting to the latest version of the `cardano-wallet-agda` repository. ### Issue Number ADP-3488
- Loading branch information
Showing
6 changed files
with
210 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/Write/Keys.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
|
||
-- | Module for converting key types from | ||
-- @Cardano.Ledger@ with key types from @Cardano.Crypto.Wallet@. | ||
-- | ||
-- TODO: Match this up with the @Write@ hierarchy. | ||
module Cardano.Wallet.Deposit.Write.Keys | ||
( enterpriseAddressFromVKey | ||
, vkeyFromXPub | ||
, signedDSIGNfromXSignature | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.Crypto.Wallet | ||
( xpubPublicKey | ||
) | ||
import Cardano.Ledger.Keys | ||
( SignedDSIGN | ||
, VKey (..) | ||
) | ||
import Cardano.Wallet.Address.BIP32_Ed25519 | ||
( XPub | ||
, XSignature | ||
, rawSerialiseXSignature | ||
) | ||
import Cardano.Wallet.Deposit.Read | ||
( Address | ||
) | ||
import Data.Maybe | ||
( fromMaybe | ||
) | ||
|
||
import qualified Cardano.Crypto.DSIGN as DSIGN | ||
import qualified Cardano.Ledger.Address as L | ||
import qualified Cardano.Ledger.Api as L | ||
import qualified Cardano.Ledger.BaseTypes as L | ||
import qualified Cardano.Ledger.Credential as L | ||
import qualified Cardano.Ledger.Hashes as L | ||
import qualified Cardano.Ledger.Keys as L | ||
|
||
{----------------------------------------------------------------------------- | ||
Key conversion | ||
------------------------------------------------------------------------------} | ||
-- | Create an enterprise address from a ledger 'VKey'. | ||
enterpriseAddressFromVKey | ||
:: L.Network | ||
-> VKey 'L.Witness L.StandardCrypto | ||
-> Address | ||
enterpriseAddressFromVKey network = | ||
mkEnterpriseAddress | ||
. L.coerceKeyRole | ||
. L.hashKey | ||
where | ||
mkEnterpriseAddress h = | ||
L.compactAddr | ||
$ L.Addr network (L.KeyHashObj h) L.StakeRefNull | ||
|
||
-- | Convert 'XPub' to a ledger verification key. | ||
vkeyFromXPub :: XPub -> VKey 'L.Witness L.StandardCrypto | ||
vkeyFromXPub = | ||
VKey | ||
. fromMaybe impossible | ||
. DSIGN.rawDeserialiseVerKeyDSIGN | ||
. xpubPublicKey | ||
where | ||
impossible = error "impossible: Cannot convert XPub to VKey" | ||
|
||
-- | Convert 'XSignature' to a ledger signature. | ||
signedDSIGNfromXSignature | ||
:: XSignature | ||
-> SignedDSIGN L.StandardCrypto | ||
(L.Hash L.StandardCrypto L.EraIndependentTxBody) | ||
signedDSIGNfromXSignature = | ||
DSIGN.SignedDSIGN | ||
. fromMaybe impossible | ||
. DSIGN.rawDeserialiseSigDSIGN | ||
. rawSerialiseXSignature | ||
where | ||
impossible = error "impossible: Cannot convert XSignature to SignedDSIGN" |
116 changes: 116 additions & 0 deletions
116
lib/customer-deposit-wallet/test/unit/Cardano/Wallet/Deposit/Write/KeysSpec.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
{-# 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 | ||
, elements | ||
, 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 networkTag -> | ||
let network = toLedgerNetwork networkTag | ||
in enterpriseAddressFromVKey network (vkeyFromXPub xpub) | ||
=== enterpriseAddressFromXPub networkTag 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 :: NetworkTag -> XPub -> Read.CompactAddr | ||
enterpriseAddressFromXPub networkTag = | ||
compactAddrFromEnterpriseAddr | ||
. EnterpriseAddrC networkTag | ||
. credentialFromXPub | ||
|
||
toLedgerNetwork :: NetworkTag -> L.Network | ||
toLedgerNetwork MainnetTag = L.Mainnet | ||
toLedgerNetwork TestnetTag = L.Testnet | ||
|
||
instance Arbitrary NetworkTag where | ||
arbitrary = elements [MainnetTag, TestnetTag] | ||
|
||
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 |