-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
248 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
global using Microsoft.FSharp.Collections; | ||
global using Microsoft.FSharp.Core; | ||
global using static Microsoft.FSharp.Core.FSharpOption<Nostra.AuthorIdT>; | ||
global using static Microsoft.FSharp.Core.FSharpOption<Nostra.Kind>; | ||
global using static Nostra.Client.Request; | ||
global using static Nostra.Client.Response; | ||
global using RelayConnection = Nostra.Client.RelayConnection; | ||
global using EventId = Nostra.EventIdModule; | ||
global using Event = Nostra.EventModule; | ||
global using SecretKey = Nostra.SecretKeyModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<OutputType>Exe</OutputType> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Nostra\Nostra.fsproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace Nostra.CSharp; | ||
using NostrListenerCallback = Action<FSharpResult<RelayMessage, string>>; | ||
|
||
public static class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
var relay = await Client.ConnectToRelayAsync(new Uri("wss://relay.damus.io")); | ||
if (args.Length > 1) | ||
{ | ||
var textToPublish = args[1]; | ||
PublishNote(relay, textToPublish); | ||
} | ||
await ListenEverything(relay); | ||
} | ||
|
||
private static void PublishNote(RelayConnection relay, string noteText) | ||
{ | ||
// Creates a note with the given text, sign it using a new randomly-generated | ||
// secret key and send it to the connected relay. | ||
var unsignedEvent = Event.CreateNote(noteText); | ||
var signedEvent = Event.Sign(SecretKey.CreateRandom(), unsignedEvent); | ||
Client.Publish(signedEvent, relay); | ||
|
||
// Encode the event as a shareable bech32 nevent event and prints it | ||
// in the console. | ||
Console.WriteLine(Shareable.ToNEvent( | ||
signedEvent.Id, | ||
ToFSharpList(["wss://relay.damus.io"]), | ||
Some(signedEvent.PubKey), | ||
Some(signedEvent.Kind))); | ||
|
||
// Serialized the event as SJON and prints it in the console. | ||
var signedEventAsJson = Event.Serialize(signedEvent); | ||
Console.WriteLine(signedEventAsJson); | ||
} | ||
|
||
private static async Task ListenEverything(RelayConnection relay) | ||
{ | ||
// Subscribes to all the new events. | ||
var filter = Filter.since(DateTime.UtcNow, Filter.all); | ||
var filters = ToFSharpList([filter]); | ||
Client.Subscribe("all", filters, relay); | ||
|
||
// Start listeniong for all the events. | ||
await Client.StartListening(FuncConvert.ToFSharpFunc((NostrListenerCallback) (mresult => | ||
{ | ||
var relayMessage = Result.requiresOk(mresult); | ||
if (relayMessage.IsRMEvent) | ||
{ | ||
var (_, relayEvent) = GetEvent(relayMessage); | ||
Console.WriteLine(Event.Serialize(relayEvent)); | ||
} | ||
})), relay); | ||
} | ||
|
||
private static FSharpList<T> ToFSharpList<T>(this IEnumerable<T> seq) => | ||
seq.Aggregate(FSharpList<T>.Empty, (state, e) => new FSharpList<T>(e, state)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# C# Interop demo | ||
|
||
This is a simple working example of how to consume Nostra from C# projects. | ||
|
||
### Create a note, sign it and publish it | ||
Creates a note with a text, signs it using a new randomly-generated secret key and sends it to the connected relay. | ||
```c# | ||
// Connect to the relay | ||
var relay = await Client.ConnectToRelayAsync(new Uri("wss://relay.damus.io")); | ||
|
||
// Create a note | ||
var unsignedEvent = Event.CreateNote("Hello everybody!"); | ||
|
||
// Sign the note | ||
var signedEvent = Event.Sign(SecretKey.CreateRandom(), unsignedEvent); | ||
|
||
// Publish the note in the relay | ||
Client.Publish(signedEvent, relay); | ||
``` | ||
|
||
#### Subscribe to events | ||
Creates a filter to match all events created from now and start listening for them. | ||
Display the raw json for those messages received from the relay that are events. | ||
```c# | ||
// Connect to the relay | ||
var relay = await Client.ConnectToRelayAsync(new Uri("wss://relay.damus.io")); | ||
|
||
// Subscribes to all the new events. | ||
var filter = Filter.since(DateTime.UtcNow, Filter.all); | ||
var filters = ToFSharpList([filter]); | ||
Client.Subscribe("all", filters, relay); | ||
|
||
// Start listeniong for all the events. | ||
await Client.StartListening(FuncConvert.ToFSharpFunc((NostrListenerCallback) (mresult => | ||
{ | ||
var relayMessage = Result.requiresOk(mresult); | ||
if (relayMessage.IsRMEvent) | ||
{ | ||
var (_, relayEvent) = GetEvent(relayMessage); | ||
Console.WriteLine(Event.Serialize(relayEvent)); | ||
} | ||
})), relay); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.