From e4f2b002ec80a67f846f0cd7906795840b930058 Mon Sep 17 00:00:00 2001 From: Jordan Millar Date: Thu, 9 Jun 2022 13:12:04 -0500 Subject: [PATCH] Adjustments to the cli Temp modification of example-babbage-script-usage change to all script Spend a utxo at a simple script address with the reference script attached to said utxo Try spending utxo at simple script address with a reference script sitting at a different utxo fix --- .../src/Cardano/CLI/Shelley/Parsers.hs | 91 ++++++++------- .../Cardano/CLI/Shelley/Run/Transaction.hs | 13 ++- .../plutus/plutus-spending-script-example.md | 2 +- .../plutus/example-babbage-script-usage.sh | 104 +++++++++++------- 4 files changed, 129 insertions(+), 81 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs index 9d6df161282..973e1bb9b84 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs @@ -229,47 +229,37 @@ pScriptFor name (Just deprecated) help = <> Opt.internal ) -pSimpleReferenceScriptWitnessFiles - :: Parser (ScriptWitnessFiles witctx) -pSimpleReferenceScriptWitnessFiles = - toReferenceScriptWitnessFiles - <$> ((,) <$> pReferenceTxIn - <*> pSimpleScriptLang - ) - where - toReferenceScriptWitnessFiles - :: (TxIn, AnyScriptLanguage) - -> ScriptWitnessFiles witctx - toReferenceScriptWitnessFiles (txin, sLang) = SimpleReferenceScriptWitnessFiles txin sLang - - pSimpleScriptLang :: Parser AnyScriptLanguage - pSimpleScriptLang = - Opt.flag' (AnyScriptLanguage $ SimpleScriptLanguage SimpleScriptV1) - ( Opt.long "simple-script-v1" - <> Opt.help "Specify a simple script v1 reference script. \ - \See documentation at doc/reference/simple-scripts.md" - ) <|> - Opt.flag' (AnyScriptLanguage $ SimpleScriptLanguage SimpleScriptV2) - ( Opt.long "simple-script-v2" - <> Opt.help "Specify a simple script v2 reference script. \ - \See documentation at doc/reference/simple-scripts.md" - ) - -pPlutusReferenceScriptWitnessFiles +pReferenceScriptWitnessFiles :: WitCtx witctx -> BalanceTxExecUnits -- ^ Use the @execution-units@ flag. -> Parser (ScriptWitnessFiles witctx) -pPlutusReferenceScriptWitnessFiles witctx autoBalanceExecUnits = +pReferenceScriptWitnessFiles witctx autoBalanceExecUnits = toReferenceScriptWitnessFiles - <$> ( (,,,,) <$> pReferenceTxIn - <*> pPlutusScriptLanguage - <*> pScriptDatumOrFile "reference-tx-in" witctx - <*> pScriptRedeemerOrFile "reference-tx-in" - <*> (case autoBalanceExecUnits of - AutoBalance -> pure (ExecutionUnits 0 0) - ManualBalance -> pExecutionUnits "reference-tx-in") - ) + <$> pReferenceTxIn + <*> (simpleWit <|> pPWitness) + + where + pPWitness = + (,,,) <$> pPlutusScriptLanguage + <*> (Just <$> pScriptDatumOrFile "reference-tx-in" witctx) + <*> (Just <$> pScriptRedeemerOrFile "reference-tx-in") + <*> (case autoBalanceExecUnits of + AutoBalance -> pure $ Just (ExecutionUnits 0 0) + ManualBalance -> Just <$> pExecutionUnits "reference-tx-in") + + simpleWit + :: Parser ( AnyScriptLanguage + , Maybe (ScriptDatumOrFile witctx) + , Maybe ScriptRedeemerOrFile + , Maybe ExecutionUnits + ) + simpleWit = + (,,,) <$> pSimpleScriptLang + <*> pure Nothing + <*> pure Nothing + <*> pure Nothing + pPlutusScriptLanguage :: Parser AnyScriptLanguage pPlutusScriptLanguage = Opt.flag' (AnyScriptLanguage $ PlutusScriptLanguage PlutusScriptV2) @@ -277,10 +267,31 @@ pPlutusReferenceScriptWitnessFiles witctx autoBalanceExecUnits = <> Opt.help "Specify a plutus script v2 reference script." ) + pSimpleScriptLang :: Parser AnyScriptLanguage + pSimpleScriptLang = + Opt.flag' (AnyScriptLanguage $ SimpleScriptLanguage SimpleScriptV2) + ( Opt.long "simple-script" + <> Opt.help "Specify a simple script reference script. \ + \See documentation at doc/reference/simple-scripts.md" + ) + toReferenceScriptWitnessFiles - :: (TxIn, AnyScriptLanguage, ScriptDatumOrFile witctx, ScriptRedeemerOrFile, ExecutionUnits) + :: TxIn + -> ( AnyScriptLanguage + , Maybe (ScriptDatumOrFile witctx) + , Maybe ScriptRedeemerOrFile + , Maybe ExecutionUnits + ) -> ScriptWitnessFiles witctx - toReferenceScriptWitnessFiles (txin, sLang, d,r, e) = PlutusReferenceScriptWitnessFiles txin sLang d r e + toReferenceScriptWitnessFiles txin (sLang, mD, mR, mE) = + case sLang of + AnyScriptLanguage (SimpleScriptLanguage _) -> + SimpleReferenceScriptWitnessFiles txin sLang + AnyScriptLanguage (PlutusScriptLanguage _) -> + case (mD, mR, mE) of + (Just d, Just r, Just e) -> PlutusReferenceScriptWitnessFiles txin sLang d r e + (_,_,_) -> panic "toReferenceScriptWitnessFiles: Should not be possible" + pReferenceTxIn :: Parser TxIn pReferenceTxIn = @@ -2093,8 +2104,8 @@ pTxIn balance = pScriptWitnessFiles WitCtxTxIn balance "tx-in" (Just "txin") "the spending of the transaction input." <|> - pPlutusReferenceScriptWitnessFiles WitCtxTxIn balance <|> - pSimpleReferenceScriptWitnessFiles + pReferenceScriptWitnessFiles WitCtxTxIn balance + pTxInCollateral :: Parser TxIn pTxInCollateral = diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs b/cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs index eb9382f4c73..f22bb0b9c5c 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Run/Transaction.hs @@ -394,6 +394,10 @@ runTxBuildRaw (AnyCardanoEra era) let referenceInputsWithWits = [ (input, wit) | (_, wit@(Just (PlutusReferenceScriptWitnessFiles input _ _ _ _))) <- inputsAndScripts + ] ++ + [(input, wit) + | (_, wit@(Just (SimpleReferenceScriptWitnessFiles input _))) + <- inputsAndScripts ] txBodyContent <- @@ -482,6 +486,10 @@ runTxBuild (AnyCardanoEra era) (AnyConsensusModeParams cModeParams) networkId mS referenceInputsWithWits = [ (input, wit) | (_, wit@(Just (PlutusReferenceScriptWitnessFiles input _ _ _ _))) <- txins + ] ++ + [(input, wit) + | (_, wit@(Just (SimpleReferenceScriptWitnessFiles input _))) + <- txins ] referenceInputs = map fst referenceInputsWithWits case (consensusMode, cardanoEraStyle era) of @@ -697,7 +705,10 @@ validateTxInsReference era txins = return $ Just refIn PlutusScriptWitness _ _ PScript{} _ _ _ -> return Nothing - SimpleScriptWitness {} -> return Nothing + SimpleScriptWitness _ _ (SReferenceScript refIn) -> + return $ Just refIn + SimpleScriptWitness _ _ SScript{} -> + return Nothing Nothing -> return Nothing validateTxOuts :: forall era. diff --git a/doc/reference/plutus/plutus-spending-script-example.md b/doc/reference/plutus/plutus-spending-script-example.md index 0041bcb5104..fb7dc5374d2 100644 --- a/doc/reference/plutus/plutus-spending-script-example.md +++ b/doc/reference/plutus/plutus-spending-script-example.md @@ -113,7 +113,7 @@ cardano-cli transaction sign \ --out-file alonzo.tx ``` -If there is ADA at `$dummyaddress` then the Plutus script was successfully executed. Conversely, if the Plutus script failed, the collateral input would have been consumed. +If there is ADA at `$dummyaddress` then the Plutus script was successfully executed. You can use the [example-txin-locking-plutus-script.sh](../../../scripts/plutus/example-txin-locking-plutus-script.sh) in conjunction with [mkfiles.sh alonzo](../../../scripts/byron-to-alonzo/mkfiles.sh) script to automagically run the `AlwaysSucceeds` script. diff --git a/scripts/plutus/example-babbage-script-usage.sh b/scripts/plutus/example-babbage-script-usage.sh index 040b20dec23..01335df7eb1 100755 --- a/scripts/plutus/example-babbage-script-usage.sh +++ b/scripts/plutus/example-babbage-script-usage.sh @@ -10,14 +10,15 @@ export BASE="${BASE:-.}" export CARDANO_CLI="${CARDANO_CLI:-cardano-cli}" export CARDANO_NODE_SOCKET_PATH="${CARDANO_NODE_SOCKET_PATH:-example/node-spo1/node.sock}" export TESTNET_MAGIC="${TESTNET_MAGIC:-42}" -export UTXO_VKEY="${UTXO_VKEY:-example/stake-delegator-keys/payment1.vkey}" -export UTXO_SKEY="${UTXO_SKEY:-example/stake-delegator-keys/payment1.skey}" +export UTXO_VKEY="${UTXO_VKEY:-example/utxo-keys/utxo1.vkey}" +export UTXO_SKEY="${UTXO_SKEY:-example/utxo-keys/utxo1.skey}" export RESULT_FILE="${RESULT_FILE:-$WORK/result.out}" echo "Socket path: $CARDANO_NODE_SOCKET_PATH" echo "Socket path: $(pwd)" ls -al "$CARDANO_NODE_SOCKET_PATH" +mkdir -p "$WORK" plutusscriptinuse="$BASE/scripts/plutus/scripts/v2/required-redeemer.plutus" ## This datum hash is the hash of the untyped 42 @@ -25,6 +26,32 @@ scriptdatumhash="9e1199a988ba72ffd6e9c269cadb3b53b5f360ff99f112d9b2ee30c4d74ad88 datumfilepath="$BASE/scripts/plutus/data/42.datum" redeemerfilepath="$BASE/scripts/plutus/data/42.redeemer" echo "Script at: $plutusscriptinuse" + +simplescriptrequiredkeyhash=$($CARDANO_CLI address key-hash --payment-verification-key-file "$UTXO_VKEY") +simplescript=$WORK/simplescript + +echo "{" >> "$simplescript" +echo " \"type\": \"all\", ">> "$simplescript" +echo " \"scripts\": " >> "$simplescript" +echo " [" >> "$simplescript" +echo " {" >> "$simplescript" +echo " \"keyHash\": \"${simplescriptrequiredkeyhash}\"," >> "$simplescript" +echo " \"type\": \"sig\"" >> "$simplescript" +echo " }" >> "$simplescript" +echo " ]" >> "$simplescript" +echo "}" >> "$simplescript" + +simplescriptaddress=$($CARDANO_CLI address build --payment-script-file "$simplescript" --testnet-magic "$TESTNET_MAGIC") +# { +# "type": "all", +# "scripts": +# [ +# { +# "type": "sig", +# "keyHash": "d47e92b929075f48c3c9c43a239077b36f82c4ff6f73dd2f22945b56" +# } +# ] +# } # # # @@ -33,20 +60,20 @@ echo "Script at: $plutusscriptinuse" ## in order to accommodate this. # # -plutusscriptaddr=$($CARDANO_CLI address build --payment-script-file "$plutusscriptinuse" --testnet-magic "$TESTNET_MAGIC") -echo "Plutus Script Address" -echo "$plutusscriptaddr" -mkdir -p "$WORK" - -utxoaddr=$($CARDANO_CLI address build --testnet-magic "$TESTNET_MAGIC" --payment-verification-key-file "$UTXO_VKEY" --stake-verification-key-file example/stake-delegator-keys/staking1.vkey) +#plutusscriptaddr=$($CARDANO_CLI address build --payment-script-file "$plutusscriptinuse" --testnet-magic "$TESTNET_MAGIC") +#echo "Plutus Script Address" +#echo "$plutusscriptaddr" +echo "here 2" +utxoaddr=$($CARDANO_CLI address build --testnet-magic "$TESTNET_MAGIC" --payment-verification-key-file "$UTXO_VKEY") +echo "here 3" $CARDANO_CLI query utxo --address "$utxoaddr" --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/utxo-1.json cat $WORK/utxo-1.json - +echo "here 4" txin=$(jq -r 'keys[0]' $WORK/utxo-1.json) lovelaceattxin=$(jq -r ".[\"$txin\"].value.lovelace" $WORK/utxo-1.json) lovelaceattxindiv3=$(expr $lovelaceattxin / 4) - +echo "here 5" $CARDANO_CLI query protocol-parameters --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/pparams.json dummyaddress=addr_test1vpqgspvmh6m2m5pwangvdg499srfzre2dd96qq57nlnw6yctpasy4 @@ -60,11 +87,9 @@ $CARDANO_CLI transaction build \ --change-address "$utxoaddr" \ --tx-in "$txin" \ --tx-out "$utxoaddr+$lovelaceattxindiv3" \ - --tx-out "$plutusscriptaddr+$lovelaceattxindiv3" \ - --tx-out-inline-datum-file "$datumfilepath" \ + --tx-out "$simplescriptaddress+$lovelaceattxindiv3" \ --tx-out "$dummyaddress+$lovelaceattxindiv3" \ - --tx-out-inline-datum-file "$datumfilepath" \ - --tx-out-reference-script-file "$plutusscriptinuse" \ + --tx-out-reference-script-file "$simplescript" \ --protocol-params-file "$WORK/pparams.json" \ --out-file "$WORK/create-datum-output.body" @@ -79,10 +104,10 @@ $CARDANO_CLI transaction submit --tx-file $WORK/create-datum-output.tx --testnet echo "Pausing for 5 seconds..." sleep 5 -$CARDANO_CLI query utxo --address "$dummyaddress" --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/dummy-address-ref-script.json -cat $WORK/dummy-address-ref-script.json +$CARDANO_CLI query utxo --address "$dummyaddress" --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/ref-script-at-dummy-address.json +cat $WORK/ref-script-at-dummy-address.json # Get reference script txin -plutusreferencescripttxin=$(jq -r 'keys[0]' $WORK/dummy-address-ref-script.json) +simplereferencescripttxin=$(jq -r 'keys[0]' $WORK/ref-script-at-dummy-address.json) # Step 2 # After "locking" the tx output at the script address, let's spend the utxo as the script address using the corresponding reference script @@ -95,29 +120,33 @@ txinCollateral=$(jq -r 'keys[1]' $WORK/utxo-2.json) lovelaceattxin1=$(jq -r ".[\"$txin1\"].value.lovelace" $WORK/utxo-2.json) lovelaceattxindiv31=$(expr $lovelaceattxin1 / 3) +#Get input at simple script address +$CARDANO_CLI query utxo --address $simplescriptaddress --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/simple-script-utxo.json +simplelockedutxotxin=$(jq -r 'keys[0]' $WORK/simple-script-utxo.json) +lovelaceatsimplecriptaddr=$(jq -r ".[\"$simplelockedutxotxin\"].value.lovelace" $WORK/simple-script-utxo.json) # Get input at plutus script that we will attempt to spend -$CARDANO_CLI query utxo --address $plutusscriptaddr --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/plutusutxo.json -plutuslockedutxotxin=$(jq -r 'keys[0]' $WORK/plutusutxo.json) -lovelaceatplutusscriptaddr=$(jq -r ".[\"$plutuslockedutxotxin\"].value.lovelace" $WORK/plutusutxo.json) +#$CARDANO_CLI query utxo --address $plutusscriptaddr --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/plutusutxo.json +#plutuslockedutxotxin=$(jq -r 'keys[0]' $WORK/plutusutxo.json) +#lovelaceatplutusscriptaddr=$(jq -r ".[\"$plutuslockedutxotxin\"].value.lovelace" $WORK/plutusutxo.json) dummyaddress2=addr_test1vzq57nyrwdwne9vzjxr908qqkdxwuavlgzl20qveua303vq024qkk -echo "Plutus txin" -echo "$plutuslockedutxotxin" +#echo "Plutus txin" +#echo "$plutuslockedutxotxin" -echo "Collateral" -echo "$txinCollateral" +#echo "Collateral" +#echo "$txinCollateral" echo "Funding utxo" echo "$txin1" -echo "Plutus reference script txin" -echo "$plutusreferencescripttxin" +#echo "Plutus reference script txin" +#echo "$simplereferencescripttxin" -echo "Plutus input we are trying to spend" -echo "$plutuslockedutxotxin" +#echo "Plutus input we are trying to spend" +#echo "$plutuslockedutxotxin" # Alternative build-raw method #$CARDANO_CLI transaction build-raw \ @@ -127,7 +156,7 @@ echo "$plutuslockedutxotxin" # --tx-in-collateral "$txinCollateral" \ # --out-file "$WORK/test-alonzo-ref-script.body" \ # --tx-in "$plutuslockedutxotxin" \ -# --tx-in-reference "$plutusreferencescripttxin" \ +# --tx-in-reference "$simplereferencescripttxin" \ # --plutus-script-v2 \ # --reference-tx-in-datum-file "$datumfilepath" \ # --reference-tx-in-redeemer-file "$redeemerfilepath" \ @@ -141,27 +170,24 @@ $CARDANO_CLI transaction build \ --babbage-era \ --cardano-mode \ --testnet-magic "$TESTNET_MAGIC" \ - --change-address "$utxoaddr" \ --tx-in "$txin1" \ - --tx-in-collateral "$txinCollateral" \ - --out-file "$WORK/test-alonzo-ref-script.body" \ - --tx-in "$plutuslockedutxotxin" \ - --tx-in-reference "$plutusreferencescripttxin" \ - --plutus-script-v2 \ - --reference-tx-in-inline-datum-present \ - --reference-tx-in-redeemer-file "$redeemerfilepath" \ + --change-address "$utxoaddr" \ + --tx-in "$simplelockedutxotxin" \ + --tx-in-reference "$simplereferencescripttxin" \ + --simple-script \ --tx-out "$dummyaddress2+10000000" \ + --out-file "$WORK/test-alonzo-ref-script.body" \ --protocol-params-file "$WORK/pparams.json" $CARDANO_CLI transaction sign \ --tx-body-file $WORK/test-alonzo-ref-script.body \ --testnet-magic "$TESTNET_MAGIC" \ --signing-key-file "${UTXO_SKEY}" \ - --out-file $WORK/alonzo-ref-script.tx + --out-file $WORK/babbage-ref-script.tx # SUBMIT $WORK/alonzo.tx echo "Submit the tx using reference script and wait 5 seconds..." -$CARDANO_CLI transaction submit --tx-file $WORK/alonzo-ref-script.tx --testnet-magic "$TESTNET_MAGIC" +$CARDANO_CLI transaction submit --tx-file $WORK/babbage-ref-script.tx --testnet-magic "$TESTNET_MAGIC" sleep 5 echo "" echo "Querying UTxO at $dummyaddress2. If there is ADA at the address the Plutus reference script successfully executed!"