<p>Looked into plutus fw code, I found the function datumHash will eventually call to this function</p>
<pre><code>-- NOT a builtin, only safe off-chain, hence the NOINLINE
{-# NOINLINE builtinDataToData #-}
-- | Convert a 'BuiltinData' into a 'PLC.Data'. Only works off-chain.
builtinDataToData :: BuiltinData -&gt; PLC.Data
builtinDataToData (BuiltinData d) = d
</code></pre>
<p>I guess it's not possible to run datumHash in on-chain, this also caused my issue.</p>

CC BY-SA 4.0
2022-02-04T16:57:01.183Z
2022-02-04T16:57:01.183Z
17159
<p>What is the correct way to compare DatumHash?</p>
<p>I imported the Eq function from PlutusTx</p>
<pre><code>import PlutusTx.Prelude (Eq((==)))
...

checkDatumHash :: TxOut -&gt; Bool
    checkDatumHash txOut = traceIfFalse &quot;Incorrect datum&quot; (own_dh == Just new_dh)
        where    
             
            --d = Datum (PlutusTx.toBuiltinData nfts)
            --dh = fromJust (findDatumHash (Datum (PlutusTx.toBuiltinData nfts)) info)
            --new_d = findDatum dh info
            new_dt = nfts {p = r_prct, l = mk_prct}
            new_dh = datumHash (Datum (PlutusTx.toBuiltinData new_dt))
            own_dh =   txOutDatumHash txOut
</code></pre>
<p>It's fine to compare 2 Datum datas or PubKeyHash but I got unsupported feature error when compare 2 DatumHash data. (For above example, it's fine to compare: new_d == Just d)</p>
<pre><code>GHC Core to PLC plugin: E042:Error: Unsupported feature: Kind: forall k. k -&gt; *
Context: Compiling kind: forall k. k -&gt; *
...
</code></pre>
<p>The error message may not show the direct issue, it points to my validator</p>
<pre><code>typedBuyValidator :: [PubKeyHash] -&gt; Scripts.TypedValidator Sale
typedBuyValidator [pkh1, pkh2] = Scripts.mkTypedValidator @Sale
    ($$(PlutusTx.compile [|| mkBuyValidator ||]) `PlutusTx.applyCode` PlutusTx.liftCode [pkh1, pkh2]) 
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @DatumInfo @RedeemInfo
</code></pre>
<p>Why both DatumHash and PubKeyHash look very similar where both are wrapper of  PlutusTx.BuiltinByteString but issue happened only in DatumHash?</p>
<p>Another strange thing to me that I can compile the code using function findDatum from Ledger successfully where this also makes comparison of DatumHash inside.</p>
<pre><code>findDatum :: DatumHash -&gt; TxInfo -&gt; Maybe Datum
findDatum dsh TxInfo{txInfoData} = snd &lt;$&gt; find f txInfoData
    where
        f (dsh', _) = dsh' == dsh
</code></pre>
<hr />
<p>UPDATE:</p>
<p>I tested the comparision DatumHash by getting another own_dh1 to compare with own_dh and it compiled successfully:</p>
<pre><code>checkDatumHash :: TxOut -&gt; Bool
    checkDatumHash txOut = traceIfFalse &quot;Incorrect datum&quot; (own_dh == own_dh1)
        where    
             
            --d = Datum (PlutusTx.toBuiltinData nfts)
            --dh = fromJust (findDatumHash (Datum (PlutusTx.toBuiltinData nfts)) info)
            --new_d = findDatum dh info
            new_dt = nfts {p = r_prct, l = mk_prct}
            new_dh = datumHash (Datum (PlutusTx.toBuiltinData new_dt))
            own_dh =   txOutDatumHash txOut
            own_dh1 =   txOutDatumHash txOut
</code></pre>
<p>Then I thought that maybe the problem may not relate to comparision but datumHash function which is hashing Datum to DatumHash was not allowed to run on Onchain code. Am I correct ?</p>
<pre><code>new_dh = datumHash (Datum (PlutusTx.toBuiltinData new_dt))
</code></pre>

<plutus><haskell><datum>
What is the correct way to compare DatumHash?
# What is the correct way to compare DatumHash?

(Asked by: username_0 on 2022-02-04)

What is the correct way to compare DatumHash?
I imported the Eq function from PlutusTx
import PlutusTx.Prelude (Eq((==)))
...

checkDatumHash :: TxOut -> Bool
    checkDatumHash txOut = traceIfFalse "Incorrect datum" (own_dh == Just new_dh)
        where    
             
            --d = Datum (PlutusTx.toBuiltinData nfts)
            --dh = fromJust (findDatumHash (Datum (PlutusTx.toBuiltinData nfts)) info)
            --new_d = findDatum dh info
            new_dt = nfts {p = r_prct, l = mk_prct}
            new_dh = datumHash (Datum (PlutusTx.toBuiltinData new_dt))
            own_dh =   txOutDatumHash txOut

It's fine to compare 2 Datum datas or PubKeyHash but I got unsupported feature error when compare 2 DatumHash data. (For above example, it's fine to compare: new_d == Just d)
GHC Core to PLC plugin: E042:Error: Unsupported feature: Kind: forall k. k -> *
Context: Compiling kind: forall k. k -> *
...

