-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
740 additions
and
698 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
members = [ | ||
"iroh", | ||
"iroh-bytes", | ||
"iroh-base", | ||
"iroh-gossip", | ||
"iroh-metrics", | ||
"iroh-net", | ||
|
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,34 @@ | ||
[package] | ||
name = "iroh-base" | ||
version = "0.10.0" | ||
edition = "2021" | ||
readme = "README.md" | ||
description = "base type and utilities for Iroh" | ||
license = "MIT OR Apache-2.0" | ||
authors = ["n0 team"] | ||
repository = "/~https://github.com/n0-computer/iroh" | ||
|
||
# Sadly this also needs to be updated in .github/workflows/ci.yml | ||
rust-version = "1.72" | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
anyhow = { version = "1", features = ["backtrace"] } | ||
bao-tree = { version = "0.9.1", features = ["tokio_fsm"], default-features = false } | ||
bytes = { version = "1.4", features = ["serde"] } | ||
data-encoding = "2.3.3" | ||
hex = "0.4.3" | ||
multibase = "0.9.1" | ||
postcard = { version = "1", default-features = false, features = ["alloc", "use-std", "experimental-derive"] } | ||
serde = { version = "1", features = ["derive"] } | ||
serde-error = "0.1.2" | ||
thiserror = "1" | ||
|
||
[dev-dependencies] | ||
|
||
[features] | ||
default = ["hash", "base32"] | ||
hash = [] | ||
base32 = [] |
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,21 @@ | ||
# iroh-base | ||
|
||
This crate provides base types and utilities used throughout Iroh. | ||
|
||
# License | ||
|
||
This project is licensed under either of | ||
|
||
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or | ||
http://www.apache.org/licenses/LICENSE-2.0) | ||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or | ||
http://opensource.org/licenses/MIT) | ||
|
||
at your option. | ||
|
||
### Contribution | ||
|
||
Unless you explicitly state otherwise, any contribution intentionally submitted | ||
for inclusion in this project by you, as defined in the Apache-2.0 license, | ||
shall be dual licensed as above, without any additional terms or conditions. | ||
|
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,40 @@ | ||
pub use data_encoding::{DecodeError, DecodeKind}; | ||
|
||
/// Convert to a base32 string | ||
pub fn fmt(bytes: impl AsRef<[u8]>) -> String { | ||
let mut text = data_encoding::BASE32_NOPAD.encode(bytes.as_ref()); | ||
text.make_ascii_lowercase(); | ||
text | ||
} | ||
|
||
/// Convert to a base32 string and append out `out` | ||
pub fn fmt_append(bytes: impl AsRef<[u8]>, out: &mut String) { | ||
let start = out.len(); | ||
data_encoding::BASE32_NOPAD.encode_append(bytes.as_ref(), out); | ||
let end = out.len(); | ||
out[start..end].make_ascii_lowercase(); | ||
} | ||
|
||
/// Convert to a base32 string limited to the first 10 bytes | ||
pub fn fmt_short(bytes: impl AsRef<[u8]>) -> String { | ||
let len = bytes.as_ref().len().min(10); | ||
let mut text = data_encoding::BASE32_NOPAD.encode(&bytes.as_ref()[..len]); | ||
text.make_ascii_lowercase(); | ||
text | ||
} | ||
|
||
/// Parse from a base32 string into a byte array | ||
pub fn parse_array<const N: usize>(input: &str) -> Result<[u8; N], DecodeError> { | ||
data_encoding::BASE32_NOPAD | ||
.decode(input.to_ascii_uppercase().as_bytes())? | ||
.try_into() | ||
.map_err(|_| DecodeError { | ||
position: N, | ||
kind: DecodeKind::Length, | ||
}) | ||
} | ||
|
||
/// Decode form a base32 string to a vector of bytes | ||
pub fn parse_vec(input: &str) -> Result<Vec<u8>, DecodeError> { | ||
data_encoding::BASE32_NOPAD.decode(input.to_ascii_uppercase().as_bytes()) | ||
} |
Oops, something went wrong.