{-# LANGUAGE DataKinds #-}

module Cardano.CLI.EraIndependent.Cip.Cip129.Internal.Conversion
  ( encodeCip129DrepVerficationKeyText
  , encodeCip129CommitteeColdVerficationKeyText
  , encodeCip129CommitteeHotVerficationKeyText
  , encodeCip129GovernanceActionIdText
  )
where

import Cardano.Api
import Cardano.Api.Ledger qualified as L

import Cardano.CLI.Read.Committee.ColdKey
import Cardano.CLI.Read.Committee.HotKey
import Cardano.CLI.Read.DRep

encodeCip129DrepVerficationKeyText :: AnyDrepVerificationKey -> Text
encodeCip129DrepVerficationKeyText :: AnyDrepVerificationKey -> Text
encodeCip129DrepVerficationKeyText = Credential DRepRole -> Text
forall a. Cip129 a => a -> Text
serialiseToBech32Cip129 (Credential DRepRole -> Text)
-> (AnyDrepVerificationKey -> Credential DRepRole)
-> AnyDrepVerificationKey
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnyDrepVerificationKey -> Credential DRepRole
anyDrepVerificationKeyToCredential

anyDrepVerificationKeyToCredential
  :: AnyDrepVerificationKey -> L.Credential L.DRepRole
anyDrepVerificationKeyToCredential :: AnyDrepVerificationKey -> Credential DRepRole
anyDrepVerificationKeyToCredential AnyDrepVerificationKey
drepKey =
  case AnyDrepVerificationKey
drepKey of
    AnyDrepVerificationKey VerificationKey DRepKey
vk ->
      let DRepKeyHash KeyHash DRepRole
hash = VerificationKey DRepKey -> Hash DRepKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey DRepKey
vk
       in KeyHash DRepRole -> Credential DRepRole
forall (kr :: KeyRole). KeyHash kr -> Credential kr
L.KeyHashObj KeyHash DRepRole
hash
    AnyDrepExtendedVerificationKey VerificationKey DRepExtendedKey
vk ->
      let DRepExtendedKeyHash KeyHash DRepRole
hash = VerificationKey DRepExtendedKey -> Hash DRepExtendedKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey DRepExtendedKey
vk
       in KeyHash DRepRole -> Credential DRepRole
forall (kr :: KeyRole). KeyHash kr -> Credential kr
L.KeyHashObj KeyHash DRepRole
hash

encodeCip129CommitteeHotVerficationKeyText :: AnyCommitteeHotVerificationKey -> Text
encodeCip129CommitteeHotVerficationKeyText :: AnyCommitteeHotVerificationKey -> Text
encodeCip129CommitteeHotVerficationKeyText = Credential HotCommitteeRole -> Text
forall a. Cip129 a => a -> Text
serialiseToBech32Cip129 (Credential HotCommitteeRole -> Text)
-> (AnyCommitteeHotVerificationKey -> Credential HotCommitteeRole)
-> AnyCommitteeHotVerificationKey
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnyCommitteeHotVerificationKey -> Credential HotCommitteeRole
anyCommitteeHotVerificationKeyToCredential

anyCommitteeHotVerificationKeyToCredential
  :: AnyCommitteeHotVerificationKey -> L.Credential L.HotCommitteeRole
anyCommitteeHotVerificationKeyToCredential :: AnyCommitteeHotVerificationKey -> Credential HotCommitteeRole
anyCommitteeHotVerificationKeyToCredential AnyCommitteeHotVerificationKey
committeeHotKey =
  case AnyCommitteeHotVerificationKey
committeeHotKey of
    AnyCommitteeHotVerificationKey VerificationKey CommitteeHotKey
vk ->
      let CommitteeHotKeyHash KeyHash HotCommitteeRole
hash = VerificationKey CommitteeHotKey -> Hash CommitteeHotKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey CommitteeHotKey
vk
       in KeyHash HotCommitteeRole -> Credential HotCommitteeRole
forall (kr :: KeyRole). KeyHash kr -> Credential kr
L.KeyHashObj KeyHash HotCommitteeRole
hash
    AnyCommitteeHotExtendedVerificationKey VerificationKey CommitteeHotExtendedKey
vk ->
      let CommitteeHotExtendedKeyHash KeyHash HotCommitteeRole
hash = VerificationKey CommitteeHotExtendedKey
-> Hash CommitteeHotExtendedKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey CommitteeHotExtendedKey
vk
       in KeyHash HotCommitteeRole -> Credential HotCommitteeRole
forall (kr :: KeyRole). KeyHash kr -> Credential kr
L.KeyHashObj KeyHash HotCommitteeRole
hash

encodeCip129CommitteeColdVerficationKeyText :: AnyCommitteeColdVerificationKey -> Text
encodeCip129CommitteeColdVerficationKeyText :: AnyCommitteeColdVerificationKey -> Text
encodeCip129CommitteeColdVerficationKeyText = Credential ColdCommitteeRole -> Text
forall a. Cip129 a => a -> Text
serialiseToBech32Cip129 (Credential ColdCommitteeRole -> Text)
-> (AnyCommitteeColdVerificationKey
    -> Credential ColdCommitteeRole)
-> AnyCommitteeColdVerificationKey
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnyCommitteeColdVerificationKey -> Credential ColdCommitteeRole
anyCommitteeColdVerificationKeyToCredential

anyCommitteeColdVerificationKeyToCredential
  :: AnyCommitteeColdVerificationKey -> L.Credential L.ColdCommitteeRole
anyCommitteeColdVerificationKeyToCredential :: AnyCommitteeColdVerificationKey -> Credential ColdCommitteeRole
anyCommitteeColdVerificationKeyToCredential AnyCommitteeColdVerificationKey
committeeColdKey =
  case AnyCommitteeColdVerificationKey
committeeColdKey of
    AnyCommitteeColdVerificationKey VerificationKey CommitteeColdKey
vk ->
      let CommitteeColdKeyHash KeyHash ColdCommitteeRole
hash = VerificationKey CommitteeColdKey -> Hash CommitteeColdKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey CommitteeColdKey
vk
       in KeyHash ColdCommitteeRole -> Credential ColdCommitteeRole
forall (kr :: KeyRole). KeyHash kr -> Credential kr
L.KeyHashObj KeyHash ColdCommitteeRole
hash
    AnyCommitteeColdExtendedVerificationKey VerificationKey CommitteeColdExtendedKey
vk ->
      let CommitteeColdExtendedKeyHash KeyHash ColdCommitteeRole
hash = VerificationKey CommitteeColdExtendedKey
-> Hash CommitteeColdExtendedKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey CommitteeColdExtendedKey
vk
       in KeyHash ColdCommitteeRole -> Credential ColdCommitteeRole
forall (kr :: KeyRole). KeyHash kr -> Credential kr
L.KeyHashObj KeyHash ColdCommitteeRole
hash

encodeCip129GovernanceActionIdText :: L.GovActionId -> Text
encodeCip129GovernanceActionIdText :: GovActionId -> Text
encodeCip129GovernanceActionIdText = GovActionId -> Text
forall a. Cip129 a => a -> Text
serialiseToBech32Cip129