Skip to content

Commit f646f26

Browse files
committed
InetAddr: Use onion addresses instead of public keys
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.
1 parent d271c7e commit f646f26

File tree

2 files changed

+220
-67
lines changed

2 files changed

+220
-67
lines changed

addr/src/encoding.rs

+52-17
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ use strict_encoding::net::{
1717
AddrFormat, DecodeError, RawAddr, Transport, Uniform, UniformAddr,
1818
};
1919
#[cfg(feature = "tor")]
20-
use torut::onion::{TorPublicKeyV3, TORV3_PUBLIC_KEY_LENGTH};
20+
use torut::onion::{OnionAddressV3, TorPublicKeyV3, TORV3_PUBLIC_KEY_LENGTH};
2121

2222
use crate::inet::PartialSocketAddr;
23+
#[cfg(feature = "tor")]
24+
use crate::inet::TorAddrV3;
2325
use crate::{InetAddr, InetSocketAddr, InetSocketAddrExt};
2426

2527
impl strict_encoding::Strategy for InetAddr {
@@ -58,17 +60,21 @@ impl Uniform for InetAddr {
5860
InetAddr::Tor(tor) => {
5961
use strict_encoding::net::ADDR_LEN;
6062
let mut buf = [0u8; ADDR_LEN];
61-
buf[1..].copy_from_slice(&tor.to_bytes());
63+
buf[1..].copy_from_slice(&tor.get_public_key().to_bytes());
6264
buf
6365
}
6466
}
6567
}
6668

6769
#[inline]
68-
fn port(&self) -> Option<u16> { None }
70+
fn port(&self) -> Option<u16> {
71+
None
72+
}
6973

7074
#[inline]
71-
fn transport(&self) -> Option<Transport> { None }
75+
fn transport(&self) -> Option<Transport> {
76+
None
77+
}
7278

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

103109
impl Uniform for PartialSocketAddr {
104-
fn addr_format(&self) -> AddrFormat { self.address().addr_format() }
110+
fn addr_format(&self) -> AddrFormat {
111+
self.address().addr_format()
112+
}
105113

106-
fn addr(&self) -> RawAddr { self.address().addr() }
114+
fn addr(&self) -> RawAddr {
115+
self.address().addr()
116+
}
107117

108-
fn port(&self) -> Option<u16> { PartialSocketAddr::port(*self) }
118+
fn port(&self) -> Option<u16> {
119+
PartialSocketAddr::port(*self)
120+
}
109121

110-
fn transport(&self) -> Option<Transport> { None }
122+
fn transport(&self) -> Option<Transport> {
123+
None
124+
}
111125

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

135149
impl Uniform for InetSocketAddr {
136150
#[inline]
137-
fn addr_format(&self) -> AddrFormat { self.address().addr_format() }
151+
fn addr_format(&self) -> AddrFormat {
152+
self.address().addr_format()
153+
}
138154

139155
#[inline]
140-
fn addr(&self) -> RawAddr { self.address().addr() }
156+
fn addr(&self) -> RawAddr {
157+
self.address().addr()
158+
}
141159

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

152170
#[inline]
153-
fn transport(&self) -> Option<Transport> { None }
171+
fn transport(&self) -> Option<Transport> {
172+
None
173+
}
154174

155175
#[inline]
156176
fn from_uniform_addr(addr: UniformAddr) -> Result<Self, DecodeError>
@@ -177,7 +197,14 @@ impl Uniform for InetSocketAddr {
177197
),
178198
#[cfg(feature = "tor")]
179199
AddrFormat::OnionV3 => {
180-
InetSocketAddr::Tor(tor_from_raw_addr(addr.addr)?)
200+
if let Some(port) = addr.port {
201+
InetSocketAddr::Tor(TorAddrV3::new(
202+
tor_from_raw_addr(addr.addr)?,
203+
port,
204+
))
205+
} else {
206+
return Err(DecodeError::InsufficientData);
207+
}
181208
}
182209
_ => return Err(DecodeError::UnsupportedAddrFormat),
183210
})
@@ -186,13 +213,19 @@ impl Uniform for InetSocketAddr {
186213

187214
impl Uniform for InetSocketAddrExt {
188215
#[inline]
189-
fn addr_format(&self) -> AddrFormat { self.1.addr_format() }
216+
fn addr_format(&self) -> AddrFormat {
217+
self.1.addr_format()
218+
}
190219

191220
#[inline]
192-
fn addr(&self) -> RawAddr { self.1.addr() }
221+
fn addr(&self) -> RawAddr {
222+
self.1.addr()
223+
}
193224

194225
#[inline]
195-
fn port(&self) -> Option<u16> { self.1.port() }
226+
fn port(&self) -> Option<u16> {
227+
self.1.port()
228+
}
196229

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

236269
#[cfg(feature = "tor")]
237-
fn tor_from_raw_addr(raw: RawAddr) -> Result<TorPublicKeyV3, DecodeError> {
270+
fn tor_from_raw_addr(raw: RawAddr) -> Result<OnionAddressV3, DecodeError> {
238271
let mut a = [0u8; TORV3_PUBLIC_KEY_LENGTH];
239272
a.copy_from_slice(&raw[1..]);
240-
TorPublicKeyV3::from_bytes(&a).map_err(|_| DecodeError::InvalidPubkey)
273+
TorPublicKeyV3::from_bytes(&a)
274+
.map_err(|_| DecodeError::InvalidPubkey)
275+
.map(|key| key.get_onion_address())
241276
}

0 commit comments

Comments
 (0)