<p>You can use the function <a href="https://input-output-hk.github.io/cardano-node/cardano-api/lib/Cardano-Api.html#v:makeShelleyAddress" rel="nofollow noreferrer">(1)</a> in <code>Cardano.Api</code>,</p>
<pre><code>makeShelleyAddress :: NetworkId → PaymentCredential → StakeAddressReference → Address ShelleyAddr
</code></pre>
<p>The resulting address of type <code>Address ShelleyAddr</code> has an instance of <code>SerialiseAddress</code> <a href="https://input-output-hk.github.io/cardano-node/cardano-api/lib/Cardano-Api.html#t:ShelleyAddr" rel="nofollow noreferrer">(2)</a> so you can use</p>
<pre><code>serialiseAddress ∷ Address ShelleyAddr → Text
</code></pre>
<p>on it.</p>

CC BY-SA 4.0
2022-09-06T10:21:16.323Z
2022-09-06T10:21:16.323Z
Fermat
<p>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: <a href="https://github.com/input-output-hk/plutus-pioneer-program/blob/main/code/week06/src/Week06/Utils.hs" rel="nofollow noreferrer">https://github.com/input-output-hk/plutus-pioneer-program/blob/main/code/week06/src/Week06/Utils.hs</a></p>
<p>In particular, I just tried out it works to get the <code>PaymentPubKeyHash</code> from <code>Bech32 Address</code> through the below code snippet.</p>
<pre class="lang-haskell prettyprint-override"><code>unsafeReadAddress :: String -&gt; Plutus.Address
unsafeReadAddress s = fromMaybe (error $ &quot;can't parse &quot; ++ s ++ &quot; as an address&quot;) $ tryReadAddress s

credentialLedgerToPlutus :: Ledger.Credential a StandardCrypto -&gt; 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 -&gt; Maybe Plutus.Address
tryReadAddress x = case deserialiseAddress AsAddressAny $ pack x of
    Nothing                                      -&gt; Nothing
    Just (AddressByron _)                        -&gt; Nothing
    Just (AddressShelley (ShelleyAddress _ p _)) -&gt; Just Plutus.Address
        { Plutus.addressCredential        = credentialLedgerToPlutus p
        , Plutus.addressStakingCredential = Nothing
        -- , Plutus.addressStakingCredential = stakeReferenceLedgerToPlutus s
        }

getCredentials :: Plutus.Address -&gt; Maybe (Plutus.PaymentPubKeyHash, Maybe Plutus.StakePubKeyHash)
getCredentials (Plutus.Address x y) = case x of
    ScriptCredential _   -&gt; Nothing
    PubKeyCredential pkh -&gt;
      let
        ppkh = Plutus.PaymentPubKeyHash pkh
      in
        case y of
            Nothing                        -&gt; Just (ppkh, Nothing)
            Just (Plutus.StakingPtr _ _ _) -&gt; Nothing
            Just (StakingHash h)           -&gt; case h of
                ScriptCredential _    -&gt; Nothing
                PubKeyCredential pkh' -&gt; Just (ppkh, Just $ Plutus.StakePubKeyHash pkh')

unsafePaymentPubKeyHash :: Plutus.Address -&gt; Plutus.PaymentPubKeyHash
unsafePaymentPubKeyHash addr = maybe (error $ &quot;script address &quot; ++ show addr ++ &quot; does not contain a payment key&quot;) fst $ getCredentials addr
</code></pre>
<p>i.e. I skipped the staking credential part coz seems part of the code is deprecated, still under investigation. But the <code>PaymentPubKeyHash</code> part works fine for me.</p>

