Skip to content

Commit 4e8ff59

Browse files
committed
Simplify Mint deserialization and add account type validation
- Remove StateWithExtensions overhead, just unpack base 82-byte mint - Validate byte 165 == 1 for mints with extensions (rejects token accounts)
1 parent d08f02d commit 4e8ff59

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

spl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ token_2022 = ["spl-token-2022"]
2626
token_2022_extensions = ["spl-token-2022", "spl-token-group-interface", "spl-token-metadata-interface", "spl-pod"]
2727

2828
[dependencies]
29-
anchor-lang = { path = "../lang", features = ["derive"] }
29+
anchor-lang = { version = "0.31.1", features = ["derive"] }
3030
borsh = { version = "0.10.3", optional = true }
3131
mpl-token-metadata = { version = "5", optional = true }
3232
spl-associated-token-account = { version = "6", features = ["no-entrypoint"], optional = true }

spl/src/token_interface.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ pub struct Mint(spl_token_2022::state::Mint);
5050

5151
impl anchor_lang::AccountDeserialize for Mint {
5252
fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
53-
spl_token_2022::extension::StateWithExtensions::<spl_token_2022::state::Mint>::unpack(buf)
53+
// If >= 166 bytes, validate account type byte is Mint (1)
54+
if buf.len() >= 166 && buf[165] != 1 {
55+
return Err(anchor_lang::error::ErrorCode::AccountDidNotDeserialize.into());
56+
}
57+
58+
// Deserialize base mint only (82 bytes)
59+
let base_slice = &buf[..spl_token_2022::state::Mint::LEN];
60+
spl_token_2022::extension::StateWithExtensions::<spl_token_2022::state::Mint>::unpack(base_slice)
5461
.map(|t| Mint(t.base))
5562
.map_err(Into::into)
5663
}

0 commit comments

Comments
 (0)