🎉 [Gate 30 Million Milestone] Share Your Gate Moment & Win Exclusive Gifts!
Gate has surpassed 30M users worldwide — not just a number, but a journey we've built together.
Remember the thrill of opening your first account, or the Gate merch that’s been part of your daily life?
📸 Join the #MyGateMoment# campaign!
Share your story on Gate Square, and embrace the next 30 million together!
✅ How to Participate:
1️⃣ Post a photo or video with Gate elements
2️⃣ Add #MyGateMoment# and share your story, wishes, or thoughts
3️⃣ Share your post on Twitter (X) — top 10 views will get extra rewards!
👉
Rust smart contracts permission control: function visibility and privilege access management
Rust Smart Contracts Development Diary (7) Contract Security - Access Control
This article will introduce the relevant content of permission control in Rust smart contracts from two perspectives:
1. Contract Function (Method) Visibility
When writing smart contracts, the visibility of the contract functions can be specified to control the permissions for calling those functions. This is crucial for protecting key parts of the contract from being accessed or manipulated accidentally.
Taking the Bancor Network exchange as an example, a security incident occurred on June 18, 2020, due to an incorrect access control setting for a key function in the contract. The contract was written in Solidity, and function visibility is divided into public/external and private/internal.
Bancor mistakenly set some key transfer functions to public attributes while fixing a security vulnerability, allowing anyone to call these functions from outside the contract for transfer operations, putting users' assets worth $590,000 at serious risk.
In Rust smart contracts, function visibility control is equally important. The contract functions marked with the #[near_bindgen] macro defined by the NEAR SDK have the following visibility attributes:
Another way to set the method as internal is to define it in the impl Contract code block that is not decorated with #[near_bindgen].
For callback functions, they must be set as public properties to be called via function call. At the same time, it is necessary to ensure that the callback functions can only be called by the contract itself, which can be implemented using the #[private] macro.
2. Access Control of Privileged Functions(Whitelist Mechanism)
In addition to function visibility, a complete access control whitelist mechanism needs to be established from a semantic level. Certain privileged functions (such as contract initialization, enabling/disabling, unified transfers, etc.) can only be called by the contract owner (owner).
You can implement custom traits to control access to privileged functions, checking whether the transaction caller is the contract owner:
rust pub trait Ownable { fn assert_owner(&self) { assert_eq!(env::predecessor_account_id(), self.get_owner()); } AccountId; fn set_owner(&mut self, owner: AccountId); }
Based on this principle, more complex traits can be customized to set multiple users or multiple whitelists in the whitelist, achieving fine-grained access control.
3. More Access Control Methods
Other access control methods in Rust smart contracts include:
These contents will be detailed in the subsequent series of smart contracts cultivation diaries.