4

I have managed to retrive a user address from a dapp connector wallet using JS:

api_promise = window.cardano.{wallet}.enable();
api_promise.then(function(api) {
  addr_promise = api.getUsedAddresses();
  addr_promise.then(
    function(addr) {
      value[0];  // this is a CBOR address !
...

I get this type of (presumably CBOR) address: 012f4d0b584ecb52e4e4b8e78e4ce77a6f822e59ede3277305fd526b71824b2d2740284cf6a28e2a512cc4a00f41004c18ed728a0f7a04eb29 Apparently with some skill it could be read using this -> https://cbor.me.

# Is there any method to extract a shelley addr_34.... using JS or python?

Working implementations are available in node.js, but I'm using flask (python + JS). And I'm struggling to make it work wit packages cbor2 and bech32. For example I tried cbor2.loads(bytes(addr_str, 'utf-8')) without success.

My further goal is to query blockfrost.

2 Answers 2

2

You can also use typhonjs used by typhonwallet. It's a Pure JS library not dependent on WASM.

Install

npm i @stricahq/typhonjs

Usage

const typhonjs =  require("@stricahq/typhonjs");

const bech32Address = typhonjs.utils
  .getAddressFromHex("012f4d0b584ecb52e4e4b8e78e4ce77a6f822e59ede3277305fd526b71824b2d2740284cf6a28e2a512cc4a00f41004c18ed728a0f7a04eb29")
  .getBech32();
2
  • I might appear clueless, but I am running code via python Flask. It means the javascript is executed client-side. In the background, everything is executed via python and ajax queries. Will I be able to execute this client side?
    – Lokinou
    Mar 30 at 9:38
  • Yes, you can execute this code client side :) @Lokinou
    – Pavan Vora
    Mar 30 at 13:52
1

First install the npm module:

npm i @emurgo/cardano-serialization-lib-browser

Then you need a wasm loader module

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

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

export default new wasm();

Then import into your module/component using:

import WASMLoader from "../../utils/Loader";

Now you should be able to use wasm module to convert the CBOR address into bech32 (addr1.., stake1.. etc)

const getAddressToBech32 = async () => {
    await WASMLoader.load();
    const address = (await api.getUsedAddresses())[0];
    return WASMLoader.Cardano.Address.from_bytes(Buffer.from(address,"hex")).to_bech32();
};

Now that you have a bech32 address you should be able to use with for blockfrost API queries

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.