Skip to content

Commit

Permalink
refactor: common base library (#1780)
Browse files Browse the repository at this point in the history
## Description

This adds a new crate `iroh_base`. The following types were moved into
the new crate:

* `Hash`, `HashAndFormat` and `BlobFormat` from `iroh_bytes::util`
* `base32` encoding/decoding util functions. They lived at various
places before.
* `RpcError` and `RpcResult` from `iroh_bytes::util`

## Notes & open questions

* Naming: Shall it be `iroh_common` or `iroh_base`? I do not mind. If
anyhow has a preference, please raise. I can change easily.
* Anything else that should be moved over? Anything that I moved that
should *not* be moved?

## Change checklist

- [ ] Self-review.
- [ ] Documentation updates if relevant.
- [ ] Tests if relevant.

---------

Co-authored-by: Asmir Avdicevic <asmir.avdicevic64@gmail.com>
  • Loading branch information
Frando and Arqu committed Nov 8, 2023
1 parent c4514aa commit de58d71
Show file tree
Hide file tree
Showing 49 changed files with 761 additions and 709 deletions.
26 changes: 24 additions & 2 deletions 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
37 changes: 37 additions & 0 deletions iroh-base/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[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, optional = true }
data-encoding = { version = "2.3.3", optional = true }
hex = "0.4.3"
multibase = { version = "0.9.1", optional = true }
postcard = { version = "1", default-features = false, features = ["alloc", "use-std", "experimental-derive"], optional = true }
serde = { version = "1", features = ["derive"] }
serde-error = "0.1.2"
thiserror = "1"

[dev-dependencies]
iroh-test = { path = "../iroh-test" }
proptest = "1.0.0"
serde_json = "1.0.107"
serde_test = "1.0.176"

[features]
default = ["hash", "base32"]
hash = ["bao-tree", "multibase", "data-encoding", "postcard"]
base32 = ["data-encoding"]
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 de58d71

Please sign in to comment.