基本概念
先看看這篇文章,了解下EOS.IO權限模型的三個基本概念:
- Wallets(錢包)——錢包是EOS.IO提供的用于管理密鑰對的客戶端,錢包支持鎖定和密碼解鎖。
- Accounts (賬戶)——賬戶是公示在區塊鏈上的人工易讀(不是公鑰或短地址!!!)的名字。
- Authorities and Permissions(權限及授權驗證)——每個賬戶都內置
owner
和active
權限,owner
冷藏用于恢復其他權限。
權限(Permission)定義的目標
EOSIO的賬戶權限定義的目標在白皮書中這樣敘述:
EOS.IO software allows each account to define a mapping between a Named Message Handler Group of any account and their own Named Permission Level. For example, an account holder could map the account holder's social media application to the account holder's "Friend" permission group. With this mapping, any friend could post as the account holder on the account holder's social media. Even though they would post as the account holder, they would still use their own keys to sign the message. This means it is always possible to identify which friends used the account and in what way.
這段話明確了EOSIO允許賬戶持有者部分讓度權限,而且權限受讓者是用自己持有的密鑰簽名,以便明確區分授權行為是如何發生的。
賬戶(Account)如何生成
權限是建立在賬戶的基礎之上的,那么賬戶又是如何產生的呢?
首先要明確的一點是:
無論賬戶還是權限,它們一定是通過簽名交易(signed transaction)來定義并在區塊鏈中公示的。——這是其他節點在執行合約時進行權限驗證(Authorities)的基礎。
來看看利用EOSIO的命令交互,如何生成一個新賬戶:
$ eosc create account inita tester EOS4toFS3YXEQCkuuw1aqDLrtHim86Gz9u3hBdcBw5KNPZcursVHq EOS7d9A3uLe6As66jzN8j44TXJUqJSK3bFjjEEqR4oTvNAB3iM9SA
命令的說明及返回在這里
返回JSON文本比較長,不過讀懂它的每一部分含義你也就理解了EOSIO權限模型的設計思路。這里我們先看看與本文相關的幾處:
- 每個賬戶默認攜帶
owner
和active
兩個權限,分別對應一對密鑰(*其中owner
密鑰冷藏保存,通常用active
來干活,方便用owner
恢復active
) - 該命令用賬戶
inita
的active
權限簽名提交了一個交易 - 交易調用了內置合約
eos
的newaccount
行為 - 命令傳入的公鑰分別作為新產生的tester賬戶的
owner
和active
權限公鑰
這里一定有人會問:那么這個最初的賬戶inita
是由什么賬戶來簽名生成的呢?——它是在啟動區塊鏈時,在創世區塊的配置config.ini
中直接定義的。
參考文檔在這里
權限(Permission)如何定義
上面我們看到,對于賬戶默認攜帶的owner
和active
權限,只需要在建立賬戶時指定就好。那么賬戶如何自定義權限呢?
看這里 后續的Create or Modify Permissions 章節。
權限定義的關鍵在于那個描述權重的JSON:
{
"threshold" : 100, /*An integer that defines cumulative signature weight required for authorization*/
"keys" : [], /*An array made up of individual permissions defined with an EOS PUBLIC KEY*/
"accounts" : [] /*An array made up of individual permissions defined with an EOS ACCOUNT*/
}
-
threshold
給出滿足此權限驗證至少要達到(當然比它高也滿足驗證)的權重。 -
keys
定義了在本權限定義中,可獲得的權重的密鑰集合。 -
accounts
定義了可獲得權重的賬戶+權限公鑰簽名集合。
即是說,后兩者是以不同的形式給出可獲得權重的簽名集合,實施中可根據具體情況靈活運用。
另外可以看出,目前的權限定義采用了覆蓋的形式,即:對同一權限的重復定義,后者會覆蓋前者。