Skip to content

Commit

Permalink
Add cardano-cli transaction hash-script-data
Browse files Browse the repository at this point in the history
Needed as a util for helping to build txs that use Plutus scripts.
  • Loading branch information
dcoutts committed Jun 14, 2021
1 parent 2cf9e82 commit a350aa6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Expand Up @@ -201,6 +201,8 @@ data TransactionCmd
| TxCalculateMinValue
ProtocolParamsSourceSpec
Value
| TxHashScriptData
ScriptDataOrFile
| TxGetTxId InputTxFile
| TxView InputTxFile
deriving Show
Expand Down Expand Up @@ -230,6 +232,7 @@ renderTransactionCmd cmd =
TxMintedPolicyId {} -> "transaction policyid"
TxCalculateMinFee {} -> "transaction calculate-min-fee"
TxCalculateMinValue {} -> "transaction calculate-min-value"
TxHashScriptData {} -> "transaction hash-script-data"
TxGetTxId {} -> "transaction txid"
TxView {} -> "transaction view"

Expand Down
49 changes: 28 additions & 21 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Expand Up @@ -253,31 +253,40 @@ pScriptWitnessFiles witctx scriptFlagPrefix scriptFlagPrefixDeprecated help =
pScriptDatumOrFile :: Parser (ScriptDatumOrFile witctx)
pScriptDatumOrFile =
case witctx of
WitCtxTxIn -> ScriptDatumOrFileForTxIn <$> pScriptDataOrFile "datum"
WitCtxTxIn -> ScriptDatumOrFileForTxIn <$>
pScriptDataOrFile (scriptFlagPrefix ++ "-datum")
WitCtxMint -> pure NoScriptDatumOrFileForMint
WitCtxStake -> pure NoScriptDatumOrFileForStake

pScriptRedeemerOrFile :: Parser ScriptDataOrFile
pScriptRedeemerOrFile = pScriptDataOrFile "redeemer"
pScriptRedeemerOrFile = pScriptDataOrFile (scriptFlagPrefix ++ "-redeemer")

pScriptDataOrFile :: String -> Parser ScriptDataOrFile
pScriptDataOrFile dataFlagPrefix =
ScriptDataFile <$> pScriptDataFile dataFlagPrefix
<|> ScriptDataValue <$> pScriptDataValue dataFlagPrefix
pExecutionUnits :: Parser ExecutionUnits
pExecutionUnits =
uncurry ExecutionUnits <$>
Opt.option Opt.auto
( Opt.long (scriptFlagPrefix ++ "-execution-units")
<> Opt.metavar "(INT, INT)"
<> Opt.help "The time and space units needed by the script."
)

