metaplex-foundation / mpl-token-metadata

Program to attach additional data to Fungible or Non-Fungible tokens on Solana.
https://developers.metaplex.com/token-metadata
Other
125 stars 65 forks source link

Metadata.fromAccountAddress returns garbage at end of strings #9

Closed edgework closed 1 year ago

edgework commented 1 year ago

Using "@metaplex-foundation/mpl-token-metadata": "^2.12.0",

async function example()
{
    const web3 = require("@solana/web3.js");
    const Metadata = require("@metaplex-foundation/mpl-token-metadata");

    const connection = new web3.Connection(web3.clusterApiUrl("mainnet-beta"));
    const mint = new PublicKey('PhiLR4JDZB9z92rYT5xBXKCxmq4pGB1LYjtybii7aiS');

    const [publicKey] = await web3.PublicKey.findProgramAddress(
        [Buffer.from("metadata"), Metadata.PROGRAM_ID.toBuffer(), mint.toBuffer()], Metadata.PROGRAM_ID
    );

    const tokenInfo = await sMetadata.Metadata.fromAccountAddress(connection, publicKey);

    console.log(tokenInfo);
}

returns


Metadata {
  key: 4,
  updateAuthority: PublicKey [PublicKey(povZWsPCagU5MS43jsakEuqyMiwKyRXJotJdDRMYZkz)] {
    _bn: <BN: c3f5ac3d092ca771a34c64f6f626f64bb291bf1c5f034280733c51da00e498f>
  },
  mint: PublicKey [PublicKey(PhiLR4JDZB9z92rYT5xBXKCxmq4pGB1LYjtybii7aiS)] {
    _bn: <BN: 5d0cfcf80b0814f0591790318457d74c5c3cc68184e4297427252c083fab1a7>
  },
  data: {
    name: 'Phil Overdrive | Youtube\x00\x00\x00\x00\x00\x00\x00\x00',
    symbol: 'POVT\x00\x00\x00\x00\x00\x00',
    uri: 'https://raw.githubusercontent.com/PhilOverdrive/Token/main/PhiLR4JDZB9z92rYT5xBXKCxmq4pGB1LYjtybii7aiS-info.json\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00',
    sellerFeeBasisPoints: 0,
    creators: null
  },
  primarySaleHappened: false,
  isMutable: true,
  editionNonce: 255,
  tokenStandard: 2,
  collection: null,
  uses: null,
  collectionDetails: null,
  programmableConfig: null
}

Notice all the \x00 in the string values. This happens for any mint value token

lorisleiva commented 1 year ago

Hi there, this is because strings on Token Metadata are stored using a fixed amount of bytes are therefore you are likely to get null characters at the end (used as padding). The Token Metadata Umi library (available at @metaplex-foundation/mpl-token-metadata@alpha) already abstracts this away from you but not the Solita-generated library you are using.

If you wish to continue using the Solita-generated library, you can remove these null characters using the following function:

export const removeNullCharacters = (value: string) =>
  // eslint-disable-next-line no-control-regex
  value.replace(/\u0000/g, '');