user212: 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!

user6196: 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<void>}
     */

    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

212: <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>
 6196: <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>
