inbox
Account Inbox
Accounts also possess an Inbox
that can be used to make Capabilities available to specific accounts.
The functions in this Inbox
provide a convenient means to "bootstrap" Capabilities,
setting up an initial connection between two accounts that will later allow them to transfer data or permissions through a Capability.
Publishing a Capability
An account (the provider) that would like to provide a Capability to another account (the recipient) can do so using the publish
function:
_10fun publish(_ value: Capability, name: String, recipient: Address)
This publishes the specified Capability using the provided string as an identifier, to be later claimed by the recipient. Note, however, that until the recipient does claim this Capability, it is stored on the provider's account, and contributes towards their Account Storage total.
Calling this function emits an event, InboxValuePublished
,
that includes the address of both the provider and the recipient, as well as the name and the type of the published Capability.
Refer to the Core Events
section for more details on this event.
Claiming a Capability
The intended recipient of a Capability can claim that Capability from the provider using the claim
function:
_10fun claim<T: &Any>(_ name: String, provider: Address): Capability<T>?
This looks up the specified name in the provider's inbox, returning it to the recipient if it is present,
conforms to the provided type argument, and is intended for the calling recipient.
If the provider has no Capability stored under the provided name,
or if the calling recipient is not the intended recipient of the Capability, the function returns nil
.
If the borrow type of the Capability is not a subtype of the provided type argument, the function will error at runtime.
Upon successful completion of the claim
function, the claimed Capability is removed from the provider's inbox.
Note that this means a given Capability can only be claimed once.
Calling this function emits an event, InboxValueClaimed
,
that includes the address of both the provider and the recipient, as well as the name of the claimed Capability.
Refer to the Core Events
section for more details on this event.
Unpublishing a Capability
If the provider of a Capability no longer wishes for it to be published for some reason (e.g. they no longer wish to pay for its storage costs),
they can unpublish it using the unpublish
function:
_10fun unpublish<T: &Any>(_ name: String): Capability<T>?
This looks up the specified name in the provider's inbox, returning it to the provider if it is present and conforms to the provided type argument.
If the provider has no Capability stored under the provided name, the function returns nil
.
If the borrow type of the Capability is not a subtype of the provided type argument, the function will error at runtime.
Upon successful completion of the unpublish
function, the unpublished Capability is removed from the provider's inbox.
Calling this function emits an event, InboxValueUnpublished
,
that includes the address of the provider, and the name of the claimed Capability.
Refer to the Core Events
section for more details on this event.