> ## Documentation Index
> Fetch the complete documentation index at: https://docs.peaq.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Data streams

> Sell machine data with peaqOS Stream: signed packages, tamper-evident chunk chains, per-chunk encryption, and buyer access grants. Plaintext stays protected.

Selling machine data has a trust problem on both sides. A buyer needs to know the data really came from a specific machine and was not tampered with, *before* paying. A seller needs to hand over exactly what was bought and nothing more, without leaking the rest or trusting a middleman to hold the plaintext. [Stream](https://docs.peaq.xyz/peaqos/functions/stream.md) solves both with the machine's signature (Using your private key to produce a cryptographic stamp proving you approved a specific action, without ever revealing the key.), verifiable hashes, and per-chunk encryption, not with shared secrets or a trusted intermediary.

<details><summary>Key terms in this guide</summary></details>

## Roles

| Role | Description |
| :- | :- |
| **Machine** | The robot or device generating data. Holds a signing key. Packages it signs with the SDK carry a DID (A globally unique, self-owned ID for a machine that lives on the chain and isn't issued by any single company. peaqID is peaq's version: did:peaq:\<machine ID as a decimal> for a machine; did:peaq:0x\<address> names an account, such as an operator.) of the form `did:peaq:0x<signing address>`; the SDK package verifier does not accept the decimal `did:peaq:<machine id>` DID that [Activate](https://docs.peaq.xyz/peaqos/functions/activate.md) issues. |
| **Machine Owner / Proxy Operator** | Sets the data policy, runs the Edge Agent (Software that runs on the machine itself (as a ROS 2 node) and signs, encrypts, and ships the data it produces. The on-machine half of Stream.), lists data for sale, and configures recovery recipients. |
| **Context Provider (buyer)** | Buys access, verifies it, and normalizes the data into datasets for consumption such as AI training. DataHive is one. |
| **peaqOS backend** | Stores policies, manifests, receipts, and access grants, and emits audit events. It does **not** store raw plaintext data. |

## The trust model

**1. Signed data packages.** As data is captured, the machine bundles it with its identity (DID, timestamp, schema version, sequence number) and signs it. Anyone can verify the signature, with no account or login. For packages signed with the SDK, the verifier recovers the EIP-191 signer and compares it with the address in the package's `did:peaq:0x...` DID. The ROS 2 Edge Agent signs its envelopes with Ed25519 instead, and those are checked against the agent's Ed25519 public key. Field rules are applied *before* signing, so a buyer can detect tampering without the seller having to reveal protected fields.

**2. Field-level rules.** A Data Event Map (The policy file a machine owner writes to control what streams out: which topics to read, which fields to keep, drop, or encrypt, and where the signed data goes.) defines, per topic and field, whether to `include`, `exclude`, `encrypt`, or `anonymize` (by `hash`, `generalize`, or `redact`). Sensitive values are protected before they ever leave the machine.

**3. Chunks and chunk chains.** Continuous data is grouped into bounded chunks (A bounded, individually encrypted slice of a data stream (by time window or size). The unit a buyer actually purchases and decrypts.): the SDK splits input into fixed-size chunks (256 KiB by default), and the Edge Agent writes one chunk per captured message. Each chunk links to the previous one, forming a chunk chain (A run of chunks linked in order, each referencing the one before it, so missing, reordered, or edited chunks are detectable. Tamper-evidence for a continuous stream.): reordering, gaps, or edits become detectable. A chunk is the unit a buyer purchases.

**4. Per-chunk encryption.** Each chunk is encrypted under its own random key. That key is then wrapped separately for each authorized recipient using their public key. Granting access to a new buyer wraps the same chunk key to *their* key. The chunk data is never re-encrypted, and no master key is shared. This is envelope encryption (Encrypt the data once with a random key, then lock that key separately for each authorized reader. Granting a buyer access re-locks the key to their public key; the data itself is never re-encrypted.).

**5. Manifests.** Each chunk carries a manifest (A record describing a chunk: its hashes, storage location, and encryption details, without the data itself. The machine signs the chunk's encrypted data hash; buyers check the signature and hashes before trusting or buying.) (the chunk envelope) recording its hashes (ciphertext and plaintext), storage reference, and encryption metadata, never the data itself. The machine's Ed25519 signature covers the ciphertext hash, the chunk ID links that hash to the previous chunk, and the plaintext hash and key commitment are checked when a recipient decrypts. The owner lists chunks for sale; buyers verify the signatures and hashes before paying.

## The chunk envelope

A chunk is stored and shared as a self-describing envelope: the encrypted bytes plus everything needed to verify and (with the right key) decrypt them.

```json theme={"theme":{"light":"github-light-default","dark":"github-dark"}}
{
  "schemaVersion": "peaq.stream.chunks.v1",
  "chunkId": "…",
  "previousChunkId": null,
  "index": 0,
  "encryptedDataHash": "0x…",
  "plaintextHash": "0x…",
  "storageRef": "walrus://…",
  "encryption": {
    "algorithm": "xchacha20-poly1305",
    "nonce": "…",
    "keyCommitment": "sha256:…",
    "keyRecipients": [
      {
        "recipientId": "…",
        "recipientType": "owner",
        "algorithm": "x25519-sealedbox",
        "publicKeyHex": "…",
        "wrappedKeyHex": "…"
      }
    ]
  },
  "signature": { "algorithm": "ed25519", "machineDid": "did:peaq:0x…", "keyId": "did:peaq:0x…#keys-1", "publicKeyHex": "…", "value": "…" }
}
```

The encrypted bytes live off-chain: local disk, owner storage, Walrus (A decentralized storage network where encrypted data chunks can be parked, referenced by walrus\:// links. The data stays off the blockchain; only its reference and fingerprint are tracked on-chain.) (`walrus://`), or a cloud adapter. The backend tracks only the manifest: hashes, storage reference, and the per-recipient wrapped keys. The envelope's `keyRecipients` are the owner, operator, and machine, and are never modified after the chunk is built. An access grant is a separate buyer access entry that wraps the same chunk key to the buyer's key, so no data is re-encrypted.

## Buyer and seller flow

**1. Capture and chunk**

The Edge Agent captures allowed topics, applies field rules, and writes each captured message as an encrypted chunk with its manifest.

**2. Discover and verify**

The seller hands the buyer the listing and chunk IDs. The buyer fetches the chunk envelopes, verifies the machine's chunk signatures and chain links, and checks the chunk hashes before paying.

**3. Pay and grant access**

The buyer submits their public key and pays. After payment, an access grant (What a buyer receives after paying: the chunk keys they bought, each locked to their public key. They unlock with their private key and decrypt only those chunks.) re-wraps the purchased chunk keys to the buyer's key. The backend records the grant and an audit event.

**4. Deliver and decrypt**

The buyer receives the encrypted chunks, fetched from storage or streamed [directly machine-to-machine over P2P](https://docs.peaq.xyz/peaqos/sdk-reference/stream-distribution.md#p2p-delivery), unwraps the chunk keys with their private key, and decrypts only the chunks they bought. Over P2P, every chunk's hash, signature, and chain link is verified *before* decryption.

Owner and operator **recovery recipients** can be configured before chunks are created: the chunk key is also wrapped to their keys, so data stays recoverable if the machine goes offline.

## Build it

Signing, chunk chains, encryption, and key wrapping are exposed by the [`stream` SDK module](https://docs.peaq.xyz/peaqos/sdk-reference/stream) for JavaScript and Python. Purchases, payment rails, and the S3 and P2P delivery channels are the [distribution surface](https://docs.peaq.xyz/peaqos/sdk-reference/stream-distribution.md). The on-machine [Edge Agent](https://docs.peaq.xyz/peaqos/sdk-reference/stream-agent.md) runs this pipeline from a Data Event Map.

## Related

* [Stream function](https://docs.peaq.xyz/peaqos/functions/stream.md)
* [Stream SDK reference](https://docs.peaq.xyz/peaqos/sdk-reference/stream.md)
* [peaqID](https://docs.peaq.xyz/peaqos/concepts/peaqid.md)
* [Machine Markets](https://docs.peaq.xyz/peaqos/concepts/machine-markets.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.