Skip to content

Commit faf2eb7

Browse files
committed
blossom: cleanup examples
Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent a913806 commit faf2eb7

File tree

5 files changed

+48
-72
lines changed

5 files changed

+48
-72
lines changed

crates/nostr-blossom/examples/delete.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
use std::error::Error;
2-
use std::str::FromStr;
3-
41
use clap::Parser;
5-
use nostr::hashes::sha256;
6-
use nostr::key::SecretKey;
7-
use nostr::Keys;
8-
use nostr_blossom::client::BlossomClient;
2+
use nostr::hashes::sha256::Hash as Sha256Hash;
3+
use nostr::prelude::*;
4+
use nostr_blossom::prelude::*;
95

106
#[derive(Parser, Debug)]
117
#[command(author, version, about = "Delete a blob from a Blossom server", long_about = None)]
@@ -16,29 +12,27 @@ struct Args {
1612

1713
/// The SHA256 hash of the blob to delete (in hex)
1814
#[arg(long)]
19-
sha256: String,
15+
sha256: Sha256Hash,
2016

21-
/// Optional private key for signing the deletion (in hex)
17+
/// Optional private key for signing the deletion
2218
#[arg(long, value_name = "PRIVATE_KEY")]
23-
private_key: String,
19+
private_key: SecretKey,
2420
}
2521

