|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Unofficial bash strict mode. |
| 4 | +# See: http://redsymbol.net/articles/unofficial-bash-strict-mode/ |
| 5 | +set -e |
| 6 | +set -o pipefail |
| 7 | + |
| 8 | +export WORK="${WORK:-example/work}" |
| 9 | +export BASE="${BASE:-.}" |
| 10 | +export CARDANO_CLI="${CARDANO_CLI:-cardano-cli}" |
| 11 | +export CARDANO_NODE_SOCKET_PATH="${CARDANO_NODE_SOCKET_PATH:-example/main.sock}" |
| 12 | +export TESTNET_MAGIC="${TESTNET_MAGIC:-42}" |
| 13 | +export UTXO_VKEY="${UTXO_VKEY:-example/utxo-keys/utxo1.vkey}" |
| 14 | +export UTXO_SKEY="${UTXO_SKEY:-example/utxo-keys/utxo1.skey}" |
| 15 | +export RESULT_FILE="${RESULT_FILE:-$WORK/result.out}" |
| 16 | + |
| 17 | +mkdir -p "$WORK" |
| 18 | + |
| 19 | +echo "Socket path: $CARDANO_NODE_SOCKET_PATH" |
| 20 | +echo "Socket path: $(pwd)" |
| 21 | + |
| 22 | +ls -al "$CARDANO_NODE_SOCKET_PATH" |
| 23 | + |
| 24 | +# NB: This plutus script uses a "typed" redeemer and "typed" datum. |
| 25 | +plutusscriptinuse="$BASE/scripts/plutus/scripts/v2/context-equivalence-test.plutus" |
| 26 | +# This datum hash is the hash of the typed 42 |
| 27 | +scriptdatumhash="fcaa61fb85676101d9e3398a484674e71c45c3fd41b492682f3b0054f4cf3273" |
| 28 | +datumfilepath="$BASE/scripts/plutus/data/typed-42.datum" |
| 29 | +redeemerfilepath="$BASE/scripts/plutus/data/script-context.redeemer" |
| 30 | +certifyingscript="scripts/plutus/scripts/v2/stake-script.plutus" |
| 31 | + |
| 32 | +# Create certificate |
| 33 | +cardano-cli stake-address registration-certificate \ |
| 34 | + --stake-script-file "$certifyingscript" \ |
| 35 | + --out-file "$WORK/script.regcert" |
| 36 | + |
| 37 | +# Step 1: Create a tx output with a datum hash at the script address. In order for a tx output to be locked |
| 38 | +# by a plutus script, it must have a datahash. We also need collateral tx inputs so we split the utxo |
| 39 | +# in order to accommodate this. |
| 40 | + |
| 41 | + |
| 42 | +plutusscriptaddr=$($CARDANO_CLI address build --payment-script-file "$plutusscriptinuse" --testnet-magic "$TESTNET_MAGIC") |
| 43 | +readonlyaddress=addr_test1vz3t3f2kgy2re66tnhgxc4t8jgylw2cqfnxdwlrq9agfmtstxxkm5 |
| 44 | + |
| 45 | +mkdir -p "$WORK" |
| 46 | + |
| 47 | +utxoaddr=$($CARDANO_CLI address build --testnet-magic "$TESTNET_MAGIC" --payment-verification-key-file "$UTXO_VKEY") |
| 48 | + |
| 49 | +$CARDANO_CLI query utxo --address "$utxoaddr" --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/utxo-1.json |
| 50 | +cat $WORK/utxo-1.json |
| 51 | + |
| 52 | +txin=$(jq -r 'keys[]' $WORK/utxo-1.json) |
| 53 | +lovelaceattxin=$(jq -r ".[\"$txin\"].value.lovelace" $WORK/utxo-1.json) |
| 54 | +lovelaceattxindiv6=$(expr $lovelaceattxin / 6) |
| 55 | + |
| 56 | +$CARDANO_CLI query protocol-parameters --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/pparams.json |
| 57 | + |
| 58 | +$CARDANO_CLI transaction build \ |
| 59 | + --babbage-era \ |
| 60 | + --cardano-mode \ |
| 61 | + --testnet-magic "$TESTNET_MAGIC" \ |
| 62 | + --change-address "$utxoaddr" \ |
| 63 | + --tx-in "$txin" \ |
| 64 | + --tx-out "$readonlyaddress+$lovelaceattxindiv6" \ |
| 65 | + --tx-out "$plutusscriptaddr+$lovelaceattxindiv6" \ |
| 66 | + --tx-out-datum-hash "$scriptdatumhash" \ |
| 67 | + --tx-out "$utxoaddr+$lovelaceattxindiv6" \ |
| 68 | + --protocol-params-file "$WORK/pparams.json" \ |
| 69 | + --out-file "$WORK/create-datum-output.body" |
| 70 | + |
| 71 | +$CARDANO_CLI transaction sign \ |
| 72 | + --tx-body-file $WORK/create-datum-output.body \ |
| 73 | + --testnet-magic "$TESTNET_MAGIC" \ |
| 74 | + --signing-key-file $UTXO_SKEY \ |
| 75 | + --out-file $WORK/create-datum-output.tx |
| 76 | + |
| 77 | +# SUBMIT |
| 78 | +$CARDANO_CLI transaction submit --tx-file $WORK/create-datum-output.tx --testnet-magic "$TESTNET_MAGIC" |
| 79 | +echo "Pausing for 5 seconds..." |
| 80 | +sleep 5 |
| 81 | + |
| 82 | +# Step 2 |
| 83 | +# After "locking" the tx output at the script address, we can now can attempt to spend |
| 84 | +# the "locked" tx output below. |
| 85 | + |
| 86 | +$CARDANO_CLI query utxo --address $plutusscriptaddr --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/plutusutxo.json |
| 87 | + |
| 88 | +plutusutxotxin=$(jq -r 'keys[]' $WORK/plutusutxo.json) |
| 89 | + |
| 90 | +$CARDANO_CLI query utxo --address $utxoaddr --cardano-mode --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/utxo-2.json |
| 91 | +cat $WORK/utxo-2.json |
| 92 | +txinCollateral=$(jq -r 'keys[0]' $WORK/utxo-2.json) |
| 93 | + |
| 94 | + |
| 95 | +dummyaddress=addr_test1vpqgspvmh6m2m5pwangvdg499srfzre2dd96qq57nlnw6yctpasy4 |
| 96 | + |
| 97 | +lovelaceatplutusscriptaddr=$(jq -r ".[\"$plutusutxotxin\"].value.lovelace" $WORK/plutusutxo.json) |
| 98 | + |
| 99 | +#Get read only reference input |
| 100 | +$CARDANO_CLI query utxo --address "$readonlyaddress" --cardano-mode \ |
| 101 | + --testnet-magic "$TESTNET_MAGIC" --out-file $WORK/read-only-ref-input-utxo.json |
| 102 | +readonlyrefinput=$(jq -r 'keys[0]' $WORK/read-only-ref-input-utxo.json) |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +# We need to generate a dummy redeemer in order to create a txbody from which we can generate |
| 107 | +# a tx and then derive the correct redeemer. TODO: Why? The cli demands you specify a redeemer (double check this) |
| 108 | +create-script-context --plutus-v2 --out-file "$WORK/script-context.redeemer" |
| 109 | + |
| 110 | +correctredeemer="$WORK/script-context.redeemer" |
| 111 | + |
| 112 | +echo "Constructing dummy tx..." |
| 113 | + |
| 114 | +# Create dummy certificate |
| 115 | +certifyingscript="scripts/plutus/scripts/v2/stake-script.plutus" |
| 116 | +cardano-cli stake-address registration-certificate \ |
| 117 | + --stake-script-file "$certifyingscript" \ |
| 118 | + --out-file "$WORK/script.regcert" |
| 119 | + |
| 120 | +requiredsignerhash=$(cardano-cli address key-hash --payment-verification-key-file ${UTXO_VKEY}) |
| 121 | + |
| 122 | +# DUMMY TX! We generate the actual redeemer from this! |
| 123 | +# --certificate-file "$WORK/script.regcert" \ |
| 124 | +redeemerfilepath="$BASE/scripts/plutus/data/42.redeemer" |
| 125 | +$CARDANO_CLI transaction build \ |
| 126 | + --babbage-era \ |
| 127 | + --cardano-mode \ |
| 128 | + --testnet-magic "$TESTNET_MAGIC" \ |
| 129 | + --script-invalid \ |
| 130 | + --change-address "$utxoaddr" \ |
| 131 | + --invalid-before 1 \ |
| 132 | + --invalid-hereafter 400 \ |
| 133 | + --certificate-file "$WORK/script.regcert" \ |
| 134 | + --read-only-tx-in-reference "$readonlyrefinput" \ |
| 135 | + --tx-in "$plutusutxotxin" \ |
| 136 | + --tx-in-collateral "$txinCollateral" \ |
| 137 | + --tx-out "$dummyaddress+10000000" \ |
| 138 | + --tx-in-script-file "$plutusscriptinuse" \ |
| 139 | + --tx-in-datum-file "$datumfilepath" \ |
| 140 | + --protocol-params-file "$WORK/pparams.json" \ |
| 141 | + --tx-in-redeemer-file "$redeemerfilepath" \ |
| 142 | + --required-signer-hash "$requiredsignerhash" \ |
| 143 | + --out-file $WORK/test-alonzo.body |
| 144 | + |
| 145 | +$CARDANO_CLI transaction sign \ |
| 146 | + --tx-body-file $WORK/test-alonzo.body \ |
| 147 | + --testnet-magic "$TESTNET_MAGIC" \ |
| 148 | + --signing-key-file "${UTXO_SKEY}" \ |
| 149 | + --out-file $WORK/test-alonzo.tx |
| 150 | + |
| 151 | +# Generate the "real" redeeemer! |
| 152 | + |
| 153 | +create-script-context \ |
| 154 | + --generate-tx "$WORK/test-alonzo.tx" \ |
| 155 | + --plutus-v2 \ |
| 156 | + --out-file "$WORK/script-context.redeemer" \ |
| 157 | + --cardano-mode \ |
| 158 | + --testnet-magic 42 \ |
| 159 | + |
| 160 | +echo "Constructing real tx..." |
| 161 | +# REAL TX! |
| 162 | + |
| 163 | +$CARDANO_CLI transaction build \ |
| 164 | + --babbage-era \ |
| 165 | + --cardano-mode \ |
| 166 | + --testnet-magic "$TESTNET_MAGIC" \ |
| 167 | + --script-valid \ |
| 168 | + --invalid-before 1 \ |
| 169 | + --invalid-hereafter 400 \ |
| 170 | + --change-address "$utxoaddr" \ |
| 171 | + --certificate-file "$WORK/script.regcert" \ |
| 172 | + --read-only-tx-in-reference "$readonlyrefinput" \ |
| 173 | + --tx-in "$plutusutxotxin" \ |
| 174 | + --tx-in-collateral "$txinCollateral" \ |
| 175 | + --tx-out "$dummyaddress+10000000" \ |
| 176 | + --tx-in-script-file "$plutusscriptinuse" \ |
| 177 | + --tx-in-datum-file "$datumfilepath" \ |
| 178 | + --protocol-params-file "$WORK/pparams.json" \ |
| 179 | + --tx-in-redeemer-file "$correctredeemer" \ |
| 180 | + --required-signer-hash "$requiredsignerhash" \ |
| 181 | + --out-file $WORK/test-alonzo-final.body |
| 182 | + |
| 183 | +$CARDANO_CLI transaction sign \ |
| 184 | + --tx-body-file $WORK/test-alonzo-final.body \ |
| 185 | + --testnet-magic "$TESTNET_MAGIC" \ |
| 186 | + --signing-key-file "${UTXO_SKEY}" \ |
| 187 | + --out-file $WORK/alonzo.tx |
| 188 | + |
| 189 | +# SUBMIT $WORK/alonzo.tx containing the correct redeemer |
| 190 | + |
| 191 | +echo "Submit the tx with plutus script and wait 5 seconds..." |
| 192 | +$CARDANO_CLI transaction submit --tx-file $WORK/alonzo.tx --testnet-magic "$TESTNET_MAGIC" |
| 193 | +sleep 5 |
| 194 | +echo "" |
| 195 | +echo "Querying UTxO at $dummyaddress. If there is ADA at the address the Plutus script successfully executed!" |
| 196 | +echo "" |
| 197 | +$CARDANO_CLI query utxo --address "$dummyaddress" --testnet-magic "$TESTNET_MAGIC" \ |
| 198 | + | tee "$RESULT_FILE" |
0 commit comments