sui icon indicating copy to clipboard operation
sui copied to clipboard

[move-std] Relaxing UID creation for `sui::derived_object::claim`

Open admin-aftermath opened this issue 1 month ago • 2 comments

Overview

Currently object creation enforces the UID is fresh. Is you provide a non-fresh UID, you receive the following error:

error[Sui E01001]: invalid object construction
    ┌─ ./sources/<module>.move:<line>:<column>
    │  
232 │       let object = Object {
    │ ╭──────────────────^
233 │ │         id: uid,
    │ │         --  ------------------------- Non fresh UID from this position
    │ │         │    
    │ │         The UID must come directly from `sui::object::new`, or `sui::derived_object::claim`. For tests, it can come from `sui::test_scenario::new_object`
234 │ │     };
    │ ╰─────^ Invalid object creation without a newly created UID.

This means that in order to use a central object to derive all UIDs for the objects created within your package either:

  1. The central object needs to be defined within the same module, or
  2. The central object needs to expose a public(package) fun borrow_mut_id(object: &mut <Object>): &mut UID.

(1) starts to break down as you encapsulate logic / objects across many modules and (2) is not a great pattern to allow. Also both of these methods break down if you want to derive a UID using an object from another package, a case I have run into a few times already with packages that separate logic across many packages, let alone modules.

It would be great if we can define a wrapper around sui::derived_object::claim for a locally defined object with a signature like:

// or public(package)
public fun claim<K: copy + drop + store>(
    object: &mut Object,
    key: K,
): UID {
    sui::derived:object(object.id, key)
}

And then from other modules use this object to derive UIDs through:

let local_object = LocalObject {
    id: object.claim(LocalKey {})
};

Currently the above is not possible due to the error[Sui E01001]: invalid object construction error.

Would it be possible to alleviate the above restriction to allow wrapping sui::derived_object::claim into a function while the returned UID is deemed fresh?

admin-aftermath avatar Nov 12 '25 19:11 admin-aftermath

Thank you for opening this issue, a team member will review it shortly. Until then, please do not interact with any users that claim to be from Sui support and do not click on any links!

github-actions[bot] avatar Nov 12 '25 19:11 github-actions[bot]

@damirka

zihehuang avatar Nov 13 '25 16:11 zihehuang