Skip to content

Commit

Permalink
Add functional dependency to ExtendedSigningKeyRole and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Nov 13, 2024
1 parent 0894dfb commit 07b6668
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
14 changes: 11 additions & 3 deletions cardano-api/internal/Cardano/Api/Keys/Mnemonics.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.Api.Keys.Mnemonics
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions cardano-api/test/cardano-api-test/Test/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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
Expand Down

0 comments on commit 07b6668

Please sign in to comment.