Code Monkey home page Code Monkey logo

Comments (6)

YFrendo avatar YFrendo commented on August 15, 2024 2

Hey here some code for your issue
Full JS solution
Thanks to @kodemill

import { Address } from '/static/node_modules/@emurgo/cardano-serialization-lib-asmjs/cardano_serialization_lib.js'

const MAP_HEX = {
  0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6,
  7: 7, 8: 8, 9: 9, a: 10, b: 11, c: 12, d: 13,
  e: 14, f: 15, A: 10, B: 11, C: 12, D: 13,
  E: 14, F: 15
};

function fromHex(hexString) {
    const bytes = new Uint8Array(Math.floor((hexString || "").length / 2));
    let i;
    for (i = 0; i < bytes.length; i++) {
      const a = MAP_HEX[hexString[i * 2]];
      const b = MAP_HEX[hexString[i * 2 + 1]];
      if (a === undefined || b === undefined) {
        break;
      }
      bytes[i] = (a << 4) | b;
    }
    return i === bytes.length ? bytes : bytes.slice(0, i);
  }

async function request_address(){
    var enable = await window.cardano.enable();
    if (enable){
        if (await window.cardano.getNetworkId() == 1){
            var ByteAddress = await window.cardano.getUsedAddresses();
            var address = await Address.from_bytes(fromHex(ByteAddress[0])).to_bech32();
            console.log(address);

        }
    }
}

from nami.

cjkoepke avatar cjkoepke commented on August 15, 2024

Nami injects the API into the window, so yes — you can access it via window.cardano. I would make sure your extension is installed, and to ensure it's working properly you should see this printed in your console:

injection succeeded

from nami.

arressaye avatar arressaye commented on August 15, 2024

Thanks! I managed to access window.cardano now. The site I'm trying to build really only needs to be able to read the user's Nami wallet address. I tried using getUsedAddresses() and it does return a value, and I understand from the documentation that I'll need to use Cardano serialization lib to decode it. Is there any documentation on how to actually use this library?

from nami.

JimmyMcNider avatar JimmyMcNider commented on August 15, 2024

Thanks! I managed to access window.cardano now. The site I'm trying to build really only needs to be able to read the user's Nami wallet address. I tried using getUsedAddresses() and it does return a value, and I understand from the documentation that I'll need to use Cardano serialization lib to decode it. Is there any documentation on how to actually use this library?

hey @arressaye just reading this - did you ever find the right documentation that laid this process out?

from nami.

arressaye avatar arressaye commented on August 15, 2024

Hey @JimmyMcNider unfortunately not yet. I was looking through the documentation and it looks a bit beyond me. Hope someone could explain how to use it in simpler terms...

from nami.

arressaye avatar arressaye commented on August 15, 2024

Thanks @YFrendo! This is the first solution I've come across which makes sense and is easy to follow!! :)

from nami.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.