4

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?

2
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"
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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