2622
#[tokio::main]
27-
async fn main() -> Result<(), Box<dyn Error>> {
23+
async fn main() -> Result<()> {
2824
let args = Args::parse();
2925

3026
let client = BlossomClient::new(&args.server);
3127

3228
// Create signer keys using the given private key
33-
let keys = Keys::new(SecretKey::from_hex(&args.private_key)?);
29+
let keys = Keys::new(args.private_key);
3430

3531
println!("Attempting to delete blob with SHA256: {}", args.sha256);
3632

37-
let blob_hash = sha256::Hash::from_str(&args.sha256)?;
38-
39-
match client.delete_blob(blob_hash, None, &keys).await {
33+
match client.delete_blob(args.sha256, None, &keys).await {
4034
Ok(()) => println!("Blob deleted successfully."),
41-
Err(e) => eprintln!("Failed to delete blob: {}", e),
35+
Err(e) => eprintln!("{e}"),
4236
}
4337

4438
Ok(())

crates/nostr-blossom/examples/download.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::error::Error;
21
use std::fs;
3-
use std::str::FromStr;
42

53
use clap::Parser;
6-
use nostr::hashes::sha256;
7-
use nostr_blossom::client::BlossomClient;
4+
use nostr::hashes::sha256::Hash as Sha256Hash;
5+
use nostr::prelude::*;
6+
use nostr_blossom::prelude::*;
87

98
#[derive(Parser, Debug)]
109
#[command(author, version, about = "Download a blob from a Blossom server", long_about = None)]
@@ -15,36 +14,36 @@ struct Args {
1514

1615
/// SHA256 hash of the blob to download
1716
#[arg(long)]
18-
sha256: String,
17+
sha256: Sha256Hash,
1918

20-
/// Private key to use for authorization (in hex)
19+
/// Private key to use for authorization
2120
#[arg(long)]
22-
private_key: String,
21+
private_key: SecretKey,
2322
}
2423

2524
#[tokio::main]
26-
async fn main() -> Result<(), Box<dyn Error>> {
25+
async fn main() -> Result<()> {
2726
let args = Args::parse();
2827

2928
// Initialize the client.
3029
let client = BlossomClient::new(&args.server);
3130

32-
// Convert the provided SHA256 string into a hash.
33-
let blob_sha = sha256::Hash::from_str(&args.sha256)?;
34-
3531
// Parse the private key.
36-
let keypair = nostr::Keys::parse(&args.private_key)?;
32+
let keypair = Keys::new(args.private_key);
3733

3834
// Download the blob with optional authorization.
39-
match client.get_blob(blob_sha, None, None, Some(&keypair)).await {
35+
match client
36+
.get_blob(args.sha256, None, None, Some(&keypair))
37+
.await
38+
{
4039
Ok(blob) => {
4140
println!("Successfully downloaded blob with {} bytes", blob.len());
42-
let file_name = format!("{}", blob_sha);
41+
let file_name = format!("{}", args.sha256);
4342
fs::write(&file_name, &blob)?;
4443
println!("Blob saved as {}", file_name);
4544
}
46-
Err(err) => {
47-
eprintln!("Failed to download blob: {}", err);
45+
Err(e) => {
46+
eprintln!("{e}");
4847
}
4948
}
5049

crates/nostr-blossom/examples/integration_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ struct Args {
2020
#[arg(long)]
2121
file: Option<PathBuf>,
2222

23-
/// Private key to use for signing (in hex)
23+
/// Private key to use for signing
2424
#[arg(long, value_name = "PRIVATE_KEY")]
25-
private_key: String,
25+
private_key: SecretKey,
2626
}
2727

2828
#[tokio::main]
@@ -33,7 +33,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
3333
// Create the Blossom client.
3434
let client = BlossomClient::new(&args.server);
3535
// Create signer keys from the provided private key.
36-
let keys = Keys::new(SecretKey::from_hex(args.private_key.as_str())?);
36+
let keys = Keys::new(args.private_key);
3737

3838
// Read the blob data from a file if provided, otherwise use default test data.
3939
let data = if let Some(file_path) = args.file {

crates/nostr-blossom/examples/list.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
use std::error::Error;
2-
use std::str::FromStr;
3-
41
use clap::Parser;
5-
use nostr::key::SecretKey;
6-
use nostr::{Keys, PublicKey};
7-
use nostr_blossom::client::BlossomClient;
2+
use nostr::prelude::*;
3+
use nostr_blossom::prelude::*;
84

95
#[derive(Parser, Debug)]
106
#[command(author, version, about = "List blob on a Blossom server", long_about = None)]
@@ -13,30 +9,27 @@ struct Args {
139
#[arg(long)]
1410
server: String,
1511

16-
/// The public key to list blobs for (in hex format)
12+
/// The public key to list blobs for
1713
#[arg(long)]
18-
pubkey: String,
14+
pubkey: PublicKey,
1915

2016
/// Optional private key for authorization (in hex)
2117
#[arg(long)]
22-
private_key: Option<String>,
18+
private_key: Option<SecretKey>,
2319
}
2420

2521
#[tokio::main]
26-
async fn main() -> Result<(), Box<dyn Error>> {
22+
async fn main() -> Result<()> {
2723
let args = Args::parse();
2824
let client = BlossomClient::new(&args.server);
2925

30-
let pubkey = PublicKey::from_str(&args.pubkey)?;
31-
3226
// Check if a private key was provided and branch accordingly
33-
if let Some(private_key_str) = args.private_key {
27+
if let Some(private_key) = args.private_key {
3428
// Attempt to create the secret key, propagating error if parsing fails
35-
let secret_key = SecretKey::from_hex(&private_key_str)?;
36-
let keys = Keys::new(secret_key);
29+
let keys = Keys::new(private_key);
3730

3831
let descriptors = client
39-
.list_blobs(&pubkey, None, None, None, Some(&keys))
32+
.list_blobs(&args.pubkey, None, None, None, Some(&keys))
4033
.await?;
4134

4235
println!("Successfully listed blobs (with auth):");
@@ -45,7 +38,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
4538
}
4639
} else {
4740
let descriptors = client
48-
.list_blobs(&pubkey, None, None, None, None::<&Keys>)
41+
.list_blobs(&args.pubkey, None, None, None, None::<&Keys>)
4942
.await?;
5043

5144
println!("Successfully listed blobs (without auth):");

crates/nostr-blossom/examples/upload.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use std::error::Error;
21
use std::fs;
2+
use std::path::PathBuf;
33

44
use clap::Parser;
5-
use nostr::key::SecretKey;
6-
use nostr::Keys;
7-
use nostr_blossom::client::BlossomClient;
5+
use nostr::prelude::*;
6+
use nostr_blossom::prelude::*;
87

98
#[derive(Parser, Debug)]
109
#[command(author, version, about = "Upload a blob to a Blossom server", long_about = None)]
@@ -15,19 +14,19 @@ struct Args {
1514

1615
/// Path to the file to upload
1716
#[arg(long)]
18-
file: String,
17+
file: PathBuf,
1918

2019
/// Optional content type (e.g., "application/octet-stream")
2120
#[arg(long)]
2221
content_type: Option<String>,
2322

24-
/// Optional private key for signing the upload (in hex)
23+
/// Optional private key for signing the upload
2524
#[arg(long)]
26-
private_key: Option<String>,
25+
private_key: Option<SecretKey>,
2726
}
2827

2928
#[tokio::main]
30-
async fn main() -> Result<(), Box<dyn Error>> {
29+
async fn main() -> Result<()> {
3130
let args = Args::parse();
3231

3332
let client = BlossomClient::new(&args.server);
@@ -44,16 +43,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
4443
// Create signer keys.
4544
// If a private key is provided, try to use it; otherwise generate a new key.
4645
let keys = match args.private_key {
47-
Some(private_key) => match SecretKey::from_hex(private_key.as_str()) {
48-
Ok(secret_key) => Keys::new(secret_key),
49-
Err(e) => {
50-
eprintln!(
51-
"Failed to parse private key: {}. Using generated key instead.",
52-
e
53-
);
54-
Keys::generate()
55-
}
56-
},
46+
Some(private_key) => Keys::new(private_key),
5747
None => Keys::generate(),
5848
};
5949

@@ -64,8 +54,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
6454
Ok(descriptor) => {
6555
println!("Successfully uploaded blob: {:?}", descriptor);
6656
}
67-
Err(err) => {
68-
eprintln!("Failed to upload blob: {}", err);
57+
Err(e) => {
58+
eprintln!("{e}");
6959
}
7060
}
7161

0 commit comments

Comments
 (0)