Skip to content

Instantly share code, notes, and snippets.

@ryandotsmith
Last active July 17, 2024 18:49
Show Gist options
  • Save ryandotsmith/7cbe77f061b4cdbc9ee270e84bb1fbca to your computer and use it in GitHub Desktop.
Save ryandotsmith/7cbe77f061b4cdbc9ee270e84bb1fbca to your computer and use it in GitHub Desktop.
Playing around with Iroh Tickets
use anyhow::{Context, Result};
use iroh::{
base::{
node_addr::AddrInfoOptions,
ticket::{BlobTicket, Ticket},
},
blobs::BlobFormat,
};
use tokio;
#[tokio::main]
async fn main() -> Result<()> {
let node = iroh::node::Node::memory().spawn().await?;
let hash = node
.client()
.blobs()
.add_bytes(b"hello".to_vec())
.await?
.hash;
let ticket = node
.client()
.blobs()
.share(hash, BlobFormat::Raw, AddrInfoOptions::RelayAndAddresses)
.await?;
// tickets can be serialized and shared
let ticket: BlobTicket = Ticket::deserialize(&ticket.serialize())?;
let other_node = iroh::node::Node::memory().spawn().await?;
other_node
.client()
.blobs()
.download(ticket.hash(), ticket.node_addr().clone())
.await
.context("downloading")?
.await?;
let data = other_node
.blobs()
.read_to_bytes(ticket.hash())
.await?
.to_vec();
println!("{}", String::from_utf8(data)?);
node.shutdown().await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment