在 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.provider.publicKey.toBase58());
console.log("The signer2: ", myKeypair.publicKey.toBase58());
});
});
说明:
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<OnlyOwner>) -> Result<()> {
msg!("Holla, I'm the owner.");
Ok(())
}
}
fn check(ctx: &Context<OnlyOwner>) -> Result<()> {
require_keys_eq!(
ctx.accounts.signer_account.key(),
OWNER.parse::<Pubkey>().unwrap(),
OnlyOwnerError::NotOwner
);
Ok(())
}
#[derive(Accounts)]
pub struct OnlyOwner<'info> {
signer_account: Signer<'info>,
}
#[error_code]
pub enum OnlyOwnerError {
#[msg("Only owner can call this function!")]
NotOwner,
}
说明:
describe("sender", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace.Sender as Program<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<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
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!