【问题标题】:Use PDA account as shared Storage solana使用 PDA 帐户作为共享 Storage solana
【发布时间】:2022-07-13 12:03:16
【问题描述】:

我的意图是拥有可以存储程序使用的数据的帐户。我将在每次调用我的链上程序时传递这个帐户。如果我尝试从 onchain 调用 allocate,则结果为 <pda-address>'s writable permission escalated

似乎拥有 PDA 是个好主意,因为只有我的程序才能使用它。但我不确定我该怎么做。这是我迄今为止尝试过的

  • 在前端派生一个 PDA
  • 作为其他普通账户传递
  • 程序将收到的第一个 AccountInfo 视为 PDA 并使用它来存储数据

上述方法出错:

  • 我需要分配空间来在 PDA 中存储数据。但由于 PDA 没有 Secret Key 并且因此没有 KeyPair,我无法调用 allocateSpace 之类的调用。

我在这里缺少什么?我是 solana 的新手,所以我什至可能错过了最简单的事情。

客户端

const deposit_instruction = new solana.TransactionInstruction({
        keys: [
            { pubkey: system_account, isSigner: false, isWriteable: false },
            { pubkey: payer.publicKey, isSigner: true, isWritable: true },
            { pubkey: pdaAccount, isSigner: false, isWriteable: false },
            { pubkey: sender.publicKey, isSigner: true, isWriteable: true },
            { pubkey: receiver, isSigner: false, isWriteable: false },
        ],
        programId: programId,
        data: argument_ser,
    });

    return await callEntryPoint(deposit_instruction, [sender, payer]);

链上计划

 let instruction = system_instruction::allocate(pda_account.key, 100u64);

        invoke_signed(
            &instruction,
            &[pda_account.clone(), system_account.clone()],
            &[&[SEED], &[&[bump]]],
        )?;

【问题讨论】:

    标签: javascript rust blockchain solana solana-web3js


    【解决方案1】:

    使用PublicKey.findProgramAddress 获取程序密钥和凹凸值。将其发送到程序中的指令。 然后在您的程序中调用system_instruction::create_account 来创建带有存储(最多10k)的PDA。

    【讨论】:

    • 感谢弗兰克的帮助。问题寿。我不能来自 js 的 create_account PDA 吗?还有为什么我需要传递凹凸值?
    • 如果您在您的程序中创建帐户,则需要凹凸值。
    • 哦,谢谢。但是,当我调用 system_instruction::allocate 并在种子中传递 SEED & Bump 值并在 account_infos 中传递 pda_accountsystem_account 时,它会显示:<pda_address>'s permission escalated。我可能会错过什么?
    【解决方案2】:
    1. 确保您从您的链上程序中派生了该 PDA。

    2. 在链上程序中使用invoked_signed 而不是invoked 到system_program。

    既然那是从你的程序派生的PDA,那么只有你的程序可以sign那个PDA。

    【讨论】:

    • 关于您的第 1 点,我不能在客户端使用链上程序的公钥导出 PDA
    • 是的,在客户端你可以使用findProgramAddress你从任何链上程序派生任何PDA。这个链上程序将是唯一可以sign PDA 的程序。
    • 我的理解也一样。但是当我调用 system_instruction::allocate 并在种子中传递 SEED & Bump 值并在 account_infos 中传递 pda_accountsystem_account 时,它会显示:<pda_address>'s permission escalated。我可能会错过什么?
    • 你是链上程序的system_instruction::allocate吗?
    • 是的。我已经放置了示例代码块以便更好地解释。我做错了什么?
    【解决方案3】:

    回到这个问题(可能对未来的读者有帮助),

    我构建流程的方式没有任何问题。问题是我拼错了isWriteable 它应该是isWritable

    我花了一整天的时间才弄明白

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-31
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      相关资源
      最近更新 更多