From 07b66682505aa2a6ce7397ac0003b560bfe649b5 Mon Sep 17 00:00:00 2001 From: Pablo Lamela Date: Wed, 13 Nov 2024 12:11:41 +0100 Subject: [PATCH] Add functional dependency to `ExtendedSigningKeyRole` and comments --- cardano-api/internal/Cardano/Api/Keys/Mnemonics.hs | 14 +++++++++++--- .../cardano-api-test/Test/Cardano/Api/Address.hs | 8 ++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/Keys/Mnemonics.hs b/cardano-api/internal/Cardano/Api/Keys/Mnemonics.hs index e7cd2f91f..557a0dda1 100644 --- a/cardano-api/internal/Cardano/Api/Keys/Mnemonics.hs +++ b/cardano-api/internal/Cardano/Api/Keys/Mnemonics.hs @@ -1,6 +1,6 @@ {-# LANGUAGE DataKinds #-} +{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE InstanceSigs #-} -{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeApplications #-} module Cardano.Api.Keys.Mnemonics @@ -68,10 +68,18 @@ instance Error MnemonicToSigningStakeKeyError where prettyError (InvalidAccountNumberError accNo) = "Invalid account number: " <> pretty accNo prettyError (InvalidPaymentKeyNoError keyNo) = "Invalid payment key number: " <> pretty keyNo -class ExtendedSigningKeyRole keyrole indexType where +class ExtendedSigningKeyRole keyrole indexType | keyrole -> indexType where -- | Derive an extended private key of the keyrole from an account extended private key deriveSigningKeyFromAccount - :: AsType keyrole -> Shelley 'AccountK XPrv -> indexType -> Either Word32 (SigningKey keyrole) + :: AsType keyrole + -- ^ Type of the extended signing key to generate. + -> Shelley 'AccountK XPrv + -- ^ The account extended private key from which to derivate the private key for the keyrole. + -> indexType + -- ^ The payment key number in the derivation path (as 'Word32') if applicable for + -- the given key role, otherwise '()'. First key is 0. + -> Either Word32 (SigningKey keyrole) + -- ^ The derived extended signing key or the 'indexType' if it is 'Word32' and it is invalid. instance ExtendedSigningKeyRole PaymentExtendedKey Word32 where deriveSigningKeyFromAccount diff --git a/cardano-api/test/cardano-api-test/Test/Cardano/Api/Address.hs b/cardano-api/test/cardano-api-test/Test/Cardano/Api/Address.hs index 85d0afd08..b66d714c2 100644 --- a/cardano-api/test/cardano-api-test/Test/Cardano/Api/Address.hs +++ b/cardano-api/test/cardano-api-test/Test/Cardano/Api/Address.hs @@ -74,7 +74,7 @@ exampleMnemonic = prop_payment_derivation_is_accurate :: Property prop_payment_derivation_is_accurate = H.propertyOnce $ do signingKey <- - H.evalEither $ signingKeyFromMnemonic AsPaymentExtendedKey exampleMnemonic 0 (0 :: Word32) + H.evalEither $ signingKeyFromMnemonic AsPaymentExtendedKey exampleMnemonic 0 0 let verificationKey = getVerificationKey (signingKey :: SigningKey PaymentExtendedKey) :: VerificationKey PaymentExtendedKey @@ -92,7 +92,7 @@ prop_payment_derivation_is_accurate = H.propertyOnce $ do prop_stake_derivation_is_accurate :: Property prop_stake_derivation_is_accurate = H.propertyOnce $ do signingKey <- - H.evalEither $ signingKeyFromMnemonic AsStakeExtendedKey exampleMnemonic 0 (0 :: Word32) + H.evalEither $ signingKeyFromMnemonic AsStakeExtendedKey exampleMnemonic 0 0 let verificationKey = getVerificationKey (signingKey :: SigningKey StakeExtendedKey) :: VerificationKey StakeExtendedKey addr = @@ -106,9 +106,9 @@ prop_stake_derivation_is_accurate = H.propertyOnce $ do prop_payment_with_stake_derivation_is_accurate :: Property prop_payment_with_stake_derivation_is_accurate = H.propertyOnce $ do paymentSigningKey <- - H.evalEither $ signingKeyFromMnemonic AsPaymentExtendedKey exampleMnemonic 0 (0 :: Word32) + H.evalEither $ signingKeyFromMnemonic AsPaymentExtendedKey exampleMnemonic 0 0 stakeSigningKey <- - H.evalEither $ signingKeyFromMnemonic AsStakeExtendedKey exampleMnemonic 0 (0 :: Word32) + H.evalEither $ signingKeyFromMnemonic AsStakeExtendedKey exampleMnemonic 0 0 let paymentVerificationKey = getVerificationKey (paymentSigningKey :: SigningKey PaymentExtendedKey) :: VerificationKey PaymentExtendedKey