前面兩篇文章我們分析了eosio.msig
合約荸哟。中間有些內(nèi)容因?yàn)槠鶝]有仔細(xì)講解假哎,今天開始打算把一些知識(shí)點(diǎn)攻克一下。有些比較難的知識(shí)點(diǎn)鞍历,自然會(huì)詳細(xì)介紹舵抹;有些呢,則看起來比較簡(jiǎn)單劣砍,然而深入進(jìn)去之后惧蛹,卻可以加深對(duì)EOS系統(tǒng)的理解。今天先介紹第一個(gè)知識(shí)點(diǎn):require_auth
函數(shù)刑枝。
action的結(jié)構(gòu)
要說清楚這個(gè)方法的含義和用法香嗓,咱們需要從action
的結(jié)構(gòu)說起。詳見eoslib.hpp
中的action類装畅,這里把它的結(jié)構(gòu)簡(jiǎn)化表示成下面這樣:
* 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
* };
一個(gè)action的數(shù)據(jù)包含:
account
: action的處理器(handler
)所在的合約賬號(hào)
name
: action的名字
authorization
: 調(diào)用者提供的action的權(quán)限列表(可以是一組keys靠娱,也可以是一組別人的許可權(quán)限,回憶一下前幾篇關(guān)于自定義許可權(quán)限的內(nèi)容洁灵,把知識(shí)點(diǎn)打通)
data
: action的數(shù)據(jù)參數(shù)饱岸,如果是transfer
action掺出,這里的數(shù)據(jù)就是類似這樣的內(nèi)容:
{
"from": "inita",
"to": "initb",
"amount": "100.0000 EOS",
"memo": "1234"
}
你可能說,“不對(duì)苫费,data明明是個(gè)byte數(shù)組汤锨,怎么能存儲(chǔ)一個(gè)結(jié)構(gòu)呢?”百框,這其實(shí)是數(shù)據(jù)序列化的結(jié)果闲礼,關(guān)于序列化和反序列化,如果你還不是很了解铐维,可以從網(wǎng)上搜索一下相關(guān)知識(shí)柬泽。
require_auth
現(xiàn)在我們?cè)僬frequire_auth
就比較容易了,先看簽名:
/**
* 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 );
英文好的看下注釋嫁蛇,再結(jié)合action的結(jié)構(gòu)就完全明白了:它校驗(yàn)通過name
形參傳進(jìn)來的賬戶锨并,看是否在本action已提供的權(quán)限列表
中。如果在睬棚,則校驗(yàn)通過第煮,否則,拋出異常抑党。
比如如果執(zhí)行下面的命令發(fā)起一個(gè)action:
cleos push action hello.code hi '["user"]' -p user@active
這里發(fā)起的這個(gè)hi
action的結(jié)構(gòu)就是類似這樣的:
{
"account": "hello.code",
"name": "hi",
"authorization": [ {"account": "user", "permission":"active"} ],
"data": ["user"]
}
所以如果在hi
action的處理器里面調(diào)用require_auth(N(user))
是可以通過檢查的包警,因?yàn)?code>user在authorization
數(shù)組中;而require_auth(N(hello.code))
就會(huì)檢測(cè)失敗底靠,并拋出異常害晦。
有時(shí)候,你可能就想看看某個(gè)賬戶是否在action的已提供權(quán)限列表
里暑中,并不想拋出異常壹瘟,那該怎么辦?
這個(gè)時(shí)候可以用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
還有一個(gè)類似的require_auth2
方法痒芝,它的簽名是這樣的:
/**
* 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 );
這個(gè)檢查更為嚴(yán)格一點(diǎn)俐筋,除了指定賬戶,還要指定許可严衬。這個(gè)許可是嚴(yán)格檢查的澄者,也就是說,假如你在代碼里寫的是:
require_auth(N(user), N(active));
那么下面的命令是通不過這個(gè)檢查的:
cleos push action hello.code hi '["user"]' -p user@owner
盡管這里使用的是更高的權(quán)限user@owner
请琳,也無(wú)法通過檢查粱挡。
好了,今天就這樣俄精。
簡(jiǎn)介:不羈询筏,一名程序員;專研EOS技術(shù)竖慧,玩轉(zhuǎn)EOS智能合約開發(fā)嫌套。
微信公眾號(hào):know_it_well
知識(shí)星球地址:https://t.zsxq.com/QvbuzFM