diff --git a/crates/iota-framework/docs/timelock/label.md b/crates/iota-framework/docs/timelock/label.md deleted file mode 100644 index 34912defefa..00000000000 --- a/crates/iota-framework/docs/timelock/label.md +++ /dev/null @@ -1,387 +0,0 @@ ---- -title: Module `0x10cf::label` ---- - - - -- [Resource `LabelerCap`](#0x10cf_label_LabelerCap) -- [Struct `Label`](#0x10cf_label_Label) -- [Constants](#@Constants_0) -- [Function `create_labeler_cap`](#0x10cf_label_create_labeler_cap) -- [Function `destroy_labeler_cap`](#0x10cf_label_destroy_labeler_cap) -- [Function `is_type`](#0x10cf_label_is_type) -- [Function `value`](#0x10cf_label_value) -- [Function `create`](#0x10cf_label_create) -- [Function `destroy`](#0x10cf_label_destroy) -- [Function `destroy_opt`](#0x10cf_label_destroy_opt) -- [Function `type_name`](#0x10cf_label_type_name) -- [Function `clone`](#0x10cf_label_clone) -- [Function `clone_opt`](#0x10cf_label_clone_opt) - - -
use 0x1::ascii;
-use 0x1::option;
-use 0x1::string;
-use 0x1::type_name;
-use 0x2::object;
-use 0x2::tx_context;
-use 0x2::types;
-
- - - - - -## Resource `LabelerCap` - -LabelerCap allows to create labels of the specific type L. -Can be publicly transferred like any other object. - - -
struct LabelerCap<L> has store, key
-
- - - -
-Fields - - -
-
-id: object::UID -
-
- -
-
- - -
- - - -## Struct `Label` - -Label is an immutable label representation. -The only way to create instances is through the LabelerCap<L>. -Upon creation, value field becomes the fully qualified type name of L. - - -
struct Label has store
-
- - - -
-Fields - - -
-
-value: string::String -
-
- A fully qualified type name with the original package IDs. -
-
- - -
- - - -## Constants - - - - -Error code for when a type passed to the create_labeler_cap function is not a one-time witness. - - -
const ENotOneTimeWitness: u64 = 0;
-
- - - - - -## Function `create_labeler_cap` - -Create a LabelerCap instance. -Can be created only by consuming a one time witness. - - -
public fun create_labeler_cap<L: drop>(witness: L, ctx: &mut tx_context::TxContext): label::LabelerCap<L>
-
- - - -
-Implementation - - -
public fun create_labeler_cap<L: drop>(witness: L, ctx: &mut TxContext): LabelerCap<L> {
-    assert!(types::is_one_time_witness(&witness), ENotOneTimeWitness);
-
-    LabelerCap<L> {
-        id: object::new(ctx),
-    }
-}
-
- - - -
- - - -## Function `destroy_labeler_cap` - -Delete a LabelerCap instance. -If a capability is destroyed, it is impossible to add the related labels. - - -
public fun destroy_labeler_cap<L>(cap: label::LabelerCap<L>)
-
- - - -
-Implementation - - -
public fun destroy_labeler_cap<L>(cap: LabelerCap<L>) {
-    let LabelerCap<L> {
-        id,
-    } = cap;
-
-    object::delete(id);
-}
-
- - - -
- - - -## Function `is_type` - -Check if a Label represents the type L. - - -
public fun is_type<L>(self: &label::Label): bool
-
- - - -
-Implementation - - -
public fun is_type<L>(self: &Label): bool {
-    self.value == type_name<L>()
-}
-
- - - -
- - - -## Function `value` - -Function to get the value of a Label. - - -
public fun value(self: &label::Label): &string::String
-
- - - -
-Implementation - - -
public fun value(self: &Label): &String {
-    &self.value
-}
-
- - - -
- - - -## Function `create` - -Create a Label instance. -The created label holds a fully qualified type name with the original package IDs. -Can be called only by the related LabelerCap owner. - - -
public fun create<L>(_: &label::LabelerCap<L>): label::Label
-
- - - -
-Implementation - - -
public fun create<L>(_: &LabelerCap<L>): Label {
-    Label {
-        value: type_name<L>(),
-    }
-}
-
- - - -
- - - -## Function `destroy` - -Destroy a Label instance. - - -
public fun destroy(self: label::Label)
-
- - - -
-Implementation - - -
public fun destroy(self: Label) {
-    let Label {
-        value: _,
-    } = self;
-}
-
- - - -
- - - -## Function `destroy_opt` - -Destroy an optional Label instance. - - -
public fun destroy_opt(self: option::Option<label::Label>)
-
- - - -
-Implementation - - -
public fun destroy_opt(self: Option<Label>) {
-    if (self.is_some()) {
-        destroy(option::destroy_some(self));
-    }
-    else {
-        option::destroy_none(self);
-    };
-}
-
- - - -
- - - -## Function `type_name` - -Return a fully qualified type name with the original package IDs. - - -
fun type_name<L>(): string::String
-
- - - -
-Implementation - - -
fun type_name<L>(): String {
-    string::from_ascii(std::type_name::get_with_original_ids<L>().into_string())
-}
-
- - - -
- - - -## Function `clone` - -Clone a Label instance. -It is a protected utility function, it should be impossible to clone Label -because it leads to unauthorized labeled objects creation. - - -
public(friend) fun clone(self: &label::Label): label::Label
-
- - - -
-Implementation - - -
public(package) fun clone(self: &Label): Label {
-    Label {
-        value: self.value,
-    }
-}
-
- - - -
- - - -## Function `clone_opt` - -Clone an optional Label instance. -It is a protected utility function, it should be impossible to clone Label -because it leads to unauthorized labeled objects creation. - - -
public(friend) fun clone_opt(self: &option::Option<label::Label>): option::Option<label::Label>
-
- - - -
-Implementation - - -
public(package) fun clone_opt(self: &Option<Label>): Option<Label> {
-    if (self.is_some()) {
-        option::some(clone(self.borrow()))
-    }
-    else {
-        option::none()
-    }
-}
-
- - - -
diff --git a/crates/iota-framework/docs/timelock/labeler.md b/crates/iota-framework/docs/timelock/labeler.md new file mode 100644 index 00000000000..28e809f4389 --- /dev/null +++ b/crates/iota-framework/docs/timelock/labeler.md @@ -0,0 +1,152 @@ +--- +title: Module `0x10cf::labeler` +--- + + + +- [Resource `LabelerCap`](#0x10cf_labeler_LabelerCap) +- [Constants](#@Constants_0) +- [Function `create_labeler_cap`](#0x10cf_labeler_create_labeler_cap) +- [Function `destroy_labeler_cap`](#0x10cf_labeler_destroy_labeler_cap) +- [Function `type_name`](#0x10cf_labeler_type_name) + + +
use 0x1::ascii;
+use 0x1::string;
+use 0x1::type_name;
+use 0x2::object;
+use 0x2::tx_context;
+use 0x2::types;
+
+ + + + + +## Resource `LabelerCap` + +LabelerCap allows to create labels of the specific type L. +Can be publicly transferred like any other object. + + +
struct LabelerCap<L> has store, key
+
+ + + +
+Fields + + +
+
+id: object::UID +
+
+ +
+
+ + +
+ + + +## Constants + + + + +Error code for when a type passed to the create_labeler_cap function is not a one-time witness. + + +
const ENotOneTimeWitness: u64 = 0;
+
+ + + + + +## Function `create_labeler_cap` + +Create a LabelerCap instance. +Can be created only by consuming a one time witness. + + +
public fun create_labeler_cap<L: drop>(witness: L, ctx: &mut tx_context::TxContext): labeler::LabelerCap<L>
+
+ + + +
+Implementation + + +
public fun create_labeler_cap<L: drop>(witness: L, ctx: &mut TxContext): LabelerCap<L> {
+    assert!(types::is_one_time_witness(&witness), ENotOneTimeWitness);
+
+    LabelerCap<L> {
+        id: object::new(ctx),
+    }
+}
+
+ + + +
+ + + +## Function `destroy_labeler_cap` + +Delete a LabelerCap instance. +If a capability is destroyed, it is impossible to add the related labels. + + +
public fun destroy_labeler_cap<L>(cap: labeler::LabelerCap<L>)
+
+ + + +
+Implementation + + +
public fun destroy_labeler_cap<L>(cap: LabelerCap<L>) {
+    let LabelerCap<L> {
+        id,
+    } = cap;
+
+    object::delete(id);
+}
+
+ + + +
+ + + +## Function `type_name` + +Return a fully qualified type name with the original package IDs +that is used as type related a label value. + + +
public(friend) fun type_name<L>(): string::String
+
+ + + +
+Implementation + + +
public(package) fun type_name<L>(): String {
+    string::from_ascii(std::type_name::get_with_original_ids<L>().into_string())
+}
+
+ + + +
diff --git a/crates/iota-framework/docs/timelock/timelock.md b/crates/iota-framework/docs/timelock/timelock.md index 11e650f43ec..d90c43e6692 100644 --- a/crates/iota-framework/docs/timelock/timelock.md +++ b/crates/iota-framework/docs/timelock/timelock.md @@ -16,14 +16,16 @@ A timelock implementation. - [Function `locked`](#0x10cf_timelock_locked) - [Function `locked_mut`](#0x10cf_timelock_locked_mut) - [Function `label`](#0x10cf_timelock_label) +- [Function `is_labeled_with`](#0x10cf_timelock_is_labeled_with) - [Function `pack`](#0x10cf_timelock_pack) - [Function `unpack`](#0x10cf_timelock_unpack) - [Function `transfer`](#0x10cf_timelock_transfer) - [Function `check_expiration_timestamp_ms`](#0x10cf_timelock_check_expiration_timestamp_ms) -
use 0x10cf::label;
+
use 0x10cf::labeler;
 use 0x1::option;
+use 0x1::string;
 use 0x2::object;
 use 0x2::transfer;
 use 0x2::tx_context;
@@ -67,7 +69,7 @@ A timelock implementation.
  This is the epoch time stamp of when the lock expires.
 
 
-label: option::Option<label::Label> +label: option::Option<string::String>
Timelock related label. @@ -138,7 +140,7 @@ Function to lock an object till a unix timestamp in milliseconds. Function to lock a labeled object till a unix timestamp in milliseconds. -
public fun lock_with_label<T: store, L>(cap: &label::LabelerCap<L>, locked: T, expiration_timestamp_ms: u64, ctx: &mut tx_context::TxContext): timelock::TimeLock<T>
+
public fun lock_with_label<T: store, L>(_: &labeler::LabelerCap<L>, locked: T, expiration_timestamp_ms: u64, ctx: &mut tx_context::TxContext): timelock::TimeLock<T>
 
@@ -148,7 +150,7 @@ Function to lock a labeled object till a unix timestamp in milliseconds.
public fun lock_with_label<T: store, L>(
-    cap: &LabelerCap<L>,
+    _: &LabelerCap<L>,
     locked: T,
     expiration_timestamp_ms: u64,
     ctx: &mut TxContext
@@ -156,11 +158,11 @@ Function to lock a labeled object till a unix timestamp in milliseconds.
     // Check that `expiration_timestamp_ms` is valid.
     check_expiration_timestamp_ms(expiration_timestamp_ms, ctx);
 
-    // Create a label instance.
-    let label = label::create(cap);
+    // Calculate a label value.
+    let label = labeler::type_name<L>();
 
     // Create a labeled timelock.
-    pack(locked, expiration_timestamp_ms, option::some(label), ctx)
+    pack(locked, expiration_timestamp_ms, option::some(label), ctx)
 }
 
@@ -186,14 +188,11 @@ Function to unlock the object from a unlock<T: store>(self: TimeLock<T>, ctx: &TxContext): T { // Unpack the timelock. - let (locked, expiration_timestamp_ms, label) = unpack(self); + let (locked, expiration_timestamp_ms, _) = unpack(self); // Check if the lock has expired. assert!(expiration_timestamp_ms <= ctx.epoch_timestamp_ms(), ENotExpiredYet); - // Destroy the label. - label::destroy_opt(label); - locked }
@@ -345,7 +344,32 @@ Must not be callable from the outside, as one could modify the locked object. Function to get the label of a TimeLock. -
public fun label<T: store>(self: &timelock::TimeLock<T>): &option::Option<label::Label>
+
public fun label<T: store>(self: &timelock::TimeLock<T>): option::Option<string::String>
+
+ + + +
+Implementation + + +
public fun label<T: store>(self: &TimeLock<T>): Option<String> {
+    self.label
+}
+
+ + + +
+ + + +## Function `is_labeled_with` + +Check if a TimeLock is labeled with the type L. + + +
public fun is_labeled_with<T: store, L>(self: &timelock::TimeLock<T>): bool
 
@@ -354,8 +378,13 @@ Function to get the label of a label<T: store>(self: &TimeLock<T>): &Option<Label> { - &self.label +
public fun is_labeled_with<T: store, L>(self: &TimeLock<T>): bool {
+    if (self.label.is_some()) {
+        self.label.borrow() == labeler::type_name<L>()
+    }
+    else {
+        false
+    }
 }
 
@@ -370,7 +399,7 @@ Function to get the label of a TimeLock. -
public(friend) fun pack<T: store>(locked: T, expiration_timestamp_ms: u64, label: option::Option<label::Label>, ctx: &mut tx_context::TxContext): timelock::TimeLock<T>
+
public(friend) fun pack<T: store>(locked: T, expiration_timestamp_ms: u64, label: option::Option<string::String>, ctx: &mut tx_context::TxContext): timelock::TimeLock<T>
 
@@ -382,7 +411,7 @@ A utility function to pack a pack<T: store>( locked: T, expiration_timestamp_ms: u64, - label: Option<Label>, + label: Option<String>, ctx: &mut TxContext): TimeLock<T> { // Create a timelock. @@ -390,7 +419,7 @@ A utility function to pack a object::new(ctx), locked, expiration_timestamp_ms, - label, + label, } }
@@ -406,7 +435,7 @@ A utility function to pack a TimeLock. -
public(friend) fun unpack<T: store>(lock: timelock::TimeLock<T>): (T, u64, option::Option<label::Label>)
+
public(friend) fun unpack<T: store>(lock: timelock::TimeLock<T>): (T, u64, option::Option<string::String>)
 
@@ -415,19 +444,19 @@ An utility function to unpack a unpack<T: store>(lock: TimeLock<T>): (T, u64, Option<Label>) { +
public(package) fun unpack<T: store>(lock: TimeLock<T>): (T, u64, Option<String>) {
     // Unpack the timelock.
     let TimeLock {
         id,
         locked,
         expiration_timestamp_ms,
-        label,
+        label,
     } = lock;
 
     // Delete the timelock.
     object::delete(id);
 
-    (locked, expiration_timestamp_ms, label)
+    (locked, expiration_timestamp_ms, label)
 }
 
diff --git a/crates/iota-framework/docs/timelock/timelocked_balance.md b/crates/iota-framework/docs/timelock/timelocked_balance.md index 396b2ff83b4..4c709e5d199 100644 --- a/crates/iota-framework/docs/timelock/timelocked_balance.md +++ b/crates/iota-framework/docs/timelock/timelocked_balance.md @@ -11,9 +11,9 @@ Utility functions for time-locked balance. - [Function `split`](#0x10cf_timelocked_balance_split) -
use 0x10cf::label;
-use 0x10cf::timelock;
+
use 0x10cf::timelock;
 use 0x1::option;
+use 0x1::string;
 use 0x2::balance;
 use 0x2::tx_context;
 
@@ -64,13 +64,10 @@ Join two TimeLock<Balance<T>> together.
public fun join<T>(self: &mut TimeLock<Balance<T>>, other: TimeLock<Balance<T>>) {
     // Check the preconditions.
     assert!(self.expiration_timestamp_ms() == other.expiration_timestamp_ms(), EDifferentExpirationTime);
-    assert!(self.label() == other.label(), EDifferentLabels);
+    assert!(self.label() == other.label(), EDifferentLabels);
 
     // Unpack the time-locked balance.
-    let (value, _, label) = timelock::unpack(other);
-
-    // Destroy the labels.
-    label::destroy_opt(label);
+    let (value, _, _) = timelock::unpack(other);
 
     // Join the balances.
     self.locked_mut().join(value);
@@ -138,7 +135,7 @@ Split a TimeLock<Balance<T>> and take a sub balance fro
     let value = self.locked_mut().split(value);
 
     // Pack the splitted balance into a timelock.
-    timelock::pack(value, self.expiration_timestamp_ms(), label::clone_opt(self.label()), ctx)
+    timelock::pack(value, self.expiration_timestamp_ms(), self.label(), ctx)
 }
 
diff --git a/crates/iota-framework/docs/timelock/timelocked_staked_iota.md b/crates/iota-framework/docs/timelock/timelocked_staked_iota.md index 57fb7d6be21..ba39a5d0886 100644 --- a/crates/iota-framework/docs/timelock/timelocked_staked_iota.md +++ b/crates/iota-framework/docs/timelock/timelocked_staked_iota.md @@ -12,6 +12,7 @@ title: Module `0x10cf::timelocked_staked_iota` - [Function `stake_activation_epoch`](#0x10cf_timelocked_staked_iota_stake_activation_epoch) - [Function `expiration_timestamp_ms`](#0x10cf_timelocked_staked_iota_expiration_timestamp_ms) - [Function `label`](#0x10cf_timelocked_staked_iota_label) +- [Function `is_labeled_with`](#0x10cf_timelocked_staked_iota_is_labeled_with) - [Function `split`](#0x10cf_timelocked_staked_iota_split) - [Function `split_staked_iota`](#0x10cf_timelocked_staked_iota_split_staked_iota) - [Function `join_staked_iota`](#0x10cf_timelocked_staked_iota_join_staked_iota) @@ -20,8 +21,9 @@ title: Module `0x10cf::timelocked_staked_iota` - [Function `transfer`](#0x10cf_timelocked_staked_iota_transfer) -
use 0x10cf::label;
+
use 0x10cf::labeler;
 use 0x1::option;
+use 0x1::string;
 use 0x2::object;
 use 0x2::transfer;
 use 0x2::tx_context;
@@ -66,7 +68,7 @@ A self-custodial object holding the timelocked staked IOTA tokens.
  This is the epoch time stamp of when the lock expires.
 
-label: option::Option<label::Label> +label: option::Option<string::String>
Timelock related label. @@ -97,7 +99,7 @@ A self-custodial object holding the timelocked staked IOTA tokens. Create a new instance of TimelockedStakedIota. -
public(friend) fun create(staked_iota: staking_pool::StakedIota, expiration_timestamp_ms: u64, label: option::Option<label::Label>, ctx: &mut tx_context::TxContext): timelocked_staked_iota::TimelockedStakedIota
+
public(friend) fun create(staked_iota: staking_pool::StakedIota, expiration_timestamp_ms: u64, label: option::Option<string::String>, ctx: &mut tx_context::TxContext): timelocked_staked_iota::TimelockedStakedIota
 
@@ -109,14 +111,14 @@ Create a new instance of create( staked_iota: StakedIota, expiration_timestamp_ms: u64, - label: Option<Label>, + label: Option<String>, ctx: &mut TxContext ): TimelockedStakedIota { TimelockedStakedIota { id: object::new(ctx), staked_iota, expiration_timestamp_ms, - label, + label, } }
@@ -228,7 +230,7 @@ Function to get the expiration timestamp of a TimelockedStakedIota. -
public fun label(self: &timelocked_staked_iota::TimelockedStakedIota): &option::Option<label::Label>
+
public fun label(self: &timelocked_staked_iota::TimelockedStakedIota): option::Option<string::String>
 
@@ -237,8 +239,38 @@ Function to get the label of a label(self: &TimelockedStakedIota): &Option<Label> { - &self.label +
public fun label(self: &TimelockedStakedIota): Option<String> {
+    self.label
+}
+
+ + + + + + + +## Function `is_labeled_with` + +Check if a TimelockedStakedIota is labeled with the type L. + + +
public fun is_labeled_with<L>(self: &timelocked_staked_iota::TimelockedStakedIota): bool
+
+ + + +
+Implementation + + +
public fun is_labeled_with<L>(self: &TimelockedStakedIota): bool {
+    if (self.label.is_some()) {
+        self.label.borrow() == labeler::type_name<L>()
+    }
+    else {
+        false
+    }
 }
 
@@ -271,7 +303,7 @@ All the other parameters of the object::new(ctx), staked_iota: splitted_stake, expiration_timestamp_ms: self.expiration_timestamp_ms, - label: label::clone_opt(&self.label), + label: self.label, } }
@@ -330,11 +362,9 @@ Aborts if some of the staking parameters are incompatible (pool id, stake activa id, staked_iota, expiration_timestamp_ms: _, - label, + label: _, } = other; - label::destroy_opt(label); - id.delete(); self.staked_iota.join(staked_iota); @@ -364,7 +394,7 @@ Returns true if all the staking parameters of the staked iota except the princip
public fun is_equal_staking_metadata(self: &TimelockedStakedIota, other: &TimelockedStakedIota): bool {
     self.staked_iota.is_equal_staking_metadata(&other.staked_iota) &&
     (self.expiration_timestamp_ms == other.expiration_timestamp_ms) &&
-    (self.label() == other.label())
+    (self.label() == other.label())
 }
 
@@ -379,7 +409,7 @@ Returns true if all the staking parameters of the staked iota except the princip A utility function to destroy a TimelockedStakedIota. -
public(friend) fun unpack(self: timelocked_staked_iota::TimelockedStakedIota): (staking_pool::StakedIota, u64, option::Option<label::Label>)
+
public(friend) fun unpack(self: timelocked_staked_iota::TimelockedStakedIota): (staking_pool::StakedIota, u64, option::Option<string::String>)
 
@@ -388,17 +418,17 @@ A utility function to destroy a unpack(self: TimelockedStakedIota): (StakedIota, u64, Option<Label>) { +
public(package) fun unpack(self: TimelockedStakedIota): (StakedIota, u64, Option<String>) {
     let TimelockedStakedIota {
         id,
         staked_iota,
         expiration_timestamp_ms,
-        label,
+        label,
     } = self;
 
     object::delete(id);
 
-    (staked_iota, expiration_timestamp_ms, label)
+    (staked_iota, expiration_timestamp_ms, label)
 }
 
diff --git a/crates/iota-framework/docs/timelock/timelocked_staking.md b/crates/iota-framework/docs/timelock/timelocked_staking.md index 41382bfb1fb..cb2fa01ce63 100644 --- a/crates/iota-framework/docs/timelock/timelocked_staking.md +++ b/crates/iota-framework/docs/timelock/timelocked_staking.md @@ -13,10 +13,10 @@ title: Module `0x10cf::timelocked_staking` - [Function `request_withdraw_stake_non_entry`](#0x10cf_timelocked_staking_request_withdraw_stake_non_entry) -
use 0x10cf::label;
-use 0x10cf::timelock;
+
use 0x10cf::timelock;
 use 0x10cf::timelocked_staked_iota;
 use 0x1::option;
+use 0x1::string;
 use 0x2::balance;
 use 0x2::coin;
 use 0x2::iota;
@@ -103,7 +103,7 @@ The non-entry version of request_add_stake, which returns the time-
     assert!(timelocked_balance.is_locked(ctx), ETimeLockShouldNotBeExpired);
 
     // Unpack the time-locked balance.
-    let (balance, expiration_timestamp_ms, label) = timelock::unpack(timelocked_balance);
+    let (balance, expiration_timestamp_ms, label) = timelock::unpack(timelocked_balance);
 
     // Stake the time-locked balance.
     let staked_iota = iota_system.request_add_stake_non_entry(
@@ -116,7 +116,7 @@ The non-entry version of request_add_stake, which returns the time-
     timelocked_staked_iota::create(
         staked_iota,
         expiration_timestamp_ms,
-        label,
+        label,
         ctx,
     )
 }
@@ -292,7 +292,7 @@ instead of transferring it to the sender.
     ctx: &mut TxContext,
 ) : (TimeLock<Balance<IOTA>>, Balance<IOTA>) {
     // Unpack the `TimelockedStakedIota` instance.
-    let (staked_iota, expiration_timestamp_ms, label) = timelocked_staked_iota.unpack();
+    let (staked_iota, expiration_timestamp_ms, label) = timelocked_staked_iota.unpack();
 
     // Store the original stake amount.
     let principal = staked_iota.staked_iota_amount();
@@ -305,7 +305,7 @@ instead of transferring it to the sender.
     let principal = withdraw_stake.split(principal);
 
     // Pack and return a time-locked balance, and the reward.
-    (timelock::pack(principal, expiration_timestamp_ms, label, ctx), withdraw_stake)
+    (timelock::pack(principal, expiration_timestamp_ms, label, ctx), withdraw_stake)
 }
 
diff --git a/crates/iota-framework/packages/timelock/sources/label.move b/crates/iota-framework/packages/timelock/sources/label.move deleted file mode 100644 index 7959582fa42..00000000000 --- a/crates/iota-framework/packages/timelock/sources/label.move +++ /dev/null @@ -1,116 +0,0 @@ -// Copyright (c) 2024 IOTA Stiftung -// Modifications Copyright (c) 2024 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -module timelock::label { - - use std::string::{Self, String}; - - use iota::types; - - // === Error codes === - - /// Error code for when a type passed to the `create_labeler_cap` function is not a one-time witness. - const ENotOneTimeWitness: u64 = 0; - - // === Structs === - - /// `LabelerCap` allows to create labels of the specific type `L`. - /// Can be publicly transferred like any other object. - public struct LabelerCap has key, store { - id: UID, - } - - /// `Label` is an immutable label representation. - /// The only way to create instances is through the `LabelerCap`. - /// Upon creation, `value` field becomes the fully qualified type name of `L`. - public struct Label has store { - /// A fully qualified type name with the original package IDs. - value: String, - } - // === `LabelerCap` functions === - - /// Create a `LabelerCap` instance. - /// Can be created only by consuming a one time witness. - public fun create_labeler_cap(witness: L, ctx: &mut TxContext): LabelerCap { - assert!(types::is_one_time_witness(&witness), ENotOneTimeWitness); - - LabelerCap { - id: object::new(ctx), - } - } - - /// Delete a `LabelerCap` instance. - /// If a capability is destroyed, it is impossible to add the related labels. - public fun destroy_labeler_cap(cap: LabelerCap) { - let LabelerCap { - id, - } = cap; - - object::delete(id); - } - - // === `Label` functions === - - /// Check if a `Label` represents the type `L`. - public fun is_type(self: &Label): bool { - self.value == type_name() - } - - /// Function to get the value of a `Label`. - public fun value(self: &Label): &String { - &self.value - } - - /// Create a `Label` instance. - /// The created label holds a fully qualified type name with the original package IDs. - /// Can be called only by the related `LabelerCap` owner. - public fun create(_: &LabelerCap): Label { - Label { - value: type_name(), - } - } - - /// Destroy a `Label` instance. - public fun destroy(self: Label) { - let Label { - value: _, - } = self; - } - - /// Destroy an optional `Label` instance. - public fun destroy_opt(self: Option