The error message may not show the direct issue, it points to my validator
typedBuyValidator :: [PubKeyHash] -> Scripts.TypedValidator Sale
typedBuyValidator [pkh1, pkh2] = Scripts.mkTypedValidator @Sale
    ($$(PlutusTx.compile [|| mkBuyValidator ||]) `PlutusTx.applyCode` PlutusTx.liftCode [pkh1, pkh2]) 
    $$(PlutusTx.compile [|| wrap ||])
  where
    wrap = Scripts.wrapValidator @DatumInfo @RedeemInfo

Why both DatumHash and PubKeyHash look very similar where both are wrapper of  PlutusTx.BuiltinByteString but issue happened only in DatumHash?
Another strange thing to me that I can compile the code using function findDatum from Ledger successfully where this also makes comparison of DatumHash inside.
findDatum :: DatumHash -> TxInfo -> Maybe Datum
findDatum dsh TxInfo{txInfoData} = snd <$> find f txInfoData
    where
        f (dsh', _) = dsh' == dsh


UPDATE:
I tested the comparision DatumHash by getting another own_dh1 to compare with own_dh and it compiled successfully:
checkDatumHash :: TxOut -> Bool
    checkDatumHash txOut = traceIfFalse "Incorrect datum" (own_dh == own_dh1)
        where    
             
            --d = Datum (PlutusTx.toBuiltinData nfts)
            --dh = fromJust (findDatumHash (Datum (PlutusTx.toBuiltinData nfts)) info)
            --new_d = findDatum dh info
            new_dt = nfts {p = r_prct, l = mk_prct}
            new_dh = datumHash (Datum (PlutusTx.toBuiltinData new_dt))
            own_dh =   txOutDatumHash txOut
            own_dh1 =   txOutDatumHash txOut

Then I thought that maybe the problem may not relate to comparision but datumHash function which is hashing Datum to DatumHash was not allowed to run on Onchain code. Am I correct ?
new_dh = datumHash (Datum (PlutusTx.toBuiltinData new_dt))

**Tags:** plutus, haskell, datum

---

## 1 answers

### Answer by username_0 on 2022-02-04. Score: 0

Looked into plutus fw code, I found the function datumHash will eventually call to this function
-- NOT a builtin, only safe off-chain, hence the NOINLINE
{-# NOINLINE builtinDataToData #-}
-- | Convert a 'BuiltinData' into a 'PLC.Data'. Only works off-chain.
builtinDataToData :: BuiltinData -> PLC.Data
builtinDataToData (BuiltinData d) = d

I guess it's not possible to run datumHash in on-chain, this also caused my issue.


---

