Skip to content

Commit

Permalink
Add client helper for ProgrammableNonFungible master editions (#64)
Browse files Browse the repository at this point in the history
* Fix shank annotation on account

* Add helper

* Regenerate clients

* Add key check

* Change error type

* Improve data length test

* Use specific length error

* Code style
  • Loading branch information
febo authored Nov 18, 2023
1 parent cfc9d42 commit b18bbb0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function setAndVerifySizedCollectionItem(
collection: { index: 5, isWritable: true, value: input.collection ?? null },
collectionMasterEditionAccount: {
index: 6,
isWritable: true,
isWritable: false,
value: input.collectionMasterEditionAccount ?? null,
},
collectionAuthorityRecord: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl SetAndVerifySizedCollectionItem {
self.collection,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.collection_master_edition_account,
false,
));
Expand Down Expand Up @@ -333,7 +333,7 @@ impl<'a, 'b> SetAndVerifySizedCollectionItemCpi<'a, 'b> {
*self.collection.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.collection_master_edition_account.key,
false,
));
Expand Down
39 changes: 39 additions & 0 deletions clients/rust/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
use borsh::BorshDeserialize;

use crate::{
errors::MplTokenMetadataError,
types::{Key, TokenStandard},
};

/// The offset of the token standard byte in the master edition
/// from the end of the account data.
const TOKEN_STANDARD_OFFSET: usize = 1;

/// Removes all null bytes from a string.
pub fn clean(value: String) -> String {
value.replace('\0', "")
}

/// Checks that the `master_edition` is Programmable NFT master edition.
pub fn assert_edition_is_programmable(edition_data: &[u8]) -> Result<(), MplTokenMetadataError> {
if edition_data.len() > TOKEN_STANDARD_OFFSET {
// the first byte is the account key
let key = Key::deserialize(&mut &edition_data[0..1])
.map_err(|_error| MplTokenMetadataError::InvalidEditionKey)?;

return match key {
Key::MasterEditionV1 | Key::MasterEditionV2 => {
// the last byte is the token standard
let standard = TokenStandard::deserialize(
&mut &edition_data[edition_data.len() - TOKEN_STANDARD_OFFSET..],
)
.map_err(|_error| MplTokenMetadataError::InvalidTokenStandard)?;

return match standard {
TokenStandard::ProgrammableNonFungible
| TokenStandard::ProgrammableNonFungibleEdition => Ok(()),
_ => Err(MplTokenMetadataError::InvalidTokenStandard),
};
}
_ => Err(MplTokenMetadataError::InvalidEditionKey),
};
}

Err(MplTokenMetadataError::InvalidMasterEditionAccountLength)
}
2 changes: 1 addition & 1 deletion idls/token_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2288,7 +2288,7 @@
},
{
"name": "collectionMasterEditionAccount",
"isMut": true,
"isMut": false,
"isSigner": false,
"docs": [
"MasterEdition2 Account of the Collection Token"
Expand Down
2 changes: 1 addition & 1 deletion programs/token-metadata/program/src/instruction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ pub enum MetadataInstruction {
#[account(3, name="update_authority", desc="Update Authority of Collection NFT and NFT")]
#[account(4, name="collection_mint", desc="Mint of the Collection")]
#[account(5, writable, name="collection", desc="Metadata Account of the Collection")]
#[account(6, writable, name="collection_master_edition_account", desc="MasterEdition2 Account of the Collection Token")]
#[account(6, name="collection_master_edition_account", desc="MasterEdition2 Account of the Collection Token")]
#[account(7, optional, name="collection_authority_record", desc="Collection Authority Record PDA")]
#[legacy_optional_accounts_strategy]
SetAndVerifySizedCollectionItem,
Expand Down

0 comments on commit b18bbb0

Please sign in to comment.