Skip to content

Commit d74c87f

Browse files
committed
refactor from_str fun
1 parent 90ae34c commit d74c87f

File tree

1 file changed

+34
-50
lines changed

1 file changed

+34
-50
lines changed

lightning/src/ln/msgs.rs

+34-50
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ impl Readable for NetAddress {
904904
}
905905
}
906906

907+
907908
/// NetAddress error variants
908909
#[cfg(feature = "std")]
909910
#[derive(Debug, Eq, PartialEq, Clone)]
@@ -918,25 +919,22 @@ pub enum NetAddressError {
918919
InvalidOnionV3,
919920
}
920921

922+
impl From<std::net::SocketAddr> for NetAddress {
923+
fn from(addr: std::net::SocketAddr) -> Self {
924+
match addr {
925+
std::net::SocketAddr::V4(addr) => NetAddress::IPv4 { addr: addr.ip().octets(), port: addr.port() },
926+
std::net::SocketAddr::V6(addr) => NetAddress::IPv6 { addr: addr.ip().octets(), port: addr.port() },
927+
}
928+
}
929+
}
930+
921931
#[cfg(feature = "std")]
922932
impl FromStr for NetAddress {
923933
type Err = NetAddressError;
924934

925935
fn from_str(s: &str) -> Result<Self, Self::Err> {
926936
match std::net::SocketAddr::from_str(s) {
927-
Ok(addr) => {
928-
let port: u16 = addr.port();
929-
match addr {
930-
std::net::SocketAddr::V4(addr) => {
931-
let addr = addr.ip().octets();
932-
return Ok(NetAddress::IPv4 { addr, port });
933-
},
934-
std::net::SocketAddr::V6(addr) => {
935-
let addr = addr.ip().octets();
936-
return Ok(NetAddress::IPv6 { addr, port });
937-
},
938-
}
939-
},
937+
Ok(addr) => Ok(addr.into()),
940938
Err(e) => {
941939
let trimmed_input = match s.rfind(":") {
942940
Some(pos) => pos,
@@ -947,44 +945,30 @@ impl FromStr for NetAddress {
947945
Ok(port) => port,
948946
Err(_) => return Err(NetAddressError::InvalidPort),
949947
};
950-
let host: Vec<&str> = host.split(".").collect();
951-
if host.len() == 2 {
952-
match (host.get(0), host.get(1)) {
953-
(Some(domain), Some(suffix)) => {
954-
if suffix.to_string() == "onion".to_owned() {
955-
if domain.len() != 56 {
956-
return Err(NetAddressError::InvalidOnionV3);
957-
}
958-
let onion = match (base32::Alphabet::RFC4648 { padding: false }.decode(&domain)) {
959-
Ok(onion) => onion,
960-
Err(_) => return Err(NetAddressError::InvalidOnionV3),
961-
};
962-
if onion.len() < 35 {
963-
return Err(NetAddressError::InvalidOnionV3);
964-
}
965-
match (onion.get(0), onion.get(1), onion.get(2)) {
966-
(Some(version), Some(first_checksum_flag), Some(second_checksum_flag)) => {
967-
let checksum = u16::from_be_bytes([*first_checksum_flag, *second_checksum_flag]);
968-
let ed25519_pubkey = match onion[3..35].try_into() {
969-
Ok(ed25519_pubkey) => ed25519_pubkey,
970-
Err(_) => return Err(NetAddressError::InvalidOnionV3),
971-
};
972-
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version: *version, port });
973-
974-
},
975-
_ => return Err(NetAddressError::InvalidOnionV3),
976-
}
977-
} else {
978-
if let Ok(hostname) = Hostname::try_from(s[..trimmed_input].to_string()) {
979-
return Ok(NetAddress::Hostname { hostname, port });
980-
} else {
981-
return Err(NetAddressError::InvalidInput("Invalid input. Expected format: \"<host>:<port>\"" .to_string()));
982-
}
983-
}
984-
},
985-
_ => return Err(NetAddressError::SocketAdrrParseError(e))
948+
if host.ends_with(".onion") {
949+
let domain = host.trim_end_matches(".onion");
950+
if domain.len() != 56 {
951+
return Err(NetAddressError::InvalidOnionV3);
952+
}
953+
let onion = match (base32::Alphabet::RFC4648 { padding: false }.decode(&domain)) {
954+
Ok(onion) => onion,
955+
Err(_) => return Err(NetAddressError::InvalidOnionV3),
986956
};
987-
}
957+
if onion.len() < 35 {
958+
return Err(NetAddressError::InvalidOnionV3);
959+
}
960+
let version = onion[0];
961+
let first_checksum_flag = onion[1];
962+
let second_checksum_flag = onion[2];
963+
let mut ed25519_pubkey = [0;32];
964+
ed25519_pubkey.copy_from_slice(&onion[3..35]);
965+
let checksum = u16::from_be_bytes([first_checksum_flag, second_checksum_flag]);
966+
return Ok(NetAddress::OnionV3 { ed25519_pubkey, checksum, version, port });
967+
968+
};
969+
if let Ok(hostname) = Hostname::try_from(s[..trimmed_input].to_string()) {
970+
return Ok(NetAddress::Hostname { hostname, port });
971+
};
988972
return Err(NetAddressError::SocketAdrrParseError(e))
989973
},
990974
}

0 commit comments

Comments
 (0)