EOS require_auth function

action structure

Make it clear meaning and usage of this method, we need the actionstructure to start. See eoslib.hppthe action class, where it refers to the structure can be simplified into the following:

   *   struct action {
   *     account_name account; // the contract defining the primary code to execute for code/type
   *     action_name name; // the action to be taken
   *     permission_level[] authorization; // the accounts and permission levels provided
   *     bytes data; // opaque data processed by code
   *   };

The data includes an action:

account: action processor (handler) where the contract account 
name: action name 
authorization: permissions list action is provided by the caller (can be a set of keys, it can be a group of other people's permission) 
the Data: Data action parameter, if transfer action, the data here is something like this:

{   "from": "inita",   "to": "initb",   "amount": "100.0000 EOS",   "memo": "1234"} 

You might say, "No, data obviously is a byte array, how can store a structure?", This is actually a data serialization results on serialization and de-serialization, if you are not very understanding, you can search online about knowledge.

require_auth

Now we say require_authit is easier, and look at the signature:

  /**
   *  Verifies that @ref name exists in the set of provided auths on a action. Throws if not found.
   *
   *  @brief Verify specified account exists in the set of provided auths
   *  @param name - name of the account to be verified
   */
  void require_auth( account_name name );

English comments look good, combined structure is completely understand the action: it through calibration nameparameter passed in the account to see if this action in 已提供的权限列表the. If so, then the check is passed, otherwise, an exception is thrown.

For example, if the following command to initiate a action:

cleos push action hello.code hi '["user"]' -p user@active

Here initiated this hi action structure is like this:

{  "account": "hello.code",  "name": "hi",  "authorization": [ {"account": "user", "permission":"active"} ],  "data": ["user"] }

So if the hi call to action processor which require_auth(N(user))is by check, because userin authorizationthe array; and require_auth(N(hello.code))would fail to detect, and throws an exception.

Sometimes, you might want to see whether the action of an account 已提供权限列表, the do not want to throw an exception, then how to do?

This method can be used when has_auth:

 /**
   *  Verifies that @ref name has auth.
   *
   *  @brief Verifies that @ref name has auth.
   *  @param name - name of the account to be verified
   */
  bool has_auth( account_name name );

require_auth2

There is also a similar require_auth2method, its signature is this:

/**
   *  Verifies that @ref name exists in the set of provided auths on a action. Throws if not found. 
   *
   *  @brief Verify specified account exists in the set of provided auths
   *  @param name - name of the account to be verified
   *  @param permission - permission level to be verified
   */
  void require_auth2( account_name name, permission_name permission );

This check a little more stringent, in addition to the designated account, also specify the license. This license is strictly checked, that is, if you write the code is:

require_auth(N(user), N(active));

Then the following command is pass this examination:

cleos push action hello.code hi '["user"]' -p user@owner

Although the use of a higher authority user@owner, can not pass inspection.

Guess you like

Origin www.cnblogs.com/zhangmingcheng/p/11532548.html