Skip to content

Commit

Permalink
Merge pull request solana-labs#116 from jarry-xiao/more-bgum-parsing
Browse files Browse the repository at this point in the history
adds more validation to bgum and parses redeem and cancel redeem
  • Loading branch information
austbot authored Jun 20, 2022
2 parents c1b7f08 + a67db30 commit 0e3a1e8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
4 changes: 4 additions & 0 deletions contracts/programs/bubblegum/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ pub enum BubblegumError {
AssetOwnerMismatch,
#[msg("PublicKeyMismatch")]
PublicKeyMismatch,
#[msg("Hashing Mismatch Within Leaf Schema")]
HashingMismatch,
#[msg("Unsupported Schema Version")]
UnsupportedSchemaVersion,
}
33 changes: 29 additions & 4 deletions contracts/programs/bubblegum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ use {
Nonce, Voucher,
metaplex_adapter::{MetadataArgs, TokenProgramVersion},
NewNFTEvent,
NFTDecompressionEvent
},
gummyroll::{program::Gummyroll, Node},
crate::error::BubblegumError,
crate::utils::{append_leaf,
insert_or_append_leaf,
replace_leaf,
get_asset_id,
cmp_bytes,
cmp_pubkeys,
assert_pubkey_equal,
},
anchor_lang::{
prelude::*,
Expand Down Expand Up @@ -321,8 +326,6 @@ pub fn get_instruction_type(full_bytes: &[u8]) -> InstructionName {

#[program]
pub mod bubblegum {
use crate::error::BubblegumError;
use crate::utils::assert_pubkey_equal;
use super::*;

pub fn create_tree(
Expand Down Expand Up @@ -582,8 +585,29 @@ pub mod bubblegum {

pub fn decompress_v1(ctx: Context<DecompressV1>, metadata: MetadataArgs) -> Result<()> {
// Allocate and create mint
let data_hash = hash_metadata(&metadata)?;
assert_eq!(ctx.accounts.voucher.leaf_schema.data_hash(), data_hash);
let incoming_data_hash = hash_metadata(&metadata)?;
let event = match ctx.accounts.voucher.leaf_schema {
LeafSchema::V1 {
owner,
data_hash,
nonce,
..
} => {
if !cmp_bytes(&data_hash, &incoming_data_hash, 32) {
return Err(BubblegumError::HashingMismatch.into());
}
if !cmp_pubkeys(&owner, ctx.accounts.owner.key) {
return Err(BubblegumError::AssetOwnerMismatch.into());
}
Ok(NFTDecompressionEvent {
version: Version::V1,
tree_id: ctx.accounts.voucher.merkle_slab.key(),
id: get_asset_id(&ctx.accounts.voucher.merkle_slab.key(), nonce),
nonce: nonce
})
}
_ => Err(BubblegumError::UnsupportedSchemaVersion)
}?;
let voucher = &ctx.accounts.voucher;
match metadata.token_program_version {
TokenProgramVersion::Original => {
Expand Down Expand Up @@ -740,6 +764,7 @@ pub mod bubblegum {
&[ctx.bumps["mint_authority"]],
]],
)?;
emit!(event);
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions contracts/programs/bubblegum/src/state/leaf_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl LeafSchema {
LeafSchema::V1 { data_hash, .. } => *data_hash,
}
}


pub fn to_event(&self) -> LeafSchemaEvent {
LeafSchemaEvent {
Expand Down
9 changes: 9 additions & 0 deletions contracts/programs/bubblegum/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@ impl Voucher {
}
}
}

#[event]
pub struct NewNFTEvent {
pub version: Version,
pub metadata: MetadataArgs,
pub nonce: u64,
}

#[event]
pub struct NFTDecompressionEvent {
pub version: Version,
pub id: Pubkey,
pub tree_id: Pubkey,
pub nonce: u64,
}
4 changes: 4 additions & 0 deletions contracts/programs/bubblegum/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ pub fn cmp_pubkeys(a: &Pubkey, b: &Pubkey) -> bool {
sol_memcmp(a.as_ref(), b.as_ref(), PUBKEY_BYTES) == 0
}

pub fn cmp_bytes(a: &[u8], b: &[u8], size: usize) -> bool {
sol_memcmp(a.as_ref(), b.as_ref(), size) == 0
}

pub fn assert_pubkey_equal(a: &Pubkey, b: &Pubkey, error: Option<anchor_lang::error::Error>) -> Result<()> {
if !cmp_pubkeys(a, b) {
if error.is_some() {
Expand Down

0 comments on commit 0e3a1e8

Please sign in to comment.