lez-programs/token/src/initialize.rs
Ricardo Guilherme Schmidt 8005c74e26 feat(token): verify definition ownership via self_program_id in initialize and mint
Pass `ctx.self_program_id` from `ProgramContext` into `initialize_account`
and `mint`, which now assert that the token definition account is owned by
the token program. This prevents callers from supplying a foreign-owned
account as the definition.

See https://github.com/logos-co/spel/issues/172
2026-05-12 16:10:40 +02:00

40 lines
1.3 KiB
Rust

use nssa_core::{
account::{Account, AccountWithMetadata, Data},
program::{AccountPostState, Claim, ProgramId},
};
use token_core::{TokenDefinition, TokenHolding};
pub fn initialize_account(
definition_account: AccountWithMetadata,
account_to_initialize: AccountWithMetadata,
token_program_id: ProgramId,
) -> Vec<AccountPostState> {
assert_eq!(
account_to_initialize.account,
Account::default(),
"Only Uninitialized accounts can be initialized"
);
assert!(
account_to_initialize.is_authorized,
"Account to initialize must be authorized"
);
assert_eq!(
definition_account.account.program_owner, token_program_id,
"Token definition must be owned by token program"
);
let definition = TokenDefinition::try_from(&definition_account.account.data)
.expect("Definition account must be valid");
let holding =
TokenHolding::zeroized_from_definition(definition_account.account_id, &definition);
let definition_post = definition_account.account;
let mut account_to_initialize = account_to_initialize.account;
account_to_initialize.data = Data::from(&holding);
vec![
AccountPostState::new(definition_post),
AccountPostState::new_claimed(account_to_initialize, Claim::Authorized),
]
}