Problem
An agent wants to maintain control over a resource but also wants to delegate their agency to another agent.
Examples
- Alice is a CEO. She hires a ghost writer to publish blog articles in her name.
- Bob controls the credit limit on a community currency. He’s going on a year-long sailing trip and wants Carol to manage that credit limit in his stead.
Solution
Use capability-based security and call_remote
to allow an agent to delegate the power to ‘call’ the zome functions in her DNA instance.
Implementation
Holochain uses capability-based security to secure access to zome functions. Using call_remote
, one agent can call another agent’s functions over the DNA’s peer-to-peer network.
Choose the zome functions in your DNA that can be delegated to other agents. For each of these functions, create a flow that lets one agent ask another agent for permission to call the function in their DNA instance. Here’s an example using a blog article posting function and call_remote
to handle the initial requesting and granting of the capability:
- Bob wants to ghost-write for Alice, so he calls a zome function
request_ghost_post_capability(alice_id)
. - Within
request_ghost_post_capability
Bob’s cell call’s a zome functionhandle_ghost_post_capability_request(bob_id)
in Alice’s cell. This function notifies Alice’s UI that Bob wants to post on her behalf. - Alice decides to approve this request. She calls the zome function
grant_ghost_post_capability(bob_id)
, which a capability grant, assigned to Bob, for the zome functioncreate_post
. - Within
grant_ghost_post_capability
, Alice’s cell call’s the zome functionhandle_ghost_post_capability_grant(capability_secret)
in Bob’s cell. - Within
handle_ghost_post_capability_grant
, Bob’s cell stores the capability secret Alice has given to him, along with a memo of what the grant is for. - Now it’s time for Bob to write an article. He composes it, then calls the
ghost_post(title, content, alice_id)
zome function. - Within
ghost_post
, Bob’s cell brings up the capability secret Alice gave him, then calls the zome functionhandle_ghost_post(title, content, capability_secret)
in Alice’s cell. - Alice’s cell writes the new post in her source chain, then publishes it to the DHT. It looks like she has written the post, because it’s got her signature on it.
Variations
- Partial grant: Alice might choose to place limits on his grant, like the ability to only post until a certain date or under a certain category. Currently this would have to be checked using extra metadata written to Alice’s source chain, but in the future we intend to bring partial application to capability grants.
- Transferable vs assigned: Holochain’s capability grants let you specify whether it can be used by anyone who holds the token (transferable) or a certain set of agents by public key (assigned).
-
Chained delegation: Bob might want to let Charlie ghost-write for Alice by giving Charlie a capability grant for his
ghost_post
function. -
Asynchronous: If agents aren’t guaranteed to be online at the same time, use the Mailbox pattern rather than
call_remote
. If necessary, protect communications using Asynchronous Private Messaging.
Warnings
- Think hard about the possible conditions to be placed on a grant. Agents can’t revoke a capability unless your DNA and UI code give them a way to do it.
- Once a transferable token has been leaked, anyone can use it. Agents should keep grants and claims secret!
- Chained delegation could get pretty hairy pretty quickly.
Related patterns
Capability-based security is for giving access to resources under one agent’s control, which in Holochain means one agent’s cell and source chain. It isn’t appropriate for controlling access to public data on the DHT. Use these patterns instead:
- Membership by Invitation or Member Whitelist for read access to a DHT’s data
- Privilege Certificate or Benevolent Dictator for write privileges