From 47f925b677029bf662166c6f6349eef829c1d02e Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Fri, 9 Aug 2024 10:24:44 +0200 Subject: [PATCH] use uri instead of deprecated url constructor --- .../sparrow/control/PayNymAvatar.java | 4 +-- .../com/sparrowwallet/sparrow/net/Auth47.java | 8 ++--- .../sparrow/net/BroadcastSource.java | 36 +++++++++---------- .../sparrowwallet/sparrow/net/LnurlAuth.java | 8 ++--- .../cormorant/bitcoind/BitcoindTransport.java | 9 ++--- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/control/PayNymAvatar.java b/src/main/java/com/sparrowwallet/sparrow/control/PayNymAvatar.java index 70edca93..ae9a796a 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/PayNymAvatar.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/PayNymAvatar.java @@ -20,7 +20,7 @@ import org.slf4j.LoggerFactory; import java.io.InputStream; import java.net.Proxy; -import java.net.URL; +import java.net.URI; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -124,7 +124,7 @@ public class PayNymAvatar extends StackPane { log.debug("Requesting PayNym avatar from " + url); } - try(InputStream is = (proxy == null ? new URL(url).openStream() : new URL(url).openConnection(proxy).getInputStream())) { + try(InputStream is = (proxy == null ? new URI(url).toURL().openStream() : new URI(url).toURL().openConnection(proxy).getInputStream())) { Image image = new Image(is, 150, 150, true, false); paymentCodeCache.put(cacheId, image); Platform.runLater(() -> EventManager.get().post(new PayNymImageLoadedEvent(paymentCode, image))); diff --git a/src/main/java/com/sparrowwallet/sparrow/net/Auth47.java b/src/main/java/com/sparrowwallet/sparrow/net/Auth47.java index afb920a9..83d6e17f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/Auth47.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/Auth47.java @@ -36,7 +36,7 @@ public class Auth47 { private String srbnName; private String resource; - public Auth47(URI uri) throws MalformedURLException { + public Auth47(URI uri) throws MalformedURLException, URISyntaxException { this.nonce = uri.getHost(); Map parameterMap = new LinkedHashMap<>(); @@ -64,12 +64,12 @@ public class Auth47 { } if(strCallback.startsWith(SRBN_PROTOCOL)) { String srbnCallback = HTTPS_PROTOCOL + strCallback.substring(SRBN_PROTOCOL.length()); - URL srbnUrl = new URL(srbnCallback); + URL srbnUrl = new URI(srbnCallback).toURL(); this.srbn = true; this.srbnName = srbnUrl.getUserInfo(); - this.callback = new URL(HTTPS_PROTOCOL + srbnUrl.getHost()); + this.callback = new URI(HTTPS_PROTOCOL + srbnUrl.getHost()).toURL(); } else { - this.callback = new URL(strCallback); + this.callback = new URI(strCallback).toURL(); } this.expiry = parameterMap.get("e"); diff --git a/src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java b/src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java index 6577f59c..1ed220d9 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java @@ -11,8 +11,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; -import java.security.SecureRandom; import java.util.List; public enum BroadcastSource { @@ -28,11 +29,11 @@ public enum BroadcastSource { return List.of(Network.MAINNET, Network.TESTNET); } - protected URL getURL(HostAndPort proxy) throws MalformedURLException { + protected URL getURL(HostAndPort proxy) throws MalformedURLException, URISyntaxException { if(Network.get() == Network.MAINNET) { - return new URL(getBaseUrl(proxy) + "/api/tx"); + return new URI(getBaseUrl(proxy) + "/api/tx").toURL(); } else if(Network.get() == Network.TESTNET) { - return new URL(getBaseUrl(proxy) + "/testnet/api/tx"); + return new URI(getBaseUrl(proxy) + "/testnet/api/tx").toURL(); } else { throw new IllegalStateException("Cannot broadcast transaction to " + getName() + " on network " + Network.get()); } @@ -49,15 +50,15 @@ public enum BroadcastSource { return List.of(Network.MAINNET, Network.TESTNET, Network.SIGNET, Network.TESTNET4); } - protected URL getURL(HostAndPort proxy) throws MalformedURLException { + protected URL getURL(HostAndPort proxy) throws MalformedURLException, URISyntaxException { if(Network.get() == Network.MAINNET) { - return new URL(getBaseUrl(proxy) + "/api/tx"); + return new URI(getBaseUrl(proxy) + "/api/tx").toURL(); } else if(Network.get() == Network.TESTNET) { - return new URL(getBaseUrl(proxy) + "/testnet/api/tx"); + return new URI(getBaseUrl(proxy) + "/testnet/api/tx").toURL(); } else if(Network.get() == Network.SIGNET) { - return new URL(getBaseUrl(proxy) + "/signet/api/tx"); + return new URI(getBaseUrl(proxy) + "/signet/api/tx").toURL(); } else if(Network.get() == Network.TESTNET4) { - return new URL(getBaseUrl(proxy) + "/testnet4/api/tx"); + return new URI(getBaseUrl(proxy) + "/testnet4/api/tx").toURL(); } else { throw new IllegalStateException("Cannot broadcast transaction to " + getName() + " on network " + Network.get()); } @@ -74,13 +75,13 @@ public enum BroadcastSource { return List.of(Network.MAINNET); } - protected URL getURL(HostAndPort proxy) throws MalformedURLException { + protected URL getURL(HostAndPort proxy) throws MalformedURLException, URISyntaxException { if(Network.get() == Network.MAINNET) { - return new URL(getBaseUrl(proxy) + "/api/tx"); + return new URI(getBaseUrl(proxy) + "/api/tx").toURL(); } else if(Network.get() == Network.TESTNET) { - return new URL(getBaseUrl(proxy) + "/testnet/api/tx"); + return new URI(getBaseUrl(proxy) + "/testnet/api/tx").toURL(); } else if(Network.get() == Network.SIGNET) { - return new URL(getBaseUrl(proxy) + "/signet/api/tx"); + return new URI(getBaseUrl(proxy) + "/signet/api/tx").toURL(); } else { throw new IllegalStateException("Cannot broadcast transaction to " + getName() + " on network " + Network.get()); } @@ -97,11 +98,11 @@ public enum BroadcastSource { return List.of(Network.MAINNET); } - protected URL getURL(HostAndPort proxy) throws MalformedURLException { + protected URL getURL(HostAndPort proxy) throws MalformedURLException, URISyntaxException { if(Network.get() == Network.MAINNET) { - return new URL(getBaseUrl(proxy) + "/api/tx"); + return new URI(getBaseUrl(proxy) + "/api/tx").toURL(); } else if(Network.get() == Network.TESTNET) { - return new URL(getBaseUrl(proxy) + "/testnet/api/tx"); + return new URI(getBaseUrl(proxy) + "/testnet/api/tx").toURL(); } else { throw new IllegalStateException("Cannot broadcast transaction to " + getName() + " on network " + Network.get()); } @@ -113,7 +114,6 @@ public enum BroadcastSource { private final String onionUrl; private static final Logger log = LoggerFactory.getLogger(BroadcastSource.class); - private static final SecureRandom secureRandom = new SecureRandom(); BroadcastSource(String name, String tlsUrl, String onionUrl) { this.name = name; @@ -141,7 +141,7 @@ public enum BroadcastSource { public abstract List getSupportedNetworks(); - protected abstract URL getURL(HostAndPort proxy) throws MalformedURLException; + protected abstract URL getURL(HostAndPort proxy) throws MalformedURLException, URISyntaxException; public Sha256Hash postTransactionData(String data) throws BroadcastException { //If a Tor proxy is configured, ensure we use a new circuit by configuring a random proxy password diff --git a/src/main/java/com/sparrowwallet/sparrow/net/LnurlAuth.java b/src/main/java/com/sparrowwallet/sparrow/net/LnurlAuth.java index 19dd6759..b15be3a3 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/LnurlAuth.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/LnurlAuth.java @@ -38,12 +38,12 @@ public class LnurlAuth { private final byte[] k1; private final String action; - public LnurlAuth(URI uri) throws MalformedURLException { + public LnurlAuth(URI uri) throws MalformedURLException, URISyntaxException { String lnurl = uri.getSchemeSpecificPart(); Bech32.Bech32Data bech32 = Bech32.decode(lnurl, 2000); byte[] urlBytes = Bech32.convertBits(bech32.data, 0, bech32.data.length, 5, 8, false); String strUrl = new String(urlBytes, StandardCharsets.UTF_8); - this.url = new URL(strUrl); + this.url = new URI(strUrl).toURL(); Map parameterMap = new LinkedHashMap<>(); String query = url.getQuery(); @@ -144,8 +144,8 @@ public class LnurlAuth { try { ECKey linkingKey = deriveLinkingKey(wallet); byte[] signature = getSignature(linkingKey); - return new URL(url.toString() + "&sig=" + Utils.bytesToHex(signature) + "&key=" + Utils.bytesToHex(linkingKey.getPubKey())); - } catch(MalformedURLException e) { + return new URI(url.toString() + "&sig=" + Utils.bytesToHex(signature) + "&key=" + Utils.bytesToHex(linkingKey.getPubKey())).toURL(); + } catch(MalformedURLException | URISyntaxException e) { throw new IllegalStateException("Malformed return URL", e); } } diff --git a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindTransport.java b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindTransport.java index 846616b1..0ceda59b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindTransport.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/cormorant/bitcoind/BitcoindTransport.java @@ -10,10 +10,7 @@ import org.slf4j.LoggerFactory; import javax.net.ssl.*; import java.io.*; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.Proxy; -import java.net.URL; +import java.net.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -48,8 +45,8 @@ public class BitcoindTransport implements Transport { if(!bitcoindServer.getHostAndPort().hasPort()) { serverUrl += ":" + Network.get().getDefaultPort(); } - this.bitcoindUrl = new URL(serverUrl + "/wallet/" + bitcoindWallet); - } catch(MalformedURLException e) { + this.bitcoindUrl = new URI(serverUrl + "/wallet/" + bitcoindWallet).toURL(); + } catch(MalformedURLException | URISyntaxException e) { log.error("Malformed Bitcoin Core RPC URL", e); } }