CC BY-SA 4.0
2022-10-07T06:34:50.327Z
2022-10-07T06:34:50.327Z
SIDAN Whatever
<p>Here is a demo in Java:</p>
<pre><code>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] &amp; 0x0F;
    String hrp = &quot;&quot;;
    if (networkId == 0) {
        hrp = &quot;addr&quot;;
        networkId = 1;
    } else {
        hrp = &quot;addr_test&quot;;
        networkId = 0;
    }
    conv[0] = ((byte) networkId);
    byte[] data = Utils.convertBits(conv);
    return Bech32.encode(hrp, data);
}
</code></pre>
<p>You can find the convertBits <a href="https://github.com/bitcoinj/bitcoinj/blob/5e2e62f86844653cd21187d2a2fc1eeecfa27c09/core/src/main/java/org/bitcoinj/core/SegwitAddress.java" rel="nofollow noreferrer">here</a> and <a href="https://github.com/bitcoinj/bitcoinj/blob/44d0576ce4310a3d6f1859964e391fa8d981d409/core/src/main/java/org/bitcoinj/base/Bech32.java#L148" rel="nofollow noreferrer">Bech32 here</a>.
For Cardano, Bech32 #152 &amp; #153 are unnecessary.</p>

CC BY-SA 4.0
2022-07-01T09:24:54.333Z
Thanks for your kindly help. But I write plutus code and looking forward to haskell example.
breeze wang
CC BY-SA 4.0
2022-07-01T06:48:49.373Z
2022-07-07T08:29:55.163Z
2022-07-07T08:29:55.163Z
sounder
<p>In plutus endpoint, I want to convert Address type value to get addr_testXXX format address.</p>
<p>I have try :</p>
<pre><code>import Cardano.Api  ( SerialiseAddress (deserialiseAddress, serialiseAddress) )
  ...
-- here, var 'testAddr' is of 'Address' type
printf &quot;ledger address is: %s&quot;  $ show $ serialiseAddress testAddr
</code></pre>
<p>but it raised error when compiling:</p>
<pre><code>    • No instance for (SerialiseAddress
                         plutus-ledger-api-0.1.0.0:Plutus.V1.Ledger.Address.Address)
        arising from a use of ‘serialiseAddress’
</code></pre>
<p>From google, I found this: <a href="https://github.com/functionally/mantis/blob/1cd121202452dcc1bce56ed4b4f41f0e880c9d04/src/Mantra/Wallet.hs#L56" rel="nofollow noreferrer">https://github.com/functionally/mantis/blob/1cd121202452dcc1bce56ed4b4f41f0e880c9d04/src/Mantra/Wallet.hs#L56</a>
It seem that I should convert <code>Address</code> to <code>AddressAny</code>, but don't know how to.</p>
<p>Any help will be appreciated!</p>

CC BY-SA 4.0
2022-07-11T09:52:31.800Z
Have you tried converting using this: https://github.com/input-output-hk/cardano-node/blob/master/cardano-api/src/Cardano/Api/Address.hs#L455
james
CC BY-SA 4.0
2022-08-08T04:59:20.140Z
I think you actually want `toAddressAny` : https://github.com/input-output-hk/cardano-node/blob/master/cardano-api/src/Cardano/Api/Address.hs#L470
Erik de Castro Lopo
<plutus><smart-contract><address>
How to convert Address type variable to get "addr_testXXX" format address?
# How to convert Address type variable to get "addr_testXXX" format address?

(Asked by: username_0 on 2022-07-01)

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!

**Tags:** plutus, smart-contract, address
> **Comments:**
> **username_1**: Have you tried converting using this: https://github.com/input-output-hk/cardano-node/blob/master/cardano-api/src/Cardano/Api/Address.hs#L455  
> **username_2**: I think you actually want `toAddressAny` : https://github.com/input-output-hk/cardano-node/blob/master/cardano-api/src/Cardano/Api/Address.hs#L470  

---

## 3 answers

### Answer by username_3 on 2022-09-06. Score: 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.


---

### Answer by username_4 on 2022-10-07. Score: 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.


---

### Answer by username_5 on 2022-07-01. Score: 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.

> **Comments:**
> **username_0**: Thanks for your kindly help. But I write plutus code and looking forward to haskell example.  

---

