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!