Is it possible to set the 8-byte discriminator on an Anchor account manually?
I'm trying to initialize a new pda account in an instruction, but not all of the seeds are available in the context (can't use instruction(...)
macro to get seed either). I still want to initialize the account as an Anchor #[account]
though, so that I can access it in subsequent instructions.
Basic program setup:
#[account]
pub struct MyAccount {...}
#[derive(Accounts)]
pub struct InitIx<'info> {
...
#[account(mut)]
pub my_account: UncheckedAccount<'info>,
...
}
#[derive(Accounts)]
pub struct InitIxWrapper<'info> {
...
init_ix: InitIx<'info>,
some_pubkey: UncheckedAccount<'info>,
...
}
#[derive(Accounts)]
pub struct OtherIx<'info> {
...
my_account: Account<'info, MyAccount>,
...
}
// The global instruction
pub fn init_ix_wrapper(ctx: Context<InitIxWrapper>) -> Result<()> {
...
// local ix for processing
local::inix_ix(
Context::new(
...
ctx.accounts.init_ix,
...
),
ctx.accounts.some_pubkey.key
)?;
...
}
pub fn other_ix(ctx: Context<OtherIx>) -> Result<()> {
// access data from my_account like usual
}
I would like the local ix to do something like the following:
pub fn init_ix(ctx: Context<InitIx>, my_account_seed: &Pubkey) -> Result<()> {
// assert derivation of my_account key using my_account_seed
// manually initialize account (solana create_account ix)
// manually set discriminator for MyAccount and write to my_account.data
// write relevant remaining data to my_account.data
}
If manually setting discriminator is not possible, then can someone please direct me to a different way to accomplish what I want given my program structure?