Skip to content

Commit

Permalink
refactor: add iroh-base crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Nov 8, 2023
1 parent c4514aa commit 62e886e
Show file tree
Hide file tree
Showing 46 changed files with 740 additions and 698 deletions.
22 changes: 21 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"iroh",
"iroh-bytes",
"iroh-base",
"iroh-gossip",
"iroh-metrics",
"iroh-net",
Expand Down
34 changes: 34 additions & 0 deletions iroh-base/Cargo.toml
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 = []
21 changes: 21 additions & 0 deletions iroh-base/README.md
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.

40 changes: 40 additions & 0 deletions iroh-base/src/base32.rs
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())
}
Loading

0 comments on commit 62e886e

Please sign in to comment.