In one of the utility function for serialising validator functions, we have

validator :: PubKeyHash -> Validator


saveVal :: PubKeyHash -> IO ()

I was able to compute the PubKeyHash for my test wallet, but within the cabal repl, I'm getting different script addresses when I serialise the contract with:

:set -XOverloadedStrings 
import PlutusTx
import PlutusTx.Builtins 
import Plutus.V1.Ledger.Crypto

saveVal (PubKeyHash ("f8493..." :: BuiltinByteString))

vs with

saveVal "f8493..." 

I'm not sure why this is the case. How am I suppose to "give" the pubkeyhash to the function?

1 Answer 1


You're right, that won't store the PubkeyHash as the literal ByteString you want. We can see that by trying:

ghci> PubKeyHash "f778997b5e0fcaec19b7580bc23d010d24de9eea16f0275b5e766046"

One way round is to use fromHex. For example, define a function using it to convert its output to BuiltinByteString:

import PlutusLedgerApi.V1.Bytes qualified as P
import PlutusTx.Builtins qualified as BI

bytesFromHex :: BS.ByteString -> BI.BuiltinByteString
bytesFromHex = BI.toBuiltin . P.bytes . fromEither . P.fromHex
    fromEither (Left e)  = error $ show e
    fromEither (Right b) = b

(note that Plutus.V1.Ledger.Bytes changed to PlutusLedgerApi.V1.Bytes)

Then use to produce PubKeyHash:

PubKeyHash (bytesFromHex "f778997b5e0fcaec19b7580bc23d010d24de9eea16f0275b5e766046")

Your Answer

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

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