0

I am attempting to write a simple function to check if an address is an account (e.g tz1, tz2 etc) or contract address (KT).

I want to write something like this:

function isAddressContract(ad: address) : bool {
  const adString = address_to_string(ad);
  const firstTwo = slice(adString, 0, 2);
  return (firstTwo = "KT" ? true : false)
}

But of course address_to_string is not a real archetype builtin.

I attempted to leverage address_to_contract but I do not really understand what this is for and I wasn't able to get the syntax right anyway and received a parsing error. My attempt:

function isAddressContract(ad: address) : bool {
  return ((address_to_contract(ad) <> unit) ? true : false)
}

How can I convert an address to a string or otherwise solve this simple problem?

Thankyou in advance for any assistance!

1

1 Answer 1

3

There are two very common special cases for which you can simply restrict your contract to implicit accounts:

  • if the address you want to check is the sender of the operation, you have the guarentee that it is implicit if and only if it is equal to the source of the transaction.
  • if the address you want to check is an input of your contract you can change its type from address to key_hash . key_hash is the type of addresses of implicit accounts.

If you are not in one of these common special cases, you test if an address is implicit by either:

  • serializing it and checking the appropriate byte
  • compare it to the smallest possible KT1 address (if new kinds of addresses are added in the future, its probable that implicit addresses will be added before the first KT1 and new originated addresses will be added after it). The smallest KT1 address is KT18amZmM5W7qDWVt2pH6uj7sCEd3kbzLrHT.

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.