Skip to content

Commit

Permalink
Use associated type family for payment key index
Browse files Browse the repository at this point in the history
Co-authored-by: Mateusz Galazyn <[email protected]>
  • Loading branch information
palas and carbolymer committed Nov 18, 2024
1 parent 3192ab8 commit 0d0b98a
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions cardano-api/internal/Cardano/Api/Keys/Mnemonics.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.Api.Keys.Mnemonics
( MnemonicSize (..)
Expand Down Expand Up @@ -69,20 +69,23 @@ instance Error MnemonicToSigningKeyError where
prettyError (InvalidAccountNumberError accNo) = "Invalid account number: " <> pretty accNo
prettyError (InvalidPaymentKeyNoError keyNo) = "Invalid payment key number: " <> pretty keyNo

class ExtendedSigningKeyRole keyrole indexType | keyrole -> indexType where
class ExtendedSigningKeyRole keyrole where
type EskrPaymentAddrIndex keyrole

-- | Derive an extended private key of the keyrole from an account extended private key
deriveSigningKeyFromAccount
:: 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
-> EskrPaymentAddrIndex keyrole
-- ^ 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
instance ExtendedSigningKeyRole PaymentExtendedKey where
type EskrPaymentAddrIndex PaymentExtendedKey = Word32
deriveSigningKeyFromAccount
:: AsType PaymentExtendedKey
-> Shelley 'AccountK XPrv
Expand All @@ -92,7 +95,8 @@ instance ExtendedSigningKeyRole PaymentExtendedKey Word32 where
payKeyIx <- maybeToEither idx $ indexFromWord32 @(Index 'Soft 'PaymentK) idx
return $ PaymentExtendedSigningKey $ getKey $ deriveAddressPrivateKey accK UTxOExternal payKeyIx

instance ExtendedSigningKeyRole StakeExtendedKey Word32 where
instance ExtendedSigningKeyRole StakeExtendedKey where
type EskrPaymentAddrIndex StakeExtendedKey = Word32
deriveSigningKeyFromAccount
:: AsType StakeExtendedKey
-> Shelley 'AccountK XPrv
Expand All @@ -102,7 +106,8 @@ instance ExtendedSigningKeyRole StakeExtendedKey Word32 where
payKeyIx <- maybeToEither idx $ indexFromWord32 @(Index 'Soft 'PaymentK) idx
return $ StakeExtendedSigningKey $ getKey $ deriveAddressPrivateKey accK Stake payKeyIx

instance ExtendedSigningKeyRole DRepExtendedKey () where
instance ExtendedSigningKeyRole DRepExtendedKey where
type EskrPaymentAddrIndex DRepExtendedKey = ()
deriveSigningKeyFromAccount
:: AsType DRepExtendedKey
-> Shelley 'AccountK XPrv
Expand All @@ -111,7 +116,8 @@ instance ExtendedSigningKeyRole DRepExtendedKey () where
deriveSigningKeyFromAccount _ accK _ =
return $ DRepExtendedSigningKey $ getKey $ deriveDRepPrivateKey accK

instance ExtendedSigningKeyRole CommitteeColdExtendedKey () where
instance ExtendedSigningKeyRole CommitteeColdExtendedKey where
type EskrPaymentAddrIndex CommitteeColdExtendedKey = ()
deriveSigningKeyFromAccount
:: AsType CommitteeColdExtendedKey
-> Shelley 'AccountK XPrv
Expand All @@ -120,7 +126,8 @@ instance ExtendedSigningKeyRole CommitteeColdExtendedKey () where
deriveSigningKeyFromAccount _ accK _ =
return $ CommitteeColdExtendedSigningKey $ getKey $ deriveCCColdPrivateKey accK

instance ExtendedSigningKeyRole CommitteeHotExtendedKey () where
instance ExtendedSigningKeyRole CommitteeHotExtendedKey where
type EskrPaymentAddrIndex CommitteeHotExtendedKey = ()
deriveSigningKeyFromAccount
:: AsType CommitteeHotExtendedKey
-> Shelley 'AccountK XPrv
Expand All @@ -136,15 +143,15 @@ instance ExtendedSigningKeyRole CommitteeHotExtendedKey () where
-- In this function we only ask for two indices: the account number and the
-- payment key number. Each account can have multiple payment keys.
signingKeyFromMnemonic
:: ExtendedSigningKeyRole keyrole indexType
:: ExtendedSigningKeyRole keyrole
=> AsType keyrole
-- ^ Type of the extended signing key to generate.
-> [Text]
-- ^ The mnemonic sentence. The length must be one of 12, 15, 18, 21, or 24.
-- Each element of the list must be a single word.
-> Word32
-- ^ The account number in the derivation path. First account is 0.
-> indexType
-> EskrPaymentAddrIndex keyrole
-- ^ The payment key number in the derivation path (as 'Word32') if applicable for
-- the given key role, otherwise '()'. First key is 0.
-> Either MnemonicToSigningKeyError (SigningKey keyrole)
Expand Down

0 comments on commit 0d0b98a

Please sign in to comment.