Recently I am also looking at this issue as well, digging around different resources and find out 3rd Cohort PPP's week6, where Lars has demonstrated how to do it: https://github.com/input-output-hk/plutus-pioneer-program/blob/main/code/week06/src/Week06/Utils.hs
In particular, I just tried out it works to get the PaymentPubKeyHash
from Bech32 Address
through the below code snippet.
unsafeReadAddress :: String -> Plutus.Address
unsafeReadAddress s = fromMaybe (error $ "can't parse " ++ s ++ " as an address") $ tryReadAddress s
credentialLedgerToPlutus :: Ledger.Credential a StandardCrypto -> Plutus.Credential
credentialLedgerToPlutus (ScriptHashObj (Hash.ScriptHash h)) = Plutus.ScriptCredential $ Plutus.ValidatorHash $ toBuiltin $ hashToBytes h
credentialLedgerToPlutus (KeyHashObj (KeyHash h)) = Plutus.PubKeyCredential $ Plutus.PubKeyHash $ toBuiltin $ hashToBytes h
tryReadAddress :: String -> Maybe Plutus.Address
tryReadAddress x = case deserialiseAddress AsAddressAny $ pack x of
Nothing -> Nothing
Just (AddressByron _) -> Nothing
Just (AddressShelley (ShelleyAddress _ p _)) -> Just Plutus.Address
{ Plutus.addressCredential = credentialLedgerToPlutus p
, Plutus.addressStakingCredential = Nothing
-- , Plutus.addressStakingCredential = stakeReferenceLedgerToPlutus s
}
getCredentials :: Plutus.Address -> Maybe (Plutus.PaymentPubKeyHash, Maybe Plutus.StakePubKeyHash)
getCredentials (Plutus.Address x y) = case x of
ScriptCredential _ -> Nothing
PubKeyCredential pkh ->
let
ppkh = Plutus.PaymentPubKeyHash pkh
in
case y of
Nothing -> Just (ppkh, Nothing)
Just (Plutus.StakingPtr _ _ _) -> Nothing
Just (StakingHash h) -> case h of
ScriptCredential _ -> Nothing
PubKeyCredential pkh' -> Just (ppkh, Just $ Plutus.StakePubKeyHash pkh')
unsafePaymentPubKeyHash :: Plutus.Address -> Plutus.PaymentPubKeyHash
unsafePaymentPubKeyHash addr = maybe (error $ "script address " ++ show addr ++ " does not contain a payment key") fst $ getCredentials addr
i.e. I skipped the staking credential part coz seems part of the code is deprecated, still under investigation. But the PaymentPubKeyHash
part works fine for me.
toAddressAny
: github.com/input-output-hk/cardano-node/blob/master/cardano-api/…