Let's quickly get into why there isn't necessarily a msg.sender
like you're used to on Ethereum.
On Solana, you cannot guarantee that the person who paid the fee on the transaction is the traditional msg.sender
you're used to on Ethereum. This is because anyone can pay the fee on a transaction. The only additional work is a required signature from the fee payer.
If you want to do something like msg.sender
on Solana, you can check if the signer is equal to a specific address you're expecting. For example, you can do the following:
use anchor_lang::prelude::*;declare_id!("Hf96fZsgq9R6Y1AHfyGbhi9EAmaQw2oks8NqakS6XVt1");#[program]pub mod signer_check { use super::*; pub fn initialize(ctx: Context<Initialize>) -> Result<()> { let the_signer1: &mut Signer = &mut ctx.accounts.signer1; // Function logic.... msg!("The signer1: {:?}", *the_signer1.key); Ok(()) }}#[derive(Accounts)]pub struct Initialize<'info> { #[account(mut)] pub signer1: Signer<'info>,}
With the above code you could potentially check that the_signer1
is equal to a publicKey you're expecting. With this you should be able to get started with all of your requirements.
Resources: