Nostr library

Hex.pmHex DocsLicense

A comprehensive low-level Elixir library implementing the Nostr protocol. Provides structures, parsing, serialization, and cryptographic functions for building Nostr applications.

Features

Installation

Add nostr_lib to your dependencies in mix.exs:

def deps do
[
{:nostr_lib, "~> 0.2.1"}
]
end

Quick Start

Create and Sign an Event

# Generate or use existing private key (32 bytes hex)
private_key = "your_private_key_hex"
# Create a text note (kind 1)
{:ok, event} = Nostr.Event.Note.create("Hello Nostr!", private_key)
# Serialize for sending to relay
json = Nostr.Event.serialize(event)

Parse Incoming Events

# Parse JSON from relay
{:ok, event} = Nostr.Event.parse(json_string)
# Validate event signature
case Nostr.Event.Validator.validate(event) do
{:ok, event} -> # Valid event
{:error, reason, event} -> # Invalid
end

Build Subscription Filters

# Create a filter for text notes from specific authors
filter = %Nostr.Filter{
kinds: [1],
authors: ["pubkey1", "pubkey2"],
limit: 100
}

Bech32 Encoding

# Encode public key as npub
{:ok, npub} = Nostr.Bech32.encode(:npub, pubkey_hex)
# => "npub1..."
# Decode back
{:ok, {:npub, pubkey}} = Nostr.Bech32.decode(npub)

Implemented NIPs

NIPStatusDescriptionEvent Kinds
NIP-01FullBasic protocol0, 1
NIP-02FullFollow list3
NIP-03FullOpenTimestamps1040
NIP-04DeprecatedEncrypted DMs (use NIP-17)4
NIP-05FullDNS-based identifiers-
NIP-09FullEvent deletion5
NIP-13FullProof of Work-
NIP-16FullEvent kind ranges1000-39999
NIP-17FullPrivate direct messages14, 15, 10050
NIP-18DeprecatedReposts6
NIP-19FullBech32 encoding-
NIP-21Fullnostr: URI scheme-
NIP-22FullComments1111
NIP-23FullLong-form content30023, 30024
NIP-25FullReactions7, 17
NIP-28FullPublic chat channels40-44
NIP-29PartialRelay-based groups helpers9000-9022, 39000-39003
NIP-30FullCustom emoji-
NIP-32FullLabeling1985
NIP-36FullSensitive content-
NIP-37FullDraft events31234
NIP-38FullUser statuses30315
NIP-39FullExternal identities-
NIP-42FullRelay authentication22242
NIP-44FullVersioned encryption-
NIP-45PartialEvent counting-
NIP-49FullPrivate key encryption-
NIP-51FullLists10001-10030, 30000-30030
NIP-52FullCalendar31924
NIP-56FullReporting1984
NIP-57FullLightning zaps9734, 9735
NIP-58FullBadges8
NIP-59FullGift wrap13, 1059
NIP-65FullRelay list metadata10002
NIP-77PartialNegentropy message contract-
NIP-94FullFile metadata1063
NIP-98PartialHTTP Auth event semantics27235

Core Modules

ModuleDescription
Nostr.EventCore event struct with create, parse, serialize, sign, validate
Nostr.CryptoCryptographic operations (keys, signing, encryption)
Nostr.MessageWebSocket protocol messages (EVENT, REQ, CLOSE, etc.)
Nostr.FilterSubscription filter building
Nostr.Bech32NIP-19 bech32 encoding/decoding
Nostr.NIP44Versioned encrypted payloads
Nostr.NIP17Private message convenience functions
Nostr.NIP05DNS-based identifier verification

Event Types

Each event kind has a dedicated module in Nostr.Event.*:

KindModuleDescription
0MetadataUser profile metadata
1NoteText notes/posts
3ContactsFollow list
4DirectMessageEncrypted DMs (deprecated)
5DeletionEvent deletion requests
6RepostReposts (deprecated)
7ReactionReactions to events
8BadgeAwardBadge awards
13SealSealed/encrypted events
14PrivateMessagePrivate chat messages
15FileMessageEncrypted file messages
17ExternalReactionReactions to external content
40-44Channel*Public chat channels
1040OpenTimestampsTimestamp attestations
1059GiftWrapGift wrapped events
1063FileMetadataFile metadata
1111CommentThreaded comments
1984ReportContent reports
1985LabelContent labels
9734ZapRequestLightning zap requests
9735ZapReceiptLightning zap receipts
10002RelayListRelay list metadata
10050DMRelayListDM relay preferences
22242ClientAuthClient authentication
27235HttpAuthHTTP authentication
30023ArticleLong-form content
30315UserStatusUser status updates

This library is part of a larger Nostr ecosystem for Elixir:

Documentation

Full documentation is available on HexDocs.

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.