npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@wisehodl/roots

v0.2.1

Published

A minimal Nostr protocol library for typescript

Downloads

20

Readme

TS-Roots - Nostr Protocol Library for TypeScript

Source: https://git.wisehodl.dev/jay/ts-roots

Mirror: https://github.com/wisehodl/ts-roots

What this library does

ts-roots is a consensus-layer Nostr protocol library for TypeScript. It only provides primitives that define protocol compliance:

  • Event Structure
  • Serialization
  • Cryptographic Signatures
  • Subscription Filters

What this library does not do

ts-roots serves as a foundation for other libraries and applications to implement higher level abstractions of the Nostr protocol on top of it, including message transport, semantic event definitions, event storage mechanisms, and user interfaces.

ts-roots prioritizes correctness and clarity over optimization and efficiency. For high performance applications, it is recommended to implement optimizations in a separate library or in the application which requires them.

Installation

  1. Add ts-roots to your project:
npm install @wisehodl/roots
  1. Import it:
import * as events from '@wisehodl/roots/events';
import * as filters from '@wisehodl/roots/filters';
import * as keys from '@wisehodl/roots/keys';
import * as constants from '@wisehodl/roots/constants';
import * as errors from '@wisehodl/roots/errors';

import type { Event } from '@wisehodl/roots/events';
import type { Filter } from '@wisehodl/roots/filters';

Usage Examples

Key Management

Generate a new keypair

const privateKey = keys.generatePrivateKey();
const publicKey = keys.getPublicKey(privateKey);

Derive public key from existing private key

const privateKey = "f43a0435f69529f310bbd1d6263d2fbf0977f54bfe2310cc37ae5904b83bb167";
const publicKey = keys.getPublicKey(privateKey);
// publicKey: "cfa87f35acbde29ba1ab3ee42de527b2cad33ac487e80cf2d6405ea0042c8fef"

Event Creation and Signing

Create and sign a complete event

// 1. Build the event structure
const event: Event = {
  pubkey: publicKey,
  created_at: Math.floor(Date.now() / 1000),
  kind: 1,
  tags: [
    ["e", "5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"],
    ["p", "91cf9b32f3735070f46c0a86a820a47efa08a5be6c9f4f8cf68e5b5b75c92d60"],
  ],
  content: "Hello, Nostr!",
  id: "",
  sig: "",
};

// 2. Compute the event ID
const id = events.getID(event);
event.id = id;

// 3. Sign the event
const sig = events.sign(id, privateKey);
event.sig = sig;

Serialize an event for ID computation

// Returns canonical JSON: [0, pubkey, created_at, kind, tags, content]
const serialized = events.serialize(event);

Compute event ID manually

const id = events.getID(event);
// Returns lowercase hex SHA-256 hash of serialized form

Event Validation

Validate complete event

// Checks structure, ID computation, and signature
try {
  events.validate(event);
} catch (err) {
  console.log(`Invalid event: ${err.message}`);
}

Validate individual aspects

// Check field formats and lengths
try {
  events.validateStructure(event);
} catch (err) {
  console.log(`Malformed structure: ${err.message}`);
}

// Verify ID matches computed hash
try {
  events.validateID(event);
} catch (err) {
  console.log(`ID mismatch: ${err.message}`);
}

// Verify cryptographic signature
try {
  events.validateSignature(event);
} catch (err) {
  console.log(`Invalid signature: ${err.message}`);
}

Event JSON

Marshal event to JSON

const jsonString = JSON.stringify(events.toJSON(event));
// Standard JSON.stringify works with events.toJSON()

Unmarshal event from JSON

const event = events.fromJSON(JSON.parse(jsonString));

// Validate after unmarshaling
try {
  events.validate(event);
} catch (err) {
  console.log(`Received invalid event: ${err.message}`);
}

Filter Creation

Basic filter with standard fields

const since = Math.floor(Date.now() / 1000) - (24 * 60 * 60);
const limit = 50;

const filter: Filter = {
  ids: ["abc123", "def456"],  // Prefix match
  authors: ["cfa87f35"],      // Prefix match
  kinds: [1, 6, 7],
  since: since,
  limit: limit,
};

Filter with tag conditions

const filter: Filter = {
  kinds: [1],
  tags: {
    e: ["5c83da77af1dec6d7289834998ad7aafbd9e2191396d75ec3cc27f5a77226f36"],
    p: ["91cf9b32f3735070f46c0a86a820a47efa08a5be6c9f4f8cf68e5b5b75c92d60"],
  },
};

Filter with extensions (custom fields)

// Extensions allow arbitrary JSON fields beyond the standard filter spec.
// For example, this is how to implement non-standard filters like 'search'.
const filter: Filter = {
  kinds: [1],
  extensions: {
    search: "bitcoin",
  },
};

// Extensions are preserved during marshal/unmarshal but ignored by matches().
// Storage/transport layers can inspect extensions to implement custom behavior.

Filter Matching

Match single event

const filter: Filter = {
  authors: ["cfa87f35"],
  kinds: [1],
};

if (filters.matches(filter, event)) {
  // Event satisfies all filter conditions
}

Filter event collection

const since = Math.floor(Date.now() / 1000) - (60 * 60);
const filter: Filter = {
  kinds: [1],
  since: since,
  tags: {
    p: ["abc123", "def456"],  // OR within tag values
  },
};

const matches = eventCollection.filter(event => filters.matches(filter, event));

Filter JSON

Marshal filter to JSON

const filter: Filter = {
  ids: ["abc123"],
  kinds: [1],
  tags: {
    e: ["event-id"],
  },
  extensions: {
    search: "nostr",
  },
};

const jsonString = JSON.stringify(filters.toJSON(filter));
// Result: {"ids":["abc123"],"kinds":[1],"#e":["event-id"],"search":"nostr"}

Unmarshal filter from JSON

const jsonData = `{
  "authors": ["cfa87f35"],
  "kinds": [1],
  "#e": ["abc123"],
  "since": 1234567890,
  "search": "bitcoin"
}`;

const filter = filters.fromJSON(JSON.parse(jsonData));

// Standard fields populated: authors, kinds, since
// Tag filters populated: tags.e = ["abc123"]
// Unknown fields populated: extensions.search = "bitcoin"

Extensions field behavior

The extensions field captures any JSON properties not recognized as standard filter fields or tag filters. This design allows the core library to remain frozen while storage and transport layers implement custom filtering behavior.

Standard fields: ids, authors, kinds, since, until, limit

Tag filters: Any key starting with # (e.g., #e, #p, #emoji)

Extensions: Everything else

During marshaling, extensions merge into the output JSON. During unmarshaling, unrecognized fields populate extensions. The matches() method ignores extensions, and the library expects higher protocol layers to implement their usage.

Example implementing search filter:

const filter: Filter = {
  kinds: [1],
  extensions: {
    search: "bitcoin",
  },
};

// In a storage layer (not this library):
if (filter.extensions?.search) {
  const searchTerm = filter.extensions.search as string;
  // Apply full-text search using searchTerm
}

Testing

This library contains a comprehensive suite of unit tests. Run them with:

npm test

Or for a single run:

npm run test:run