發布者權限 (Publisher Authority)
應用程式通常需要證明 誰發布了某個型別。這在數位資產的情境中尤其重要,因為發布者可能會為其資產啟用或停用特定功能。Sui Framework 中定義的 Publisher 物件,可讓發布者證明其 對某個型別的權限。
定義 (Definition)
Publisher 物件定義於 Sui Framework 的 sui::package 模組中。它是一個非常簡單的非泛型物件,每個模組可初始化一次(每個套件則可初始化多次),用於證明發布者對某個型別的權限。若要領取 Publisher 物件,發布者必須向 package::claim 函式提供一次性見證。
module sui::package;
public struct Publisher has key, store {
id: UID,
package: String,
module_name: String,
}
以下是在模組中領取 Publisher 物件的簡單範例:
module book::publisher;
use sui::package::{Self, Publisher};
/// Some type defined in the module.
public struct Book {}
/// The OTW for the module.
public struct PUBLISHER has drop {}
/// Uses the One Time Witness to claim the Publisher object.
fun init(otw: PUBLISHER, ctx: &mut TxContext) {
// Claim the Publisher object.
let publisher: Publisher = sui::package::claim(otw, ctx);
// Usually it is transferred to the sender.
// It can also be stored in another object.
transfer::public_transfer(publisher, ctx.sender())
}
對於常見的領取並轉移流程,sui::package 模組也提供了簡寫形式: package::claim_and_keep,可在一次呼叫中領取 Publisher 物件並將其轉移給傳送者。
用法 (Usage)
Publisher 物件具有兩個相關函式:from_module 與 from_package。它們會檢查某個型別是否定義於此 Publisher 所代表的模組或套件中:
// Checks if the type is from the same module, hence the `Publisher` has the
// authority over it.
assert!(publisher.from_module<Book>());
// Checks if the type is from the same package, hence the `Publisher` has the
// authority over it.
assert!(publisher.from_package<Book>());
發布者作為管理員角色 (Publisher as Admin Role)
對於小型應用程式或簡單的使用情境,Publisher 物件可作為管理員權能使用。雖然在更廣泛的情境中,Publisher 物件可控制系統設定,它也可用於管理應用程式的狀態。
/// Some action in the application gated by the Publisher object.
public fun admin_action(cap: &Publisher, /* app objects... */ param: u64) {
assert!(cap.from_module<Book>(), ENotAuthorized);
// perform application-specific action
}
不過,Publisher 物件缺乏權能的一些原生特性,例如型別安全性與表達能力。admin_action 的簽章未說明所需的權限;任何持有 任何 Publisher 物件的人都可以呼叫此函式,因此必須在函式主體內檢查授權。而且,由於每個已發布的套件都會產生一個 Publisher,若忘記進行 from_module 檢查,網路上的每位發布者都能執行該動作。因此,將 Publisher 物件作為管理員角色時,務必謹慎。
Sui 上的角色 (Role on Sui)
Sui 的特定功能需要 Publisher。物件顯示在定義型別的模組外設定時,可使用 Publisher 建立;而 Kiosk 系統的重要元件 TransferPolicy 也需要 Publisher 物件來證明該型別的所有權。
後續步驟 (Next Steps)
下一節將介紹第一項可使用 Publisher 物件的功能:物件顯示。它提供一種向用戶端描述物件並標準化中繼資料的方法,是打造使用者友善應用程式不可或缺的功能。