@gorbchain-xyz/chaindecode
Version:
GorbchainSDK V1.3+ - Complete Solana development toolkit with advanced cryptography, messaging, and collaboration features. Build secure applications with blockchain, DeFi, and end-to-end encryption.
33 lines (32 loc) • 1.49 kB
JavaScript
/**
* Decode a Metaplex Metadata account buffer (raw data)
* This is a minimal implementation for demonstration. For full support, use BufferLayout or Beet.
*/
export function decodeNFT(data) {
// Metaplex Metadata layout (v1):
// https://github.com/metaplex-foundation/metaplex-program-library/blob/master/token-metadata/program/src/state/metadata.rs
// [key (1), updateAuthority (32), mint (32), name (32), symbol (10), uri (200), sellerFeeBasisPoints (2), ...]
const name = Buffer.from(data.slice(1 + 32 + 32, 1 + 32 + 32 + 32))
.toString('utf8').replace(/\0+$/, '');
const symbol = Buffer.from(data.slice(1 + 32 + 32 + 32, 1 + 32 + 32 + 32 + 10))
.toString('utf8').replace(/\0+$/, '');
const uri = Buffer.from(data.slice(1 + 32 + 32 + 32 + 10, 1 + 32 + 32 + 32 + 10 + 200))
.toString('utf8').replace(/\0+$/, '');
// Use DataView for cross-type compatibility
const sellerFeeOffset = 1 + 32 + 32 + 32 + 10 + 200;
let sellerFeeBasisPoints = 0;
if (typeof Buffer !== 'undefined' && data instanceof Buffer) {
sellerFeeBasisPoints = data.readUInt16LE(sellerFeeOffset);
}
else {
const dv = new DataView(data.buffer, data.byteOffset, data.byteLength);
sellerFeeBasisPoints = dv.getUint16(sellerFeeOffset, true);
}
return {
name,
symbol,
uri,
sellerFeeBasisPoints
// TODO: parse creators, collection, uses, editionNonce, updateAuthority, etc.
};
}