Secure Scuttlebutt TypeScript Types

2.8.0 · active · verified Wed Apr 22

ssb-typescript provides comprehensive TypeScript type definitions for the Secure Scuttlebutt (SSB) protocol, enabling developers to build SSB applications with strong type safety. It defines core SSB concepts such as `FeedId`, `MsgId`, `BlobId`, and various message interfaces like `Msg`, `UnboxedMsg`, and `Content` (which is a union of specific content types like `PostContent`, `ContactContent`, `VoteContent`, `BlogContent`, and more). The current stable version is 2.8.0. As a type-only package, its release cadence is typically driven by changes in the underlying SSB protocol or common application patterns, rather than frequent functional updates. Its key differentiator is providing a standardized, community-maintained set of types for the decentralized SSB ecosystem, helping to ensure interoperability and reduce development errors when working with SSB data structures.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define common SSB types like `FeedId`, `MsgId`, `PostContent`, and the full `Msg` interface using the provided type definitions.

import { Msg, PostContent, FeedId, MsgId, Content } from 'ssb-typescript';

// Example FeedId, MsgId, BlobId (standard SSB identifiers)
const exampleFeedId: FeedId = '@HXjeG7t2p8zU9gT0t2p8zU9gT0t2p8zU9gT0t2p8zU9gT0.ed25519';
const exampleMsgId: MsgId = '%mS3G7t2p8zU9gT0t2p8zU9gT0t2p8zU9gT0t2p8zU9gT0.sha256';
const exampleRootMsgId: MsgId = '%mS3G7t2p8zU9gT0t2p8zU9gT0t2p8zU9gT0t2p8zU9gT0.sha256';

// Define a PostContent message
const postContent: PostContent = {
  type: 'post',
  text: 'Hello, Secure Scuttlebutt! This is a test post from ssb-typescript.',
  channel: 'testing',
  mentions: [], // Empty array for no mentions
  root: exampleRootMsgId,
};

// Define an SSB Message wrapper
const ssbMessage: Msg<PostContent> = {
  key: exampleMsgId,
  value: {
    previous: '%prevMsgId.sha256',
    author: exampleFeedId,
    sequence: 123,
    timestamp: 1678886400000, // UTC epoch milliseconds
    hash: 'sha256',
    content: postContent,
    signature: 'fake-signature-string-here',
  },
  timestamp: 1678886400000,
};

// Demonstrate generic Content type usage
const genericContent: Content = {
    type: 'contact',
    contact: exampleFeedId,
    following: true
};

console.log('Example SSB Post Message:', JSON.stringify(ssbMessage, null, 2));
console.log('Example generic Content:', JSON.stringify(genericContent, null, 2));

// To compile and run this example:
// 1. Install dependencies: `npm install typescript ssb-typescript ts-node`
// 2. Save the code as `example.ts`.
// 3. Execute: `npx ts-node example.ts`

view raw JSON →