I'm trying to figure out a reliable way to decode an assetName using JavaScript.

My current implementation is this:

function hexToBytes (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!

  • At this stage it looks like both methods work well, but the JS version is obviously lighter. Feb 11, 2022 at 0:56
  • The asset names are not guaranteed to be UTF-8 encoded. They are just a string of 8-bit bytes. Jul 20, 2022 at 3:07

2 Answers 2


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
                // console.log(`utxo: ${str}`)
        } catch (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


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.