<pre><code>module Main ( main) where

import           Data.String (IsString(fromString))
import           Data.Either (fromRight)
import           Data.Text (Text, pack)
import           Ledger (PubKeyHash(..), pubKeyHash) 
import           Ledger.Bytes (LedgerBytes(LedgerBytes), fromHex)
import qualified Wallet.Emulator.Wallet as W

main :: IO ()
main = do
    let pkh1 = pubKeyHash $ W.walletPubKey $ W.Wallet 1
    let pkh9 = pubKeyHash $ W.walletPubKey $ W.Wallet 9

    let s = show pkh1
    let eiPkh = pkhFromStr s

    let s' = show $ fromRight pkh9 eiPkh
    let eiPkh' = pkhFromStr s'

    let s'' = show $ fromRight pkh9 eiPkh'

    print s   
    print s'
    print s''


pkhFromStr :: String -&gt; Either Text PubKeyHash   
pkhFromStr s =         
    case fromHex (fromString s) of 
        Right (LedgerBytes bytes) -&gt; Right $ PubKeyHash bytes 
        Left msg -&gt; Left $ pack (&quot;Could not convert from hex to bytes: &quot; &lt;&gt; msg)
</code></pre>
<p>The above code produces the following correct outputs.</p>
<pre><code>&quot;35dedd2982a03cf39e7dce03c839994ffdec2ec6b04f1cf2d40e61a3&quot;
&quot;35dedd2982a03cf39e7dce03c839994ffdec2ec6b04f1cf2d40e61a3&quot;
&quot;35dedd2982a03cf39e7dce03c839994ffdec2ec6b04f1cf2d40e61a3&quot;
</code></pre>

CC BY-SA 4.0
2021-08-31T23:59:37.223Z
2021-09-01T19:05:41.463Z
2021-09-01T19:05:41.463Z
kolam
<p>Given the variable <code>s</code> from</p>
<pre><code>import qualified Wallet.Emulator.Wallet as W
import qualified Ledger

-- Has value of &quot;35dedd2982a03cf39e7dce03c839994ffdec2ec6b04f1cf2d40e61a3&quot;
s :: String
s = show (Ledger.pubKeyHash $ W.walletPubKey $ W.Wallet 1)
</code></pre>
<p>How do I turn that back into a <code>PubKeyHash</code>?</p>

<plutus>
Find PubKeyHash from String
# Find PubKeyHash from String

(Asked by: username_0 on 2021-08-31)

Given the variable s from
import qualified Wallet.Emulator.Wallet as W
import qualified Ledger

-- Has value of "35dedd2982a03cf39e7dce03c839994ffdec2ec6b04f1cf2d40e61a3"
s :: String
s = show (Ledger.pubKeyHash $ W.walletPubKey $ W.Wallet 1)

How do I turn that back into a PubKeyHash?

**Tags:** plutus

---

## 1 answers

### Answer by username_1 on 2021-08-31. Score: 5 | Accepted answer

module Main ( main) where

import           Data.String (IsString(fromString))
import           Data.Either (fromRight)
import           Data.Text (Text, pack)
import           Ledger (PubKeyHash(..), pubKeyHash) 
import           Ledger.Bytes (LedgerBytes(LedgerBytes), fromHex)
import qualified Wallet.Emulator.Wallet as W

main :: IO ()
main = do
    let pkh1 = pubKeyHash $ W.walletPubKey $ W.Wallet 1
    let pkh9 = pubKeyHash $ W.walletPubKey $ W.Wallet 9

    let s = show pkh1
    let eiPkh = pkhFromStr s

    let s' = show $ fromRight pkh9 eiPkh
    let eiPkh' = pkhFromStr s'

    let s'' = show $ fromRight pkh9 eiPkh'

    print s   
    print s'
    print s''


pkhFromStr :: String -> Either Text PubKeyHash   
pkhFromStr s =         
    case fromHex (fromString s) of 
        Right (LedgerBytes bytes) -> Right $ PubKeyHash bytes 
        Left msg -> Left $ pack ("Could not convert from hex to bytes: " <> msg)

The above code produces the following correct outputs.
"35dedd2982a03cf39e7dce03c839994ffdec2ec6b04f1cf2d40e61a3"
"35dedd2982a03cf39e7dce03c839994ffdec2ec6b04f1cf2d40e61a3"
"35dedd2982a03cf39e7dce03c839994ffdec2ec6b04f1cf2d40e61a3"


---

