Skip to content

Commit

Permalink
Add to first commit! Creation of read module
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jan 22, 2025
1 parent 2fd6517 commit d69a6f4
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Script/Certificate/Read.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TupleSections #-}

module Cardano.CLI.EraBased.Script.Certificate.Read
( readCertificateScriptWitness
, readCertificateScriptWitnesses
)
where

import Cardano.Api
import Cardano.Api.Shelley

import Cardano.CLI.EraBased.Script.Certificate.Types
import Cardano.CLI.EraBased.Script.Types
import Cardano.CLI.Read
import Cardano.CLI.Types.Common

import Control.Monad

readCertificateScriptWitnesses
:: MonadIOTransError (FileError CliScriptWitnessError) t m
=> ShelleyBasedEra era
-> [(CertificateFile, Maybe CliCertificateScriptRequirements)]
-> t m [(CertificateFile, Maybe (CertificateScriptWitness era))]
readCertificateScriptWitnesses sbe =
mapM
( \(certFile, mSWit) -> do
(certFile,) <$> forM mSWit (readCertificateScriptWitness sbe)
)

readCertificateScriptWitness
:: MonadIOTransError (FileError CliScriptWitnessError) t m
=> ShelleyBasedEra era -> CliCertificateScriptRequirements -> t m (CertificateScriptWitness era)
readCertificateScriptWitness sbe certScriptReq =
case certScriptReq of
OnDiskSimpleScript scriptFp -> do
let sFp = unFile scriptFp
s <-
modifyError (fmap SimpleScriptWitnessDecodeError) $
readFileSimpleScript sFp
case s of
SimpleScript ss -> do
return $
CertificateScriptWitness $
SimpleScriptWitness (sbeToSimpleScriptLanguageInEra sbe) $
SScript ss
OnDiskPlutusScript (OnDiskPlutusScriptCliArgs scriptFp redeemerFile execUnits) -> do
let plutusScriptFp = unFile scriptFp
plutusScript <-
modifyError (fmap PlutusScriptWitnessDecodeError) $
readFilePlutusScript plutusScriptFp
redeemer <-
modifyError (FileError plutusScriptFp . PlutusScriptWitnessRedeemerError) $
readScriptDataOrFile redeemerFile
case plutusScript of
AnyPlutusScript lang script -> do
let pScript = PScript script
sLangSupported <-
modifyError (FileError plutusScriptFp)
$ hoistMaybe
( PlutusScriptWitnessLanguageNotSupportedInEra
(AnyPlutusScriptVersion lang)
(shelleyBasedEraConstraints sbe $ AnyShelleyBasedEra sbe)
)
$ scriptLanguageSupportedInEra sbe
$ PlutusScriptLanguage lang
return $
CertificateScriptWitness $
PlutusScriptWitness
sLangSupported
lang
pScript
NoScriptDatumForStake
redeemer
execUnits
OnDiskPlutusRefScript (PlutusRefScriptCliArgs refTxIn anyPlutusScriptVersion redeemerFile execUnits) -> do
case anyPlutusScriptVersion of
AnyPlutusScriptVersion lang -> do
let pScript = PReferenceScript refTxIn
redeemer <-
-- TODO: Implement a new error type to capture this. FileError is not representative of cases
-- where we do not have access to the script.
modifyError
( FileError "Reference script filepath not available"
. PlutusScriptWitnessRedeemerError
)
$ readScriptDataOrFile redeemerFile
sLangSupported <-
-- TODO: Implement a new error type to capture this. FileError is not representative of cases
-- where we do not have access to the script.
modifyError (FileError "Reference script filepath not available")
$ hoistMaybe
( PlutusScriptWitnessLanguageNotSupportedInEra
(AnyPlutusScriptVersion lang)
(shelleyBasedEraConstraints sbe $ AnyShelleyBasedEra sbe)
)
$ scriptLanguageSupportedInEra sbe
$ PlutusScriptLanguage lang

return $
CertificateScriptWitness $
PlutusScriptWitness
sLangSupported
lang
pScript
NoScriptDatumForStake
redeemer
execUnits

0 comments on commit d69a6f4

Please sign in to comment.