-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from input-output-hk/newhoggy/cip-1694-api
CIP-1694 API - Part 1
- Loading branch information
Showing
11 changed files
with
898 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
|
||
-- | DRep off-chain metadata | ||
-- | ||
module Cardano.Api.DRepMetadata ( | ||
-- * DRep off-chain metadata | ||
DRepMetadata(..), | ||
validateAndHashDRepMetadata, | ||
DRepMetadataValidationError(..), | ||
|
||
-- * Data family instances | ||
AsType(..), | ||
Hash(..), | ||
) where | ||
|
||
import Cardano.Api.Eras | ||
import Cardano.Api.Error | ||
import Cardano.Api.Hash | ||
import Cardano.Api.HasTypeProxy | ||
import Cardano.Api.Keys.Byron | ||
import Cardano.Api.Keys.Praos | ||
import Cardano.Api.Script | ||
import Cardano.Api.SerialiseRaw | ||
|
||
import qualified Cardano.Crypto.Hash.Class as Crypto | ||
import Cardano.Ledger.Crypto (StandardCrypto) | ||
import qualified Cardano.Ledger.Keys as Shelley | ||
|
||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString as BS | ||
import Data.Either.Combinators (maybeToRight) | ||
|
||
-- ---------------------------------------------------------------------------- | ||
-- DRep metadata | ||
-- | ||
|
||
-- | A representation of the required fields for off-chain drep metadata. | ||
-- | ||
newtype DRepMetadata = DRepMetadata | ||
{ unDRepMetadata :: ByteString | ||
} deriving (Eq, Show) | ||
|
||
newtype instance Hash DRepMetadata = DRepMetadataHash (Shelley.Hash StandardCrypto ByteString) | ||
deriving (Eq, Show) | ||
|
||
instance HasTypeProxy DRepMetadata where | ||
data AsType DRepMetadata = AsDRepMetadata | ||
proxyToAsType _ = AsDRepMetadata | ||
|
||
instance SerialiseAsRawBytes (Hash DRepMetadata) where | ||
serialiseToRawBytes (DRepMetadataHash h) = Crypto.hashToBytes h | ||
|
||
deserialiseFromRawBytes (AsHash AsDRepMetadata) bs = | ||
maybeToRight (SerialiseAsRawBytesError "Unable to deserialise Hash DRepMetadata") $ | ||
DRepMetadataHash <$> Crypto.hashFromBytes bs | ||
|
||
-- | A drep metadata validation error. | ||
data DRepMetadataValidationError | ||
= DRepMetadataInvalidLengthError | ||
-- ^ The length of the JSON-encoded drep metadata exceeds the | ||
-- maximum. | ||
!Int | ||
-- ^ Maximum byte length. | ||
!Int | ||
-- ^ Actual byte length. | ||
deriving Show | ||
|
||
instance Error DRepMetadataValidationError where | ||
displayError = \case | ||
DRepMetadataInvalidLengthError maxLen actualLen -> | ||
mconcat | ||
[ "DRep metadata must consist of at most " | ||
, show maxLen | ||
, " bytes, but it consists of " | ||
, show actualLen | ||
, " bytes." | ||
] | ||
|
||
-- | Decode and validate the provided JSON-encoded bytes as 'DRepMetadata'. | ||
-- Return the decoded metadata and the hash of the original bytes. | ||
validateAndHashDRepMetadata | ||
:: ByteString | ||
-> Either DRepMetadataValidationError (DRepMetadata, Hash DRepMetadata) | ||
validateAndHashDRepMetadata bs | ||
-- TODO confirm if there are size limits to the DRep metadata | ||
| BS.length bs <= 512 = do | ||
let md = DRepMetadata bs | ||
let mdh = DRepMetadataHash (Crypto.hashWith id bs) | ||
return (md, mdh) | ||
| otherwise = Left $ DRepMetadataInvalidLengthError 512 (BS.length bs) |
83 changes: 83 additions & 0 deletions
83
cardano-api/internal/Cardano/Api/Governance/Actions/ProposalProcedure.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,83 @@ | ||
{-# LANGUAGE ExistentialQuantification #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
|
||
module Cardano.Api.Governance.Actions.ProposalProcedure where | ||
|
||
import Cardano.Api.Eras | ||
import Cardano.Api.Keys.Shelley | ||
import Cardano.Api.Utils | ||
import Cardano.Api.Value | ||
|
||
import qualified Cardano.Ledger.Conway.Governance as Gov | ||
import Cardano.Ledger.Core (EraCrypto) | ||
import Cardano.Ledger.Crypto (StandardCrypto) | ||
import Cardano.Ledger.SafeHash | ||
|
||
import Data.ByteString (ByteString) | ||
import Data.Maybe.Strict | ||
import Data.Proxy | ||
|
||
-- | A representation of whether the era supports tx governance actions. | ||
-- | ||
-- The Conway and subsequent eras support governance actions. | ||
-- | ||
data TxGovernanceActions era where | ||
TxGovernanceActionsNone :: TxGovernanceActions era | ||
|
||
TxGovernanceActions | ||
:: TxGovernanceActionSupportedInEra era | ||
-- (deposit, return address, governance action) | ||
-> [(Lovelace, Hash StakeKey, GovernanceAction)] | ||
-> TxGovernanceActions era | ||
|
||
deriving instance Show (TxGovernanceActions era) | ||
deriving instance Eq (TxGovernanceActions era) | ||
|
||
|
||
-- | A representation of whether the era supports transactions with governance | ||
-- actions. | ||
-- | ||
-- The Conway and subsequent eras support governance actions. | ||
-- | ||
data TxGovernanceActionSupportedInEra era where | ||
|
||
GovernanceActionsSupportedInConwayEra :: TxGovernanceActionSupportedInEra ConwayEra | ||
|
||
deriving instance Show (TxGovernanceActionSupportedInEra era) | ||
deriving instance Eq (TxGovernanceActionSupportedInEra era) | ||
|
||
|
||
data AnyGovernanceAction = forall era. AnyGovernanceAction (Gov.GovernanceAction era) | ||
|
||
-- TODO: Conway - fill in remaining actions | ||
data GovernanceAction | ||
= MotionOfNoConfidence | ||
| ProposeNewConstitution ByteString | ||
deriving (Eq, Show) | ||
|
||
toSafeHash :: ByteString -> SafeHash StandardCrypto ByteString | ||
toSafeHash = makeHashWithExplicitProxys (Proxy :: Proxy StandardCrypto) (Proxy :: Proxy ByteString) | ||
|
||
toGovernanceAction | ||
:: EraCrypto ledgerera ~ StandardCrypto | ||
=> GovernanceAction | ||
-> Gov.GovernanceAction ledgerera | ||
toGovernanceAction MotionOfNoConfidence = Gov.NoConfidence | ||
toGovernanceAction (ProposeNewConstitution bs) = | ||
Gov.NewConstitution $ toSafeHash bs | ||
|
||
createProposalProcedure | ||
:: ShelleyBasedEra era | ||
-> Lovelace -- ^ Deposit | ||
-> Hash StakeKey -- ^ Return address | ||
-> GovernanceAction | ||
-> Gov.ProposalProcedure (ShelleyLedgerEra era) | ||
createProposalProcedure sbe dep (StakeKeyHash retAddrh) govAct = | ||
obtainEraCryptoConstraints sbe $ | ||
Gov.ProposalProcedure | ||
{ Gov.pProcDeposit = toShelleyLovelace dep | ||
, Gov.pProcReturnAddr = retAddrh | ||
, Gov.pProcGovernanceAction = toGovernanceAction govAct | ||
, Gov.pProcAnchor = SNothing -- TODO: Conway | ||
} |
Oops, something went wrong.