Skip to content

Commit

Permalink
use uri instead of deprecated url constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
craigraw committed Aug 9, 2024
1 parent 5db3096 commit 47f925b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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;
Expand Down Expand Up @@ -124,7 +124,7 @@ protected Image call() throws Exception {
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)));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/sparrowwallet/sparrow/net/Auth47.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> parameterMap = new LinkedHashMap<>();
Expand Down Expand Up @@ -64,12 +64,12 @@ public Auth47(URI uri) throws MalformedURLException {
}
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");
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/com/sparrowwallet/sparrow/net/BroadcastSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
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 {
Expand All @@ -28,11 +29,11 @@ public List<Network> getSupportedNetworks() {
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());
}
Expand All @@ -49,15 +50,15 @@ public List<Network> getSupportedNetworks() {
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());
}
Expand All @@ -74,13 +75,13 @@ public List<Network> getSupportedNetworks() {
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());
}
Expand All @@ -97,11 +98,11 @@ public List<Network> getSupportedNetworks() {
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());
}
Expand All @@ -113,7 +114,6 @@ protected URL getURL(HostAndPort proxy) throws MalformedURLException {
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;
Expand Down Expand Up @@ -141,7 +141,7 @@ public String getBaseUrl(HostAndPort proxy) {

public abstract List<Network> 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
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/sparrowwallet/sparrow/net/LnurlAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> parameterMap = new LinkedHashMap<>();
String query = url.getQuery();
Expand Down Expand Up @@ -144,8 +144,8 @@ private URL getReturnURL(Wallet wallet) {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

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;
Expand Down Expand Up @@ -48,8 +45,8 @@ private BitcoindTransport(Server bitcoindServer, String bitcoindWallet) {
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);
}
}
Expand Down

0 comments on commit 47f925b

Please sign in to comment.