Skip to content

Commit

Permalink
feat(console): blob share ticket (#1746)
Browse files Browse the repository at this point in the history
## Description

Adds a subcommand to blob to produce a ticket. Option include:
- make the ticket a derp only one
- make the ticket a udp only one
- use a blob hashseq format (default is raw as it's more general)
- hidden dev flag: debug the ticket contents
- Add a token

## Notes & open questions

I believe the generated tickets are correct (if they are not, then there
is no validation about it) but some of them don't seem to work, for
example if the ticket is direct address only. We should debug this
afterwards

Sample output:
```
author:wi46loxp… 
> blob share bafkr4ibzusj4ulmrhrbymsgklwlcencx63jwcurmoimjhk4mutqdsqhnqa --debug --token sunshine
Ticket for blob bafkr4ibzusj4ulmrhrbymsgklwlcencx63jwcurmoimjhk4mutqdsqhnqa (347 B)
blob:edjoqrkky753mdwphqrxbr2wjzkizv4hkb5s5ovav73c2zfuaja7aaibamal6xy6a73lgayaycucxc6ek4asqayyabirj5ltvyppnqt24t6cjrkxae42je6kfwityq4gjdff3frcgrl7nu3bkiwhegetvogkjybzidwyaaiion2w443infxgk
Ticket {
    node: PeerAddr {
        peer_id: PublicKey(2luekswh7o3a5tz4),
        info: AddrInfo {
            derp_region: Some(
                1,
            ),
            direct_addresses: {
                191.95.30.7:55798,
                192.168.43.139:11204,
                [2803:1800:5114:f573:ae1e:f6c2:7ae4:fc24]:11205,
            },
        },
    },
    format: HashSeq,
    hash: Hash(
        39a493ca2d913c438648ca5d96223457f6d361522c721893ab8ca4e03940ed80,
    ),
    token: Some(
        RequestToken {
            bytes: b"sunshine",
        },
    ),
}
```

## Change checklist

- [x] Self-review.
- [ ] Documentation updates if relevant.
- [ ] Tests if relevant.
  • Loading branch information
divagant-martian committed Oct 30, 2023
1 parent 376748c commit fa9fa83
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 73 additions & 1 deletion iroh/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;
use std::{net::SocketAddr, path::PathBuf, time::Duration};

use anyhow::Result;
use anyhow::{Context, Result};
use bytes::Bytes;
use clap::{Args, Parser, Subcommand};
use colored::Colorize;
Expand Down Expand Up @@ -567,6 +567,26 @@ pub enum BlobCommands {
/// Delete content on the node.
#[clap(subcommand)]
Delete(self::delete::Commands),
/// Get a ticket to share this blob.
Share {
/// Hash of the blob to share.
hash: Hash,
/// Include an optional authentication token in the ticket.
#[clap(long)]
token: Option<String>,
/// Do not include DERP reion information in the ticket. (advanced)
#[clap(long, conflicts_with = "derp_only", default_value_t = false)]
no_derp: bool,
/// Include only the DERP region information in the ticket. (advanced)
#[clap(long, conflicts_with = "no_derp", default_value_t = false)]
derp_only: bool,
/// If the blob is a collection, the requester will also fetch the listed blobs.
#[clap(long, default_value_t = false)]
recursive: bool,
/// Display the contents of this ticket too.
#[clap(long, hide = true)]
debug: bool,
},
}

impl BlobCommands {
Expand Down Expand Up @@ -638,6 +658,58 @@ impl BlobCommands {
// node (last argument to run_with_opts).
self::add::run_with_opts(iroh, opts, None).await
}
Self::Share {
hash,
token,
no_derp,
derp_only,
recursive,
debug,
} => {
let NodeStatusResponse { addr, .. } = iroh.node.status().await?;
let node_addr = if no_derp {
PeerAddr::new(addr.peer_id)
.with_direct_addresses(addr.direct_addresses().copied())
} else if derp_only {
if let Some(region) = addr.derp_region() {
PeerAddr::new(addr.peer_id).with_derp_region(region)
} else {
addr
}
} else {
addr
};

let blob_reader = iroh
.blobs
.read(hash)
.await
.context("failed to retrieve blob info")?;
let blob_status = if blob_reader.is_complete() {
"blob"
} else {
"incomplete blob"
};

let format = if recursive {
BlobFormat::HashSeq
} else {
BlobFormat::Raw
};

let request_token = token.map(RequestToken::new).transpose()?;

let ticket =
Ticket::new(node_addr, hash, format, request_token).expect("correct ticket");
println!(
"Ticket for {blob_status} {hash} ({})\n{ticket}",
HumanBytes(blob_reader.size())
);
if debug {
println!("{ticket:#?}")
}
Ok(())
}
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions iroh/src/ticket/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl IrohTicket for Ticket {
const KIND: Kind = Kind::Blob;

fn verify(&self) -> std::result::Result<(), &'static str> {
if self.node.info.direct_addresses.is_empty() {
return Err("Invalid address list in ticket");
if self.node.info.is_empty() {
return Err("addressing info cannot be empty");
}
Ok(())
}
Expand All @@ -54,10 +54,7 @@ impl Ticket {
format: BlobFormat,
token: Option<RequestToken>,
) -> Result<Self> {
ensure!(
!peer.info.direct_addresses.is_empty(),
"addrs list can not be empty"
);
ensure!(!peer.info.is_empty(), "addressing info cannot be empty");
Ok(Self {
hash,
format,
Expand Down

0 comments on commit fa9fa83

Please sign in to comment.