Skip to content

move: Add verified lookup and reverse_lookup functions #768

@Thoralf-M

Description

@Thoralf-M

Description

Add public fun lookup_address(self: &Registry, name: Name, clock: &Clock): Option<Address> { and public fun reverse_lookup(self: &Registry, address: address, clock: &Clock): Option<Name> {, maybe with _verified suffix to distinguish with the functions in registry.move.

Motivation

Currently, callers need to manually call the checks if they want to be sure that something is not expired:

    /// Different custom error messages.
    #[error]
    const ENameNotFound: vector<u8> = b"Name not found.";
    #[error]
    const ENameNotPointingToAddress: vector<u8> = b"Name not pointing to an address.";
    #[error]
    const ENameExpired: vector<u8> = b"Name expired.";
 
    /// A function to transfer an object of any type T to a name (for instance `example.iota`)
    public fun send_to_name<T: key + store>(iota_names: &IotaNames, obj: T, name: String, clock: &Clock) {
        // Look up the name on the registry.
        let mut optional = iota_names.registry<Registry>().lookup(name::new(name));
        // Check that the name indeed exists.
        assert!(optional.is_some(), ENameNotFound);
 
        let name_record = optional.extract();
        // Check that name has not expired. 
        // This check is optional, but it's recommended you perform the verification.
        assert!(!name_record.has_expired(clock), ENameExpired);
        // Check that the name has a target address set.
        assert!(name_record.target_address().is_some(), ENameNotPointingToAddress);
 
        // Transfer the object to that name.
        transfer::public_transfer(obj, name_record.target_address().extract())
    }

Having high level methods that do these checks internally makes it easier to use without doing something wrong

Requirements

Add the two new functions

Open questions (optional)

Are other names better? Should they return an Option or just error instead?

Metadata

Metadata

Labels

dev-toolsIssues related to the Developer Tools TeamenhancementNew feature or requestiota-namespackagesIssues/PRs related to smart contract packages

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions