Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to add creators to cNFTs #16

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions programs/bubblegum/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub enum BubblegumError {
CollectionMustBeAUniqueMasterEdition,
#[msg("Could not convert external error to BubblegumError")]
UnknownExternalError,
#[msg("Metadata must be mutable to make this change")]
MetadataMustBeMutable,
}

// Converts certain Token Metadata errors into Bubblegum equivalents
Expand Down
95 changes: 95 additions & 0 deletions programs/bubblegum/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,30 @@ pub struct CreatorVerification<'info> {
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct AddCreator<'info> {
#[account(
seeds = [merkle_tree.key().as_ref()],
bump,
has_one = tree_delegate
)]
pub tree_authority: Account<'info, TreeConfig>,
pub tree_delegate: Signer<'info>,
/// CHECK: The new creator being added
pub creator: AccountInfo<'info>,
/// CHECK: This account is checked in the instruction
pub leaf_owner: UncheckedAccount<'info>,
/// CHECK: This account is checked in the instruction
pub leaf_delegate: UncheckedAccount<'info>,
#[account(mut)]
/// CHECK: This account is modified in the downstream program
pub merkle_tree: UncheckedAccount<'info>,
pub payer: Signer<'info>,
pub log_wrapper: Program<'info, Noop>,
pub compression_program: Program<'info, SplAccountCompression>,
pub system_program: Program<'info, System>,
}

#[derive(Accounts)]
pub struct CollectionVerification<'info> {
#[account(
Expand Down Expand Up @@ -468,6 +492,7 @@ pub enum InstructionName {
UnverifyCollection,
SetAndVerifyCollection,
MintToCollectionV1,
AddCreator,
}

pub fn get_instruction_type(full_bytes: &[u8]) -> InstructionName {
Expand Down Expand Up @@ -1078,6 +1103,76 @@ pub mod bubblegum {
)
}

pub fn add_creator<'info>(
ctx: Context<'_, '_, '_, 'info, AddCreator<'info>>,
root: [u8; 32],
metadata: MetadataArgs,
nonce: u64,
index: u32,
) -> Result<()> {
require!(metadata.is_mutable, BubblegumError::MetadataMustBeMutable);

let owner = ctx.accounts.leaf_owner.to_account_info();
let delegate = ctx.accounts.leaf_delegate.to_account_info();
let merkle_tree = ctx.accounts.merkle_tree.to_account_info();
let mut new_metadata = metadata.clone();

let old_creators = new_metadata.creators;

// Calculate new creator Vec with `verified` set to true for signing creator.
let mut updated_creator_vec = old_creators.clone();
updated_creator_vec.push(Creator {
address: ctx.accounts.creator.key(),
verified: ctx.accounts.creator.is_signer,
// TODO: Need to add a rebalance shares endpoint to allow setting these shares,
// and ensuring they all balance to 100
share: 0,
});

new_metadata.creators = updated_creator_vec;

// Calculate new creator hash.
let updated_creator_hash = hash_creators(&new_metadata.creators)?;

// Calculate new data hash.
let updated_data_hash = hash_metadata(&new_metadata)?;

// Build previous leaf struct, new leaf struct, and replace the leaf in the tree.
let asset_id = get_asset_id(&merkle_tree.key(), nonce);
let previous_leaf = LeafSchema::new_v0(
asset_id,
owner.key(),
delegate.key(),
nonce,
hash_metadata(&metadata)?,
hash_creators(&old_creators)?,
);
let new_leaf = LeafSchema::new_v0(
asset_id,
owner.key(),
delegate.key(),
nonce,
updated_data_hash,
updated_creator_hash,
);

wrap_application_data_v1(new_leaf.to_event().try_to_vec()?, &ctx.accounts.log_wrapper)?;

replace_leaf(
&merkle_tree.key(),
*ctx.bumps.get("tree_authority").unwrap(),
&ctx.accounts.compression_program.to_account_info(),
&ctx.accounts.tree_authority.to_account_info(),
&ctx.accounts.merkle_tree.to_account_info(),
&ctx.accounts.log_wrapper.to_account_info(),
ctx.remaining_accounts,
root,
previous_leaf.to_node(),
new_leaf.to_node(),
index,
)
}

pub fn unverify_creator<'info>(
ctx: Context<'_, '_, '_, 'info, CreatorVerification<'info>>,
root: [u8; 32],
Expand Down