Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed May 1, 2023
1 parent 75ec6d6 commit 471c5d7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
40 changes: 29 additions & 11 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,26 +671,44 @@ impl FromStr for NetAddress {
let port: u16 = addr.port();
match addr {
SocketAddr::V4(addr) => {
let addr = addr.ip().to_string().parse::<Ipv4Addr>()?;
return Ok(NetAddress::IPv4 { addr: addr.octets(), port });
let addr = addr.ip().octets();
return Ok(NetAddress::IPv4 { addr, port });
},
SocketAddr::V6(addr) => {
let addr = addr.ip().to_string().parse::<Ipv6Addr>()?;
return Ok(NetAddress::IPv6 { addr: addr.octets(), port });
let addr = addr.ip().octets();
return Ok(NetAddress::IPv6 { addr, port });
},
}
},
Err(e) => {
let trimmed_input = s.rfind(":").expect("Invalid input, needs to include seperator \":\"");
let trimmed_input = match s.rfind(":") {
Some(pos) => pos,
None => return Err(e),
};
let host = &s[..trimmed_input];
let port: u16 = s[trimmed_input + 1..].parse().expect("Invalid input, port needs to be u16");
let port: u16 = match s[trimmed_input + 1..].parse() {
Ok(port) => port,
Err(_) => return Err(e),
};
if host.ends_with(".onion") {
let onion = host.split(".onion").collect::<Vec<&str>>()[0];
let onion = base32::decode(base32::Alphabet::RFC4648 { padding: false }, &onion).expect("Error decoding onion address");
let version = onion[0];
let onion = match host.split(".onion").nth(0) {
Some(onion) => onion,
None => return Err(e),
};
let onion = match base32::decode(base32::Alphabet::RFC4648 { padding: false }, &onion) {
Some(onion) => onion,
None => return Err(e),
};
let version = match onion.get(0) {
Some(version) => version,
None => return Err(e),
};
let checksum = u16::from_be_bytes([onion[1], onion[2]]);
let ed25519_pubkey = onion[3..35].try_into().expect("Invalid onion address");
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version, port });
let ed25519_pubkey = match onion[3..35].try_into() {
Ok(ed25519_pubkey) => ed25519_pubkey,
Err(_) => return Err(e),
};
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version: *version, port });
}

if let Ok(hostname) = Hostname::try_from(host.to_string()) {
Expand Down
9 changes: 6 additions & 3 deletions lightning/src/util/base32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
/// Alphabet used for encoding and decoding.
#[derive(Copy, Clone)]
pub enum Alphabet {
/// RFC4648 base32 encoding. padding: bool indicates whether to use padding.
RFC4648 { padding: bool },
/// RFC4648 base32 encoding.
RFC4648 {
/// Whether to use padding.
padding: bool
},
/// Crockford base32 encoding.
Crockford,
}
Expand Down Expand Up @@ -88,7 +91,7 @@ const CROCKFORD_INV_ALPHABET: [i8; 43] = [
18, 19, 1, 20, 21, 0, 22, 23, 24, 25, 26, -1, 27, 28, 29, 30, 31,
];

// Decode a base32 string into a byte vector.
/// Decode a base32 string into a byte vector.
pub fn decode(alphabet: Alphabet, data: &str) -> Option<Vec<u8>> {
let data = data.as_bytes();
let alphabet = match alphabet {
Expand Down
4 changes: 4 additions & 0 deletions lightning/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub mod invoice;
pub mod persist;
pub mod string;
pub mod wakers;
#[cfg(fuzzing)]
pub mod base32;
/// base32 utils
#[cfg(not(fuzzing))]
pub mod base32;

pub(crate) mod atomic_counter;
Expand Down

0 comments on commit 471c5d7

Please sign in to comment.