Skip to content

Commit bb8e9a3

Browse files
committed
Final working version
1 parent 937b400 commit bb8e9a3

File tree

1 file changed

+45
-28
lines changed

1 file changed

+45
-28
lines changed

docs/docs/how-to/commit-script-utxo.md

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ To avoid specifying the network identifier and the path to the node's socket in
2929
```shell
3030
export CARDANO_NODE_SOCKET_PATH=<path-to>/node.socket
3131
export CARDANO_TESTNET_MAGIC=1
32+
export CREDENTIALS_PATH=hydra-cluster/config/credentials
3233
```
3334

3435
## Step 2: Create the script
@@ -66,15 +67,14 @@ cat script.addr
6667

6768
Before we can commit a script UTxO, we need to create one. This is done by sending funds to the script address.
6869

69-
First, find a UTxO in your wallet that you can use. You can query your wallet's UTxOs using `cardano-cli`. For this example, we will use `alice-funds.sk`:
70+
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.
7071

7172
```shell
72-
cardano-cli query utxo \
73-
--address $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC) \
74-
--testnet-magic $CARDANO_TESTNET_MAGIC
73+
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]')
74+
echo "Captured funding UTxO TxIn: $UTXO_TXIX"
7575
```
7676

77-
Pick a UTxO from the output and use it to build a transaction that sends funds to the script address. Let's say you picked a UTxO with TxHash `<UTXO_TXIX>` containing `100 ADA`. We will send `10 ADA` to the script address.
77+
Now that we have a funding UTxO, we can build the transaction to lock 10 ADA at the script address.
7878

7979
First, create a file named `datum.json` that contains the datum. For this example, we will just use the integer `42`.
8080

@@ -93,10 +93,10 @@ Now, build the transaction:
9393

9494
```shell
9595
cardano-cli conway transaction build \
96-
--tx-in <UTXO_TXIX> \
96+
--tx-in $UTXO_TXIX \
9797
--tx-out $(cat script.addr)+10000000 \
9898
--tx-out-inline-datum-file datum.json \
99-
--change-address $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC) \
99+
--change-address $(cardano-cli address build --payment-verification-key-file ${CREDENTIALS_PATH}/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC) \
100100
--testnet-magic $CARDANO_TESTNET_MAGIC \
101101
--out-file tx.raw
102102
```
@@ -108,59 +108,75 @@ Now, sign and submit the transaction:
108108
```shell
109109
cardano-cli conway transaction sign \
110110
--tx-body-file tx.raw \
111-
--signing-key-file hydra-cluster/config/credentials/alice-funds.sk \
111+
--signing-key-file ${CREDENTIALS_PATH}/alice-funds.sk \
112112
--out-file tx.signed
113113

114114
cardano-cli conway transaction submit \
115115
--tx-file tx.signed \
116116
--testnet-magic $CARDANO_TESTNET_MAGIC
117117
```
118118

119-
Once the transaction is confirmed, you can query the script address to see the newly created UTxO:
119+
Once the transaction is confirmed, you can query the script address to find the transaction input (`TxIn`) of the script UTxO we just created.
120120

121121
```shell
122-
cardano-cli query utxo \
123-
--address $(cat script.addr) \
124-
--testnet-magic $CARDANO_TESTNET_MAGIC
122+
export SCRIPT_UTXO_TXIX=$(cardano-cli query utxo --address $(cat script.addr) --testnet-magic $CARDANO_TESTNET_MAGIC --output-json | jq -r 'keys[0]')
123+
echo "Captured script UTxO TxIn: $SCRIPT_UTXO_TXIX"
125124
```
126125

127-
## Step 5: Prepare the commit
126+
## Step 5: Prepare the Blueprint
128127

129128
Now we are ready to prepare the commit. We will create a blueprint transaction that spends the script UTxO. Note that this transaction is not meant to be signed and submitted to the Cardano network. It is just a blueprint that we will send to the `hydra-node` to get a properly drafted commit transaction.
130129

131130
We use `cardano-cli ... build-raw` to construct this blueprint because it gives us full control over the transaction structure without trying to automatically balance it or calculate fees, which is perfect for a blueprint.
132131

133-
In this transaction, we will spend the script UTxO and send the funds to our own wallet address.
132+
For a script UTxO, the blueprint only needs to declare the script UTxO as an input. The `hydra-node` will use this, along with the UTxO context provided in the next step, to build the full commit transaction.
134133

135134
```shell
136135
cardano-cli conway transaction build-raw \
137-
--tx-in <SCRIPT_UTXO_TXIX> \
136+
--tx-in $SCRIPT_UTXO_TXIX \
138137
--tx-in-script-file always-true.plutus \
139138
--tx-in-inline-datum-present \
140139
--tx-in-redeemer-value 42 \
141-
--tx-in-execution-units '(0, 0)' \
142-
--tx-out $(cardano-cli address build --payment-verification-key-file hydra-cluster/config/credentials/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC)+10000000 \
140+
--tx-in-execution-units '(1000000, 100000)' \
141+
--tx-out $(cardano-cli address build --payment-verification-key-file ${CREDENTIALS_PATH}/alice-funds.vk --testnet-magic $CARDANO_TESTNET_MAGIC)+10000000 \
143142
--fee 0 \
144143
--out-file tx.json
145144
```
146145

