I'm trying to understand how Constraints.mustHashDatum
works but I'm not able to figure out the purpose of this Constraint.
Any help would be great.
Cardano Stack Exchange is a question and answer site for users and developers of the Cardano cryptocurrency ecosystem. It only takes a minute to sign up.
Sign up to join this communityThis is my beginner takeaway about Constraints.mustHashDatum after reading about TxContraints and TxConstraint and mustHashDatum at the Ledger.Constraints documentation link below. I will also be interested in the responses so that I can know the answer too.
Here are my thoughts. It seems like when we're creating a transaction, we can dictate that one of the constraints/requirements is just that there exist a Datum and a DatumHash, and that both must be supplied as part of transaction creation. If one or both is not supplied, then the transaction won't be generated because the particular mustHashDatum constraint wouldn't be met. I think maybe the mustHashDatum constraint does this for us, since just creating that constraint requires that we provide both. Presumably, we might set this as a constraint to make sure that later code involving the transaction can use/check the Datum and the DatumHash.
Below are the excerpts from the documentation that seemed applicable.
Ledger.Constraints
This module defines TxConstraints, a list of constraints on transactions. To construct a value of TxConstraints use the mustPayToTheScript, mustSpendAtLeast, etc functions. Once we have a TxConstraints value it can be used both to generate a transaction that satisfies the constraints (off-chain, using mkTx) and to check whether a given pending transaction meets the constraints (on-chain, using checkScriptContext).
~~~~~
Constructors
TxConstraints
txConstraints :: [TxConstraint]
~~~~~
data TxConstraint
Constraints on transactions that want to spend script outputs
Constructors
MustSpendAtLeast Value
MustHashDatum DatumHash Datum
~~~~~
Defining constraints
{-# INLINABLE mustHashDatum #-}
mustHashDatum :: DatumHash -> Datum -> TxConstraints i o
mustHashDatum dvh = singleton . MustHashDatum dvh