pScriptDataFile dataFlagPrefix =
pScriptDataOrFile :: String -> Parser ScriptDataOrFile
pScriptDataOrFile dataFlagPrefix =
ScriptDataFile <$> pScriptDataFile
<|> ScriptDataValue <$> pScriptDataValue
where
pScriptDataFile =
Opt.strOption
( Opt.long (scriptFlagPrefix ++ "-" ++ dataFlagPrefix ++ "-file")
( Opt.long (dataFlagPrefix ++ "-file")
<> Opt.metavar "FILE"
<> Opt.help ("The file containing the script input "
++ dataFlagPrefix ++ ".")
<> Opt.help "The file containing the script data."
)

pScriptDataValue dataFlagPrefix =
pScriptDataValue =
Opt.option readerScriptData
( Opt.long (scriptFlagPrefix ++ "-" ++ dataFlagPrefix ++ "-value")
( Opt.long (dataFlagPrefix ++ "-value")
<> Opt.metavar "JSON"
<> Opt.help ("The value for the script input " ++ dataFlagPrefix ++ ".")
<> Opt.help "The value (in JSON syntax) for the script data."
)

readerScriptData = do
Expand All @@ -286,14 +295,6 @@ pScriptWitnessFiles witctx scriptFlagPrefix scriptFlagPrefixDeprecated help =
Left err -> fail (displayError err)
Right sd -> return sd

pExecutionUnits :: Parser ExecutionUnits
pExecutionUnits =
uncurry ExecutionUnits <$>
Opt.option Opt.auto
( Opt.long (scriptFlagPrefix ++ "-execution-units")
<> Opt.metavar "(INT, INT)"
<> Opt.help "The time and space units needed by the script."
)

pStakeAddressCmd :: Parser StakeAddressCmd
pStakeAddressCmd =
Expand Down Expand Up @@ -572,6 +573,9 @@ pTransaction =
(Opt.info pTransactionCalculateMinFee $ Opt.progDesc "Calculate the minimum fee for a transaction")
, subParser "calculate-min-value"
(Opt.info pTransactionCalculateMinValue $ Opt.progDesc "Calculate the minimum value for a transaction")

, subParser "hash-script-data"
(Opt.info pTxHashScriptData $ Opt.progDesc "Calculate the hash of script data")
, subParser "txid"
(Opt.info pTransactionId $ Opt.progDesc "Print a transaction identifier")
, subParser "view" $
Expand Down Expand Up @@ -660,6 +664,9 @@ pTransaction =
<|>
ParamsFromFile <$> pProtocolParamsFile

pTxHashScriptData :: Parser TransactionCmd
pTxHashScriptData = TxHashScriptData <$> pScriptDataOrFile "script-data"

pTransactionId :: Parser TransactionCmd
pTransactionId = TxGetTxId <$> pInputTxFile

Expand Down
20 changes: 20 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs
Expand Up @@ -249,6 +249,7 @@ runTransactionCmd cmd =
runTxCalculateMinFee txbody mnw pGenesisOrParamsFile nInputs nOutputs
nShelleyKeyWitnesses nByronKeyWitnesses
TxCalculateMinValue pParamSpec txOuts -> runTxCalculateMinValue pParamSpec txOuts
TxHashScriptData scriptDataOrFile -> runTxHashScriptData scriptDataOrFile
TxGetTxId txinfile -> runTxGetTxId txinfile
TxView txinfile -> runTxView txinfile
TxMintedPolicyId sFile -> runTxCreatePolicyId sFile
Expand Down Expand Up @@ -866,6 +867,11 @@ readProtocolParameters (ProtocolParamsFile fpath) = do
firstExceptT (ShelleyTxCmdAesonDecodeProtocolParamsError fpath . Text.pack) . hoistEither $
Aeson.eitherDecode' pparams


-- ----------------------------------------------------------------------------
-- Witness handling
--

data SomeWitness
= AByronSigningKey (SigningKey ByronKey) (Maybe (Address ByronAddr))
| APaymentSigningKey (SigningKey PaymentKey)
Expand Down Expand Up @@ -1046,6 +1052,15 @@ mkShelleyBootstrapWitnesses mnw txBody =
mapM (mkShelleyBootstrapWitness mnw txBody)


-- ----------------------------------------------------------------------------
-- Other misc small commands
--

runTxHashScriptData :: ScriptDataOrFile -> ExceptT ShelleyTxCmdError IO ()
runTxHashScriptData scriptDataOrFile = do
d <- readScriptDataOrFile scriptDataOrFile
liftIO $ BS.putStrLn $ serialiseToRawBytesHex (hashScriptData d)

runTxGetTxId :: InputTxFile -> ExceptT ShelleyTxCmdError IO ()
runTxGetTxId txfile = do
InAnyCardanoEra _era txbody <-
Expand All @@ -1067,6 +1082,11 @@ runTxView txfile = do
return . InAnyCardanoEra era $ getTxBody tx
liftIO $ BS.putStr $ friendlyTxBodyBS era txbody


-- ----------------------------------------------------------------------------
-- Witness commands
--

runTxCreateWitness
:: TxBodyFile
-> WitnessSigningData
Expand Down

0 comments on commit a350aa6

Please sign in to comment.