1

If you can help me with this I will really appreciate it. I have worked through the token construction tutorial on Cardano docs https://docs.cardano.org/core-concepts/cardano-addresses. Using the command line I can successfully create transactions

cardano-cli transaction-build-raw /
--fee 0 /
--tx-in ${TxHash}#$T{xIX} /
--tx-out ${receiveAddress}+${amountToSend} /
--out-file mat.raw

when running this command I can successfully send ada to my Nami wallet if I set the variable:

receiveAddress="addr_test1qq5vc9wurul0cd3f2n8fu5uahdaefp6r4rn6nq9tmnvdwzdgchadmnjxnqgaf4k3ju8t74jaj80uwdrcvnxfjmawzats23jjcl"

This address can be obtained by clicking the nami wallet "receive button" and copying the address. This part works fine.

I am trying to tie this transaction building command to a button in javascript. To do this I am looking at CIP-30. https://cips.cardano.org/cips/cip30/. But when I use the injected javascript from Namie wallet

''' let address = await cardano.getChangeAddress() ''' the variable address is a string not in the proper format. "0028cc15dc1f3efc362954ce9e539dbb7b948743a8e7a980abdcd8d709a8c5faddce469811d4d6d1970ebf565d91dfc7347864cc996fae1757". What is this format? How do I convert it to the Addr_test- string?

Thank you in advance!

2 Answers 2

2

It is hex encoded byteString. You can use the Cardano serialization lib browser https://www.npmjs.com/package/@emurgo/cardano-serialization-lib-browser

And the following code in javascript

const addressBech32 = async () => {
  await Loader.load();
  const address = (await window.cardano.getUsedAddresses())[0];
  return Loader.Cardano.Address.from_bytes(
    Buffer.from(address, "hex")
  ).to_bech32();
};

Where Loader is my loader to the above Library :) the Address class has a method for converting Hex encoded bytes to Address Bech 32 :

class Loader {
  async load() {
    if (this._wasm) {
      return;
    }
    /**
     * @private
     */
    this._wasm = await import(
      "./@emurgo/cardano-serialization-lib-browser/cardano_serialization_lib_bg"
    );
  }

  get Cardano() {
    return this._wasm;
  }
}

export default new Loader();
6
  • Could you elaborate on "loader to the above library". I installed the link you posted using npm, copied this code to a file called "index.js" and then tried running the code with "node index.js". I get an error saying "Loader is not defined". I apologize if there is something obvious I'm missing here, I have a programming background but am brand new to javascript.
    – Richard
    Feb 9, 2022 at 4:46
  • Done, nothing obvious at all if you are new to JS. If you run it in node.js you do not need this... it is only needed if it is going to run in the browser... put the above code in another file that you import in the file where you are going to use the script that gets the address. Feb 10, 2022 at 3:03
  • I have tried copying the exact code into a file and importing that file and get an error saying only modules can import. If the only way to do this is to learn webpack and node so be it but I feel there has to be a simple way of doing this. that doesn't require learning all these new javascript tools I'm not concerned about being in browser, python or command line are fine too. I just need a working script that I can run that will convert this string into a usable address. I can figure out how to port it to my application if I can just figure that part out.
    – Richard
    Feb 10, 2022 at 6:55
  • in python this would be two lines of code: import <stringConvert> <stringConvert>.decode(encodedString)
    – Richard
    Feb 10, 2022 at 6:56
  • I found out there was a binary Bech32 that does the job. Thank you for the help!
    – Richard
    Feb 10, 2022 at 23:22
0

According to the docs, this is a BaseAddress type, which is a hex representation in byte strings

https://github.com/Berry-Pool/nami-wallet/blob/main/README.md#cardanogetusedaddresses

2
  • ok. so how do i convert that to address format?
    – Richard
    Feb 7, 2022 at 5:31
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 8, 2022 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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