How do I limit who can call to my entry points in Sophia?

Viewed 30

Which would the best way for a contract to manage access to my public functions (entrypoints) on my smart contract?

I have this entrypoint but I want only the "admin" to be able to access it.

public stateful entrypoint add_string_to_list(my_element:string) = 
    put(state{my_list=my_element::state.my_list})

First thing came to my mind was to manage a list of allowed addresses and add address parameter but still it won't ensure that who is calling is who I want.

1 Answers

In fact is super easy to get this done.

You only need to manage Call.caller and require function following the documentation here https://aeternity-sophia.readthedocs.io/en/latest/

Call.caller returns the address of who is calling the actual function (could be another contract by the way)

Contract.creator returns contract creator address.

And require will check the condition passed showing the error message and stopping the execution in case of failing.

So if we want the contract owner to be the onlyone who can call we can do:

public stateful entrypoint add_string_to_list(my_element:string) = 
    require(Call.caller==Contract.creator,"Only the contract owner is allowed to do this")
    put(state{my_list=my_element::state.my_list})

And that's it!

If you would do this in a more dynamic way then we should manage a specific address field and check against it.

Related