use uri instead of deprecated url constructor

This commit is contained in:
Craig Raw 2024-08-09 10:24:44 +02:00
parent 5db3096386
commit 47f925b677
5 changed files with 31 additions and 34 deletions

View file

@ -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)));

View file

@ -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<>();
@ -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");

View file

@ -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<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

View file

@ -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();
@ -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);
}
}

View file

@ -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);
}
}