在 Solidity 中,msg.sender 表示调用智能合约函数的地址,tx.origin 表示签署交易的钱包地址。本文将探讨 Solana 中类似的调用者识别机制及 onlyOwner 模式的实现。
在 Solidity 中,msg.sender 表示调用智能合约函数的地址,tx.origin 表示签署交易的钱包地址。本文将探讨 Solana 中类似的调用者识别机制及 onlyOwner 模式的实现。
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>,
}
说明:
测试代码:
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<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<Initialize>) -> Result<()> {
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<'info> {
pub signer1: Signer<'info>,
pub signer2: Signer<'info>,
}
测试代码:
describe("sender", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.Sender as Program<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.pr...
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!