2

I would like to know if the entrypoint of my smart contract is called from an implicit(tz1) or an originated address(KT1). How can i do the check in smartpy?

Looking at the Smartpy documentation, it's said that the address type sp.TAddress is "An address of a contract or implicit account". Further you can't convert the address to string because Michelson doesn't allow type casting.

Am i missing something? How can i differentiate between the address types?

2 Answers 2

1

If you are specifically interested in the address of your caller there a simpler solution than the one proposed by @rodrigo-quelhas: sp.sender is an implicit account if and only if it is exactly sp.source.

1

You can use: https://github.com/RomarQ/tezos-sc-utils

Usage:

Utils = sp.io.import_script_from_url("https://raw.githubusercontent.com/RomarQ/tezos-sc-utils/main/smartpy/utils.py");

# And then inside the entrypoint

Utils.Address.is_kt1(sp.address("tz28QJHLyqvaY2rXAoFZTbxrXeD88NA8wscC")) # False
Utils.Address.is_kt1(sp.address("KT18hYjnko76SBVv6TaCT4kU6B32mJk6JWLZ")) # True

You can check the code here: https://github.com/RomarQ/tezos-sc-utils/blob/main/smartpy/utils.py#L301

Documented here: https://github.com/RomarQ/tezos-sc-utils/blob/main/smartpy/README.md#documentation

Explanation:

  • Addresses of implicit accounts are strictly less than addresses of originated accounts;
  • Addresses of the same type are compared lexicographically;
  • KT1XvNYseNDJJ6Kw27qhSEDF8ys8JhDopzfG is the highest KT1 address;
  • KT18amZmM5W7qDWVt2pH6uj7sCEd3kbzLrHT is the lowest KT1 address;
  • Any address in between (including) those two addresses is an originated account;
3
  • how and why are those the highest and lowest KT1 addresses?
    – 0x10
    Apr 26, 2022 at 10:08
  • 1
    The addresses that you see are a slice of bytes encoded to base58check. The lowest is [2, 90, 121, 0, 0, 0, ....] and the highest is [2, 90, 121, 255, 255, 255, ....], where the first 3 bytes represent the prefix (KT1). Apr 26, 2022 at 17:12
  • what would the tz version of this look like (in terms of the first n bytes)?
    – 0x10
    Apr 30, 2022 at 6:25

Your Answer

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

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