1

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))
0

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.