Skip to content

Commit d1c0598

Browse files
authored
Merge pull request #275 from onflow/brian-doyle/add-capabilities-graphic
Change title and add graphic
2 parents d47cdbb + 2c886f8 commit d1c0598

3 files changed

Lines changed: 28 additions & 19 deletions

File tree

docs/tutorial/03-resources.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,5 +543,5 @@ Reference solutions are functional, but may not be optimal.
543543
[nil-coalescing operator (`??`)]: ../language/operators/optional-operators.md#nil-coalescing-operator-
544544
[Non-Fungible Token Contract]: https://github.com/onflow/flow-nft/blob/master/contracts/NonFungibleToken.cdc#L115-L121
545545
[Generic NFT Transfer transaction]: https://github.com/onflow/flow-nft/blob/master/transactions/generic_transfer_with_address_and_type.cdc#L46-L50
546-
[Reference Solution]: https://play.flow.com/6f74fe85-465d-4e4f-a534-1895f6a3c0a6
546+
[Reference Solution]: https://play.flow.com/8b28da4e-0235-499f-8653-1f55e1b3b725
547547
[play.flow.com/b999f656-5c3e-49fa-96f2-5b0a4032f4f1]: https://play.flow.com/b999f656-5c3e-49fa-96f2-5b0a4032f4f1

docs/tutorial/04-capabilities.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
---
2-
archived: false
3-
draft: false
4-
title: Capabilities
5-
description: An introduction to capabilities and how they interact with resources in Cadence
6-
date: 2024-12-11
2+
title: Capabilities and Entitlements
3+
description: An introduction to capabilities, entitlements, and how they interact with resources in Cadence
74
meta:
85
keywords:
96
- tutorial
@@ -12,19 +9,19 @@ meta:
129
- Resources
1310
- Capabilities
1411
- Capability
12+
- Entitlement
13+
- Entitlements
1514
tags:
1615
- reference
1716
- cadence
1817
- tutorial
19-
socialImageTitle: Cadence Resources
20-
socialImageDescription: Capability smart contract image.
2118
---
2219

2320
This tutorial builds on your understanding of [accounts] and [resources]. You'll learn how to interact with resources using [capabilities] and [entitlements].
2421

2522
:::tip[Reminder]
2623

27-
In Cadence, resources are a composite type like a struct or a class, but with some **special rules**:
24+
In Cadence, resources are a composite type like a `struct` or a class in other languages, but with some **special rules**:
2825

2926
- Each instance of a resource can only exist in exactly one location and cannot be copied.
3027
- Resources must be explicitly moved from one location to another when accessed.
@@ -48,12 +45,14 @@ If you're working on an app that allows users to exchange tokens, you'll want di
4845

4946
:::info
5047

51-
In Cadence, users have complete control over their storage, and their storage is tied directly to their accounts. This feature allows amazing benefits including peer-to-peer transfers of property and it being impossible to accidentally burn an asset by sending it to an unused address. The one mixed blessing is that you can't airdrop tokens or NFTs without the recipient signing a transaction. Less spam, but you'll need to use a claim mechanism if the recipient doesn't already have a vault for your asset.
48+
In Cadence, users have complete control over their storage, and their storage is tied directly to their accounts. This feature allows amazing benefits including peer-to-peer transfers of property and it being impossible to accidentally burn an asset by sending it to an unused address. The one mixed blessing is that you can't airdrop tokens or NFTs without the recipient signing a transaction. Less spam, but you'll need to use a claim mechanism if the recipient doesn't already have a vault for your asset.
5249

5350
:::
5451

5552
Capabilities and entitlements are what allows for this detailed control of access to owned assets. They allow a user to indicate which of the functionality of their account and owned objects should be accessible to themselves, their trusted friends, and the public.
5653

54+
![Capabilities and Entitlements](./capabilities-entitlements.jpg)
55+
5756
For example, a user might want to allow a friend of theirs to use some of their money to spend. In this case, they could create an entitled capability that gives the friend access to only this part of their account, instead of having to hand over full control.
5857

5958
Another example is when a user authenticates a trading app for the first time, the trading app could ask the user for a capability object that allows the app to access the trading functionality of a user's account so that the app doesn't need to ask the user for a signature every time it wants to do a trade. The user can choose to empower the app, and that app alone, for this functionality and this functionality alone.
@@ -68,7 +67,7 @@ Next, you'll write a script that anyone can use that links to borrow a [referenc
6867

6968
## Creating capabilities and references to stored resources
7069

