Hello, rust-nostr!
Now that you’ve installed the SDK, it’s time to write your first nostr program.
Generate random keys and construct the client
Rust
let keys: Keys = Keys::generate();
let client = Client::new(keys);
Python
keys = Keys.generate()
client = Client(keys)
JavaScript
let keys: Keys = Keys.generate();
let signer = NostrSigner.keys(keys);
let client = new Client(signer);
Kotlin
//val keys = Keys.generate()
//val client = Client(signer = keys) // TODO: uncomment when fixed
Swift
TODO
Flutter
TODO
Add some relays and connect
Rust
client.add_relay("wss://relay.damus.io").await?;
client.connect().await;
Python
await client.add_relay("wss://relay.damus.io")
await client.connect()
JavaScript
await client.addRelay("wss://relay.damus.io")
await client.connect();
Kotlin
//client.addRelay("wss://relay.damus.io")
//client.connect()
Swift
TODO
Flutter
TODO
Publish a text note
Rust
let builder = EventBuilder::text_note("Hello, rust-nostr!", []);
client.send_event_builder(builder).await?;
Python
builder = EventBuilder.text_note("Hello, rust-nostr!", [])
await client.send_event_builder(builder)
JavaScript
let builder = EventBuilder.textNote("Hello, rust-nostr!", []);
await client.sendEventBuilder(builder);
Kotlin
//val builder = EventBuilder.textNote("Hello, rust-nostr!", listOf())
//client.sendEventBuilder(builder)
Swift
TODO
Flutter
TODO
Full example
Rust
use nostr_sdk::prelude::*;
pub async fn hello() -> Result<()> {
let keys: Keys = Keys::generate();
let client = Client::new(keys);
client.add_relay("wss://relay.damus.io").await?;
client.connect().await;
let builder = EventBuilder::text_note("Hello, rust-nostr!", []);
client.send_event_builder(builder).await?;
Ok(())
}
Python
from nostr_sdk import Keys, Client, EventBuilder
async def hello():
keys = Keys.generate()
client = Client(keys)
await client.add_relay("wss://relay.damus.io")
await client.connect()
builder = EventBuilder.text_note("Hello, rust-nostr!", [])
await client.send_event_builder(builder)
JavaScript
import {Keys, Client, EventBuilder, NostrSigner} from "@rust-nostr/nostr-sdk";
export async function hello() {
let keys: Keys = Keys.generate();
let signer = NostrSigner.keys(keys);
let client = new Client(signer);
await client.addRelay("wss://relay.damus.io")
await client.connect();
let builder = EventBuilder.textNote("Hello, rust-nostr!", []);
await client.sendEventBuilder(builder);
}
Kotlin
import rust.nostr.sdk.*
suspend fun hello() {
//val keys = Keys.generate()
//val client = Client(signer = keys) // TODO: uncomment when fixed
//client.addRelay("wss://relay.damus.io")
//client.connect()
//val builder = EventBuilder.textNote("Hello, rust-nostr!", listOf())
//client.sendEventBuilder(builder)
}
Swift
TODO
Flutter
TODO