Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: take hash of token_id if it is longer than 32 #208

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@ use crate::state::message::{
Payload,
};
use anchor_lang::prelude::*;
use anchor_lang::solana_program::hash::hash;
use anchor_spl::metadata::mpl_token_metadata::types::DataV2;
use anchor_spl::metadata::{
create_metadata_accounts_v3, CreateMetadataAccountsV3, Metadata as Metaplex, ID as MetaplexID,
};
use anchor_spl::token::{Mint, Token};

pub trait StringExt {
fn to_hashed_bytes(&self) -> [u8; 32];
}

impl StringExt for String {
fn to_hashed_bytes(&self) -> [u8; 32] {
let bytes = self.as_bytes();
if bytes.len() > 32 {
let hash = hash(bytes);
hash.to_bytes()
} else {
let mut padded_bytes = [0u8; 32];
padded_bytes[..bytes.len()].copy_from_slice(bytes);
padded_bytes
}
}
}

#[derive(Accounts)]
#[instruction(data: SignedPayload<DeployTokenPayload>)]
pub struct DeployToken<'info> {
Expand All @@ -23,7 +42,7 @@ pub struct DeployToken<'info> {
#[account(
init,
payer = common.payer,
seeds = [WRAPPED_MINT_SEED, data.payload.token.as_bytes().as_ref()],
seeds = [WRAPPED_MINT_SEED, data.payload.token.to_hashed_bytes().as_ref()],
bump,
mint::decimals = std::cmp::min(MAX_ALLOWED_DECIMALS, data.payload.decimals),
mint::authority = authority,
Expand Down
Loading