71-
Continue working with your code from the previous tutorial. Alternately, open a fresh copy here: [play.flow.com/6f74fe85-465d-4e4f-a534-1895f6a3c0a6].
70+
Continue working with your code from the previous tutorial. Alternately, open a fresh copy here: [https://play.flow.com/8b28da4e-0235-499f-8653-1f55e1b3b725].
7271

7372
If you started with the playground linked above, be sure to deploy the `HelloResource` contract with account `0x06` and call the `Create Hello` transaction, also with `0x06`.
7473

@@ -78,20 +77,24 @@ To prepare:
7877

7978
1. Create a new transaction called `Create Link`.
8079
1. Import `HelloResource` and stub out a `transaction` with a `prepare` phase.
80+
8181
- Cadence allows for static analysis of imported contracts. You'll get errors in the transactions and scripts that import `HelloResource` from `0x06` if you haven't deployed that contract.
82+
8283
```cadence create_link.cdc
8384
import HelloResource from 0x06
84-
85+
8586
transaction {
8687
prepare() {
8788
// TODO
8889
}
8990
}
9091
```
92+
9193
1. Pass an `&Account` reference into `prepare` with the capabilities needed to give the `transaction` the ability to create and publish a capability:
94+
9295
```cadence create_link.cdc
9396
import HelloResource from 0x06
94-
97+
9598
transaction {
9699
prepare(account: auth(
97100
IssueStorageCapabilityController,
@@ -210,40 +213,46 @@ Now that you've published the capability with `public` `access`, **anyone** who
210213

211214
1. Create a script called `GetGreeting`.
212215
1. Import `HelloResource` and give it public `access`. To avoid syntax errors while writing the function, you may wish to add a temporary and obvious `return` value:
216+
213217
```cadence GetGreeting.cdc
214218
import HelloResource from 0x06
215-
219+
216220
access(all) fun main(): String {
217221
// TODO
218222
return "TODO";
219223
}
220224
```
225+
221226
- You'll need a reference to the public account object for the `0x06` account to be able to access public capabilities within it.
227+
222228
1. Use `getAccount` to get a reference to account `0x06`. Hardcode it for now:
223229
```cadence
224230
let helloAccount = getAccount(0x06)
225231
```
226232
- Addresses are **not** strings and thus do **not** have quotes around them.
227233
1. Use `borrow` to borrow the public capability for your `Create Link` transaction saved in `/public/HelloAssetTutorial`.
234+
228235
- Your script should return `helloReference.hello()`.
229236
- You've already borrowed something before. Try to implement this on your own. **Hint:** this time, you're borrowing a `capability` from the account, **not** something from `storage`. Don't forget to handle the case where the object can't be found!
230237
<dl><dd><em>You should end up with a script similar to:</em></dd></dl>
238+
231239
```cadence GetGreeting.cdc
232240
import HelloResource from 0x06
233-
241+
234242
access(all) fun main(): String {
235243
let helloAccount = getAccount(0x06)
236-
244+
237245
let helloReference = helloAccount
238246
.capabilities
239247
.borrow<&HelloResource.HelloAsset>(/public/HelloAssetTutorial)
240248
?? panic("Could not borrow a reference to the HelloAsset capability")
241-
249+
242250
return helloReference.hello()
243251
}
244252
```
253+
245254
1. Use `Execute` to execute your script.
246-
<dl><dd><em>You'll see `"Hello, World!"` logged to the console.</em></dd></dl>
255+
<dl><dd><em>You'll see `"Hello, World!"` logged to the console.</em></dd></dl>
247256

248257
Note that scripts don't need any authorization and can only access public information. You've enabled the user to make this capability public through the transaction you wrote and they signed. **Anyone** can write their own scripts to interact with your contracts this way!
249258

@@ -309,4 +318,4 @@ Reference solutions are functional, but may not be optimal.
309318
[Cadence Best Practices document]: ../design-patterns.md
310319
[Anti-patterns document]: ../anti-patterns.md
311320
[Reference Solution]: https://play.flow.com/6f74fe85-465d-4e4f-a534-1895f6a3c0a6
312-
[play.flow.com/6f74fe85-465d-4e4f-a534-1895f6a3c0a6]: https://play.flow.com/6f74fe85-465d-4e4f-a534-1895f6a3c0a6
321+
[https://play.flow.com/8b28da4e-0235-499f-8653-1f55e1b3b725]: https://play.flow.com/8b28da4e-0235-499f-8653-1f55e1b3b725
74.7 KB
Loading

0 commit comments

Comments
 (0)