utils.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import {AccountMeta, PublicKey, SystemProgram} from "@solana/web3.js";
  2. import {PROGRAM_ID as BUBBLEGUM_PROGRAM_ID} from "@metaplex-foundation/mpl-bubblegum/dist/src/generated";
  3. import {PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID} from "@metaplex-foundation/mpl-token-metadata/dist/src/generated";
  4. import * as bs58 from "bs58";
  5. import {SPL_ACCOUNT_COMPRESSION_PROGRAM_ID, SPL_NOOP_PROGRAM_ID} from "@solana/spl-account-compression";
  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(
  29. [tree.toBuffer()],
  30. BUBBLEGUM_PROGRAM_ID
  31. );
  32. // derive a PDA (owned by Bubblegum) to act as the signer of the compressed minting
  33. const [bubblegumSigner] = PublicKey.findProgramAddressSync(
  34. // `collection_cpi` is a custom prefix required by the Bubblegum program
  35. [Buffer.from("collection_cpi", "utf8")],
  36. BUBBLEGUM_PROGRAM_ID
  37. );
  38. // collection metadata account
  39. const [metadataAccount] = PublicKey.findProgramAddressSync(
  40. [Buffer.from("metadata", "utf8"), TOKEN_METADATA_PROGRAM_ID.toBuffer(), collectionMint.toBuffer()],
  41. TOKEN_METADATA_PROGRAM_ID
  42. );
  43. // collection master edition
  44. const [masterEditionAccount] = PublicKey.findProgramAddressSync(
  45. [
  46. Buffer.from("metadata", "utf8"),
  47. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  48. collectionMint.toBuffer(),
  49. Buffer.from("edition", "utf8"),
  50. ],
  51. TOKEN_METADATA_PROGRAM_ID
  52. );
  53. return {
  54. treeAuthority,
  55. collectionMint,
  56. collectionMetadata: metadataAccount,
  57. editionAccount: masterEditionAccount,
  58. merkleTree: tree,
  59. bubblegumSigner,
  60. logWrapper: SPL_NOOP_PROGRAM_ID,
  61. compressionProgram: SPL_ACCOUNT_COMPRESSION_PROGRAM_ID,
  62. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  63. bubblegumProgram: BUBBLEGUM_PROGRAM_ID,
  64. systemProgram: SystemProgram.programId,
  65. };
  66. }