From c23879f3b631e9df5621ea38c61485bd42c67661 Mon Sep 17 00:00:00 2001 From: pokkst Date: Sun, 6 Nov 2022 22:15:13 -0600 Subject: [PATCH] Add ability to enter a .b32.i2p address for a node to sync. Requires that user has set up their own SOCKS proxy in their I2P app --- .../java/net/mynero/wallet/data/Node.java | 17 +++++++++- .../dialog/AddNodeBottomSheetDialog.java | 34 +++++++++++++++---- .../mynero/wallet/model/WalletManager.java | 2 +- .../net/mynero/wallet/util/OnionHelper.java | 4 +++ 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/net/mynero/wallet/data/Node.java b/app/src/main/java/net/mynero/wallet/data/Node.java index 900cc69..db9e709 100644 --- a/app/src/main/java/net/mynero/wallet/data/Node.java +++ b/app/src/main/java/net/mynero/wallet/data/Node.java @@ -230,6 +230,10 @@ public class Node { return OnionHelper.isOnionHost(host); } + public boolean isI2P() { + return OnionHelper.isI2PHost(host); + } + public String toNodeString() { return toString(); } @@ -240,7 +244,10 @@ public class Node { if (!username.isEmpty() && !password.isEmpty()) { sb.append(username).append(":").append(password).append("@"); } - sb.append(host).append(":").append(rpcPort); + sb.append(host); + if(!isI2P()) { + sb.append(":").append(rpcPort); + } sb.append("/"); switch (networkType) { case NetworkType_Mainnet: @@ -263,6 +270,14 @@ public class Node { } public String getAddress() { + String port = ""; + if(!isI2P()) { + port = ":" + rpcPort; + } + return getHost() + port; + } + + public String getDaemonAddress() { return getHost() + ":" + rpcPort; } diff --git a/app/src/main/java/net/mynero/wallet/fragment/dialog/AddNodeBottomSheetDialog.java b/app/src/main/java/net/mynero/wallet/fragment/dialog/AddNodeBottomSheetDialog.java index 4085451..54acb87 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/dialog/AddNodeBottomSheetDialog.java +++ b/app/src/main/java/net/mynero/wallet/fragment/dialog/AddNodeBottomSheetDialog.java @@ -47,10 +47,11 @@ public class AddNodeBottomSheetDialog extends BottomSheetDialogFragment { addNodeButton.setOnClickListener(view1 -> { String node = addressEditText.getText().toString(); String name = nodeNameEditText.getText().toString(); - if (node.contains(":") && !name.isEmpty()) { - String[] nodeParts = node.split(":"); - if (nodeParts.length == 2) { - try { + try { + + if (node.contains(":") && !name.isEmpty()) { + String[] nodeParts = node.split(":"); + if (nodeParts.length == 2) { String address = nodeParts[0]; int port = Integer.parseInt(nodeParts[1]); String newNodeString = address + ":" + port + "/mainnet/" + name; @@ -75,10 +76,31 @@ public class AddNodeBottomSheetDialog extends BottomSheetDialogFragment { } dismiss(); } - } catch (NumberFormatException | JSONException e) { - e.printStackTrace(); + } + } else if(node.endsWith(".b32.i2p")) { + String newNodeString = node + "/mainnet/" + name; + String nodesArray = PrefService.getInstance().getString(Constants.PREF_CUSTOM_NODES, "[]"); + JSONArray jsonArray = new JSONArray(nodesArray); + boolean exists = false; + for (int i = 0; i < jsonArray.length(); i++) { + String nodeString = jsonArray.getString(i); + if (nodeString.equals(newNodeString)) + exists = true; + } + + if (!exists) { + jsonArray.put(newNodeString); + } + + PrefService.getInstance().edit().putString(Constants.PREF_CUSTOM_NODES, jsonArray.toString()).apply(); + if (listener != null) { + listener.onNodeAdded(); + } + dismiss(); } + } catch (NumberFormatException | JSONException e) { + e.printStackTrace(); } }); } diff --git a/app/src/main/java/net/mynero/wallet/model/WalletManager.java b/app/src/main/java/net/mynero/wallet/model/WalletManager.java index a7d0788..8859010 100644 --- a/app/src/main/java/net/mynero/wallet/model/WalletManager.java +++ b/app/src/main/java/net/mynero/wallet/model/WalletManager.java @@ -258,7 +258,7 @@ public class WalletManager { // this should not be called on the main thread as it connects to the node (and takes a long time) public void setDaemon(Node node) { if (node != null) { - this.daemonAddress = node.getAddress(); + this.daemonAddress = node.getDaemonAddress(); if (networkType != node.getNetworkType()) throw new IllegalArgumentException("network type does not match"); this.daemonUsername = node.getUsername(); diff --git a/app/src/main/java/net/mynero/wallet/util/OnionHelper.java b/app/src/main/java/net/mynero/wallet/util/OnionHelper.java index d4c8b32..919a58a 100644 --- a/app/src/main/java/net/mynero/wallet/util/OnionHelper.java +++ b/app/src/main/java/net/mynero/wallet/util/OnionHelper.java @@ -21,4 +21,8 @@ public class OnionHelper { public static boolean isOnionHost(String hostname) { return hostname.endsWith(".onion"); } + + public static boolean isI2PHost(String hostname) { + return hostname.endsWith(".b32.i2p"); + } }