From 27e3fd6a2c0a18517b9e5510f17ba1fb981ba25e Mon Sep 17 00:00:00 2001 From: austbot Date: Sun, 19 Jun 2022 12:57:04 -0500 Subject: [PATCH 1/2] adds more validation to bgum and parses redeem and cancel redeem --- contracts/programs/bubblegum/src/error.rs | 4 ++++ contracts/programs/bubblegum/src/lib.rs | 24 +++++++++++++++---- .../bubblegum/src/state/leaf_schema.rs | 1 + contracts/programs/bubblegum/src/utils.rs | 4 ++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/contracts/programs/bubblegum/src/error.rs b/contracts/programs/bubblegum/src/error.rs index 31fba5d8928..76ee597c2a7 100644 --- a/contracts/programs/bubblegum/src/error.rs +++ b/contracts/programs/bubblegum/src/error.rs @@ -6,4 +6,8 @@ pub enum BubblegumError { AssetOwnerMismatch, #[msg("PublicKeyMismatch")] PublicKeyMismatch, + #[msg("Hashing Mismatch Within Leaf Schema")] + HashingMismatch, + #[msg("Unsupported Schema Version")] + UnsupportedSchemaVersion, } \ No newline at end of file diff --git a/contracts/programs/bubblegum/src/lib.rs b/contracts/programs/bubblegum/src/lib.rs index 3fb1408df48..f92066c183f 100644 --- a/contracts/programs/bubblegum/src/lib.rs +++ b/contracts/programs/bubblegum/src/lib.rs @@ -12,10 +12,14 @@ use { NewNFTEvent, }, 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::*, @@ -321,8 +325,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( @@ -582,8 +584,22 @@ pub mod bubblegum { pub fn decompress_v1(ctx: Context, 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)?; + match ctx.accounts.voucher.leaf_schema { + LeafSchema::V1 { + owner, + data_hash, + .. + } => { + 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::<(), ProgramError>(()) + } + }?; let voucher = &ctx.accounts.voucher; match metadata.token_program_version { TokenProgramVersion::Original => { diff --git a/contracts/programs/bubblegum/src/state/leaf_schema.rs b/contracts/programs/bubblegum/src/state/leaf_schema.rs index 0cc3e72a91e..11cda9fa14c 100644 --- a/contracts/programs/bubblegum/src/state/leaf_schema.rs +++ b/contracts/programs/bubblegum/src/state/leaf_schema.rs @@ -95,6 +95,7 @@ impl LeafSchema { LeafSchema::V1 { data_hash, .. } => *data_hash, } } + pub fn to_event(&self) -> LeafSchemaEvent { LeafSchemaEvent { diff --git a/contracts/programs/bubblegum/src/utils.rs b/contracts/programs/bubblegum/src/utils.rs index fb436f940b4..4e74a23fbe0 100644 --- a/contracts/programs/bubblegum/src/utils.rs +++ b/contracts/programs/bubblegum/src/utils.rs @@ -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) -> Result<()> { if !cmp_pubkeys(a, b) { if error.is_some() { From a67db30c70c681cc48c654c19b925db3d772a844 Mon Sep 17 00:00:00 2001 From: austbot Date: Mon, 20 Jun 2022 12:38:56 -0500 Subject: [PATCH 2/2] all bgum events done --- contracts/programs/bubblegum/src/lib.rs | 13 +++++++++++-- contracts/programs/bubblegum/src/state/mod.rs | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/contracts/programs/bubblegum/src/lib.rs b/contracts/programs/bubblegum/src/lib.rs index f92066c183f..78557b82e58 100644 --- a/contracts/programs/bubblegum/src/lib.rs +++ b/contracts/programs/bubblegum/src/lib.rs @@ -10,6 +10,7 @@ use { Nonce, Voucher, metaplex_adapter::{MetadataArgs, TokenProgramVersion}, NewNFTEvent, + NFTDecompressionEvent }, gummyroll::{program::Gummyroll, Node}, crate::error::BubblegumError, @@ -585,10 +586,11 @@ pub mod bubblegum { pub fn decompress_v1(ctx: Context, metadata: MetadataArgs) -> Result<()> { // Allocate and create mint let incoming_data_hash = hash_metadata(&metadata)?; - match ctx.accounts.voucher.leaf_schema { + let event = match ctx.accounts.voucher.leaf_schema { LeafSchema::V1 { owner, data_hash, + nonce, .. } => { if !cmp_bytes(&data_hash, &incoming_data_hash, 32) { @@ -597,8 +599,14 @@ pub mod bubblegum { if !cmp_pubkeys(&owner, ctx.accounts.owner.key) { return Err(BubblegumError::AssetOwnerMismatch.into()); } - Ok::<(), ProgramError>(()) + 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 { @@ -756,6 +764,7 @@ pub mod bubblegum { &[ctx.bumps["mint_authority"]], ]], )?; + emit!(event); Ok(()) } diff --git a/contracts/programs/bubblegum/src/state/mod.rs b/contracts/programs/bubblegum/src/state/mod.rs index 6903c41a9b3..c2cac9e0df3 100644 --- a/contracts/programs/bubblegum/src/state/mod.rs +++ b/contracts/programs/bubblegum/src/state/mod.rs @@ -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, +}