diff --git a/docs/user-documentation/pss.md b/docs/user-documentation/pss.md index ef8fc08b..d9648dd7 100644 --- a/docs/user-documentation/pss.md +++ b/docs/user-documentation/pss.md @@ -1,6 +1,5 @@ --- title: Postal Service over Swarm -hide_title: true id: pss slug: /pss sidebar_label: Postal Service over Swarm @@ -8,3 +7,178 @@ sidebar_label: Postal Service over Swarm import Tabs from '@theme/Tabs' import TabItem from '@theme/TabItem' + +Swarm provides the ability to send messages that appear to be normal Swarm traffic, but are in fact messages that may be received and decrypted to reveal their content only to specific nodes that were intended to receive them. + +PSS provides a pub-sub facility that can be used for a variety of tasks. Nodes are able to listen to messages received for a specific topic in their nearest neighbourhood and create messages destined for another neighbourhood which are sent over the network using Swarm's usual data dissemination protocols. + +The intended use of PSS is to communicate privately with a publicly known identity (to for example initiate further communication directly). Due to the cost of mining the trojan chunks, it is not recommended to use as an instant messaging system. + +## Getting the relevant data +When you start `bee`, you may find all the necessary information in the log: +```sh +INFO using existing swarm network address: 9e2ebf266369090091620db013aab164afb1574aedb3fcc08ce8dc6e6f28ef54 +INFO swarm public key 03e0cee7e979fa99350fc2e2f8c81d857b525b710380f238742af269bb794dfd3c +INFO pss public key 02fa24cac43531176d21678900b37bd800c93da3b02c5e11572fb6a96ec49527fa +INFO using ethereum address 5f5505033e3b985b88e20616d95201596b463c9a +``` +Let's break it down: +- **Ethereum address** is the public address of your node wallet. Together with the corresponding private key, it is used for things such as making Ethereum transactions (receiving and sending ETH and BZZ); receiving, claiming and singing cheques and the Swarm network address is also derived from it. +- The **Swarm network address** defines your location in the kademlia and within the context of PSS is used for addressing the trojan chunks to you. In other words, others may use it to send you a message. +- **PSS public key** can be used by others to encrypt their messages for you. + + + +## Sending message + +To send data simply define a topic, prefix of the recipient's swarm network address (we recommend 4-6 character prefix length) and the data to be send. +:::caution Your communication privacy may be at risk +When sending PSS messages without encryption key, any Bee node through which the trojan chunk passes would be able to read the message. +::: + + + + +```ts +/** + * @param {string} topic + * @param {string} targetPrefix + * @param {string|Uint8Array} data + * @param {string} encryptionKey + */ +bee.pssSend('topic', '9e2e', 'Hello!') +``` + + + + +```js +/** + * @param {string} topic + * @param {string} targetPrefix + * @param {string|Uint8Array} data + * @param {string} encryptionKey + */ +bee.pssSend('topic', '9e2e', 'Hello!') +``` + + + + +If you want to encrypt the message, you may provide the recipient's PSS public key. + + + + +```ts +bee.pssSend( + 'topic', + '9e2e', + 'Encrypted Hello!', + '02fa24cac43531176d21678900b37bd800c93da3b02c5e11572fb6a96ec49527fa', +) +``` + + + + +```js +bee.pssSend( + 'topic', + '9e2e', + 'Encrypted Hello!', + '02fa24cac43531176d21678900b37bd800c93da3b02c5e11572fb6a96ec49527fa', +) +``` + + + + +## Retrieving message +As a recipient, you have two ways how to receive the message. If you are expecting one off message (which is the intended PSS use case to exchange credentials for further direct communication), you can use the `pssReceive` method. + + + + +```ts +const message = await bee.pssReceive('topic') + +console.log(new TextDecoder("utf-8").decode(message)) // prints the received message +``` + + + + +```js +const message = await bee.pssReceive('topic') + +console.log(new TextDecoder("utf-8").decode(message)) // prints the received message +``` + + + + +If you want to subscribe to multiple messagees, use the `pssSubscribe` method. + + + + + +```ts +const handler = { + onMessage: (message: Uint8Array) => {console.log(new TextDecoder("utf-8").decode(message))}, + onError: (error: BeeError) => {console.log(error)} +} + +// Subscribe +const subscription = bee.pssSubscribe('topic', handler) + +// Terminate the subscription +subscription.cancel() +``` + + + + +```js +const handler = { + onMessage: (message: Uint8Array) => {console.log(new TextDecoder("utf-8").decode(message))}, + onError: (error: BeeError) => {console.log(error)} +} + +// Subscribe +const subscription = bee.pssSubscribe('topic', handler) + +// Terminate the subscription +subscription.cancel() +``` + + + diff --git a/sidebars.js b/sidebars.js index d8a9818e..e4086059 100644 --- a/sidebars.js +++ b/sidebars.js @@ -8,7 +8,8 @@ module.exports = { 'user-documentation/getting-started', 'user-documentation/upload-download', 'user-documentation/track-upload', - // 'user-documentation/pss', + 'user-documentation/pss', + // 'user-documentation/feeds', ], collapsed: false },