简体   繁体   English

使用相同的 pubkey 和不同的 uuid 派生另一个地址

[英]Derive another address with the same pubkey and different uuid

I want to program a smart contract that stores data of a random generated "planet".我想编写一个存储随机生成的“行星”数据的智能合约。 I generated the data for the planet in another program and i use a uuid to recreate the planet.我在另一个程序中为行星生成了数据,并使用 uuid 重新创建了行星。 Now i want to use this uuid and a public key to create the address.现在我想使用这个 uuid 和一个公钥来创建地址。 I tried to use PDA's for that and it kind of works.我尝试为此使用 PDA,它确实有效。 The first address i derive with the pubkey and the uuid works just fine, but if i want to derive another address with the same pubkey and different uuid I get a CPI Error.我用 pubkey 和 uuid 派生的第一个地址工作得很好,但是如果我想用相同的 pubkey 和不同的 uuid 派生另一个地址,我会得到一个 CPI 错误。

Had anybody common experiences or is the way i use PDA's wrong and if so how would you store the data?有没有人有共同的经验,或者我使用 PDA 的方式不对,如果有,你将如何存储数据?

My code:我的代码:

pub fn create_planet(ctx: Context<CreatePlanet>, owner: Pubkey, authority: Pubkey, bump: u8, uuid: u64) -> Result<()>{
//pub fn create_planet(ctx: Context<CreatePlanet>, owner: Pubkey, authority: Pubkey, bump: u8) -> Result<()>{
    msg!("test");
    let planet: &mut Account<Planet> = &mut ctx.accounts.planet;
    let payer: &Signer = &ctx.accounts.payer;
    /*if data.chars().count() > 212 {
        return Err(ErrorCode::dataTooLong.into());
    }*/
    if planet.owner == anchor_lang::prelude::Pubkey::default() {
        planet.owner = owner;
    }
    planet.authority = authority;
    planet.bump = bump;
    planet.uuid = uuid;
    Ok(())
}

Struct:结构:

#[derive(Accounts)]
#[instruction(bump: u8, uuid: u64)]
//#[instruction(bump: u8)]
pub struct CreatePlanet<'info>{
   #[account(init, seeds = [b"planet".as_ref(), 
   payer.key.as_ref(), uuid.to_le_bytes().as_ref()], bump, 
   payer=payer, space=Planet::LEN)]
   //#[account(init, seeds = [payer.key.as_ref(), 
   uuid.to_le_bytes().as_ref()], bump, payer=payer, 
   space=Planet::LEN)]
   pub planet: Account<'info, Planet>,
   #[account(mut)]
   pub payer: Signer<'info>,
   pub system_program: Program <'info, System>
}

RPC Call: RPC 调用:

const uuid = new anchor.BN(parseInt((Date.now() / 1000).toString()));
const uuidBuffer = uuid.toBuffer('le', 8);

const [sandboxPda, sandboxBump] = 
await PublicKey.findProgramAddress([Buffer.from('planet'), wallet.publicKey.toBuffer(), uuidBuffer], program.programId);
//await PublicKey.findProgramAddress([wallet.publicKey.toBuffer(), uuid], program.programId);


console.log("PDA: ", sandboxPda.toBase58());
console.log("Bump: ", sandboxBump);

await program.rpc.createPlanet(wallet.publicKey, ourWallet.publicKey, sandboxBump, uuid, {
  accounts: {
    planet: sandboxPda, //planet.publicKey
    payer: wallet.publicKey,
    systemProgram: anchor.web3.SystemProgram.programId
  },
  signers: [wallet]
});

It causes to the following error:它导致以下错误:

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account
      at Connection.sendEncodedTransaction (node_modules/@solana/web3.js/src/connection.ts:4068:13)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Connection.sendRawTransaction (node_modules/@solana/web3.js/src/connection.ts:4030:20)
      at sendAndConfirmRawTransaction (node_modules/@project-serum/anchor/src/provider.ts:284:21)
      at AnchorProvider.sendAndConfirm (node_modules/@project-serum/anchor/src/provider.ts:144:14)
      at Object.rpc [as createPlanet] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:29:16)

你能上传你调用CPI​​的代码吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM