utils.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { PROGRAM_ID as BUBBLEGUM_PROGRAM_ID } from '@metaplex-foundation/mpl-bubblegum/dist/src/generated';
  2. import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from '@metaplex-foundation/mpl-token-metadata/dist/src/generated';
  3. import { SPL_ACCOUNT_COMPRESSION_PROGRAM_ID, SPL_NOOP_PROGRAM_ID } from '@solana/spl-account-compression';
  4. import { type AccountMeta, PublicKey, SystemProgram } from '@solana/web3.js';
  5. import * as bs58 from 'bs58';
  6. export function decode(stuff: string) {
  7. return bufferToArray(bs58.decode(stuff));
  8. }
  9. function bufferToArray(buffer: Buffer): number[] {
  10. const nums: number[] = [];
  11. for (let i = 0; i < buffer.length; i++) {
  12. nums.push(buffer[i]);
  13. }
  14. return nums;
  15. }
  16. export const mapProof = (assetProof: { proof: string[] }): AccountMeta[] => {
  17. if (!assetProof.proof || assetProof.proof.length === 0) {
  18. throw new Error('Proof is empty');
  19. }
  20. return assetProof.proof.map((node) => ({
  21. pubkey: new PublicKey(node),
  22. isSigner: false,
  23. isWritable: false,
  24. }));
  25. };
  26. export function getAccounts(collectionMint: PublicKey, tree: PublicKey) {
  27. // treeAuth
  28. const [treeAuthority] = PublicKey.findProgramAddressSync([tree.toBuffer()], BUBBLEGUM_PROGRAM_ID);
  29. // derive a PDA (owned by Bubblegum) to act as the signer of the compressed minting
  30. const [bubblegumSigner] = PublicKey.findProgramAddressSync(
  31. // `collection_cpi` is a custom prefix required by the Bubblegum program
  32. [Buffer.from('collection_cpi', 'utf8')],
  33. BUBBLEGUM_PROGRAM_ID,
  34. );
  35. // collection metadata account
  36. const [metadataAccount] = PublicKey.findProgramAddressSync(
  37. [Buffer.from('metadata', 'utf8'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), collectionMint.toBuffer()],
  38. TOKEN_METADATA_PROGRAM_ID,
  39. );
  40. // collection master edition
  41. const [masterEditionAccount] = PublicKey.findProgramAddressSync(
  42. [Buffer.from('metadata', 'utf8'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), collectionMint.toBuffer(), Buffer.from('edition', 'utf8')],
  43. TOKEN_METADATA_PROGRAM_ID,
  44. );
  45. return {
  46. treeAuthority,
  47. collectionMint,
  48. collectionMetadata: metadataAccount,
  49. editionAccount: masterEditionAccount,
  50. merkleTree: tree,
  51. bubblegumSigner,
  52. logWrapper: SPL_NOOP_PROGRAM_ID,
  53. compressionProgram: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
  54. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  55. bubblegumProgram: BUBBLEGUM_PROGRAM_ID,
  56. systemProgram: SystemProgram.programId,
  57. };
  58. }