1

In plutus endpoint, I want to convert Address type value to get addr_testXXX format address.

I have try :

import Cardano.Api  ( SerialiseAddress (deserialiseAddress, serialiseAddress) )
  ...
-- here, var 'testAddr' is of 'Address' type
printf "ledger address is: %s"  $ show $ serialiseAddress testAddr

but it raised error when compiling:

    • No instance for (SerialiseAddress
                         plutus-ledger-api-0.1.0.0:Plutus.V1.Ledger.Address.Address)
        arising from a use of ‘serialiseAddress’

From google, I found this: https://github.com/functionally/mantis/blob/1cd121202452dcc1bce56ed4b4f41f0e880c9d04/src/Mantra/Wallet.hs#L56 It seem that I should convert Address to AddressAny, but don't know how to.

Any help will be appreciated!

2

3 Answers 3

0

Here is a demo in Java:

public String swapAddress(String address) {
    byte[] bech = Bech32.decode(address).data;
    byte[] conv = Utils.convertBits(bech, 5, 8, false);
    conv = Bytes.sub(conv, 0, conv.length - 1);
    int networkId = conv[0] & 0x0F;
    String hrp = "";
    if (networkId == 0) {
        hrp = "addr";
        networkId = 1;
    } else {
        hrp = "addr_test";
        networkId = 0;
    }
    conv[0] = ((byte) networkId);
    byte[] data = Utils.convertBits(conv);
    return Bech32.encode(hrp, data);
}

You can find the convertBits here and Bech32 here. For Cardano, Bech32 #152 & #153 are unnecessary.

1
  • Thanks for your kindly help. But I write plutus code and looking forward to haskell example. Jul 1, 2022 at 9:24
0

You can use the function (1) in Cardano.Api,

makeShelleyAddress :: NetworkId → PaymentCredential → StakeAddressReference → Address ShelleyAddr

The resulting address of type Address ShelleyAddr has an instance of SerialiseAddress (2) so you can use

serialiseAddress ∷ Address ShelleyAddr → Text

on it.

0

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.

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.