What is the correct way of checking Identity type and retrieving from storage?

What is the correct standard way of checking Identity?

fn _check_manager() {
    let sender: Identity = msg_sender().unwrap();
    let owner: Identity = storage.owner.read(); 
    let manager: Identity = storage.manager.read();

    // Ensure the sender is either the owner or the manager
    require(sender == owner|| sender == manager, "Not authorized");
}

Another question is, If I initialized as Address, in other functions can I write with ContractID?

storage {
    owner: Identity = Identity::Address(Address::zero());,
    manager: Identity = Identity::Address(Address::zero()),
}

According to Docs, it can only be one. Was wondering if there would be any issue.

Enum std::identity::Identity
pub enum Identity {
    Address: Address,
    ContractId: ContractId,
}
The Identity type: either an Address or a ContractId.
1 Like

This is the correct implementation for checking if the sender is either the owner or the manager. You can have a look at this example for the ref.

Regarding the another question, yes, the Identity type can be either an Address or a ContractId, but not both at the same time. If you initialize a storage variable as an Address , you should consistently use it as an Address in other functions. Similarly, if you initialize it as a ContractId , you should use it as a ContractId . Mixing them can lead to errors because they are distinct types within the Identity enum.

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.