Skip to main content

可转让见证模式

可转让见证模式是基于能力和见证模式的结合。由于见证模式需要谨慎使用,你应该只允许经授权的用户生成它(最好只允许一次)。然而,某些场景需要模块X的类型授权可在另一个模块Y中使用。或者,可能存在需要在一段时间后执行授权的情况。

对于这些相当罕见的场景,可存储见证是一个不错的解决方案。

module examples::transferable_witness {
use sui::transfer;
use sui::object::{Self, UID};
use sui::tx_context::{Self, TxContext};


/// Witness now has a `store` that allows us to store it inside a wrapper.
struct WITNESS has store, drop {}


/// Carries the witness type. Can be used only once to get a Witness.
struct WitnessCarrier has key { id: UID, witness: WITNESS }


/// Send a `WitnessCarrier` to the module publisher.
fun init(ctx: &mut TxContext) {
transfer::transfer(
WitnessCarrier { id: object::new(ctx), witness: WITNESS {} },
tx_context::sender(ctx)
)
}


/// Unwrap a carrier and get the inner WITNESS type.
public fun get_witness(carrier: WitnessCarrier): WITNESS {
let WitnessCarrier { id, witness } = carrier;
object::delete(id);
witness
}
}