blossom-rs 0.5.6

Full-featured Blossom (BUD-01) blob storage library for Rust — embeddable server, async client, BIP-340 Nostr auth
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Async HTTP client for Blossom blob storage.
//!
//! Uploads/downloads content-addressed blobs with BIP-340 Schnorr
//! authorization and multi-server failover.

pub mod batch;
pub mod multi;

use crate::auth::{
    auth_header_value, build_blossom_auth, build_blossom_auth_with_extra_tags, BlossomSigner,
};
use crate::protocol::{sha256_hex, BlobDescriptor, STREAM_CHUNK_SIZE};
use tracing::{info, instrument, warn};

/// Async HTTP client for Blossom blob servers.
///
/// Tries servers in order for each operation, failing over to the next
/// on error or non-success status.
pub struct BlossomClient {
    http: reqwest::Client,
    servers: Vec<String>,
    signer: Box<dyn BlossomSigner>,
}

impl BlossomClient {
    /// Create a new client with the given server URLs and signer.
    /// Create a new client with the given server URLs and signer.
    /// Default timeout: 30 seconds.
    pub fn new(servers: Vec<String>, signer: impl BlossomSigner + 'static) -> Self {
        Self::with_timeout(servers, signer, std::time::Duration::from_secs(30))
    }

    /// Create a new client with a custom timeout.
    pub fn with_timeout(
        servers: Vec<String>,
        signer: impl BlossomSigner + 'static,
        timeout: std::time::Duration,
    ) -> Self {
        let http = reqwest::Client::builder()
            .timeout(timeout)
            .build()
            .unwrap_or_else(|_| reqwest::Client::new());
        Self {
            http,
            servers,
            signer: Box::new(signer),
        }
    }

