-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-partial-download-raw
- Loading branch information
Showing
21 changed files
with
476 additions
and
246 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ pub mod base32; | |
#[cfg(feature = "hash")] | ||
pub mod hash; | ||
pub mod rpc; | ||
#[cfg(feature = "base32")] | ||
pub mod ticket; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use crate::base32; | ||
|
||
/// A ticket is a serializable object that combines all information required | ||
/// for an operation. E.g. an iroh blob ticket would contain the hash of the | ||
/// data as well as information about how to reach the provider. | ||
/// | ||
/// Tickets support serialization to a string using base32 encoding. The kind of | ||
/// ticket will be prepended to the string to make it somewhat self describing. | ||
/// | ||
/// Versioning is left to the implementer. Some kinds of tickets might need | ||
/// versioning, others might not. | ||
/// | ||
/// The serialization format for converting the ticket from and to bytes is left | ||
/// to the implementer. We recommend using [postcard] for serialization. | ||
/// | ||
/// [postcard]: https://docs.rs/postcard/latest/postcard/ | ||
pub trait Ticket: Sized { | ||
/// String prefix describing the kind of iroh ticket. | ||
/// | ||
/// This should be lower case ascii characters. | ||
const KIND: &'static str; | ||
|
||
/// Serialize to bytes used in the base32 string representation. | ||
fn to_bytes(&self) -> Vec<u8>; | ||
|
||
/// Deserialize from the base32 string representation bytes. | ||
fn from_bytes(bytes: &[u8]) -> Result<Self, Error>; | ||
|
||
/// Serialize to string. | ||
fn serialize(&self) -> String { | ||
let mut out = Self::KIND.to_string(); | ||
base32::fmt_append(&self.to_bytes(), &mut out); | ||
out | ||
} | ||
|
||
/// Deserialize from a string. | ||
fn deserialize(str: &str) -> Result<Self, Error> { | ||
let expected = Self::KIND; | ||
let Some(rest) = str.strip_prefix(expected) else { | ||
return Err(Error::Kind { expected }); | ||
}; | ||
let bytes = base32::parse_vec(rest)?; | ||
let ticket = Self::from_bytes(&bytes)?; | ||
Ok(ticket) | ||
} | ||
} | ||
|
||
/// An error deserializing an iroh ticket. | ||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
/// Found a ticket of with the wrong prefix, indicating the wrong kind. | ||
#[error("wrong prefix, expected {expected}")] | ||
Kind { expected: &'static str }, | ||
/// This looks like a ticket, but postcard deserialization failed. | ||
#[error("deserialization failed: {_0}")] | ||
Postcard(#[from] postcard::Error), | ||
/// This looks like a ticket, but base32 decoding failed. | ||
#[error("decoding failed: {_0}")] | ||
Encoding(#[from] base32::DecodeError), | ||
/// Verification of the deserialized bytes failed. | ||
#[error("verification failed: {_0}")] | ||
Verify(&'static str), | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//! Tickets supported by iroh-net | ||
mod blob; | ||
mod node; | ||
|
||
pub use blob::BlobTicket; | ||
pub use node::NodeTicket; |
Oops, something went wrong.