Skip to content

Commit 1e4adb2

Browse files
feat: soc and feeds documentation (#37)
* feat: soc and feeds documentation * docs: apply suggestions from code review Co-authored-by: Vojtech Simetka <[email protected]> * docs: update feed explanation Co-authored-by: Vojtech Simetka <[email protected]>
1 parent 733160e commit 1e4adb2

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
title: SOC and Feeds
3+
id: soc-and-feeds
4+
slug: /soc-and-feeds
5+
sidebar_label: SOC and Feeds
6+
---
7+
8+
import Tabs from '@theme/Tabs'
9+
import TabItem from '@theme/TabItem'
10+
11+
Swarm provides the ability to store content in content-addressed chunks or Single Owner Chunks (SOC). With single owner chunks, a user can assign arbitrary data to an address and attest chunk integrity with their digital signature.
12+
13+
Feeds are a unique feature of Swarm. They constitute the primary use case for single owner chunks. Feeds can be used for versioning revisions of a mutable resource, indexing sequential updates to a topic, publish the parts to streams, or post consecutive messages in a communication channel to name but a few. Feeds implement persisted pull-messaging and can also be interpreted as a pub-sub system.
14+
15+
Because Feeds are built on top of SOCs, their interfaces in Bee is somewhat similar and they are using common concepts.
16+
17+
## Single Owner Chunks
18+
19+
The address of an SOC is calculated as the hash of an `identifier` and `owner`. The `identifier` is a 32 bytes long arbitrary data, usually expected as a hex string or in a `Uint8Array` type. The `owner` is the Ethereum address of the owner of the chunk, which is 20 bytes expected as a hex string or in a `Uint8Array` type.
20+
21+
### Reading SOCs
22+
23+
In order to read data from an SOC we need to make a reader object for a certain `owner`, then we can download the data with the provided chunk `identifier`.
24+
25+
```js
26+
const owner = '0x8d3766440f0d7b949a5e32995d09619a7f86e632'
27+
const socReader = bee.makeSOCReader(owner)
28+
const identifier = '0000000000000000000000000000000000000000000000000000000000000000'
29+
const soc = await socReader.download(identifier)
30+
const data = soc.payload()
31+
```
32+
33+
### Writing SOCs
34+
35+
When writing an SOC, first we need to make a writer object. Because we need to sign the chunk we need to pass in a `signer` object. The `signer` object can be either an Ethereum private key (as a hex string or `Uint8Array` type) or it can be an instance of the `Signer` interface. The `Signer` interface can be used for integration with 3rd party Ethereum wallet applications, because Swarm uses the same format for signing chunks that Ethereum uses for signing transactions.
36+
37+
```ts
38+
type SyncSigner = (digest: Uint8Array) => Signature
39+
type AsyncSigner = (digest: Uint8Array) => Promise<Signature>
40+
41+
/**
42+
* Interface for implementing Ethereum compatible signing.
43+
*
44+
* @property sign The sign function that can be sync or async
45+
* @property address The ethereum address of the signer
46+
*/
47+
export type Signer = {
48+
sign: SyncSigner | AsyncSigner
49+
address: EthAddress
50+
}
51+
```
52+
53+
:::warning Your communication privacy may be at risk
54+
We suggest to use either ephemeral private keys (e.g. randomly generated) or the `Signer` interface when writing to SOC or Feeds. Never use your real Ethereum private keys here (or in any web applications really) directly because you may lose your funds stored on it.
55+
:::
56+
57+
Using the writer interface is similar to using the reader:
58+
59+
```js
60+
const signer = '0x634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd'
61+
const socWriter = bee.makeSOCWriter(signer)
62+
const identifier = '0000000000000000000000000000000000000000000000000000000000000000'
63+
const data = new Uint8Array([1, 2, 3])
64+
const response = await socWriter.upload(identifier, data)
65+
```
66+
67+
## Feeds
68+
69+
Feeds are an abstraction built on top of SOCs to provide mutable resources on the otherwise immutable data types that Swarm supports.
70+
71+
One of the most common use cases for feeds is to store mutable data in an immutable address. For example, when hosting a website on Swarm, we may want its address stored in ENS but we don't want to pay for changing the reference every time the site is updated.
72+
73+
A feed is defined by its `owner` (see above), a `topic` (which is 32 bytes arbitrary data, usually expected as a hex string or in a `Uint8Array` type) and a `type`. `type` defines how the updates and lookup of the feed index are made (currently only the `sequence` type is supported).
74+
75+
Publishers are the single owners of feed chunks and are the only ones able to post updates to their feed. Posting an update requires (1) constructing the identifier from the topic and the correct index, and (2) signing it concatenated together with the hash of the arbitrary content of the update.
76+
77+
Conversely, users can consume a feed by retrieving the chunk by its address. Retrieving an update requires the consumer to construct the address from the owner’s address and the identifier. To calculate the identifier they need the topic and the appropriate index. For this they need to know the indexing scheme.
78+
79+
Feeds enable Swarm users to represent a sequence of content updates. The content of the update is the payload that the feed owner signs against the identifier. The payload can be a swarm reference from which the user can retrieve the associated data.
80+
81+
### Topic
82+
83+
In Swarm `topic` is a 32-byte long arbitrary byte array. It's possible to choose an arbitrary topic for each application and then knowing someone's (or something's) address it's possible to find their feeds. Also this can be the hash of one or more human readable strings, specifying the topic and optionally the subtopic of the feed. There is a helper function provided for that:
84+
85+
```js
86+
const topic = bee.makeFeedTopic('my-dapp.eth/outbox')
87+
```
88+
89+
### Reading feeds
90+
91+
In order to read data from a feed we need to make a reader object for a certain `type`, `topic` and `owner`, then we can download the latest update containing a reference.
92+
93+
```js
94+
const topic = '0000000000000000000000000000000000000000000000000000000000000000'
95+
const owner = '0x8d3766440f0d7b949a5e32995d09619a7f86e632'
96+
const feedReader = bee.makeFeedReader('sequence', topic, owner)
97+
const feedUpdate = await feedReader.download()
98+
console.log(feedUpdate.reference) // prints the latest reference stored in the feed
99+
```
100+
101+
### Writing feeds
102+
103+
When writing a feed, typically an immutable content is uploaded first and then its reference is updated in the feed. The `signer` here is the same as with [writing the SOCs](#writing-socs) (with the same caveats!).
104+
105+
```js
106+
const data = new Uint8Array([1, 2, 3])
107+
const reference = await bee.uploadData(data)
108+
const topic = '0000000000000000000000000000000000000000000000000000000000000000'
109+
const signer = '0x634fb5a872396d9693e5c9f9d7233cfa93f395c093371017ff44aa9ae6564cdd'
110+
const feedWriter = bee.makeFeedWriter('sequence', topic, signer)
111+
const response = await feedWriter.upload(reference)
112+
```
113+
114+
### Using feed manifest
115+
116+
One of the most common use cases for feeds is to store mutable data in an immutable address. For example, when hosting a website on Swarm, we may want its address stored in ENS but we don't want to pay for changing the reference every time the site is updated.
117+
118+
For this Swarm provides a feature called `feed manifests`. It is a content addressed chunk which stores the definition of a feed (the `type`, the `topic` and the `owner`) and when it is looked up on the `bzz` endpoint, Swarm recognizes that it refers to a feed and continues the lookup according to the parameters of the feed.
119+
120+
```js
121+
const topic = '0000000000000000000000000000000000000000000000000000000000000000'
122+
const owner = '0x8d3766440f0d7b949a5e32995d09619a7f86e632'
123+
const reference = bee.createFeedManifest('sequence', topic, owner)
124+
```
125+
126+
This creates the feed manifest chunk on Swarm. You can use the returned reference to look up with the `/bzz` endpoint or use it with ENS.

sidebars.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
'user-documentation/upload-download',
1010
'user-documentation/track-upload',
1111
'user-documentation/pss',
12-
// 'user-documentation/feeds',
12+
'user-documentation/soc-and-feeds',
1313
],
1414
collapsed: false
1515
},
@@ -28,4 +28,4 @@ module.exports = {
2828
collapsed: true
2929
}
3030
]
31-
};
31+
};

0 commit comments

Comments
 (0)