Skip to content

Commit 3f32e13

Browse files
Fixes (#31)
* handle possible error * add edition decoders * improve returned errors
1 parent 55a33d0 commit 3f32e13

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

toolbox/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "tensor-toolbox"
33
description = "Toolbox of useful Rust utilities for Tensor Foundation's Solana programs"
44
repository = "https://github.com/tensor-foundation/toolbox"
55
homepage = "https://github.com/tensor-foundation/toolbox"
6-
version = "0.7.1"
6+
version = "0.8.0"
77
edition = "2021"
88
readme = "../README.md"
99
license = "Apache-2.0"

toolbox/src/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ pub enum TensorError {
3131

3232
#[msg("invalid whitelist")]
3333
InvalidWhitelist = 9010,
34+
35+
#[msg("invalid program owner")]
36+
InvalidProgramOwner = 9011,
37+
38+
#[msg("invalid edition")]
39+
InvalidEdition = 9012,
3440
}

toolbox/src/metaplex_core.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ pub fn validate_core_asset(
5050
assert_ownership(asset_info, Key::AssetV1)?;
5151

5252
// validates the collection is owned by the MPL Core program
53-
maybe_collection_info
54-
.as_ref()
55-
.map(|c| assert_ownership(c, Key::CollectionV1));
53+
if let Some(collection_info) = maybe_collection_info {
54+
assert_ownership(collection_info, Key::CollectionV1)?;
55+
}
5656

5757
let asset = BaseAssetV1::try_from(asset_info)?;
5858

toolbox/src/token_metadata.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use anchor_spl::{
77
token_interface::{self, Mint, TokenAccount, TokenInterface, TransferChecked},
88
};
99
use mpl_token_metadata::{
10-
accounts::Metadata,
10+
accounts::{Edition, MasterEdition, Metadata},
1111
instructions::{DelegateTransferV1CpiBuilder, TransferV1CpiBuilder},
12-
types::{AuthorizationData, TokenStandard},
12+
types::{AuthorizationData, Key as MplKey, TokenStandard},
1313
};
1414
use tensor_vipers::{throw_err, unwrap_opt};
1515

@@ -20,7 +20,7 @@ pub use mpl_token_metadata::ID;
2020
#[inline(never)]
2121
pub fn assert_decode_metadata(mint: &Pubkey, metadata: &AccountInfo) -> Result<Metadata> {
2222
if *metadata.owner != mpl_token_metadata::ID {
23-
throw_err!(TensorError::BadMetadata);
23+
throw_err!(TensorError::InvalidProgramOwner);
2424
}
2525

2626
// We must use `safe_deserialize` since there are variations on the metadata struct
@@ -36,6 +36,36 @@ pub fn assert_decode_metadata(mint: &Pubkey, metadata: &AccountInfo) -> Result<M
3636
Ok(metadata)
3737
}
3838

39+
#[inline(never)]
40+
pub fn assert_decode_master_edition(edition: &AccountInfo) -> Result<MasterEdition> {
41+
if *edition.owner != mpl_token_metadata::ID {
42+
throw_err!(TensorError::InvalidProgramOwner);
43+
}
44+
45+
let edition = MasterEdition::safe_deserialize(&edition.try_borrow_data()?)
46+
.map_err(|_error| TensorError::InvalidEdition)?;
47+
48+
Ok(edition)
49+
}
50+
51+
#[inline(never)]
52+
pub fn assert_decode_edition(edition: &AccountInfo) -> Result<Edition> {
53+
if *edition.owner != mpl_token_metadata::ID {
54+
throw_err!(TensorError::InvalidProgramOwner);
55+
}
56+
57+
let data = edition.try_borrow_data()?;
58+
59+
if data.is_empty() || data[0] != MplKey::EditionV1 as u8 {
60+
throw_err!(TensorError::InvalidEdition);
61+
}
62+
63+
let edition = Edition::from_bytes(&edition.try_borrow_data()?)
64+
.map_err(|_error| TensorError::InvalidEdition)?;
65+
66+
Ok(edition)
67+
}
68+
3969
/// Transfer Args using AccountInfo types to be more generic.
4070
pub struct TransferArgsAi<'a, 'info> {
4171
/// Account that will pay for any associated fees.

0 commit comments

Comments
 (0)