<p>You're right, that won't store the PubkeyHash as the literal ByteString you want. We can see that by trying:</p>
<pre><code>ghci&gt; PubKeyHash &quot;f778997b5e0fcaec19b7580bc23d010d24de9eea16f0275b5e766046&quot;
6637373839393762356530666361656331396237353830626332336430313064323464653965656131366630323735623565373636303436
</code></pre>
<p>One way round is to use <a href="https://input-output-hk.github.io/plutus/master/plutus-ledger-api/html/PlutusLedgerApi-V1-Bytes.html#v:fromHex" rel="nofollow noreferrer">fromHex</a>. For example, define a function using it to convert its output to BuiltinByteString:</p>
<pre><code>import PlutusLedgerApi.V1.Bytes qualified as P
import PlutusTx.Builtins qualified as BI

bytesFromHex :: BS.ByteString -&gt; BI.BuiltinByteString
bytesFromHex = BI.toBuiltin . P.bytes . fromEither . P.fromHex
  where
    fromEither (Left e)  = error $ show e
    fromEither (Right b) = b
</code></pre>
<p>(note that <code>Plutus.V1.Ledger.Bytes</code> changed to <code>PlutusLedgerApi.V1.Bytes</code>)</p>
<p>Then use to produce PubKeyHash:</p>
<pre><code>PubKeyHash (bytesFromHex &quot;f778997b5e0fcaec19b7580bc23d010d24de9eea16f0275b5e766046&quot;)
f778997b5e0fcaec19b7580bc23d010d24de9eea16f0275b5e766046
</code></pre>

CC BY-SA 4.0
2023-06-30T08:52:32.923Z
2023-06-30T08:52:32.923Z
james
<p>In one of the utility function for serialising validator functions, we have</p>
<pre><code>validator :: PubKeyHash -&gt; Validator
</code></pre>
<p>and</p>
<pre><code>saveVal :: PubKeyHash -&gt; IO ()
</code></pre>
<p>I was able to compute the PubKeyHash for my test wallet, but within the <code>cabal repl</code>, I'm getting different script addresses when I serialise the contract with:</p>
<pre><code>:set -XOverloadedStrings 
import PlutusTx
import PlutusTx.Builtins 
import Plutus.V1.Ledger.Crypto

saveVal (PubKeyHash (&quot;f8493...&quot; :: BuiltinByteString))
</code></pre>
<p>vs with</p>
<pre><code>saveVal &quot;f8493...&quot; 
</code></pre>
<p>I'm not sure why this is the case. How am I suppose to &quot;give&quot; the pubkeyhash to the function?</p>

<plutus-pioneer-program>
PubKeyHash datatype vs BuiltinByteString for serialising scripts
# PubKeyHash datatype vs BuiltinByteString for serialising scripts

(Asked by: username_0 on 2023-06-30)

In one of the utility function for serialising validator functions, we have
validator :: PubKeyHash -> Validator

and
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?

**Tags:** plutus-pioneer-program

---

## 1 answers

### Answer by username_1 on 2023-06-30. Score: 2 | Accepted answer

You're right, that won't store the PubkeyHash as the literal ByteString you want. We can see that by trying:
ghci> PubKeyHash "f778997b5e0fcaec19b7580bc23d010d24de9eea16f0275b5e766046"
6637373839393762356530666361656331396237353830626332336430313064323464653965656131366630323735623565373636303436

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
  where
    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")
f778997b5e0fcaec19b7580bc23d010d24de9eea16f0275b5e766046


---

