Back to Sway

Message Sender

docs/reference/src/documentation/operations/call-data/msg-sender.md

0.71.0903 B
Original Source

Message Sender

The standard prelude imports a function msg_sender() automatically, which retrieves the Identity of the caller.

The identity can be used for a variety of reasons however a common application is access control i.e. restricting functionality for non-privileged users (non-admins).

Example

We can implement access control by specifying that only the owner can call a function.

In the following snippet we accomplish this by comparing the caller msg_sender() to the OWNER. If a regular user calls the function then it will revert otherwise it will continue to run when called by the OWNER.

sway
{{#include ../../../code/operations/call_data/src/lib.sw:access_control}}