mirror of
https://github.com/logos-blockchain/lez-programs.git
synced 2026-05-19 23:49:52 +00:00
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
40 lines
1.3 KiB
Rust
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),
|
|
]
|
|
}
|