Skip to content

Commit 01657ec

Browse files
author
Ferenc Sárai
committed
feat: act upload, download, createGrantees, listGrantees, patchGrantees
1 parent d9fd28a commit 01657ec

File tree

1 file changed

+127
-38
lines changed

1 file changed

+127
-38
lines changed

docs/user-documentation/act.md

Lines changed: 127 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,81 +14,170 @@ import TabItem from '@theme/TabItem'
1414
The [Access Control Trie (ACT)](https://solarpunk.buzz/introducing-the-access-control-trie-act-in-swarm/) is an essential feature designed to manage access control in Swarm’s decentralized storage infrastructure. It enables __publishers__ to grant or revoke access to specific content at the chunk level using encrypted session keys. This guide will walk you through the key concepts and practical aspects of using __ACT__ to protect your data in Swarm.
1515

1616
:::warning Postage stamps
17-
18-
Uploading to Swarm network require to have Postage stamps for every write operation. To understand better what does it mean see [Bee docs - Keep your data alive](https://docs.ethswarm.org/docs/access-the-swarm/keep-your-data-alive).
17+
Uploading to the Swarm network requires Postage stamps for every write operation. To understand this better, see [Bee docs - Keep your data alive](https://docs.ethswarm.org/docs/access-the-swarm/keep-your-data-alive). In the following examples, we assume that we already have a `postageBatchId`.
1918
:::
2019

21-
### Data
20+
We also need an instance of Bee to interact with the Bee node.
21+
22+
```ts
23+
import { Bee } from "@ethersphere/bee-js"
2224

23-
The same data structures can be handled with ACT as without ACT.
25+
const bee = new Bee('http://localhost:1633')
26+
```
2427

2528
### Upload with ACT
2629

27-
When uploading, we can indicate that we are uploading with ACT in the optional requestOptions of bee-js uploadData method.
30+
When uploading, we can specify that we are using ACT in the optional `requestOptions` of the `bee-js` `uploadData` method.
2831

2932
```ts
3033
const beeRequestOptionsUpload = {
3134
act: true,
3235
}
3336
const uploadResultACT = await bee.uploadData(
34-
postageBatchId, "Bee is awesome with ACT!",
35-
beeRequestOptionsUpload)
37+
postageBatchId,
38+
"Bee is awesome with ACT!",
39+
beeRequestOptionsUpload
40+
)
3641
console.log(uploadResultACT)
3742
```
3843

39-
The return value provides a reference and a history reference. Both will be needed for the download.
44+
The return value provides a `reference` and a `historyAddress`. Both will be needed for the download.
4045

41-
```json, title="uploadResultACT"
46+
```json title="uploadResultACT"
4247
{
43-
reference: '97132e8e17831dfa220e73ee083bd82819aaeffce0aaf7e1e0abf8135fcfd2fc',
44-
tagUid: 14,
45-
historyAddress: 'c6cabe3a3b7879ddb182277f3037c02002d4ce33280007cac580ac9256be20ea'
48+
"reference": "97132e8e17831dfa220e73ee083bd82819aaeffce0aaf7e1e0abf8135fcfd2fc",
49+
"tagUid": 14,
50+
"historyAddress": "c6cabe3a3b7879ddb182277f3037c02002d4ce33280007cac580ac9256be20ea"
4651
}
4752
```
4853

4954
:::info
50-
During the upload, the publisher will be the uploading node.
55+
During the upload process, the node performing the upload acts as the __publisher__. The __public key__ of this uploading node will be used as the publisher's public key.
5156
:::
5257

53-
### Get Node Public Key
58+
### Download with ACT
5459

55-
```js
56-
import { Bee } from "@ethersphere/bee-js"
60+
When downloading, we can specify that we are using __ACT__ in the optional `requestOptions` of the `bee-js` `downloadData` method.
5761

58-
const bee = new Bee('http://localhost:1633')
59-
const addr = await bee.getNodeAddresses();
62+
:::warning
63+
Downloading is only possible with the knowledge of the __publisher's__ public key.
64+
:::
65+
66+
```ts
67+
const addr = await bee.getNodeAddresses()
6068

6169
// node public key (publisher public key)
6270
const pubk = addr.publicKey
63-
```
6471

65-
### Download with ACT
72+
const beeRequestOptionsDownload = {
73+
baseURL: BEE_API_URL,
74+
timeout: 0, // false converted to number
75+
headers: {
76+
'swarm-act': 'true',
77+
'swarm-act-publisher': pubk, // publisher public key
78+
'swarm-act-history-address': uploadResultACT.historyAddress,
79+
},
80+
}
81+
82+
const retrievedData = await bee.downloadData(uploadResultACT.reference, beeRequestOptionsDownload)
83+
console.log(await retrievedData.text()) // prints 'Bee is awesome with ACT!'
84+
```
6685

67-
When downloading, we can indicate that we are downloading with __ACT__ in the optional requestOptions of `bee-js` `downloadData` method.
86+
### Create Grantees
6887

69-
:::warning
70-
The download is only possible with the knowledge of the publisher's public key.
71-
:::
88+
To download from another node, we need to create a grantee list for the uploaded data that includes the public key of the downloading node. In the following example, we create a grantee list with three public keys. This list can be modified later using the `patchGrantees` method.
7289

7390
```ts
74-
const beeRequestOptionsDownload = {
75-
baseURL: BEE_API_URL,
76-
timeout: 0, // false converted to number
77-
headers: {
78-
'swarm-act': 'true',
79-
'swarm-act-publisher': pubk, // publisher public key
80-
'swarm-act-history-address': uploadResultACT.historyAddress,
81-
},
82-
}
83-
84-
const retrievedData = await bee.downloadData(uploadResultACT.reference, beeRequestOptionsDownload)
85-
console.log(retrievedData) // prints 'Bee is awesome with ACT!'
91+
const granteePublicKeys = [
92+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e8",
93+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e9",
94+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12ee"
95+
]
96+
97+
// Create Grantees
98+
const granteeResult = await bee.createGrantees(postageBatchId, granteePublicKeys)
99+
console.log(granteeResult)
86100
```
87101

88-
### Create Grantee
102+
```json title="granteeResult"
103+
{
104+
"status": 201,
105+
"statusText": "Created",
106+
"ref": "810b802516f3f0d1ded180d19fdf27bbc05b95a5b7ec8448b5a2d90819e06bed8fdf0697efad7074e66f52679a3aa51010d1897f4ce00918f8fd938b0ff35c3a",
107+
"historyref": "4feb7fc56dcea1acaa74c1fb980156d6db9667b223809c082d69542545c67faf"
108+
}
109+
```
89110

90111
### List Grantees
91112

113+
```ts
114+
// List Grantees
115+
const getGranteesResult = await bee.getGrantees(granteeResult.ref)
116+
console.log(getGranteesResult)
117+
```
118+
119+
```json title="getGranteesResult"
120+
{
121+
"status": 200,
122+
"statusText": "OK",
123+
"data": [
124+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e8",
125+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e9",
126+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12ee"
127+
]
128+
}
129+
```
130+
92131
### Patch Grantees
93132

94-
### List Grantees after patch
133+
The grantee list can be modified with `patchGrantees`. In our example, we add a new element and remove the last two elements from the `granteePublicKeys` list.
134+
135+
```ts
136+
// Patch Grantees
137+
const patchGrantees = {
138+
add: [
139+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e7"
140+
],
141+
revoke: [
142+
granteePublicKeys[1],
143+
granteePublicKeys[2],
144+
]
145+
}
146+
// A short delay is needed if we call it immediately after createGrantees.
147+
await new Promise(resolve => setTimeout(resolve, 1500))
148+
const granteeResultAfterPatch = await bee.patchGrantees(
149+
postageBatchId,
150+
granteeResult.ref,
151+
granteeResult.historyref,
152+
patchGrantees
153+
)
154+
console.log(granteeResultAfterPatch)
155+
```
156+
157+
```json title="granteeResultAfterPatch"
158+
{
159+
"status": 200,
160+
"statusText": "OK",
161+
"ref": "ae9fd8e0d9158ab9a433bacfde9109bd0545e31a0d8e4f592a5a558e842de59a86f73e08a594874fe38d6a68cfa636a729dcc73b746083b12a384b6578ac5dce",
162+
"historyref": "0a0f709ed9d5ecd46434b8c62aeefc84bffeed7a80741c453d27ffdf67359fff"
163+
}
164+
```
165+
166+
After the patch, the grantee list will have a new reference.
167+
168+
```ts
169+
// List Grantees
170+
const getGranteesResultAfterPatch = await bee.getGrantees(granteeResultAfterPatch.ref)
171+
console.log(getGranteesResultAfterPatch)
172+
```
173+
174+
```json title="getGranteesResultAfterPatch"
175+
{
176+
"status": 200,
177+
"statusText": "OK",
178+
"data": [
179+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e8",
180+
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e7"
181+
]
182+
}
183+
```

0 commit comments

Comments
 (0)