diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/artifacts/defichain_income_address_helper_jar.xml b/.idea/artifacts/defichain_income_address_helper_jar.xml new file mode 100644 index 0000000..d2edae6 --- /dev/null +++ b/.idea/artifacts/defichain_income_address_helper_jar.xml @@ -0,0 +1,9 @@ + + + $PROJECT_DIR$/out/artifacts/defichain_income_address_helper_jar + + + + + + \ No newline at end of file diff --git a/.idea/libraries/json_simple_1_1.xml b/.idea/libraries/json_simple_1_1.xml new file mode 100644 index 0000000..d183450 --- /dev/null +++ b/.idea/libraries/json_simple_1_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d6eecf9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/defichain-income-address-helper.iml b/defichain-income-address-helper.iml new file mode 100644 index 0000000..bd0c28c --- /dev/null +++ b/defichain-income-address-helper.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/out/artifacts/defichain_income_address_helper_jar/defichain-income-address-helper.jar b/out/artifacts/defichain_income_address_helper_jar/defichain-income-address-helper.jar new file mode 100644 index 0000000..7d4f1e7 Binary files /dev/null and b/out/artifacts/defichain_income_address_helper_jar/defichain-income-address-helper.jar differ diff --git a/out/production/defichain-income-address-helper/META-INF/MANIFEST.MF b/out/production/defichain-income-address-helper/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/out/production/defichain-income-address-helper/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/out/production/defichain-income-address-helper/Main.class b/out/production/defichain-income-address-helper/Main.class new file mode 100644 index 0000000..8136fd6 Binary files /dev/null and b/out/production/defichain-income-address-helper/Main.class differ diff --git a/out/production/defichain-income-address-helper/libraries/json-simple-1.1.jar b/out/production/defichain-income-address-helper/libraries/json-simple-1.1.jar new file mode 100644 index 0000000..f395f41 Binary files /dev/null and b/out/production/defichain-income-address-helper/libraries/json-simple-1.1.jar differ diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5ee19cb --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..4ab3528 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,140 @@ +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + +import java.awt.*; +import java.awt.datatransfer.StringSelection; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Base64; +import java.util.Map; +import java.util.Properties; +import java.util.TreeMap; +import java.util.concurrent.TimeUnit; + +public class Main { + + public static TreeMap ownerMap = new TreeMap(); + + public static void main(String[] args) { + try { + //Read config from DeFi Wallet + Path path = Paths.get(System.getProperty("user.home") + "/.defi/defi.conf"); + Properties configProps = new Properties(); + try (FileInputStream i = new FileInputStream(path.toString())) { + configProps.load(i); + } + + //get all token addresses + getListAccounts(configProps.getProperty("rpcport"), configProps.getProperty("rpcuser") + ":" + configProps.getProperty("rpcpassword")); + + //get all coin addresses with balance > 0.0 + getListAddressGroupings(configProps.getProperty("rpcport"), configProps.getProperty("rpcuser") + ":" + configProps.getProperty("rpcpassword")); + + //Convert TreeMap to StringBuilder + StringBuilder sb = new StringBuilder(); + for (Map.Entry address : ownerMap.entrySet()) { + sb.append(address.getValue()).append(","); + } + + //Copy all addresses im clipboard + if (ownerMap.size() > 0) { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents( + new StringSelection(sb.substring(0, sb.toString().length() - 1)), null + ); + } else { + Toolkit.getDefaultToolkit().getSystemClipboard().setContents( + new StringSelection("Something went wrong. Maybe DeFi Wallet is not running"), null + ); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void getListAccounts(String rpcport, String auth) { + + try { + //Connection to RPC Server + HttpURLConnection conn = (HttpURLConnection) new URL("http://127.0.0.1:" + rpcport).openConnection(); + conn.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(0L)); + conn.setReadTimeout((int) TimeUnit.MINUTES.toMillis(0L)); + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setRequestProperty("Authorization", "Basic " + new String(Base64.getEncoder().encode((auth.getBytes())))); + conn.setRequestProperty("Content-Type", "application/json-rpc"); + conn.getOutputStream().write("{\"method\":\"listaccounts\",\"params\":[{},false,false,true]}".getBytes(StandardCharsets.UTF_8)); + conn.getOutputStream().close(); + + //Get response + String jsonText = ""; + int responseCode = conn.getResponseCode(); + if (responseCode == 200) { + + try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { + jsonText = br.readLine(); + } catch (Exception ex) { + ex.printStackTrace(); + } + + JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonText); + JSONArray transactionJson = (JSONArray) jsonObject.get("result"); + + //Put all token addresses in ownerMap + for (Object owner : transactionJson) { + JSONObject ownerObject = (JSONObject) owner; + if (!ownerMap.containsKey(ownerObject.get("owner").toString())) { + ownerMap.put(ownerObject.get("owner").toString(), ownerObject.get("owner").toString()); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void getListAddressGroupings(String rpcport, String auth) { + + try { + //Connection to RPC Server + HttpURLConnection conn = (HttpURLConnection) new URL("http://127.0.0.1:" + rpcport).openConnection(); + conn.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(0L)); + conn.setReadTimeout((int) TimeUnit.MINUTES.toMillis(0L)); + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setRequestProperty("Authorization", "Basic " + new String(Base64.getEncoder().encode((auth.getBytes())))); + conn.setRequestProperty("Content-Type", "application/json-rpc"); + conn.getOutputStream().write("{\"method\":\"listaddressgroupings\"}".getBytes(StandardCharsets.UTF_8)); + conn.getOutputStream().close(); + + //Get response + String jsonText = ""; + int responseCode = conn.getResponseCode(); + if (responseCode == 200) { + try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { + jsonText = br.readLine(); + } catch (Exception ex) { + ex.printStackTrace(); + } + + JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonText); + JSONArray transactionJson = (JSONArray) jsonObject.get("result"); + JSONArray ownerArray = (JSONArray) transactionJson.get(0); + + //Put all coin addresses with balance > 0.0 in a treemap + for (Object owner : ownerArray) { + JSONArray ownerObject = (JSONArray) owner; + if (!ownerMap.containsKey(ownerObject.get(0).toString()) && (Double) ownerObject.get(1) > 0.0) { + ownerMap.put(ownerObject.get(0).toString(), ownerObject.get(0).toString()); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/libraries/json-simple-1.1.jar b/src/libraries/json-simple-1.1.jar new file mode 100644 index 0000000..f395f41 Binary files /dev/null and b/src/libraries/json-simple-1.1.jar differ