Previous << Access control
Next >> Interfaces
CadenceはObject-capability modelを通じて、Capability-based securityをサポートします。
CadenceにおけるCapabilityとは、オブジェクトに対するアクセス及びそのオブジェクト上で特定の操作を実行する為の権利を表現した値です。一つのCapabilityは、アクセス可能なものは何で、それに対してどのようにアクセス出来るかを示しています。
Capabilityは偽造できず、譲渡可能で、削除可能です。
Capabilityには、ストレージCapabilityとアカウントCapabilityがあります。
- ストレージCapabilityは、Pathを介して、アカウントストレージ内のオブジェクトへのアクセスを許可します。
- アカウントCapabilityは、アカウントへのアクセスを許可します。
Capabilityは借用することで、格納されたオブジェクトへの、またはそのオブジェクトが参照するアカウントへの参照を取得できます。
Capabilityは、Capability<T: &Any>
という型を持ちます。型のパラメータには、Capabilityの借用時に取得できる参照(Reference)の種類を指定します。型は、Entitlement(権限)により関連付けられた一連のアクセス権を示します。すなわち、Capabilityの参照型は認証されており、与えられた権限が必要とする対象のフィールドや関数にアクセスできる機能群がCapabilityの所有者に与えられています。
例えば、Capability<auth(SaveValue) &Account>
という型を持つCapabilityはアカウントへのアクセスを許可し、そのアカウントへの値を保存することを(Capabilityの所有者に対して)許可しています。
各CapabilityにはIDがあります。IDはアカウント即ちアドレス毎に一意です。
Capabilityは、capability controllerを通じて作成および管理されます。
Capabilityは構造体であるため、コピーが可能です。対象のcapability controllerが削除されない限り何回でも使用(借用)できます。
Capability
access(all)
struct Capability<T: &Any> {
/* The address of the account which the capability targets. */
access(all)
let address: Address
/* The ID of the capability. */
access(all)
let id: UInt64
/** Returns a reference to the targeted object.
*
* If the capability is revoked, the function returns nil.
*
* If the capability targets an object in account storage,
* and and no object is stored at the target storage path,
* the function returns nil.
*
* If the targeted object cannot be borrowed using the given type,
* the function panics.
*/
access(all)
view fun borrow(): T?
/** Returns true if the capability currently targets an object
* that satisfies the given type, i.e. could be borrowed using the given type.
*/
access(all)
view fun check(): Bool
}
翻訳元
Flow BlockchainのCadence version1.0ドキュメント (Capabilities)