Skip to content

Commit e8fa3da

Browse files
refactor: switch to hex
Ref n0-computer/iroh#2841
1 parent 6b2c973 commit e8fa3da

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ tokio = { version = "1", features = ["sync", "macros"] }
6464
proptest = "1.2.0"
6565
tempfile = "3.4"
6666
test-strategy = "0.3.1"
67+
data-encoding = "2.6.0"
6768

6869
[features]
6970
default = ["net", "metrics", "engine"]

src/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl LiveEvent {
296296
/// engine. Changing the default author will not be persisted.
297297
///
298298
/// If set to `Persistent`, the default author will be loaded from and persisted to the specified
299-
/// path (as base32 encoded string of the author's public key).
299+
/// path (as hex encoded string of the author's public key).
300300
#[derive(Debug)]
301301
pub enum DefaultAuthorStorage {
302302
/// Memory storage.

src/keys.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::{cmp::Ordering, fmt, str::FromStr};
44

55
use ed25519_dalek::{Signature, SignatureError, Signer, SigningKey, VerifyingKey};
6-
use iroh_base::base32;
76
use rand_core::CryptoRngCore;
87
use serde::{Deserialize, Serialize};
98

@@ -160,37 +159,37 @@ impl NamespacePublicKey {
160159

161160
impl fmt::Display for Author {
162161
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163-
write!(f, "{}", base32::fmt(self.to_bytes()))
162+
write!(f, "{}", hex::encode(self.to_bytes()))
164163
}
165164
}
166165

167166
impl fmt::Display for NamespaceSecret {
168167
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169-
write!(f, "{}", base32::fmt(self.to_bytes()))
168+
write!(f, "{}", hex::encode(self.to_bytes()))
170169
}
171170
}
172171

173172
impl fmt::Display for AuthorPublicKey {
174173
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175-
write!(f, "{}", base32::fmt(self.as_bytes()))
174+
write!(f, "{}", hex::encode(self.as_bytes()))
176175
}
177176
}
178177

179178
impl fmt::Display for NamespacePublicKey {
180179
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181-
write!(f, "{}", base32::fmt(self.as_bytes()))
180+
write!(f, "{}", hex::encode(self.as_bytes()))
182181
}
183182
}
184183

185184
impl fmt::Display for AuthorId {
186185
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
187-
write!(f, "{}", base32::fmt(self.as_bytes()))
186+
write!(f, "{}", hex::encode(self.as_bytes()))
188187
}
189188
}
190189

191190
impl fmt::Display for NamespaceId {
192191
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
193-
write!(f, "{}", base32::fmt(self.as_bytes()))
192+
write!(f, "{}", hex::encode(self.as_bytes()))
194193
}
195194
}
196195

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

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

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

@@ -230,35 +229,41 @@ impl fmt::Debug for AuthorPublicKey {
230229
}
231230
}
232231

232+
fn parse_hex_array(s: &str) -> anyhow::Result<[u8; 32]> {
233+
let mut bytes = [0u8; 32];
234+
hex::decode_to_slice(s, &mut bytes)?;
235+
Ok(bytes)
236+
}
237+
233238
impl FromStr for Author {
234239
type Err = anyhow::Error;
235240

236241
fn from_str(s: &str) -> Result<Self, Self::Err> {
237-
Ok(Self::from_bytes(&base32::parse_array(s)?))
242+
Ok(Self::from_bytes(&parse_hex_array(s)?))
238243
}
239244
}
240245

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

244249
fn from_str(s: &str) -> Result<Self, Self::Err> {
245-
Ok(Self::from_bytes(&base32::parse_array(s)?))
250+
Ok(Self::from_bytes(&parse_hex_array(s)?))
246251
}
247252
}
248253

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

252257
fn from_str(s: &str) -> Result<Self, Self::Err> {
253-
Self::from_bytes(&base32::parse_array(s)?).map_err(Into::into)
258+
Self::from_bytes(&parse_hex_array(s)?).map_err(Into::into)
254259
}
255260
}
256261

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

260265
fn from_str(s: &str) -> Result<Self, Self::Err> {
261-
Self::from_bytes(&base32::parse_array(s)?).map_err(Into::into)
266+
Self::from_bytes(&parse_hex_array(s)?).map_err(Into::into)
262267
}
263268
}
264269

@@ -386,10 +391,10 @@ impl AuthorId {
386391
AuthorPublicKey::from_bytes(&self.0)
387392
}
388393

389-
/// Convert to a base32 string limited to the first 10 bytes for a friendly string
394+
/// Convert to a hex string limited to the first 10 bytes for a friendly string
390395
/// representation of the key.
391396
pub fn fmt_short(&self) -> String {
392-
base32::fmt_short(self.0)
397+
hex::encode(self.0).chars().take(10).collect()
393398
}
394399
}
395400

@@ -421,10 +426,10 @@ impl NamespaceId {
421426
NamespacePublicKey::from_bytes(&self.0)
422427
}
423428

424-
/// Convert to a base32 string limited to the first 10 bytes for a friendly string
429+
/// Convert to a hex string limited to the first 10 bytes for a friendly string
425430
/// representation of the key.
426431
pub fn fmt_short(&self) -> String {
427-
base32::fmt_short(self.0)
432+
hex::encode(self.0).chars().take(10).collect()
428433
}
429434
}
430435

src/sync.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616

1717
use bytes::{Bytes, BytesMut};
1818
use ed25519_dalek::{Signature, SignatureError};
19-
use iroh_base::{base32, hash::Hash};
19+
use iroh_base::hash::Hash;
2020
#[cfg(feature = "metrics")]
2121
use iroh_metrics::{inc, inc_by};
2222
use serde::{Deserialize, Serialize};
@@ -826,11 +826,11 @@ impl Debug for EntrySignature {
826826
f.debug_struct("EntrySignature")
827827
.field(
828828
"namespace_signature",
829-
&base32::fmt(self.namespace_signature.to_bytes()),
829+
&hex::encode(self.namespace_signature.to_bytes()),
830830
)
831831
.field(
832832
"author_signature",
833-
&base32::fmt(self.author_signature.to_bytes()),
833+
&hex::encode(self.author_signature.to_bytes()),
834834
)
835835
.finish()
836836
}

src/ticket.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ impl std::str::FromStr for DocTicket {
6565
mod tests {
6666
use std::str::FromStr;
6767

68-
use iroh_base::base32;
6968
use iroh_net::key::PublicKey;
7069
use iroh_test::{assert_eq_hex, hexdump::parse_hexdump};
7170

@@ -89,7 +88,8 @@ mod tests {
8988
capability: Capability::Read(namespace_id),
9089
nodes: vec![NodeAddr::from_parts(node_id, None, [])],
9190
};
92-
let base32 = base32::parse_vec(ticket.to_string().strip_prefix("doc").unwrap()).unwrap();
91+
let s = ticket.to_string();
92+
let base32 = data_encoding::BASE32_NOPAD.decode(s.strip_prefix("doc").unwrap().to_ascii_uppercase().as_bytes()).unwrap();
9393
let expected = parse_hexdump("
9494
00 # variant
9595
01 # capability discriminator, 1 = read

0 commit comments

Comments
 (0)