Skip to content

Commit

Permalink
Initial commit for defichain_income_address_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolibri1990 committed Apr 24, 2021
0 parents commit bf2a822
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/artifacts/defichain_income_address_helper_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/libraries/json_simple_1_1.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions defichain-income-address-helper.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="json-simple-1.1" level="project" />
</component>
</module>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main

140 changes: 140 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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();
}
}
}
Binary file added src/libraries/json-simple-1.1.jar
Binary file not shown.

0 comments on commit bf2a822

Please sign in to comment.