Solana 中的Tx.origin、msg.sender 和 onlyOwner

  • 0xE
  • 发布于 2天前
  • 阅读 338

在 Solidity 中,msg.sender 表示调用智能合约函数的地址,tx.origin 表示签署交易的钱包地址。本文将探讨 Solana 中类似的调用者识别机制及 onlyOwner 模式的实现。

在 Solidity 中,msg.sender 表示调用智能合约函数的地址,tx.origin 表示签署交易的钱包地址。本文将探讨 Solana 中类似的调用者识别机制及 onlyOwner 模式的实现。


Solana 中的调用者识别

Solana 无直接等价于 msg.sender 的概念,但可通过 Signer 获取类似 tx.origin 的交易发起者地址。需要注意的是,Solana 交易支持多个签署者,可视为“多个交易发起者”。

以下示例展示如何获取签署者地址:

use anchor_lang::prelude::*;

declare_id!("3FFyn7ysmuz4WThFWV4cSiNy67aW8fFsCWu9wYC1Nkob");

#[program]
pub mod sender {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        let the_signer1: &mut Signer = &mut ctx.accounts.signer1;

        msg!("The signer1: {:?}", *the_signer1.key);

        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(mut)]
    pub signer1: Signer<'info>,
}

说明

  • Signer<'info> 确保 signer1 已签署交易。
  • the_signer1.key 获取签署者的公钥。

测试代码:

import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { Sender } from "../target/types/sender";

describe("sender", () => {
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.Sender as Program&lt;Sender>;

  it("Is initialized!", async () => {
    const tx = await program.methods.initialize().accounts({
      signer1: program.provider.publicKey
    }).rpc();

    console.log("The signer1: ", program.provider.publicKey.toBase58());
  });
});

多个签署者

Solana 支持多签交易,可通过添加多个 Signer 实现:

use anchor_lang::prelude::*;

declare_id!("3FFyn7ysmuz4WThFWV4cSiNy67aW8fFsCWu9wYC1Nkob");

#[program]
pub mod sender {
    use super::*;

    pub fn initialize(ctx: Context&lt;Initialize>) -> Result&lt;()> {
        let the_signer1: &mut Signer = &mut ctx.accounts.signer1;
        let the_signer2: &mut Signer = &mut ctx.accounts.signer2;

        msg!("The signer1: {:?}", *the_signer1.key);
        msg!("The signer2: {:?}", *the_signer2.key);

        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize&lt;'info> {
    pub signer1: Signer&lt;'info>,
    pub signer2: Signer&lt;'info>,
}

测试代码:

describe("sender", () => {
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.Sender as Program&lt;Sender>;

  let myKeypair = anchor.web3.Keypair.generate();

  it("Is signed by multiple signers", async () => {
    const tx = await program.methods
      .initialize()
      .accounts({
        signer1: program.provider.publicKey,
        signer2: myKeypair.publicKey,
      })
      .signers([myKeypair])
      .rpc();

    console.log("The signer1: ", program.provider.publicKey.toBase58());
    console.log("The signer2: ", myKeypair.publicKey.toBase58());
  });
});

说明

  • signers() 方法接受一个签署者数组作为参数。尽管我们仅在数组中提供了一个签署者,而非两个,但 Anchor 会自动将提供程序中的钱包账户作为额外的签署者,因此无需手动将其加入数组。
  • myKeypair 是随机生成的,每次运行测试时 signer2 的公钥都会变化。

onlyOwner

Solidity 的 onlyOwner 模式限制函数仅限合约所有者调用。Solana 使用 Anchor 的 #[access_control] 属性实现类似功能:

use anchor_lang::prelude::*;

declare_id!("3FFyn7ysmuz4WThFWV4cSiNy67aW8fFsCWu9wYC1Nkob");

const OWNER: &str = "5NhLjdFKocoRMqic9sqAe5TxLagJCoCBunzg51ioMYot";

#[program]
pub mod sender {
    use super::*;

    #[access_control(check(&ctx))]
    pub fn initialize(ctx: Context&lt;OnlyOwner>) -> Result&lt;()> {

        msg!("Holla, I'm the owner.");
        Ok(())
    }
}

fn check(ctx: &Context&lt;OnlyOwner>) -> Result&lt;()> {
    require_keys_eq!(
        ctx.accounts.signer_account.key(),
        OWNER.parse::&lt;Pubkey>().unwrap(),
        OnlyOwnerError::NotOwner
    );

    Ok(())
}

#[derive(Accounts)]
pub struct OnlyOwner&lt;'info> {
    signer_account: Signer&lt;'info>,
}

#[error_code]
pub enum OnlyOwnerError {
    #[msg("Only owner can call this function!")]
    NotOwner,
}

说明

  • OWNER 需替换为实际钱包公钥(运行 solana address 获取)。
  • [access_control] 在执行函数前调用 check,验证签署者是否为所有者。

测试:所有者调用

describe("sender", () => {
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.Sender as Program&lt;Sender>;

  it("Is called by the owner", async () => {
    const tx = await program.methods
      .initialize()
      .accounts({
        signerAccount: program.provider.publicKey,
      })
      .rpc();

    console.log("Transaction hash:", tx);
  });
});

运行 anchor test --skip-local-validator,测试通过。

测试:非所有者调用

describe("sender", () => {
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.Sender as Program&lt;Sender>;

  let Keypair = anchor.web3.Keypair.generate();

  it("Is NOT called by the owner", async () => {
    const tx = await program.methods
      .initialize()
      .accounts({
        signerAccount: Keypair.publicKey,
      })
      .signers([Keypair])
      .rpc();

    console.log("Transaction hash:", tx);
  });
});

运行测试,报错:

  1) sender
       Is NOT called by the owner:
     Error: AnchorError thrown in programs/sender/src/lib.rs:23. Error Code: NotOwner. Error Number: 6000. Error Message: Only owner can call this function!.
Program log: Left:
Program log: 587Q2QL6h1cMpFYQ45p6jx8a66ASEAhvnJZGcG8a86KR
Program log: Right:
Program log: 5NhLjdFKocoRMqic9sqAe5TxLagJCoCBunzg51ioMYot

修改所有者

当前示例中,OWNER 是硬编码的常量。要动态修改所有者,需将公钥存储在链上(存储将在后续教程中讨论)。目前,可通过重新部署程序字节码更新所有者。


【笔记配套代码】 https://github.com/0xE1337/rareskills_evm_to_solana 【参考资料】 https://learnblockchain.cn/column/119 https://www.rareskills.io/solana-tutorial

点赞 0
收藏 1
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
0xE
0xE
0x59f6...a17e
17年进入币圈,Web3 开发者。刨根问底探链上真相,品味坎坷悟 Web3 人生。