Commit a Script UTxO into a Head
This guide provides a walkthrough on how to commit a UTxO from a script into a Hydra Head.
A Script UTxO is a special kind of Unspent Transaction Output (UTxO) that isn't controlled by a simple private key. Instead, it is locked by a script - a small program that runs on the blockchain. To spend the funds in a Script UTxO, you must provide data (a "redeemer" and "datum") that satisfies the conditions defined in that script.
Committing a script UTxO to a Hydra Head is a powerful feature that allows for more complex on-chain validation logic to be brought into the Head. This is useful for scenarios where you need to enforce specific rules on how funds can be spent, even within the Head's off-chain environment.
This tutorial will guide you through the entire process, which involves creating a script, locking an on-chain UTxO with it, and then preparing a special "blueprint" transaction to commit this script UTxO into an open Head.
Prerequisites
This tutorial assumes you are familiar with the process of committing a standard UTxO to a Hydra Head. If you are not, please first read the Commit using a blueprint tutorial.
You will also need:
- A running
cardano-node
connected to a testnet. cardano-cli
in yourPATH
.hydra-node
andhydra-tui
in yourPATH
.
Step 1: Set Up Your Environment
To avoid specifying the network identifier and the path to the node's socket in every command, you can set the following environment variables in your shell. Make sure to replace the values with the ones that are appropriate for your setup (e.g., for the pre-production testnet, the magic is 1
).
export CARDANO_NODE_SOCKET_PATH=<path-to>/node.socket
export CARDANO_TESTNET_MAGIC=1
export CREDENTIALS_PATH=hydra-cluster/config/credentials
Step 2: Create the script
For this tutorial, we will use a simple "always true" validator script. This script will always succeed, regardless of the redeemer or datum.
Create a file named always-true.plutus
with the following content:
{
"type": "PlutusScriptV2",
"description": "Always true validator",
"cborHex": "49480100002221200101"
}
Step 3: Create the script address
Now, we need to create an address for this script. We can do this using cardano-cli
:
cardano-cli address build \
--payment-script-file always-true.plutus \
--testnet-magic $CARDANO_TESTNET_MAGIC \
--out-file script.addr
This will create a file named script.addr
containing the script address. You can inspect the address using cat
:
cat script.addr
Step 4: Lock funds in the script address
Before we can commit a script UTxO, we need to create one. This is done by sending funds to the script address.
First, we need a UTxO in a wallet to fund the transaction. The following command will find the first available UTxO in a wallet funded by alice-funds.sk
, and export its transaction input (TxIn
) to an environment variable.
export UTXO_TXIX=$(cardano-cli query utxo --address $(cardano-cli address build --payment-verification-key-file ${CREDENTIALS_PATH}/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC) --testnet-magic $CARDANO_TESTNET_MAGIC --output-json | jq -r 'keys[0]')
echo "Captured funding UTxO TxIn: $UTXO_TXIX"
Now that we have a funding UTxO, we can build the transaction to lock 10 ADA at the script address.
First, create a file named datum.json
that contains the datum. For this example, we will just use the integer 42
.
{
"constructor": 0,
"fields": [
{
"int": 42
}
]
}
Now, build the transaction:
cardano-cli conway transaction build \
--tx-in $UTXO_TXIX \
--tx-out $(cat script.addr)+10000000 \
--tx-out-inline-datum-file datum.json \
--change-address $(cardano-cli address build --payment-verification-key-file ${CREDENTIALS_PATH}/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC) \
--testnet-magic $CARDANO_TESTNET_MAGIC \
--out-file tx.raw
Note that we are also providing an inline datum via the datum.json
file. Even though our script does not use it, a datum is required for all script UTxOs.
Now, sign and submit the transaction:
cardano-cli conway transaction sign \
--tx-body-file tx.raw \
--signing-key-file ${CREDENTIALS_PATH}/alice-funds.sk \
--out-file tx.signed
cardano-cli conway transaction submit \
--tx-file tx.signed \
--testnet-magic $CARDANO_TESTNET_MAGIC
Once the transaction is confirmed, you can query the script address to find the transaction input (TxIn
) of the script UTxO we just created.
export SCRIPT_UTXO_TXIX=$(cardano-cli query utxo --address $(cat script.addr) --testnet-magic $CARDANO_TESTNET_MAGIC --output-json | jq -r 'keys[0]')
echo "Captured script UTxO TxIn: $SCRIPT_UTXO_TXIX"