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?
Tezos Stack Exchange is a question and answer site for users, experts, and developers of the Tezos blockchain project. It only takes a minute to sign up.
Sign up to join this communityA 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?
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
delegatable
return ([], storage)
else:
# 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.)