Git Product home page Git Product logo

nostr-client's Introduction

Logo

Nostr client

.NET Core NuGet version NuGet downloads

This is a C# implementation of the Nostr protocol found here:

https://github.com/nostr-protocol/nips

Nostr protocol is based on websocket communication. This library keeps a reliable connection to get real-time data and fast execution of your commands.

Releases and breaking changes

License:

Apache License 2.0

Features

  • installation via NuGet (Nostr.Client)
  • targeting .NET 6.0 and higher (.NET Core, Linux/MacOS compatible)
  • reactive extensions (Rx.NET)

Usage

Receiving events

var url = new Uri("wss://relay.damus.io");

using var communicator = new NostrWebsocketCommunicator(url);
using var client = new NostrWebsocketClient(communicator, null);

client.Streams.EventStream.Subscribe(response =>
{
    var ev = response.Event;
    Log.Information("{kind}: {content}", ev?.Kind, ev?.Content)
            
    if(ev is NostrMetadataEvent evm) {
        Log.Information("Name: {name}, about: {about}", evm.Metadata?.Name, evm.Metadata?.About);
    }
});

await communicator.Start();

Sending event

var ev = new NostrEvent
{
    Kind = NostrKind.ShortTextNote,
    CreatedAt = DateTime.UtcNow,
    Content = "Test message from C# client"
};

var key = NostrPrivateKey.FromBech32("nsec1xxx");
var signed = ev.Sign(key);

client.Send(new NostrEventRequest(signed));

Sending encrypted direct message (NIP-04)

var sender = NostrPrivateKey.FromBech32("nsec1l0a7m5dlg4h9wurhnmgsq5nv9cqyvdwsutk4yf3w4fzzaqw7n80ssdfzkg");
var receiver = NostrPublicKey.FromBech32("npub1dd668dyr9un9nzf9fjjkpdcqmge584c86gceu7j97nsp4lj2pscs0xk075");

var ev = new NostrEvent
{
    CreatedAt = DateTime.UtcNow,
    Content = $"Test private message from C# client"
};

var encrypted = ev.EncryptDirect(sender, receiver);
var signed = encrypted.Sign(sender);

client.Send(new NostrEventRequest(signed));

Multi relays support

var relays = new[]
{
    new NostrWebsocketCommunicator(new Uri("wss://relay.snort.social")),
    new NostrWebsocketCommunicator(new Uri("wss://relay.damus.io")),
    new NostrWebsocketCommunicator(new Uri("wss://nos.lol"))
};

var client = new NostrMultiWebsocketClient(NullLogger<NostrWebsocketClient>.Instance, relays);

client.Streams.EventStream.Subscribe(HandleEvent);

relays.ToList().ForEach(relay => relay.Start());

More usage examples:

image

NIP's coverage

  • NIP-01: Basic protocol flow description
  • NIP-02: Contact List and Petnames (No petname support)
  • NIP-03: OpenTimestamps Attestations for Events
  • NIP-04: Encrypted Direct Message
  • NIP-05: Mapping Nostr keys to DNS-based internet identifiers
  • NIP-06: Basic key derivation from mnemonic seed phrase
  • NIP-07: window.nostr capability for web browsers
  • NIP-08: Handling Mentions
  • NIP-09: Event Deletion
  • NIP-10: Conventions for clients' use of e and p tags in text events
  • NIP-11: Relay Information Document
  • NIP-12: Generic Tag Queries
  • NIP-13: Proof of Work
  • NIP-14: Subject tag in text events
  • NIP-15: End of Stored Events Notice
  • NIP-19: bech32-encoded entities
  • NIP-20: Command Results
  • NIP-21: nostr: Protocol handler (web+nostr)
  • NIP-25: Reactions
  • NIP-26: Delegated Event Signing (Display delegated signings only)
  • NIP-28: Public Chat
  • NIP-36: Sensitive Content
  • NIP-40: Expiration Timestamp
  • NIP-42: Authentication of clients to relays
  • NIP-50: Search
  • NIP-51: Lists
  • NIP-65: Relay List Metadata

Pull Requests are welcome!

Reconnecting

A built-in reconnection invokes after 1 minute (default) of not receiving any messages from the server. It is possible to configure that timeout via communicator.ReconnectTimeout. Also, a stream ReconnectionHappened sends information about a type of reconnection. However, if you are subscribed to low-rate channels, you will likely encounter that timeout - higher it to a few minutes or implement ping-pong interaction on your own every few seconds.

In the case of Nostr relay outage, there is a built-in functionality that slows down reconnection requests (could be configured via client.ErrorReconnectTimeout, the default is 1 minute).

Beware that you need to resubscribe to channels after reconnection happens. You should subscribe to ReconnectionHappened stream and send subscription requests.

Testing

The library is prepared for replay testing. The dependency between Client and Communicator is via abstraction INostrCommunicator. There are two communicator implementations:

  • NostrWebsocketCommunicator - real-time communication with Nostr relay.
  • NostrFileCommunicator - a simulated communication, raw data are loaded from files and streamed.

Feel free to implement INostrCommunicator on your own, for example, load raw data from database, cache, etc.

Usage:

var communicator = new NostrFileCommunicator();
communicator.FileNames = new[]
{
    "data/nostr-data.txt"
};
communicator.Delimiter = "\n";

var client = new NostrWebsocketClient(communicator);
client.Streams.EventStream.Subscribe(trade =>
{
    // do something with an event
});

await communicator.Start();

Multi-threading and other considerations

See Websocket Client readme

nostr-client's People

Contributors

kroese avatar marfusios avatar weex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nostr-client's Issues

Please ad a reply "Stop" Option. I really hate this bot

Wish you would have some kinda reply function like "Stop" to make it leave me alone. It really ruins the nostr experiences. Especially for new people. and blocking doesn't seem to copy across clients. Now we have to spend countless man hours creating relays that can detect this kinda spam and auto block it.

Library Support for Multi-Relay

Any thoughts about writing multi-relay support into the client? It would be nice not to leave that up to the implementation, as it's kind of central to Nostr function and all clients would need to figure that out first before their client could be useful.

Compile from dockerfile

Hi
im trying to compile the bot but no matter what i do i always get errors not a docker expert

running the docker build where the Dockerfile is gives me error

COPY [apps/nostr-bot/NostrBot.Web/NostrBot.Web.csproj, apps/nostr-bot/NostrBot.Web/

but the file exists in that directory weird...

i have deleted the .Dockerfile but i get the same error

then i have tried

DOCKER_BUILDKIT=1 docker build -f .

it starts to compile but stops at stage 3

=> ERROR [build 3/7] COPY [apps/nostr-bot/NostrBot.Web/NostrBot.Web.csproj, apps/nostr-bot/NostrBot.Web/]

any ideas to solve this?

thanks in advanced

NIP 99

Can you add support for NIP 99?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.