<p>cardano-serialization-lib does pretty much everything for building and reading transactions and interacting with the webwallets.</p>
<p>Here is how you can extract all the asset names from the a user's wallet and process them to get the policyid, the assename hex value and the asset name string from each of the assets</p>
<p>This part <code>this.API.getUtxos()</code> reads all UTXOs from the user's wallet and <code>TransactionUnspentOutput</code> is a function from cardano-serialization-lib</p>
<pre><code>    /**
     * Gets the UTXOs from the user's wallet and then
     * stores in an object in the state
     * @returns {Promise&lt;void&gt;}
     */

    getUtxos = async () =&gt; {

        let Utxos = [];

        try {
            const rawUtxos = await this.API.getUtxos();

            for (const rawUtxo of rawUtxos) {
                const utxo = TransactionUnspentOutput.from_bytes(Buffer.from(rawUtxo, &quot;hex&quot;));
                const input = utxo.input();
                const txid = Buffer.from(input.transaction_id().to_bytes(), &quot;utf8&quot;).toString(&quot;hex&quot;);
                const txindx = input.index();
                const output = utxo.output();
                const amount = output.amount().coin().to_str(); // ADA amount in lovelace
                const multiasset = output.amount().multiasset();
                let multiAssetStr = &quot;&quot;;

                if (multiasset) {
                    const keys = multiasset.keys() // policy Ids of thee multiasset
                    const N = keys.len();
                    // console.log(`${N} Multiassets in the UTXO`)


                    for (let i = 0; i &lt; N; i++){
                        const policyId = keys.get(i);
                        const policyIdHex = Buffer.from(policyId.to_bytes(), &quot;utf8&quot;).toString(&quot;hex&quot;);
                        // console.log(`policyId: ${policyIdHex}`)
                        const assets = multiasset.get(policyId)
                        const assetNames = assets.keys();
                        const K = assetNames.len()
                        // console.log(`${K} Assets in the Multiasset`)

                        for (let j = 0; j &lt; K; j++) {
                            const assetName = assetNames.get(j);
                            const assetNameString = Buffer.from(assetName.name(),&quot;utf8&quot;).toString();
                            const assetNameHex = Buffer.from(assetName.name(),&quot;utf8&quot;).toString(&quot;hex&quot;)
                            const multiassetAmt = multiasset.get_asset(policyId, assetName)
                            multiAssetStr += `+ ${multiassetAmt.to_str()} + ${policyIdHex}.${assetNameHex} (${assetNameString})`
                            // console.log(assetNameString)
                            // console.log(`Asset Name: ${assetNameHex}`)
                        }
                    }
                }


                const obj = {
                    txid: txid,
                    txindx: txindx,
                    amount: amount,
                    str: `${txid} #${txindx} = ${amount}`,
                    multiAssetStr: multiAssetStr,
                    TransactionUnspentOutput: utxo
                }
                Utxos.push(obj);
                // console.log(`utxo: ${str}`)
            }
            this.setState({Utxos})
        } catch (err) {
            console.log(err)
        }
    }
</code></pre>
<p>Or you can take a look at my git repo with this code and other examples of cardano-serialization-lib: <a href="https://github.com/dynamicstrategies/cardano-wallet-connector" rel="nofollow noreferrer">https://github.com/dynamicstrategies/cardano-wallet-connector</a></p>

CC BY-SA 4.0
2022-02-19T13:22:03.263Z
2022-02-19T13:22:03.263Z
D S
<p>I did use <code> console.log(output.amount().multiasset().get(aadaTokenPolicy).get(aadaTokenName).to_str())</code> to transform output-&gt;Value-&gt;MultiAsset-&gt;Asset-&gt;AssetName-&gt;string.</p>
<p>For your case <code>.to_str()</code> might do the work.</p>

CC BY-SA 4.0
2022-02-18T07:11:53.050Z
2022-02-18T07:11:53.050Z
Mantas Andriuška
<p>I'm trying to figure out a reliable way to decode an assetName using JavaScript.</p>
<p>My current implementation is this:</p>
<pre><code>function hexToBytes (hex) {
    if(!hex){
        return hex;
    } else {
        const bytes = new Uint8Array(hex.length / 2);
        for (let i = 0; i !== bytes.length; i++) {
            bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
        }
        return bytes;
    }
}

return new TextDecoder(&quot;UTF-8&quot;).decode(hexToBytes(encodedAssetName))
</code></pre>
<p>The above seems to be work most of the time but on occasion seems to stumble on some assetNames.</p>
<p>As an alternative, I've been trying to see if the <code>cardano-serialization-lib</code> has anything built in that can help.</p>
<p>The most I can find is this:</p>
<pre><code>let assetName = Loader.Cardano.AssetName.new(Buffer.from(hex, 'hex'))
</code></pre>
<p><code>assetName</code> then seems to only have 3 functions and the <code>name</code> function returns a <code>Uint8Array</code>, which when passed using <code>new TextDecoder().decode(assetName.name())</code> returns the same result as my current implementation.</p>
<p>Is the cardano-serialization-lib method the best way to decode assetNames or is there a better way?</p>
<p>Any help would be greatly appreciated!</p>

