Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: switch to hex #2

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 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 @@ -64,6 +64,7 @@ tokio = { version = "1", features = ["sync", "macros"] }
proptest = "1.2.0"
tempfile = "3.4"
test-strategy = "0.3.1"
data-encoding = "2.6.0"

[features]
default = ["net", "metrics", "engine"]
Expand Down
2 changes: 1 addition & 1 deletion src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl LiveEvent {
/// engine. Changing the default author will not be persisted.
///
/// If set to `Persistent`, the default author will be loaded from and persisted to the specified
/// path (as base32 encoded string of the author's public key).
/// path (as hex encoded string of the author's public key).
#[derive(Debug)]
pub enum DefaultAuthorStorage {
/// Memory storage.
Expand Down
39 changes: 22 additions & 17 deletions src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::{cmp::Ordering, fmt, str::FromStr};

use ed25519_dalek::{Signature, SignatureError, Signer, SigningKey, VerifyingKey};
use iroh_base::base32;
use rand_core::CryptoRngCore;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -160,37 +159,37 @@ impl NamespacePublicKey {

impl fmt::Display for Author {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", base32::fmt(self.to_bytes()))
write!(f, "{}", hex::encode(self.to_bytes()))
}
}

impl fmt::Display for NamespaceSecret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", base32::fmt(self.to_bytes()))
write!(f, "{}", hex::encode(self.to_bytes()))
}
}

impl fmt::Display for AuthorPublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", base32::fmt(self.as_bytes()))
write!(f, "{}", hex::encode(self.as_bytes()))
}
}

impl fmt::Display for NamespacePublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", base32::fmt(self.as_bytes()))
write!(f, "{}", hex::encode(self.as_bytes()))
}
}

impl fmt::Display for AuthorId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", base32::fmt(self.as_bytes()))
write!(f, "{}", hex::encode(self.as_bytes()))
}
}

impl fmt::Display for NamespaceId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", base32::fmt(self.as_bytes()))
write!(f, "{}", hex::encode(self.as_bytes()))
}
}

Expand All @@ -202,13 +201,13 @@ impl fmt::Debug for NamespaceSecret {

impl fmt::Debug for NamespaceId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NamespaceId({})", base32::fmt_short(self.0))
write!(f, "NamespaceId({})", hex::encode(self.0))
}
}

impl fmt::Debug for AuthorId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "AuthorId({})", base32::fmt_short(self.0))
write!(f, "AuthorId({})", hex::encode(self.0))
}
}

Expand All @@ -230,35 +229,41 @@ impl fmt::Debug for AuthorPublicKey {
}
}

fn parse_hex_array(s: &str) -> anyhow::Result<[u8; 32]> {
let mut bytes = [0u8; 32];
hex::decode_to_slice(s, &mut bytes)?;
Ok(bytes)
}

impl FromStr for Author {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from_bytes(&base32::parse_array(s)?))
Ok(Self::from_bytes(&parse_hex_array(s)?))
}
}

impl FromStr for NamespaceSecret {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from_bytes(&base32::parse_array(s)?))
Ok(Self::from_bytes(&parse_hex_array(s)?))
}
}

impl FromStr for AuthorPublicKey {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_bytes(&base32::parse_array(s)?).map_err(Into::into)
Self::from_bytes(&parse_hex_array(s)?).map_err(Into::into)
}
}

impl FromStr for NamespacePublicKey {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::from_bytes(&base32::parse_array(s)?).map_err(Into::into)
Self::from_bytes(&parse_hex_array(s)?).map_err(Into::into)
}
}

Expand Down Expand Up @@ -386,10 +391,10 @@ impl AuthorId {
AuthorPublicKey::from_bytes(&self.0)
}

/// Convert to a base32 string limited to the first 10 bytes for a friendly string
/// Convert to a hex string limited to the first 10 bytes for a friendly string
/// representation of the key.
pub fn fmt_short(&self) -> String {
base32::fmt_short(self.0)
hex::encode(self.0).chars().take(10).collect()
}
}

Expand Down Expand Up @@ -421,10 +426,10 @@ impl NamespaceId {
NamespacePublicKey::from_bytes(&self.0)
}

/// Convert to a base32 string limited to the first 10 bytes for a friendly string
/// Convert to a hex string limited to the first 10 bytes for a friendly string
/// representation of the key.
pub fn fmt_short(&self) -> String {
base32::fmt_short(self.0)
hex::encode(self.0).chars().take(10).collect()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{

use bytes::{Bytes, BytesMut};
use ed25519_dalek::{Signature, SignatureError};
use iroh_base::{base32, hash::Hash};
use iroh_base::hash::Hash;
#[cfg(feature = "metrics")]
use iroh_metrics::{inc, inc_by};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -826,11 +826,11 @@ impl Debug for EntrySignature {
f.debug_struct("EntrySignature")
.field(
"namespace_signature",
&base32::fmt(self.namespace_signature.to_bytes()),
&hex::encode(self.namespace_signature.to_bytes()),
)
.field(
"author_signature",
&base32::fmt(self.author_signature.to_bytes()),
&hex::encode(self.author_signature.to_bytes()),
)
.finish()
}
Expand Down
11 changes: 9 additions & 2 deletions src/ticket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ impl std::str::FromStr for DocTicket {
mod tests {
use std::str::FromStr;

use iroh_base::base32;
use iroh_net::key::PublicKey;
use iroh_test::{assert_eq_hex, hexdump::parse_hexdump};

Expand All @@ -89,7 +88,15 @@ mod tests {
capability: Capability::Read(namespace_id),
nodes: vec![NodeAddr::from_parts(node_id, None, [])],
};
let base32 = base32::parse_vec(ticket.to_string().strip_prefix("doc").unwrap()).unwrap();
let s = ticket.to_string();
let base32 = data_encoding::BASE32_NOPAD
.decode(
s.strip_prefix("doc")
.unwrap()
.to_ascii_uppercase()
.as_bytes(),
)
.unwrap();
let expected = parse_hexdump("
00 # variant
01 # capability discriminator, 1 = read
Expand Down
Loading