Skip to content

Commit

Permalink
InetAddr: Use onion addresses instead of public keys
Browse files Browse the repository at this point in the history
Define new Tor enum inner types for the `InetAddr` `PartialSocketAddr`, and
`InetSocketAddr` types. Replace the usage of the `TorPublicKeyV3` with
`OnionAddressV3`. While the two types are interchangeable,
`OnionAddressV3` is the human-readable format.

Additionally, adds port numbers to the `PartialSocketAddr` and
`InetSocketAddr` tor variants. This allows users to specify a specific
port on which an onion address is listening.
  • Loading branch information
TheCharlatan committed Nov 17, 2022
1 parent d271c7e commit f646f26
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 67 deletions.
69 changes: 52 additions & 17 deletions addr/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ use strict_encoding::net::{
AddrFormat, DecodeError, RawAddr, Transport, Uniform, UniformAddr,
};
#[cfg(feature = "tor")]
use torut::onion::{TorPublicKeyV3, TORV3_PUBLIC_KEY_LENGTH};
use torut::onion::{OnionAddressV3, TorPublicKeyV3, TORV3_PUBLIC_KEY_LENGTH};

use crate::inet::PartialSocketAddr;
#[cfg(feature = "tor")]
use crate::inet::TorAddrV3;
use crate::{InetAddr, InetSocketAddr, InetSocketAddrExt};

impl strict_encoding::Strategy for InetAddr {
Expand Down Expand Up @@ -58,17 +60,21 @@ impl Uniform for InetAddr {
InetAddr::Tor(tor) => {
use strict_encoding::net::ADDR_LEN;
let mut buf = [0u8; ADDR_LEN];
buf[1..].copy_from_slice(&tor.to_bytes());
buf[1..].copy_from_slice(&tor.get_public_key().to_bytes());
buf
}
}
}

#[inline]
fn port(&self) -> Option<u16> { None }
fn port(&self) -> Option<u16> {
None
}

#[inline]
fn transport(&self) -> Option<Transport> { None }
fn transport(&self) -> Option<Transport> {
None
}

#[inline]
fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
Expand Down Expand Up @@ -101,13 +107,21 @@ impl Uniform for InetAddr {
}

impl Uniform for PartialSocketAddr {
fn addr_format(&self) -> AddrFormat { self.address().addr_format() }
fn addr_format(&self) -> AddrFormat {
self.address().addr_format()
}

fn addr(&self) -> RawAddr { self.address().addr() }
fn addr(&self) -> RawAddr {
self.address().addr()
}

fn port(&self) -> Option<u16> { PartialSocketAddr::port(*self) }
fn port(&self) -> Option<u16> {
PartialSocketAddr::port(*self)
}

fn transport(&self) -> Option<Transport> { None }
fn transport(&self) -> Option<Transport> {
None
}

fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
where
Expand All @@ -134,10 +148,14 @@ impl Uniform for PartialSocketAddr {

impl Uniform for InetSocketAddr {
#[inline]
fn addr_format(&self) -> AddrFormat { self.address().addr_format() }
fn addr_format(&self) -> AddrFormat {
self.address().addr_format()
}

#[inline]
fn addr(&self) -> RawAddr { self.address().addr() }
fn addr(&self) -> RawAddr {
self.address().addr()
}

#[inline]
fn port(&self) -> Option<u16> {
Expand All @@ -150,7 +168,9 @@ impl Uniform for InetSocketAddr {
}

#[inline]
fn transport(&self) -> Option<Transport> { None }
fn transport(&self) -> Option<Transport> {
None
}

#[inline]
fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
Expand All @@ -177,7 +197,14 @@ impl Uniform for InetSocketAddr {
),
#[cfg(feature = "tor")]
AddrFormat::OnionV3 => {
InetSocketAddr::Tor(tor_from_raw_addr(addr.addr)?)
if let Some(port) = addr.port {
InetSocketAddr::Tor(TorAddrV3::new(
tor_from_raw_addr(addr.addr)?,
port,
))
} else {
return Err(DecodeError::InsufficientData);
}
}
_ => return Err(DecodeError::UnsupportedAddrFormat),
})
Expand All @@ -186,13 +213,19 @@ impl Uniform for InetSocketAddr {

impl Uniform for InetSocketAddrExt {
#[inline]
fn addr_format(&self) -> AddrFormat { self.1.addr_format() }
fn addr_format(&self) -> AddrFormat {
self.1.addr_format()
}

#[inline]
fn addr(&self) -> RawAddr { self.1.addr() }
fn addr(&self) -> RawAddr {
self.1.addr()
}

#[inline]
fn port(&self) -> Option<u16> { self.1.port() }
fn port(&self) -> Option<u16> {
self.1.port()
}

#[inline]
fn transport(&self) -> Option<Transport> {
Expand Down Expand Up @@ -234,8 +267,10 @@ impl Uniform for InetSocketAddrExt {
}

#[cfg(feature = "tor")]
fn tor_from_raw_addr(raw: RawAddr) -> Result<TorPublicKeyV3, DecodeError> {
fn tor_from_raw_addr(raw: RawAddr) -> Result<OnionAddressV3, DecodeError> {
let mut a = [0u8; TORV3_PUBLIC_KEY_LENGTH];
a.copy_from_slice(&raw[1..]);
TorPublicKeyV3::from_bytes(&a).map_err(|_| DecodeError::InvalidPubkey)
TorPublicKeyV3::from_bytes(&a)
.map_err(|_| DecodeError::InvalidPubkey)
.map(|key| key.get_onion_address())
}
Loading

0 comments on commit f646f26

Please sign in to comment.