Reputation: 462
What is the difference between transfer::share_object
vs transfer::public_share_object
in Sui Move?
I need to create a shared object which can be only modified in the same package but others cannot modify.
The explanation from Sui documentation is not clear for me.
Upvotes: 0
Views: 62
Reputation: 21
transfer::transfer
is used for Structs (objects) that only have the key
ability, and as pointed out it will only work if used in the module that the object is defined. This is the reason why objects with only the key ability are called "soulbound", meaning they are not transferable, because as the developer you can choose when and if you will call transfer::transfer
, a user cannot call it from web2, it will fail every time.
transfer::public_transfer
on the other hand is for Structs (objects) that have key + store, this are the most common type of objects that can be transferred by the user. So this function can be called from web2.
Shared objects cannot be transferred, so in your case for a shared object it doesn't really matter whether your object has only key or key and store. Also your concern about the object being modified by the same package, this is guaranteed by the Move language itself. Any object can only be modified by the functions the developer wrote in the module that defined it (even more restrictive than your requirement for just the package).
Upvotes: 0
Reputation: 91
I have a bit of experience in sui move. I am computer vision developer and I start learning blockchain technology. In sui move, both transfer::share_object
and transfer::public_share_object
are used to create shared objects, but they differ in how the object can be accessed and modified. Designing secure shared objects requires understanding these differences and applying your development talent effectively.
solution
transfer::shared_object
Upvotes: 0
Reputation: 7505
According to the official SUI transfer.move
package:
/// Turn the given object into a mutable shared object that everyone can access and mutate.
/// This is irreversible, i.e. once an object is shared, it will stay shared forever.
/// Aborts with `ESharedNonNewObject` of the object being shared was not created in this
/// transaction. This restriction may be relaxed in the future.
/// This function has custom rules performed by the Sui Move bytecode verifier that ensures
/// that `T` is an object defined in the module where `share_object` is invoked. Use
/// `public_share_object` to share an object with `store` outside of its module.
public fun share_object<T: key>(obj: T) {
share_object_impl(obj)
}
/// Turn the given object into a mutable shared object that everyone can access and mutate.
/// This is irreversible, i.e. once an object is shared, it will stay shared forever.
/// Aborts with `ESharedNonNewObject` of the object being shared was not created in this
/// transaction. This restriction may be relaxed in the future.
/// The object must have `store` to be shared outside of its module.
public fun public_share_object<T: key + store>(obj: T) {
share_object_impl(obj)
}
In short:
Both functions make an object publicly accessible and mutable by anyone on the network, with one key difference:
• share_object<T>
can only be called from within the same module where type T is defined
• public_share_object<T>
can be called from anywhere but requires the type T to have the store ability
In both cases:
• The sharing is permanent/irreversible
• The object must be newly created in the same transaction
• Everyone can access and modify the shared object afterwards
The main difference is about where you can call them from and what types of objects they accept.
Upvotes: 0