Skip to content

Commit

Permalink
crates/nostr/examples/metadata-options
Browse files Browse the repository at this point in the history
add example
  • Loading branch information
RandyMcMillan committed Dec 10, 2024
1 parent 36dd68a commit 89075a5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions crates/nostr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ nip47 = ["nip04"]
nip49 = ["dep:chacha20poly1305", "dep:scrypt", "dep:unicode-normalization"]
nip57 = ["dep:aes", "dep:cbc"]
nip59 = ["nip44"]
metadata_options = ["dep:clap"]

[dependencies]
aes = { version = "0.8", optional = true }
Expand All @@ -70,6 +71,7 @@ bitcoin = { version = "0.32", default-features = false, features = ["rand", "ser
cbc = { version = "0.1", optional = true }
chacha20 = { version = "0.9", optional = true }
chacha20poly1305 = { version = "0.10", default-features = false, features = ["getrandom"], optional = true }
clap = { version = "4.4.18", features = ["derive"], optional = true }
negentropy.workspace = true
negentropy-deprecated.workspace = true
nostr-ots = { version = "0.2", optional = true }
Expand Down Expand Up @@ -102,6 +104,10 @@ required-features = ["std"]
name = "metadata"
required-features = ["std"]

[[example]]
name = "metadata-options"
required-features = ["std", "metadata_options"]

[[example]]
name = "nip05"
required-features = ["std", "nip05"]
Expand Down
70 changes: 70 additions & 0 deletions crates/nostr/examples/metadata-options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

#[deny(warnings)]
use clap::Parser;
use nostr::prelude::*;

#[derive(Parser)]
struct Args {
#[structopt(
name = "secret",
long,
default_value = "6b911fd37cdf5c81d4c0adb1ab7fa822ed253ab0ad9aa18d77257c88b29b718e"
)]
/// Nostr secret key
secret: String,
#[structopt(name = "username", long, default_value = "nostr-rs user")]
/// Nostr username
username: String,
#[structopt(name = "displayname", long, default_value = "nostr-rs user")]
/// Nostr display name
displayname: String,
#[structopt(name = "about", long, default_value = "nostr-rs user")]
/// Nostr about string
about: Option<String>,
#[structopt(name = "picture", long, default_value = "https://robohash.org/nostr-rs")]
/// picture url
picture: Option<String>,
#[structopt(name = "banner", long, default_value = "https://robohash.org/nostr-rs")]
/// banner url
banner: Option<String>,
#[structopt(name = "nip05", long, default_value = "[email protected]")]
/// nip05
nip05: Option<String>,
#[structopt(name = "lud16", long, default_value = "[email protected]")]
/// lud16
lud16: Option<String>,
}

fn run(args: &Args) -> Result<()> {
let metadata = Metadata::new()
.name(args.username.clone())
.display_name(args.displayname.clone())
.about(args.about.clone().unwrap())
.picture(Url::parse(&args.picture.clone().unwrap())?)
.banner(Url::parse(&args.banner.clone().unwrap())?)
.nip05(args.nip05.clone().unwrap())
.lud16(args.lud16.clone().unwrap());

let keys = Keys::parse(&args.secret);

let event: Event = EventBuilder::metadata(&metadata).sign_with_keys(&keys?).unwrap();

// Convert client nessage to JSON
let json = ClientMessage::event(event).as_json();
println!("{json}");

return Ok(());
}

fn main() -> Result<()> {
let args = Args::parse();
match run(&args) {
Ok(()) => {}
Err(e) => println!("error: {}", e),
}

Ok(())
}

0 comments on commit 89075a5

Please sign in to comment.