How do I access the message sender in my contract function?

To access the message sender, you need to import the msg_sender function from the std library . Then you can use msg_sender() to get a Result with an Identity .

use std::auth::{AuthError, msg_sender};
let sender: Result<Identity, AuthError> = msg_sender();
  • If the caller is a contract, then Result::Ok(Sender) is returned with the ContractId sender variant.
  • If the caller is external (i.e. from a script), then all coin input owners in the transaction are checked. If all owners are the same, then Result::Ok(Sender) is returned with the Address sender variant.
  • If the caller is external and coin input owners are different, then the caller cannot be determined and a Result::Err(AuthError) is returned.
3 Likes