Closed jjordan-quantum closed 8 months ago
Its likely a (de)serialization issue. If the block_id_string
is going to be of constant length, it would be better to be [u8; LEN]
instead of String
.
I see this example:
seeds = [b"my-seed", domain.as_bytes(), foo.key.as_ref(), &seed, &[bump]],
where domain
is a String
otherwise, if my seed is u32, instead of a String, is there a convenient way to use that in a seed? that would also work for me
this test program has examples for using tons of different rust types as seeds you can look at.
this works:
#[instruction(seed: Vec<u8>)]
...
#[account(
init,
payer = user,
space = 8 + 4 + 4 + 200 + 1,
seeds = [user.key.as_ref(), &seed],
bump
)]
although, it requires me to convert the seed (which is just an uint32) into a buffer to get the PDA:
const seed = Buffer.from([0, 0, 34, 30, 62]);
const [blockPda, _] = await PublicKey.findProgramAddress(
[
anchor.getProvider().wallet.publicKey.toBuffer(),
seed,
],
program.programId
);
this test program has examples for using tons of different rust types as seeds you can look at.
great I will take a look. thanks!
Hi Friends. I am trying to use PDA seeds to create an account. Somehow I have tried everything still I am getting error msg: 'The program could not deserialize the given instruction'. Not sure what wrong I am doing. Please advise. Following is my code.
pub mod referal {
use super::*;
pub fn initialize(
ctx: Context<Initialize>,
bump: u8,
refree: Pubkey,
seed: String,
) -> ProgramResult {
msg!("Seed: {}", seed);
let base_account: &mut Account<BaseAccount> = &mut ctx.accounts.base_account;
base_account.bump = bump;
base_account.refree = refree;
return Ok(());
}
}
fn seed_bytes(seed: &str) -> &[u8] {
msg!("Seed: {}", seed);
seed.as_bytes()
}
#[derive(Accounts)]
#[instruction(bump:u8, seed: String)]
pub struct Initialize<'info> {
#[account(
seeds = [seed_bytes(&seed).as_ref()],
bump, init, payer = creator, space = 41)]
pub base_account: Account<'info, BaseAccount>,
#[account(mut)]
pub creator: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
#[derive(Default)]
pub struct BaseAccount {
pub refree: Pubkey,
pub bump: u8,
} ```
this test program has examples for using tons of different rust types as seeds you can look at.
Hey, I used this code and it works for me, but if I wann derive another address from the same "base" account with a different seed i get the following CPI error: Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account
Is it even possible to derive more addresses for one public key just by changing the seed?
When I try to init a pda using the following, all is well:
but if I try to use an instruction argument, which is a string that matches the bytes string in the previous example, I get the follwoing:
Program failed to complete: Could not create program address with signer seeds: Length of the seed is too long for address generation
snippet: (note that the block_id_string passed in the instruction is "123456"
Is there another way that I need to format / convert the string from the arguments so that it will work as a seed?