🎉 Gate Square Growth Points Summer Lucky Draw Round 1️⃣ 2️⃣ Is Live!
🎁 Prize pool over $10,000! Win Huawei Mate Tri-fold Phone, F1 Red Bull Racing Car Model, exclusive Gate merch, popular tokens & more!
Try your luck now 👉 https://www.gate.com/activities/pointprize?now_period=12
How to earn Growth Points fast?
1️⃣ Go to [Square], tap the icon next to your avatar to enter [Community Center]
2️⃣ Complete daily tasks like posting, commenting, liking, and chatting to earn points
100% chance to win — prizes guaranteed! Come and draw now!
Event ends: August 9, 16:00 UTC
More details: https://www
A Detailed Explanation of Function Visibility and Access Control in Rust Smart Contracts
Rust Smart Contracts Development Diary (7) Contract Security and Calculation Precision
This article will introduce permission control in Rust smart contracts from two perspectives:
1. Contract Function Visibility
The visibility control of contract functions is crucial for protecting key parts from being misoperated. Taking the security incident of Bancor Network exchange on June 18, 2020, as an example, the misconfiguration of a key transfer function as public exposed users' assets worth $590,000 to risk.
In Rust smart contracts, function visibility mainly includes:
Another way to set the internal method is to define a separate impl Contract code block without the #[near_bindgen] modifier.
For the callback function, it must be set to public but restricted to be called only by the contract itself. This can be implemented using the #[private] macro.
By default, everything in Rust is private, except for items in pub trait and pub enum.
2. Access Control of Privileged Functions
In addition to function visibility, a complete access control whitelist mechanism needs to be established from a semantic level. Similar to Solidity's Ownable contract, certain privileged functions can only be called by the owner.
In Rust smart contracts, custom Traits can be implemented:
rust pub trait Ownable { fn assert_owner(\u0026self) { assert_eq!(env::predecessor_account_id(), self.get_owner()); } fn get_owner(\u0026self) -\u003e AccountId; fn set_owner(&mut self, owner: AccountId); }
This enables access control for privileged functions. Based on this, it is also possible to set up multi-user whitelists or multiple whitelist groups.
3. More Access Control Methods
Other access control methods include:
These contents will be detailed in subsequent articles.