p2panda_blobs/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3#![cfg_attr(doctest, doc=include_str!("../README.md"))]
4
5//! Blobs service offering storage, retrieval and synchronisation of content-addressed data.
6//!
7//! `p2panda-blobs` relies on the
8//! [`iroh-blobs`](https://docs.rs/iroh-blobs/latest/iroh_blobs/index.html) crate and offers an API
9//! to import blobs into a store and use the resulting BLAKE3 hashes to address them for downloads.
10//! In-memory and filesystem-based store options are provided.
11//!
12//! The blobs service integrates with `p2panda-net` to provide a means of synchronising files
13//! between devices using BLAKE3 verified streaming. Memory usage is generally low, even when
14//! transferring very large files.
15mod blobs;
16mod config;
17mod download;
18mod export;
19mod import;
20mod protocol;
21
22use iroh::{NodeAddr as IrohNodeAddr, NodeId};
23use iroh_blobs::store;
24
25pub use blobs::Blobs;
26pub use config::Config;
27pub use download::DownloadBlobEvent;
28pub use import::ImportBlobEvent;
29use p2panda_net::NodeAddress;
30pub use protocol::{BLOBS_ALPN, BlobsProtocol};
31
32/// In-memory storage database with support for partial blobs.
33pub type MemoryStore = store::mem::Store;
34
35/// Filesystem storage database backed by [redb](https://crates.io/crates/redb) for small blobs and
36/// files for large blobs.
37pub type FilesystemStore = store::fs::Store;
38
39/// Converts a `p2panda-net` node address type to the `iroh` implementation.
40pub(crate) fn from_node_addr(addr: NodeAddress) -> IrohNodeAddr {
41    let node_id = NodeId::from_bytes(addr.public_key.as_bytes()).expect("invalid public key");
42    let mut node_addr =
43        IrohNodeAddr::new(node_id).with_direct_addresses(addr.direct_addresses.to_vec());
44    if let Some(url) = addr.relay_url {
45        node_addr = node_addr.with_relay_url(url.into());
46    }
47    node_addr
48}