4

Is there any possibility to implement conditional require_auth(<condition>) or what is a good practice to make contract accept invocations from a set of predetermined accounts? Is there any analogue of solidity's msg.sender in EOS?

It seems that the documentation for require_auth2 is missing.

Example: I need the contract to require authorization from account walletxxxxxx OR walletzzzzzz.

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;

class [[eosio::contract("tmp")]] tmp : public eosio::contract {

public:
  using contract::contract;

  tmp(name receiver, name code,  datastream<const char*> ds): contract(receiver, code, ds) {}

  // Bad practice of multiauth implementation.
  [[eosio::action]]
  void multiauth(name user) {

    // Allow only `walletxxxxxx` OR `walletzzzzzz`
    // to call this function.

    if (user == name("walletxxxxxx"))
    {
      require_auth(name("walletxxxxxx"));
    }
    else if (user == name("walletzzzzzz"))
    {
      require_auth(name("walletzzzzzz"));
    }
    else
    {
      require_auth(name(get_self()));
    }

    // DO SOMETHING
  }

private:

};

EOSIO_DISPATCH( tmp, (multiauth) )

Honestly, the above contract can solve my problem, but it does not look like a good practice. I'm looking for any relevant documentation in regards to this issue.

2 Answers 2

1

The above case works if you do it like the following:

void multiauth(name user) {

   if (user == name("walletxxxxxx") || user == name("walletzzzzzz"))
   {
     require_auth(user);
   }
   else
   {
     require_auth(_self);
   }
}

But that's definitely not state of the art to grant permissions for execution of actions to specific accounts on eosio.

The name 'multiauth' itself says permissions should be granted to multiple users, a feature other blockchains lack of. And eosio's solution is the inbuild permission system.

1

I believe this is what you're looking for:

check(has_auth(name1) || has_auth(name2));
1
  • check(has_auth(name1) || has_auth(name2), "Invalid user"); Commented Feb 9, 2021 at 21:31

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.