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

examples: minimize use of raw base32 in examples #2304

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 4 additions & 12 deletions iroh-gossip/examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ async fn main() -> anyhow::Result<()> {
// parse or generate our secret key
let secret_key = match args.secret_key {
None => SecretKey::generate(),
Some(key) => parse_secret_key(&key)?,
Some(key) => key.parse()?,
};
println!("> our secret key: {}", base32::fmt(secret_key.to_bytes()));
println!("> our secret key: {secret_key}");

// confgure our relay map
let relay_mode = match (args.no_relay, args.relay) {
Expand Down Expand Up @@ -176,12 +176,12 @@ async fn subscribe_loop(gossip: Gossip, topic: TopicId) -> anyhow::Result<()> {
match message {
Message::AboutMe { name } => {
names.insert(from, name.clone());
println!("> {} is now known as {}", fmt_node_id(&from), name);
println!("> {} is now known as {}", from.fmt_short(), name);
}
Message::Message { text } => {
let name = names
.get(&from)
.map_or_else(|| fmt_node_id(&from), String::to_string);
.map_or_else(|| from.fmt_short(), String::to_string);
println!("{}: {}", name, text);
}
}
Expand Down Expand Up @@ -295,14 +295,6 @@ impl FromStr for Ticket {

// helpers

fn fmt_node_id(input: &PublicKey) -> String {
base32::fmt_short(input.as_bytes())
}
fn parse_secret_key(secret: &str) -> anyhow::Result<SecretKey> {
let bytes: [u8; 32] = base32::parse_array(secret)?;
Ok(SecretKey::from(bytes))
}

fn fmt_relay_mode(relay_mode: &RelayMode) -> String {
match relay_mode {
RelayMode::Disabled => "None".to_string(),
Expand Down
3 changes: 1 addition & 2 deletions iroh-net/examples/connect-unreliable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::net::SocketAddr;
use anyhow::Context;
use clap::Parser;
use futures_lite::StreamExt;
use iroh_base::base32;
use iroh_net::{
key::SecretKey,
relay::{RelayMode, RelayUrl},
Expand Down Expand Up @@ -40,7 +39,7 @@ async fn main() -> anyhow::Result<()> {
println!("\nconnect (unreliable) example!\n");
let args = Cli::parse();
let secret_key = SecretKey::generate();
println!("secret key: {}", base32::fmt(secret_key.to_bytes()));
println!("secret key: {secret_key}");

// Build a `Endpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers.
let endpoint = Endpoint::builder()
Expand Down
3 changes: 1 addition & 2 deletions iroh-net/examples/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::net::SocketAddr;
use anyhow::Context;
use clap::Parser;
use futures_lite::StreamExt;
use iroh_base::base32;
use iroh_net::relay::RelayUrl;
use iroh_net::{key::SecretKey, relay::RelayMode, Endpoint, NodeAddr};
use tracing::info;
Expand All @@ -37,7 +36,7 @@ async fn main() -> anyhow::Result<()> {
println!("\nconnect example!\n");
let args = Cli::parse();
let secret_key = SecretKey::generate();
println!("secret key: {}", base32::fmt(secret_key.to_bytes()));
println!("secret key: {secret_key}");

// Build a `Endpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers.
let endpoint = Endpoint::builder()
Expand Down
3 changes: 1 addition & 2 deletions iroh-net/examples/listen-unreliable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! $ cargo run --example listen-unreliable
use anyhow::Context;
use futures_lite::StreamExt;
use iroh_base::base32;
use iroh_net::{key::SecretKey, relay::RelayMode, Endpoint};
use tracing::info;

Expand All @@ -17,7 +16,7 @@ async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
println!("\nlisten (unreliable) example!\n");
let secret_key = SecretKey::generate();
println!("secret key: {}", base32::fmt(secret_key.to_bytes()));
println!("secret key: {secret_key}");

// Build a `Endpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers.
let endpoint = Endpoint::builder()
Expand Down
3 changes: 1 addition & 2 deletions iroh-net/examples/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! $ cargo run --example listen
use anyhow::Context;
use futures_lite::StreamExt;
use iroh_base::base32;
use iroh_net::{key::SecretKey, relay::RelayMode, Endpoint};
use tracing::{debug, info};

Expand All @@ -17,7 +16,7 @@ async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
println!("\nlisten example!\n");
let secret_key = SecretKey::generate();
println!("secret key: {}", base32::fmt(secret_key.to_bytes()));
println!("secret key: {secret_key}");

// Build a `Endpoint`, which uses PublicKeys as node identifiers, uses QUIC for directly connecting to other nodes, and uses the relay protocol and relay servers to holepunch direct connections between nodes when there are NATs or firewalls preventing direct connections. If no direct connection can be made, packets are relayed over the relay servers.
let endpoint = Endpoint::builder()
Expand Down
2 changes: 1 addition & 1 deletion iroh/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn main() -> anyhow::Result<()> {
fn fmt_entry(entry: &Entry) -> String {
let id = entry.id();
let key = std::str::from_utf8(id.key()).unwrap_or("<bad key>");
let author = base32::fmt_short(id.author());
let author = id.author().fmt_short();
let hash = entry.content_hash();
let hash = base32::fmt_short(hash.as_bytes());
let len = HumanBytes(entry.content_len());
Expand Down
Loading