CC BY-SA 4.0
2022-02-11T00:56:37.740Z
At this stage it looks like both methods work well, but the JS version is obviously lighter.
Casey Gibson
CC BY-SA 4.0
2022-07-20T03:07:13.247Z
The asset names are not guaranteed to be UTF-8 encoded. They are just a string of 8-bit bytes.
Erik de Castro Lopo
<cardano-serialization-lib><javascript>
Javascript assetName Decoding
# Javascript assetName Decoding

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

I'm trying to figure out a reliable way to decode an assetName using JavaScript.
My current implementation is this:
function hexToBytes (hex) {
    if(!hex){
        return hex;
    } else {
        const bytes = new Uint8Array(hex.length / 2);
        for (let i = 0; i !== bytes.length; i++) {
            bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
        }
        return bytes;
    }
}

return new TextDecoder("UTF-8").decode(hexToBytes(encodedAssetName))

The above seems to be work most of the time but on occasion seems to stumble on some assetNames.
As an alternative, I've been trying to see if the cardano-serialization-lib has anything built in that can help.
The most I can find is this:
let assetName = Loader.Cardano.AssetName.new(Buffer.from(hex, 'hex'))

assetName then seems to only have 3 functions and the name function returns a Uint8Array, which when passed using new TextDecoder().decode(assetName.name()) returns the same result as my current implementation.
Is the cardano-serialization-lib method the best way to decode assetNames or is there a better way?
Any help would be greatly appreciated!

**Tags:** cardano-serialization-lib, javascript
> **Comments:**
> **username_0**: At this stage it looks like both methods work well, but the JS version is obviously lighter.  
> **username_1**: The asset names are not guaranteed to be UTF-8 encoded. They are just a string of 8-bit bytes.  

---

## 2 answers

### Answer by username_2 on 2022-02-19. Score: 1

cardano-serialization-lib does pretty much everything for building and reading transactions and interacting with the webwallets.
Here is how you can extract all the asset names from the a user's wallet and process them to get the policyid, the assename hex value and the asset name string from each of the assets
This part this.API.getUtxos() reads all UTXOs from the user's wallet and TransactionUnspentOutput is a function from cardano-serialization-lib
    /**
     * Gets the UTXOs from the user's wallet and then
     * stores in an object in the state
     * @returns {Promise}
     */

    getUtxos = async () => {

        let Utxos = [];

        try {
            const rawUtxos = await this.API.getUtxos();

            for (const rawUtxo of rawUtxos) {
                const utxo = TransactionUnspentOutput.from_bytes(Buffer.from(rawUtxo, "hex"));
                const input = utxo.input();
                const txid = Buffer.from(input.transaction_id().to_bytes(), "utf8").toString("hex");
                const txindx = input.index();
                const output = utxo.output();
                const amount = output.amount().coin().to_str(); // ADA amount in lovelace
                const multiasset = output.amount().multiasset();
                let multiAssetStr = "";

                if (multiasset) {
                    const keys = multiasset.keys() // policy Ids of thee multiasset
                    const N = keys.len();
                    // console.log(`${N} Multiassets in the UTXO`)


                    for (let i = 0; i < N; i++){
                        const policyId = keys.get(i);
                        const policyIdHex = Buffer.from(policyId.to_bytes(), "utf8").toString("hex");
                        // console.log(`policyId: ${policyIdHex}`)
                        const assets = multiasset.get(policyId)
                        const assetNames = assets.keys();
                        const K = assetNames.len()
                        // console.log(`${K} Assets in the Multiasset`)

                        for (let j = 0; j < K; j++) {
                            const assetName = assetNames.get(j);
                            const assetNameString = Buffer.from(assetName.name(),"utf8").toString();
                            const assetNameHex = Buffer.from(assetName.name(),"utf8").toString("hex")
                            const multiassetAmt = multiasset.get_asset(policyId, assetName)
                            multiAssetStr += `+ ${multiassetAmt.to_str()} + ${policyIdHex}.${assetNameHex} (${assetNameString})`
                            // console.log(assetNameString)
                            // console.log(`Asset Name: ${assetNameHex}`)
                        }
                    }
                }


                const obj = {
                    txid: txid,
                    txindx: txindx,
                    amount: amount,
                    str: `${txid} #${txindx} = ${amount}`,
                    multiAssetStr: multiAssetStr,
                    TransactionUnspentOutput: utxo
                }
                Utxos.push(obj);
                // console.log(`utxo: ${str}`)
            }
            this.setState({Utxos})
        } catch (err) {
            console.log(err)
        }
    }

Or you can take a look at my git repo with this code and other examples of cardano-serialization-lib: https://github.com/dynamicstrategies/cardano-wallet-connector


---

### Answer by username_3 on 2022-02-18. Score: 0

I did use  `console.log(output.amount().multiasset().get(aadaTokenPolicy).get(aadaTokenName).to_str())` to transform output->Value->MultiAsset->Asset->AssetName->string.

For your case `.to_str()` might do the work.


---

