4

I am working on an app, I am using Nami wallet and cardano-serialization-lib.

When I query UTxO and decode it using cardano-serialization-lib, I get an object with only attribute is something called "ptr", ptr is a number I was expecting an actual UTxO... I want to know how can I get the UTXO because I want it for using it inside a policy script.

Edit:

Extended explanation: I run the following comands in my browser:

window.cardano.enable();
window.cardano.getUtxos();

I get this :

Promise {<pending>}[[Prototype]]: Promise[[PromiseState]]: "fulfilled"[[PromiseResult]]: Array(1)0: "82825820eef507b798abbb8a1360efbd113f51eda22e2fbaae3cc32c78724c8644c164a40082583900756e62ff9667b187209b6fe1b577061491cf6f93a81e1f91741e2e3accb864ae67087f69bbd9ab74f71781d13ae40aab86e1de30ca7e21171a1dcd6500"length: 1[[Prototype]]: Array(0)

when i decode it using the library in NodeJS with the following script:

const S = require("./node_modules/@emurgo/cardano-serialization-lib-nodejs/cardano_serialization_lib.js");
const _Buffer = require("buffer/").Buffer;
const rawUtxo =
  "82825820eef507b798abbb8a1360efbd113f51eda22e2fbaae3cc32c78724c8644c164a40082583900756e62ff9667b187209b6fe1b577061491cf6f93a81e1f91741e2e3accb864ae67087f69bbd9ab74f71781d13ae40aab86e1de30ca7e21171a1dcd6500";

const utxos = S.TransactionUnspentOutput.from_bytes(
  _Buffer.from(rawUtxo, "hex")
);

console.log(utxos); 

I get this:

TransactionUnspentOutput { ptr: 1179656 },

3
  • 1
    It looks like you are looking at a pointer address. But you really need to expand your question as it is not clear. Oct 22 '21 at 8:31
  • Yes as far as i underestand ptr is some kind of pointer, I edited explaing more details as you suggested. Thank you! Oct 22 '21 at 14:19
  • I'm use cardano-serialization-lib. How to get key hash ? Thank.
    – qi.chalerm
    Dec 11 '21 at 11:35
3

The serialization lib is written in Rust and compiled to web assembly. Rust manually manages its memory, and I'm assuming that you need to do the same here. If you look at the typescript types you can see that every object has a free() method.

The ptr is a pointer to memory. If you need to access information of the UTXO you need to use the provided functions. So for example, this will construct the TransactionOutput from the CBOR hex string

const fromHexString = hexString =>
  new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));

const utxos = await cardano
        .getUtxos()
        .then(cborUTXOS => 
                cborUTXOS.map(cborUTXO => 
                        CardanoWasm.TransactionUnspentOutput.from_bytes(fromHexString(cborUTXO))
                    )
             )

And you can see how many lovelaces an output has like this:

utxos[0].output().amount().coin().to_str()
3
  • As far as i underestand you are telling me to do the same i did using a map function and a hex decoder explicit ... in fact i got the same ptr than before. Nov 6 '21 at 3:22
  • when i do .coin() i get coin is not a function... where can i read about ehis objets? .output() for example? Nov 6 '21 at 13:50
  • 2
    The cardano serialization lib exposes some typescript definitions, so you can see what methods are available for each object. If you install the library with npm you can see the .d.ts type definition in node_modules Nov 10 '21 at 2:27

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.