From 8be56f0f8ecc54744d572e5678a3089665587b98 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 30 May 2021 12:19:10 +0200 Subject: [PATCH] p2p, refactor: extract OnionToString() from CNetAddr::ToStringIp() --- src/netaddress.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/netaddress.cpp b/src/netaddress.cpp index 187507fe1f..0ae8dd0698 100644 --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -594,22 +594,26 @@ static std::string IPv6ToString(Span a, uint32_t scope_id) return r; } +static std::string OnionToString(const Span& addr) +{ + uint8_t checksum[torv3::CHECKSUM_LEN]; + torv3::Checksum(addr, checksum); + // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion" + prevector address{addr.begin(), addr.end()}; + address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN); + address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION)); + return EncodeBase32(address) + ".onion"; +} + std::string CNetAddr::ToStringIP() const { switch (m_net) { case NET_IPV4: return IPv4ToString(m_addr); - case NET_IPV6: { + case NET_IPV6: return IPv6ToString(m_addr, m_scope_id); - } case NET_ONION: - uint8_t checksum[torv3::CHECKSUM_LEN]; - torv3::Checksum(m_addr, checksum); - // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion" - prevector address{m_addr.begin(), m_addr.end()}; - address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN); - address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION)); - return EncodeBase32(address) + ".onion"; + return OnionToString(m_addr); case NET_I2P: return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p"; case NET_CJDNS: