Last active
July 17, 2024 18:49
-
-
Save ryandotsmith/7cbe77f061b4cdbc9ee270e84bb1fbca to your computer and use it in GitHub Desktop.
Playing around with Iroh Tickets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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