A Set_delegate operation will fail if the specified key_hash is not delegatable.

Is it possible in Michelson to verify if a kh : key_hash is not delegatable and thus if the operation emitted by SET_DELEGATE (Some kh) will fail for this reason?

1 Answer 1


There is a trick to assert that a key hash is delegatable without delegating to it. However, you cannot catch failure, but you can assure the contract execution only continues if the key hash was delegatable.

The trick is to send two Set_delegate operations, the first one to the key hash to test and the second one to restore your previous delegate.

At the condition that the key hash you want to test and your current delegate are not the same, because designating the current delegate through Set_delegate will also fails.

For instance, consider a contract with an entrypoint that lets the caller nominate a certain key hash for delegation. However, the entrypoint must verify that the passed key hash belongs to a delegatable account. We could implement this thus, in pseudo-code:

def nominate_delegate(kh : key_hash, storage):
    if (Some kh) == current_delegate:
        # do nothing, if the kh is the current_delegate then it must be 
        return ([], storage)
        # check if kh is delegatable
        ops = []
        ops += SET_DELEGATE(Some kh);
        ops += SET_DELEGATE(current_delegate);
        storage.nominated_delegates += kh
        return (ops, storage) 

Here, current_delegate : option key_hash is a part of the contracts storage that tracks the current delegate, if any.

(Credits to @raphael cauderlier and @charles on #michelson for this tips.)

  • SET_DELEGATE returns an operation. If you do not include that operation in the return value of the entrypoint, would it still fail if the kh in set_delegate(Some kh) is invalid?
    – MCH
    Jan 31, 2020 at 14:06
  • 1
    No: in my pseudo-code, i left implicit the fact that the result of the set_delegate operations must be returned. I'll update the answer to clarify.
    – arvidj
    Jan 31, 2020 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.