    /// Upload a blob to the first available server.
    ///
    /// Returns the blob descriptor with SHA256 hash. The hash is verified
    /// against the server's response to ensure integrity.
    #[instrument(name = "blossom.client.upload", skip_all, fields(
        blob.size = data.len(),
        blob.sha256,
        blob.content_type = content_type,
        server.url,
    ))]
    pub async fn upload(&self, data: &[u8], content_type: &str) -> Result<BlobDescriptor, String> {
        let our_sha256 = sha256_hex(data);
        tracing::Span::current().record("blob.sha256", our_sha256.as_str());

        let auth_event =
            build_blossom_auth(self.signer.as_ref(), "upload", Some(&our_sha256), None, "");
        let auth_header = auth_header_value(&auth_event);

        for server in &self.servers {
            let url = format!("{}/upload", server.trim_end_matches('/'));
            let result = self
                .http
                .put(&url)
                .header("Authorization", &auth_header)
                .header("Content-Type", content_type)
                .body(data.to_vec())
                .send()
                .await;

            match result {
                Ok(resp) if resp.status().is_success() => {
                    let desc: BlobDescriptor = resp
                        .json()
                        .await
                        .map_err(|e| format!("parse upload response: {e}"))?;
                    if desc.sha256 != our_sha256 {
                        return Err(format!(
                            "SHA256 mismatch: server={}, ours={}",
                            desc.sha256, our_sha256
                        ));
                    }
                    tracing::Span::current().record("server.url", server.as_str());
                    info!(
                        blob.sha256 = %desc.sha256,
                        blob.size = desc.size,
                        server.url = %server,
                        "blob uploaded"
                    );
                    return Ok(desc);
                }
                Ok(resp) => {
                    let status = resp.status();
                    let text = resp.text().await.unwrap_or_default();
                    warn!(
                        server.url = %server,
                        http.status_code = status.as_u16(),
                        error.message = %text,
                        "upload failed, trying next server"
                    );
                    continue;
                }
                Err(e) => {
                    warn!(
                        server.url = %server,
                        error.message = %e,
                        "upload request error, trying next server"
                    );
                    continue;
                }
            }
        }

        Err("all Blossom servers failed for upload".into())
    }

    /// Upload a blob with LFS context tags (BUD-20).
    ///
    /// Adds `["t","lfs"]`, `["path",...]`, `["repo",...]`, and optionally
    /// `["base",...]` and `["manifest"]` tags to the auth event so the
    /// server can apply compression and delta encoding.
    #[instrument(name = "blossom.client.upload_lfs", skip_all, fields(
        blob.size = data.len(),
        blob.sha256,
        lfs.path = path,
        lfs.repo = repo,
        server.url,
    ))]
    pub async fn upload_lfs(
        &self,
        data: &[u8],
        content_type: &str,
        path: &str,
        repo: &str,
        base_sha256: Option<&str>,
        is_manifest: bool,
    ) -> Result<BlobDescriptor, String> {
        let our_sha256 = sha256_hex(data);
        tracing::Span::current().record("blob.sha256", our_sha256.as_str());

        let mut extra_tags = vec![
            vec!["t".into(), "lfs".into()],
            vec!["path".into(), path.into()],
            vec!["repo".into(), repo.into()],
        ];
        if let Some(base) = base_sha256 {
            extra_tags.push(vec!["base".into(), base.into()]);
        }
        if is_manifest {
            extra_tags.push(vec!["manifest".into()]);
        }

        let auth_event = build_blossom_auth_with_extra_tags(
            self.signer.as_ref(),
            "upload",
            Some(&our_sha256),
            None,
            "",
            &extra_tags,
        );
        let auth_header = auth_header_value(&auth_event);

        for server in &self.servers {
            let url = format!("{}/upload", server.trim_end_matches('/'));
            let result = self
                .http
                .put(&url)
                .header("Authorization", &auth_header)
                .header("Content-Type", content_type)
                .body(data.to_vec())
                .send()
                .await;

            match result {
                Ok(resp) if resp.status().is_success() => {
                    let desc: BlobDescriptor = resp
                        .json()
                        .await
                        .map_err(|e| format!("parse upload response: {e}"))?;
                    if desc.sha256 != our_sha256 {
                        return Err(format!(
                            "SHA256 mismatch: server={}, ours={}",
                            desc.sha256, our_sha256
                        ));
                    }
                    tracing::Span::current().record("server.url", server.as_str());
                    info!(
                        blob.sha256 = %desc.sha256,
                        blob.size = desc.size,
                        lfs.path = %path,
                        server.url = %server,
                        "LFS blob uploaded"
                    );
                    return Ok(desc);
                }
                Ok(resp) => {
                    let status = resp.status();
                    let text = resp.text().await.unwrap_or_default();
                    warn!(
                        server.url = %server,
                        http.status_code = status.as_u16(),
                        error.message = %text,
                        "LFS upload failed, trying next server"
                    );
                    continue;
                }
                Err(e) => {
                    warn!(
                        server.url = %server,
                        error.message = %e,
                        "LFS upload request error, trying next server"
                    );
                    continue;
                }
            }
        }

        Err("all Blossom servers failed for LFS upload".into())
    }
    ///
    /// Verifies content-addressed integrity after download.
    #[instrument(name = "blossom.client.download", skip_all, fields(
        blob.sha256 = %sha256,
        blob.size,
        server.url,
    ))]
    pub async fn download(&self, sha256: &str) -> Result<Vec<u8>, String> {
        let auth_event = build_blossom_auth(self.signer.as_ref(), "get", None, None, "");
        let auth_header = auth_header_value(&auth_event);

        for server in &self.servers {
            let url = format!("{}/{}", server.trim_end_matches('/'), sha256);
            let result = self
                .http
                .get(&url)
                .header("Authorization", &auth_header)
                .send()
                .await;

            match result {
                Ok(resp) if resp.status().is_success() => {
                    let data = resp
                        .bytes()
                        .await
                        .map_err(|e| format!("download body: {e}"))?
                        .to_vec();
                    let actual_hash = sha256_hex(&data);
                    if actual_hash != sha256 {
                        return Err(format!(
                            "SHA256 mismatch on download: expected={}, actual={}",
                            sha256, actual_hash
                        ));
                    }
                    tracing::Span::current().record("blob.size", data.len() as u64);
                    tracing::Span::current().record("server.url", server.as_str());
                    info!(
                        blob.sha256 = %sha256,
                        blob.size = data.len(),
                        server.url = %server,
                        "blob downloaded"
                    );
                    return Ok(data);
                }
                Ok(resp) => {
                    warn!(
                        server.url = %server,
                        http.status_code = resp.status().as_u16(),
                        "download failed, trying next server"
                    );
                    continue;
                }
                Err(e) => {
                    warn!(
                        server.url = %server,
                        error.message = %e,
                        "download request error, trying next server"
                    );
                    continue;
                }
            }
        }

        Err(format!("blob {} not found on any Blossom server", sha256))
    }

    /// Check if a blob exists on any configured server.
    #[instrument(name = "blossom.client.exists", skip_all, fields(blob.sha256 = %sha256))]
    pub async fn exists(&self, sha256: &str) -> Result<bool, String> {
        for server in &self.servers {
            let url = format!("{}/{}", server.trim_end_matches('/'), sha256);
            let result = self.http.head(&url).send().await;

            match result {
                Ok(resp) if resp.status().is_success() => return Ok(true),
                Ok(resp) if resp.status().as_u16() == 404 => continue,
                Ok(_) => continue,
                Err(e) => {
                    warn!(
                        server.url = %server,
                        error.message = %e,
                        "exists check error, trying next server"
                    );
                    continue;
                }
            }
        }
        Ok(false)
    }

    /// Delete a blob by SHA256 hash (requires auth).
    ///
    /// Returns `Ok(true)` if deleted, `Ok(false)` if not found.
    #[instrument(name = "blossom.client.delete", skip_all, fields(blob.sha256 = %sha256))]
    pub async fn delete(&self, sha256: &str) -> Result<bool, String> {
        let auth_event = build_blossom_auth(self.signer.as_ref(), "delete", None, None, "");
        let auth_header = auth_header_value(&auth_event);

        for server in &self.servers {
            let url = format!("{}/{}", server.trim_end_matches('/'), sha256);
            let result = self
                .http
                .delete(&url)
                .header("Authorization", &auth_header)
                .send()
                .await;

            match result {
                Ok(resp) if resp.status().is_success() => {
                    info!(blob.sha256 = %sha256, server.url = %server, "blob deleted");
                    return Ok(true);
                }
                Ok(resp) if resp.status().as_u16() == 404 => return Ok(false),
                Ok(resp) => {
                    warn!(
                        server.url = %server,
                        http.status_code = resp.status().as_u16(),
                        "delete failed, trying next server"
                    );
                    continue;
                }
                Err(e) => {
                    warn!(
                        server.url = %server,
                        error.message = %e,
                        "delete request error, trying next server"
                    );
                    continue;
                }
            }
        }

        Err("all Blossom servers failed for delete".into())
    }

    /// List blobs uploaded by a pubkey.
    #[instrument(name = "blossom.client.list", skip_all, fields(list.pubkey = %pubkey))]
    pub async fn list(&self, pubkey: &str) -> Result<Vec<BlobDescriptor>, String> {
        for server in &self.servers {
            let url = format!("{}/list/{}", server.trim_end_matches('/'), pubkey);
            let result = self.http.get(&url).send().await;

            match result {
                Ok(resp) if resp.status().is_success() => {
                    let descs: Vec<BlobDescriptor> = resp
                        .json()
                        .await
                        .map_err(|e| format!("parse list response: {e}"))?;
                    info!(list.pubkey = %pubkey, server.url = %server, "list retrieved");
                    return Ok(descs);
                }
                Ok(resp) => {
                    warn!(
                        server.url = %server,
                        http.status_code = resp.status().as_u16(),
                        "list failed, trying next server"
                    );
                    continue;
                }
                Err(e) => {
                    warn!(
                        server.url = %server,
                        error.message = %e,
                        "list request error, trying next server"
                    );
                    continue;
                }
            }
        }

        Err("all Blossom servers failed for list".into())
    }

    /// Upload a file from disk without buffering in memory.
    ///
    /// First pass computes SHA256 for the auth header. Second pass
    /// streams the file to the server via reqwest.
    #[instrument(name = "blossom.client.upload_file", skip_all, fields(
        file.path = %path.display(),
        blob.sha256,
        blob.size,
    ))]
    pub async fn upload_file(
        &self,
        path: &std::path::Path,
        content_type: &str,
    ) -> Result<BlobDescriptor, String> {
        // First pass: compute SHA256 by streaming the file.
        let file_meta = tokio::fs::metadata(path)
            .await
            .map_err(|e| format!("stat file: {e}"))?;
        let file_size = file_meta.len();

        let our_sha256 = tokio::task::block_in_place(|| {
            let mut f = std::fs::File::open(path).map_err(|e| format!("open file: {e}"))?;
            let (hash, _) =
                crate::protocol::sha256_stream(&mut f).map_err(|e| format!("hash file: {e}"))?;
            Ok::<_, String>(hash)
        })?;

        tracing::Span::current().record("blob.sha256", our_sha256.as_str());
        tracing::Span::current().record("blob.size", file_size);

        let auth_event =
            build_blossom_auth(self.signer.as_ref(), "upload", Some(&our_sha256), None, "");
        let auth_header = auth_header_value(&auth_event);

        // Second pass: stream file to server.
        for server in &self.servers {
            let url = format!("{}/upload", server.trim_end_matches('/'));

            let file = tokio::fs::File::open(path)
                .await
                .map_err(|e| format!("open file: {e}"))?;
            let stream = tokio_util::io::ReaderStream::with_capacity(file, STREAM_CHUNK_SIZE);
            let body = reqwest::Body::wrap_stream(stream);

            let result = self
                .http
                .put(&url)
                .header("Authorization", &auth_header)
                .header("Content-Type", content_type)
                .header("Content-Length", file_size)
                .body(body)
                .send()
                .await;

            match result {
                Ok(resp) if resp.status().is_success() => {
                    let desc: BlobDescriptor = resp
                        .json()
                        .await
                        .map_err(|e| format!("parse upload response: {e}"))?;
                    if desc.sha256 != our_sha256 {
                        return Err(format!(
                            "SHA256 mismatch: server={}, ours={}",
                            desc.sha256, our_sha256
                        ));
                    }
                    info!(
                        blob.sha256 = %desc.sha256,
                        blob.size = desc.size,
                        server.url = %server,
                        "file uploaded (streaming)"
                    );
                    return Ok(desc);
                }
                Ok(resp) => {
                    let status = resp.status();
                    let text = resp.text().await.unwrap_or_default();
                    warn!(
                        server.url = %server,
                        http.status_code = status.as_u16(),
                        error.message = %text,
                        "upload_file failed, trying next server"
                    );
                    continue;
                }
                Err(e) => {
                    warn!(
                        server.url = %server,
                        error.message = %e,
                        "upload_file request error, trying next server"
                    );
                    continue;
                }
            }
        }

        Err("all Blossom servers failed for upload_file".into())
    }
}

impl crate::traits::BlobClient for BlossomClient {
    type Address = ();

    async fn upload(
        &self,
        _addr: &(),
        data: &[u8],
        content_type: &str,
    ) -> Result<BlobDescriptor, String> {
        self.upload(data, content_type).await
    }

    async fn download(&self, _addr: &(), sha256: &str) -> Result<Vec<u8>, String> {
        self.download(sha256).await
    }

    async fn exists(&self, _addr: &(), sha256: &str) -> Result<bool, String> {
        self.exists(sha256).await
    }

    async fn delete(&self, _addr: &(), sha256: &str) -> Result<bool, String> {
        self.delete(sha256).await
    }

    async fn list(&self, _addr: &(), pubkey: &str) -> Result<Vec<BlobDescriptor>, String> {
        self.list(pubkey).await
    }

    async fn upload_file(
        &self,
        _addr: &(),
        path: &std::path::Path,
        content_type: &str,
    ) -> Result<BlobDescriptor, String> {
        self.upload_file(path, content_type).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::Signer;

    #[test]
    fn test_client_creation() {
        let signer = Signer::generate();
        let client = BlossomClient::new(vec!["https://blossom.example.com".into()], signer);
        assert_eq!(client.servers.len(), 1);
    }
}