Spaces:
Sleeping
Sleeping
File size: 2,832 Bytes
6655e35 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | import { PublicKey } from '@solana/web3.js';
import {
addCodecSizePrefix,
fixCodecSize,
getArrayCodec,
getBytesCodec,
getUtf8Codec,
getU32Codec,
getStructCodec,
getTupleCodec,
} from '@solana/codecs';
import type { ReadonlyUint8Array, VariableSizeCodec } from '@solana/codecs';
export const TOKEN_METADATA_DISCRIMINATOR = Buffer.from([112, 132, 90, 90, 11, 88, 157, 87]);
function getStringCodec(): VariableSizeCodec<string> {
return addCodecSizePrefix(getUtf8Codec(), getU32Codec());
}
const tokenMetadataCodec = getStructCodec([
['updateAuthority', fixCodecSize(getBytesCodec(), 32)],
['mint', fixCodecSize(getBytesCodec(), 32)],
['name', getStringCodec()],
['symbol', getStringCodec()],
['uri', getStringCodec()],
['additionalMetadata', getArrayCodec(getTupleCodec([getStringCodec(), getStringCodec()]))],
]);
export interface TokenMetadata {
// The authority that can sign to update the metadata
updateAuthority?: PublicKey;
// The associated mint, used to counter spoofing to be sure that metadata belongs to a particular mint
mint: PublicKey;
// The longer name of the token
name: string;
// The shortened symbol for the token
symbol: string;
// The URI pointing to richer metadata
uri: string;
// Any additional metadata about the token as key-value pairs
additionalMetadata: (readonly [string, string])[];
}
// Checks if all elements in the array are 0
function isNonePubkey(buffer: ReadonlyUint8Array): boolean {
for (let i = 0; i < buffer.length; i++) {
if (buffer[i] !== 0) {
return false;
}
}
return true;
}
// Pack TokenMetadata into byte slab
export function pack(meta: TokenMetadata): ReadonlyUint8Array {
// If no updateAuthority given, set it to the None/Zero PublicKey for encoding
const updateAuthority = meta.updateAuthority ?? PublicKey.default;
return tokenMetadataCodec.encode({
...meta,
updateAuthority: updateAuthority.toBuffer(),
mint: meta.mint.toBuffer(),
});
}
// unpack byte slab into TokenMetadata
export function unpack(buffer: Buffer | Uint8Array | ReadonlyUint8Array): TokenMetadata {
const data = tokenMetadataCodec.decode(buffer);
return isNonePubkey(data.updateAuthority)
? {
mint: new PublicKey(data.mint),
name: data.name,
symbol: data.symbol,
uri: data.uri,
additionalMetadata: data.additionalMetadata,
}
: {
updateAuthority: new PublicKey(data.updateAuthority),
mint: new PublicKey(data.mint),
name: data.name,
symbol: data.symbol,
uri: data.uri,
additionalMetadata: data.additionalMetadata,
};
}
|