147146
A real-world script, like one written in [Aiken](https://aiken-lang.org/), would use the datum to carry state and the redeemer to provide input for validation. Our "always-true" script doesn't actually check any of these, but they are still required fields for a valid transaction that spends a script UTxO. Note that we use `--tx-in-inline-datum-present` because the datum was already included on-chain when we created the script UTxO. We also provide `--tx-in-execution-units`. This is required for any script spend to tell the network how much computational resource to budget for the script's execution. Since our script does nothing, we can use `(0, 0)`.
148147

149-
## Step 6: Commit the script UTxO
148+
## Step 6: Prepare the commit
150149

151-
This final step is very similar to the standard commit tutorial. We will start a `hydra-node`, initialize a Head, and then use the blueprint transaction to get a commit transaction from the `hydra-node`.
150+
This final step brings everything together. We will start a `hydra-node`, initialize a Head, and then send our blueprint and UTxO context to the `/commit` endpoint.
152151

153-
First, start the `hydra-node` and `hydra-tui` as explained in the [Commit using a blueprint](./commit-blueprint.md#step-5) tutorial and initialize a Head.
152+
**1. Start the Hydra Node and TUI**
154153

155-
Once the Head is in the `Initializing` state, you can send the HTTP request to the `/commit` API path.
154+
In a **new terminal**, start a single-party `hydra-node`.
155+
```shell
156+
hydra-node \
157+
--node-id 1 \
158+
--api-port 4001 \
159+
--hydra-signing-key ${CREDENTIALS_PATH}/../alice.sk \
160+
--cardano-signing-key ${CREDENTIALS_PATH}/alice.sk \
161+
--ledger-protocol-parameters devnet/protocol-parameters.json \
162+
--testnet-magic $CARDANO_TESTNET_MAGIC \
163+
--node-socket $CARDANO_NODE_SOCKET_PATH
164+
```
156165

157-
Just like in the other tutorial, you need to assemble a JSON request body. The `blueprintTx` is the `tx.json` file we just created, and the `utxo` is the script UTxO you are committing.
166+
In another **new terminal**, start the `hydra-tui`.
167+
```shell
168+
hydra-tui \
169+
--connect 0.0.0.0:4001 \
170+
--cardano-signing-key ${CREDENTIALS_PATH}/alice-funds.sk
171+
```
172+
In the TUI, press `i` to initialize the Head. Once the state is `Initializing`, you can quit the TUI (`q`) and proceed.
158173

159-
You can use the following script to query the necessary information and generate the `commit-request.json` file automatically. Make sure to replace `<SCRIPT_UTXO_TXIX>` with the actual transaction input of your script UTxO.
174+
**2. Send the Commit Request**
175+
176+
Now, we will build the commit request. This request contains two parts: the `utxo` context (which must include the script) and the `blueprintTx` (which provides the witness information).
160177

161178
```shell
162179
# Set variables
163-
SCRIPT_UTXO_TXIX="<SCRIPT_UTXO_TXIX>"
164180
BLUEPRINT_JSON=$(cat tx.json)
165181
UTXO_JSON=$(cardano-cli query utxo --tx-in ${SCRIPT_UTXO_TXIX} --testnet-magic $CARDANO_TESTNET_MAGIC --output-json)
166182

@@ -178,14 +194,15 @@ Now, use `curl` to send the request to the `hydra-node`, which will respond with
178194
curl -X POST --data @commit-request.json http://127.0.0.1:4001/commit > commit-tx.json
179195
```
180196

181-
This will give you the commit transaction, which you can then sign and submit as usual.
182-
The commit transaction must be signed by two keys: the key for the funds you are committing, and the party key that is participating in the Head.
197+
**3. Sign and Submit**
198+
199+
This is the final step. The `commit-tx.json` file now contains a valid, balanced transaction drafted by the `hydra-node`. We just need to sign it with both our funds key and our party key, and then submit it.
183200

184201
```shell
185202
cardano-cli conway transaction sign \
186203
--tx-body-file commit-tx.json \
187-
--signing-key-file hydra-cluster/config/credentials/alice-funds.sk \
188-
--signing-key-file hydra-cluster/config/credentials/alice.sk \
204+
--signing-key-file ${CREDENTIALS_PATH}/alice-funds.sk \
205+
--signing-key-file ${CREDENTIALS_PATH}/alice.sk \
189206
--out-file signed-tx.json
190207

191208
cardano-cli conway transaction submit \

0 commit comments

Comments
 (0)