diff --git a/.idea/artifacts/java_rubix_core_jar.xml b/.idea/artifacts/java_rubix_core_jar.xml
index 24574e90..12fdf4fd 100644
--- a/.idea/artifacts/java_rubix_core_jar.xml
+++ b/.idea/artifacts/java_rubix_core_jar.xml
@@ -3,18 +3,8 @@
$PROJECT_DIR$/out/artifacts/java_rubix_core_jar
-
-
-
-
-
-
-
-
-
-
-
-
+
+
\ No newline at end of file
diff --git a/.idea/libraries/IPFS.xml b/.idea/libraries/IPFS.xml
deleted file mode 100644
index 2be8c6bc..00000000
--- a/.idea/libraries/IPFS.xml
+++ /dev/null
@@ -1,15 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/did_6_3.xml b/.idea/libraries/did_6_3.xml
new file mode 100644
index 00000000..5d39a34a
--- /dev/null
+++ b/.idea/libraries/did_6_3.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/libraries/rubix_9_9.xml b/.idea/libraries/rubix_9_9.xml
new file mode 100644
index 00000000..2ba21774
--- /dev/null
+++ b/.idea/libraries/rubix_9_9.xml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/java-rubix-core.iml b/java-rubix-core.iml
index d9323fbd..a19c778d 100644
--- a/java-rubix-core.iml
+++ b/java-rubix-core.iml
@@ -7,7 +7,7 @@
-
-
+
+
\ No newline at end of file
diff --git a/out/artifacts/java_rubix_core_jar/java-rubix-core.jar b/out/artifacts/java_rubix_core_jar/java-rubix-core.jar
index 8c4a3edb..184e470d 100644
Binary files a/out/artifacts/java_rubix_core_jar/java-rubix-core.jar and b/out/artifacts/java_rubix_core_jar/java-rubix-core.jar differ
diff --git a/out/production/RubiX/com/rubix/AuthenticateNode/Authenticate.class b/out/production/RubiX/com/rubix/AuthenticateNode/Authenticate.class
index 27d1a542..f0c4a8c3 100644
Binary files a/out/production/RubiX/com/rubix/AuthenticateNode/Authenticate.class and b/out/production/RubiX/com/rubix/AuthenticateNode/Authenticate.class differ
diff --git a/out/production/RubiX/com/rubix/AuthenticateNode/SecretShare.class b/out/production/RubiX/com/rubix/AuthenticateNode/SecretShare.class
index 21da112d..cd0b44db 100644
Binary files a/out/production/RubiX/com/rubix/AuthenticateNode/SecretShare.class and b/out/production/RubiX/com/rubix/AuthenticateNode/SecretShare.class differ
diff --git a/out/production/RubiX/com/rubix/AuthenticateNode/SplitShares.class b/out/production/RubiX/com/rubix/AuthenticateNode/SplitShares.class
index f4b1264a..0f1b1c0b 100644
Binary files a/out/production/RubiX/com/rubix/AuthenticateNode/SplitShares.class and b/out/production/RubiX/com/rubix/AuthenticateNode/SplitShares.class differ
diff --git a/out/production/RubiX/com/rubix/Consensus/InitiatorConsensus.class b/out/production/RubiX/com/rubix/Consensus/InitiatorConsensus.class
index 5f2b7df0..0696055f 100644
Binary files a/out/production/RubiX/com/rubix/Consensus/InitiatorConsensus.class and b/out/production/RubiX/com/rubix/Consensus/InitiatorConsensus.class differ
diff --git a/out/production/RubiX/com/rubix/Consensus/InitiatorProcedure.class b/out/production/RubiX/com/rubix/Consensus/InitiatorProcedure.class
index bda14d24..93ce6eab 100644
Binary files a/out/production/RubiX/com/rubix/Consensus/InitiatorProcedure.class and b/out/production/RubiX/com/rubix/Consensus/InitiatorProcedure.class differ
diff --git a/out/production/RubiX/com/rubix/Consensus/QuorumConsensus.class b/out/production/RubiX/com/rubix/Consensus/QuorumConsensus.class
index 4164d1c6..a205ad04 100644
Binary files a/out/production/RubiX/com/rubix/Consensus/QuorumConsensus.class and b/out/production/RubiX/com/rubix/Consensus/QuorumConsensus.class differ
diff --git a/out/production/RubiX/com/rubix/Constants/ConsensusConstants.class b/out/production/RubiX/com/rubix/Constants/ConsensusConstants.class
index dc8c2096..12a23be2 100644
Binary files a/out/production/RubiX/com/rubix/Constants/ConsensusConstants.class and b/out/production/RubiX/com/rubix/Constants/ConsensusConstants.class differ
diff --git a/out/production/RubiX/com/rubix/Constants/IPFSConstants.class b/out/production/RubiX/com/rubix/Constants/IPFSConstants.class
index 43da9c41..29382867 100644
Binary files a/out/production/RubiX/com/rubix/Constants/IPFSConstants.class and b/out/production/RubiX/com/rubix/Constants/IPFSConstants.class differ
diff --git a/out/production/RubiX/com/rubix/Resources/Functions.class b/out/production/RubiX/com/rubix/Resources/Functions.class
index c6dc4ee9..a628e4d3 100644
Binary files a/out/production/RubiX/com/rubix/Resources/Functions.class and b/out/production/RubiX/com/rubix/Resources/Functions.class differ
diff --git a/out/production/RubiX/com/rubix/Resources/IPFSNetwork.class b/out/production/RubiX/com/rubix/Resources/IPFSNetwork.class
index c19d656e..f78bdc5b 100644
Binary files a/out/production/RubiX/com/rubix/Resources/IPFSNetwork.class and b/out/production/RubiX/com/rubix/Resources/IPFSNetwork.class differ
diff --git a/out/production/RubiX/com/rubix/SplitandStore/Recombine.class b/out/production/RubiX/com/rubix/SplitandStore/Recombine.class
index 5495307b..924bfae9 100644
Binary files a/out/production/RubiX/com/rubix/SplitandStore/Recombine.class and b/out/production/RubiX/com/rubix/SplitandStore/Recombine.class differ
diff --git a/out/production/RubiX/com/rubix/SplitandStore/SeperateShares.class b/out/production/RubiX/com/rubix/SplitandStore/SeperateShares.class
index e241e653..a42c7d26 100644
Binary files a/out/production/RubiX/com/rubix/SplitandStore/SeperateShares.class and b/out/production/RubiX/com/rubix/SplitandStore/SeperateShares.class differ
diff --git a/out/production/RubiX/com/rubix/SplitandStore/Split.class b/out/production/RubiX/com/rubix/SplitandStore/Split.class
index 2435dd20..2a99122f 100644
Binary files a/out/production/RubiX/com/rubix/SplitandStore/Split.class and b/out/production/RubiX/com/rubix/SplitandStore/Split.class differ
diff --git a/out/production/java-rubix-core/com/rubix/AuthenticateNode/Authenticate.class b/out/production/java-rubix-core/com/rubix/AuthenticateNode/Authenticate.class
index 14f85ca7..5be6faf3 100644
Binary files a/out/production/java-rubix-core/com/rubix/AuthenticateNode/Authenticate.class and b/out/production/java-rubix-core/com/rubix/AuthenticateNode/Authenticate.class differ
diff --git a/out/production/java-rubix-core/com/rubix/Consensus/InitiatorConsensus.class b/out/production/java-rubix-core/com/rubix/Consensus/InitiatorConsensus.class
index ec07560f..db816f05 100644
Binary files a/out/production/java-rubix-core/com/rubix/Consensus/InitiatorConsensus.class and b/out/production/java-rubix-core/com/rubix/Consensus/InitiatorConsensus.class differ
diff --git a/out/production/java-rubix-core/com/rubix/Consensus/InitiatorProcedure.class b/out/production/java-rubix-core/com/rubix/Consensus/InitiatorProcedure.class
index 053d46be..71608763 100644
Binary files a/out/production/java-rubix-core/com/rubix/Consensus/InitiatorProcedure.class and b/out/production/java-rubix-core/com/rubix/Consensus/InitiatorProcedure.class differ
diff --git a/out/production/java-rubix-core/com/rubix/Consensus/QuorumConsensus.class b/out/production/java-rubix-core/com/rubix/Consensus/QuorumConsensus.class
index 7f6d8af1..010c33c0 100644
Binary files a/out/production/java-rubix-core/com/rubix/Consensus/QuorumConsensus.class and b/out/production/java-rubix-core/com/rubix/Consensus/QuorumConsensus.class differ
diff --git a/out/production/java-rubix-core/com/rubix/Resources.zip b/out/production/java-rubix-core/com/rubix/Resources.zip
deleted file mode 100644
index ca1cb801..00000000
Binary files a/out/production/java-rubix-core/com/rubix/Resources.zip and /dev/null differ
diff --git a/out/production/java-rubix-core/com/rubix/Resources/APIHandler.class b/out/production/java-rubix-core/com/rubix/Resources/APIHandler.class
index f3edbf6a..2f0ee080 100644
Binary files a/out/production/java-rubix-core/com/rubix/Resources/APIHandler.class and b/out/production/java-rubix-core/com/rubix/Resources/APIHandler.class differ
diff --git a/out/production/java-rubix-core/com/rubix/Resources/Functions.class b/out/production/java-rubix-core/com/rubix/Resources/Functions.class
index 86f96185..2a879970 100644
Binary files a/out/production/java-rubix-core/com/rubix/Resources/Functions.class and b/out/production/java-rubix-core/com/rubix/Resources/Functions.class differ
diff --git a/out/production/java-rubix-core/com/rubix/Resources/IPFSNetwork.class b/out/production/java-rubix-core/com/rubix/Resources/IPFSNetwork.class
index f399c159..2f58ea74 100644
Binary files a/out/production/java-rubix-core/com/rubix/Resources/IPFSNetwork.class and b/out/production/java-rubix-core/com/rubix/Resources/IPFSNetwork.class differ
diff --git a/out/production/java-rubix-core/com/rubix/TokenTransfer/ProofCredits.class b/out/production/java-rubix-core/com/rubix/TokenTransfer/ProofCredits.class
deleted file mode 100644
index 5e56ce87..00000000
Binary files a/out/production/java-rubix-core/com/rubix/TokenTransfer/ProofCredits.class and /dev/null differ
diff --git a/out/production/java-rubix-core/com/rubix/TokenTransfer/TokenReceiver.class b/out/production/java-rubix-core/com/rubix/TokenTransfer/TokenReceiver.class
index bf41c1bc..cdd7eedd 100644
Binary files a/out/production/java-rubix-core/com/rubix/TokenTransfer/TokenReceiver.class and b/out/production/java-rubix-core/com/rubix/TokenTransfer/TokenReceiver.class differ
diff --git a/out/production/java-rubix-core/com/rubix/TokenTransfer/TokenSender.class b/out/production/java-rubix-core/com/rubix/TokenTransfer/TokenSender.class
index a2cbd9c6..43622a3f 100644
Binary files a/out/production/java-rubix-core/com/rubix/TokenTransfer/TokenSender.class and b/out/production/java-rubix-core/com/rubix/TokenTransfer/TokenSender.class differ
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 00000000..a395a570
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,43 @@
+
+ 4.0.0
+ com.rubix.core
+ rubixDataJar
+ d0.3.01
+
+ src
+
+
+ src
+
+ **/*.java
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ 11
+ 11
+
+
+
+
+
+
+ com.did.core
+ did
+ provided
+
+
+
+
+
+ com.did.core
+ did
+ 8.7
+
+
+
+
\ No newline at end of file
diff --git a/src/com/rubix/AuthenticateNode/Authenticate.java b/src/com/rubix/AuthenticateNode/Authenticate.java
index 5218b56c..866cc40b 100644
--- a/src/com/rubix/AuthenticateNode/Authenticate.java
+++ b/src/com/rubix/AuthenticateNode/Authenticate.java
@@ -42,38 +42,12 @@ public class Authenticate {
public static boolean verifySignature(String detailString) throws IOException, JSONException {
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
-
verifyCount++;
IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
JSONObject details = new JSONObject(detailString);
String decentralizedID = details.getString("did");
String hash = details.getString("hash");
-
- String quorumSign;
- String tokenSign;
- String TIDSign;
- JSONObject signs = new JSONObject();
-
- if (details.optJSONObject(quorumSign) != null) {
-
- JSONObject signatureObject = details.getJSONObject("signatures");
- quorumSign = signatureObject.getString("QuorumSign");
- tokenSign = signatureObject.getString("TokenSign");
- TIDSign = signatureObject.getString("TIDSign");
-
- signs.put(tokenSign, false);
- signs.put(quorumSign, false);
- signs.put(TIDSign, false);
-
- } else {
- // make all three params in JSONObject signs as true
- signs.put(quorumSign, false);
- signs.put(tokenSign, true);
- signs.put(TIDSign, true);
-
- quorumSign = details.getString("signature");
- }
-
+ String signature = details.getString("signature");
syncDataTable(decentralizedID, null);
String walletIdIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "didHash", decentralizedID);
nodeData(decentralizedID, walletIdIpfsHash, ipfs);
diff --git a/src/com/rubix/Consensus/DataConsensus.java b/src/com/rubix/Consensus/DataConsensus.java
new file mode 100644
index 00000000..b9e9cb30
--- /dev/null
+++ b/src/com/rubix/Consensus/DataConsensus.java
@@ -0,0 +1,377 @@
+package com.rubix.Consensus;
+
+
+import static com.rubix.Constants.ConsensusConstants.DATA;
+import static com.rubix.Constants.ConsensusConstants.PRIMARY;
+import static com.rubix.Constants.ConsensusConstants.TRANS_TYPE;
+import static com.rubix.Resources.Functions.DATA_PATH;
+import static com.rubix.Resources.Functions.IPFS_PORT;
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.WALLET_DATA_PATH;
+import static com.rubix.Resources.Functions.calculateHash;
+import static com.rubix.Resources.Functions.deleteFile;
+import static com.rubix.Resources.Functions.getPeerID;
+import static com.rubix.Resources.Functions.getSignFromShares;
+import static com.rubix.Resources.Functions.getValues;
+import static com.rubix.Resources.Functions.nodeData;
+import static com.rubix.Resources.Functions.syncDataTable;
+import static com.rubix.Resources.Functions.updateJSON;
+import static com.rubix.Resources.Functions.writeToFile;
+import static com.rubix.Resources.IPFSNetwork.add;
+import static com.rubix.Resources.IPFSNetwork.listen;
+import static com.rubix.Resources.IPFSNetwork.pin;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+import com.rubix.AuthenticateNode.Authenticate;
+import com.rubix.Datum.Dependency;
+import com.rubix.Resources.IPFSNetwork;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import io.ipfs.api.IPFS;
+
+public class DataConsensus implements Runnable {
+
+ public static Logger DataConsensusLogger = Logger.getLogger(DataConsensus.class);
+
+ /**
+ * This method is used to run a thread for Quorum Members
+ *
+ * This involves
+ *
+ * - Verify sender signature
+ * - Signing the transaction
+ * - Receiving share from sender
+ *
+ */
+
+ int port;
+ IPFS ipfs;
+ String role;
+ int round;
+
+ public DataConsensus(String role, int port) {
+ this.role = role;
+ this.port = port;
+ this.ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
+ }
+
+ @Override
+ public void run() {
+ while (true) {
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ String peerID, transactionID, verifySenderHash, receiverDID, appName, senderPrivatePos,
+ senderDidIpfsHash = "", senderPID = "", ownerHash = "", initHash = "";
+ ServerSocket serverSocket = null;
+ Socket socket = null;
+ try {
+
+ peerID = getPeerID(DATA_PATH + "DID.json");
+ String didHash = Dependency.getDIDfromPID(peerID, Dependency.dataTableHashMap()); //getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID);
+ appName = peerID.concat(role);
+
+ listen(appName, port);
+
+ DataConsensusLogger.debug("Quorum Listening on " + port + " appname " + appName);
+ serverSocket = new ServerSocket(port);
+ socket = serverSocket.accept();
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ PrintStream out = new PrintStream(socket.getOutputStream());
+
+ Thread validate = new ValidationHandler(socket, in, out, ipfs, peerID, didHash, appName);
+ validate.start();
+
+ } catch (IOException e) {
+ DataConsensusLogger.error("IOException Occurred", e);
+ } catch (NullPointerException e) {
+ DataConsensusLogger.error("NullPointer Exception Occurred ", e);
+ }
+ }
+
+ }
+}
+
+class ValidationHandler extends Thread {
+
+ public static Logger ValidationHandler = Logger.getLogger(ValidationHandler.class);
+
+ final Socket socket;
+ final BufferedReader in;
+ final PrintStream out;
+ final IPFS ipfs;
+ final String peerID;
+ final String didHash;
+ final String appName;
+
+ public ValidationHandler(Socket socket, BufferedReader in, PrintStream out, IPFS ipfs, String peerID,
+ String didHash,
+ String appName) {
+ this.socket = socket;
+ this.in = in;
+ this.out = out;
+ this.ipfs = ipfs;
+ this.peerID = peerID;
+ this.didHash = didHash;
+ this.appName = appName;
+ this.start();
+ }
+
+ boolean integrityCheck = true;
+ String temp = "";
+ String transactionID = "";
+ String verifySenderHash = "";
+ String blockHash = "";
+ String senderPrivatePos = "";
+ String senderDidIpfsHash = "";
+ String senderPID = "";
+ String getData = "";
+ String receiverDID = "";
+
+ JSONObject readSenderData;
+
+ public void run() {
+
+ while (true) {
+
+ try {
+
+ String getData;
+
+ // TODO: check if initiator is sending ping check to see if it is alive
+ getData = in.readLine();
+ if (getData.contains("ping check")) {
+ ValidationHandler.debug("Ping check from sender: " + getData);
+ out.println("pong response");
+ } else {
+ ValidationHandler.debug("Received Details from initiator: " + getData);
+ readSenderData = new JSONObject(getData);
+ senderPrivatePos = readSenderData.getString("sign");
+ senderDidIpfsHash = readSenderData.getString("senderDID");
+ transactionID = readSenderData.getString("Tid");
+ verifySenderHash = readSenderData.getString("Hash");
+
+ syncDataTable(senderDidIpfsHash, null);
+
+ senderPID = Dependency.getPIDfromDID(senderDidIpfsHash, Dependency.dataTableHashMap());//getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", senderDidIpfsHash);
+ String senderWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "didHash",
+ senderDidIpfsHash);
+
+ nodeData(senderDidIpfsHash, senderWidIpfsHash, ipfs);
+
+ JSONObject detailsToVerify = new JSONObject();
+ detailsToVerify.put("did", senderDidIpfsHash);
+ detailsToVerify.put("hash", verifySenderHash);
+ detailsToVerify.put("signature", senderPrivatePos);
+
+ writeToFile(LOGGER_PATH + "tempverifysenderhash", verifySenderHash, false);
+ String verifySenderIPFSHash = IPFSNetwork.addHashOnly(LOGGER_PATH + "tempverifysenderhash", ipfs);
+ deleteFile(LOGGER_PATH + "tempverifysenderhash");
+
+ // DataConsensusLogger.debug("Checking providers for: " + verifySenderHash);
+ // ArrayList dhtOwnersList = dhtOwnerCheck(verifySenderHash);
+ // DataConsensusLogger.debug("Providers: " + dhtOwnersList);
+ // boolean consensusIDcheck = false;
+ // if(dhtOwnersList.size() <= 2 && dhtOwnersList.contains(senderPID))
+ // consensusIDcheck = true;
+
+ // if for data, primary or secondary token condition starts here
+
+ if (readSenderData.getString(TRANS_TYPE) == DATA) {
+
+ blockHash = readSenderData.getString("blockHash");
+ String quorumHash = calculateHash(verifySenderHash.concat(blockHash), "SHA3-256");
+
+ if (Authenticate.verifySignature(detailsToVerify.toString())) {
+ ValidationHandler.debug("Quorum Authenticated Sender");
+
+ ValidationHandler.debug("ConsensusID pass");
+ String QuorumSignature = getSignFromShares(DATA_PATH + didHash + "/PrivateShare.png",
+ quorumHash);
+
+ out.println(QuorumSignature);
+ String creditval;
+ creditval = in.readLine();
+ ValidationHandler.debug("credit value " + creditval);
+
+ if (!creditval.equals("null")) {
+
+ // ? quorum pinning blockHash and files from sender
+ IPFSNetwork.pin(blockHash, ipfs);
+
+ String blockHashData = IPFSNetwork.get(blockHash, ipfs);
+ JSONObject blockDataObject = new JSONObject(blockHashData);
+ JSONArray blockArray = new JSONArray(blockDataObject.getJSONObject("metadata"));
+
+ for (int i = 0; i < blockArray.length(); i++) {
+ JSONObject blockObject = blockArray.getJSONObject(i);
+ String blockFileHash = blockObject.getString("file_uid");
+ IPFSNetwork.pin(blockFileHash, ipfs);
+ }
+
+ FileWriter shareWriter = new FileWriter(new File(LOGGER_PATH + "mycredit.txt"), true);
+ shareWriter.write(creditval);
+ shareWriter.close();
+ File readCredit = new File(LOGGER_PATH + "mycredit.txt");
+ String credit = add(readCredit.toString(), ipfs);
+ pin(credit, ipfs);
+ // adding credit to credit mapping
+ JSONArray CreditBody = new JSONArray(creditval);
+ // JSONObject creditMappingObject = new JSONObject();
+ JSONArray creditMappingArray = new JSONArray();
+
+ for (int i = 0; i < CreditBody.length(); i++) {
+ JSONObject creditMappingObject = new JSONObject();
+ JSONObject object = CreditBody.getJSONObject(i);
+ String key = object.getString("did");
+ String sign = object.getString("sign");
+ String creditHash = calculateHash(sign, "SHA3-256");
+
+ creditMappingObject.put("did", key);
+ creditMappingObject.put("sign", sign);
+ creditMappingObject.put("hash", creditHash);
+ creditMappingObject.put("tid", transactionID);
+
+ creditMappingArray.put(creditMappingObject);
+
+ }
+ writeToFile(WALLET_DATA_PATH + "CreditMapping.json", creditMappingArray.toString(),
+ false);
+
+ JSONObject storeDetailsQuorum = new JSONObject();
+ storeDetailsQuorum.put("tid", transactionID);
+ storeDetailsQuorum.put("consensusID", verifySenderHash);
+ storeDetailsQuorum.put("sign", senderPrivatePos);
+ storeDetailsQuorum.put("credits", credit);
+ storeDetailsQuorum.put("senderdid", senderDidIpfsHash);
+ storeDetailsQuorum.put("blockHash", blockHash);
+ JSONArray data = new JSONArray();
+ data.put(storeDetailsQuorum);
+ ValidationHandler.debug("Quorum Share: " + credit);
+ updateJSON("add", WALLET_DATA_PATH + "QuorumSignedTransactions.json", data.toString());
+ deleteFile(LOGGER_PATH + "mycredit.txt");
+ writeToFile(LOGGER_PATH + "consenusIDhash", verifySenderHash, false);
+ String consenusIDhash = IPFSNetwork.add(LOGGER_PATH + "consenusIDhash", ipfs);
+ deleteFile(LOGGER_PATH + "consenusIDhash");
+ ValidationHandler.debug("added consensus ID " + consenusIDhash);
+
+ } else {
+ JSONObject storeDetailsQuorum = new JSONObject();
+ storeDetailsQuorum.put("tid", transactionID);
+ storeDetailsQuorum.put("consensusID", verifySenderHash);
+ storeDetailsQuorum.put("sign", senderPrivatePos);
+ storeDetailsQuorum.put("credits", "");
+ storeDetailsQuorum.put("senderdid", senderDidIpfsHash);
+ storeDetailsQuorum.put("blockHash", blockHash);
+ JSONArray data = new JSONArray();
+ data.put(storeDetailsQuorum);
+ updateJSON("add", WALLET_DATA_PATH + "QuorumSignedTransactions.json", data.toString());
+ }
+
+ } else {
+ ValidationHandler.debug("Sender Authentication Failure - Quorum");
+ out.println("Auth_Failed");
+ }
+ }
+
+ if (readSenderData.getString(TRANS_TYPE) == PRIMARY) {
+
+ receiverDID = readSenderData.getString("RID");
+ String quorumHash = calculateHash(verifySenderHash.concat(receiverDID), "SHA3-256");
+
+ if (Authenticate.verifySignature(detailsToVerify.toString())) {
+ ValidationHandler.debug("Quorum Authenticated Sender");
+
+ ValidationHandler.debug("ConsensusID pass");
+ String QuorumSignature = getSignFromShares(DATA_PATH + didHash + "/PrivateShare.png",
+ quorumHash);
+ out.println(QuorumSignature);
+ String creditval;
+ creditval = in.readLine();
+ ValidationHandler.debug("credit value " + creditval);
+
+ if (!creditval.equals("null")) { // commented as per test for multiple consensus threads
+
+ FileWriter shareWriter = new FileWriter(new File(LOGGER_PATH + "mycredit.txt"), true);
+ shareWriter.write(creditval);
+ shareWriter.close();
+ File readCredit = new File(LOGGER_PATH + "mycredit.txt");
+ String credit = add(readCredit.toString(), ipfs);
+ pin(credit, ipfs);
+ // adding credit to credit mapping
+ JSONArray CreditBody = new JSONArray(creditval);
+ // JSONObject creditMappingObject = new JSONObject();
+ JSONArray creditMappingArray = new JSONArray();
+
+ for (int i = 0; i < CreditBody.length(); i++) {
+ JSONObject creditMappingObject = new JSONObject();
+ JSONObject object = CreditBody.getJSONObject(i);
+ String key = object.getString("did");
+ String sign = object.getString("sign");
+ String creditHash = calculateHash(sign, "SHA3-256");
+
+ creditMappingObject.put("did", key);
+ creditMappingObject.put("sign", sign);
+ creditMappingObject.put("hash", creditHash);
+ creditMappingObject.put("tid", transactionID);
+
+ creditMappingArray.put(creditMappingObject);
+
+ }
+ writeToFile(WALLET_DATA_PATH + "CreditMapping.json", creditMappingArray.toString(),
+ false);
+
+ JSONObject storeDetailsQuorum = new JSONObject();
+ storeDetailsQuorum.put("tid", transactionID);
+ storeDetailsQuorum.put("consensusID", verifySenderHash);
+ storeDetailsQuorum.put("sign", senderPrivatePos);
+ storeDetailsQuorum.put("credits", credit);
+ storeDetailsQuorum.put("senderdid", senderDidIpfsHash);
+ storeDetailsQuorum.put("recdid", receiverDID);
+ JSONArray data = new JSONArray();
+ data.put(storeDetailsQuorum);
+ ValidationHandler.debug("Quorum Share: " + credit);
+ updateJSON("add", WALLET_DATA_PATH + "QuorumSignedTransactions.json", data.toString());
+ deleteFile(LOGGER_PATH + "mycredit.txt");
+ writeToFile(LOGGER_PATH + "consenusIDhash", verifySenderHash, false);
+ String consenusIDhash = IPFSNetwork.add(LOGGER_PATH + "consenusIDhash", ipfs);
+ deleteFile(LOGGER_PATH + "consenusIDhash");
+ ValidationHandler.debug("added consensus ID " + consenusIDhash);
+
+ } else {
+ JSONObject storeDetailsQuorum = new JSONObject();
+ storeDetailsQuorum.put("tid", transactionID);
+ storeDetailsQuorum.put("consensusID", verifySenderHash);
+ storeDetailsQuorum.put("sign", senderPrivatePos);
+ storeDetailsQuorum.put("credits", "");
+ storeDetailsQuorum.put("senderdid", senderDidIpfsHash);
+ storeDetailsQuorum.put("recdid", receiverDID);
+ JSONArray data = new JSONArray();
+ data.put(storeDetailsQuorum);
+ updateJSON("add", WALLET_DATA_PATH + "QuorumSignedTransactions.json", data.toString());
+ }
+
+ } else {
+ ValidationHandler.debug("Sender Authentication Failure - Quorum");
+ out.println("Auth_Failed");
+ }
+ }
+ }
+ } catch (Exception e) {
+ ValidationHandler.debug("Exception in Quorum Consensus: " + e);
+ }
+
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/com/rubix/Consensus/DatainitiatorProcedure.java b/src/com/rubix/Consensus/DatainitiatorProcedure.java
new file mode 100644
index 00000000..0dcc908e
--- /dev/null
+++ b/src/com/rubix/Consensus/DatainitiatorProcedure.java
@@ -0,0 +1,160 @@
+package com.rubix.Consensus;
+
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.calculateHash;
+import static com.rubix.Resources.Functions.getSignFromShares;
+import static com.rubix.Resources.Functions.initHash;
+import static com.rubix.Resources.Functions.minQuorum;
+
+import java.io.IOException;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.rubix.Constants.ConsensusConstants;
+import com.rubix.SplitandStore.SeperateShares;
+import com.rubix.SplitandStore.Split;
+
+import io.ipfs.api.IPFS;
+
+public class DatainitiatorProcedure {
+ public static String essential;
+ public static String senderSignQ;
+ public static JSONObject payload = new JSONObject();
+ public static JSONObject alphaReply, betaReply, gammaReply;
+
+ public static Logger DataInitiatorProcedureLogger = Logger.getLogger(DatainitiatorProcedure.class);
+
+ /**
+ * This function sets up the initials before the consensus
+ *
+ * @param data Data required for hashing and signing
+ * @param ipfs IPFS instance
+ * @param PORT port for forwarding to quorum
+ * @throws IOException
+ */
+ public static void dataConsensusSetUp(String data, IPFS ipfs, int PORT, int alphaSize)
+ throws JSONException {
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ //DataInitiatorProcedureLogger.debug("Incoming data to initator procedure is "+data);
+ JSONObject dataSend = new JSONObject();
+ JSONObject dataObject = new JSONObject(data);
+ //DataInitiatorProcedureLogger.debug("dataObject is "+dataObject.toString());
+ String tid = dataObject.getString("tid");
+ String pvt = dataObject.getString("pvt");
+ String operation = ConsensusConstants.DATA;
+ String senderDidIpfs = dataObject.getString("senderDidIpfs");
+ JSONArray alphaList = dataObject.getJSONArray("alphaList");
+ JSONArray betaList = dataObject.getJSONArray("betaList");
+ JSONArray gammaList = dataObject.getJSONArray("gammaList");
+ String token = dataObject.getString("token");
+
+ String blockHash = dataObject.getString("blockHash");
+ String authSenderByQuorumHash = calculateHash(blockHash, "SHA3-256");
+ String authQuorumHash = calculateHash(authSenderByQuorumHash.concat(blockHash), "SHA3-256");
+
+ // InitiatorProcedureLogger.debug("Data Sender by Quorum Hash " +
+ // authSenderByQuorumHash);
+ // InitiatorProcedureLogger.debug("Data Quorum Auth Hash " + authQuorumHash);
+
+ try {
+ payload.put("sender", senderDidIpfs);
+ payload.put("blockHash", blockHash);
+ payload.put("tid", tid);
+ //DataInitiatorProcedureLogger.debug("payload is "+payload.toString());
+ } catch (JSONException e) {
+ DataInitiatorProcedureLogger.error("JSON Exception occurred", e);
+ e.printStackTrace();
+ }
+
+ Split.split(payload.toString());
+
+ int[][] shares = Split.get135Shares();
+
+ essential = SeperateShares.getShare(shares, payload.toString().length(), 0);
+ String Q1Share = SeperateShares.getShare(shares, payload.toString().length(), 1);
+ String Q2Share = SeperateShares.getShare(shares, payload.toString().length(), 2);
+ String Q3Share = SeperateShares.getShare(shares, payload.toString().length(), 3);
+ String Q4Share = SeperateShares.getShare(shares, payload.toString().length(), 4);
+ JSONObject data1 = new JSONObject();
+ JSONObject data2 = new JSONObject();
+ try {
+ senderSignQ = getSignFromShares(pvt, authSenderByQuorumHash);
+ data1.put("sign", senderSignQ);
+ data1.put("senderDID", senderDidIpfs);
+ data1.put("blockHash", blockHash);
+ data1.put(ConsensusConstants.TRANSACTION_ID, tid);
+ data1.put(ConsensusConstants.HASH, authSenderByQuorumHash);
+ data1.put(ConsensusConstants.INIT_HASH, initHash());
+ data2.put("Share1", Q1Share);
+ data2.put("Share2", Q2Share);
+ data2.put("Share3", Q3Share);
+ data2.put("Share4", Q4Share);
+ // InitiatorProcedureLogger.debug("data1 is "+data1.toString());
+ // InitiatorProcedureLogger.debug("data2 is "+data2.toString());
+
+ } catch (JSONException | IOException e) {
+ DataInitiatorProcedureLogger.error("JSON Exception occurred", e);
+ e.printStackTrace();
+ }
+
+ JSONArray detailsForQuorum = new JSONArray();
+ detailsForQuorum.put(data1);
+ detailsForQuorum.put(data2);
+ // InitiatorProcedureLogger.info(detailsForQuorum.toString());
+ // InitiatorProcedureLogger.debug("Invoking Consensus");
+
+ dataSend.put("hash", authQuorumHash);
+ dataSend.put("details", detailsForQuorum);
+ // InitiatorProcedureLogger.debug("data sending to init cons is "+ dataSend.toString());
+
+
+
+ Thread alphaThread = new Thread(() -> {
+ try {
+ DataInitiatorProcedureLogger.debug("sending data to alphaThread dataSend alpha is "+ alphaList + "alpha size is "+ alphaSize
+ +" port is "+ PORT);
+ alphaReply = InitiatorConsensus.start(dataSend.toString(), ipfs, PORT, 0, "alpha", alphaList, alphaSize,
+ alphaSize, operation);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ });
+
+ Thread betaThread = new Thread(() -> {
+ try {
+ DataInitiatorProcedureLogger.debug("sending data to betaThread dataSend beta is "+ betaList + "beta size is "+ alphaSize
+ +" port is "+ (PORT + 100));
+ betaReply = InitiatorConsensus.start(dataSend.toString(), ipfs, PORT + 100, 1, "beta", betaList,
+ alphaSize, 7, operation);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ });
+
+ Thread gammaThread = new Thread(() -> {
+ try {
+ DataInitiatorProcedureLogger.debug("sending data to gammaThread dataSend JSON gamma is "+ gammaList + "alpha size is "+ alphaSize
+ +" port is "+ (PORT + 107));
+ gammaReply = InitiatorConsensus.start(dataSend.toString(), ipfs, PORT + 107, 2, "gamma", gammaList,
+ alphaSize, 7, operation);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ });
+ InitiatorConsensus.quorumSignature = new JSONObject();
+ InitiatorConsensus.finalQuorumSignsArray = new JSONArray();
+ alphaThread.start();
+ betaThread.start();
+ gammaThread.start();
+ while (InitiatorConsensus.quorumSignature.length() < (minQuorum(alphaSize) + 2 * minQuorum(7))) {
+ }
+ DataInitiatorProcedureLogger
+ .debug("ABG Consensus completed with length " + InitiatorConsensus.quorumSignature.length());
+ }
+
+
+}
diff --git a/src/com/rubix/Consensus/InitiatorConsensus.java b/src/com/rubix/Consensus/InitiatorConsensus.java
index d030dcfa..3e996c3c 100644
--- a/src/com/rubix/Consensus/InitiatorConsensus.java
+++ b/src/com/rubix/Consensus/InitiatorConsensus.java
@@ -1,10 +1,11 @@
package com.rubix.Consensus;
+import static com.rubix.Constants.ConsensusConstants.INIT_HASH;
import static com.rubix.Resources.Functions.DATA_PATH;
import static com.rubix.Resources.Functions.LOGGER_PATH;
import static com.rubix.Resources.Functions.QUORUM_COUNT;
-import static com.rubix.Resources.Functions.checkTokenOwnershiByDID;
import static com.rubix.Resources.Functions.getValues;
+import static com.rubix.Resources.Functions.initHash;
import static com.rubix.Resources.Functions.minQuorum;
import static com.rubix.Resources.Functions.nodeData;
import static com.rubix.Resources.Functions.syncDataTable;
@@ -13,7 +14,6 @@
import static com.rubix.Resources.IPFSNetwork.swarmConnectP2P;
import java.io.BufferedReader;
-import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
@@ -21,16 +21,16 @@
import java.net.SocketException;
import java.util.ArrayList;
-import com.rubix.AuthenticateNode.Authenticate;
-import com.rubix.Resources.Functions;
-import com.rubix.Resources.IPFSNetwork;
-
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import com.rubix.AuthenticateNode.Authenticate;
+import com.rubix.Datum.Dependency;
+import com.rubix.Resources.IPFSNetwork;
+
import io.ipfs.api.IPFS;
public class InitiatorConsensus {
@@ -38,17 +38,13 @@ public class InitiatorConsensus {
public static Logger InitiatorConsensusLogger = Logger.getLogger(InitiatorConsensus.class);
public static volatile JSONObject quorumSignature = new JSONObject();
+ public static volatile JSONArray signedAphaQuorumArray = new JSONArray();
private static final Object countLock = new Object();
private static final Object signLock = new Object();
public static ArrayList quorumWithShares = new ArrayList<>();
public static volatile int[] quorumResponse = { 0, 0, 0 };
public static volatile JSONArray finalQuorumSignsArray = new JSONArray();
-
- /**
- * Added by Anuradha K on 04/01/20222.
- * To address SocketConnection reset Issue
- */
- private static int socketTimeOut = 120000;
+ private static int socketTimeOut = 0;
/**
* This method increments the quorumResponse variable
@@ -70,26 +66,6 @@ private static synchronized boolean voteNCount(int i, int quorumSize) {
return status;
}
- /**
- * This method increments the quorumResponse variable
- */
- private static synchronized boolean selectStakingQuorum(String DID) {
- boolean status;
- PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
- synchronized (countLock) {
- if (quorumResponse[i] < minQuorum(quorumSize)) {
- quorumResponse[i]++;
- InitiatorConsensusLogger.debug("quorum response added index " + i + " is " + quorumResponse[i]
- + " quorumsize " + minQuorum(quorumSize));
- status = true;
- } else {
- status = false;
- InitiatorConsensusLogger.debug("Staking Reached");
- }
- }
- return status;
- }
-
/**
* This method stores all the quorum signatures until required count for
* consensus
@@ -136,7 +112,6 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
PrintStream[] qOut = new PrintStream[QUORUM_COUNT];
BufferedReader[] qIn = new BufferedReader[QUORUM_COUNT];
String[] quorumID = new String[QUORUM_COUNT];
- String[] signedAlphaQuorumID = new String[minQuorum(7)];
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
JSONObject dataObject = new JSONObject(data);
String hash = dataObject.getString("hash");
@@ -166,9 +141,9 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
try {
swarmConnectP2P(quorumID[j], ipfs);
syncDataTable(null, quorumID[j]);
- String quorumDidIpfsHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid",
- quorumID[j]);
-
+ String quorumDidIpfsHash = Dependency.getDIDfromPID(quorumID[j], Dependency.dataTableHashMap());
+ // getValues(DATA_PATH + "DataTable.json", "didHash", "peerid",
+ // quorumID[j]);
String quorumWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid",
quorumID[j]);
nodeData(quorumDidIpfsHash, quorumWidIpfsHash, ipfs);
@@ -186,9 +161,9 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
qOut[j].println(operation);
if (operation.equals("new-credits-mining")) {
-
- // ? QST + credit info
JSONObject qstDetails = dataObject.getJSONObject("qstDetails");
+ qstDetails.put(INIT_HASH, initHash());
+ // Verify QST Credits
qOut[j].println(qstDetails.toString());
try {
qResponse[j] = qIn[j].readLine();
@@ -198,9 +173,6 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
}
if (qResponse[j] != null) {
if (qResponse[j].equals("Verified")) {
-
- // ! let all quorum mems know who is staking and use it in signature
- detailsToken.put("stakingQuorumPeerID", quorumID[0]);
qOut[j].println(detailsToken);
try {
qResponse[j] = qIn[j].readLine();
@@ -213,9 +185,8 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
if (qResponse[j].equals("Auth_Failed")) {
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumID[j]);
} else {
- InitiatorConsensusLogger.trace(
- "Signature Received from Q" + j + "(" + quorumID[j] + ") : "
- + qResponse[j]);
+ InitiatorConsensusLogger.debug(
+ "Signature Received from " + quorumID[j] + " " + qResponse[j]);
if (quorumResponse[index] > minQuorum(quorumSize)) {
qOut[j].println("null");
IPFSNetwork
@@ -234,6 +205,11 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
if (quorumResponse[index] <= minQuorum(quorumSize) && voteStatus) {
InitiatorConsensusLogger.debug(
"waiting for " + quorumSize + " +signs " + role);
+ if (role.equals("alpha")) {
+ InitiatorConsensusLogger
+ .debug("Picking Quorum for Staking " + quorumID[j]);
+ signedAphaQuorumArray.put(quorumID[j]);
+ }
while (quorumResponse[index] < minQuorum(quorumSize)) {
}
InitiatorConsensusLogger.debug("between Q1- to Q" + quorumSize
@@ -268,6 +244,7 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
}
}
}
+
} else if (qResponse[j].equals("440")) {
InitiatorConsensusLogger.debug("Credit Verification failed: Duplicates found");
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumID[j]);
@@ -279,10 +256,11 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
InitiatorConsensusLogger.debug("Credit Verification failed: Credits hash mismatch");
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumID[j]);
} else if (qResponse[j].equals("443")) {
- InitiatorConsensusLogger.debug("Failed to initialize credit verification");
+ InitiatorConsensusLogger.debug("Credit Verification failed: Init hash mismatch");
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumID[j]);
}
}
+
}
qOut[j].println(detailsToken);
@@ -310,13 +288,18 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
JSONObject detailsToVerify = new JSONObject();
detailsToVerify.put("did", didHash);
detailsToVerify.put("hash", hash);
- detailsToVerify.put("signatures", qResponse[j]);
+ detailsToVerify.put("signature", qResponse[j]);
if (Authenticate.verifySignature(detailsToVerify.toString())) {
InitiatorConsensusLogger.debug(role + " node authenticated at index " + index);
boolean voteStatus = voteNCount(index, quorumSize);
if (quorumResponse[index] <= minQuorum(quorumSize) && voteStatus) {
InitiatorConsensusLogger
.debug("waiting for " + quorumSize + " +signs " + role);
+ if (role.equals("alpha")) {
+ InitiatorConsensusLogger
+ .debug("Picking Quorum for Staking " + quorumID[j]);
+ signedAphaQuorumArray.put(quorumID[j]);
+ }
while (quorumResponse[index] < minQuorum(quorumSize)) {
}
InitiatorConsensusLogger
@@ -346,7 +329,6 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
}
}
}
-
} catch (IOException | JSONException e) {
IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumID[j]);
InitiatorConsensusLogger.error("IOException Occurred");
@@ -356,79 +338,6 @@ public static JSONObject start(String data, IPFS ipfs, int PORT, int index, Stri
quorumThreads[j].start();
}
- if (operation.equals("new-credits-mining")) {
- Thread[] stakingThreads = new Thread[signedAlphaQuorumID.length];
- // ? one of the validators stake 1 RBT and send the stake ID to the miner.
- // choose a quorum member from 1 to 5 who have signed the transaction
- for (int p = 0; p < signedAlphaQuorumID.length; p++) {
- int s = p;
- stakingThreads[p] = new Thread(() -> {
- try {
- InitiatorConsensusLogger.debug(
- "Contacting Signed Alpha Quorum ID : " + signedAlphaQuorumID[s]
- + " for staking. Index "
- + s);
-
- qSocket[s] = new Socket("127.0.0.1", PORT + s);
- qSocket[s].setSoTimeout(socketTimeOut);
- qIn[s] = new BufferedReader(new InputStreamReader(qSocket[s].getInputStream()));
- qOut[s] = new PrintStream(qSocket[s].getOutputStream());
-
- qOut[s].println("stake-token");
- try {
- qResponse[s] = qIn[s].readLine();
- String stakingQuorumDID = getValues(DATA_PATH + "DataTable.json", "didHash",
- "peerid", signedAlphaQuorumID[s]);
- if (checkTokenOwnershiByDID(qResponse[s], stakingQuorumDID)) {
- if (true) {
-
- // ! generate stake ID and send to staking quorum
-
- InitiatorConsensusLogger.debug("Staking Successful at index " + s);
- } else {
-
- InitiatorConsensusLogger.debug("sending null for slow quorum ");
- qOut[s].println("null");
- IPFSNetwork.executeIPFSCommands(
- "ipfs p2p close -t /p2p/" + signedAlphaQuorumID[s]);
-
- }
-
- } else {
-
- InitiatorConsensusLogger.debug("sending null for slow quorum ");
- qOut[s].println("null");
- IPFSNetwork.executeIPFSCommands(
- "ipfs p2p close -t /p2p/" + signedAlphaQuorumID[s]);
-
- }
- } catch (SocketException e) {
- InitiatorConsensusLogger
- .warn("Alpha Quorum (for Staking)" + signedAlphaQuorumID[s]
- + " is unable to Respond!");
- IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + signedAlphaQuorumID[s]);
- }
-
- } catch (Exception e) {
- InitiatorConsensusLogger.error("Exception Occurred");
- e.printStackTrace();
- }
- });
- stakingThreads[s].start();
- }
- // check the balance
- // if balance is sufficient then stake the token
- // else send a null to Qa(j)
- // if Qa(j) sends a null then close the connection
- // else send the stake ID to Qa(j)
- // pin the stake ID (initiator) and keep it in a file for future use
- File stakesFolder = new File(Functions.WALLET_DATA_PATH.concat("/Stakes"));
- if (!stakesFolder.exists())
- stakesFolder.mkdirs();
- // send mining fee to Qa(j) - 0.02 RBT
- // close the connection
- }
-
while (quorumResponse[index] < minQuorum(quorumSize)
|| quorumSignature.length() < (minQuorum(alphaSize) + 2 * minQuorum(7))) {
}
diff --git a/src/com/rubix/Consensus/InitiatorProcedure.java b/src/com/rubix/Consensus/InitiatorProcedure.java
index 142364e4..10557faf 100644
--- a/src/com/rubix/Consensus/InitiatorProcedure.java
+++ b/src/com/rubix/Consensus/InitiatorProcedure.java
@@ -3,6 +3,7 @@
import static com.rubix.Resources.Functions.LOGGER_PATH;
import static com.rubix.Resources.Functions.calculateHash;
import static com.rubix.Resources.Functions.getSignFromShares;
+import static com.rubix.Resources.Functions.initHash;
import static com.rubix.Resources.Functions.minQuorum;
import java.io.IOException;
@@ -33,6 +34,7 @@ public class InitiatorProcedure {
* @param data Data required for hashing and signing
* @param ipfs IPFS instance
* @param PORT port for forwarding to quorum
+ * @throws IOException
*/
public static void consensusSetUp(String data, IPFS ipfs, int PORT, int alphaSize, String operation)
throws JSONException {
@@ -44,7 +46,6 @@ public static void consensusSetUp(String data, IPFS ipfs, int PORT, int alphaSiz
String receiverDidIpfs = dataObject.getString("receiverDidIpfs");
String pvt = dataObject.getString("pvt");
String senderDidIpfs = dataObject.getString("senderDidIpfs");
- String initHash = dataObject.getString("initHash");
String token = dataObject.getString("token");
JSONArray alphaList = dataObject.getJSONArray("alphaList");
JSONArray betaList = dataObject.getJSONArray("betaList");
@@ -78,11 +79,10 @@ public static void consensusSetUp(String data, IPFS ipfs, int PORT, int alphaSiz
senderSignQ = getSignFromShares(pvt, authSenderByQuorumHash);
data1.put("sign", senderSignQ);
data1.put("senderDID", senderDidIpfs);
- data1.put("initHash", initHash);
- data1.put("token", token);
data1.put(ConsensusConstants.TRANSACTION_ID, tid);
data1.put(ConsensusConstants.HASH, authSenderByQuorumHash);
data1.put(ConsensusConstants.RECEIVERID, receiverDidIpfs);
+ data1.put(ConsensusConstants.INIT_HASH, initHash());
data2.put("Share1", Q1Share);
data2.put("Share2", Q2Share);
@@ -102,6 +102,9 @@ public static void consensusSetUp(String data, IPFS ipfs, int PORT, int alphaSiz
JSONObject dataSend = new JSONObject();
dataSend.put("hash", authQuorumHash);
dataSend.put("details", detailsForQuorum);
+
+ InitiatorProcedureLogger.debug("hash"+authQuorumHash);
+ InitiatorProcedureLogger.debug("data1"+data1.toString());
if (operation.equals("new-credits-mining")) {
JSONObject qstDetails = dataObject.getJSONObject("qstDetails");
diff --git a/src/com/rubix/Consensus/QuorumConsensus.java b/src/com/rubix/Consensus/QuorumConsensus.java
index f581e1d3..5ac83a09 100644
--- a/src/com/rubix/Consensus/QuorumConsensus.java
+++ b/src/com/rubix/Consensus/QuorumConsensus.java
@@ -1,421 +1,817 @@
-package com.rubix.Consensus;
-
-import static com.rubix.Resources.Functions.DATA_PATH;
-import static com.rubix.Resources.Functions.IPFS_PORT;
-import static com.rubix.Resources.Functions.LOGGER_PATH;
-import static com.rubix.Resources.Functions.SYNC_IP;
-import static com.rubix.Resources.Functions.WALLET_DATA_PATH;
-import static com.rubix.Resources.Functions.calculateHash;
-import static com.rubix.Resources.Functions.deleteFile;
-import static com.rubix.Resources.Functions.getPeerID;
-import static com.rubix.Resources.Functions.getSignFromShares;
-import static com.rubix.Resources.Functions.getValues;
-import static com.rubix.Resources.Functions.initHash;
-import static com.rubix.Resources.Functions.nodeData;
-import static com.rubix.Resources.Functions.syncDataTable;
-import static com.rubix.Resources.Functions.updateJSON;
-import static com.rubix.Resources.Functions.writeToFile;
-import static com.rubix.Resources.IPFSNetwork.add;
-import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
-import static com.rubix.Resources.IPFSNetwork.listen;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.PrintStream;
-import java.net.HttpURLConnection;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.net.SocketException;
-import java.net.URL;
-import java.text.ParseException;
-import java.util.HashSet;
-
-import com.rubix.AuthenticateNode.Authenticate;
-import com.rubix.Resources.Functions;
-import com.rubix.Resources.IPFSNetwork;
-
-import org.apache.log4j.Logger;
-import org.apache.log4j.PropertyConfigurator;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-import io.ipfs.api.IPFS;
-
-public class QuorumConsensus implements Runnable {
-
- public static Logger QuorumConsensusLogger = Logger.getLogger(QuorumConsensus.class);
-
- /**
- * This method is used to run a thread for Quorum Members
- *
- * This involves
- *
- * - Verify sender signature
- * - Signing the transaction
- * - Receiving share from sender
- *
- */
-
- int port;
- IPFS ipfs;
- String role;
-
- public QuorumConsensus(String role, int port) {
- this.role = role;
- this.port = port;
- this.ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
- }
-
- @Override
- public void run() {
- while (true) {
- PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
- String peerID, transactionID, verifySenderHash, receiverDID, appName, initHash, senderPrivatePos, token,
- stakingQuorumDID,
- stakedToken,
- senderDidIpfsHash = "", senderPID = "";
- ServerSocket serverSocket = null;
- Socket socket = null;
- try {
- File creditsFolder = new File(Functions.WALLET_DATA_PATH.concat("/Credits"));
- if (!creditsFolder.exists())
- creditsFolder.mkdirs();
- peerID = getPeerID(DATA_PATH + "DID.json");
- String didHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID);
- appName = peerID.concat(role);
-
- listen(appName, port);
-
- QuorumConsensusLogger.debug("Quorum Listening on " + port + " appname " + appName);
- serverSocket = new ServerSocket(port);
- socket = serverSocket.accept();
-
- BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- PrintStream out = new PrintStream(socket.getOutputStream());
-
- JSONObject readSenderData;
- String operation = null;
- try {
- operation = in.readLine();
- } catch (SocketException e) {
- QuorumConsensusLogger.debug("Sender Input Stream Null - Operation");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
-
- if (operation.equals("stake-token")) {
-
- String stakeDetails;
-
- // ! send token ID choosen
- out.println("null");
-
- try {
- stakeDetails = in.readLine();
- } catch (SocketException e) {
- QuorumConsensusLogger.debug("Sender Input Stream Null - New stake ID Details");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
- JSONObject stakeDetailsJSON = new JSONObject(stakeDetails);
- generateStakeIDByToken(stakeDetailsJSON.getString("stakeID"), stakeDetailsJSON.getString("minedToken"));
- out.println("OK")
- // ! wait for 0.02 RBT
-
- }
-
- if (operation.equals("new-credits-mining")) {
-
- QuorumConsensusLogger.debug("New Credits");
- String getNewCreditsData = null;
- try {
- getNewCreditsData = in.readLine();
- } catch (SocketException e) {
- QuorumConsensusLogger.debug("Sender Input Stream Null - New Credits Details");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
- // Verify QST Credits
- JSONObject qstObject = new JSONObject(getNewCreditsData);
-
- // Get level of token from advisory node
- int creditsRequired = 0;
- JSONObject resJsonData_credit = new JSONObject();
- String GET_URL_credit = SYNC_IP + "/getlevel";
- URL URLobj_credit = new URL(GET_URL_credit);
- HttpURLConnection con_credit = (HttpURLConnection) URLobj_credit.openConnection();
- con_credit.setRequestMethod("GET");
- int responseCode_credit = con_credit.getResponseCode();
- System.out.println("GET Response Code :: " + responseCode_credit);
- if (responseCode_credit == HttpURLConnection.HTTP_OK) {
- BufferedReader in_credit = new BufferedReader(
- new InputStreamReader(con_credit.getInputStream()));
- String inputLine_credit;
- StringBuffer response_credit = new StringBuffer();
- while ((inputLine_credit = in_credit.readLine()) != null) {
- response_credit.append(inputLine_credit);
- }
- in_credit.close();
- QuorumConsensusLogger.debug("response from service " + response_credit.toString());
- resJsonData_credit = new JSONObject(response_credit.toString());
- int level_credit = resJsonData_credit.getInt("level");
- creditsRequired = (int) Math.pow(2, (2 + level_credit));
- QuorumConsensusLogger.debug("credits required " + creditsRequired);
-
- } else
- QuorumConsensusLogger.debug("GET request not worked");
-
- // Level 1 Verification: Verify hash of n objects
- JSONArray qstArray = qstObject.getJSONArray("qstArray");
- JSONArray creditsArray = qstObject.getJSONArray("credits");
-
- boolean flag = true;
- for (int i = 0; i < creditsRequired; i++) {
- QuorumConsensusLogger.debug("Credit object: " + creditsArray.getJSONObject(i).toString());
- QuorumConsensusLogger.debug(
- "Credit Hash: " + calculateHash(creditsArray.getJSONObject(i).toString(), "SHA3-256"));
- String reHash = calculateHash(qstArray.getJSONObject(i).getString("credits"), "SHA3-256");
- if (!reHash.equals(qstArray.getJSONObject(i).getString("creditHash"))) {
- QuorumConsensusLogger.debug("Recalculation " + reHash + " - "
- + qstArray.getJSONObject(i).getString("creditHash"));
- flag = false;
- }
- }
- if (flag) {
-
- boolean verifySigns = true;
- for (int i = 0; i < creditsRequired; i++) {
- if (!Authenticate.verifySignature(creditsArray.getJSONObject(i).toString()))
- verifySigns = false;
- }
- if (verifySigns) {
- HashSet hashSet = new HashSet();
- long startTime = System.currentTimeMillis();
- for (int i = 0; i < creditsArray.length(); i++) {
- String sign = creditsArray.getJSONObject(i).getString("signature");
- String signHash = calculateHash(sign, "SHA3-256");
- hashSet.add(signHash);
- }
- long endTime = System.currentTimeMillis();
- QuorumConsensusLogger.debug("Total Time for HashSet: " + (endTime - startTime));
- if (hashSet.size() == qstArray.length() * 15) {
- QuorumConsensusLogger.debug("Mining Verified");
- out.println("Verified");
- } else {
- QuorumConsensusLogger
- .debug("HashSet: " + hashSet.size() + " QST Size " + qstArray.length());
- QuorumConsensusLogger.debug("Mining Not Verified: Duplicates Found");
- out.println("440");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
- } else {
- out.println("441");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
- } else {
- out.println("442");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
- } else
- QuorumConsensusLogger.debug("Old Credits Mining / Whole RBT Token Transfer");
-
- String getRecData = null;
- try {
- getRecData = in.readLine();
- } catch (SocketException e) {
- QuorumConsensusLogger.debug("Sender Input Stream Null - Ping Check / Receiver Details");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
-
- if (getRecData != null) {
- if (getRecData.contains("ping check")) {
- QuorumConsensusLogger.debug("Ping check from sender: " + getRecData);
- out.println("pong response");
- } else {
-
- QuorumConsensusLogger.debug("Received Details from initiator: " + getRecData);
- readSenderData = new JSONObject(getRecData);
- senderPrivatePos = readSenderData.getString("sign");
- senderDidIpfsHash = readSenderData.getString("senderDID");
- transactionID = readSenderData.getString("Tid");
- verifySenderHash = readSenderData.getString("Hash");
- receiverDID = readSenderData.getString("RID");
-
- // ! additional info for the mining verification
- initHash = readSenderData.getString("initHash");
- token = readSenderData.getString("token");
- stakedToken = readSenderData.getString("stakedToken");
- String stakingQuorumPeerID = readSenderData.getString("stakingQuorumPeerID");
- stakingQuorumDID = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid",
- stakingQuorumPeerID);
-
- // ! match initiator initHash with quorum. if didn't match, reject
-
- if (!initHash.equals(initHash())) {
- QuorumConsensusLogger.debug("Initiator Hash Mismatch");
- out.println("443");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
-
- syncDataTable(senderDidIpfsHash, null);
-
- senderPID = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", senderDidIpfsHash);
- String senderWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "didHash",
- senderDidIpfsHash);
-
- nodeData(senderDidIpfsHash, senderWidIpfsHash, ipfs);
-
- // ! check if each quorum member is signing on the quorumHash (which now is
- // sender + receiver hash)
- // concat of sender hash and rec hash)
-
- String quorumHash = calculateHash(token + transactionID + stakingQuorumDID, "SHA3-256");
- String stakeHash = calculateHash(token + stakedToken, "SHA3-256");
-
- QuorumConsensusLogger.debug("1: " + verifySenderHash);
- QuorumConsensusLogger.debug("2: " + receiverDID);
- QuorumConsensusLogger.debug("Quorum hash: " + quorumHash);
-
- JSONObject detailsToVerify = new JSONObject();
- detailsToVerify.put("did", senderDidIpfsHash);
- detailsToVerify.put("hash", verifySenderHash);
- detailsToVerify.put("signature", senderPrivatePos);
-
- writeToFile(LOGGER_PATH + "tempverifysenderhash", verifySenderHash, false);
- String verifySenderIPFSHash = IPFSNetwork.addHashOnly(LOGGER_PATH + "tempverifysenderhash",
- ipfs);
- deleteFile(LOGGER_PATH + "tempverifysenderhash");
-
- // QuorumConsensusLogger.debug("Checking providers for: " + verifySenderHash);
- // ArrayList dhtOwnersList = dhtOwnerCheck(verifySenderHash);
- // QuorumConsensusLogger.debug("Providers: " + dhtOwnersList);
- // boolean consensusIDcheck = false;
- // if(dhtOwnersList.size() <= 2 && dhtOwnersList.contains(senderPID))
- // consensusIDcheck = true;
-
- if (Authenticate.verifySignature(detailsToVerify.toString())) {
- QuorumConsensusLogger.debug("Quorum Authenticated Sender");
-
- QuorumConsensusLogger.debug("ConsensusID pass");
-
- // json obj called quorumSignss
- JSONObject quorumSigns = new JSONObject();
-
- // ! 1st sign: TID
- String QuorumSignatureOne = getSignFromShares(DATA_PATH + didHash + "/PrivateShare.png",
- transactionID);
- quorumSigns.put("TIDSign", QuorumSignatureOne);
- // ?
- // ! 2st sign: Token
- String QuorumSignatureTwo = getSignFromShares(DATA_PATH + didHash + "/PrivateShare.png",
- token);
- quorumSigns.put("TokenSign", QuorumSignatureTwo);
-
- // ! 3th sign: Token + TID
- String QuorumSignatureFive = getSignFromShares(DATA_PATH + didHash + "/PrivateShare.png",
- quorumHash);
- quorumSigns.put("QuorumSign", QuorumSignatureFive);
-
- out.println(quorumSigns);
-
- String creditSignatures = null;
- try {
- creditSignatures = in.readLine();
- } catch (SocketException e) {
- QuorumConsensusLogger.debug("Sender Input Stream Null - Credits");
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- }
- QuorumConsensusLogger.debug("credit Signature " + creditSignatures);
-
- if (!creditSignatures.equals("null")) { // commented as per test for multiple consensus
- // threads
-
- FileWriter shareWriter = new FileWriter(new File(LOGGER_PATH + "mycredit.txt"), true);
- shareWriter.write(creditSignatures);
- shareWriter.close();
- File readCredit = new File(LOGGER_PATH + "mycredit.txt");
- String credit = add(readCredit.toString(), ipfs);
-
- File creditFile = new File(
- WALLET_DATA_PATH.concat("/Credits/").concat(credit).concat(".json"));
- if (!creditFile.exists())
- creditFile.createNewFile();
- writeToFile(creditFile.toString(), creditSignatures, false);
-
- QuorumConsensusLogger.debug("Credit object: " + credit);
- QuorumConsensusLogger.debug("Credit Hash: " + calculateHash(credit, "SHA3-256"));
- JSONObject storeDetailsQuorum = new JSONObject();
- storeDetailsQuorum.put("tid", transactionID);
- storeDetailsQuorum.put("consensusID", verifySenderHash);
- storeDetailsQuorum.put("sign", senderPrivatePos);
- storeDetailsQuorum.put("credits", credit);
- storeDetailsQuorum.put("creditHash", calculateHash(credit, "SHA3-256"));
- storeDetailsQuorum.put("senderdid", senderDidIpfsHash);
- storeDetailsQuorum.put("Date", Functions.getCurrentUtcTime());
- storeDetailsQuorum.put("recdid", receiverDID);
- JSONArray data = new JSONArray();
- data.put(storeDetailsQuorum);
- QuorumConsensusLogger.debug("Quorum Share: " + credit);
- updateJSON("add", WALLET_DATA_PATH + "QuorumSignedTransactions.json", data.toString());
- deleteFile(LOGGER_PATH + "mycredit.txt");
- writeToFile(LOGGER_PATH + "consenusIDhash", verifySenderHash, false);
- String consenusIDhash = IPFSNetwork.add(LOGGER_PATH + "consenusIDhash", ipfs);
- deleteFile(LOGGER_PATH + "consenusIDhash");
- QuorumConsensusLogger.debug("added consensus ID " + consenusIDhash);
- }
-
- } else {
- QuorumConsensusLogger.debug("Sender Authentication Failure - Quorum");
- out.println("Auth_Failed");
- }
-
- // ! moving staked token to bottom of BNK00 file.
-
- }
- } else {
- QuorumConsensusLogger.debug("Quorum - " + didHash + " is unable to respond!" + getRecData);
- out.println(getRecData);
- }
-
- } catch (IOException e) {
- QuorumConsensusLogger.error("IOException Occurred", e);
- } catch (JSONException e) {
- QuorumConsensusLogger.error("JSONException Occurred", e);
- } catch (NullPointerException e) {
- QuorumConsensusLogger.error("NullPointer Exception Occurred ", e);
- } catch (ParseException e) {
- QuorumConsensusLogger.error("ParseException Occurred ", e);
- } finally {
- try {
- socket.close();
- serverSocket.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- } catch (IOException e) {
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
- QuorumConsensusLogger.error("IOException Occurred", e);
- }
-
- }
- }
-
- }
-}
+package com.rubix.Consensus;
+
+import static com.rubix.Constants.ConsensusConstants.INIT_HASH;
+import static com.rubix.Constants.MiningConstants.MINED_RBT;
+import static com.rubix.Constants.MiningConstants.MINED_RBT_SIGN;
+import static com.rubix.Constants.MiningConstants.MINE_ID;
+import static com.rubix.Constants.MiningConstants.MINE_ID_SIGN;
+import static com.rubix.Constants.MiningConstants.MINE_TID;
+import static com.rubix.Constants.MiningConstants.MINING_TID_SIGN;
+import static com.rubix.Constants.MiningConstants.STAKED_QUORUM_DID;
+import static com.rubix.Constants.MiningConstants.STAKED_TOKEN;
+import static com.rubix.Constants.MiningConstants.STAKED_TOKEN_SIGN;
+import static com.rubix.Constants.MiningConstants.STAKE_DATA;
+import static com.rubix.Resources.Functions.DATA_PATH;
+import static com.rubix.Resources.Functions.IPFS_PORT;
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.PAYMENTS_PATH;
+import static com.rubix.Resources.Functions.SYNC_IP;
+import static com.rubix.Resources.Functions.TOKENCHAIN_PATH;
+import static com.rubix.Resources.Functions.TOKENS_PATH;
+import static com.rubix.Resources.Functions.WALLET_DATA_PATH;
+import static com.rubix.Resources.Functions.calculateHash;
+import static com.rubix.Resources.Functions.deleteFile;
+import static com.rubix.Resources.Functions.getPeerID;
+import static com.rubix.Resources.Functions.getSignFromShares;
+import static com.rubix.Resources.Functions.getValues;
+import static com.rubix.Resources.Functions.initHash;
+import static com.rubix.Resources.Functions.nodeData;
+import static com.rubix.Resources.Functions.readFile;
+import static com.rubix.Resources.Functions.strToIntArray;
+import static com.rubix.Resources.Functions.syncDataTable;
+import static com.rubix.Resources.Functions.updateJSON;
+import static com.rubix.Resources.Functions.writeToFile;
+import static com.rubix.Resources.IPFSNetwork.add;
+import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
+import static com.rubix.Resources.IPFSNetwork.listen;
+import static com.rubix.Resources.IPFSNetwork.pin;
+
+import java.awt.image.BufferedImage;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.net.HttpURLConnection;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+import java.net.URL;
+import java.text.ParseException;
+import java.util.HashSet;
+import java.util.Random;
+
+import javax.imageio.ImageIO;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.rubix.AuthenticateNode.Authenticate;
+import com.rubix.AuthenticateNode.PropImage;
+import com.rubix.Datum.Dependency;
+import com.rubix.Resources.Functions;
+import com.rubix.Resources.IPFSNetwork;
+
+import io.ipfs.api.IPFS;
+
+public class QuorumConsensus implements Runnable {
+
+ public static Logger QuorumConsensusLogger = Logger.getLogger(QuorumConsensus.class);
+
+ /**
+ * This method is used to run a thread for Quorum Members
+ *
+ * This involves
+ *
+ * - Verify sender signature
+ * - Signing the transaction
+ * - Receiving share from sender
+ *
+ */
+
+ int port;
+ IPFS ipfs;
+ String role;
+
+ public QuorumConsensus(String role, int port) {
+ this.role = role;
+ this.port = port;
+ this.ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
+ }
+
+ @Override
+ public void run() {
+ while (true) {
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ String peerID, transactionID, verifySenderHash, receiverDID, appName, senderPrivatePos,
+ senderDidIpfsHash = "", senderPID = "", ownerHash = "", initHash = "", blockHashString = "";
+ ServerSocket serverSocket = null;
+ Socket socket = null;
+ try {
+ File creditsFolder = new File(Functions.WALLET_DATA_PATH.concat("/Credits"));
+ if (!creditsFolder.exists())
+ creditsFolder.mkdirs();
+ peerID = getPeerID(DATA_PATH + "DID.json");
+ String didHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID);
+ appName = peerID.concat(role);
+
+ listen(appName, port);
+
+ QuorumConsensusLogger.debug("Quorum Listening on " + port + " appname " + appName);
+ serverSocket = new ServerSocket(port);
+ socket = serverSocket.accept();
+
+ BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ PrintStream out = new PrintStream(socket.getOutputStream());
+
+ JSONObject readSenderData;
+ String operation = null;
+ try {
+ operation = in.readLine();
+ } catch (SocketException e) {
+ QuorumConsensusLogger.debug("Sender Input Stream Null - Operation");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+
+ // ? staking logic starts here
+ if (operation.equals("new-credits-mining")) {
+ QuorumConsensusLogger.debug("New Credits");
+ String getNewCreditsData = null;
+ try {
+ getNewCreditsData = in.readLine();
+ } catch (SocketException e) {
+ QuorumConsensusLogger.debug("Sender Input Stream Null - New Credits Details");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ // Verify QST Credits
+ JSONObject qstObject = new JSONObject(getNewCreditsData);
+
+ if (qstObject.getString(INIT_HASH).equals(initHash())) {
+
+ // Get level of token from advisory node
+ int creditsRequired = 0;
+ JSONObject resJsonData_credit = new JSONObject();
+ String GET_URL_credit = SYNC_IP + "/getCurrentLevel";
+ URL URLobj_credit = new URL(GET_URL_credit);
+ HttpURLConnection con_credit = (HttpURLConnection) URLobj_credit.openConnection();
+ con_credit.setRequestMethod("GET");
+ int responseCode_credit = con_credit.getResponseCode();
+ System.out.println("GET Response Code :: " + responseCode_credit);
+ if (responseCode_credit == HttpURLConnection.HTTP_OK) {
+ BufferedReader in_credit = new BufferedReader(
+ new InputStreamReader(con_credit.getInputStream()));
+ String inputLine_credit;
+ StringBuffer response_credit = new StringBuffer();
+ while ((inputLine_credit = in_credit.readLine()) != null) {
+ response_credit.append(inputLine_credit);
+ }
+ in_credit.close();
+ // QuorumConsensusLogger.debug("response from service " +
+ // response_credit.toString());
+ resJsonData_credit = new JSONObject(response_credit.toString());
+ int level_credit = resJsonData_credit.getInt("level");
+ creditsRequired = (int) Math.pow(2, (2 + level_credit));
+ QuorumConsensusLogger.debug("credits required " + creditsRequired);
+
+ } else
+ QuorumConsensusLogger.debug("GET request not worked");
+
+ // Level 1 Verification: Verify hash of n objects
+ JSONArray qstArray = qstObject.getJSONArray("qstArray");
+ JSONArray creditsArray = qstObject.getJSONArray("credits");
+
+ boolean flag = true;
+ // if qstArray has any duplicate object
+ for (int i = 0; i < qstArray.length(); i++) {
+ for (int j = i + 1; j < qstArray.length(); j++) {
+ if (qstArray.getJSONObject(i).getString("credits")
+ .equals(qstArray.getJSONObject(j).getString("credits"))) {
+ flag = false;
+ break;
+ }
+ }
+ }
+ for (int i = 0; i < creditsRequired; i++) {
+ QuorumConsensusLogger.debug("Credit object: " + creditsArray.getJSONObject(i).toString());
+ QuorumConsensusLogger.debug(
+ "Credit Hash: "
+ + calculateHash(creditsArray.getJSONObject(i).toString(), "SHA3-256"));
+ String reHash = calculateHash(qstArray.getJSONObject(i).getString("credits"), "SHA3-256");
+ if (!reHash.equals(qstArray.getJSONObject(i).getString("creditHash"))) {
+ QuorumConsensusLogger.debug("Recalculation " + reHash + " - "
+ + qstArray.getJSONObject(i).getString("creditHash"));
+ flag = false;
+ }
+ }
+
+ if (flag) {
+ boolean verifySigns = true;
+ for (int i = 0; i < creditsRequired; i++) {
+ if (!Authenticate.verifySignature(creditsArray.getJSONObject(i).toString()))
+ verifySigns = false;
+ }
+ if (verifySigns) {
+ HashSet hashSet = new HashSet();
+ long startTime = System.currentTimeMillis();
+ for (int i = 0; i < creditsArray.length(); i++) {
+ String sign = creditsArray.getJSONObject(i).getString("signature");
+ String signHash = calculateHash(sign, "SHA3-256");
+ hashSet.add(signHash);
+ }
+ long endTime = System.currentTimeMillis();
+ QuorumConsensusLogger.debug("Total Time for HashSet: " + (endTime - startTime));
+ if (hashSet.size() == qstArray.length() * 15) {
+ QuorumConsensusLogger.debug("Mining Verified");
+ out.println("Verified");
+
+ } else {
+ QuorumConsensusLogger
+ .debug("HashSet: " + hashSet.size() + " QST Size " + qstArray.length());
+ QuorumConsensusLogger.debug("Mining Not Verified: Duplicates Found");
+ out.println("440");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ } else {
+ out.println("441");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ } else {
+ out.println("442");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ } else {
+ out.println("443");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ } else if (operation.equals("alpha-stake-token")) {
+
+ QuorumConsensusLogger.debug("Staking 1 RBT for incoming mining transaction...");
+ String response = null;
+
+ try {
+ // ! token hash just mined
+ response = in.readLine();
+ } catch (SocketException e) {
+ QuorumConsensusLogger.debug("Sender Input Stream Null - New Credits Details");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+
+ JSONObject genesisBlock = new JSONObject(response);
+ QuorumConsensusLogger.debug("Validating new token details: " + genesisBlock);
+
+ boolean LEVEL_VALID = false;
+ boolean MINE_CREDIT_VALID = false;
+
+ // ! check token is in same level
+ String TokenContent = genesisBlock.getString("tokenContent");
+ String tokenLevel = TokenContent.substring(0, 3);
+ int tokenLevelInt = Integer.parseInt(tokenLevel);
+ int tokenLevelValue = (int) Math.pow(2, tokenLevelInt + 2);
+
+ String GET_URL_credit = SYNC_IP + "/getCurrentLevel";
+ URL URLobj_credit = new URL(GET_URL_credit);
+ HttpURLConnection con_credit = (HttpURLConnection) URLobj_credit.openConnection();
+ con_credit.setRequestMethod("GET");
+ int responseCode_credit = con_credit.getResponseCode();
+ System.out.println("GET Response Code :: " + responseCode_credit);
+ if (responseCode_credit == HttpURLConnection.HTTP_OK) {
+ BufferedReader in_credit = new BufferedReader(
+ new InputStreamReader(con_credit.getInputStream()));
+ String inputLine_credit;
+ StringBuffer response_credit = new StringBuffer();
+ while ((inputLine_credit = in_credit.readLine()) != null) {
+ response_credit.append(inputLine_credit);
+ }
+ in_credit.close();
+ // QuorumConsensusLogger.debug("response from service " +
+ // response_credit.toString());
+ JSONObject resJsonData_credit = new JSONObject(response_credit.toString());
+ int level_credit = resJsonData_credit.getInt("level");
+ int creditsRequired = (int) Math.pow(2, (2 + level_credit));
+
+ if (level_credit == tokenLevelInt) {
+
+ QuorumConsensusLogger.debug("Validated level of newly minted token");
+ LEVEL_VALID = true;
+ } else {
+ QuorumConsensusLogger.debug("Invalid level of newly minted token");
+ LEVEL_VALID = false;
+ }
+
+ } else
+ QuorumConsensusLogger.debug("GET request not worked");
+
+ // ! validate mined token hash and ownership
+ if (genesisBlock.has("quorumSignatures")) {
+
+ try {
+ int randomNumber = new Random().nextInt(15);
+ JSONObject genesisSignatures = genesisBlock.getJSONObject("quorumSignContent");
+ JSONArray keys = genesisSignatures.names();
+ String signer = keys.getString(randomNumber);
+ String signature = genesisSignatures.getString(signer);
+
+ JSONObject VerificationPick = new JSONObject();
+ VerificationPick.put("signature", signature);
+ VerificationPick.put("did", signer);
+ VerificationPick.put("hash", genesisBlock.getString("tid"));
+
+ // QuorumConsensusLogger.debug("Verifying credit signature of new token: " +
+ // VerificationPick);
+
+ if (Authenticate.verifySignature(VerificationPick.toString())) {
+
+ QuorumConsensusLogger.debug("Validated signature of newly minted token");
+ MINE_CREDIT_VALID = true;
+ } else {
+ QuorumConsensusLogger.debug("Signature not verified");
+ MINE_CREDIT_VALID = false;
+ }
+ } catch (Exception e) {
+ QuorumConsensusLogger
+ .debug("Mined Token - Quorum Signature Hash not found. Skipping... Exception: "
+ + e.getMessage());
+ MINE_CREDIT_VALID = false;
+ }
+
+ }
+
+ if (LEVEL_VALID) {
+ // LEVEL_VALID && MINE_CREDIT_VALID
+
+ QuorumConsensusLogger.debug("Sending staking token details...");
+ JSONArray tokenToStake = new JSONArray();
+
+ String bankFile = readFile(PAYMENTS_PATH.concat("BNK00.json"));
+ JSONArray bankArray = new JSONArray(bankFile);
+
+ if (bankArray.length() > 0) {
+
+ File tokenFile;
+ File tokenchainFile;
+ JSONObject bankObject = new JSONObject();
+ String stakedTokenHash = "";
+ boolean tokenAvailableToStake = false;
+ JSONArray stakedTokenChainArray = new JSONArray();
+
+ // for loop to check bankArray for token
+ for (int i = 0; i < bankArray.length(); i++) {
+
+ bankObject = bankArray.getJSONObject(i);
+ stakedTokenHash = bankObject.getString("tokenHash");
+
+ tokenFile = new File(TOKENS_PATH + stakedTokenHash);
+ tokenchainFile = new File(TOKENCHAIN_PATH + stakedTokenHash + ".json");
+
+ QuorumConsensusLogger.debug("Checking token" + stakedTokenHash + " for staking...");
+
+ if (tokenFile.exists() && tokenchainFile.exists()) {
+
+ String tokenChain = readFile(TOKENCHAIN_PATH + stakedTokenHash + ".json");
+ stakedTokenChainArray = new JSONArray(tokenChain);
+
+ if (stakedTokenChainArray.length() > 0) {
+ // get last object of tokenchainarray
+ JSONObject lastTokenChainObject = stakedTokenChainArray
+ .getJSONObject(stakedTokenChainArray.length() - 1);
+
+ if (!lastTokenChainObject.has(MINE_ID)) {
+ // && stakedTokenChainArray.length() > tokenLevelValue
+
+ QuorumConsensusLogger
+ .debug("Staking 1 RBT for incoming mining transaction...");
+ tokenToStake.put(stakedTokenHash);
+ tokenToStake.put(stakedTokenChainArray);
+
+ bankArray.remove(i);
+ bankArray.put(bankObject);
+ writeToFile(PAYMENTS_PATH.concat("BNK00.json"), bankArray.toString(),
+ false);
+
+ tokenAvailableToStake = true;
+
+ break;
+ }
+
+ } else {
+
+ QuorumConsensusLogger.debug(
+ "Token Chain does not have enough height to pledge...Trying to pledge with another token!");
+ }
+ }
+ }
+
+ if (tokenAvailableToStake) {
+
+ QuorumConsensusLogger.debug("Token and TokenChain files found");
+
+ String hashString = stakedTokenHash.concat(senderDidIpfsHash);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+ BufferedImage privateShare = ImageIO
+ .read(new File(
+ DATA_PATH.concat(didHash).concat("/PrivateShare.png")));
+ String firstPrivate = PropImage.img2bin(privateShare);
+ int[] privateIntegerArray1 = strToIntArray(firstPrivate);
+ String privateBinary = Functions.intArrayToStr(privateIntegerArray1);
+ String positions = "";
+ for (int j = 0; j < privateIntegerArray1.length; j += 49152) {
+ positions += privateBinary.charAt(j);
+ }
+ tokenToStake.put(positions);
+ // ! token which will be staked
+ out.println("tokenToStake");
+ out.println(tokenToStake);
+
+ try {
+ response = in.readLine();
+ QuorumConsensusLogger
+ .debug("Staking response after verifying staked token: " + response);
+ } catch (SocketException e) {
+ QuorumConsensusLogger
+ .debug("Sender Input Stream Null - Stake Token Validation by Miner Failed");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+
+ if (response.equals("alpha-stake-token-verified")) {
+
+ JSONObject stakingSigns = new JSONObject();
+ // staker DID
+ stakingSigns.put(STAKED_QUORUM_DID, didHash);
+ // staked token and sign from staker
+ stakingSigns.put(STAKED_TOKEN, stakedTokenHash);
+ stakingSigns.put(
+ STAKED_TOKEN_SIGN, getSignFromShares(DATA_PATH + didHash +
+ "/PrivateShare.png",
+ stakedTokenHash));
+ // tid and sign from staker
+ stakingSigns.put(MINE_TID, genesisBlock.getString("tid"));
+ stakingSigns.put(
+ MINING_TID_SIGN,
+ getSignFromShares(DATA_PATH + didHash + "/PrivateShare.png",
+ genesisBlock.getString("tid")));
+ // mined token and sign from staker
+ stakingSigns.put(MINED_RBT, genesisBlock.getString("tokenHash"));
+ stakingSigns.put(
+ MINED_RBT_SIGN, getSignFromShares(DATA_PATH + didHash + "/PrivateShare.png",
+ genesisBlock.getString("tokenHash")));
+
+ genesisBlock.put(STAKE_DATA, stakingSigns);
+
+ FileWriter shareWriter = new FileWriter(new File(LOGGER_PATH + "stake.txt"),
+ true);
+ shareWriter.write(genesisBlock.toString());
+ shareWriter.close();
+ File readStake = new File(LOGGER_PATH + "stake.txt");
+ String mineID = add(readStake.toString(), ipfs);
+ pin(mineID, ipfs);
+
+ File stakeFile = new File(
+ WALLET_DATA_PATH.concat("/Stake/").concat(mineID).concat(".json"));
+ if (!stakeFile.exists())
+ stakeFile.createNewFile();
+ writeToFile(stakeFile.toString(), genesisBlock.toString(), false);
+
+ deleteFile(LOGGER_PATH + "stake.txt");
+
+ // mine ID
+ stakingSigns.put(MINE_ID, mineID);
+ stakingSigns.put(MINE_ID_SIGN,
+ getSignFromShares(DATA_PATH + didHash + "/PrivateShare.png", mineID));
+ stakingSigns.put("sender", genesisBlock.getString("sender"));
+
+ QuorumConsensusLogger.debug("Token Staked Successfully. MINE ID: " +
+ mineID);
+
+ stakedTokenChainArray.put(stakingSigns);
+ writeToFile(TOKENCHAIN_PATH + stakedTokenHash + ".json",
+ stakedTokenChainArray.toString(),
+ false);
+
+ out.println(stakingSigns.toString());
+ QuorumConsensusLogger.debug("Staking Completed!");
+
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+
+ } else {
+ QuorumConsensusLogger.debug("Token to stake not verified");
+ out.println("447");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ } else {
+ QuorumConsensusLogger.debug("Token to stake not found");
+ out.println("446");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+
+ } else {
+ QuorumConsensusLogger.debug("Token Staking Failed: Insufficient Balance to Stake!");
+ out.println("445");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+
+ } else {
+ QuorumConsensusLogger.debug("Incorrect stake details");
+ out.println("444");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ } else if (operation.equals("CID-to-pin") && port == 15041) {
+
+ QuorumConsensusLogger.debug("CID to pin request");
+ QuorumConsensusLogger.debug("appname connected is " + appName);
+ String cidString = null;
+ try {
+ cidString = in.readLine();
+ } catch (SocketException e) {
+ QuorumConsensusLogger.debug("Sender Input Stream Null - ContentID");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ QuorumConsensusLogger.debug(cidString + " data passed");
+
+ } else
+ QuorumConsensusLogger.debug("Old Credits Mining / Whole RBT Token Transfer");
+
+ String getRecData = null;
+ try {
+ getRecData = in.readLine();
+
+ if (getRecData != null) {
+ if (getRecData.contains("ping check")) {
+ QuorumConsensusLogger.debug("Ping check from sender: " + getRecData);
+ out.println("pong response");
+ } else {
+ if (getRecData.contains("blockHash")) {
+ QuorumConsensusLogger.debug("Port connected is " + port);
+ QuorumConsensusLogger.debug("inside getRecData contains Data");
+ QuorumConsensusLogger.debug("Received Details from initiator: " + getRecData);
+ readSenderData = new JSONObject(getRecData);
+ senderPrivatePos = readSenderData.getString("sign");
+ senderDidIpfsHash = readSenderData.getString("senderDID");
+ transactionID = readSenderData.getString("Tid");
+ verifySenderHash = readSenderData.getString("Hash");
+ blockHashString = readSenderData.getString("blockHash");
+ // initHash = readSenderData.getString(INIT_HASH);
+
+ syncDataTable(senderDidIpfsHash, null);
+
+ senderPID = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
+ senderDidIpfsHash);
+ QuorumConsensusLogger.debug("PID from DID "
+ + Dependency.getPIDfromDID(senderDidIpfsHash, Dependency.dataTableHashMap()));
+ //
+ String senderWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash",
+ "didHash", senderDidIpfsHash);
+ // Dependency.getWIDfromPID(senderPID, Dependency.dataTableHashMap());
+ QuorumConsensusLogger.debug("WID from PID "
+ + Dependency.getWIDfromPID(senderPID, Dependency.dataTableHashMap()));
+
+ nodeData(senderDidIpfsHash, senderWidIpfsHash, ipfs);
+ String quorumHash = calculateHash(verifySenderHash.concat(blockHashString), "SHA3-256");
+
+ QuorumConsensusLogger.debug("1: " + verifySenderHash);
+ QuorumConsensusLogger.debug("2: " + blockHashString);
+ QuorumConsensusLogger.debug("Quorum hash: " + quorumHash);
+
+ JSONObject detailsToVerify = new JSONObject();
+ detailsToVerify.put("did", senderDidIpfsHash);
+ detailsToVerify.put("hash", verifySenderHash);
+ detailsToVerify.put("signature", senderPrivatePos);
+
+ QuorumConsensusLogger.debug("Details to verify " + detailsToVerify.toString());
+
+ writeToFile(LOGGER_PATH + "tempverifysenderhash", verifySenderHash, false);
+ String verifySenderIPFSHash = IPFSNetwork.addHashOnly(
+ LOGGER_PATH + "tempverifysenderhash",
+ ipfs);
+ deleteFile(LOGGER_PATH + "tempverifysenderhash");
+
+ if (Authenticate.verifySignature(detailsToVerify.toString())) {
+ QuorumConsensusLogger.debug("Quorum Authenticated Sender");
+
+ QuorumConsensusLogger.debug("ConsensusID pass");
+ String QuorumSignature = getSignFromShares(
+ DATA_PATH + didHash + "/PrivateShare.png",
+ quorumHash);
+ out.println(QuorumSignature);
+
+ String creditSignatures = null;
+ try {
+ creditSignatures = in.readLine();
+ // QuorumConsensusLogger.debug("Credit signature is "+creditSignatures);
+ } catch (SocketException e) {
+ QuorumConsensusLogger.debug("Sender Input Stream Null - Credits");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ // QuorumConsensusLogger.debug("credit Signature " + creditSignatures);
+
+ if (!creditSignatures.equals("null")) { // commented as per test for multiple
+ // consensus
+ // threads
+ QuorumConsensusLogger.debug("inside creditsignature not equal to null");
+ FileWriter shareWriter = new FileWriter(new File(LOGGER_PATH + "mycredit.txt"),
+ true);
+ shareWriter.write(creditSignatures);
+ shareWriter.close();
+ File readCredit = new File(LOGGER_PATH + "mycredit.txt");
+ String credit = add(readCredit.toString(), ipfs);
+
+ File creditFile = new File(
+ WALLET_DATA_PATH.concat("/Credits/").concat(credit).concat(".json"));
+ if (!creditFile.exists())
+ creditFile.createNewFile();
+ writeToFile(creditFile.toString(), creditSignatures, false);
+
+ // QuorumConsensusLogger.debug("Credit object: " + credit);
+ // QuorumConsensusLogger.debug("Credit Hash: " + calculateHash(credit,
+ // "SHA3-256"));
+ JSONObject storeDetailsQuorum = new JSONObject();
+ storeDetailsQuorum.put("tid", transactionID);
+ storeDetailsQuorum.put("consensusID", verifySenderHash);
+ storeDetailsQuorum.put("sign", senderPrivatePos);
+ storeDetailsQuorum.put("credits", credit);
+ storeDetailsQuorum.put("creditHash", calculateHash(credit, "SHA3-256"));
+ storeDetailsQuorum.put("senderdid", senderDidIpfsHash);
+ storeDetailsQuorum.put("Date", Functions.getCurrentUtcTime());
+ storeDetailsQuorum.put("recdid", blockHashString);
+ JSONArray data = new JSONArray();
+ data.put(storeDetailsQuorum);
+ QuorumConsensusLogger.debug("Quorum Share: " + credit);
+ updateJSON("add", WALLET_DATA_PATH + "QuorumSignedTransactions.json",
+ data.toString());
+ deleteFile(LOGGER_PATH + "mycredit.txt");
+ writeToFile(LOGGER_PATH + "consenusIDhash", verifySenderHash, false);
+ String consenusIDhash = IPFSNetwork.add(LOGGER_PATH + "consenusIDhash", ipfs);
+ deleteFile(LOGGER_PATH + "consenusIDhash");
+ QuorumConsensusLogger.debug("added consensus ID " + consenusIDhash);
+
+ // Dependency.subscribe(transactionID);
+
+ }
+ } else {
+ QuorumConsensusLogger.debug("Sender Authentication Failure - Quorum");
+ out.println("Auth_Failed");
+ }
+
+ } else {
+ QuorumConsensusLogger.debug("Received Details from initiator: " + getRecData);
+ readSenderData = new JSONObject(getRecData);
+ senderPrivatePos = readSenderData.getString("sign");
+ senderDidIpfsHash = readSenderData.getString("senderDID");
+ transactionID = readSenderData.getString("Tid");
+ verifySenderHash = readSenderData.getString("Hash");
+ receiverDID = readSenderData.getString("RID");
+ // initHash = readSenderData.getString(INIT_HASH);
+
+ syncDataTable(senderDidIpfsHash, null);
+
+ senderPID = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
+ senderDidIpfsHash);
+ String senderWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash",
+ "didHash",
+ senderDidIpfsHash);
+
+ nodeData(senderDidIpfsHash, senderWidIpfsHash, ipfs);
+ String quorumHash = calculateHash(verifySenderHash.concat(receiverDID), "SHA3-256");
+
+ QuorumConsensusLogger.debug("1: " + senderPrivatePos);
+ QuorumConsensusLogger.debug("2: " + senderDidIpfsHash);
+ QuorumConsensusLogger.debug("3: " + transactionID);
+ QuorumConsensusLogger.debug("4: " + verifySenderHash);
+
+ JSONObject detailsToVerify = new JSONObject();
+ detailsToVerify.put("did", senderDidIpfsHash);
+ detailsToVerify.put("hash", verifySenderHash);
+ detailsToVerify.put("signature", senderPrivatePos);
+
+ writeToFile(LOGGER_PATH + "tempverifysenderhash", verifySenderHash, false);
+ String verifySenderIPFSHash = IPFSNetwork.addHashOnly(
+ LOGGER_PATH + "tempverifysenderhash",
+ ipfs);
+ deleteFile(LOGGER_PATH + "tempverifysenderhash");
+
+ if (Authenticate.verifySignature(detailsToVerify.toString())) {
+ QuorumConsensusLogger.debug("Quorum Authenticated Sender");
+
+ QuorumConsensusLogger.debug("ConsensusID pass");
+ String QuorumSignature = getSignFromShares(
+ DATA_PATH + didHash + "/PrivateShare.png",
+ quorumHash);
+
+ QuorumConsensusLogger.debug(QuorumSignature);
+ out.println(QuorumSignature);
+
+ String creditSignatures = null;
+ try {
+ creditSignatures = in.readLine();
+ } catch (SocketException e) {
+ QuorumConsensusLogger.debug("Sender Input Stream Null - Credits");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+ // QuorumConsensusLogger.debug("credit Signature " + creditSignatures);
+
+ if (!creditSignatures.equals("null")) { // commented as per test for multiple
+ // consensus
+ // threads
+ FileWriter shareWriter = new FileWriter(new File(LOGGER_PATH + "mycredit.txt"),
+ true);
+ shareWriter.write(creditSignatures);
+ shareWriter.close();
+ File readCredit = new File(LOGGER_PATH + "mycredit.txt");
+ String credit = add(readCredit.toString(), ipfs);
+
+ File creditFile = new File(
+ WALLET_DATA_PATH.concat("/Credits/").concat(credit).concat(".json"));
+ if (!creditFile.exists())
+ creditFile.createNewFile();
+ writeToFile(creditFile.toString(), creditSignatures, false);
+
+ QuorumConsensusLogger.debug("Credit object: " + credit);
+ QuorumConsensusLogger
+ .debug("Credit Hash: " + calculateHash(credit, "SHA3-256"));
+ JSONObject storeDetailsQuorum = new JSONObject();
+ storeDetailsQuorum.put("tid", transactionID);
+ storeDetailsQuorum.put("consensusID", verifySenderHash);
+ storeDetailsQuorum.put("sign", senderPrivatePos);
+ storeDetailsQuorum.put("credits", credit);
+ storeDetailsQuorum.put("creditHash", calculateHash(credit, "SHA3-256"));
+ storeDetailsQuorum.put("senderdid", senderDidIpfsHash);
+ storeDetailsQuorum.put("Date", Functions.getCurrentUtcTime());
+ storeDetailsQuorum.put("recdid", receiverDID);
+ JSONArray data = new JSONArray();
+ data.put(storeDetailsQuorum);
+ QuorumConsensusLogger.debug("Quorum Share: " + credit);
+ updateJSON("add", WALLET_DATA_PATH + "QuorumSignedTransactions.json",
+ data.toString());
+ deleteFile(LOGGER_PATH + "mycredit.txt");
+ writeToFile(LOGGER_PATH + "consenusIDhash", verifySenderHash, false);
+ String consenusIDhash = IPFSNetwork.add(LOGGER_PATH + "consenusIDhash", ipfs);
+ deleteFile(LOGGER_PATH + "consenusIDhash");
+ QuorumConsensusLogger.debug("added consensus ID " + consenusIDhash);
+ }
+
+ } else {
+ QuorumConsensusLogger.debug("Sender Authentication Failure - Quorum");
+ out.println("Auth_Failed");
+ }
+ }
+ }
+ } else {
+ QuorumConsensusLogger.debug("Quorum - " + didHash + " is unable to respond!" + getRecData);
+ out.println(getRecData);
+ }
+ } catch (SocketException e) {
+ QuorumConsensusLogger.debug("Sender Input Stream Null - Ping Check / Receiver Details");
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ }
+
+ } catch (IOException e) {
+ QuorumConsensusLogger.error("IOException Occurred", e);
+ } catch (JSONException e) {
+ QuorumConsensusLogger.error("JSONException Occurred", e);
+ } catch (NullPointerException e) {
+ QuorumConsensusLogger.error("NullPointer Exception Occurred ", e);
+ } catch (ParseException e) {
+ QuorumConsensusLogger.error("ParseException Occurred ", e);
+ } finally {
+ try {
+ QuorumConsensusLogger.debug("disconnecting connection from " + port);
+ socket.close();
+ serverSocket.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ } catch (IOException e) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPID);
+ QuorumConsensusLogger.error("IOException Occurred", e);
+ }
+
+ }
+ }
+
+ }
+}
diff --git a/src/com/rubix/Consensus/StakeConsensus.java b/src/com/rubix/Consensus/StakeConsensus.java
new file mode 100644
index 00000000..ff5583e4
--- /dev/null
+++ b/src/com/rubix/Consensus/StakeConsensus.java
@@ -0,0 +1,330 @@
+package com.rubix.Consensus;
+
+import static com.rubix.Constants.MiningConstants.MINE_ID;
+import static com.rubix.Constants.MiningConstants.MINE_ID_SIGN;
+import static com.rubix.Constants.MiningConstants.STAKED_QUORUM_DID;
+import static com.rubix.Resources.Functions.DATA_PATH;
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.calculateHash;
+import static com.rubix.Resources.Functions.getValues;
+import static com.rubix.Resources.Functions.nodeData;
+import static com.rubix.Resources.Functions.syncDataTable;
+import static com.rubix.Resources.IPFSNetwork.forward;
+import static com.rubix.Resources.IPFSNetwork.swarmConnectP2P;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.net.Socket;
+import java.net.SocketException;
+import java.util.ArrayList;
+
+import com.rubix.AuthenticateNode.Authenticate;
+import com.rubix.Resources.IPFSNetwork;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import io.ipfs.api.IPFS;
+
+public class StakeConsensus {
+ public static Logger StakeConsensusLogger = Logger.getLogger(StakeConsensus.class);
+ private static int socketTimeOut = 1800000;
+ public static volatile int STAKE_LOCKED = 0;
+ public static volatile int STAKE_SUCCESS = 0;
+ public static volatile int STAKE_FAILED = 0;
+ public static volatile JSONArray stakeDetails = new JSONArray();
+ public static volatile ArrayList stakedDIDs = new ArrayList();
+
+ // MINE_ID
+ // QST_HEIGHT
+ // STAKED_QUORUM_DID
+ // STAKED_TOKEN
+ // STAKED_TOKEN_SIGN
+ // MINE_ID_SIGN
+ // MINING_TID_SIGN
+ // MINED_RBT_SIGN
+ // same object will be added to tokenchains of staked and mined token
+
+ public static void getStakeConsensus(JSONArray signedAphaQuorumArray, JSONObject data, IPFS ipfs, int PORT,
+ String operation) {
+
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ String[] qResponse = new String[signedAphaQuorumArray.length()];
+ Socket[] qSocket = new Socket[signedAphaQuorumArray.length()];
+ PrintStream[] qOut = new PrintStream[signedAphaQuorumArray.length()];
+ BufferedReader[] qIn = new BufferedReader[signedAphaQuorumArray.length()];
+ String[] quorumPID = new String[signedAphaQuorumArray.length()];
+
+ StakeConsensusLogger.debug("Initiating Staking with " + signedAphaQuorumArray.length() + " Alpha Quorums: "
+ + signedAphaQuorumArray);
+
+ try {
+
+ for (int j = 0; j < signedAphaQuorumArray.length(); j++)
+ quorumPID[j] = signedAphaQuorumArray.getString(j);
+
+ Thread[] quorumThreads = new Thread[signedAphaQuorumArray.length()];
+ for (int i = 0; i < signedAphaQuorumArray.length(); i++) {
+ int j = i;
+ quorumThreads[i] = new Thread(() -> {
+ try {
+ StakeConsensusLogger.debug("Connecting to Quorum: " + quorumPID[j]);
+
+ swarmConnectP2P(quorumPID[j], ipfs);
+ syncDataTable(null, quorumPID[j]);
+ String quorumDidIpfsHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid",
+ quorumPID[j]);
+ String quorumWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid",
+ quorumPID[j]);
+ nodeData(quorumDidIpfsHash, quorumWidIpfsHash, ipfs);
+ String appName = quorumPID[j].concat("alpha");
+ StakeConsensusLogger.debug("quourm ID " + quorumPID[j] + " appname " + appName);
+ forward(appName, PORT + j, quorumPID[j]);
+ StakeConsensusLogger.debug(
+ "Connected to " + quorumPID[j] + " on port " + (PORT + j) + "with AppName" + appName);
+ qSocket[j] = new Socket("127.0.0.1", PORT + j);
+ qSocket[j].setSoTimeout(socketTimeOut);
+ qIn[j] = new BufferedReader(new InputStreamReader(qSocket[j].getInputStream()));
+ qOut[j] = new PrintStream(qSocket[j].getOutputStream());
+
+ qOut[j].println(operation);
+ if (operation.equals("alpha-stake-token")) {
+
+ qOut[j].println(data.toString());
+
+ String stakerDID = getValues(DATA_PATH + "DataTable.json", "didHash",
+ "peerid",
+ quorumPID[j]);
+
+ StakeConsensusLogger.debug("Mined Token Details sent for validation...DID: " + stakerDID);
+
+ try {
+ qResponse[j] = qIn[j].readLine();
+
+ if (qResponse[j] != null && qResponse[j].equals("tokenToStake")) {
+
+ try {
+ qResponse[j] = qIn[j].readLine();
+
+ if (qResponse[j] != null) {
+ /****** start */
+
+ StakeConsensusLogger
+ .debug("Mined Token Details validated. Received staked token details from DID: "
+ + stakerDID);
+ Boolean ownerCheck = true;
+
+ JSONArray stakeTokenArray = new JSONArray(qResponse[j]);
+ String stakeTokenHash = stakeTokenArray.getString(0);
+ JSONArray stakeTC = stakeTokenArray.getJSONArray(1);
+ String positionsArray = stakeTokenArray.getString(2);
+
+ // ! check ownership of stakeTC from Token Receiver logic
+
+ if (stakeTC.length() > 0 && stakeTokenHash != null
+ && positionsArray != null) {
+
+ JSONObject lastObject = stakeTC.getJSONObject(stakeTC.length() - 1);
+ StakeConsensusLogger.debug("Last Object = " + lastObject);
+ if (lastObject.has("owner")) {
+ StakeConsensusLogger
+ .debug("Checking ownership of " + stakeTokenHash
+ + " from DID "
+ + lastObject.getString("owner")
+ + " for positions send: "
+ + positionsArray);
+ String owner = lastObject.getString("owner");
+ String tokens = stakeTokenHash;
+ String hashString = tokens.concat(stakerDID);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+ String ownerIdentity = hashForPositions.concat(positionsArray);
+ String ownerRecalculated = calculateHash(ownerIdentity, "SHA3-256");
+
+ StakeConsensusLogger.debug("Ownership Here Sender Calculation");
+ StakeConsensusLogger.debug("tokens: " + tokens);
+ StakeConsensusLogger.debug("hashString: " + hashString);
+ StakeConsensusLogger.debug("hashForPositions: " + hashForPositions);
+ StakeConsensusLogger.debug("p1: " + positionsArray);
+ StakeConsensusLogger.debug("ownerIdentity: " + ownerIdentity);
+ StakeConsensusLogger
+ .debug("ownerIdentityHash: " + ownerRecalculated);
+
+ if (!owner.equals(ownerRecalculated)) {
+ ownerCheck = false;
+ STAKE_FAILED++;
+ StakeConsensusLogger.debug(
+ "Ownership Check Failed for index " + j + " with DID: "
+ + owner);
+ IPFSNetwork.executeIPFSCommands(
+ "ipfs p2p close -t /p2p/" + quorumPID[j]);
+ }
+
+ }
+ } else {
+ STAKE_FAILED++;
+ StakeConsensusLogger
+ .debug("insufficient stake token height details from DID: "
+ + stakerDID);
+ IPFSNetwork
+ .executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumPID[j]);
+ }
+
+ if (ownerCheck && !(STAKE_LOCKED > 2)) {
+ StakeConsensusLogger
+ .debug("Ownership Check Success for Peer: " + quorumPID[j]);
+ STAKE_LOCKED++;
+ qOut[j].println("alpha-stake-token-verified");
+ StakeConsensusLogger
+ .debug("Staking locked with for Peer: " + quorumPID[j]);
+ StakeConsensusLogger.debug("Waiting for stake signatures");
+
+ try {
+ qResponse[j] = qIn[j].readLine();
+ StakeConsensusLogger
+ .debug("Received stake token signatures: " + qResponse[j]);
+
+ if (qResponse[j] != null && qResponse[j].equals("447")) {
+ STAKE_FAILED++;
+ StakeConsensusLogger.debug("Token to stake not verified");
+ IPFSNetwork.executeIPFSCommands(
+ "ipfs p2p close -t /p2p/" + quorumPID[j]);
+ } else if (qResponse[j] != null) {
+ // ! add mine signs to tokenchain
+ StakeConsensusLogger.debug("Adding mine signatures");
+ JSONObject mineSigns = new JSONObject(qResponse[j]);
+
+ if (mineSigns.length() > 0) {
+
+ // stakeDetails = mineSigns;
+
+ String quorumDID = getValues(DATA_PATH + "DataTable.json",
+ "didHash", "peerid",
+ quorumPID[j]);
+
+ // ! validate signatures
+ StakeConsensusLogger
+ .debug("Validating Signatures from DID: "
+ + stakerDID);
+ JSONObject mineIDSign = new JSONObject();
+ mineIDSign.put("did", quorumDID);
+ mineIDSign.put("hash", mineSigns.getString(MINE_ID));
+ mineIDSign.put("signature",
+ mineSigns.getString(MINE_ID_SIGN));
+ boolean mineSignCheck = Authenticate
+ .verifySignature(mineIDSign.toString());
+
+ ArrayList ownersArray = new ArrayList();
+ ownersArray = IPFSNetwork.dhtOwnerCheck(MINE_ID);
+
+ if (ownersArray.contains(STAKED_QUORUM_DID)) {
+ StakeConsensusLogger
+ .debug("Staking pin check passed: " + mineSigns
+ .getString(STAKED_QUORUM_DID));
+ } else {
+ StakeConsensusLogger.debug(
+ "Staking pin check failed for DID: " + mineSigns
+ .getString(STAKED_QUORUM_DID));
+ }
+
+ if (mineSignCheck) {
+ StakeConsensusLogger.debug(
+ "########--Staking Complete " + STAKE_SUCCESS
+ + "/5 !--########");
+
+ qOut[j].println("staking-completed");
+ StakeConsensusLogger.debug(
+ "Staking completed for Peer: " + quorumPID[j]);
+ stakeDetails.put(mineSigns);
+ STAKE_SUCCESS++;
+ }
+
+ } else {
+ STAKE_FAILED++;
+ StakeConsensusLogger.debug(
+ "Stake signatures not received from DID: "
+ + stakerDID + " Quorum response: "
+ + qResponse[j]);
+ IPFSNetwork.executeIPFSCommands(
+ "ipfs p2p close -t /p2p/" + quorumPID[j]);
+ }
+ }
+
+ } catch (SocketException e) {
+ StakeConsensusLogger
+ .debug("Mined Token Details validation failed for DID: "
+ + stakerDID + " Received null response");
+ }
+
+ } else {
+ STAKE_FAILED++;
+ StakeConsensusLogger.debug("Ownership Check Failed");
+ IPFSNetwork
+ .executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumPID[j]);
+ }
+
+ /** End */
+ }
+
+ } catch (SocketException e) {
+ StakeConsensusLogger
+ .debug("Mined Token Details validation failed. Received null response");
+ IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumPID[j]);
+ }
+
+ } else if (qResponse[j] != null && qResponse[j].equals("446")) {
+ STAKE_FAILED++;
+ StakeConsensusLogger
+ .debug("Token files picked by quorum to stake is corroupted. Skipping...DID: "
+ + stakerDID);
+ IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumPID[j]);
+ } else if (qResponse[j].equals("445")) {
+ STAKE_FAILED++;
+ StakeConsensusLogger.debug("Insufficient quorum member balance. Skipping...DID: "
+ + stakerDID);
+ IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumPID[j]);
+ } else if (qResponse[j].equals("444")) {
+ STAKE_FAILED++;
+ StakeConsensusLogger.debug("Quorum could not verify mined token. Skipping...DID: "
+ + stakerDID);
+ IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumPID[j]);
+ } else {
+ STAKE_FAILED++;
+ StakeConsensusLogger.debug("Unexpected response from staker: " + qResponse[j]);
+ IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumPID[j]);
+ }
+
+ } catch (SocketException e) {
+ StakeConsensusLogger
+ .debug("Mined Token Details validation failed for DID: " + stakerDID
+ + " Received null response");
+ IPFSNetwork.executeIPFSCommands("ipfs p2p close -t /p2p/" + quorumPID[j]);
+ }
+
+ }
+
+ } catch (Exception e) {
+ StakeConsensusLogger.error("Error in quorum consensus thread: " + e);
+ }
+ StakeConsensusLogger.debug("*******STAKE_SUCCESS********* " + STAKE_SUCCESS
+ + " **********STAKE_FAILED*********** " + STAKE_FAILED);
+ });
+ quorumThreads[j].start();
+
+ // do {
+ // } while (STAKE_SUCCESS < 2 || STAKE_FAILED < 3);
+
+ }
+
+ do {
+ } while (STAKE_SUCCESS < 2 || STAKE_FAILED < 3);
+
+ } catch (Exception e) {
+ StakeConsensusLogger.error("Error in getStakeConsensus: " + e);
+ }
+
+ }
+
+}
diff --git a/src/com/rubix/Constants/ConsensusConstants.java b/src/com/rubix/Constants/ConsensusConstants.java
index b67e588c..f06f4162 100644
--- a/src/com/rubix/Constants/ConsensusConstants.java
+++ b/src/com/rubix/Constants/ConsensusConstants.java
@@ -1,11 +1,16 @@
package com.rubix.Constants;
-
public class ConsensusConstants {
public static final String TRANSACTION_ID = "Tid";
public static final String HASH = "Hash";
public static final String RECEIVERID = "RID";
-
+ public static final String INIT_HASH = "initHash";
+ public static final String TRANS_TYPE = "TRANASACTION_TYPE";
+ public static final String TRANS_STATUS = "TRANSACTION_STATUS";
+ public static final String TRANS_AMOUNT = "TRANSACTION_AMOUNT";
+ public static final String PRIMARY = "PRIMARY_TOKENS";
+ public static final String SECONDARY = "SECONDARY_TOKENS";
+ public static final String DATA = "DATA_VALIDATION";
}
diff --git a/src/com/rubix/Constants/MiningConstants.java b/src/com/rubix/Constants/MiningConstants.java
new file mode 100644
index 00000000..4d44faed
--- /dev/null
+++ b/src/com/rubix/Constants/MiningConstants.java
@@ -0,0 +1,17 @@
+package com.rubix.Constants;
+
+public class MiningConstants {
+
+ public static final String MINE_TID = "mineTransactionID";
+ public static final String MINED_RBT = "minedToken";
+ public static final String MINE_ID = "mineID";
+ public static final String STAKED_QUORUM_DID = "stakedQuorumDID";
+ public static final String STAKE_DATA = "stakeData";
+ public static final String STAKED_TOKEN = "stakedToken";
+ public static final String STAKED_TOKEN_SIGN = "stakedTokenSignature";
+ public static final String MINING_TID_SIGN = "TIDSignature";
+ public static final String MINED_RBT_SIGN = "minedTokenSignature";
+ public static final String MINE_ID_SIGN = "mineIDSignature";
+ public static final String QST_HEIGHT = "QSTHeight";
+ public static final String QUORUM_SIGN_CONTENT = "quorumSignContent";
+}
\ No newline at end of file
diff --git a/src/com/rubix/Datum/DataCommitter.java b/src/com/rubix/Datum/DataCommitter.java
new file mode 100644
index 00000000..6ea8482d
--- /dev/null
+++ b/src/com/rubix/Datum/DataCommitter.java
@@ -0,0 +1,911 @@
+package com.rubix.Datum;
+
+import static com.rubix.Resources.Functions.DATA_PATH;
+import static com.rubix.Resources.Functions.DATUM_CHAIN_PATH;
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.PAYMENTS_PATH;
+import static com.rubix.Resources.Functions.QuorumCheck;
+import static com.rubix.Resources.Functions.QuorumSwarmConnect;
+import static com.rubix.Resources.Functions.SEND_PORT;
+import static com.rubix.Resources.Functions.TOKENCHAIN_PATH;
+import static com.rubix.Resources.Functions.TOKENS_PATH;
+import static com.rubix.Resources.Functions.WALLET_DATA_PATH;
+import static com.rubix.Resources.Functions.calculateHash;
+import static com.rubix.Resources.Functions.deleteFile;
+import static com.rubix.Resources.Functions.getCurrentUtcTime;
+import static com.rubix.Resources.Functions.getPeerID;
+import static com.rubix.Resources.Functions.getQuorum;
+import static com.rubix.Resources.Functions.getSignFromShares;
+import static com.rubix.Resources.Functions.getValues;
+import static com.rubix.Resources.Functions.minQuorum;
+import static com.rubix.Resources.Functions.readFile;
+import static com.rubix.Resources.Functions.strToIntArray;
+import static com.rubix.Resources.Functions.updateJSON;
+import static com.rubix.Resources.Functions.updateQuorum;
+import static com.rubix.Resources.Functions.writeToFile;
+import static com.rubix.Resources.IPFSNetwork.add;
+import static com.rubix.Resources.IPFSNetwork.pin;
+import static com.rubix.Resources.IPFSNetwork.repo;
+
+import java.awt.image.BufferedImage;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.net.Socket;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.imageio.ImageIO;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.rubix.AuthenticateNode.PropImage;
+import com.rubix.Consensus.DatainitiatorProcedure;
+import com.rubix.Consensus.InitiatorConsensus;
+import com.rubix.Consensus.InitiatorProcedure;
+import com.rubix.Resources.Functions;
+import com.rubix.Resources.IPFSNetwork;
+
+import io.ipfs.api.IPFS;
+
+public class DataCommitter {
+ private static final Logger DataCommitterLogger = Logger.getLogger(DataCommitter.class);
+ private static final Logger eventLogger = Logger.getLogger("eventLogger");
+
+ public static BufferedReader serverInput;
+ private static PrintStream output;
+ private static BufferedReader input;
+ private static Socket senderSocket;
+ private static boolean senderMutex = false;
+ private static HashMap dataTableHashMap = Dependency.dataTableHashMap();
+
+ /**
+ * A committer node to commit data
+ *
+ * @param data Details required for dataCommit
+ * @param ipfs IPFS instance
+ * @param port committer port for communication
+ * @return Transaction Details (JSONObject)
+ * @throws IOException handles IO Exceptions
+ * @throws JSONException handles JSON Exceptions
+ * @throws NoSuchAlgorithmException handles No Such Algorithm Exceptions
+ */
+
+ public static boolean compareJsons(JSONArray j1, JSONArray j2) {
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ boolean isEqual = true;
+ ArrayList dataToken = new ArrayList();
+
+ // data
+ for (int i = 0; i < j1.length(); i++) {
+ // bank
+ for (int j = 0; j < j2.length(); j++) {
+
+ }
+ }
+
+ return isEqual;
+ }
+
+ /**
+ * @param data
+ * @param ipfs
+ * @param port
+ * @return
+ * @throws Exception
+ */
+ public static JSONObject Commit(String data, IPFS ipfs, int port) throws Exception {
+ repo(ipfs);
+ Dependency.checkDatumPath();
+ JSONObject APIResponse = new JSONObject();
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ JSONObject detailsObject = new JSONObject(data);
+ // DataCommitterLogger.debug("data is "+data);
+ String pvt = detailsObject.getString("pvt");
+ int type = detailsObject.getInt("type");
+ String comment = detailsObject.getString("comment");
+
+
+ APIResponse = new JSONObject();
+
+ // DataCommitterLogger.debug("detailsObject is "+ detailsObject.toString());
+
+ String senderPeerID = getPeerID(DATA_PATH + "DID.json");
+ // DataCommitterLogger.debug("sender peer id" + senderPeerID);
+ String senderDidIpfsHash = Dependency.getDIDfromPID(senderPeerID, dataTableHashMap);
+ // getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", senderPeerID);
+ DataCommitterLogger.debug("sender did ipfs hash" + senderDidIpfsHash);
+
+ if (senderMutex) {
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender busy. Try again later");
+ DataCommitterLogger.warn("Sender busy");
+ return APIResponse;
+ }
+
+ /*
+ * if(!detailsObject.has(TRANS_TYPE)) { APIResponse.put("ERROR",
+ * "TRANS_TYPE not found"); APIResponse.put("did", senderDidIpfsHash);
+ * APIResponse.put("tid", "null"); APIResponse.put("status", "Failed");
+ * APIResponse.put("message", "TRANS_TYPE not found"); return APIResponse; }
+ */
+
+ senderMutex = true;
+ DataCommitterLogger.debug("sender mutex is " + senderMutex);
+ // DataCommitterLogger.debug("Transscation type is "+
+ // detailsObject.getString(TRANS_TYPE));
+
+ /*
+ *
+ * Data Jar Starts here
+ *
+ *
+ */
+ // DataCommitterLogger.debug("skipping normal trnx");
+ String datumFolderPath = DATUM_CHAIN_PATH;
+ File datumFolder = new File(datumFolderPath);
+ if (!datumFolder.exists()) {
+ DataCommitterLogger.debug("datum Folder is missing");
+ datumFolder.mkdir();
+ DataCommitterLogger.debug("datum Folder created");
+
+ }
+ File datumCommitHistory = new File(datumFolderPath.concat("datumCommitHistory.json"));
+ if (!datumCommitHistory.exists()) {
+ DataCommitterLogger.debug("datumCommitHistory is missing");
+ datumCommitHistory.createNewFile();
+ writeToFile(datumCommitHistory.toString(), "[]", false);
+ DataCommitterLogger.debug("datumCommitHistory is created");
+ }
+ File datumCommitToken = new File(PAYMENTS_PATH.concat("dataToken.json"));
+ if (!datumCommitToken.exists()) {
+ DataCommitterLogger.debug("datumCommitToken is missing");
+ datumCommitToken.createNewFile();
+ writeToFile(datumCommitToken.toString(), "[]", false);
+ }
+
+ File datumTokenFolder = new File(datumFolderPath + "DatumTokens/");
+ if (!datumTokenFolder.exists()) {
+ DataCommitterLogger.debug("datum token Folder is missing");
+ datumTokenFolder.mkdir();
+ DataCommitterLogger.debug("datum token Folder created");
+ }
+ // File datumCommitChain =
+
+
+
+
+
+ String blockHash = detailsObject.getString("blockHash");
+ // DataCommitterLogger.debug("blockhash is "+ blockHash);
+ String authSenderByRecHash = calculateHash(blockHash + senderDidIpfsHash + comment, "SHA3-256");
+ // TokenSenderLogger.debug("iauthSenderByRecHash is "+authSenderByRecHash);
+ // TokenSenderLogger.debug("Hash to verify Data: " + authSenderByRecHash);
+ String tid = calculateHash(authSenderByRecHash, "SHA3-256");
+ // TokenSenderLogger.debug("DataInitator by Data Hash " + authSenderByRecHash);
+ // TokenSenderLogger.debug("TID on Initator " + tid);
+
+ // TokenSenderLogger.debug("BlockHash "+blockHash+" fetched from
+ // datumCommitChain is "+
+ // getValues(datumFolderPath.concat("datumCommitChain.json"), "blockHash",
+ // "blockHash", blockHash));
+
+ String bankFile = readFile(PAYMENTS_PATH.concat("BNK00.json"));
+ JSONArray bankArray = new JSONArray(bankFile);
+ // JSONArray wholeTokens = new JSONArray();
+ JSONArray wholeTokensListForData = new JSONArray();
+ wholeTokensListForData.put(Dependency.tokenToCommit());
+
+ String dat01File = PAYMENTS_PATH.concat("DAT01.json");
+ File DAT_TOKEN_FILE = new File(dat01File);
+
+ if (!DAT_TOKEN_FILE.exists()) {
+ DataCommitterLogger.debug("Data token file not found");
+ DAT_TOKEN_FILE.createNewFile();
+ writeToFile(DAT_TOKEN_FILE.toString(), "[]", false);
+ DataCommitterLogger.debug("Data token file created");
+
+ } else {
+ DataCommitterLogger.debug("Data token file exists");
+ }
+
+ // JSONArray datArray = new JSONArray(DAT_TOKEN_FILE);
+ String dataTokenFile = readFile(PAYMENTS_PATH.concat("DAT01.json"));
+ JSONArray datArray = new JSONArray(dataTokenFile);
+
+ DataCommitterLogger.debug("DAT01 size is " + datArray.length());
+
+ // if (datArray.length() < 1) {
+ // if (bankArray.length() < 1) {
+ // senderMutex = false;
+ // APIResponse.put("ERROR", "Insufficent Balance");
+ // APIResponse.put("did", senderDidIpfsHash);
+ // APIResponse.put("tid", "null");
+ // APIResponse.put("status", "Failed");
+ // APIResponse.put("message", "Insufficent Balance");
+ // return APIResponse;
+ // } else {
+ // // DataCommitterLogger.debug("Data token is empty");
+ //
+ // DataCommitterLogger.debug("Token to be moved to DAT01 " +
+ // bankArray.get(0).toString());
+ // DataCommitterLogger.debug("Token to be moved to DAT01 in json is" +
+ // bankArray.get(0));
+ //
+ // JSONObject tokenChainName = new JSONObject(bankArray.get(0).toString());
+ // DataCommitterLogger.debug("tokenChainName is " + tokenChainName);
+ // // DataCommitterLogger.debug("tokenChainType is " +
+ // // tokenChainName.getClass().getName());
+ // JSONArray tokenNameArray = new JSONArray();
+ // tokenNameArray.put(tokenChainName);
+ // DataCommitterLogger.debug("tokenNameArray " + tokenNameArray);
+ // updateJSON("add", dat01File, tokenNameArray.toString());
+ // DataCommitterLogger.debug("dat01 open");
+ // dataTokenFile = readFile(PAYMENTS_PATH.concat("DAT01.json"));
+ // datArray = new JSONArray(dataTokenFile);
+ // }
+ // } // else
+ /*
+ * { DataCommitterLogger.debug("Token in data commit is "+ datArray.toString());
+ * DataCommitterLogger.debug("bankArray.toString() "+ bankArray.toString());
+ * DataCommitterLogger.debug("datArray.get(0).toString() "+
+ * datArray.get(0).toString()); DataCommitterLogger.
+ * debug("datArray.get(0).toString().contains(bankArray.toString()) is "+
+ * datArray.get(0).toString().contains(bankArray.toString()));
+ *
+ * for(int i=0;i dataToken = new ArrayList();
+ List bankToken = new ArrayList();
+
+ // for (int i = 0; i < datArray.length(); i++) {
+ // dataToken.add(datArray.get(i).toString());
+ // }
+ dataToken.add(unstakedToken);
+ for (int j = 0; j < bankArray.length(); j++) {
+ bankToken.add(bankArray.get(j).toString());
+ }
+
+ dataToken.retainAll(bankToken);
+ Set dataTokenSet = new LinkedHashSet(dataToken);
+ Set bankTokenSet = new LinkedHashSet(bankToken);
+ JSONArray jsonArr = new JSONArray(dataTokenSet.toString());
+ datArray = jsonArr;
+
+ DataCommitterLogger.debug("datArray is " + datArray.toString());
+ DataCommitterLogger.debug("jsonArr is " + jsonArr.toString());
+ DataCommitterLogger.debug("datArray size after cleanup is " +
+ datArray.length());
+
+ writeToFile(dataTokenFile, datArray.toString(), false);
+
+ int wholeAmount = datArray.length();
+ // DataCommitterLogger.debug("Whole amount is " + wholeAmount);
+
+ // DataCommitterLogger.debug(
+ // getValues(datumFolderPath.concat("datumCommitHistory.json"), "blockHash",
+ // "blockHash", blockHash));
+
+ if (getValues(datumFolderPath.concat("datumCommitHistory.json"), "blockHash", "blockHash", blockHash)
+ .equals(blockHash)) {
+ senderMutex = false;
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Block Hash already exists");
+ return APIResponse;
+ }
+
+ // JSONArray allTokens = new JSONArray();
+
+ // for (int i = 0; i < wholeAmount; i++) {
+ // // wholeTokens.put(bankArray.getJSONObject(i).getString("tokenHash"));
+ // wholeTokensListForData.put(datArray.getJSONObject(i).getString("tokenHash"));
+ //
+ // }
+
+ // DataCommitterLogger.debug("WholeTokens for data is " +
+ // wholeTokensListForData.toString());
+ //
+ // for (int i = 0; i < wholeTokensListForData.length(); i++) {
+ // String tokenRemove = wholeTokensListForData.getString(i);
+ // for (int j = 0; j < datArray.length(); j++) {
+ // if (datArray.getJSONObject(j).getString("tokenHash").equals(tokenRemove))
+ // datArray.remove(j);
+ // }
+ // }
+
+ // unstakedToken;
+ // JSONArray wholeTokenChainHash = new JSONArray();
+
+ DataCommitterLogger.debug("wholeTokensListForData " + wholeTokensListForData.toString());
+ JSONArray wholeTokenForDataChainHash = new JSONArray();
+ JSONArray tokenPreviousSender = new JSONArray();
+
+ File token = new File(TOKENS_PATH + unstakedToken);
+ File tokenchain = new File(TOKENCHAIN_PATH + unstakedToken + ".json");
+ if (!(token.exists() && tokenchain.exists())) {
+ DataCommitterLogger.info("Tokens Not Verified : " + unstakedToken);
+ senderMutex = false;
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Invalid token(s)");
+ return APIResponse;
+ }
+ // DataCommitterLogger.debug("wholeTokenForDataHashIPFSPin path is " +
+ // unstakedToken);
+
+ String wholeTokenForDataHashIPFSPin = add(TOKENS_PATH + unstakedToken, ipfs);
+
+ DataCommitterLogger.debug("Whole Token wholeTokenForDataHashIPFSPin is " + wholeTokenForDataHashIPFSPin);
+ pin(wholeTokenForDataHashIPFSPin, ipfs);
+ String tokenChainForDataHashIPFSPin = add(TOKENCHAIN_PATH + unstakedToken + ".json", ipfs);
+ wholeTokenForDataChainHash.put(tokenChainForDataHashIPFSPin);
+
+ // DataCommitterLogger.debug("IPFS pinned tokenchain is " +
+ // tokenChainForDataHashIPFSPin);
+ // DataCommitterLogger.debug("IPFS pinned token is " +
+ // wholeTokenForDataHashIPFSPin);
+ // DataCommitterLogger.debug("Whole token chain hash " +
+ // wholeTokenForDataChainHash);
+
+ // DataCommitterLogger.debug("tokenchain open");
+
+ String tokenChainFileContent = readFile(TOKENCHAIN_PATH + unstakedToken + ".json");
+
+ // tokenChainFileContent = null;
+
+ // tokenChainFileContent = Functions
+ // .readFile(TOKENCHAIN_PATH + wholeTokensListForData.get(i) + ".json");
+
+ // DataCommitterLogger.debug("tokenChainFile content is "+
+ // tokenChainFileContent);
+ JSONArray tokenChainFileArray = new JSONArray(tokenChainFileContent);
+ // DataCommitterLogger.debug("Tokenchain length is " +
+ // tokenChainFileArray.length());
+
+ // DataCommitterLogger.debug("tokenChainFileArray is
+ // "+tokenChainFileArray.toString());
+ JSONArray previousSenderArray = new JSONArray();
+ JSONObject lastObject = tokenChainFileArray.getJSONObject(tokenChainFileArray.length() - 1);
+ // DataCommitterLogger.debug("LastObject " + lastObject.toString());
+ // TokenSenderLogger.debug("Last object is "+lastObject.toString());
+ // DataCommitterLogger.debug("tokenChainFileArray
+ // "+tokenChainFileArray.toString());
+
+ for (int j = 0; j < tokenChainFileArray.length(); j++) {
+ String peerIDString = Dependency.getPIDfromDID(tokenChainFileArray.getJSONObject(j).getString("sender"),
+ dataTableHashMap);
+ if (peerIDString.contains("Not Found")) {
+ // throw new IOException("PeerID not found for the did");
+ APIResponse.put("ERROR",
+ "PeerID not found for the DID " + tokenChainFileArray.getJSONObject(j).getString("sender"));
+ APIResponse.put("blockHash", blockHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Kindly rotate the token to add more commits");
+ senderMutex = false;
+ return APIResponse;
+
+ } else {
+ previousSenderArray.put(peerIDString);
+ }
+
+ }
+
+ /*
+ * if (tokenChainFileArray.length() > 0) { // JSONObject lastObject = //
+ * tokenChainFileArray.getJSONObject(tokenChainFileArray.length() - 1);
+ *
+ * for (int j = 0; j < tokenChainFileArray.length(); j++) { String peerID =
+ * getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
+ * tokenChainFileArray.getJSONObject(j).getString("sender"));
+ * previousSenderArray.put(peerID); } }
+ */
+
+ if (tokenChainFileArray.length() > 256) {
+ int dataCtr = 0;
+ int nodataCtr = 0;
+ List tokenChainContentSet = new ArrayList();
+ // System.out.println(tokenChainFileArray.getJSONObject(((tokenChainFileArray.length())-256)).toString());
+ // System.out.println("loop starting at "+ (tokenChainFileArray.length()-256));
+ for (int dataCount = ((tokenChainFileArray.length()) - 256); dataCount < tokenChainFileArray
+ .length(); dataCount++) {
+ tokenChainContentSet.add(tokenChainFileArray.getJSONObject(dataCount).toString());
+ }
+ // System.out.println("Hashset size is " + tokenChainContentSet.size());
+ // System.out.println(tokenChainContentSet.get(255).toString());
+
+ for (int d = 0; d < tokenChainContentSet.size(); d++) {
+ if (!tokenChainContentSet.get(d).contains("blockHash")) {
+ nodataCtr++;
+ break;
+ } else {
+ dataCtr++;
+ }
+ }
+ DataCommitterLogger.debug("Data commit counter is " + dataCtr);
+ DataCommitterLogger.debug("nodataCtr counter is " + nodataCtr);
+ if (dataCtr >= 256) {
+ senderMutex = false;
+ APIResponse.put("ERROR", "Commit limit exceeded");
+ APIResponse.put("blockHash", blockHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Kindly rotate the token to add more commits");
+ return APIResponse;
+ }
+
+ }
+ // DataCommitterLogger.debug("Hash set end time is "+LocalDateTime.now());
+ // DataCommitterLogger.debug("Normal strating time is "+LocalDateTime.now());
+ //
+ //
+ //
+ // if(tokenChainFileArray.length()>256) {
+ // int dataCtr = 0;
+ // int nodataCtr = 0;
+ // //System.out.println(tokenChainFileArray.getJSONObject(((tokenChainFileArray.length())-256)).toString());
+ // //System.out.println("loop starting at "+
+ // (tokenChainFileArray.length()-256));
+ // for(int
+ // dataCount=((tokenChainFileArray.length())-256);dataCount=256) {
+ // senderMutex =false;
+ // APIResponse.put("ERROR", "Commit limit exceeded");
+ // APIResponse.put("blockHash", blockHash);
+ // APIResponse.put("tid", "null");
+ // APIResponse.put("status", "Failed");
+ // APIResponse.put("message", "Kindly rotate the token to add more commits");
+ // // return APIResponse;
+ // }
+ // }
+ // DataCommitterLogger.debug("Normal ending time is "+LocalDateTime.now());
+
+ // if (lastObject.has("mineID")) {
+ // wholeTokensListForData.remove(i);
+ // }
+
+ JSONObject previousSenderObject = new JSONObject();
+ previousSenderObject.put("token", wholeTokenForDataHashIPFSPin);
+ previousSenderObject.put("sender", previousSenderArray);
+ tokenPreviousSender.put(previousSenderObject);
+
+ // DataCommitterLogger.debug("previousSenderObject is " +
+ // previousSenderObject.get("sender").toString());
+ DataCommitterLogger.debug("previousSender size is " + previousSenderArray.length());
+
+ JSONArray dataCommitToken = new JSONArray();
+
+ DataCommitterLogger.debug("unstakedToken is " + unstakedToken);
+ DataCommitterLogger.debug(wholeTokensListForData.getString(0));
+
+ dataCommitToken.put(unstakedToken);
+
+ DataCommitterLogger.debug("WholeToken for data is " + unstakedToken);
+ // DataCommitterLogger.debug("dataCommitToken for data is " +
+ // dataCommitToken.toString());
+
+ // DataCommitterLogger.debug("Whole token length is "+
+ // wholeTokensListForData.length()+" and Whole token content is
+ // "+wholeTokensListForData.toString());
+
+ JSONArray positionsArray = new JSONArray();
+ String tokens = new String();
+ // DataCommitterLogger.debug("All token lenght is " + dataCommitToken.length() +
+ // " Whole token length is "
+ // + wholeTokensListForData.length());
+ for (int i = 0; i < dataCommitToken.length(); i++) {
+ tokens = dataCommitToken.getString(i);
+ DataCommitterLogger.debug("tokens in string is " + tokens);
+ String hashString = tokens.concat(senderDidIpfsHash);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+ BufferedImage privateShare = ImageIO
+ .read(new File(DATA_PATH.concat(senderDidIpfsHash).concat("/PrivateShare.png")));
+ String firstPrivate = PropImage.img2bin(privateShare);
+ int[] privateIntegerArray1 = strToIntArray(firstPrivate);
+ String privateBinary = Functions.intArrayToStr(privateIntegerArray1);
+ String positions = "";
+ for (int j = 0; j < privateIntegerArray1.length; j += 49152) {
+ positions += privateBinary.charAt(j);
+ }
+ positionsArray.put(positions);
+
+ String ownerIdentity = hashForPositions.concat(positions);
+ String ownerIdentityHash = calculateHash(ownerIdentity, "SHA3-256");
+
+ DataCommitterLogger.debug("Ownership Here Sender Calculation");
+ DataCommitterLogger.debug("tokens: " + tokens);
+ DataCommitterLogger.debug("hashString: " + hashString);
+ DataCommitterLogger.debug("hashForPositions: " + hashForPositions);
+ DataCommitterLogger.debug("p1: " + positions);
+ DataCommitterLogger.debug("ownerIdentity: " + ownerIdentity);
+ DataCommitterLogger.debug("ownerIdentityHash: " + ownerIdentityHash);
+
+ }
+
+ JSONArray alphaQuorum = new JSONArray();
+ JSONArray betaQuorum = new JSONArray();
+ JSONArray gammaQuorum = new JSONArray();
+ int alphaSize;
+
+ ArrayList alphaPeersList;
+ ArrayList betaPeersList;
+ ArrayList gammaPeersList;
+
+ JSONArray quorumArray;
+ // TokenSenderLogger.debug("Type "+type);
+ switch (type) {
+ case 1: {
+ writeToFile(LOGGER_PATH + "tempbeta", tid.concat(senderDidIpfsHash), false);
+ String betaHash = IPFSNetwork.add(LOGGER_PATH + "tempbeta", ipfs);
+ deleteFile(LOGGER_PATH + "tempbeta");
+
+ writeToFile(LOGGER_PATH + "tempgamma", tid.concat(blockHash), false);
+ String gammaHash = IPFSNetwork.add(LOGGER_PATH + "tempgamma", ipfs);
+ deleteFile(LOGGER_PATH + "tempgamma");
+
+ quorumArray = getQuorum(senderDidIpfsHash, blockHash, 0);
+ break;
+ }
+
+ case 2: {
+ DataCommitterLogger.debug("quorumlist open");
+ quorumArray = new JSONArray(readFile(DATA_PATH + "quorumlist.json"));
+ break;
+ }
+ case 3: {
+ quorumArray = detailsObject.getJSONArray("quorum");
+ break;
+ }
+ default: {
+ DataCommitterLogger.error("Unknown quorum type input, cancelling transaction");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Unknown quorum type input, cancelling transaction");
+ return APIResponse;
+
+ }
+ }
+
+ DataCommitterLogger.debug("Quorum list " + quorumArray.toString());
+ int alphaCheck = 0, betaCheck = 0, gammaCheck = 0;
+ JSONArray sanityFailedQuorum = new JSONArray();
+ DataCommitterLogger.debug("Getting into Sanity Check for quorum");
+ for (int i = 0; i < quorumArray.length(); i++) {
+ String quorumPeerID = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
+ quorumArray.getString(i));
+ boolean quorumSanityCheck = Functions.sanityCheck("Quorum", quorumPeerID, ipfs, port + 11);
+ DataCommitterLogger.debug("quorumSanityCheck is " + quorumSanityCheck);
+
+ if (!quorumSanityCheck) {
+ sanityFailedQuorum.put(quorumPeerID);
+ if (i <= 6)
+ alphaCheck++;
+ if (i >= 7 && i <= 13)
+ betaCheck++;
+ if (i >= 14 && i <= 20)
+ gammaCheck++;
+ }
+ }
+
+ if (alphaCheck > 2 || betaCheck > 2 || gammaCheck > 2) {
+ DataCommitterLogger.debug("Inisde alpha beta gamma check");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ String message = "Quorum: ".concat(sanityFailedQuorum.toString()).concat(" ");
+ APIResponse.put("message", message.concat(Functions.sanityMessage));
+ DataCommitterLogger.warn("Quorum: ".concat(message.concat(Functions.sanityMessage)));
+ return APIResponse;
+ }
+
+ long startTime, endTime, totalTime;
+
+ QuorumSwarmConnect(quorumArray, ipfs);
+
+ alphaSize = quorumArray.length() - 14;
+
+ for (int i = 0; i < alphaSize; i++)
+ alphaQuorum.put(quorumArray.getString(i));
+
+ for (int i = 0; i < 7; i++) {
+ betaQuorum.put(quorumArray.getString(alphaSize + i));
+ gammaQuorum.put(quorumArray.getString(alphaSize + 7 + i));
+ }
+ startTime = System.currentTimeMillis();
+
+ // DataCommitterLogger.debug("Alpha node list is " + alphaQuorum.toString());
+
+ alphaPeersList = QuorumCheck(alphaQuorum, alphaSize);
+ betaPeersList = QuorumCheck(betaQuorum, 7);
+ gammaPeersList = QuorumCheck(gammaQuorum, 7);
+ // DataCommitterLogger.debug("Alpha peer list list is " + alphaPeersList);
+
+ endTime = System.currentTimeMillis();
+ totalTime = endTime - startTime;
+ eventLogger.debug("Quorum Check " + totalTime);
+
+ if (alphaPeersList.size() < minQuorum(alphaSize) || betaPeersList.size() < 5 || gammaPeersList.size() < 5) {
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Quorum Members not available");
+ DataCommitterLogger.warn("Quorum Members not available");
+ senderMutex = false;
+ return APIResponse;
+ }
+
+ JSONObject tokenDetails = new JSONObject();
+ tokenDetails.put("whole-tokens", wholeTokensListForData);
+ tokenDetails.put("whole-tokenChains", wholeTokenForDataChainHash);
+ // tokenDetails.put("hashSender", partTokenChainHash);
+ // tokenDetails.put("part-tokens", partTokens);
+ // tokenDetails.put("part-tokenChains", partTokenChainArrays);
+ tokenDetails.put("sender", senderDidIpfsHash);
+
+ // DataCommitterLogger.debug("tokenDetails is " + tokenDetails.toString());
+
+ // DataCommitterLogger.debug("tokenDetails " + tokenDetails.toString());
+ String doubleSpendString = tokenDetails.toString();
+
+ String doubleSpend = calculateHash(doubleSpendString, "SHA3-256");
+ writeToFile(LOGGER_PATH + "doubleSpend", doubleSpend, false);
+ DataCommitterLogger.debug("********Double Spend Hash*********: " + doubleSpend);
+ IPFSNetwork.addHashOnly(LOGGER_PATH + "doubleSpend", ipfs);
+ deleteFile(LOGGER_PATH + "doubleSpend");
+
+ JSONObject dataObject = new JSONObject();
+ dataObject.put("tid", tid);
+ // dataObject.put(TRANS_TYPE, detailsObject.getString(TRANS_TYPE));
+ dataObject.put("blockHash", blockHash);
+ dataObject.put("pvt", pvt);
+ dataObject.put("senderDidIpfs", senderDidIpfsHash);
+ dataObject.put("token", wholeTokensListForData.get(0).toString());
+ dataObject.put("alphaList", alphaPeersList);
+ dataObject.put("betaList", betaPeersList);
+ dataObject.put("gammaList", gammaPeersList);
+
+ // DataCommitterLogger.debug("dataobject for Data (Double Spend Hash) " +
+ // dataObject.toString());
+
+ DataCommitterLogger.debug("Starting consensusSetUp");
+ DatainitiatorProcedure.dataConsensusSetUp(dataObject.toString(), ipfs, SEND_PORT + 100, alphaSize);
+ DataCommitterLogger.debug("SENDport is " + SEND_PORT);
+ // InitiatorConsensus.quorumSignature.length() + "response count "
+ // + InitiatorConsensus.quorumResponse);
+ if (InitiatorConsensus.quorumSignature.length() < (minQuorum(alphaSize) + 2 * minQuorum(7))) {
+ DataCommitterLogger.debug("Consensus Failed");
+ // senderDetails2Receiver.put("status", "Consensus Failed");
+ // output.println(senderDetails2Receiver);
+ // executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ output.close();
+ input.close();
+ senderSocket.close();
+ senderMutex = false;
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Data commit declined by Quorum");
+ return APIResponse;
+
+ }
+
+ DataCommitterLogger.debug("Consensus Reached");
+ DataCommitterLogger.debug("Quorum Signatures length " + InitiatorConsensus.quorumSignature.length());
+ String senderSign = getSignFromShares(pvt, authSenderByRecHash);
+ Iterator keys = InitiatorConsensus.quorumSignature.keys();
+ JSONArray signedQuorumList = new JSONArray();
+
+ // TokenSenderLogger.debug("signed quorumlist count is "+keys.toString());
+
+ int ctr = 0;
+
+ while (keys.hasNext()) {
+ ctr++;
+ signedQuorumList.put(keys.next());
+
+ }
+
+ DataCommitterLogger.debug("signed Quorum list count is " + signedQuorumList.length());
+ DataCommitterLogger.debug("signed Quorum list is " + signedQuorumList.toString());
+
+ JSONObject blockHashObject = new JSONObject();
+ blockHashObject.put("blockHash", blockHash);
+ blockHashObject.put("token", dataCommitToken.toString().substring(1, dataCommitToken.toString().length() - 1));
+ blockHashObject.put("committer", senderDidIpfsHash);
+ JSONArray blockHashArray = new JSONArray();
+ blockHashArray.put(blockHashObject);
+ String dataTokenNameString = datumTokenFolder + "/" + blockHash + ".json";
+
+ DataCommitterLogger.debug("path is " + dataTokenNameString);
+ writeToFile(dataTokenNameString, blockHashArray.toString(), false);
+ // writeToFile(datumTokenFolder+ blockHash + ".json" ,
+ // blockHashArray.toString(), false);
+ String filenameString = add(dataTokenNameString, ipfs);
+ DataCommitterLogger.debug("CID is " + filenameString);
+ pin(filenameString, ipfs);
+ Path renameFile = Paths.get(dataTokenNameString);
+ Files.move(renameFile, renameFile.resolveSibling(filenameString));
+
+ /*
+ * try { senderSocket = new Socket("127.0.0.1", 15041); DataOutputStream dout =
+ * new DataOutputStream(senderSocket.getOutputStream());
+ * dout.writeUTF("Hello Server"); dout.flush(); dout.close();
+ * senderSocket.close(); } catch (Exception e) { System.out.println(e); }
+ */
+
+ /*
+ * for (int i = 0; i < signedQuorumList.length(); i++) {
+ * DataCommitterLogger.debug("signed quorum at index " + i + " is " +
+ * signedQuorumList.getString(i));
+ * DataTokenPin.Ping(Dependency.getPIDfromDID(signedQuorumList.getString(i),
+ * dataTableHashMap), 15021, filenameString);
+ * //DataTokenPin.Ping(signedQuorumList.getString(i), port + 11,
+ * filenameString); }
+ */
+
+ APIResponse.put("tid", tid);
+ APIResponse.put("committedToken", wholeTokensListForData.getString(0));
+ APIResponse.put("blockHash", blockHash);
+ APIResponse.put("status", "Success");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("message", "Data added successfully");
+ APIResponse.put("quorumlist", signedQuorumList);
+ APIResponse.put("CID", filenameString);
+ APIResponse.put("totaltime", totalTime);
+
+ // TokenSenderLogger.debug("API Response is "+APIResponse.toString());
+
+ updateQuorum(quorumArray, signedQuorumList, true, type);
+
+ JSONObject dataBlockRecord = new JSONObject();
+ dataBlockRecord.put("role", "DataCommitter");
+ dataBlockRecord.put("tokens", dataCommitToken.toString().substring(1, dataCommitToken.toString().length() - 1));
+ dataBlockRecord.put("txn", tid);
+ dataBlockRecord.put("quorumList", signedQuorumList);
+ dataBlockRecord.put("senderDID", senderDidIpfsHash);
+ dataBlockRecord.put("receiverDID", senderDidIpfsHash);
+ dataBlockRecord.put("blockHash", blockHash);
+ dataBlockRecord.put("Date", getCurrentUtcTime());
+ dataBlockRecord.put("totalTime", totalTime);
+ dataBlockRecord.put("comment", comment);
+ dataBlockRecord.put("essentialShare", InitiatorProcedure.essential);
+ // requestedAmount = formatAmount(requestedAmount);
+ // dataBlockRecord.put("amount-spent", requestedAmount);
+
+ // DataCommitterLogger.debug("data block record is " +
+ // dataBlockRecord.toString());
+ JSONArray dataBlockEntry = new JSONArray();
+ dataBlockEntry.put(dataBlockRecord);
+ // DataCommitterLogger.debug("dataBlockRecord being added to json files");
+ updateJSON("add", datumFolderPath.concat("datumCommitHistory.json"), dataBlockEntry.toString());
+ updateJSON("add", WALLET_DATA_PATH.concat("TransactionHistory.json"), dataBlockEntry.toString());
+ // writeToFile(DATUM_CHAIN_PATH + wholeTokensForData.getString(0) + ".json",
+ // "[test data]", false);
+
+ // Token receiver part starts here
+
+ // JSONObject tokenObject = new JSONObject(tokenDetails);
+ // JSONArray wholeTokens = tokenObject.getJSONArray("whole-tokens");
+ // JSONArray wholeTokenChains = tokenObject.getJSONArray("whole-tokenChains");
+ // JSONArray previousSendersArray = tokenObject.getJSONArray("previousSender");
+ // Double amount = tokenObject.getDouble("amount");
+
+ String hashString = tokens.concat(senderDidIpfsHash);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+ BufferedImage prvt = ImageIO.read(new File(DATA_PATH.concat(senderDidIpfsHash).concat("/PrivateShare.png")));
+ String firstPrivate = PropImage.img2bin(prvt);
+ int[] privateIntegerArray1 = strToIntArray(firstPrivate);
+ String privateBinary = Functions.intArrayToStr(privateIntegerArray1);
+ String positions = "";
+ for (int j = 0; j < privateIntegerArray1.length; j += 49152) {
+ positions += privateBinary.charAt(j);
+ }
+
+ String ownerIdentity = hashForPositions.concat(positions);
+ String ownerIdentityHash = calculateHash(ownerIdentity, "SHA3-256");
+
+ JSONObject commitChainObject = new JSONObject();
+ commitChainObject.put("sender", senderDidIpfsHash);
+ commitChainObject.put("senderSign", senderSign);
+ commitChainObject.put("comment", comment);
+ commitChainObject.put("tid", tid);
+ commitChainObject.put("blockHash", blockHash);
+ commitChainObject.put("owner", ownerIdentityHash);
+ commitChainObject.put("group", JSONObject.NULL);
+
+ JSONArray commitChainEntry = new JSONArray();
+ commitChainEntry.put(commitChainObject);
+
+ // writeToFile(DATUM_CHAIN_PATH + wholeTokensListForData.getString(0) + ".json",
+ // commitChainObject.toString(), true);
+ // writeToFile(TOKENCHAIN_PATH + wholeTokensListForData.getString(0) + ".json",
+ // commitChainObject.toString(), true);
+ updateJSON("add", DATUM_CHAIN_PATH + wholeTokensListForData.getString(0) + ".json",
+ commitChainEntry.toString());
+ updateJSON("add", TOKENCHAIN_PATH + wholeTokensListForData.getString(0) + ".json", commitChainEntry.toString());
+ add(TOKENS_PATH + wholeTokensListForData.getString(0), ipfs);
+ pin(wholeTokensListForData.getString(0), ipfs);
+ DataCommitterLogger.debug("IPFS Add & Pin completed");
+ // JSONObject amountLedger = tokenObject.getJSONObject("amountLedger");
+
+ DataCommitterLogger.info("Data Block Build Successful");
+ // executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ // output.close();
+ // input.close();
+ // senderSocket.close();
+
+ senderMutex = false;
+ return APIResponse;
+
+ }
+}
diff --git a/src/com/rubix/Datum/DataTokenPin.java b/src/com/rubix/Datum/DataTokenPin.java
new file mode 100644
index 00000000..1c6ef6e9
--- /dev/null
+++ b/src/com/rubix/Datum/DataTokenPin.java
@@ -0,0 +1,109 @@
+package com.rubix.Datum;
+
+import static com.rubix.Resources.Functions.DATA_PATH;
+import static com.rubix.Resources.Functions.IPFS_PORT;
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.getValues;
+import static com.rubix.Resources.Functions.initHash;
+import static com.rubix.Resources.Functions.nodeData;
+import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
+import static com.rubix.Resources.IPFSNetwork.forward;
+import static com.rubix.Resources.IPFSNetwork.repo;
+import static com.rubix.Resources.IPFSNetwork.swarmConnectP2P;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.net.Socket;
+import java.net.SocketException;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.rubix.Ping.PingCheck;
+
+import io.ipfs.api.IPFS;
+
+public class DataTokenPin {
+
+ private static final Logger DataTokenPinLogger = Logger.getLogger(DataTokenPin.class);
+ public static IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
+ public static BufferedReader serverInput;
+
+ private static int socketTimeOut = 120000;
+ public static String currentVersion = initHash();
+
+ public static JSONObject Ping(String peerID, int port, String cid) throws IOException, JSONException {
+ repo(ipfs);
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ port = port +1;
+ DataTokenPinLogger.debug("Incoming PID is "+peerID);
+
+ JSONObject APIResponse = new JSONObject();
+
+ String receiverWidIpfsHash = Dependency.getWIDfromPID(peerID, Dependency.widDataTableHashMap());
+ //getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid", peerID);
+ DataTokenPinLogger.debug("receiverWidIpfsHash "+receiverWidIpfsHash+" is of pid "+peerID);
+
+ //
+
+ String receiverDidIpfsHash = Dependency.getDIDfromPID(peerID, Dependency.dataTableHashMap());
+ //getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID);
+
+
+ if (!receiverWidIpfsHash.equals("")) {
+ nodeData(receiverDidIpfsHash, receiverWidIpfsHash, ipfs);
+ } else {
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Receiver WID null");
+ DataTokenPinLogger.warn("Receiver WID null");
+ return APIResponse;
+ }
+
+ DataTokenPinLogger.debug("Sender IPFS forwarding to DID: " + receiverDidIpfsHash + " PeerID: " + peerID);
+ String appName = peerID.concat("Ping");
+ forward(appName, port, peerID);
+ DataTokenPinLogger.debug("Forwarded to " + appName + " on " + port);
+ Socket senderSocket = new Socket("127.0.0.1", port);
+ senderSocket.setSoTimeout(socketTimeOut);
+ BufferedReader input = new BufferedReader(new InputStreamReader(senderSocket.getInputStream()));
+ PrintStream output = new PrintStream(senderSocket.getOutputStream());
+
+ output.println("CID-to-pin");
+ DataTokenPinLogger.debug("CID-to-pin request");
+
+ String pongResponse = cid;
+ /*
+ * try { pongResponse = input.readLine();
+ * DataTokenPinLogger.debug("Pong received is "+pongResponse); } catch
+ * (SocketException e) { DataTokenPinLogger.warn("Receiver " +
+ * receiverDidIpfsHash + " is unable to Respond! - Ping Check");
+ * executeIPFSCommands(" ipfs p2p close -t /p2p/" + peerID); output.close();
+ * input.close(); senderSocket.close(); APIResponse.put("status", "Failed");
+ * APIResponse.put("message", "Receiver " + receiverDidIpfsHash +
+ * "is unable to respond! - Sender Auth");
+ *
+ * return APIResponse; }
+ */
+ DataTokenPinLogger.debug("Sending CID to "+appName);
+ if (pongResponse != null) {
+ output.println(cid);
+
+ } else {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + peerID);
+ DataTokenPinLogger.info("Pong response not received");
+ DataTokenPinLogger.info("Pong response now received with error is "+pongResponse);
+ output.close();
+ input.close();
+ senderSocket.close();
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "cid response not received");
+
+ }
+ return APIResponse;
+ }
+
+}
diff --git a/src/com/rubix/Datum/Dependency.java b/src/com/rubix/Datum/Dependency.java
new file mode 100644
index 00000000..a731b972
--- /dev/null
+++ b/src/com/rubix/Datum/Dependency.java
@@ -0,0 +1,214 @@
+package com.rubix.Datum;
+
+import static com.rubix.Resources.Functions.DATA_PATH;
+import static com.rubix.Resources.Functions.DATUM_CHAIN_PATH;
+import static com.rubix.Resources.Functions.PAYMENTS_PATH;
+import static com.rubix.Resources.Functions.TOKENCHAIN_PATH;
+import static com.rubix.Resources.Functions.TOKENS_PATH;
+import static com.rubix.Resources.Functions.getOsName;
+import static com.rubix.Resources.Functions.getSystemUser;
+import static com.rubix.Resources.Functions.readFile;
+import static com.rubix.Resources.Functions.writeToFile;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map.Entry;
+
+import org.apache.log4j.Logger;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.rubix.Resources.Functions;
+
+public class Dependency {
+
+ private static final Logger DependencyLogger = Logger.getLogger(Dependency.class);
+ private static final Logger eventLogger = Logger.getLogger("eventLogger");
+
+ public static HashMap dataTableHashMap() {
+ String dataTableContent = Functions.readFile(
+ DATA_PATH + "DataTable.json");
+ JSONArray dataTableArray = null;
+ try {
+ dataTableArray = new JSONArray(dataTableContent);
+ } catch (JSONException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ HashMap dataTableMap = new HashMap();
+ for (int ctr = 0; ctr < dataTableArray.length(); ctr++) {
+ try {
+ dataTableMap.put(dataTableArray.getJSONObject(ctr).get("didHash").toString(),
+ dataTableArray.getJSONObject(ctr).get("peerid").toString());
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ return dataTableMap;
+
+ }
+
+ public static HashMap widDataTableHashMap() {
+ String dataTableContent = Functions.readFile(
+ DATA_PATH + "DataTable.json");
+ JSONArray dataTableArray = null;
+ try {
+ dataTableArray = new JSONArray(dataTableContent);
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ HashMap dataTableMap = new HashMap();
+ for (int ctr = 0; ctr < dataTableArray.length(); ctr++) {
+ try {
+ dataTableMap.put(dataTableArray.getJSONObject(ctr).get("peerid").toString(),
+ dataTableArray.getJSONObject(ctr).get("walletHash").toString());
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ return dataTableMap;
+
+ }
+
+ public static String getPIDfromDID(String did, HashMap dataTable) {
+ String pidString = "";
+ if (dataTable.containsKey(did)) {
+ pidString = dataTable.get(did);
+ } else {
+ pidString = "Not Found";
+ }
+ return pidString;
+ }
+
+ public static String getDIDfromPID(String pid, HashMap dataTable) {
+ String didString = "Not Found";
+ for (Entry entry : dataTable.entrySet()) {
+ if (entry.getValue().equals(pid)) {
+ didString = entry.getKey();
+ break;
+ }
+ }
+ return didString;
+ }
+
+ public static String getWIDfromPID(String pid, HashMap dataTable) {
+ String widString = "";
+ if (dataTable.containsKey(pid)) {
+ widString = dataTable.get(pid);
+ } else {
+ widString = "Not Found";
+ }
+ return widString;
+ }
+
+ public static void checkDatumPath() {
+ boolean status = false;
+ String configPath = "";
+ String OSName = getOsName();
+ if (OSName.contains("Windows")) {
+ configPath = ("C:\\Rubix\\");
+ } else if (OSName.contains("Mac")) {
+ configPath = "/Applications/Rubix/";
+ } else if (OSName.contains("Linux")) {
+ configPath = "/home/" + getSystemUser() + "/Rubix/";
+ }
+ String configContentString = readFile(configPath + "config.json");
+ JSONArray configContentArray = null;
+ try {
+ configContentArray = new JSONArray(configContentString);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+
+ if (!configContentString.contains("DATUM_CHAIN_PATH")) {
+ // DependencyLogger.debug("Datum chain path is being appended");
+ try {
+ configContentArray.getJSONObject(0).put("DATUM_CHAIN_PATH", configPath + "DATUM/");
+ } catch (JSONException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ writeToFile(configPath + "config.json", configContentArray.toString(), false);
+ }
+
+ configContentString = readFile(configPath + "config.json");
+ if (configContentString.contains("DATUM_CHAIN_PATH")) {
+ status = true;
+ // DependencyLogger.debug("DATUM_CHAIN_PATH is found in "+configPath);
+ }
+
+ }
+
+ public static String tokenToCommit() throws JSONException {
+ String tokenResult = "Insufficent token to commit data";
+ String bankFile = readFile(PAYMENTS_PATH.concat("BNK00.json"));
+ JSONArray bankArray = new JSONArray(bankFile);
+ DependencyLogger.debug("bank array length " + bankArray.length());
+ if (bankArray.length() < 1) {
+ tokenResult = "Insufficent Balance";
+ DependencyLogger.debug(tokenResult);
+ return tokenResult;
+ } else {
+ for (int i = 0; i < bankArray.length(); i++) {
+ DependencyLogger.debug(i + " is " + bankArray.getJSONObject(i).getString("tokenHash"));
+ File token = new File(TOKENS_PATH + bankArray.getJSONObject(i).getString("tokenHash"));
+ File tokenchain = new File(
+ TOKENCHAIN_PATH + bankArray.getJSONObject(i).getString("tokenHash") + ".json");
+ if (!(token.exists() && tokenchain.exists())) {
+ tokenResult = "Tokens Not Verified " + bankArray.getJSONObject(i).getString("tokenHash");
+ DependencyLogger.debug(tokenResult);
+ return tokenResult;
+ }
+
+ String tokenChainFileContent = readFile(
+ TOKENCHAIN_PATH + bankArray.getJSONObject(i).getString("tokenHash") + ".json");
+ JSONArray tokenChainFileArray = new JSONArray(tokenChainFileContent);
+ JSONObject lastObject = tokenChainFileArray.getJSONObject(tokenChainFileArray.length() - 1);
+
+ if (!lastObject.has("mineID")) {
+ tokenResult = bankArray.getJSONObject(i).getString("tokenHash");
+ DependencyLogger.debug(tokenResult);
+ return tokenResult;
+ }
+
+ }
+ }
+ return tokenResult;
+ }
+
+ public static void checkDatumFolder() throws IOException {
+ String datumFolderPath = DATUM_CHAIN_PATH;
+ if(datumFolderPath.length()>0) {
+ checkDatumPath();
+ }
+ datumFolderPath = DATUM_CHAIN_PATH;
+ File datumFolder = new File(datumFolderPath);
+ File datumCommitHistory = new File(datumFolderPath.concat("datumCommitHistory.json"));
+ File datumCommitToken = new File(PAYMENTS_PATH.concat("dataToken.json"));
+ File datumTokenFolder = new File(datumFolderPath + "DatumTokens/");
+
+
+ // File datumCommitChain =
+ if (!datumFolder.exists()) {
+ datumFolder.mkdir();
+ }
+ if (!datumTokenFolder.exists()) {
+ datumTokenFolder.mkdir();
+ }
+ if (!datumCommitHistory.exists()) {
+ datumCommitHistory.createNewFile();
+ writeToFile(datumCommitHistory.toString(), "[]", false);
+ }
+ if (!datumCommitToken.exists()) {
+ datumCommitToken.createNewFile();
+ writeToFile(datumCommitToken.toString(), "[]", false);
+ }
+
+ }
+
+}
diff --git a/src/com/rubix/Datum/TokenChain.java b/src/com/rubix/Datum/TokenChain.java
new file mode 100644
index 00000000..3a7eb114
--- /dev/null
+++ b/src/com/rubix/Datum/TokenChain.java
@@ -0,0 +1,59 @@
+package com.rubix.Datum;
+
+import java.util.List;
+
+public class TokenChain {
+ public String sender;
+ public String senderSign;
+ public String comment;
+ public String tid;
+ public String[] group;
+ public String owner;
+ public String blockHash;
+
+
+ public String getSender() {
+ return sender;
+ }
+ public void setSender(String sender) {
+ this.sender = sender;
+ }
+ public String getSenderSign() {
+ return senderSign;
+ }
+ public void setSenderSign(String senderSign) {
+ this.senderSign = senderSign;
+ }
+ public String getComment() {
+ return comment;
+ }
+ public void setComment(String comment) {
+ this.comment = comment;
+ }
+ public String getTid() {
+ return tid;
+ }
+ public void setTid(String tid) {
+ this.tid = tid;
+ }
+ public String[] getGroup() {
+ return group;
+ }
+ public void setGroup(String[] group) {
+ this.group = group;
+ }
+ public String getOwner() {
+ return owner;
+ }
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
+ public String getBlockHash() {
+ return blockHash;
+ }
+ public void setBlockHash(String blockHash) {
+ this.blockHash = blockHash;
+ }
+
+}
+
diff --git a/src/com/rubix/Mining/HashChain.java b/src/com/rubix/Mining/HashChain.java
new file mode 100644
index 00000000..3039ef6b
--- /dev/null
+++ b/src/com/rubix/Mining/HashChain.java
@@ -0,0 +1,145 @@
+package com.rubix.Mining;
+
+import com.rubix.Resources.Functions;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+import org.apache.log4j.Logger;
+
+public class HashChain {
+ static String hashChain = "";
+
+ public static Logger HashChainLogger = Logger.getLogger(HashChain.class);
+
+ public static String newHashChain(String tID, String[] DIDs, int rule) {
+ long totalTime = 0;
+ long currentTime = 0;
+ int ctr = 0;
+ java.util.Date date = new java.util.Date();
+ while(totalTime<3600000 || rule > tID.length()) {
+
+ // currentTime = hashChainCounter(tID, DIDs,rule+ctr);
+ totalTime = totalTime + currentTime;
+ //System.out.println("Counter is "+ctr+" Current time used is "+ totalTime);
+ ctr++;
+ currentTime = 0;
+
+
+ }
+ return hashChain;
+
+ }
+
+
+ public static String hashChainCounter(String tID, ArrayList DIDs, int rule) {
+ //String hashChain = "";
+ long start = 0;
+ long end = 0;
+ start = System.currentTimeMillis();
+ Random rand = new Random();
+ String DID = DIDs.get(rand.nextInt(3)) ;
+ //DIDs[rand.nextInt(3)];
+ //for (String DID : DIDs) {
+ int counter = 0;
+ String hash = DID;
+ while (!hash.endsWith(tID.substring(tID.length() - rule))) {
+ counter++;
+ hash = Functions.calculateHash(hash, "SHA3-256");
+ hashChain = hash;
+ }
+
+ // System.out.println(hash + " " + counter + " rule is "+ rule);
+ // }
+ end = System.currentTimeMillis();
+ // System.out.println("Current time "+(end - start));
+ return hashChain;
+ }
+
+ // using the TID and DIDs,
+
+ // fetch TID and DIDs as inputs. TID will hash itself until the last 3 char of
+ // TID matches with any of the DIDs in the list.
+
+ // public static String getTID(String TID, String DIDs) {
+
+ // token is the TID that
+
+ // public static String newHashChain(String TID, String[] DIDs) {
+ // String TIDHash = TID;
+
+ // while (!(TIDHash.substring(TIDHash.length() - 5), DIDs)) {
+ // TIDHash = Functions.calculateHash(TIDHash, "SHA3-256");
+ // }
+
+ // return TIDHash;
+ // }
+
+ // write a function which takes a string as TID and string array as DIDs. TID
+ // will hash itself until last 5 characters are same as any one of the DIDs.
+ // If not found, return null.
+
+ // write a function which takes a string as TID and string array as DIDs. The
+ // function should return the average iterations each DID took to rehash itself
+ // to match the last 3 char of TID
+ // public static String newHashChain(String TID, String[] DIDs, int matchRule) {
+ // int[] averageIterations = new int[DIDs.length];
+ // for (int i = 0; i < DIDs.length; i++) {
+ // averageIterations[i] = averageIterations(TID, DIDs[i], matchRule);
+ // }
+ // // get the average of values in the array
+ // int sum = 0;
+ // for (int i = 0; i < averageIterations.length; i++) {
+ // System.out.println("averageIterations[" + i + "] = " + averageIterations[i]);
+ // sum += averageIterations[i];
+ // }
+ // int average = sum / averageIterations.length;
+ // return Integer.toString(average);
+ // }
+
+ // private static int averageIterations(String TID, String DID, int matchRule) {
+ // int iterations = 0;
+ // String last3Chars = TID.substring(TID.length() - matchRule);
+ // String currentDID = DID;
+ // while (!currentDID.substring(currentDID.length() -
+ // matchRule).equals(last3Chars)) {
+ // currentDID = Functions.calculateHash(currentDID, "SHA3-256");
+ // iterations++;
+
+ // }
+ // System.out.println(currentDID + " " + iterations);
+
+ // return iterations;
+ // }
+
+ // public static String newHashChain(String TID, String[] DIDs) {
+ // String TIDHash = TID;
+
+ // while (!Functions.isSame(TIDHash.substring(TIDHash.length() - 5), DIDs)) {
+ // TIDHash = Functions.calculateHash(TIDHash, "SHA3-256");
+ // }
+
+ // return TIDHash;
+ // }
+
+ // public static String newHashChain(String tID, String[] DIDs, int matchRule) {
+ // String hashChain = "";
+ // int counter = 0;
+ // for (String DID : DIDs) {
+
+ // String hash = DID;
+ // while (!hash.endsWith(tID.substring(tID.length() - DIDs.length))) {
+ // // hashChain += hash;
+ // counter++;
+ // hash = Functions.calculateHash(hash, "SHA3-256");
+ // hashChain = hash;
+ // System.out.println(hash + " " + counter);
+ // }
+
+ // }
+
+ // return hashChain;
+
+ // }
+
+}
diff --git a/src/com/rubix/Mining/ProofCredits.java b/src/com/rubix/Mining/ProofCredits.java
index e4ce6292..b42118be 100644
--- a/src/com/rubix/Mining/ProofCredits.java
+++ b/src/com/rubix/Mining/ProofCredits.java
@@ -14,10 +14,13 @@
import static com.rubix.Resources.Functions.calculateHash;
import static com.rubix.Resources.Functions.deleteFile;
import static com.rubix.Resources.Functions.getQuorum;
-import static com.rubix.Resources.Functions.initHash;
+import static com.rubix.Resources.Functions.getValues;
import static com.rubix.Resources.Functions.minQuorum;
import static com.rubix.Resources.Functions.mineUpdate;
import static com.rubix.Resources.Functions.readFile;
+import static com.rubix.Resources.Functions.sanityCheck;
+import static com.rubix.Resources.Functions.sanityMessage;
+import static com.rubix.Resources.Functions.strToIntArray;
import static com.rubix.Resources.Functions.updateJSON;
import static com.rubix.Resources.Functions.updateQuorum;
import static com.rubix.Resources.Functions.writeToFile;
@@ -25,9 +28,11 @@
import static com.rubix.Resources.IPFSNetwork.pin;
import static com.rubix.Resources.IPFSNetwork.repo;
+import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
@@ -40,10 +45,14 @@
import java.util.Iterator;
import java.util.List;
+import javax.imageio.ImageIO;
import javax.net.ssl.HttpsURLConnection;
+import com.rubix.AuthenticateNode.PropImage;
import com.rubix.Consensus.InitiatorConsensus;
import com.rubix.Consensus.InitiatorProcedure;
+import com.rubix.Consensus.StakeConsensus;
+import com.rubix.Constants.MiningConstants;
import com.rubix.Resources.Functions;
import com.rubix.Resources.IPFSNetwork;
@@ -61,21 +70,37 @@ public class ProofCredits {
private static ArrayList betaPeersList;
private static ArrayList gammaPeersList;
private static int alphaSize = 0;
+ public static String hashChainProof = new String();
+ private static int hashChainRule = 7;
+
public static JSONObject create(String data, IPFS ipfs) throws IOException, JSONException {
+ StakeConsensus.STAKE_SUCCESS = 0;
+ StakeConsensus.STAKE_LOCKED = 0;
+ StakeConsensus.STAKE_FAILED = 0;
+ StakeConsensus.stakeDetails = new JSONArray();
+ InitiatorConsensus.signedAphaQuorumArray = new JSONArray();
+ InitiatorConsensus.quorumWithShares = new ArrayList<>();
+ InitiatorConsensus.finalQuorumSignsArray = new JSONArray();
+ InitiatorConsensus.quorumSignature = new JSONObject();
repo(ipfs);
JSONObject APIResponse = new JSONObject();
JSONObject detailsObject = new JSONObject(data);
- String receiverDidIpfsHash = detailsObject.getString("receiverDidIpfsHash");
+ String DID = detailsObject.getString("receiverDidIpfsHash");
String pvt = detailsObject.getString("pvt");
int type = detailsObject.getInt("type");
int creditUsed = 0;
long totalTime = 0;
+ int QSTHeight = 0;
JSONArray alphaQuorum = new JSONArray();
JSONArray betaQuorum = new JSONArray();
JSONArray gammaQuorum = new JSONArray();
+ JSONArray qstArray = new JSONArray();
+
+ boolean Status = false;
+
int creditsRequired = 50000, level;
long starttime = System.currentTimeMillis();
@@ -86,8 +111,19 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
JSONArray newQstArray = new JSONArray(readFile(WALLET_DATA_PATH + "QuorumSignedTransactions.json"));
int availableCredits = newQstArray.length();
+ File minedCH = new File(WALLET_DATA_PATH + "MinedCreditsHistory.json");
+ if (!minedCH.exists()) {
+ minedCH.createNewFile();
+ writeToFile(WALLET_DATA_PATH + "MinedCreditsHistory.json", "[]", false);
+ }
+ String creditsHistory = readFile(WALLET_DATA_PATH + "MinedCreditsHistory.json");
+ JSONArray creditsHistoryArray = new JSONArray(creditsHistory);
+
+ // ! get actual qst height for current scenario
+ QSTHeight = availableCredits + creditsHistoryArray.length();
+
ProofCreditsLogger.debug("Credits available: " + availableCredits);
- String GET_URL_credit = SYNC_IP + "/getlevel";
+ String GET_URL_credit = SYNC_IP + "/getCurrentLevel";
URL URLobj_credit = new URL(GET_URL_credit);
HttpURLConnection con_credit = (HttpURLConnection) URLobj_credit.openConnection();
con_credit.setRequestMethod("GET");
@@ -101,7 +137,8 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
response_credit.append(inputLine_credit);
}
in_credit.close();
- ProofCreditsLogger.debug("response from service " + response_credit.toString());
+ // ProofCreditsLogger.debug("response from service " +
+ // response_credit.toString());
resJsonData_credit = new JSONObject(response_credit.toString());
int level_credit = resJsonData_credit.getInt("level");
creditsRequired = (int) Math.pow(2, (2 + level_credit));
@@ -121,8 +158,27 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
ProofCreditsLogger.debug("Credits Old: " + oldCreditsFlag);
+ JSONArray quorumArray;
+ switch (type) {
+ case 2: {
+ quorumArray = new JSONArray(readFile(DATA_PATH + "quorumlist.json"));
+ break;
+ }
+ default: {
+ quorumArray = getQuorum(DID, DID, 1);
+ if (quorumArray.length() == 1) {
+ APIResponse.put("did", DID);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Type 1 mining not allowed. Please use subnet quorum to mine.");
+ ProofCreditsLogger.warn("Quorum Members not available for type 1 mining");
+ return APIResponse;
+ }
+ }
+ }
+
// String GET_URL = SYNC_IP+"/getInfo?count="+availableCredits;
- String GET_URL = SYNC_IP + "/minetoken";
+ String GET_URL = SYNC_IP + "/getTokenToMine";
URL URLobj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) URLobj.openConnection();
con.setRequestMethod("GET");
@@ -136,7 +192,7 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
response.append(inputLine);
}
in.close();
- ProofCreditsLogger.debug("response from service " + response.toString());
+ // ProofCreditsLogger.debug("response from service " + response.toString());
resJsonData = new JSONArray(response.toString());
} else
@@ -150,6 +206,11 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
level = resJsonData.getJSONObject(0).getInt("level");
for (int i = 0; i < resJsonData.length(); i++) {
+ ProofCreditsLogger
+ .debug("*************level**********" + resJsonData.getJSONObject(i).getInt("level"));
+ ProofCreditsLogger
+ .debug("*************token**********" + resJsonData.getJSONObject(i).getInt("token"));
+
token.put(Functions.mineToken(resJsonData.getJSONObject(i).getInt("level"),
resJsonData.getJSONObject(i).getInt("token")));
@@ -163,29 +224,66 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
JSONArray prooftid = new JSONArray();
String comments = resJsonData.toString() + prooftid;
- String authSenderByRecHash = calculateHash(token + receiverDidIpfsHash + comments, "SHA3-256");
+ String authSenderByRecHash = calculateHash(token + DID + comments, "SHA3-256");
String tid = calculateHash(authSenderByRecHash, "SHA3-256");
- writeToFile(LOGGER_PATH + "tempbeta", tid.concat(receiverDidIpfsHash), false);
- String betaHash = IPFSNetwork.add(LOGGER_PATH + "tempbeta", ipfs);
- deleteFile(LOGGER_PATH + "tempbeta");
-
- writeToFile(LOGGER_PATH + "tempgamma", tid.concat(receiverDidIpfsHash), false);
- String gammaHash = IPFSNetwork.add(LOGGER_PATH + "tempgamma", ipfs);
- deleteFile(LOGGER_PATH + "tempgamma");
-
- JSONArray quorumArray;
- switch (type) {
- case 2: {
- quorumArray = new JSONArray(readFile(DATA_PATH + "quorumlist.json"));
- break;
- }
- default: {
- quorumArray = getQuorum(betaHash, gammaHash, receiverDidIpfsHash, receiverDidIpfsHash,
- token.length());
+ // writeToFile(LOGGER_PATH + "tempbeta", tid.concat(DID), false);
+ // String betaHash = IPFSNetwork.add(LOGGER_PATH + "tempbeta", ipfs);
+ // deleteFile(LOGGER_PATH + "tempbeta");
+
+ // writeToFile(LOGGER_PATH + "tempgamma", tid.concat(DID), false);
+ // String gammaHash = IPFSNetwork.add(LOGGER_PATH + "tempgamma", ipfs);
+ // deleteFile(LOGGER_PATH + "tempgamma");
+
+
+
+ ProofCreditsLogger.debug("DID is "+DID);
+ ProofCreditsLogger.debug("Quorums list is "+quorumArray.toString());
+ if(quorumArray.toString().contains(DID)) {
+ JSONArray updatedQuourmList = new JSONArray();
+ ProofCreditsLogger.debug("Miner DID "+ DID + "is found in quourmArray");
+ for(int i=0;i= 7 && i <= 13)
+ betaCheck++;
+ if (i >= 14 && i <= 20)
+ gammaCheck++;
}
}
+ if (alphaCheck > 2 || betaCheck > 2 || gammaCheck > 2) {
+ APIResponse.put("did", DID);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ String message = "Quorum: ".concat(sanityFailedQuorum.toString()).concat(" ");
+ APIResponse.put("message", message.concat(sanityMessage));
+ ProofCreditsLogger.warn("Quorum: ".concat(message.concat(sanityMessage)));
+ return APIResponse;
+ }
+ //Sanity Check - Ends
+
QuorumSwarmConnect(quorumArray, ipfs);
alphaSize = quorumArray.length() - 14;
@@ -211,7 +309,7 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
if (alphaPeersList.size() < minQuorum(alphaSize) || betaPeersList.size() < 5
|| gammaPeersList.size() < 5) {
updateQuorum(quorumArray, null, false, type);
- APIResponse.put("did", receiverDidIpfsHash);
+ APIResponse.put("did", DID);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
APIResponse.put("message", "Quorum Members not available");
@@ -220,11 +318,12 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
}
JSONArray signedQuorumList = new JSONArray();
- if (!oldCreditsFlag) {
+ boolean creditRemoveStatus = false;
+ if (!oldCreditsFlag) {
ProofCreditsLogger.debug("New Credits");
// Send QST for verification
String qstContent = readFile(WALLET_DATA_PATH.concat("QuorumSignedTransactions.json"));
- JSONArray qstArray = new JSONArray(qstContent);
+ qstArray = new JSONArray(qstContent);
int count = 0;
JSONArray creditSignsArray = new JSONArray();
@@ -254,7 +353,7 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
qstObject.put("credits", creditSignsArray);
} else {
updateQuorum(quorumArray, null, false, type);
- APIResponse.put("did", receiverDidIpfsHash);
+ APIResponse.put("did", DID);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
APIResponse.put("message", "Credit File(s) missing");
@@ -265,25 +364,25 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
JSONObject dataObject = new JSONObject();
dataObject.put("tid", tid);
dataObject.put("message", comments);
- dataObject.put("receiverDidIpfs", receiverDidIpfsHash);
+ dataObject.put("receiverDidIpfs", DID);
dataObject.put("pvt", pvt);
- dataObject.put("senderDidIpfs", receiverDidIpfsHash);
+ dataObject.put("senderDidIpfs", DID);
dataObject.put("token", token.toString());
dataObject.put("alphaList", alphaPeersList);
dataObject.put("betaList", betaPeersList);
dataObject.put("gammaList", gammaPeersList);
- dataObject.put("initHash", initHash());
dataObject.put("qstDetails", qstObject);
InitiatorProcedure.consensusSetUp(dataObject.toString(), ipfs, SEND_PORT + 3, alphaSize,
"new-credits-mining");
if (!(InitiatorConsensus.quorumSignature.length() >= 3 * minQuorum(7))) {
- APIResponse.put("did", receiverDidIpfsHash);
+ APIResponse.put("did", DID);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
APIResponse.put("message", "Consensus failed");
ProofCreditsLogger.debug("consensus failed");
+ return APIResponse;
} else {
ProofCreditsLogger.debug("token mined " + token);
@@ -297,9 +396,7 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
}
}
- for (int i = 0; i < creditUsed; i++)
- deleteFile(WALLET_DATA_PATH.concat("/Credits/")
- .concat(qstArray.getJSONObject(i).getString("credits")).concat(".json"));
+ creditRemoveStatus = true;
}
} else {
@@ -308,9 +405,9 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
JSONObject dataObject = new JSONObject();
dataObject.put("tid", tid);
dataObject.put("message", comments);
- dataObject.put("receiverDidIpfs", receiverDidIpfsHash);
+ dataObject.put("receiverDidIpfs", DID);
dataObject.put("pvt", pvt);
- dataObject.put("senderDidIpfs", receiverDidIpfsHash);
+ dataObject.put("senderDidIpfs", DID);
dataObject.put("token", token.toString());
dataObject.put("alphaList", alphaPeersList);
dataObject.put("betaList", betaPeersList);
@@ -319,7 +416,7 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
InitiatorProcedure.consensusSetUp(dataObject.toString(), ipfs, SEND_PORT + 3, alphaSize, "");
if (!(InitiatorConsensus.quorumSignature.length() >= 3 * minQuorum(7))) {
- APIResponse.put("did", receiverDidIpfsHash);
+ APIResponse.put("did", DID);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
APIResponse.put("message", "Consensus failed");
@@ -338,27 +435,150 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
}
}
}
+ // JSONObject stakingData = new JSONObject();
+ // String tkHash = null;
for (int i = 0; i < token.length(); i++) {
+
writeToFile(LOGGER_PATH + "tempToken", token.getString(i), false);
String tokenHash = IPFSNetwork.add(LOGGER_PATH + "tempToken", ipfs);
- writeToFile(TOKENS_PATH + tokenHash, token.getString(i), false);
- deleteFile(LOGGER_PATH + "tempToken");
- writeToFile(TOKENCHAIN_PATH + tokenHash + ".json", "[]", false);
- JSONObject temp = new JSONObject();
- temp.put("tokenHash", tokenHash);
- JSONArray tempArray = new JSONArray();
- tempArray.put(temp);
- updateJSON("add", PAYMENTS_PATH + "BNK00.json", tempArray.toString());
+
+ FileWriter shareWriter = new FileWriter(new File(LOGGER_PATH + "mycredit.txt"), true);
+ shareWriter.write(InitiatorConsensus.quorumSignature.toString());
+ shareWriter.close();
+ File readCredit = new File(LOGGER_PATH + "mycredit.txt");
+ String credit = add(readCredit.toString(), ipfs);
+
+ File creditFile = new File(WALLET_DATA_PATH.concat("/Credits/").concat(credit).concat(".json"));
+ if (!creditFile.exists())
+ creditFile.createNewFile();
+ writeToFile(creditFile.toString(), InitiatorConsensus.quorumSignature.toString(), false);
+
+ String tokens = tokenHash;
+ String hashString = tokens.concat(DID);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+
+ BufferedImage privateImage = ImageIO
+ .read(new File(DATA_PATH.concat(DID).concat("/PrivateShare.png")));
+ String firstPrivate = PropImage.img2bin(privateImage);
+ int[] privateIntegerArray1 = strToIntArray(firstPrivate);
+ String privateBinary = Functions.intArrayToStr(privateIntegerArray1);
+ String positions = "";
+
+ for (int j = 0; j < privateIntegerArray1.length; j += 49152) {
+ positions += privateBinary.charAt(j);
+ }
+ String ownerIdentity = hashForPositions.concat(positions);
+ String ownerIdentityHash = calculateHash(ownerIdentity, "SHA3-256");
+
+ ProofCreditsLogger.debug("Ownership Here");
+ ProofCreditsLogger.debug("tokens: " + tokenHash);
+ ProofCreditsLogger.debug("hashString: " + hashString);
+ ProofCreditsLogger.debug("hashForPositions: " + hashForPositions);
+ ProofCreditsLogger.debug("p1: " + positions);
+ ProofCreditsLogger.debug("QSTHeight: " + QSTHeight);
+ ProofCreditsLogger.debug("ownerIdentity: " + ownerIdentity);
+ ProofCreditsLogger.debug("ownerIdentityHash: " + ownerIdentityHash);
+ JSONArray tokenChainArray = new JSONArray();
+ JSONObject tokenChainGenesisObject = new JSONObject();
+ tokenChainGenesisObject.put("quorumSignatures", credit);
+ tokenChainGenesisObject.put("quorumSignContent", InitiatorConsensus.quorumSignature);
+ tokenChainGenesisObject.put("sender", DID);
+ tokenChainGenesisObject.put("comment", "Mining-Genesis");
+ tokenChainGenesisObject.put("tid", tid);
+ tokenChainGenesisObject.put("owner", ownerIdentityHash);
+ tokenChainGenesisObject.put("blockNumber", 0);
+ tokenChainGenesisObject.put("QSTHeight", QSTHeight);
+ tokenChainGenesisObject.put("tokenHash", tokenHash);
+ tokenChainGenesisObject.put("tokenContent", token.getString(i));
+
+ tokenChainGenesisObject.put("nextHash", calculateHash(tid, "SHA3-256"));
+ tokenChainGenesisObject.put("previousHash", "");
+ // stakingData = tokenChainGenesisObject;
+ // tkHash = tokenHash;
+
+ //tokenChainArray.put(tokenChainGenesisObject);
+ // ! new token will now need a staked token
+
+ /*
+ * do { } while (StakeConsensus.stakeDetails.length() < 8 ||
+ * StakeConsensus.STAKE_FAILED == 5);
+ */
+
+ Thread stakingThread = new Thread(() -> {
+ try {
+ StakeConsensus.getStakeConsensus(InitiatorConsensus.signedAphaQuorumArray,
+ tokenChainGenesisObject, ipfs, SEND_PORT + 3,
+ "alpha-stake-token");
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ });
+ stakingThread.start();
+
+ while ((StakeConsensus.STAKE_SUCCESS < 3 && StakeConsensus.STAKE_FAILED < 3)) {
+
+ }
+ ProofCreditsLogger.debug("Stake consensus stake details is "+ StakeConsensus.stakeDetails.toString());
+ if (StakeConsensus.STAKE_SUCCESS == 3 && StakeConsensus.stakeDetails.length() > 0) {
+ tokenChainGenesisObject.put(MiningConstants.MINE_ID, StakeConsensus.stakeDetails);
+ tokenChainArray.put(tokenChainGenesisObject);
+
+ ProofCreditsLogger.debug("Stake Details for new mined token: " + StakeConsensus.stakeDetails);
+ ProofCreditsLogger.debug("Staked quorums are "+ StakeConsensus.stakedDIDs);
+ ProofCreditsLogger.debug("-----------------------------------------------");
+ ProofCreditsLogger.debug("Staked quorums are :");
+ for (String ele : StakeConsensus.stakedDIDs) {
+ ProofCreditsLogger.debug(ele+" ");
+ }
+ ProofCreditsLogger.debug("-----------------------------------------------");
+ writeToFile(TOKENS_PATH + tokenHash, token.getString(i), false);
+ ProofCreditsLogger.warn(" TOKENHASH " + tokenHash);
+ ProofCreditsLogger.warn(" TOKENS " + token.getString(i));
+ deleteFile(LOGGER_PATH + "tempToken");
+ writeToFile(TOKENCHAIN_PATH + tokenHash + ".json", tokenChainArray.toString(), false);
+ JSONObject temp = new JSONObject();
+ temp.put("tokenHash", tokenHash);
+ JSONArray tempArray = new JSONArray();
+ tempArray.put(temp);
+ hashChainProof = HashChain.hashChainCounter(tid, StakeConsensus.stakedDIDs, hashChainRule);
+ ProofCreditsLogger.debug("HashChainProof is "+hashChainProof+" transcation id is "+ tid);
+ updateJSON("add", PAYMENTS_PATH + "BNK00.json", tempArray.toString());
+ } else {
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", DID);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message",
+ "Staking failed.. Retry with alpha quorum members with aleast 1 RBT balance");
+ ProofCreditsLogger.warn("Staking failed");
+ return APIResponse;
+ }
+
+
}
- File minedCH = new File(WALLET_DATA_PATH + "MinedCreditsHistory.json");
- if (!minedCH.exists()) {
- minedCH.createNewFile();
- writeToFile(WALLET_DATA_PATH + "MinedCreditsHistory.json", "[]", false);
+ // updateJSON("add", TOKENCHAIN_PATH + tkHash + ".json", tempArray.toString());
+
+ if (!(InitiatorConsensus.quorumSignature.length() >= 3 * minQuorum(7))) {
+ APIResponse.put("did", DID);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Consensus failed");
+ ProofCreditsLogger.debug("consensus failed");
+ } else {
+ ProofCreditsLogger.debug("token mined " + token);
+
+ int counter = 0;
+
+ for (int i = 0; i < availableCredits; i++) {
+ JSONObject temp = newQstArray.getJSONObject(i);
+ if (counter < creditUsed) {
+ prooftid.put(temp.getString("tid"));
+ counter++;
+ }
+ }
}
- String creditsHistory = readFile(WALLET_DATA_PATH + "MinedCreditsHistory.json");
- JSONArray creditsHistoryArray = new JSONArray(creditsHistory);
for (int i = 0; i < creditUsed; i++) {
JSONObject minedObject = new JSONObject();
minedObject.put("tid", newQstArray.getJSONObject(i).getString("tid"));
@@ -383,8 +603,8 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
signedQuorumList.put(keys.next());
updateQuorum(quorumArray, signedQuorumList, true, type);
- mineUpdate(receiverDidIpfsHash, creditUsed);
- APIResponse.put("did", receiverDidIpfsHash);
+ mineUpdate(DID, creditUsed);
+ APIResponse.put("did", DID);
APIResponse.put("tid", tid);
APIResponse.put("token", token);
APIResponse.put("creditsused", creditUsed);
@@ -401,11 +621,17 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
transactionRecord.put("tokens", token);
transactionRecord.put("txn", tid);
transactionRecord.put("quorumList", signedQuorumList);
- transactionRecord.put("senderDID", receiverDidIpfsHash);
- transactionRecord.put("receiverDID", receiverDidIpfsHash);
+ transactionRecord.put("senderDID", DID);
+ transactionRecord.put("receiverDID", DID);
transactionRecord.put("Date", currentTime);
transactionRecord.put("totalTime", totalTime);
transactionRecord.put("comment", "minedtxn");
+
+ if(creditRemoveStatus == true) {
+ for (int i = 0; i < creditUsed; i++)
+ deleteFile(WALLET_DATA_PATH.concat("/Credits/")
+ .concat(qstArray.getJSONObject(i).getString("credits")).concat(".json"));
+ }
JSONArray transactionHistoryEntry = new JSONArray();
transactionHistoryEntry.put(transactionRecord);
@@ -427,7 +653,7 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
// Serialization
JSONObject dataToSend = new JSONObject();
dataToSend.put("bank_id", "01");
- dataToSend.put("user_did", receiverDidIpfsHash);
+ dataToSend.put("user_did", DID);
dataToSend.put("token_id", token);
dataToSend.put("level", level);
dataToSend.put("denomination", 1);
@@ -478,8 +704,8 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
// Serialization
JSONObject dataToSend = new JSONObject();
dataToSend.put("transaction_id", tid);
- dataToSend.put("sender_did", receiverDidIpfsHash);
- dataToSend.put("receiver_did", receiverDidIpfsHash);
+ dataToSend.put("sender_did", DID);
+ dataToSend.put("receiver_did", DID);
dataToSend.put("token_id", tokenList);
dataToSend.put("token_time", (int) totalTime);
dataToSend.put("amount", tokenList.size());
@@ -514,7 +740,7 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
ProofCreditsLogger.debug(responseTxn.toString());
}
} else {
- APIResponse.put("did", receiverDidIpfsHash);
+ APIResponse.put("did", DID);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
APIResponse.put("message", "error from mine service");
@@ -522,7 +748,7 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
return APIResponse;
}
} else {
- APIResponse.put("did", receiverDidIpfsHash);
+ APIResponse.put("did", DID);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
APIResponse.put("message", "Insufficient proofs");
@@ -533,3 +759,5 @@ public static JSONObject create(String data, IPFS ipfs) throws IOException, JSON
return APIResponse;
}
}
+
+//stakedDIDs
diff --git a/src/com/rubix/Ping/PingCheck.java b/src/com/rubix/Ping/PingCheck.java
new file mode 100644
index 00000000..146cbbff
--- /dev/null
+++ b/src/com/rubix/Ping/PingCheck.java
@@ -0,0 +1,119 @@
+package com.rubix.Ping;
+
+import static com.rubix.Resources.Functions.*;
+import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
+import static com.rubix.Resources.IPFSNetwork.forward;
+import static com.rubix.Resources.IPFSNetwork.repo;
+import static com.rubix.Resources.IPFSNetwork.swarmConnectP2P;
+
+import io.ipfs.api.IPFS;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.net.Socket;
+import java.net.SocketException;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+
+
+public class PingCheck {
+ private static final Logger PingSenderLogger = Logger.getLogger(PingCheck.class);
+ public static IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
+ public static BufferedReader serverInput;
+
+ private static int socketTimeOut = 120000;
+ public static String currentVersion = initHash();
+ public static JSONObject Ping(String peerID, int port) throws IOException, JSONException {
+ repo(ipfs);
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ PingSenderLogger.debug("Current version in PingSender is "+currentVersion);
+
+ JSONObject APIResponse = new JSONObject();
+ if (!peerID.equals("")) {
+ PingSenderLogger.debug("Swarm connecting to " + peerID);
+ swarmConnectP2P(peerID, ipfs);
+ PingSenderLogger.debug("Swarm connected");
+ } else {
+ APIResponse.put("message", "Receiver Peer ID null");
+ PingSenderLogger.warn("Receiver Peer ID null");
+ return APIResponse;
+ }
+
+ String receiverWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid", peerID);
+ String receiverDidIpfsHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID);
+ if (!receiverWidIpfsHash.equals("")) {
+ nodeData(receiverDidIpfsHash, receiverWidIpfsHash, ipfs);
+ } else {
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Receiver WID null");
+ PingSenderLogger.warn("Receiver WID null");
+ return APIResponse;
+ }
+
+ PingSenderLogger.debug("Sender IPFS forwarding to DID: " + receiverDidIpfsHash + " PeerID: " + peerID);
+ String appName = peerID.concat("Ping");
+ forward(appName, port, peerID);
+ PingSenderLogger.debug("Forwarded to " + appName + " on " + port);
+ Socket senderSocket = new Socket("127.0.0.1", port);
+ senderSocket.setSoTimeout(socketTimeOut);
+ BufferedReader input = new BufferedReader(new InputStreamReader(senderSocket.getInputStream()));
+ PrintStream output = new PrintStream(senderSocket.getOutputStream());
+
+ output.println("PingCheck");
+ PingSenderLogger.debug("Sent PingCheck request");
+
+ String pongResponse;
+ try {
+ pongResponse = input.readLine();
+ PingSenderLogger.debug("Pong received is "+pongResponse);
+ } catch (SocketException e) {
+ PingSenderLogger.warn("Receiver " + receiverDidIpfsHash + " is unable to Respond! - Ping Check");
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + peerID);
+ output.close();
+ input.close();
+ senderSocket.close();
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Receiver " + receiverDidIpfsHash + "is unable to respond! - Sender Auth");
+
+ return APIResponse;
+ }
+ PingSenderLogger.debug("Pong response after pong received "+pongResponse);
+ if (pongResponse != null) {
+ PingSenderLogger.debug("Pong response inside pongRespong not null "+pongResponse);
+ if(pongResponse.contains(currentVersion)) {
+ PingSenderLogger.debug("Pong response received is "+ pongResponse);
+ PingSenderLogger.info("Ping Successful");
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + peerID);
+ output.close();
+ input.close();
+ senderSocket.close();
+ APIResponse.put("status", "Success");
+ APIResponse.put("message", "Ping Check Success");
+ }else {
+ PingSenderLogger.debug("Receiver is running "+ pongResponse);
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + peerID);
+ output.close();
+ input.close();
+ senderSocket.close();
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", receiverDidIpfsHash)+ " not running new version");
+ }
+
+ } else {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + peerID);
+ PingSenderLogger.info("Pong response not received");
+ PingSenderLogger.info("Pong response now received with error is "+pongResponse);
+ output.close();
+ input.close();
+ senderSocket.close();
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Pong response not received");
+
+ }
+ return APIResponse;
+ }
+}
diff --git a/src/com/rubix/Ping/PingReceive.java b/src/com/rubix/Ping/PingReceive.java
new file mode 100644
index 00000000..0e1e76e8
--- /dev/null
+++ b/src/com/rubix/Ping/PingReceive.java
@@ -0,0 +1,178 @@
+package com.rubix.Ping;
+
+import static com.rubix.Resources.Functions.*;
+import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
+import static com.rubix.Resources.IPFSNetwork.listen;
+import static com.rubix.Resources.IPFSNetwork.repo;
+
+import java.io.*;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.rubix.Resources.IPFSNetwork;
+
+import io.ipfs.api.IPFS;
+
+public class PingReceive {
+ public static Logger PingReceiverLogger = Logger.getLogger(PingReceive.class);
+
+ private static final JSONObject APIResponse = new JSONObject();
+ private static final IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
+ public static String currentVersion = initHash();
+
+ /**
+ * Receiver Node: To receive a valid token from an authentic sender
+ *
+ * @return Transaction Details (JSONObject)
+ * @throws IOException handles IO Exceptions
+ * @throws JSONException handles JSON Exceptions
+ */
+ public static String receive(String userType,int port) throws JSONException {
+ pathSet();
+ ServerSocket ss;
+ Socket sk;
+
+ try {
+ repo(ipfs);
+
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+
+ String receiverPeerID = getPeerID(DATA_PATH + "DID.json");
+ String appName = receiverPeerID.concat("Ping");
+ listen(appName, port);
+ ss = new ServerSocket(port);
+ PingReceiverLogger.debug("Ping "+userType+" Listening on " + port + " appname " + appName);
+
+ sk = ss.accept();
+ PingReceiverLogger.debug("Data Incoming...");
+ BufferedReader input = new BufferedReader(new InputStreamReader(sk.getInputStream()));
+ PrintStream output = new PrintStream(sk.getOutputStream());
+
+ int height = 0;
+ String pingRequest;
+ try {
+ pingRequest = input.readLine();
+ } catch (SocketException e) {
+ PingReceiverLogger.warn("Sender Stream Null - PingCheck");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - PingCheck");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ PingReceiverLogger.debug("Ping Request Received: " + pingRequest+" in port "+port);
+ if (pingRequest != null) {
+ if(pingRequest.contains("PingCheck")) {
+ PingReceiverLogger.debug(userType + " Sending version "+currentVersion);
+ output.println(currentVersion);
+ PingReceiverLogger.debug(userType + " Sent version to pingSender"+currentVersion);
+ APIResponse.put("status", "Success");
+ APIResponse.put("message", "Pong Sent to Sender with Check Sum");
+ PingReceiverLogger.info(userType + " Pong Sent "+currentVersion);
+
+ }
+ else
+ if(pingRequest.contains("Get-TokenChain-Height")) {
+ String tokenHash;
+ try {
+ tokenHash = input.readLine();
+ } catch (SocketException e) {
+ PingReceiverLogger.warn("Sender Stream Null - tokenHash");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - tokenHash");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ if (tokenHash != null && tokenHash.startsWith("Qm") && tokenHash.length() == 46) {
+ PingReceiverLogger.info("Token chain height requested for: " + tokenHash);
+ File tokenChainFile = new File(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"));
+ if(!tokenChainFile.exists()) {
+ PingReceiverLogger.info("Token chain file not found");
+ height = 0;
+ }
+ else{
+ String tokenChain = readFile(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"));
+ JSONArray chainArray = new JSONArray(tokenChain);
+ height = chainArray.length()-1;
+ PingReceiverLogger.info("Chain height: " + height);
+ }
+ }
+
+ }
+ if (pingRequest!=null && pingRequest.contains("CID-to-pin")) {
+ PingReceiverLogger.debug("received cid to pin");
+ String cIDString = null;
+ try {
+ cIDString = input.readLine();
+ PingReceiverLogger.debug("CID to be pinned is "+ cIDString);
+ }
+ catch (SocketException e) {
+ PingReceiverLogger.warn("Sender Stream Null - tokenHash");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - tokenHash");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+
+ if (cIDString != null && cIDString.startsWith("Qm") && cIDString.length() == 46) {
+ PingReceiverLogger.debug("CID is "+ cIDString);
+ IPFSNetwork.pin(cIDString, ipfs);
+ }
+
+ }
+ else{
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Request Failed");
+ PingReceiverLogger.info(userType+ " Request Failed");
+ }
+
+ APIResponse.put("status", "Success");
+ APIResponse.put("message", "Pong Sent");
+ PingReceiverLogger.info(userType + " Pong Sent");
+
+ }
+ else {
+ PingReceiverLogger.info("Pong Not Sent ");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Pong Failed");
+ PingReceiverLogger.info(userType + " Pong Failed");
+ }
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + pingRequest);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return APIResponse.toString();
+ }
+}
diff --git a/src/com/rubix/Ping/QuorumPingReceive.java b/src/com/rubix/Ping/QuorumPingReceive.java
new file mode 100644
index 00000000..20f37f4f
--- /dev/null
+++ b/src/com/rubix/Ping/QuorumPingReceive.java
@@ -0,0 +1,173 @@
+package com.rubix.Ping;
+
+import static com.rubix.Resources.Functions.*;
+import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
+import static com.rubix.Resources.IPFSNetwork.listen;
+import static com.rubix.Resources.IPFSNetwork.repo;
+
+import java.io.*;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.rubix.Resources.IPFSNetwork;
+
+import io.ipfs.api.IPFS;
+
+public class QuorumPingReceive {
+ public static Logger QuorumPingReceiverLogger = Logger.getLogger(QuorumPingReceive.class);
+
+ private static final JSONObject APIResponse = new JSONObject();
+ private static final IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
+ public static String currentVersion = initHash();
+
+ /**
+ * Receiver Node: To receive a valid token from an authentic sender
+ *
+ * @return Transaction Details (JSONObject)
+ * @throws IOException handles IO Exceptions
+ * @throws JSONException handles JSON Exceptions
+ */
+ public static String receive(int port) throws JSONException {
+ pathSet();
+ ServerSocket ss;
+ Socket sk;
+
+ try {
+ repo(ipfs);
+
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+
+ String receiverPeerID = getPeerID(DATA_PATH + "DID.json");
+ String appName = receiverPeerID.concat("Ping");
+ listen(appName, port);
+ ss = new ServerSocket(port);
+ QuorumPingReceiverLogger.debug("Ping Quorum Listening on " + port + " appname " + appName);
+
+ sk = ss.accept();
+ QuorumPingReceiverLogger.debug("Data Incoming...");
+ BufferedReader input = new BufferedReader(new InputStreamReader(sk.getInputStream()));
+ PrintStream output = new PrintStream(sk.getOutputStream());
+
+ int height = 0;
+ String pingRequest;
+ try {
+ pingRequest = input.readLine();
+ } catch (SocketException e) {
+ QuorumPingReceiverLogger.warn("Sender Stream Null - PingCheck");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - PingCheck");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ QuorumPingReceiverLogger.debug("Ping Request Received: " + pingRequest);
+ if (pingRequest != null && pingRequest.contains("PingCheck")) {
+ output.println("Pong "+currentVersion);
+
+ APIResponse.put("status", "Success");
+ APIResponse.put("message", "Pong Sent");
+ QuorumPingReceiverLogger.info("Pong Sent");
+
+ }
+ else if (pingRequest != null && pingRequest.contains("Get-TokenChain-Height")) {
+ String tokenHash;
+ try {
+ tokenHash = input.readLine();
+ } catch (SocketException e) {
+ QuorumPingReceiverLogger.warn("Sender Stream Null - tokenHash");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - tokenHash");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ if (tokenHash != null && tokenHash.startsWith("Qm") && tokenHash.length() == 46) {
+ QuorumPingReceiverLogger.info("Token chain height requested for: " + tokenHash);
+ File tokenChainFile = new File(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"));
+ if(!tokenChainFile.exists()) {
+ QuorumPingReceiverLogger.info("Token chain file not found");
+ height = 0;
+ }
+ else{
+ String tokenChain = readFile(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"));
+ JSONArray chainArray = new JSONArray(tokenChain);
+ height = chainArray.length()-1;
+ QuorumPingReceiverLogger.info("Chain height: " + height);
+ }
+ }
+ else{
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Request Failed");
+ QuorumPingReceiverLogger.info("Request Failed");
+ }
+ output.println(height);
+
+ APIResponse.put("status", "Success");
+ APIResponse.put("message", "Pong Sent");
+ QuorumPingReceiverLogger.info("Pong Sent");
+
+ }
+ else
+ if (pingRequest!=null && pingRequest.contains("CID-to-pin")) {
+ QuorumPingReceiverLogger.debug("received cid to pin");
+ String cIDString = null;
+ try {
+ cIDString = input.readLine();
+ QuorumPingReceiverLogger.debug("CID to be pinned is "+ cIDString);
+ }
+ catch (SocketException e) {
+ QuorumPingReceiverLogger.warn("Sender Stream Null - tokenHash");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - tokenHash");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+
+ if (cIDString != null && cIDString.startsWith("Qm") && cIDString.length() == 46) {
+ QuorumPingReceiverLogger.debug("CID is "+ cIDString);
+ IPFSNetwork.pin(cIDString, ipfs);
+ }
+
+ }else{
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Quorum Pin Failed");
+ QuorumPingReceiverLogger.info("Quorum Pin Failed");
+ }
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + pingRequest);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return APIResponse.toString();
+ }
+}
diff --git a/src/com/rubix/Ping/VerifyStakedToken.java b/src/com/rubix/Ping/VerifyStakedToken.java
new file mode 100644
index 00000000..bd599f54
--- /dev/null
+++ b/src/com/rubix/Ping/VerifyStakedToken.java
@@ -0,0 +1,119 @@
+package com.rubix.Ping;
+
+import static com.rubix.Resources.Functions.IPFS_PORT;
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.SYNC_IP;
+import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
+import static com.rubix.Resources.IPFSNetwork.forward;
+import static com.rubix.Resources.IPFSNetwork.repo;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.net.HttpURLConnection;
+import java.net.Socket;
+import java.net.SocketException;
+import java.net.URL;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import io.ipfs.api.IPFS;
+
+public class VerifyStakedToken {
+ private static final Logger PingSenderLogger = Logger.getLogger(VerifyStakedToken.class);
+ public static IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
+
+ public static boolean Contact(String pid, int port, String tokenHash, String tokenContent)
+ throws IOException, JSONException {
+ repo(ipfs);
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+
+ JSONObject APIResponse = new JSONObject();
+
+ String appName = pid.concat("Ping");
+ forward(appName, port, pid);
+ PingSenderLogger.debug("Forwarded to " + appName + " on " + port + " for collecting credits");
+ Socket senderSocket = new Socket("127.0.0.1", port);
+
+ BufferedReader input = new BufferedReader(new InputStreamReader(senderSocket.getInputStream()));
+ PrintStream output = new PrintStream(senderSocket.getOutputStream());
+
+ output.println("Get-TokenChain-Height");
+ output.println(tokenHash);
+ String heightResponse;
+ try {
+ heightResponse = input.readLine();
+ } catch (SocketException e) {
+ PingSenderLogger.warn("Quorum " + pid + " is unable to Respond! - Credits Ping");
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
+ output.close();
+ input.close();
+ senderSocket.close();
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Quorum " + pid + "is unable to respond! - Credits Ping");
+
+ return false;
+ }
+
+ int height = 0;
+ if (heightResponse == null) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
+ PingSenderLogger.info("TokenChain height not received");
+ output.close();
+ input.close();
+ senderSocket.close();
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "TokenChain height not received");
+
+ } else {
+ PingSenderLogger.info("TokenChain height received from " + pid);
+ PingSenderLogger.info("TokenChain height: " + heightResponse);
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + pid);
+ output.close();
+ input.close();
+ senderSocket.close();
+ APIResponse.put("status", "Success");
+ APIResponse.put("message", Integer.parseInt(heightResponse));
+ height = Integer.parseInt(heightResponse);
+ }
+
+ String tokenLevel = tokenContent.substring(0, 3);
+ int tokenLevelInt = Integer.parseInt(tokenLevel);
+ int tokenLevelValue = (int) Math.pow(2, tokenLevelInt + 2);
+ int requiredMinedTokenHeight = tokenLevelValue * 4;
+
+ String GET_URL_credit = SYNC_IP + "/getCurrentLevel";
+ URL URLobj_credit = new URL(GET_URL_credit);
+ HttpURLConnection con_credit = (HttpURLConnection) URLobj_credit.openConnection();
+ con_credit.setRequestMethod("GET");
+ int responseCode_credit = con_credit.getResponseCode();
+ System.out.println("GET Response Code :: " + responseCode_credit);
+ if (responseCode_credit == HttpURLConnection.HTTP_OK) {
+ BufferedReader in_credit = new BufferedReader(
+ new InputStreamReader(con_credit.getInputStream()));
+ String inputLine_credit;
+ StringBuffer response_credit = new StringBuffer();
+ while ((inputLine_credit = in_credit.readLine()) != null) {
+ response_credit.append(inputLine_credit);
+ }
+ in_credit.close();
+ // QuorumConsensusLogger.debug("response from service " +
+ // response_credit.toString());
+ JSONObject resJsonData_credit = new JSONObject(response_credit.toString());
+ int level_credit = resJsonData_credit.getInt("level");
+
+ // ! release staked token if the mined token is from previous level(s)
+ if (level_credit > tokenLevelInt) {
+ return true;
+ }
+
+ } else
+ PingSenderLogger.debug("GET request not worked");
+
+ return height > requiredMinedTokenHeight;
+ }
+}
\ No newline at end of file
diff --git a/src/com/rubix/Resources/APIHandler.java b/src/com/rubix/Resources/APIHandler.java
index 14e22c13..57d625f4 100644
--- a/src/com/rubix/Resources/APIHandler.java
+++ b/src/com/rubix/Resources/APIHandler.java
@@ -1,20 +1,8 @@
package com.rubix.Resources;
-import static com.rubix.Resources.Functions.DATA_PATH;
-import static com.rubix.Resources.Functions.IPFS_PORT;
-import static com.rubix.Resources.Functions.LOGGER_PATH;
-import static com.rubix.Resources.Functions.SEND_PORT;
-import static com.rubix.Resources.Functions.SYNC_IP;
-import static com.rubix.Resources.Functions.WALLET_DATA_PATH;
-import static com.rubix.Resources.Functions.getOsName;
-import static com.rubix.Resources.Functions.getPeerID;
-import static com.rubix.Resources.Functions.getValues;
-import static com.rubix.Resources.Functions.nodeData;
-import static com.rubix.Resources.Functions.readFile;
-import static com.rubix.Resources.Functions.writeToFile;
-import static com.rubix.Resources.IPFSNetwork.add;
-import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
-import static com.rubix.Resources.IPFSNetwork.pin;
+import static com.rubix.Resources.Functions.*;
+import static com.rubix.Resources.IPFSNetwork.*;
+import static com.rubix.Constants.ConsensusConstants.*;
import java.io.BufferedReader;
import java.io.File;
@@ -32,6 +20,7 @@
import java.util.Iterator;
import java.util.List;
+import com.rubix.Datum.DataCommitter;
import com.rubix.Mining.ProofCredits;
import com.rubix.TokenTransfer.TokenSender;
@@ -96,6 +85,58 @@ public static JSONObject send(String data) throws Exception {
APILogger.info(sendMessage);
return sendMessage;
}
+
+
+ /**
+ * An API call to commit data into blockchain
+ * @param data Data specific to token transfer
+ * @return Message from the sender with transaction details
+ * @throws JSONException handles JSON Exceptions
+ * @throws NoSuchAlgorithmException handles Invalid Algorithms Exceptions
+ * @throws IOException handles IO Exceptions
+ */
+
+ public static JSONObject commit(String data) throws Exception {
+ Functions.pathSet();
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+
+ // APILogger.debug("data is "+ data);
+ String senderPeerID = getPeerID(DATA_PATH + "DID.json");
+ String senDID = getValues(DATA_PATH + "DID.json", "didHash", "peerid", senderPeerID);
+ JSONArray tokens;
+ JSONObject dataObject = new JSONObject(data);
+
+ // APILogger.debug("dataObject is "+ dataObject.toString());
+ // String recDID = dataObject.getString("receiverDidIpfsHash");
+ String blockHash;
+ String recDID;
+
+ String dataTableData = readFile(DATA_PATH + "DataTable.json");
+ boolean isObjectValid = false;
+ JSONArray dataTable = new JSONArray(dataTableData);
+ JSONObject sendMessage = new JSONObject();
+
+
+ // APILogger.debug("Trans type is "+ DATA);
+
+ // dataObject.put(TRANS_TYPE, DATA);
+
+ blockHash = dataObject.getString("blockHash");
+ if (blockHash.length() != 46) {
+ sendMessage.put("did", senDID);
+ sendMessage.put("tid", "null");
+ sendMessage.put("status", "Failed");
+ sendMessage.put("message", "Invalid Block Hash Entered");
+ return sendMessage;
+ }
+
+
+ dataObject.put("pvt", DATA_PATH + senDID + "/PrivateShare.png");
+ // APILogger.debug("dataObeject is "+dataObject.toString());
+ sendMessage = DataCommitter.Commit(dataObject.toString(), ipfs, SEND_PORT);
+ // APILogger.debug("send Message is "+sendMessage);
+ return sendMessage;
+ }
/**
* An API call to mine tokens
@@ -126,6 +167,8 @@ public static JSONObject create(int type) throws Exception {
JSONObject sendMessage = new JSONObject();
JSONObject detailsObject = new JSONObject();
+
+
detailsObject.put("receiverDidIpfsHash", senDID);
detailsObject.put("pvt", DATA_PATH + senDID + "/PrivateShare.png");
detailsObject.put("type", type);
@@ -135,42 +178,202 @@ public static JSONObject create(int type) throws Exception {
}
/**
- * A call to get details of a transaction given its ID
+ * A method to add and host your DID ans Public share to ipfs
*
- * @param txnId
- * @return Transaction Details
- * @throws JSONException handles JSON Exceptions
+ * @files DID.json, DataTable.json, DID.png, PublicShare.png
*/
- public static JSONArray transactionDetails(String txnId) throws JSONException {
- String transactionHistory = readFile(WALLET_DATA_PATH + "TransactionHistory.json");
- JSONObject countResult = new JSONObject();
+ public static void addPublicData() {
+ String peerID = getPeerID(DATA_PATH + "DID.json");
+ String didHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID);
+ String walletHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid", peerID);
+
+ add(DATA_PATH.concat(didHash).concat("/DID.png"), ipfs);
+ pin(didHash, ipfs);
+
+ add(DATA_PATH.concat(didHash).concat("/PublicShare.png"), ipfs);
+ pin(walletHash, ipfs);
+
+ }
+
+ /**
+ * A call to sync all the nodes in the network
+ *
+ * @return Message if failed or succeeded
+ * @throws IOException
+ * @files DataTable.json
+ */
+ public static String networkInfo() throws IOException, JSONException {
+ StringBuilder result = new StringBuilder();
JSONArray resultArray = new JSONArray();
- if (transactionHistory.length() == 0) {
- countResult.put("Message", "No transactions found");
- resultArray.put(countResult);
- return resultArray;
+ JSONObject jsonObject = new JSONObject();
+ int syncFlag = 0;
+ URL url = new URL(SYNC_IP + "/get");
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod("GET");
+ BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+ String line;
+
+ while ((line = rd.readLine()) != null) {
+ result.append(line);
+ syncFlag = 1;
}
- JSONArray transArray = new JSONArray(transactionHistory);
- JSONObject obj = new JSONObject();
- for (int i = 0; i < transArray.length(); i++) {
- obj = transArray.getJSONObject(i);
- if (obj.get("txn").equals(txnId)) {
- obj.remove("essentialShare");
+ rd.close();
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
- else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
- }
+ writeToFile(DATA_PATH + "DataTable.json", result.toString(), false);
+ if (syncFlag == 1) {
+ jsonObject.put("message", "Synced Successfully!");
+ } else {
+ jsonObject.put("message", "Not synced! Try again after sometime.");
+ }
+ resultArray.put(jsonObject);
+ return resultArray.toString();
+ }
+
+ /**
+ * Method to query the credits information
+ *
+ * @throws JSONException
+ * @files QuorumSignedTransactions.json, MinedCreditsHistory.json
+ */
+ public static JSONObject creditsInfo() throws JSONException {
+ String qstFile = WALLET_DATA_PATH.concat("QuorumSignedTransactions.json");
+ String mineFile = WALLET_DATA_PATH.concat("MinedCreditsHistory.json");
+
+ File quorumFile = new File(qstFile);
+ File minedFile = new File(mineFile);
+
+ int spentCredits = 0;
+ int unspentCredits = 0;
+ if (quorumFile.exists()) {
+ String qFile = readFile(qstFile);
+ JSONArray qArray = new JSONArray(qFile);
+ unspentCredits = qArray.length();
+ }
+ if (minedFile.exists()) {
+ String mFile = readFile(mineFile);
+ JSONArray mArray = new JSONArray(mFile);
+ spentCredits = mArray.length();
+ }
+
+ JSONObject returnObject = new JSONObject();
+ returnObject.put("spentCredits", spentCredits);
+ returnObject.put("unspentCredits", unspentCredits);
- resultArray.put(obj);
+ return returnObject;
+ }
+
+ /**
+ * A call to close all open IPFS streams
+ */
+ public static void closeStreams() {
+ executeIPFSCommands("ipfs p2p close --all");
+ }
+
+ public static int onlinePeersCount() throws JSONException, IOException, InterruptedException {
+ JSONArray peersArray = peersOnlineStatus();
+ int count = 0;
+ for (int i = 0; i < peersArray.length(); i++) {
+ if (peersArray.getJSONObject(i).getString("onlineStatus").contains("online"))
+ count++;
+ }
+ return count;
+ }
+
+ public static ArrayList swarmPeersList() throws IOException, InterruptedException {
+ String OS = getOsName();
+ String[] command = new String[3];
+ if (OS.contains("Mac") || OS.contains("Linux")) {
+ command[0] = "bash";
+ command[1] = "-c";
+ } else if (OS.contains("Windows")) {
+ command[0] = "cmd.exe";
+ command[1] = "/c";
+ }
+ command[2] = "export PATH=/usr/local/bin:$PATH && ipfs swarm peers";
+
+ Process P = Runtime.getRuntime().exec(command);
+ BufferedReader br = new BufferedReader(new InputStreamReader(P.getInputStream()));
+
+ ArrayList peersArray = new ArrayList();
+ String line;
+ while ((line = br.readLine()) != null) {
+ peersArray.add(line);
+ }
+ if (!OS.contains("Windows"))
+ P.waitFor();
+ br.close();
+ P.destroy();
+
+ ArrayList peersIdentities = new ArrayList();
+ if (peersArray.size() != 0) {
+ List k = ipfs.swarm.peers();
+ for (int i = 0; i < k.size(); i++)
+ peersIdentities.add(k.get(i).toString().substring(0, 46));
+
+ return peersIdentities;
+ }
+ return peersArray;
+ }
+
+ /**
+ * A call to get the online/offline status of your contacts
+ *
+ * @return List indicating online status of each DID contact
+ * @throws JSONException handles JSON Exceptions
+ * @throws IOException handles IO Exceptions
+ */
+ public static JSONArray peersOnlineStatus() throws JSONException, IOException, InterruptedException {
+ ArrayList peersArray = swarmPeersList();
+ String dataTable = readFile(DATA_PATH + "DataTable.json");
+ JSONArray dataArray = new JSONArray(dataTable);
+ JSONArray onlinePeers = new JSONArray();
+
+ for (int i = 0; i < dataArray.length(); i++) {
+ JSONObject peerObject = dataArray.getJSONObject(i);
+ String peerID = peerObject.getString("peerid");
+ if (peersArray.contains(peerID)) {
+ JSONObject onlinePeersObject = new JSONObject();
+ onlinePeersObject.put("did", getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID));
+ onlinePeersObject.put("onlineStatus", "online");
+ onlinePeers.put(onlinePeersObject);
+ } else {
+ JSONObject onlinePeersObject = new JSONObject();
+ onlinePeersObject.put("did", getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID));
+ onlinePeersObject.put("onlineStatus", "offline");
+ onlinePeers.put(onlinePeersObject);
}
}
- APILogger.info("Transaction Details for : " + obj.toString());
- return resultArray;
+
+ return onlinePeers;
+ }
+
+ /**
+ * A call to list out all contacts in the user wallet
+ *
+ * @return A list of user wallet contacts
+ * @throws JSONException handles JSON Exceptions
+ */
+ public static JSONArray contacts() throws JSONException {
+ String dataTable = readFile(DATA_PATH + "DataTable.json");
+ JSONArray dataArray = new JSONArray(dataTable);
+ JSONArray didArray = new JSONArray();
+ for (int i = 0; i < dataArray.length(); i++) {
+ didArray.put(dataArray.getJSONObject(i).getString("didHash"));
+ }
+ return didArray;
+ }
+
+ public static JSONObject syncNetworkNodes() throws JSONException, IOException {
+ String dataTable = readFile(DATA_PATH + "DataTable.json");
+ JSONArray dataArray = new JSONArray(dataTable);
+
+ for (int i = 0; i < dataArray.length(); i++)
+ nodeData(dataArray.getJSONObject(i).getString("didHash"),
+ dataArray.getJSONObject(i).getString("walletHash"), ipfs);
+
+ return new JSONObject("{\"message\":\"Synced all nodes\"}");
}
/**
@@ -178,6 +381,7 @@ public static JSONArray transactionDetails(String txnId) throws JSONException {
*
* @return Detailed explanation of the account information of the user
* @throws JSONException handles JSON Exceptions
+ * @files TransactionHistory.json, DID.json
*/
public static JSONArray accountInformation() throws JSONException {
int txnAsSender = 0, txnAsReceiver = 0;
@@ -219,53 +423,86 @@ public static JSONArray accountInformation() throws JSONException {
}
/**
- * A method to add and host your DID ans Public share to ipfs
+ * A call to list out number of transactions made per day
+ *
+ * @return List of transactions committed on every date
+ * @throws JSONException handles JSON Exceptions
+ * @files TransactionHistory.json
*/
- public static void addPublicData() {
- String peerID = getPeerID(DATA_PATH + "DID.json");
- String didHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID);
- String walletHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid", peerID);
+ public static JSONArray txnPerDay() throws JSONException {
+ String dataTableFileContent = readFile(WALLET_DATA_PATH + "TransactionHistory.json");
+ JSONArray dataTable = new JSONArray(dataTableFileContent);
+ HashSet dateSet = new HashSet<>();
+ for (int i = 0; i < dataTable.length(); i++)
+ dateSet.add(dataTable.getJSONObject(i).getString("Date"));
- add(DATA_PATH.concat(didHash).concat("/DID.png"), ipfs);
- pin(didHash, ipfs);
+ JSONObject datesTxn = new JSONObject();
+ Iterator dateIterator = dateSet.iterator();
+ while (dateIterator.hasNext()) {
+ String date = dateIterator.next();
+ int count = 0;
+ for (int i = 0; i < dataTable.length(); i++) {
+ JSONObject obj = dataTable.getJSONObject(i);
- add(DATA_PATH.concat(didHash).concat("/PublicShare.png"), ipfs);
- pin(walletHash, ipfs);
+ if (date.equals(obj.getString("Date"))) {
+ count++;
+ }
+ }
+ datesTxn.put(date, count);
+ }
+
+ return new JSONArray().put(datesTxn);
}
/**
- * A call to sync all the nodes in the network
+ * A call to get details of a transaction given its ID
*
- * @return Message if failed or succeeded
- * @throws IOException
+ * @param txnId Transaction ID
+ * @return Transaction Details
+ * @throws JSONException handles JSON Exceptions
+ * @files TransactionHistory.json
*/
- public static String networkInfo() throws IOException, JSONException {
- StringBuilder result = new StringBuilder();
+ public static JSONArray transactionDetails(String txnId) throws JSONException {
+ String transactionHistory = readFile(WALLET_DATA_PATH + "TransactionHistory.json");
+ JSONObject countResult = new JSONObject();
JSONArray resultArray = new JSONArray();
- JSONObject jsonObject = new JSONObject();
- int syncFlag = 0;
- URL url = new URL(SYNC_IP + "/get");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
-
- while ((line = rd.readLine()) != null) {
- result.append(line);
- syncFlag = 1;
+ if (transactionHistory.length() == 0) {
+ countResult.put("Message", "No transactions found");
+ resultArray.put(countResult);
+ return resultArray;
}
- rd.close();
+ JSONArray transactionArray = new JSONArray(transactionHistory);
+ JSONObject transactionObject = new JSONObject();
+ for (int i = 0; i < transactionArray.length(); i++) {
+ transactionObject = transactionArray.getJSONObject(i);
+ if (transactionObject.get("txn").equals(txnId)) {
+ transactionObject.remove("essentialShare");
+
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
+ else {
+ if (!transactionObject.get("tokens").toString().contains("[")) {
+ JSONArray tokensArray = new JSONArray("[" + transactionObject.get("tokens").toString() + "]");
+ if (transactionObject.has("tokens")) {
+ transactionObject.put("tokens", tokensArray);
+ }
+ } else {
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
+ }
+ }
+ resultArray.put(transactionObject);
+ }
- writeToFile(DATA_PATH + "DataTable.json", result.toString(), false);
- if (syncFlag == 1) {
- jsonObject.put("message", "Synced Successfully!");
- } else {
- jsonObject.put("message", "Not synced! Try again after sometime.");
}
- resultArray.put(jsonObject);
- return resultArray.toString();
+ APILogger.info("Transaction Details for : " + transactionObject.toString());
+ return resultArray;
}
/**
@@ -275,6 +512,7 @@ public static String networkInfo() throws IOException, JSONException {
* @param e End Date
* @return List of transactions
* @throws JSONException handles JSON Exceptions
+ * @files TransactionHistory.json
*/
public static JSONArray transactionsByDate(String s, String e) throws JSONException, ParseException {
JSONArray resultArray = new JSONArray();
@@ -287,8 +525,6 @@ public static JSONArray transactionsByDate(String s, String e) throws JSONExcept
JSONObject countResult = new JSONObject();
Date startDate = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss").parse(startDateString);
Date endDate = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss").parse(endDateString);
- APILogger.debug("start date is " + startDate);
- APILogger.debug("end date is " + endDate);
File fileCheck1 = new File(WALLET_DATA_PATH + "TransactionHistory.json");
if (!fileCheck1.exists()) {
countResult.put("Message", "File not found");
@@ -297,32 +533,37 @@ public static JSONArray transactionsByDate(String s, String e) throws JSONExcept
}
String transactionHistory = readFile(WALLET_DATA_PATH + "TransactionHistory.json");
JSONArray transArray = new JSONArray(transactionHistory);
- APILogger.debug(transArray.length());
if (transArray.length() == 0) {
countResult.put("Message", "No Transactions made yet");
resultArray.put(countResult);
return resultArray;
}
- JSONObject obj;
+ JSONObject transactionObject;
for (int i = 0; i < transArray.length(); i++) {
- obj = transArray.getJSONObject(i);
- String dateFromTxnHistoryString = obj.get("Date").toString();
- Date dateTH = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(dateFromTxnHistoryString);
- String dateTHS = objSDF.format(dateTH);
- Calendar c = Calendar.getInstance();
- c.setTime(objSDF.parse(dateTHS));
- dateTH = c.getTime();
- if (dateTH.after(startDate) && dateTH.before(endDate)) {
- obj.remove("essentialShare");
-
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
- else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
+ transactionObject = transArray.getJSONObject(i);
+ String dateFromTxnHistoryString = transactionObject.get("Date").toString();
+ if (dateFromTxnHistoryString.length() != 10) {
+ Date dateTH = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy").parse(dateFromTxnHistoryString);
+ String dateTHS = objSDF.format(dateTH);
+ Calendar c = Calendar.getInstance();
+ c.setTime(objSDF.parse(dateTHS));
+ dateTH = c.getTime();
+ if (dateTH.after(startDate) && dateTH.before(endDate)) {
+ transactionObject.remove("essentialShare");
+
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
+ else {
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
+ }
+
+ resultArray.put(transactionObject);
}
-
- resultArray.put(obj);
}
}
@@ -335,6 +576,7 @@ public static JSONArray transactionsByDate(String s, String e) throws JSONExcept
* @param n Count
* @return List of transactions
* @throws JSONException handles JSON Exceptions
+ * @files TransactionHistory.json
*/
public static JSONArray transactionsByCount(int n) throws JSONException {
@@ -351,7 +593,7 @@ public static JSONArray transactionsByCount(int n) throws JSONException {
}
String transactionHistory = readFile(path);
JSONArray transArray = new JSONArray(transactionHistory);
- JSONObject obj = new JSONObject();
+ JSONObject transactionObject;
if (transArray.length() == 0) {
countResult.put("Message", "No transactions made yet");
resultArray.put(countResult);
@@ -360,31 +602,39 @@ public static JSONArray transactionsByCount(int n) throws JSONException {
if (n >= transArray.length()) {
for (int i = transArray.length() - 1; i >= 0; i--) {
- obj = transArray.getJSONObject(i);
- obj.remove("essentialShare");
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
+ transactionObject = transArray.getJSONObject(i);
+ transactionObject.remove("essentialShare");
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
}
- resultArray.put(obj);
+ resultArray.put(transactionObject);
}
return resultArray;
}
for (int i = 1; i <= n; i++) {
- obj = transArray.getJSONObject(transArray.length() - i);
- obj.remove("essentialShare");
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
+ transactionObject = transArray.getJSONObject(transArray.length() - i);
+ transactionObject.remove("essentialShare");
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
}
- resultArray.put(obj);
+ resultArray.put(transactionObject);
}
return resultArray;
}
@@ -396,11 +646,12 @@ public static JSONArray transactionsByCount(int n) throws JSONException {
* @param end end index
* @return List of transactions
* @throws JSONException handles JSON Exceptions
+ * @files TransactionHistory.json
*/
public static JSONArray transactionsByRange(int start, int end) throws JSONException {
JSONObject countResult = new JSONObject();
JSONArray resultArray = new JSONArray();
- if (start <= 0 || end <= 0) {
+ if (start < 0 || end <= 0) {
countResult.put("Message", "Count can't be null or negative");
resultArray.put(countResult);
return resultArray;
@@ -427,122 +678,89 @@ public static JSONArray transactionsByRange(int start, int end) throws JSONExcep
if (!(end < transArray.length())) {
for (int i = start; i < transArray.length(); i++) {
- JSONObject obj = transArray.getJSONObject(i);
- obj.remove("essentialShare");
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
+ JSONObject transactionObject = transArray.getJSONObject(i);
+ transactionObject.remove("essentialShare");
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
}
- resultArray.put(obj);
+ resultArray.put(transactionObject);
}
} else {
if (start == end) {
- JSONObject obj = transArray.getJSONObject(start);
- obj.remove("essentialShare");
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
+ JSONObject transactionObject = transArray.getJSONObject(start);
+ transactionObject.remove("essentialShare");
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
}
- resultArray.put(obj);
- }
- for (int i = start; i < end; i++) {
- JSONObject obj = transArray.getJSONObject(i);
- obj.remove("essentialShare");
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
- else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
+ resultArray.put(transactionObject);
+ } else {
+ for (int i = start; i <= end; i++) {
+ JSONObject transactionObject = transArray.getJSONObject(i);
+ transactionObject.remove("essentialShare");
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
+ else {
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
+ }
+ resultArray.put(transactionObject);
}
-
- resultArray.put(obj);
}
}
return resultArray;
}
- /**
- * @throws JSONException
- *
- */
- public static JSONObject creditsInfo() throws JSONException {
- // String thFile = WALLET_DATA_PATH.concat("TransactionHistory.json");
- String qstFile = WALLET_DATA_PATH.concat("QuorumSignedTransactions.json");
- String mineFile = WALLET_DATA_PATH.concat("MinedCreditsHistory.json");
-
- // File txnFile = new File(thFile);
- File quorumFile = new File(qstFile);
- File minedFile = new File(mineFile);
-
- // int txnCount = 0;
- // if(txnFile.exists()){
- // String transactionFile =
- // readFile(WALLET_DATA_PATH.concat("TransactionHistory.json"));
- // JSONArray txnArray = new JSONArray(transactionFile);
- // txnCount = txnArray.length();
- //
- // }
- int spentCredits = 0;
- int unspentCredits = 0;
- if (quorumFile.exists()) {
- String qFile = readFile(qstFile);
- JSONArray qArray = new JSONArray(qFile);
- unspentCredits = qArray.length();
- }
- if (minedFile.exists()) {
- String mFile = readFile(mineFile);
- JSONArray mArray = new JSONArray(mFile);
- spentCredits = mArray.length();
- }
-
- JSONObject returnObject = new JSONObject();
- // returnObject.put("txnCount",txnCount);
- returnObject.put("spentCredits", spentCredits);
- returnObject.put("unspentCredits", unspentCredits);
-
- return returnObject;
- }
-
- /**
- * A call to close all open IPFS streams
- */
- public static void closeStreams() {
- executeIPFSCommands("ipfs p2p close --all");
- }
-
/**
* A call to get list transactions with the mentioned comment
*
* @param comment Comment
* @return List of transactions
* @throws JSONException handles JSON Exceptions
+ * @files TransactionHistory.json
*/
public static JSONArray transactionsByComment(String comment) throws JSONException {
String transactionHistory = readFile(WALLET_DATA_PATH + "TransactionHistory.json");
JSONArray transArray = new JSONArray(transactionHistory);
- JSONObject obj;
+ JSONObject transactionObject;
JSONArray resultArray = new JSONArray();
for (int i = 0; i < transArray.length(); i++) {
- obj = transArray.getJSONObject(i);
-
- if (obj.get("comment").equals(comment)) {
- obj.remove("essentialShare");
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
+ transactionObject = transArray.getJSONObject(i);
+
+ if (transactionObject.get("comment").equals(comment)) {
+ transactionObject.remove("essentialShare");
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
}
-
- resultArray.put(obj);
+ resultArray.put(transactionObject);
}
}
@@ -561,166 +779,33 @@ public static JSONArray transactionsByComment(String comment) throws JSONExcepti
* @param did DID of the contact
* @return List of transactions committed with the user DID
* @throws JSONException handles JSON Exceptions
+ * @files TransactionHistory.json
*/
public static JSONArray transactionsByDID(String did) throws JSONException {
String transactionHistory = readFile(WALLET_DATA_PATH + "TransactionHistory.json");
JSONArray transArray = new JSONArray(transactionHistory);
JSONArray resultArray = new JSONArray();
for (int i = 0; i < transArray.length(); i++) {
- JSONObject obj = transArray.getJSONObject(i);
- obj.remove("essentialShare");
+ JSONObject transactionObject = transArray.getJSONObject(i);
+ transactionObject.remove("essentialShare");
+
+ if (transactionObject.get("senderDID").equals(did) || transactionObject.get("receiverDID").equals(did)) {
+ if (transactionObject.has("amount-received")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-received"));
+ } else if (transactionObject.has("amount-spent")) {
+ transactionObject.put("amount", transactionObject.getDouble("amount-spent"));
+ } else if (transactionObject.has("amount"))
+ transactionObject.put("amount", transactionObject.getDouble("amount"));
+ else {
+ JSONArray tokensArray = (JSONArray) transactionObject.get("tokens");
+ transactionObject.put("amount", tokensArray.length());
+ }
- if (obj.has("amount"))
- obj.put("amount", obj.getDouble("amount"));
- else {
- JSONArray tokensArray = (JSONArray) obj.get("tokens");
- obj.put("amount", tokensArray.length());
+ resultArray.put(transactionObject);
}
-
- resultArray.put(obj);
-
- if (obj.get("senderDID").equals(did) || obj.get("receiverDID").equals(did))
- resultArray.put(obj);
}
return resultArray;
}
- public static int onlinePeersCount() throws JSONException, IOException, InterruptedException {
- JSONArray peersArray = peersOnlineStatus();
- int count = 0;
- for (int i = 0; i < peersArray.length(); i++) {
- if (peersArray.getJSONObject(i).getString("onlineStatus").contains("online"))
- count++;
- }
- return count;
- }
-
- public static ArrayList swarmPeersList() throws IOException, InterruptedException {
- String OS = getOsName();
- String[] command = new String[3];
- if (OS.contains("Mac") || OS.contains("Linux")) {
- command[0] = "bash";
- command[1] = "-c";
- } else if (OS.contains("Windows")) {
- command[0] = "cmd.exe";
- command[1] = "/c";
- }
- command[2] = "export PATH=/usr/local/bin:$PATH && ipfs swarm peers";
-
- Process P = Runtime.getRuntime().exec(command);
- BufferedReader br = new BufferedReader(new InputStreamReader(P.getInputStream()));
-
- ArrayList peersArray = new ArrayList();
- String line;
- while ((line = br.readLine()) != null) {
- peersArray.add(line);
- }
- if (!OS.contains("Windows"))
- P.waitFor();
- br.close();
- P.destroy();
-
- ArrayList peersIdentities = new ArrayList();
- if (peersArray.size() != 0) {
- List k = ipfs.swarm.peers();
- for (int i = 0; i < k.size(); i++)
- peersIdentities.add(k.get(i).toString().substring(0, 46));
-
- return peersIdentities;
- }
- return peersArray;
- }
-
- /**
- * A call to get the online/offline status of your contacts
- *
- * @return List indicating online status of each DID contact
- * @throws JSONException handles JSON Exceptions
- * @throws IOException handles IO Exceptions
- */
- public static JSONArray peersOnlineStatus() throws JSONException, IOException, InterruptedException {
- ArrayList peersArray = swarmPeersList();
- String dataTable = readFile(DATA_PATH + "DataTable.json");
- JSONArray dataArray = new JSONArray(dataTable);
- JSONArray onlinePeers = new JSONArray();
-
- for (int i = 0; i < dataArray.length(); i++) {
- JSONObject peerObject = dataArray.getJSONObject(i);
- String peerID = peerObject.getString("peerid");
- if (peersArray.contains(peerID)) {
- JSONObject onlinePeersObject = new JSONObject();
- onlinePeersObject.put("did", getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID));
- onlinePeersObject.put("onlineStatus", "online");
- onlinePeers.put(onlinePeersObject);
- } else {
- JSONObject onlinePeersObject = new JSONObject();
- onlinePeersObject.put("did", getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", peerID));
- onlinePeersObject.put("onlineStatus", "offline");
- onlinePeers.put(onlinePeersObject);
- }
-
- }
-
- return onlinePeers;
- }
-
- /**
- * A call to list out all contacts in the user wallet
- *
- * @return A list of user wallet contacts
- * @throws JSONException handles JSON Exceptions
- */
- public static JSONArray contacts() throws JSONException {
- String dataTable = readFile(DATA_PATH + "DataTable.json");
- JSONArray dataArray = new JSONArray(dataTable);
- JSONArray didArray = new JSONArray();
- for (int i = 0; i < dataArray.length(); i++) {
- didArray.put(dataArray.getJSONObject(i).getString("didHash"));
- }
- return didArray;
- }
-
- /**
- * A call to list out number of transactions made per day
- *
- * @return List of transactions committed on every date
- * @throws JSONException handles JSON Exceptions
- */
- public static JSONArray txnPerDay() throws JSONException {
- String dataTable = readFile(WALLET_DATA_PATH + "TransactionHistory.json");
- JSONArray dataArray = new JSONArray(dataTable);
- HashSet dateSet = new HashSet<>();
- for (int i = 0; i < dataArray.length(); i++)
- dateSet.add(dataArray.getJSONObject(i).getString("Date"));
-
- JSONObject datesTxn = new JSONObject();
- Iterator dateIterator = dateSet.iterator();
- while (dateIterator.hasNext()) {
- String date = dateIterator.next();
- int count = 0;
- for (int i = 0; i < dataArray.length(); i++) {
- JSONObject obj = dataArray.getJSONObject(i);
-
- if (date.equals(obj.getString("Date"))) {
- count++;
- }
- }
- datesTxn.put(date, count);
-
- }
-
- return new JSONArray().put(datesTxn);
- }
-
- public static JSONObject syncNetworkNodes() throws JSONException, IOException {
- String dataTable = readFile(DATA_PATH + "DataTable.json");
- JSONArray dataArray = new JSONArray(dataTable);
-
- for (int i = 0; i < dataArray.length(); i++)
- nodeData(dataArray.getJSONObject(i).getString("didHash"),
- dataArray.getJSONObject(i).getString("walletHash"), ipfs);
-
- return new JSONObject("{\"message\":\"Synced all nodes\"}");
- }
}
diff --git a/src/com/rubix/Resources/Functions.java b/src/com/rubix/Resources/Functions.java
index e03ff34c..496deac1 100644
--- a/src/com/rubix/Resources/Functions.java
+++ b/src/com/rubix/Resources/Functions.java
@@ -1,11 +1,13 @@
package com.rubix.Resources;
import static com.rubix.Resources.APIHandler.addPublicData;
+import static com.rubix.Resources.IPFSNetwork.IPFSNetworkLogger;
import static com.rubix.Resources.IPFSNetwork.checkSwarmConnect;
-import static com.rubix.Resources.IPFSNetwork.dhtOwnerCheck;
import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
import static com.rubix.Resources.IPFSNetwork.forwardCheck;
import static com.rubix.Resources.IPFSNetwork.listen;
+import static com.rubix.Resources.IPFSNetwork.swarmConnectP2P;
+import static com.rubix.Resources.IPFSNetwork.swarmConnectProcess;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
@@ -18,6 +20,7 @@
import java.io.InputStreamReader;
import java.math.RoundingMode;
import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -29,19 +32,25 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Set;
import javax.imageio.ImageIO;
-import com.rubix.AuthenticateNode.PropImage;
-
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
+import com.rubix.AuthenticateNode.PropImage;
+import com.rubix.Datum.Dependency;
+import com.rubix.Ping.PingCheck;
+
import io.ipfs.api.IPFS;
+import io.ipfs.multiaddr.MultiAddress;
public class Functions {
@@ -67,40 +76,11 @@ public class Functions {
public static boolean CONSENSUS_STATUS;
public static JSONObject QUORUM_MEMBERS;
public static JSONArray BOOTSTRAPS;
+ public static String DATA__PATH = "";
+ public static String DATUM_CHAIN_PATH = "";
public static Logger FunctionsLogger = Logger.getLogger(Functions.class);
- public static String buildVersion() throws IOException {
- String initPath = Functions.class.getProtectionDomain().getCodeSource().getLocation().getPath();
- initPath = initPath.split("\\.jar")[0];
- initPath = initPath + ".jar";
- String initHash = calculateFileHash(initPath, "MD5");
- return initHash;
- }
-
- public static String calculateFileHash(String filePath, String algorithm) {
- String hash = "";
- try {
- MessageDigest digest = MessageDigest.getInstance(algorithm);
- System.out.println("File path: " + filePath);
- System.out.println("File Hashing...");
- FileInputStream fis = new FileInputStream(filePath);
- System.out.println("File read OK");
- byte[] dataBytes = new byte[1024];
- int nread = 0;
- while ((nread = fis.read(dataBytes)) != -1) {
- digest.update(dataBytes, 0, nread);
- }
- byte[] hashBytes = digest.digest();
- hash = bytesToHex(hashBytes);
- fis.close();
- } catch (NoSuchAlgorithmException | IOException e) {
- FunctionsLogger.error("Invalid Cryptographic Algorithm", e);
- e.printStackTrace();
- }
- return hash;
- }
-
public static void setDir() {
String OSName = getOsName();
if (OSName.contains("Windows"))
@@ -118,6 +98,43 @@ public static void setConfig() {
configPath = dirPath.concat("config.json");
}
+ public static String buildVersion() throws IOException {
+ String jarPath = Functions.class.getProtectionDomain().getCodeSource().getLocation().getPath();
+ jarPath = jarPath.split("\\.jar")[0];
+ jarPath = jarPath.split("file:", 2)[1];
+ // trim first part of the string till first "/"
+
+ jarPath = jarPath + ".jar";
+ String hash = calculateFileHash(jarPath, "MD5");
+ return hash;
+ }
+
+ public static String calculateFileHash(String filePath, String algorithm) {
+ String hash = "";
+ try {
+ MessageDigest digest = MessageDigest.getInstance(algorithm);
+ System.out.println("File path: " + filePath);
+
+ // check if file exists at the filePath
+ File file = new File(filePath);
+ if (file.exists()) {
+ FileInputStream fis = new FileInputStream(file);
+ byte[] byteArray = new byte[1024];
+ int bytesCount = 0;
+ while ((bytesCount = fis.read(byteArray)) != -1) {
+ digest.update(byteArray, 0, bytesCount);
+ }
+ byte[] hashBytes = digest.digest();
+ hash = bytesToHex(hashBytes);
+ fis.close();
+ }
+ } catch (NoSuchAlgorithmException | IOException e) {
+ FunctionsLogger.error("Invalid Cryptographic Algorithm while calculating file hash", e);
+ e.printStackTrace();
+ }
+ return hash;
+ }
+
/**
* This method sets the required paths used in the functions
*/
@@ -135,7 +152,26 @@ public static void pathSet() {
TOKENCHAIN_PATH = pathsArray.getJSONObject(0).getString("TOKENCHAIN_PATH");
WALLET_DATA_PATH = pathsArray.getJSONObject(0).getString("WALLET_DATA_PATH");
PAYMENTS_PATH = pathsArray.getJSONObject(0).getString("PAYMENTS_PATH");
-
+ if (pathsArray.getJSONObject(0).toString().contains("DATUM_CHAIN_PATH")) {
+ DATUM_CHAIN_PATH = pathsArray.getJSONObject(0).getString("DATUM_CHAIN_PATH");
+ try {
+ Dependency.checkDatumFolder();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ } else {
+ Dependency.checkDatumPath();
+ try {
+ configFileContent = readFile(configPath);
+ pathsArray = new JSONArray(configFileContent);
+ DATUM_CHAIN_PATH = pathsArray.getJSONObject(0).getString("DATUM_CHAIN_PATH");
+ Dependency.checkDatumFolder();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
SEND_PORT = pathsArray.getJSONObject(1).getInt("SEND_PORT");
RECEIVER_PORT = pathsArray.getJSONObject(1).getInt("RECEIVER_PORT");
GOSSIP_RECEIVER = pathsArray.getJSONObject(1).getInt("GOSSIP_RECEIVER");
@@ -324,6 +360,7 @@ public static String bytesToHex(byte[] inputHash) {
* @return File Content as string
*/
public static String readFile(String filePath) {
+
FileReader fileReader;
StringBuilder fileContent = new StringBuilder();
try {
@@ -335,6 +372,7 @@ public static String readFile(String filePath) {
} catch (IOException e) {
e.printStackTrace();
}
+
return fileContent.toString();
}
@@ -384,6 +422,7 @@ public static String getSignFromShares(String filePath, String hash) throws IOEx
int[] finalpos = (int[]) P.get("posForSign");
int[] p1Sign = getPrivatePosition(finalpos, privateIntegerArray1);
String p1 = intArrayToStr(p1Sign);
+ FunctionsLogger.debug("sign from share is " + p1);
return p1;
}
@@ -431,29 +470,6 @@ public static void listenThread(JSONObject connectObject) {
}
- /**
- * This function check if the token is pinned by the given DID
- *
- * @param token ID and DID of owner of the token to be checked
- * @return boolean value
- */
- public static boolean checkTokenOwnershiByDID(String tokenID, String DID) {
- boolean ownStatus = false;
- String peerID = getValues(DATA_PATH + "DataTable.json", "peerid",
- "didHash", DID);
- try {
- ArrayList owners = dhtOwnerCheck(tokenID);
- if (owners.contains(peerID)) {
- ownStatus = true;
- }
-
- } catch (InterruptedException | JSONException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return ownStatus;
- }
-
/**
* This function converts any integer to its binary form
*
@@ -495,7 +511,9 @@ public static String binarytoDec(String bin) {
*/
public static void updateJSON(String operation, String filePath, String data) {
+
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+
try {
while (mutex) {
}
@@ -611,7 +629,9 @@ public static ArrayList QuorumCheck(JSONArray quorum, int size) {
for (int i = 0; i < quorum.length(); i++) {
String quorumPeer;
try {
- quorumPeer = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", quorum.getString(i));
+ quorumPeer = Dependency.getPIDfromDID(quorum.getString(i), Dependency.dataTableHashMap());
+ // getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
+ // quorum.getString(i));
if (checkSwarmConnect().contains(quorumPeer)) {
peers.add(quorumPeer);
FunctionsLogger.debug(quorumPeer + " added to list");
@@ -641,7 +661,9 @@ public static void QuorumSwarmConnect(JSONArray quorum, IPFS ipfs) {
for (int i = 0; i < quorum.length(); i++) {
String quorumPeer;
try {
- quorumPeer = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", quorum.getString(i));
+ quorumPeer = Dependency.getPIDfromDID(quorum.getString(i), Dependency.dataTableHashMap());
+ // getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
+ // quorum.getString(i));
IPFSNetwork.swarmConnectP2P(quorumPeer, ipfs);
@@ -816,13 +838,12 @@ public static void deleteFile(String fileName) {
public static void launch() {
pathSet();
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
- executeIPFSCommands("ipfs daemon --enable-gc");
+ executeIPFSCommands("ipfs daemon --enable-gc --enable-pubsub-experiment");
FunctionsLogger.debug("Enabled ipfs GC");
}
/**
- * This function checks if the Rubix Working Directory is present or not
*
* @return A message
* @throws JSONException handle all JSON Exceptions
@@ -856,14 +877,16 @@ public static String checkDirectory() throws JSONException {
File tokensFolder = new File(TOKENS_PATH);
File tokenChainsFolder = new File(TOKENCHAIN_PATH);
File walletDataFolder = new File(WALLET_DATA_PATH);
+ File commitDataFolder = new File(DATUM_CHAIN_PATH);
if (!dataFolder.exists() || !loggerFolder.exists() || !tokenChainsFolder.exists() || !tokensFolder.exists()
- || !walletDataFolder.exists()) {
+ || !walletDataFolder.exists() || !commitDataFolder.exists()) {
dataFolder.delete();
loggerFolder.delete();
tokenChainsFolder.delete();
tokensFolder.delete();
walletDataFolder.delete();
+ commitDataFolder.delete();
JSONObject result = new JSONObject();
result.put("message", "User not registered, create your Decentralised Identity!");
result.put("info", "Inner Folders Missing");
@@ -1035,7 +1058,7 @@ public static void updateQuorum(JSONArray quorumArray, JSONArray signedQuorumLis
* @return JSONArray of quorum nodes
*/
- public static JSONArray getQuorum(String betaHash, String gammaHash, String senderDidIpfsHash,
+ public static JSONArray getQuorum(String senderDidIpfsHash,
String receiverDidIpfsHash, int tokenslength) throws IOException, JSONException {
JSONArray quorumArray;
String urlQuorumPick = ADVISORY_IP + "/getQuorum";
@@ -1049,8 +1072,7 @@ public static JSONArray getQuorum(String betaHash, String gammaHash, String send
conQuorumPick.setRequestProperty("Authorization", "null");
JSONObject dataToSendQuorumPick = new JSONObject();
- dataToSendQuorumPick.put("betahash", betaHash);
- dataToSendQuorumPick.put("gammahash", gammaHash);
+
dataToSendQuorumPick.put("sender", senderDidIpfsHash);
dataToSendQuorumPick.put("receiver", receiverDidIpfsHash);
dataToSendQuorumPick.put("tokencount", tokenslength);
@@ -1067,17 +1089,25 @@ public static JSONArray getQuorum(String betaHash, String gammaHash, String send
FunctionsLogger.debug("Post Data : " + populateQuorumPick);
FunctionsLogger.debug("Response Code : " + responseCodeQuorumPick);
- BufferedReader inQuorumPick = new BufferedReader(
- new InputStreamReader(conQuorumPick.getInputStream()));
- String outputQuorumPick;
- StringBuffer responseQuorumPick = new StringBuffer();
- while ((outputQuorumPick = inQuorumPick.readLine()) != null) {
- responseQuorumPick.append(outputQuorumPick);
+ if (responseCodeQuorumPick == 200) {
+
+ BufferedReader inQuorumPick = new BufferedReader(
+ new InputStreamReader(conQuorumPick.getInputStream()));
+ String outputQuorumPick;
+ StringBuffer responseQuorumPick = new StringBuffer();
+ while ((outputQuorumPick = inQuorumPick.readLine()) != null) {
+ responseQuorumPick.append(outputQuorumPick);
+ }
+ inQuorumPick.close();
+ FunctionsLogger.debug(" responsequorumpick " + responseQuorumPick.toString());
+ quorumArray = new JSONArray(responseQuorumPick.toString());
+ return quorumArray;
+
+ } else {
+ quorumArray = new JSONArray();
+ quorumArray.put(false);
+ return quorumArray;
}
- inQuorumPick.close();
- FunctionsLogger.debug(" responsequorumpick " + responseQuorumPick.toString());
- quorumArray = new JSONArray(responseQuorumPick.toString());
- return quorumArray;
}
/**
@@ -1126,14 +1156,6 @@ public static void mineUpdate(String didHash, int credits) throws IOException, J
}
- public static String initHash() throws IOException {
- String initPath = Functions.class.getProtectionDomain().getCodeSource().getLocation().getPath();
- initPath = initPath.split("\\.jar")[0];
- initPath = initPath + ".jar";
- String initHash = calculateFileHash(initPath, "SHA3-256");
- return initHash;
- }
-
public static int checkHeartBeat(String peerId, String appName) {
if (forwardCheck(appName, QUORUM_PORT, peerId)) {
@@ -1173,88 +1195,58 @@ public static void syncDataTable(String did, String peerId) {
}
/**
- * This function will release the port in linux based machines if the port is
- * already in use
+ * To Sync DataTable.json, if required
*/
- public static void releasePorts(int port) {
- String processStr;
- Process processId;
+ public static void syncDataTableByDID(String did) {
try {
- processId = Runtime.getRuntime().exec("lsof -ti :" + port);
- long currentPid = ProcessHandle.current().pid();
- BufferedReader br = new BufferedReader(
- new InputStreamReader(processId.getInputStream()));
-
- processId = Runtime.getRuntime().exec("pgrep ipfs");
- BufferedReader ipfsPidBr = new BufferedReader(new InputStreamReader(processId.getInputStream()));
-
- processStr = br.readLine();
- while (processStr != null
- && (String.valueOf(currentPid) != processStr || (ipfsPidBr.readLine() != processStr))) {
- FunctionsLogger.debug("Port " + port + " is in using, killing PID " + processStr);
- processId = Runtime.getRuntime().exec("kill -9 " + processStr);
-
+ String dataTableData = readFile(DATA_PATH + "DataTable.json");
+ boolean isObjectValid = false;
+ JSONArray dataTable = new JSONArray(dataTableData);
+ for (int i = 0; i < dataTable.length(); i++) {
+ JSONObject dataTableObject = dataTable.getJSONObject(i);
+ if ((did != null && dataTableObject.getString("didHash").equals(did))) {
+ isObjectValid = true;
+ break;
+ }
+ }
+ if (!isObjectValid) {
+ FunctionsLogger.debug("Syncing Datatable.json!");
+ APIHandler.networkInfo();
}
- processId.waitFor();
- processId.destroy();
-
} catch (Exception e) {
- FunctionsLogger.error("Exception Occured at releasePort", e);
+ FunctionsLogger.error("Exception Occured", e);
e.printStackTrace();
}
}
- public static void portStatusWindows(int port) {
- String processStr;
- Process p;
- try {
- Runtime rt = Runtime.getRuntime();
- Process proc = rt.exec("cmd /c netstat -ano | findstr " + port);
- long currentPid = ProcessHandle.current().pid();
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
- processStr = stdInput.readLine();
- if (processStr != null && String.valueOf(currentPid) != processStr) {
- int index = processStr.lastIndexOf(" ");
- String sc = processStr.substring(index, processStr.length());
- // System.out.println("Port "+port+" is locked by PID "+sc+". Kindly close this
- // port and retry transcation");
- if (sc != String.valueOf(currentPid)) {
- FunctionsLogger.debug("Port " + port + " is locked by PID " + sc);
- } else {
- FunctionsLogger.debug("Port " + port + " is locked by current jar with PID " + sc);
- }
+ public static void correctToken() throws JSONException {
+ pathSet();
+ String bank = readFile(PAYMENTS_PATH.concat("BNK00.json"));
+ JSONArray bankArray = new JSONArray(bank);
+ JSONObject firstToken;
+ if (bankArray.length() > 1) {
+ firstToken = bankArray.getJSONObject(0);
- }
- } catch (Exception e) {
- FunctionsLogger.error("Exception occured at portStatusWindows", e);
- e.printStackTrace();
+ bankArray.remove(0);
+ bankArray.put(firstToken);
+
+ writeToFile(PAYMENTS_PATH.concat("BNK00.json"), bankArray.toString(), false);
}
}
- public static void removeToken() throws JSONException {
- String bnkFile = readFile(PAYMENTS_PATH.concat("BNK00.json"));
- JSONArray bnkArray = new JSONArray(bnkFile);
- JSONObject removeToken = bnkArray.getJSONObject(0);
- bnkArray.remove(0);
- writeToFile(PAYMENTS_PATH.concat("BNK00.json"), bnkArray.toString(), false);
+ public static void correctPartToken() throws JSONException {
+ pathSet();
+ String bank = readFile(PAYMENTS_PATH.concat("PartsToken.json"));
+ JSONArray bankArray = new JSONArray(bank);
+ JSONObject firstToken;
+ if (bankArray.length() > 1) {
+ firstToken = bankArray.getJSONObject(0);
- File doubleSpentFile = new File(PAYMENTS_PATH.concat("DoubleSpent.json"));
- if (!doubleSpentFile.exists()) {
- try {
- doubleSpentFile.createNewFile();
- } catch (IOException e) {
- FunctionsLogger.debug("File couldn't be created");
- }
- JSONArray removeArray = new JSONArray();
- removeArray.put(removeToken);
- writeToFile(PAYMENTS_PATH.concat("DoubleSpent.json"), removeArray.toString(), false);
- } else {
- String removeFile = readFile(PAYMENTS_PATH.concat("DoubleSpent.json"));
- JSONArray removeArray = new JSONArray(removeFile);
- removeArray.put(removeToken);
- writeToFile(PAYMENTS_PATH.concat("DoubleSpent.json"), removeArray.toString(), false);
- }
+ bankArray.remove(0);
+ bankArray.put(firstToken);
+ writeToFile(PAYMENTS_PATH.concat("PartsToken.json"), bankArray.toString(), false);
+ }
}
public static void tokenBank() throws JSONException {
@@ -1472,6 +1464,33 @@ public static Double getBalance() throws JSONException {
return balance;
}
+ public static String initHash() {
+ String version = "";
+ try {
+ URL url = new URL("http://localhost:1898/getVersion");
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod("GET");
+ conn.setRequestProperty("Accept", "application/json");
+ if (conn.getResponseCode() != 200) {
+ throw new RuntimeException("Failed : HTTP error code : "
+ + conn.getResponseCode());
+ }
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ (conn.getInputStream())));
+ String output;
+ while ((output = br.readLine()) != null) {
+ version = output;
+ }
+ conn.disconnect();
+ FunctionsLogger.debug("initHash version is " + version);
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return version;
+ }
+
public static Double partTokenBalance(String tokenHash) throws JSONException {
pathSet();
@@ -1551,4 +1570,539 @@ public static void backgroundChecks() {
}
+ public static String sanityMessage;
+
+ public static boolean sanityCheck(String userType, String peerid, IPFS ipfs, int port)
+ throws IOException, JSONException {
+ FunctionsLogger.info("Entering " + userType + " SanityCheck");
+ boolean sanityCheckErrorFlag = true;
+ if (sanityCheckErrorFlag && checkIPFSStatus(peerid, ipfs)) {
+ FunctionsLogger.debug(userType + " IPFS is working in " + peerid);
+ } else {
+ sanityCheckErrorFlag = false;
+ sanityMessage = userType + " IPFS is not working in " + peerid;
+ }
+
+ if (sanityCheckErrorFlag) {
+ if (bootstrapConnect(peerid, ipfs)) {
+ FunctionsLogger.debug("Bootstrap connected for " + userType + " : " + peerid);
+ } else {
+ sanityCheckErrorFlag = false;
+ sanityMessage = "Bootstrap connection unsuccessful for " + userType + " : " + peerid;
+ }
+ }
+
+ if (sanityCheckErrorFlag) {
+ if (ping(peerid, port)) {
+ FunctionsLogger.debug(userType + " is running the latest Jar :" + peerid);
+ } else {
+ sanityCheckErrorFlag = false;
+ sanityMessage = userType + " is not running the latest Jar. PID: " + peerid;
+ }
+ }
+ /*
+ * if (sanityCheckErrorFlag) {
+ * if (portCheckAndKill(port)) {
+ * FunctionsLogger.debug("Ports are available for transcations in " + peerid);
+ * } else {
+ * sanityCheckErrorFlag = false;
+ * sanityMessage = "Ports are not available for " + peerid;
+ * }
+ * }
+ */
+ return sanityCheckErrorFlag;
+ }
+
+ public static boolean sanityCheckForData(String peerid, IPFS ipfs, int port) throws IOException, JSONException {
+ FunctionsLogger.info("Entering Receiver SanityCheck");
+ boolean sanityCheckErrorFlag = true;
+ if (sanityCheckErrorFlag && checkIPFSStatus(peerid, ipfs)) {
+ FunctionsLogger.debug("Receiver IPFS is working in " + peerid);
+ } else {
+ sanityCheckErrorFlag = false;
+ sanityMessage = "Receiver IPFS is not working in " + peerid;
+ }
+
+ if (sanityCheckErrorFlag) {
+ if (bootstrapConnect(peerid, ipfs)) {
+ FunctionsLogger.debug("Bootstrap connected for Receiver " + peerid);
+ } else {
+ sanityCheckErrorFlag = false;
+ sanityMessage = "Bootstrap connection unsuccessful for Receiver " + peerid;
+ }
+ }
+
+ if (sanityCheckErrorFlag) {
+ if (ping(peerid, port)) {
+ FunctionsLogger.debug("Rceiver is running the latest Jar " + peerid);
+ } else {
+ sanityCheckErrorFlag = false;
+ sanityMessage = "Receiver is not running the latest Jar. PID: " + peerid;
+ }
+ }
+
+ if (sanityCheckErrorFlag) {
+ if (portCheckAndKill(port)) {
+ FunctionsLogger.debug("Ports are available for transcations in " + peerid);
+ } else {
+ sanityCheckErrorFlag = false;
+ sanityMessage = "Ports are not available for " + peerid;
+ }
+ }
+
+ return sanityCheckErrorFlag;
+ }
+
+ public static boolean checkIPFSStatus(String peerid, IPFS ipfs) {
+ FunctionsLogger.info("Entering checkIPFSStatus");
+ boolean swarmConnectedStatus = false;
+ try {
+ MultiAddress multiAddress = new MultiAddress("/ipfs/" + peerid);
+ boolean output = swarmConnectP2P(peerid, ipfs);
+
+ if (output) {
+ swarmConnectedStatus = true;
+ FunctionsLogger.debug("Swarm is already connected");
+ } else {
+ swarmConnectedStatus = false;
+ FunctionsLogger.debug("Swarm is not connected");
+ }
+ } catch (Exception e) {
+ FunctionsLogger.error("Check Swarm Connect is failed", e);
+
+ }
+ FunctionsLogger.info("checkIPFSStatus return value is " + swarmConnectedStatus);
+ return swarmConnectedStatus;
+ }
+
+ public static boolean ping(String peerid, int port) throws IOException, JSONException {
+ JSONObject pingCheck = PingCheck.Ping(peerid, port);
+ if (pingCheck.getString("status").contains("Failed")) {
+ return false;
+ } else
+ return true;
+
+ }
+
+ // public static String getPing(int port) {
+ // try {
+ //
+ // String didContent = readFile(DATA_PATH + "DID.json");
+ // JSONArray didArray = new JSONArray(didContent);
+ // String myPeerID = didArray.getJSONObject(0).getString("peerid");
+ //
+ // listen(myPeerID.concat("Ping"), port);
+ // ServerSocket ss = new ServerSocket(port);
+ // FunctionsLogger.info("Get Ping Listening on port " + port + " appname " +
+ // myPeerID.concat("Ping"));
+ // Socket socket = ss.accept();
+ // BufferedReader input = new BufferedReader(new
+ // InputStreamReader(socket.getInputStream()));
+ // PrintStream output = new PrintStream(socket.getOutputStream());
+ // FunctionsLogger.info("getPing- waiting response from server");
+ // String peerID = input.readLine();
+ // if (peerID != null && peerID.contains("Qm")) {
+ // FunctionsLogger.info("getPing - Received message from server");
+ // output.println("Ping received");
+ // FunctionsLogger.debug("Ping received from sender");
+ //
+ // output.close();
+ // input.close();
+ // socket.close();
+ // ss.close();
+ // executeIPFSCommands(" ipfs p2p close -t /p2p/" + peerID);
+ // FunctionsLogger.info("If - Closing Sockets");
+ // return "Ping received from sender and Pong sent";
+ // }
+ // else{
+ // output.close();
+ // input.close();
+ // socket.close();
+ // ss.close();
+ // FunctionsLogger.info("Else - Closing Sockets");
+ // return "Ping received from sender but not PeerID";
+ // }
+ //
+ // } catch (Exception e) {
+ // FunctionsLogger.error("Error in client side communication", e);
+ // return "Error in client side communication";
+ // }
+ // }
+
+ public static boolean bootstrapConnect(String peerid, IPFS ipfs) {
+ String bootNode;
+ boolean bootstrapConnected = false;
+
+ MultiAddress multiAddress = new MultiAddress("/ipfs/" + peerid);
+
+ String output = swarmConnectProcess(multiAddress);
+ try {
+ for (int i = 0; i < BOOTSTRAPS.length(); i++) {
+ FunctionsLogger.info("bootstrapConnect- Bootstrap length is " + BOOTSTRAPS.length());
+
+ if (!bootstrapConnected) {
+ FunctionsLogger.info("bootstrapConnect- Connecting to bootstrp " + i);
+ bootNode = String.valueOf(BOOTSTRAPS.get(i));
+ bootNode = bootNode.substring(bootNode.length() - 46);
+ FunctionsLogger.info("bootstrapConnect- trying to connect with " + bootNode);
+
+ multiAddress = new MultiAddress("/ipfs/" + bootNode);
+ output = swarmConnectProcess(multiAddress);
+ FunctionsLogger.info("bootstrapConnect- connection status to " + bootNode + " is " + output);
+ if (output.contains("success")) {
+ FunctionsLogger.info("bootstrapConnect- trying to swarm connect");
+ multiAddress = new MultiAddress("/ipfs/" + bootNode + "/p2p-circuit/ipfs/" + peerid);
+ output = swarmConnectProcess(multiAddress);
+ FunctionsLogger.info("bootstrapConnect- Swarmconnect status is " + output);
+ if (!output.contains("success")) {
+ IPFSNetworkLogger.debug("swarm attempt failed with " + peerid);
+ } else {
+ IPFSNetworkLogger.debug("swarm Connected : " + peerid);
+ bootstrapConnected = true;
+ }
+ } else {
+ IPFSNetworkLogger.debug("bootstrap connection failed! " + bootNode);
+ }
+
+ }
+ }
+
+ } catch (Exception e) {
+ FunctionsLogger.error("Error occured during IPFS Swarm connect with bootstrap", e);
+
+ }
+
+ if (bootstrapConnected) {
+ return true;
+ } else {
+ return false;
+ }
+
+ }
+
+ public static boolean portCheckAndKill(int port) {
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+
+ boolean portStatus = false;
+ long pid = ProcessHandle.current().pid();
+ FunctionsLogger.info("Current OS is " + getOsName() + " and port is " + port);
+ try {
+ if (!getOsName().toLowerCase().contains("windows")) {
+ portStatus = releasePorts(port);
+ } else {
+ portStatus = portStatusWindows(port);
+ }
+ } catch (Exception e) {
+ FunctionsLogger.error("Error occured during port checking ", e);
+ }
+ return portStatus;
+
+ }
+
+ /**
+ * This function will release the port in linux based machines if the port is
+ * already in use
+ */
+ public static boolean releasePorts(int port) {
+ FunctionsLogger.info("releasePorts- ");
+ boolean releasedPort = false;
+ String processStr;
+ Process processId;
+ try {
+ FunctionsLogger.info("inside try of release ports");
+ processId = Runtime.getRuntime().exec("lsof -ti :" + port);
+ long currentPid = ProcessHandle.current().pid();
+ BufferedReader br = new BufferedReader(
+ new InputStreamReader(processId.getInputStream()));
+ processId = Runtime.getRuntime().exec("pgrep ipfs");
+ BufferedReader ipfsPidBr = new BufferedReader(new InputStreamReader(processId.getInputStream()));
+
+ processStr = br.readLine();
+ if (processStr != null) {
+ if (String.valueOf(currentPid) != processStr && ipfsPidBr.readLine() != processStr) {
+
+ processId = Runtime.getRuntime().exec("kill -9 " + processStr);
+
+ }
+ }
+ releasedPort = true;
+ processId.waitFor();
+ processId.destroy();
+ } catch (Exception e) {
+ FunctionsLogger.error("Exception Occured at releasePort", e);
+ e.printStackTrace();
+ }
+ return releasedPort;
+ }
+
+ public static boolean portStatusWindows(int port) {
+ boolean releasedPort = false;
+ String portProcessStr;
+ Process p;
+ ArrayList pidTree = new ArrayList();
+ ArrayList portPidTree = new ArrayList();
+ try {
+ Runtime rt = Runtime.getRuntime();
+ Process getJarPid = rt.exec("cmd /c netstat -ano | findstr 1898");
+ BufferedReader getJarPidBR = new BufferedReader(new InputStreamReader(getJarPid.getInputStream()));
+ String getJarPidline;
+ while ((getJarPidline = getJarPidBR.readLine()) != null) {
+ String[] getJarPidTree = getJarPidline.split("\\s+");
+ int temp = Integer.parseInt(getJarPidTree[getJarPidTree.length - 1]);
+ pidTree.add(temp);
+ }
+
+ Set pidSet = new LinkedHashSet(pidTree);
+ Process getPortPid = rt.exec("cmd /c netstat -ano | findstr " + port);
+ BufferedReader getPortPidBr = new BufferedReader(new InputStreamReader(getPortPid.getInputStream()));
+ String getPortPidLine;
+ while ((getPortPidLine = getPortPidBr.readLine()) != null) {
+ String[] getPortPidTree = getPortPidLine.split("\\s+");
+ int temp = Integer.parseInt(getPortPidTree[getPortPidTree.length - 1]);
+ portPidTree.add(temp);
+ }
+
+ Set pidToKill = new LinkedHashSet(portPidTree);
+ pidToKill.removeAll(pidSet);
+ pidToKill.remove(0);
+ if (pidToKill.size() > 0) {
+ System.out.println("Port " + port + " is occupied by PIDs" + pidToKill);
+ } else {
+ releasedPort = true;
+ }
+
+ } catch (Exception e) {
+ FunctionsLogger.error("Exception occured at portStatusWindows", e);
+ }
+ return releasedPort;
+
+ }
+
+ public static JSONObject verifySpecificCommit(String blockHash) {
+ JSONObject APIResponse = new JSONObject();
+ // String quorumLiStrings[] = null;
+ String transcationID = null;
+ String senderDidIpfsHash = null;
+ String status = null;
+ String blockHashFromChainString = null;
+ FunctionsLogger.debug("data is " + blockHash);
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ // JSONObject detailsObject = new JSONObject(blockHash);
+ // String blockHashValue = detailsObject.getString("blockHash");
+ // FunctionsLogger.debug(blockHashValue);
+ String datumFolderPath = DATUM_CHAIN_PATH;
+ // quorumLiStrings[] =
+ // getValues(datumFolderPath.concat("datumCommitChain.json"), "quorumList",
+ // "blockHash", blockHash).toArray();
+ transcationID = getValues(datumFolderPath.concat("datumCommitChain.json"), "txn", "blockHash", blockHash);
+ senderDidIpfsHash = getValues(datumFolderPath.concat("datumCommitChain.json"), "senderDID", "blockHash",
+ blockHash);
+ blockHashFromChainString = getValues(datumFolderPath.concat("datumCommitChain.json"), "blockHash", "blockHash",
+ blockHash);
+
+ FunctionsLogger.debug("File path is : " + datumFolderPath.concat("datumCommitChain.json"));
+ File datumCommitChain = new File(datumFolderPath.concat("datumCommitChain.json"));
+
+ // FunctionsLogger.debug("quorumLiStrings is "+ quorumLiStrings +"\n
+ // transcationID is "+transcationID
+ // + " \n senderDidIpfsHash "+ senderDidIpfsHash + "\n blockHashFromChainString
+ // is "+blockHashFromChainString);
+
+ APIResponse = new JSONObject();
+
+ // FunctionsLogger.debug("detailsObject is "+ detailsObject.toString());
+ // APIResponse.put("txn", transcationID);
+ // APIResponse.put("Signed Quorums", quorumLiStrings);
+
+ if (getValues(datumFolderPath.concat("datumCommitChain.json"), "blockHash", "blockHash", blockHash)
+ .equals(blockHash)) {
+ FunctionsLogger.debug("BlockHash exists");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", transcationID);
+ APIResponse.put("status", "True");
+ // APIResponse.put("Signed Quorums", quorumLiStrings);
+ APIResponse.put("message", "Block Hash verified");
+ } else {
+ FunctionsLogger.debug("BlockHash doesnt exists");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", transcationID);
+ APIResponse.put("status", "False");
+ // APIResponse.put("Signed Quorums", quorumLiStrings);
+ APIResponse.put("message", "Block Hash doesnt exist");
+ }
+ // APIResponse = new JSONObject();
+
+ return APIResponse;
+
+ }
+
+ public static int multiplePinCheck(String senderDidIpfsHash, JSONObject tokenObject, IPFS ipfs)
+ throws JSONException, InterruptedException {
+ int statusCode = 200;
+ FunctionsLogger.debug("Input tokenObject is " + tokenObject.toString());
+ JSONObject TokenDetails = tokenObject.getJSONObject("tokenDetails");
+ JSONArray wholeTokens = TokenDetails.getJSONArray("whole-tokens");
+ JSONArray wholeTokenChains = TokenDetails.getJSONArray("whole-tokenChains");
+
+ JSONArray partTokens = TokenDetails.getJSONArray("part-tokens");
+ JSONObject partTokenChains = TokenDetails.getJSONObject("part-tokenChains");
+ JSONArray partTokenChainsHash = TokenDetails.getJSONArray("hashSender");
+
+ JSONArray previousSendersArray = tokenObject.getJSONArray("previousSender");
+ JSONArray positionsArray = tokenObject.getJSONArray("positions");
+
+ Double amount = tokenObject.getDouble("amount");
+ JSONObject amountLedger = tokenObject.getJSONObject("amountLedger");
+ FunctionsLogger.debug("Amount Ledger: " + amountLedger);
+ int intPart = wholeTokens.length();
+ // ? multiple pin check starts
+ Double decimalPart = formatAmount(amount - intPart);
+ JSONArray doubleSpentToken = new JSONArray();
+ boolean tokenOwners = true;
+ ArrayList pinOwnersArray = new ArrayList();
+ ArrayList previousSender = new ArrayList();
+ JSONArray ownersReceived = new JSONArray();
+
+ ArrayList ownersArray = new ArrayList();
+ for (int i = 0; i < wholeTokens.length(); ++i) {
+ try {
+ FunctionsLogger.debug("Checking owners for " + wholeTokens.getString(i) +
+ " Please wait...");
+ pinOwnersArray = IPFSNetwork.dhtOwnerCheck(wholeTokens.getString(i));
+
+ if (pinOwnersArray.size() > 2) {
+
+ for (int j = 0; j < previousSendersArray.length(); j++) {
+ if (previousSendersArray.getJSONObject(j).getString("token")
+ .equals(wholeTokens.getString(i)))
+ ownersReceived = previousSendersArray.getJSONObject(j).getJSONArray("sender");
+ }
+
+ for (int j = 0; j < ownersReceived.length(); j++) {
+ previousSender.add(ownersReceived.getString(j));
+ }
+ FunctionsLogger.debug("Previous Owners: " + previousSender);
+
+ for (int j = 0; j < pinOwnersArray.size(); j++) {
+ if (!previousSender.contains(pinOwnersArray.get(j).toString()))
+ tokenOwners = false;
+ }
+ }
+ } catch (IOException e) {
+
+ FunctionsLogger.debug("Ipfs dht find did not execute");
+ }
+ }
+ if (!tokenOwners) {
+ JSONArray owners = new JSONArray();
+ for (int i = 0; i < pinOwnersArray.size(); i++)
+ owners.put(pinOwnersArray.get(i).toString());
+ FunctionsLogger.debug("Multiple Owners for " + doubleSpentToken);
+ FunctionsLogger.debug("Owners: " + owners);
+ statusCode = 420;
+
+ return statusCode;
+ }
+
+ return statusCode;
+ }
+
+ public static HashMap checkTokenHash(HashMap tokenDetailMap, int tokenLimit)
+ throws InterruptedException {
+ HashMap tokenHashWithNumber = new HashMap<>();
+
+ int tokenNumber = -1;
+ try {
+ long start = System.currentTimeMillis();
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
+
+ int flag = -1;
+ for (int i = 1; i <= tokenLimit; i++) {
+ String tokenHashStr = calculateSHA256Hash(digest, String.valueOf(i));
+ for (String tokenHash : tokenDetailMap.keySet()) {
+ if (tokenHash.equals(tokenHashStr)) {
+ tokenNumber = i;
+ flag++;
+ tokenHashWithNumber.put(tokenHash, i);
+ FunctionsLogger.debug("TokenHash is " + tokenHash + " and token number is " + i);
+ }
+
+ }
+ }
+
+ FunctionsLogger.debug("final tokenHashMap is " + tokenHashWithNumber.toString());
+
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return tokenHashWithNumber;
+ }
+
+ private static String calculateSHA256Hash(MessageDigest digest, String message) {
+ byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
+ byte[] c = new byte[messageBytes.length];
+ System.arraycopy(messageBytes, 0, c, 0, messageBytes.length);
+ final byte[] hashBytes = digest.digest(messageBytes);
+ return bytesToHex(hashBytes);
+ }
+
+ public static void ownerIdentity(JSONArray tokens, String receiverDidIpfsHash) {
+ Functions.pathSet();
+
+ try {
+ for (int i = 0; i < tokens.length(); i++) {
+
+ String tokenHash = tokens.getString(i);
+ String hashString = tokenHash.concat(receiverDidIpfsHash);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+ BufferedImage pvt = ImageIO
+ .read(new File(DATA_PATH.concat(receiverDidIpfsHash).concat("/PrivateShare.png")));
+ String firstPrivate = PropImage.img2bin(pvt);
+ int[] privateIntegerArray1 = strToIntArray(firstPrivate);
+ String privateBinary = Functions.intArrayToStr(privateIntegerArray1);
+ String positions = "";
+ for (int j = 0; j < privateIntegerArray1.length; j += 49152) {
+ positions += privateBinary.charAt(j);
+ }
+ String ownerIdentity = hashForPositions.concat(positions);
+ String ownerIdentityHash = calculateHash(ownerIdentity, "SHA3-256");
+ File chainFile = new File(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"));
+ if (chainFile.exists()) {
+ String tokenChainFile = readFile(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"));
+ JSONArray tokenChainArray = new JSONArray(tokenChainFile);
+ JSONObject tokenChainObject = tokenChainArray.getJSONObject(tokenChainArray.length() - 1);
+ tokenChainObject.put("owner", ownerIdentityHash);
+ tokenChainArray.remove(tokenChainArray.length() - 1);
+ tokenChainArray.put(tokenChainObject);
+ writeToFile(TOKENCHAIN_PATH.concat(tokenHash).concat(".json"), tokenChainArray.toString(), false);
+
+ } else {
+ File partChainFile = new File(TOKENCHAIN_PATH.concat("PARTS/").concat(tokenHash).concat(".json"));
+ if (partChainFile.exists()) {
+ String tokenChainFile = readFile(
+ TOKENCHAIN_PATH.concat("PARTS/").concat(tokenHash).concat(".json"));
+ JSONArray tokenChainArray = new JSONArray(tokenChainFile);
+ JSONObject tokenChainObject = tokenChainArray.getJSONObject(tokenChainArray.length() - 1);
+ tokenChainObject.put("owner", ownerIdentityHash);
+ tokenChainArray.remove(tokenChainArray.length() - 1);
+ tokenChainArray.put(tokenChainObject);
+ writeToFile(TOKENCHAIN_PATH.concat("PARTS/").concat(tokenHash).concat(".json"),
+ tokenChainArray.toString(), false);
+
+ } else {
+ FunctionsLogger.info("Token chain file not found for token " + tokenHash);
+ }
+
+ }
+
+ }
+ } catch (Exception e) {
+ FunctionsLogger.error("Exception occured at ownerIdentity", e);
+ }
+
+ }
+
}
diff --git a/src/com/rubix/Resources/IPFSNetwork.java b/src/com/rubix/Resources/IPFSNetwork.java
index 2921d551..9892b039 100644
--- a/src/com/rubix/Resources/IPFSNetwork.java
+++ b/src/com/rubix/Resources/IPFSNetwork.java
@@ -1,22 +1,35 @@
package com.rubix.Resources;
-import io.ipfs.api.IPFS;
-import io.ipfs.api.MerkleNode;
-import io.ipfs.api.NamedStreamable;
-import io.ipfs.multiaddr.MultiAddress;
-import io.ipfs.multihash.Multihash;
-import org.apache.log4j.Logger;
-import org.apache.log4j.PropertyConfigurator;
-import org.json.JSONException;
+import static com.rubix.Constants.IPFSConstants.bootstrap;
+import static com.rubix.Constants.IPFSConstants.daemon;
+import static com.rubix.Constants.IPFSConstants.forward;
+import static com.rubix.Constants.IPFSConstants.listen;
+import static com.rubix.Constants.IPFSConstants.p2p;
+import static com.rubix.Constants.IPFSConstants.shutdown;
+import static com.rubix.Resources.Functions.BOOTSTRAPS;
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.getOsName;
-import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
-import static com.rubix.Constants.IPFSConstants.*;
-import static com.rubix.Resources.Functions.*;
+import javax.imageio.ImageIO;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONException;
+
+import io.ipfs.api.IPFS;
+import io.ipfs.api.MerkleNode;
+import io.ipfs.api.NamedStreamable;
+import io.ipfs.multiaddr.MultiAddress;
+import io.ipfs.multihash.Multihash;
public class IPFSNetwork {
public static int swarmAttempt = 0;
@@ -69,105 +82,109 @@ public static String checkSwarmConnect() {
return response;
}
-// public static void swarmConnector(String peerid, IPFS ipfs) throws JSONException {
-// PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
-// String bootNode;
-// int j;
-//
-// try {
-// if (!checkSwarmConnect().contains(peerid)) {
-// Random ran = new Random();
-//
-// List bootStrapList = ipfs.bootstrap.list();
-// Collections.shuffle(bootStrapList);
-// ran.setSeed(123456);
-// int bootstrapSize = bootStrapList.size();
-//
-// j = ran.nextInt(bootstrapSize);
-// bootNode = String.valueOf(bootStrapList.get(j));
-// bootNode = bootNode.substring(bootNode.length() - 46);
-// IPFSNetworkLogger.debug(bootNode);
-// while (!checkSwarmConnect().contains(bootNode)) {
-// j = (j + 1) % bootstrapSize;
-// bootNode = String.valueOf(bootStrapList.get(j));
-// bootNode = bootNode.substring(bootNode.length() - 46);
-// IPFSNetworkLogger.debug("trying to connect: " + bootNode);
-// }
-// MultiAddress multiAddress = new MultiAddress("/p2p/" + bootNode + "/p2p-circuit/p2p/" + peerid);
-// String output = swarmConnectProcess(multiAddress);
-// if (!output.contains("success"))
-// swarmConnect(peerid);
-// else
-// IPFSNetworkLogger.debug("Connected via bootstrap node: " + bootNode);
-// } else {
-// IPFSNetworkLogger.debug("Connecting to Receiver directly");
-//
-// }
-// } catch (IOException e) {
-// IPFSNetworkLogger.error("IOException Occurred", e);
-// e.printStackTrace();
-// }
-//
-// }
-
-// /**
-// * This method opens a new direct connection to a peer address. The address
-// * format is an IPFS multiaddr. See
-// * ipfs
-// * swarm connect for more
-// *
-// * @param peerid is the multiaddr of the node
-// */
-//
-// public static void swarmConnect(String peerid) throws JSONException {
-// PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
-// String bootNode;
-// int j;
-//
-// IPFSNetworkLogger.debug("at swarmconnect " + peerid);
-// if (!checkSwarmConnect().contains(peerid)) {
-// Random ran = new Random();
-//
-//// List bootStrapList = ipfs.bootstrap.list();
-//// Collections.shuffle(bootStrapList);
-// ran.setSeed(123456);
-// int bootstrapSize = BOOTSTRAPS.length();
-//
-// IPFSNetworkLogger.debug("Bootstraps " + BOOTSTRAPS + "size " + bootstrapSize);
-//
-// j = ran.nextInt(bootstrapSize);
-// bootNode = String.valueOf(BOOTSTRAPS.get(j));
-// bootNode = bootNode.substring(bootNode.length() - 46);
-// IPFSNetworkLogger.debug("bootnode is " + bootNode);
-// IPFSNetworkLogger.debug(bootNode);
-// while (!checkSwarmConnect().contains(bootNode)) {
-// j = (j + 1) % bootstrapSize;
-// bootNode = String.valueOf(BOOTSTRAPS.get(j));
-// bootNode = bootNode.substring(bootNode.length() - 46);
-// IPFSNetworkLogger.debug("trying to connect: " + bootNode);
-// }
-// MultiAddress multiAddress = new MultiAddress("/ipfs/" + bootNode + "/p2p-circuit/ipfs/" + peerid);
-// String output = swarmConnectProcess(multiAddress);
-// if (!output.contains("success")) {
-// if (swarmAttempt < 25) {
-// IPFSNetworkLogger.debug("swarm attempt round " + swarmAttempt);
-// swarmAttempt++;
-// swarmConnect(peerid);
-// } else {
-// IPFSNetworkLogger.debug("swarm attempt failed");
-// swarmAttempt = 0;
-// }
-// } else {
-// IPFSNetworkLogger.debug("Connected via bootstrap node: " + bootNode);
-// swarmAttempt = 0;
-// }
-// } else {
-// IPFSNetworkLogger.debug("Connecting to Receiver directly");
-// swarmAttempt = 0;
-//
-// }
-//
-// }
+ // public static void swarmConnector(String peerid, IPFS ipfs) throws
+ // JSONException {
+ // PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ // String bootNode;
+ // int j;
+ //
+ // try {
+ // if (!checkSwarmConnect().contains(peerid)) {
+ // Random ran = new Random();
+ //
+ // List bootStrapList = ipfs.bootstrap.list();
+ // Collections.shuffle(bootStrapList);
+ // ran.setSeed(123456);
+ // int bootstrapSize = bootStrapList.size();
+ //
+ // j = ran.nextInt(bootstrapSize);
+ // bootNode = String.valueOf(bootStrapList.get(j));
+ // bootNode = bootNode.substring(bootNode.length() - 46);
+ // IPFSNetworkLogger.debug(bootNode);
+ // while (!checkSwarmConnect().contains(bootNode)) {
+ // j = (j + 1) % bootstrapSize;
+ // bootNode = String.valueOf(bootStrapList.get(j));
+ // bootNode = bootNode.substring(bootNode.length() - 46);
+ // IPFSNetworkLogger.debug("trying to connect: " + bootNode);
+ // }
+ // MultiAddress multiAddress = new MultiAddress("/p2p/" + bootNode +
+ // "/p2p-circuit/p2p/" + peerid);
+ // String output = swarmConnectProcess(multiAddress);
+ // if (!output.contains("success"))
+ // swarmConnect(peerid);
+ // else
+ // IPFSNetworkLogger.debug("Connected via bootstrap node: " + bootNode);
+ // } else {
+ // IPFSNetworkLogger.debug("Connecting to Receiver directly");
+ //
+ // }
+ // } catch (IOException e) {
+ // IPFSNetworkLogger.error("IOException Occurred", e);
+ // e.printStackTrace();
+ // }
+ //
+ // }
+
+ // /**
+ // * This method opens a new direct connection to a peer address. The address
+ // * format is an IPFS multiaddr. See
+ // * ipfs
+ // * swarm connect for more
+ // *
+ // * @param peerid is the multiaddr of the node
+ // */
+ //
+ // public static void swarmConnect(String peerid) throws JSONException {
+ // PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+ // String bootNode;
+ // int j;
+ //
+ // IPFSNetworkLogger.debug("at swarmconnect " + peerid);
+ // if (!checkSwarmConnect().contains(peerid)) {
+ // Random ran = new Random();
+ //
+ //// List bootStrapList = ipfs.bootstrap.list();
+ //// Collections.shuffle(bootStrapList);
+ // ran.setSeed(123456);
+ // int bootstrapSize = BOOTSTRAPS.length();
+ //
+ // IPFSNetworkLogger.debug("Bootstraps " + BOOTSTRAPS + "size " +
+ // bootstrapSize);
+ //
+ // j = ran.nextInt(bootstrapSize);
+ // bootNode = String.valueOf(BOOTSTRAPS.get(j));
+ // bootNode = bootNode.substring(bootNode.length() - 46);
+ // IPFSNetworkLogger.debug("bootnode is " + bootNode);
+ // IPFSNetworkLogger.debug(bootNode);
+ // while (!checkSwarmConnect().contains(bootNode)) {
+ // j = (j + 1) % bootstrapSize;
+ // bootNode = String.valueOf(BOOTSTRAPS.get(j));
+ // bootNode = bootNode.substring(bootNode.length() - 46);
+ // IPFSNetworkLogger.debug("trying to connect: " + bootNode);
+ // }
+ // MultiAddress multiAddress = new MultiAddress("/ipfs/" + bootNode +
+ // "/p2p-circuit/ipfs/" + peerid);
+ // String output = swarmConnectProcess(multiAddress);
+ // if (!output.contains("success")) {
+ // if (swarmAttempt < 25) {
+ // IPFSNetworkLogger.debug("swarm attempt round " + swarmAttempt);
+ // swarmAttempt++;
+ // swarmConnect(peerid);
+ // } else {
+ // IPFSNetworkLogger.debug("swarm attempt failed");
+ // swarmAttempt = 0;
+ // }
+ // } else {
+ // IPFSNetworkLogger.debug("Connected via bootstrap node: " + bootNode);
+ // swarmAttempt = 0;
+ // }
+ // } else {
+ // IPFSNetworkLogger.debug("Connecting to Receiver directly");
+ // swarmAttempt = 0;
+ //
+ // }
+ //
+ // }
/**
* This function connects the peer node through the private swarm
@@ -365,7 +382,6 @@ public static ArrayList dhtOwnerCheck(String objectHash) throws InterruptedExcep
commands[2] = "ipfs dht findprovs " + objectHash;
}
-
ProcessBuilder p = new ProcessBuilder(commands);
Process process = p.start();
@@ -378,7 +394,6 @@ public static ArrayList dhtOwnerCheck(String objectHash) throws InterruptedExcep
array.add(line);
process.waitFor();
-
return array;
}
@@ -452,7 +467,8 @@ public static String executeIPFSCommandsResponse(String command) {
}
if (command.contains(listen) || command.contains(forward) || command.contains("swarm")
- || command.contains(p2p) || command.contains(shutdown) || command.contains(bootstrap) || command.contains("findprovs")) {
+ || command.contains(p2p) || command.contains(shutdown) || command.contains(bootstrap)
+ || command.contains("findprovs")) {
p = new ProcessBuilder(commands);
process = p.start();
@@ -544,7 +560,7 @@ public static void executeIPFSCommands(String command) {
}
}
- public static void swarmConnectP2P(String peerid, IPFS ipfs) throws JSONException {
+ public static boolean swarmConnectP2P(String peerid, IPFS ipfs) throws JSONException {
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
String bootNode;
boolean swarmConnected = false;
@@ -560,26 +576,31 @@ public static void swarmConnectP2P(String peerid, IPFS ipfs) throws JSONExceptio
if (!swarmConnected) {
bootNode = String.valueOf(BOOTSTRAPS.get(i));
bootNode = bootNode.substring(bootNode.length() - 46);
-
+
multiAddress = new MultiAddress("/ipfs/" + bootNode);
output = swarmConnectProcess(multiAddress);
-
+
if (output.contains("success")) {
- multiAddress = new MultiAddress("/ipfs/" + bootNode + "/p2p-circuit/ipfs/" + peerid);
- output = swarmConnectProcess(multiAddress);
- if (!output.contains("success")) {
- IPFSNetworkLogger.debug("swarm attempt failed with " + peerid);
- } else {
- IPFSNetworkLogger.debug("swarm Connected : " + peerid);
- swarmConnected = true;
- }
+ multiAddress = new MultiAddress("/ipfs/" + bootNode + "/p2p-circuit/ipfs/" + peerid);
+ output = swarmConnectProcess(multiAddress);
+ if (!output.contains("success")) {
+ IPFSNetworkLogger.debug("swarm attempt failed with " + peerid);
+ swarmConnected = false;
+ } else {
+ IPFSNetworkLogger.debug("swarm Connected : " + peerid);
+ swarmConnected = true;
+ }
} else {
- IPFSNetworkLogger.debug("bootstrap connection failed! " + bootNode);
- }
+ IPFSNetworkLogger.debug("bootstrap connection failed! " + bootNode);
+ swarmConnected = false;
+ }
}
}
+ } else {
+ swarmConnected = true;
}
+ return swarmConnected;
}
diff --git a/src/com/rubix/TokenTransfer/TokenReceiver.java b/src/com/rubix/TokenTransfer/TokenReceiver.java
index 3707abf2..78d6cff8 100644
--- a/src/com/rubix/TokenTransfer/TokenReceiver.java
+++ b/src/com/rubix/TokenTransfer/TokenReceiver.java
@@ -1,33 +1,7 @@
package com.rubix.TokenTransfer;
-import static com.rubix.Resources.Functions.DATA_PATH;
-import static com.rubix.Resources.Functions.FunctionsLogger;
-import static com.rubix.Resources.Functions.IPFS_PORT;
-import static com.rubix.Resources.Functions.LOGGER_PATH;
-import static com.rubix.Resources.Functions.PAYMENTS_PATH;
-import static com.rubix.Resources.Functions.RECEIVER_PORT;
-import static com.rubix.Resources.Functions.TOKENCHAIN_PATH;
-import static com.rubix.Resources.Functions.TOKENS_PATH;
-import static com.rubix.Resources.Functions.WALLET_DATA_PATH;
-import static com.rubix.Resources.Functions.calculateHash;
-import static com.rubix.Resources.Functions.deleteFile;
-import static com.rubix.Resources.Functions.formatAmount;
-import static com.rubix.Resources.Functions.getCurrentUtcTime;
-import static com.rubix.Resources.Functions.getPeerID;
-import static com.rubix.Resources.Functions.getValues;
-import static com.rubix.Resources.Functions.nodeData;
-import static com.rubix.Resources.Functions.pathSet;
-import static com.rubix.Resources.Functions.readFile;
-import static com.rubix.Resources.Functions.syncDataTable;
-import static com.rubix.Resources.Functions.updateJSON;
-import static com.rubix.Resources.Functions.writeToFile;
-import static com.rubix.Resources.IPFSNetwork.add;
-import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
-import static com.rubix.Resources.IPFSNetwork.get;
-import static com.rubix.Resources.IPFSNetwork.listen;
-import static com.rubix.Resources.IPFSNetwork.pin;
-import static com.rubix.Resources.IPFSNetwork.repo;
-import static com.rubix.Resources.IPFSNetwork.swarmConnectP2P;
+import static com.rubix.Resources.Functions.*;
+import static com.rubix.Resources.IPFSNetwork.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
@@ -39,15 +13,23 @@
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.Random;
import javax.imageio.ImageIO;
import com.rubix.AuthenticateNode.Authenticate;
import com.rubix.AuthenticateNode.PropImage;
+import com.rubix.Ping.VerifyStakedToken;
import com.rubix.Resources.Functions;
import com.rubix.Resources.IPFSNetwork;
+import com.rubix.Constants.MiningConstants.*;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
@@ -55,744 +37,1186 @@
import org.json.JSONException;
import org.json.JSONObject;
+import com.rubix.Constants.*;
+
import io.ipfs.api.IPFS;
public class TokenReceiver {
- public static Logger TokenReceiverLogger = Logger.getLogger(TokenReceiver.class);
-
- private static final JSONObject APIResponse = new JSONObject();
- private static IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
- private static String SenWalletBin;
-
- /**
- * Receiver Node: To receive a valid token from an authentic sender
- *
- * @return Transaction Details (JSONObject)
- * @throws IOException handles IO Exceptions
- * @throws JSONException handles JSON Exceptions
- */
- public static String receive() {
- pathSet();
- ServerSocket ss = null;
- Socket sk = null;
- String senderPeerID = null;
-
- try {
- repo(ipfs);
- String PART_TOKEN_CHAIN_PATH = TOKENCHAIN_PATH.concat("PARTS/");
- String PART_TOKEN_PATH = TOKENS_PATH.concat("PARTS/");
- File partFolder = new File(PART_TOKEN_PATH);
- if (!partFolder.exists())
- partFolder.mkdir();
- partFolder = new File(PART_TOKEN_CHAIN_PATH);
- if (!partFolder.exists())
- partFolder.mkdir();
- File partTokensFile = new File(PAYMENTS_PATH.concat("PartsToken.json"));
- if (!partTokensFile.exists()) {
- partTokensFile.createNewFile();
- writeToFile(partTokensFile.toString(), "[]", false);
- }
-
- int quorumSignVerifyCount = 0;
- JSONObject quorumSignatures = null;
-
- ArrayList quorumDID = new ArrayList<>();
- PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
-
- String receiverPeerID = getPeerID(DATA_PATH + "DID.json");
-
- String receiverDidIpfsHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", receiverPeerID);
-
- listen(receiverPeerID, RECEIVER_PORT);
- ss = new ServerSocket(RECEIVER_PORT);
- TokenReceiverLogger.debug("Receiver Listening on " + RECEIVER_PORT + " appname " + receiverPeerID);
-
- sk = ss.accept();
- BufferedReader input = new BufferedReader(new InputStreamReader(sk.getInputStream()));
- PrintStream output = new PrintStream(sk.getOutputStream());
- long startTime = System.currentTimeMillis();
-
- try {
- senderPeerID = input.readLine();
- } catch (SocketException e) {
- TokenReceiverLogger.warn("Sender Stream Null - Sender Details");
- APIResponse.put("did", "");
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender Stream Null - Sender Details");
-
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- }
- syncDataTable(null, senderPeerID);
-
- swarmConnectP2P(senderPeerID, ipfs);
-
- String senderDidIpfsHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", senderPeerID);
- String senderWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid", senderPeerID);
-
- if (!(senderDidIpfsHash.contains("Qm") && senderWidIpfsHash.contains("Qm"))) {
- output.println("420");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender details not available in network , please sync");
- TokenReceiverLogger.info("Sender details not available in datatable");
-
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
- }
-
- nodeData(senderDidIpfsHash, senderWidIpfsHash, ipfs);
- File senderDIDFile = new File(DATA_PATH + senderDidIpfsHash + "/DID.png");
- if (!senderDIDFile.exists()) {
- output.println("420");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender details not available");
- TokenReceiverLogger.info("Sender details not available");
- /* executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID); */
-
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
- }
- TokenReceiverLogger.debug("Sender details authenticated");
- output.println("200");
-
- String tokenDetails;
- try {
- tokenDetails = input.readLine();
- } catch (SocketException e) {
- TokenReceiverLogger.warn("Sender Stream Null - Token Details");
- APIResponse.put("did", "");
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender Stream Null - Token Details");
-
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- }
- JSONObject tokenObject = new JSONObject(tokenDetails);
- JSONObject TokenDetails = tokenObject.getJSONObject("tokenDetails");
- JSONArray wholeTokens = TokenDetails.getJSONArray("whole-tokens");
- JSONArray wholeTokenChains = TokenDetails.getJSONArray("whole-tokenChains");
-
- JSONArray partTokens = TokenDetails.getJSONArray("part-tokens");
- JSONObject partTokenChains = TokenDetails.getJSONObject("part-tokenChains");
- JSONArray partTokenChainsHash = TokenDetails.getJSONArray("hashSender");
-
- JSONArray previousSendersArray = tokenObject.getJSONArray("previousSender");
- Double amount = tokenObject.getDouble("amount");
- JSONObject amountLedger = tokenObject.getJSONObject("amountLedger");
- TokenReceiverLogger.debug("Amount Ledger: " + amountLedger);
- int intPart = wholeTokens.length();
- Double decimalPart = formatAmount(amount - intPart);
- JSONArray doubleSpentToken = new JSONArray();
- boolean tokenOwners = true;
- ArrayList ownersArray = new ArrayList();
- ArrayList previousSender = new ArrayList();
- JSONArray ownersReceived = new JSONArray();
- for (int i = 0; i < wholeTokens.length(); ++i) {
- try {
- TokenReceiverLogger.debug("Checking owners for " + wholeTokens.getString(i) + " Please wait...");
- ownersArray = IPFSNetwork.dhtOwnerCheck(wholeTokens.getString(i));
-
- if (ownersArray.size() > 2) {
-
- for (int j = 0; j < previousSendersArray.length(); j++) {
- if (previousSendersArray.getJSONObject(j).getString("token")
- .equals(wholeTokens.getString(i)))
- ownersReceived = previousSendersArray.getJSONObject(j).getJSONArray("sender");
- }
-
- for (int j = 0; j < ownersReceived.length(); j++) {
- previousSender.add(ownersReceived.getString(j));
- }
- TokenReceiverLogger.debug("Previous Owners: " + previousSender);
-
- for (int j = 0; j < ownersArray.size(); j++) {
- if (!previousSender.contains(ownersArray.get(j).toString()))
- tokenOwners = false;
- }
- }
- } catch (IOException e) {
-
- TokenReceiverLogger.debug("Ipfs dht find did not execute");
- }
- }
- if (!tokenOwners) {
- JSONArray owners = new JSONArray();
- for (int i = 0; i < ownersArray.size(); i++)
- owners.put(ownersArray.get(i).toString());
- TokenReceiverLogger.debug("Multiple Owners for " + doubleSpentToken);
- TokenReceiverLogger.debug("Owners: " + owners);
- output.println("200");
- output.println(doubleSpentToken.toString());
- output.println(owners.toString());
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Multiple Owners for " + doubleSpentToken + " " + owners);
- IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
- }
-
- String senderToken = TokenDetails.toString();
- String consensusID = calculateHash(senderToken, "SHA3-256");
- writeToFile(LOGGER_PATH + "consensusID", consensusID, false);
- String consensusIDIPFSHash = IPFSNetwork.addHashOnly(LOGGER_PATH + "consensusID", ipfs);
- deleteFile(LOGGER_PATH + "consensusID");
-
- if (!IPFSNetwork.dhtEmpty(consensusIDIPFSHash, ipfs)) {
- TokenReceiverLogger.debug("consensus ID not unique" + consensusIDIPFSHash);
- output.println("200");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Consensus ID not unique");
- IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
- }
-
- // Check IPFS get and Stake ID (signed when participated as quorum in mining)
- // for all Tokens
- int ipfsGetFlag = 0;
- ArrayList allTokenContent = new ArrayList<>();
- ArrayList allTokenChainContent = new ArrayList<>();
- for (int i = 0; i < intPart; i++) {
- String TokenChainContent = get(wholeTokenChains.getString(i), ipfs);
- allTokenChainContent.add(TokenChainContent);
- String TokenContent = get(wholeTokens.getString(i), ipfs);
-
- // ! yet to verify if we are going to use stake ID or quorum pinning token on
-
- // ? Steps
- // ? generate stake ID for the token hash
- // ? check if stake ID exists in the network for the token hash (someone must
- // have staked this token for participating in mining)
- // ? stake ID should be signed by the staking node to avoid malicious node
- // staking tokens they dont own
- // ? get mined token from stake ID
- // ? if the mined token has token chain content > credits required to mine that
- // token, then stake ID is not considered and token is accepted by reciever
- // ? ignore the token and continue with the transaction
-
- allTokenContent.add(TokenContent);
- ipfsGetFlag++;
- }
- repo(ipfs);
-
- if (!(ipfsGetFlag == intPart)) {
- output.println("422");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Tokens not verified");
- TokenReceiverLogger.info("Tokens not verified");
- IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
- }
-
- JSONArray partTokenChainContent = new JSONArray();
- JSONArray partTokenContent = new JSONArray();
-
- for (int i = 0; i < partTokenChains.length(); i++) {
-
- partTokenChainContent.put(partTokenChains.getJSONArray(partTokens.getString(i)));
- String TokenContent = get(partTokens.getString(i), ipfs);
- partTokenContent.put(TokenContent);
- }
-
- boolean chainFlag = true;
- for (int i = 0; i < partTokenChainContent.length(); i++) {
- JSONArray tokenChainContent = partTokenChainContent.getJSONArray(i);
- for (int j = 0; j < tokenChainContent.length(); j++) {
- String previousHash = tokenChainContent.getJSONObject(j).getString("previousHash");
- String nextHash = tokenChainContent.getJSONObject(j).getString("nextHash");
- String rePreviousHash, reNextHash;
- if (tokenChainContent.length() > 1) {
- if (j == 0) {
- rePreviousHash = "";
- String rePrev = calculateHash(new JSONObject().toString(), "SHA3-256");
- reNextHash = calculateHash(tokenChainContent.getJSONObject(j + 1).getString("tid"),
- "SHA3-256");
-
- if (!((rePreviousHash.equals(previousHash) || rePrev.equals(previousHash))
- && reNextHash.equals(nextHash))) {
- chainFlag = false;
- }
-
- } else if (j == tokenChainContent.length() - 1) {
- rePreviousHash = calculateHash(tokenChainContent.getJSONObject(j - 1).getString("tid"),
- "SHA3-256");
- reNextHash = "";
-
- if (!(rePreviousHash.equals(previousHash) && reNextHash.equals(nextHash))) {
- chainFlag = false;
- }
-
- } else {
- rePreviousHash = calculateHash(tokenChainContent.getJSONObject(j - 1).getString("tid"),
- "SHA3-256");
- reNextHash = calculateHash(tokenChainContent.getJSONObject(j + 1).getString("tid"),
- "SHA3-256");
-
- if (!(rePreviousHash.equals(previousHash) && reNextHash.equals(nextHash))) {
- chainFlag = false;
- }
- }
- }
- }
- }
- if (!chainFlag) {
-
- String errorMessage = "Broken Cheque Chain";
- output.println("423");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", errorMessage);
- TokenReceiverLogger.debug(errorMessage);
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
- }
-
- boolean partsAvailable = true;
- for (int i = 0; i < partTokenChainContent.length(); i++) {
- Double senderCount = 0.000D, receiverCount = 0.000D;
- JSONArray tokenChainContent = partTokenChainContent.getJSONArray(i);
- for (int k = 0; k < tokenChainContent.length(); k++) {
- if (tokenChainContent.getJSONObject(k).has("role")) {
- if (tokenChainContent.getJSONObject(k).getString("role").equals("Sender")
- && tokenChainContent.getJSONObject(k).getString("sender").equals(senderDidIpfsHash)) {
- senderCount += tokenChainContent.getJSONObject(k).getDouble("amount");
- } else if (tokenChainContent.getJSONObject(k).getString("role").equals("Receiver")
- && tokenChainContent.getJSONObject(k).getString("receiver").equals(senderDidIpfsHash)) {
- receiverCount += tokenChainContent.getJSONObject(k).getDouble("amount");
- }
- }
- }
- FunctionsLogger.debug("Sender Parts: " + formatAmount(senderCount));
- FunctionsLogger.debug("Receiver Parts: " + formatAmount(receiverCount));
- Double availableParts = receiverCount - senderCount;
-
- availableParts = formatAmount(availableParts);
- availableParts += amountLedger.getDouble(partTokens.getString(i));
- availableParts = formatAmount(availableParts);
-
- if (availableParts > 1.000D) {
- TokenReceiverLogger.debug("Token wholly spent: " + partTokens.getString(i));
- TokenReceiverLogger.debug("Parts: " + availableParts);
- }
- }
-
- if (!partsAvailable) {
- String errorMessage = "Token wholly spent already";
- output.println("424");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", errorMessage);
- TokenReceiverLogger.debug(errorMessage);
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
- } else
- output.println("200");
-
- String senderDetails;
- try {
- senderDetails = input.readLine();
- } catch (SocketException e) {
- TokenReceiverLogger.warn("Sender Stream Null - Sender Details");
- APIResponse.put("did", "");
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender Stream Null - Sender Details");
-
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- }
- JSONObject SenderDetails = new JSONObject(senderDetails);
- String senderSignature = SenderDetails.getString("sign");
- String tid = SenderDetails.getString("tid");
- String comment = SenderDetails.getString("comment");
- String Status = SenderDetails.getString("status");
- String QuorumDetails = SenderDetails.getString("quorumsign");
-
- BufferedImage senderWidImage = ImageIO.read(new File(DATA_PATH + senderDidIpfsHash + "/PublicShare.png"));
- SenWalletBin = PropImage.img2bin(senderWidImage);
-
- TokenReceiverLogger.debug("Verifying Quorum ... ");
- TokenReceiverLogger.debug("Please wait, this might take a few seconds");
-
- if (!Status.equals("Consensus Failed")) {
- boolean yesQuorum = false;
- if (Status.equals("Consensus Reached")) {
- quorumSignatures = new JSONObject(QuorumDetails);
- String selectQuorumHash = calculateHash(senderToken, "SHA3-256");
- String verifyQuorumHash = calculateHash(selectQuorumHash.concat(receiverDidIpfsHash), "SHA3-256");
-
- Iterator keys = quorumSignatures.keys();
- while (keys.hasNext()) {
- String key = keys.next();
- quorumDID.add(key);
- }
-
- for (String quorumDidIpfsHash : quorumDID) {
- syncDataTable(quorumDidIpfsHash, null);
- String quorumWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "didHash",
- quorumDidIpfsHash);
-
- nodeData(quorumDidIpfsHash, quorumWidIpfsHash, ipfs);
- }
-
- for (int i = 0; i < quorumSignatures.length(); i++) {
-
- JSONObject detailsForVerify = new JSONObject();
- detailsForVerify.put("did", quorumDID.get(i));
- detailsForVerify.put("hash", verifyQuorumHash);
- detailsForVerify.put("signature", quorumSignatures.getString(quorumDID.get(i)));
- boolean val = Authenticate.verifySignature(detailsForVerify.toString());
- if (val)
- quorumSignVerifyCount++;
- }
- TokenReceiverLogger.debug("Verified Quorum Count " + quorumSignVerifyCount);
- yesQuorum = quorumSignVerifyCount >= quorumSignatures.length();
- }
- JSONArray wholeTokenChainHash = new JSONArray();
- for (int i = 0; i < intPart; i++)
- wholeTokenChainHash.put(wholeTokenChains.getString(i));
-
- String hash = calculateHash(
- wholeTokens.toString() + wholeTokenChainHash.toString() + partTokens.toString()
- + partTokenChainsHash.toString() + receiverDidIpfsHash + senderDidIpfsHash + comment,
- "SHA3-256");
- TokenReceiverLogger.debug("Hash to verify Sender: " + hash);
- JSONObject detailsForVerify = new JSONObject();
- detailsForVerify.put("did", senderDidIpfsHash);
- detailsForVerify.put("hash", hash);
- detailsForVerify.put("signature", senderSignature);
-
- boolean yesSender = Authenticate.verifySignature(detailsForVerify.toString());
- TokenReceiverLogger.debug("Quorum Auth : " + yesQuorum + " Sender Auth : " + yesSender);
- if (!(yesSender && yesQuorum)) {
- output.println("420");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", tid);
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender / Quorum not verified");
- TokenReceiverLogger.info("Sender / Quorum not verified");
- IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
- }
-
- repo(ipfs);
- TokenReceiverLogger.debug("Sender and Quorum Verified");
- output.println("200");
-
- String pinDetails;
- try {
- pinDetails = input.readLine();
- } catch (SocketException e) {
- TokenReceiverLogger.warn("Sender Stream Null - Pinning Status");
- APIResponse.put("did", "");
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender Stream Null - Pinning Status");
-
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- }
- if (pinDetails.equals("Unpinned")) {
- int count = 0;
- for (int i = 0; i < intPart; i++) {
- FileWriter fileWriter;
- fileWriter = new FileWriter(TOKENS_PATH + wholeTokens.getString(i));
- fileWriter.write(allTokenContent.get(i));
- fileWriter.close();
- add(TOKENS_PATH + wholeTokens.getString(i), ipfs);
- pin(wholeTokens.getString(i), ipfs);
- count++;
-
- }
-
- for (int i = 0; i < partTokens.length(); i++) {
- File tokenFile = new File(PART_TOKEN_PATH + partTokens.getString(i));
- if (!tokenFile.exists())
- tokenFile.createNewFile();
- FileWriter fileWriter;
- fileWriter = new FileWriter(PART_TOKEN_PATH + partTokens.getString(i));
- fileWriter.write(partTokenContent.getString(i));
- fileWriter.close();
- String tokenHash = add(PART_TOKEN_PATH + partTokens.getString(i), ipfs);
- pin(tokenHash, ipfs);
-
- }
-
- if (count == intPart) {
- TokenReceiverLogger.debug("Pinned All Tokens");
- output.println("Successfully Pinned");
-
- String essentialShare;
- try {
- essentialShare = input.readLine();
- } catch (SocketException e) {
- TokenReceiverLogger.warn("Sender Stream Null - EShare Details");
- APIResponse.put("did", "");
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender Stream Null - EShare Details");
-
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- }
- long endTime = System.currentTimeMillis();
-
- for (int i = 0; i < intPart; i++) {
-
- ArrayList groupTokens = new ArrayList<>();
- for (int k = 0; k < intPart; k++) {
- if (!wholeTokens.getString(i).equals(wholeTokens.getString(k)))
- groupTokens.add(wholeTokens.getString(k));
- }
-
- JSONArray arrToken = new JSONArray();
- JSONObject objectToken = new JSONObject();
- objectToken.put("tokenHash", wholeTokens.getString(i));
- arrToken.put(objectToken);
- JSONArray arr1 = new JSONArray(allTokenChainContent.get(i));
- JSONObject obj2 = new JSONObject();
- obj2.put("senderSign", senderSignature);
- obj2.put("sender", senderDidIpfsHash);
- obj2.put("group", groupTokens);
- obj2.put("comment", comment);
- obj2.put("tid", tid);
- arr1.put(obj2);
- writeToFile(TOKENCHAIN_PATH + wholeTokens.getString(i) + ".json", arr1.toString(), false);
- }
-
- for (int i = 0; i < partTokens.length(); i++) {
- JSONObject chequeObject = new JSONObject();
- chequeObject.put("sender", senderDidIpfsHash);
- chequeObject.put("receiver", receiverDidIpfsHash);
- chequeObject.put("parent-token", partTokens.getString(i));
- chequeObject.put("parent-chain", partTokenChains.getJSONArray(partTokens.getString(i)));
- Double partAmount = formatAmount(amountLedger.getDouble(partTokens.getString(i)));
- chequeObject.put("amount", partAmount);
- chequeObject.put("tid", tid);
-
- writeToFile(LOGGER_PATH.concat(partTokens.getString(i)), chequeObject.toString(), false);
- String chequeHash = IPFSNetwork.add(LOGGER_PATH.concat(partTokens.getString(i)), ipfs);
- deleteFile(LOGGER_PATH.concat(partTokens.getString(i)));
-
- JSONObject newPartObject = new JSONObject();
- newPartObject.put("senderSign", senderSignature);
- newPartObject.put("sender", senderDidIpfsHash);
- newPartObject.put("receiver", receiverDidIpfsHash);
- newPartObject.put("comment", comment);
- newPartObject.put("tid", tid);
- newPartObject.put("nextHash", "");
- if (partTokenChainContent.getJSONArray(i).length() == 0)
- newPartObject.put("previousHash", "");
- else
- newPartObject.put("previousHash",
- calculateHash(partTokenChainContent.getJSONArray(i)
- .getJSONObject(partTokenChainContent.getJSONArray(i).length() - 1)
- .getString("tid"), "SHA3-256"));
-
- newPartObject.put("amount", partAmount);
- newPartObject.put("cheque", chequeHash);
- newPartObject.put("role", "Receiver");
-
- File chainFile = new File(
- PART_TOKEN_CHAIN_PATH.concat(partTokens.getString(i)).concat(".json"));
- if (chainFile.exists()) {
-
- String readChain = readFile(PART_TOKEN_CHAIN_PATH + partTokens.getString(i) + ".json");
- JSONArray readChainArray = new JSONArray(readChain);
- readChainArray.put(partTokenChainContent.getJSONArray(i)
- .getJSONObject(partTokenChainContent.getJSONArray(i).length() - 1));
- readChainArray.put(newPartObject);
-
- writeToFile(PART_TOKEN_CHAIN_PATH + partTokens.getString(i) + ".json",
- readChainArray.toString(), false);
-
- } else {
- partTokenChainContent.getJSONArray(i).put(newPartObject);
- writeToFile(PART_TOKEN_CHAIN_PATH + partTokens.getString(i) + ".json",
- partTokenChainContent.getJSONArray(i).toString(), false);
- }
- }
-
- JSONObject transactionRecord = new JSONObject();
- transactionRecord.put("role", "Receiver");
- transactionRecord.put("tokens", wholeTokens);
- transactionRecord.put("txn", tid);
- transactionRecord.put("quorumList", quorumSignatures.keys());
- transactionRecord.put("senderDID", senderDidIpfsHash);
- transactionRecord.put("receiverDID", receiverDidIpfsHash);
- transactionRecord.put("Date", getCurrentUtcTime());
- transactionRecord.put("totalTime", (endTime - startTime));
- transactionRecord.put("comment", comment);
- transactionRecord.put("essentialShare", essentialShare);
- amount = formatAmount(amount);
- transactionRecord.put("amount-received", amount);
-
- JSONArray transactionHistoryEntry = new JSONArray();
- transactionHistoryEntry.put(transactionRecord);
- updateJSON("add", WALLET_DATA_PATH + "TransactionHistory.json",
- transactionHistoryEntry.toString());
-
- for (int i = 0; i < wholeTokens.length(); i++) {
- String bankFile = readFile(PAYMENTS_PATH.concat("BNK00.json"));
- JSONArray bankArray = new JSONArray(bankFile);
- JSONObject tokenObject1 = new JSONObject();
- tokenObject1.put("tokenHash", wholeTokens.getString(i));
- bankArray.put(tokenObject1);
- Functions.writeToFile(PAYMENTS_PATH.concat("BNK00.json"), bankArray.toString(), false);
-
- }
-
- String partsFile = readFile(PAYMENTS_PATH.concat("PartsToken.json"));
- JSONArray partsReadArray = new JSONArray(partsFile);
-
- for (int i = 0; i < partTokens.length(); i++) {
- boolean writeParts = true;
- for (int j = 0; j < partsReadArray.length(); j++) {
- if (partsReadArray.getJSONObject(j).getString("tokenHash")
- .equals(partTokens.getString(i)))
- writeParts = false;
- }
- if (writeParts) {
- JSONObject partObject = new JSONObject();
- partObject.put("tokenHash", partTokens.getString(i));
- partsReadArray.put(partObject);
- }
- }
- writeToFile(PAYMENTS_PATH.concat("PartsToken.json"), partsReadArray.toString(), false);
-
- TokenReceiverLogger.info("Transaction ID: " + tid + "Transaction Successful");
- output.println("Send Response");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", tid);
- APIResponse.put("status", "Success");
- APIResponse.put("tokens", wholeTokens);
- APIResponse.put("comment", comment);
- APIResponse.put("message", "Transaction Successful");
- TokenReceiverLogger.info(" Transaction Successful");
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- }
- output.println("count mistmatch");
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "count mismacth");
- TokenReceiverLogger.info(" Transaction failed");
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- }
-
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Failed to unpin");
- TokenReceiverLogger.info(" Transaction failed");
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- }
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", "null");
- APIResponse.put("status", "Failed");
- APIResponse.put("message", "Consensus failed at Sender side");
- TokenReceiverLogger.info(" Transaction failed");
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- output.close();
- input.close();
- sk.close();
- ss.close();
- return APIResponse.toString();
-
- } catch (Exception e) {
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- TokenReceiverLogger.error("Exception Occurred", e);
- return APIResponse.toString();
- } finally {
- try {
- ss.close();
- sk.close();
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- } catch (Exception e) {
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
- TokenReceiverLogger.error("Exception Occurred", e);
- }
-
- }
- }
+ public static Logger TokenReceiverLogger = Logger.getLogger(TokenReceiver.class);
+
+ private static final JSONObject APIResponse = new JSONObject();
+ private static IPFS ipfs = new IPFS("/ip4/127.0.0.1/tcp/" + IPFS_PORT);
+ private static String SenWalletBin;
+
+ // token limit for each level
+ private static final int[] tokenLimit = { 0, 5000000, 2425000, 2303750, 2188563, 2079134, 1975178, 1876419, 1782598,
+ 1693468, 1608795, 1528355, 1451937, 1379340 };
+
+ /**
+ * Receiver Node: To receive a valid token from an authentic sender
+ *
+ * @return Transaction Details (JSONObject)
+ * @throws IOException handles IO Exceptions
+ * @throws JSONException handles JSON Exceptions
+ */
+ public static String receive() {
+ pathSet();
+ ServerSocket ss = null;
+ Socket sk = null;
+ String senderPeerID = null;
+
+ try {
+ repo(ipfs);
+ String PART_TOKEN_CHAIN_PATH = TOKENCHAIN_PATH.concat("PARTS/");
+ String PART_TOKEN_PATH = TOKENS_PATH.concat("PARTS/");
+ File partFolder = new File(PART_TOKEN_PATH);
+ if (!partFolder.exists())
+ partFolder.mkdir();
+ partFolder = new File(PART_TOKEN_CHAIN_PATH);
+ if (!partFolder.exists())
+ partFolder.mkdir();
+ File partTokensFile = new File(PAYMENTS_PATH.concat("PartsToken.json"));
+ if (!partTokensFile.exists()) {
+ partTokensFile.createNewFile();
+ writeToFile(partTokensFile.toString(), "[]", false);
+ }
+
+ int quorumSignVerifyCount = 0;
+ JSONObject quorumSignatures = null;
+
+ ArrayList quorumDID = new ArrayList<>();
+ PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
+
+ String receiverPeerID = getPeerID(DATA_PATH + "DID.json");
+
+ String receiverDidIpfsHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", receiverPeerID);
+
+ listen(receiverPeerID, RECEIVER_PORT);
+ ss = new ServerSocket(RECEIVER_PORT);
+ TokenReceiverLogger.debug("Receiver Listening on " + RECEIVER_PORT + " appname " + receiverPeerID);
+
+ sk = ss.accept();
+ TokenReceiverLogger.debug("Data Incoming...");
+ BufferedReader input = new BufferedReader(new InputStreamReader(sk.getInputStream()));
+ PrintStream output = new PrintStream(sk.getOutputStream());
+ long startTime = System.currentTimeMillis();
+
+ try {
+ senderPeerID = input.readLine();
+ } catch (SocketException e) {
+ TokenReceiverLogger.warn("Sender Stream Null - Sender Details");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - Sender Details");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+
+ TokenReceiverLogger.debug("Data Received: " + senderPeerID);
+ swarmConnectP2P(senderPeerID, ipfs);
+
+ String senderDidIpfsHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", senderPeerID);
+ String senderWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "peerid", senderPeerID);
+
+ if (!(senderDidIpfsHash.contains("Qm") && senderWidIpfsHash.contains("Qm"))) {
+ output.println("420");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender details not available in network , please sync");
+ TokenReceiverLogger.info("Sender details not available in datatable");
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+
+ nodeData(senderDidIpfsHash, senderWidIpfsHash, ipfs);
+ File senderDIDFile = new File(DATA_PATH + senderDidIpfsHash + "/DID.png");
+ if (!senderDIDFile.exists()) {
+ output.println("420");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender details not available");
+ TokenReceiverLogger.info("Sender details not available");
+ /* executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID); */
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+ TokenReceiverLogger.debug("Sender details authenticated");
+ output.println("200");
+
+ String tokenDetails;
+ try {
+ tokenDetails = input.readLine();
+ } catch (SocketException e) {
+ TokenReceiverLogger.warn("Sender Stream Null - Token Details");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - Token Details");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ JSONObject tokenObject = new JSONObject(tokenDetails);
+ TokenReceiverLogger.debug("Received data is "+tokenObject.toString());
+ JSONObject TokenDetails = tokenObject.getJSONObject("tokenDetails");
+ JSONArray wholeTokens = TokenDetails.getJSONArray("whole-tokens");
+ JSONArray wholeTokenChains = TokenDetails.getJSONArray("whole-tokenChains");
+ TokenReceiverLogger.debug(" ");
+ TokenReceiverLogger.debug("Whole token chain data is "+wholeTokenChains.toString());
+
+ JSONArray partTokens = TokenDetails.getJSONArray("part-tokens");
+ JSONObject partTokenChains = TokenDetails.getJSONObject("part-tokenChains");
+ JSONArray partTokenChainsHash = TokenDetails.getJSONArray("hashSender");
+
+ JSONArray previousSendersArray = tokenObject.getJSONArray("previousSender");
+ JSONArray positionsArray = tokenObject.getJSONArray("positions");
+
+ Double amount = tokenObject.getDouble("amount");
+ JSONObject amountLedger = tokenObject.getJSONObject("amountLedger");
+ TokenReceiverLogger.debug("Amount Ledger: " + amountLedger);
+ int intPart = wholeTokens.length();
+ // ? multiple pin check starts
+ Double decimalPart = formatAmount(amount - intPart);
+ JSONArray doubleSpentToken = new JSONArray();
+ boolean tokenOwners = true;
+ ArrayList pinOwnersArray = new ArrayList();
+ ArrayList previousSender = new ArrayList();
+ JSONArray ownersReceived = new JSONArray();
+ TokenReceiverLogger.debug("previousSendersArray is " + previousSendersArray.toString());
+ TokenReceiverLogger.debug("tokenObject is " + tokenObject.toString());
+ TokenReceiverLogger.debug("tokenDetails(base for tokenObj) is " + tokenDetails.toString());
+
+ // previoussenderarray
+ // tokenobject
+ for (int i = 0; i < wholeTokens.length(); ++i) {
+ try {
+ TokenReceiverLogger.debug("Checking owners for " + wholeTokens.getString(i) + " Please wait...");
+ pinOwnersArray = IPFSNetwork.dhtOwnerCheck(wholeTokens.getString(i));
+
+ if (pinOwnersArray.size() > 2) {
+
+ for (int j = 0; j < previousSendersArray.length(); j++) {
+ if (previousSendersArray.getJSONObject(j).getString("token")
+ .equals(wholeTokens.getString(i)))
+ ownersReceived = previousSendersArray.getJSONObject(j).getJSONArray("sender");
+ }
+
+ for (int j = 0; j < ownersReceived.length(); j++) {
+ previousSender.add(ownersReceived.getString(j));
+ }
+ TokenReceiverLogger.debug("Previous Owners: " + previousSender);
+
+ for (int j = 0; j < pinOwnersArray.size(); j++) {
+ if (!previousSender.contains(pinOwnersArray.get(j).toString()))
+ tokenOwners = false;
+ }
+ }
+ } catch (IOException e) {
+
+ TokenReceiverLogger.debug("Ipfs dht find did not execute");
+ }
+ }
+ if (!tokenOwners) {
+ JSONArray owners = new JSONArray();
+ for (int i = 0; i < pinOwnersArray.size(); i++)
+ owners.put(pinOwnersArray.get(i).toString());
+ TokenReceiverLogger.debug("Multiple Owners for " + doubleSpentToken);
+ TokenReceiverLogger.debug("Owners: " + owners);
+ output.println("420");
+ output.println(doubleSpentToken.toString());
+ output.println(owners.toString());
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Multiple Owners for " + doubleSpentToken + " " + owners);
+ IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+ // ? multiple pin check ends
+ String senderToken = TokenDetails.toString();
+ String consensusID = calculateHash(senderToken, "SHA3-256");
+ writeToFile(LOGGER_PATH + "consensusID", consensusID, false);
+ String consensusIDIPFSHash = IPFSNetwork.addHashOnly(LOGGER_PATH + "consensusID", ipfs);
+ deleteFile(LOGGER_PATH + "consensusID");
+
+ if (!IPFSNetwork.dhtEmpty(consensusIDIPFSHash, ipfs)) {
+ TokenReceiverLogger.debug("consensus ID not unique" + consensusIDIPFSHash);
+ output.println("421");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Consensus ID not unique");
+ IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+
+ // Check IPFS get for all Tokens
+ int ipfsGetFlag = 0;
+ ArrayList wholeTokenContent = new ArrayList<>();
+ ArrayList wholeTokenChainContent = new ArrayList<>();
+
+ /** Token Authenticity Check - starts */
+ HashMap tokenMaxLimitMap = new HashMap<>();
+ HashMap tokenDetailMap = new HashMap<>();
+
+ for (int i = 0; i < wholeTokens.length(); i++) {
+
+ String tokenChainContent = get(wholeTokenChains.getString(i), ipfs);
+ wholeTokenChainContent.add(tokenChainContent);
+ String tokenContent = get(wholeTokens.getString(i), ipfs).trim();
+ wholeTokenContent.add(tokenContent);
+ ipfsGetFlag++;
+ String tokenLevel = tokenContent.substring(0, tokenContent.length() - 64);
+ String tokenNumberHash = tokenContent.substring(tokenContent.length() - 64);
+
+ int tokenLevelInt = Integer.parseInt(tokenLevel);
+ int tokenLimitForLevel = tokenLimit[tokenLevelInt];
+ tokenMaxLimitMap.put(tokenNumberHash, tokenLimitForLevel);
+ tokenDetailMap.put(tokenNumberHash, -1);
+ }
+ repo(ipfs);
+
+ if (wholeTokens.length() > 0) {
+
+ if (!(ipfsGetFlag == intPart)) {
+ output.println("422");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Tokens not verified");
+ TokenReceiverLogger.info("Tokens not verified");
+ IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+
+ int tokenMaxValue = Collections.max(tokenMaxLimitMap.values());
+ TokenReceiverLogger.debug("Token Max Value : " + tokenMaxValue);
+ tokenDetailMap = Functions.checkTokenHash(tokenDetailMap, tokenMaxValue);
+
+ if (tokenDetailMap.isEmpty()) {
+ TokenReceiverLogger.debug("Invalid Content Found in Token");
+ String errorMessage = "Invalid Content Found in Token";
+ output.println("426");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", errorMessage);
+ TokenReceiverLogger.debug(errorMessage);
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ } else {
+ for (String tokenContent : tokenDetailMap.keySet()) {
+ TokenReceiverLogger.debug(tokenContent);
+ TokenReceiverLogger.debug(tokenDetailMap.get(tokenContent));
+ TokenReceiverLogger.debug(tokenMaxLimitMap.get(tokenContent));
+
+ if (tokenDetailMap.get(tokenContent) != null
+ && tokenDetailMap.get(tokenContent) > tokenMaxLimitMap.get(tokenContent)) {
+
+ output.println("426");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ String errorMessage1 = "Token Number is greater than Token Limit for the Level";
+ APIResponse.put("message", errorMessage1);
+ TokenReceiverLogger.debug(errorMessage1);
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+ }
+ }
+
+ }
+ /** Token Authenticity Check - Ends */
+
+ JSONArray partTokenChainContent = new JSONArray();
+ JSONArray partTokenContent = new JSONArray();
+
+ for (int i = 0; i < partTokenChains.length(); i++) {
+
+ partTokenChainContent.put(partTokenChains.getJSONArray(partTokens.getString(i)));
+ String TokenContent = get(partTokens.getString(i), ipfs).trim();
+ partTokenContent.put(TokenContent);
+ }
+
+ boolean chainFlag = true;
+ for (int i = 0; i < partTokenChainContent.length(); i++) {
+ JSONArray tokenChainContent = partTokenChainContent.getJSONArray(i);
+ for (int j = 0; j < tokenChainContent.length(); j++) {
+ String previousHash = tokenChainContent.getJSONObject(j).getString("previousHash");
+ String nextHash = tokenChainContent.getJSONObject(j).getString("nextHash");
+ String rePreviousHash, reNextHash;
+ if (tokenChainContent.length() > 1) {
+ if (j == 0) {
+ rePreviousHash = "";
+ String rePrev = calculateHash(new JSONObject().toString(), "SHA3-256");
+ reNextHash = calculateHash(tokenChainContent.getJSONObject(j + 1).getString("tid"),
+ "SHA3-256");
+
+ if (!((rePreviousHash.equals(previousHash) || rePrev.equals(previousHash))
+ && reNextHash.equals(nextHash))) {
+ chainFlag = false;
+ }
+
+ } else if (j == tokenChainContent.length() - 1) {
+ rePreviousHash = calculateHash(tokenChainContent.getJSONObject(j - 1).getString("tid"),
+ "SHA3-256");
+ reNextHash = "";
+
+ if (!(rePreviousHash.equals(previousHash) && reNextHash.equals(nextHash))) {
+ chainFlag = false;
+ }
+
+ } else {
+ rePreviousHash = calculateHash(tokenChainContent.getJSONObject(j - 1).getString("tid"),
+ "SHA3-256");
+ reNextHash = calculateHash(tokenChainContent.getJSONObject(j + 1).getString("tid"),
+ "SHA3-256");
+
+ if (!(rePreviousHash.equals(previousHash) && reNextHash.equals(nextHash))) {
+ chainFlag = false;
+ }
+ }
+ }
+ }
+ }
+
+ if (!chainFlag) {
+ String errorMessage = "Broken Cheque Chain";
+ output.println("423");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", errorMessage);
+ TokenReceiverLogger.debug(errorMessage);
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+
+ boolean ownerCheck = true;
+
+ JSONArray allTokens = new JSONArray();
+ for (int i = 0; i < wholeTokens.length(); i++)
+ allTokens.put(wholeTokens.getString(i));
+ for (int i = 0; i < partTokens.length(); i++)
+ allTokens.put(partTokens.getString(i));
+
+ JSONArray allTokensChains = new JSONArray();
+ for (int i = 0; i < wholeTokenChainContent.size(); i++)
+ allTokensChains.put(wholeTokenChainContent.get(i));
+ for (int i = 0; i < partTokenChainContent.length(); i++)
+ allTokensChains.put(partTokenChainContent.get(i));
+
+ TokenReceiverLogger.debug("allTokenChain is "+allTokensChains.toString());
+
+
+ JSONArray invalidTokens = new JSONArray();
+
+ for (int count = 0; count < wholeTokens.length(); count++) {
+ String tokens = null;
+ TokenReceiverLogger.debug("Json array tokenChain value is " + wholeTokens.get(count).toString());
+ JSONArray tokenChain = new JSONArray(allTokensChains.get(count).toString());
+ TokenReceiverLogger.debug("tokenchain is " + tokenChain);
+ TokenReceiverLogger.debug("tokenchain size is " + tokenChain.length());
+
+ String tokenContent = get(wholeTokens.getString(count), ipfs).trim();
+ String tokenLevel = tokenContent.substring(0, tokenContent.length() - 64);
+ String tokenNumberHash = tokenContent.substring(tokenContent.length() - 64);
+
+ int tokenLevelInt = Integer.parseInt(tokenLevel);
+ int tokenLimitForLevel = tokenLimit[tokenLevelInt];
+ int tokenLevelValue = (int) Math.pow(2, tokenLevelInt + 2);
+ int minumumStakeHeight = tokenLevelValue * 4;
+
+ // ! check quorum signs for previous transaction for the tokenchain to verify
+ // ! the ownership of sender for the token
+ JSONObject lastObject = new JSONObject();
+
+ if (tokenChain.length() > 0 ) {
+
+ lastObject = tokenChain.getJSONObject(tokenChain.length() - 1);
+
+ }
+ TokenReceiverLogger.debug("Last Object = " + lastObject.toString());
+
+
+ if (tokenChain.length() > 0 && lastObject.has("owner") && !lastObject.has(MiningConstants.STAKED_TOKEN) &&
+ ( (tokenLevelInt == 4 && (tokenDetailMap.get(tokenNumberHash) >= 1204400)) || (tokenLevelInt >= 5)) ) {
+
+ TokenReceiverLogger.debug("Checking ownership");
+ String owner = lastObject.getString("owner");
+ tokens = allTokens.getString(count);
+ String hashString = tokens.concat(senderDidIpfsHash);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+ String ownerIdentity = hashForPositions.concat(positionsArray.getString(count));
+ String ownerRecalculated = calculateHash(ownerIdentity, "SHA3-256");
+
+ TokenReceiverLogger.debug("Ownership Here Sender Calculation");
+ TokenReceiverLogger.debug("tokens: " + tokens);
+ TokenReceiverLogger.debug("hashString: " + hashString);
+ TokenReceiverLogger.debug("hashForPositions: " + hashForPositions);
+ TokenReceiverLogger.debug("p1: " + positionsArray.getString(count));
+ TokenReceiverLogger.debug("ownerIdentity: " + ownerIdentity);
+ TokenReceiverLogger.debug("ownerIdentityHash: " + ownerRecalculated);
+
+ if (!owner.equals(ownerRecalculated)) {
+ ownerCheck = false;
+ invalidTokens.put(tokens);
+ }
+
+ if (ownerCheck && (tokenChain.length() < minumumStakeHeight)) {
+ // && (tokenNumber > 1204400)
+ JSONObject genesiObject = tokenChain.getJSONObject(0);
+ JSONArray stakeDataArray = genesiObject.getJSONArray(MiningConstants.MINE_ID);
+
+ int randomNumber = new Random().nextInt(15);
+ JSONObject genesisSignaturesContent = genesiObject
+ .getJSONObject(MiningConstants.QUORUM_SIGN_CONTENT);
+ Iterator randomKey = genesisSignaturesContent.keys();
+ for (int i = 0; i < randomNumber; i++) {
+ randomKey.next();
+ }
+ /**
+ * String randomKeyString = randomKey.next().toString(); JSONObject
+ * verificationPick = new JSONObject(); verificationPick.put("did",
+ * randomKeyString); verificationPick.put("hash",
+ * genesiObject.getString("tid")); verificationPick.put("signature",
+ * genesisSignaturesContent.getString(randomKeyString));
+ *
+ * if (verificationPick.getString("hash").equals(genesiObject.getString("tid")))
+ * {
+ *
+ * if (Authenticate.verifySignature(verificationPick.toString())) {
+ * TokenReceiverLogger.debug("Staking check (3) successful"); } else {
+ * TokenReceiverLogger.debug( "Staking check (3) failed: Could not verify
+ * genesis credit signature"); ownerCheck = false; invalidTokens.put(tokens); }
+ * } else { TokenReceiverLogger.debug( "Staking check (3) failed: Genesis TID is
+ * not equal to the hash of the genesis signature"); ownerCheck = false;
+ * invalidTokens.put(tokens); }
+ */
+ // else {
+ // TokenReceiverLogger.debug("Staking check (3) failed: Genesis Signature not
+ // found");
+ // ownerCheck = false;
+ // invalidTokens.put(tokens);
+ // }
+
+ if (stakeDataArray.length() == 3) {
+
+ JSONObject oneOfThreeStake = stakeDataArray.getJSONObject(0);
+ JSONObject twoOfThreeStake = stakeDataArray.getJSONObject(1);
+ JSONObject threeOfThreeStake = stakeDataArray.getJSONObject(2);
+
+ String[] stakedTokenTC = new String[3];
+ String[] stakedTokenSignTC = new String[3];
+ String[] stakerDIDTC = new String[3];
+ String[] mineIDTC = new String[3];
+ String[] mineIDSignTC = new String[3];
+
+ stakedTokenTC[0] = oneOfThreeStake.getString(MiningConstants.STAKED_TOKEN);
+ stakedTokenSignTC[0] = oneOfThreeStake.getString(MiningConstants.STAKED_TOKEN_SIGN);
+ stakerDIDTC[0] = oneOfThreeStake.getString(MiningConstants.STAKED_QUORUM_DID);
+ mineIDTC[0] = oneOfThreeStake.getString(MiningConstants.MINE_ID);
+ mineIDSignTC[0] = oneOfThreeStake.getString(MiningConstants.MINE_ID_SIGN);
+
+ stakedTokenTC[1] = twoOfThreeStake.getString(MiningConstants.STAKED_TOKEN);
+ stakedTokenSignTC[1] = twoOfThreeStake.getString(MiningConstants.STAKED_TOKEN_SIGN);
+ stakerDIDTC[1] = twoOfThreeStake.getString(MiningConstants.STAKED_QUORUM_DID);
+ mineIDTC[1] = twoOfThreeStake.getString(MiningConstants.MINE_ID);
+ mineIDSignTC[1] = twoOfThreeStake.getString(MiningConstants.MINE_ID_SIGN);
+
+ stakedTokenTC[2] = threeOfThreeStake.getString(MiningConstants.STAKED_TOKEN);
+ stakedTokenSignTC[2] = threeOfThreeStake.getString(MiningConstants.STAKED_TOKEN_SIGN);
+ stakerDIDTC[2] = threeOfThreeStake.getString(MiningConstants.STAKED_QUORUM_DID);
+ mineIDTC[2] = threeOfThreeStake.getString(MiningConstants.MINE_ID);
+ mineIDSignTC[2] = threeOfThreeStake.getString(MiningConstants.MINE_ID_SIGN);
+ TokenReceiverLogger.debug("mineIDTC length is "+mineIDTC.length);
+
+ for (int stakeCount = 0; stakeCount < mineIDTC.length; stakeCount++) {
+
+ String mineIDContent = get(mineIDTC[stakeCount], ipfs);
+ JSONObject mineIDContentJSON = new JSONObject(mineIDContent);
+ TokenReceiverLogger.debug(mineIDContentJSON.toString());
+
+ JSONObject stakeData = mineIDContentJSON.getJSONObject(MiningConstants.STAKE_DATA);
+
+ String stakerDIDMineData = stakeData.getString(MiningConstants.STAKED_QUORUM_DID);
+ String stakedTokenMineData = stakeData.getString(MiningConstants.STAKED_TOKEN);
+ String stakedTokenSignMineData = stakeData
+ .getString(MiningConstants.STAKED_TOKEN_SIGN);
+
+ TokenReceiverLogger.debug(stakerDIDTC[stakeCount]);
+ TokenReceiverLogger.debug(stakedTokenTC[stakeCount]);
+ TokenReceiverLogger.debug(stakedTokenSignTC[stakeCount]);
+
+ TokenReceiverLogger.debug(stakerDIDMineData);
+ TokenReceiverLogger.debug(stakedTokenMineData);
+ TokenReceiverLogger.debug(stakedTokenSignMineData);
+
+ if (stakerDIDTC[stakeCount].equals(stakerDIDMineData)
+ && stakedTokenTC[stakeCount].equals(stakedTokenMineData)
+ && stakedTokenSignTC[stakeCount].equals(stakedTokenSignMineData)) {
+
+ TokenReceiverLogger.debug("array n non array data are same");
+
+ JSONObject detailsToVerify = new JSONObject();
+ detailsToVerify.put("did", stakerDIDTC[stakeCount]);
+ detailsToVerify.put("hash", mineIDTC[stakeCount]);
+ detailsToVerify.put("signature", mineIDSignTC[stakeCount]);
+ TokenReceiverLogger.debug("detailsToVerify - "+detailsToVerify.toString());
+
+ if (Authenticate.verifySignature(detailsToVerify.toString())) {
+
+ boolean minedTokenStatus = true;
+ ArrayList ownersArray = IPFSNetwork
+ .dhtOwnerCheck(stakedTokenTC[stakeCount]);
+ TokenReceiverLogger.debug("dht owner are "+ownersArray.toString()+" size is "+ownersArray.size());
+ for (int i = 0; i < ownersArray.size(); i++) {
+ if (ownersArray.get(i).equals(stakerDIDTC[stakeCount])) {
+ minedTokenStatus = false;
+ }
+ }
+ if (!minedTokenStatus) {
+ TokenReceiverLogger.debug("Staked token is not found with staker DID: "
+ + stakerDIDTC[stakeCount]);
+ ownerCheck = false;
+ invalidTokens.put(tokens);
+ }
+
+ } else {
+ TokenReceiverLogger.debug(
+ "Staking check (2) failed - unable to verify mine ID signature by staker: "
+ + stakerDIDTC[stakeCount]);
+ ownerCheck = false;
+ invalidTokens.put(tokens);
+ }
+
+ TokenReceiverLogger.debug("MineID Verification Successful with Staking node: "
+ + stakerDIDTC[stakeCount]);
+ } else {
+ TokenReceiverLogger.debug("Staking check (2) failed");
+ ownerCheck = false;
+ invalidTokens.put(tokens);
+ }
+
+ TokenReceiverLogger.debug("Staking check (2) successful for count "+stakeCount);
+ // } else {
+ // TokenReceiverLogger.debug(
+ // "Staking check (2) failed: Could not verify mine ID signature");
+ // ownerCheck = false;
+ // invalidTokens.put(tokens);
+ // }
+ }
+
+ } else {
+ ownerCheck = false;
+ TokenReceiverLogger.debug("Staked Token is not available!");
+
+ }
+
+ }
+ }
+ else if (tokenChain.length() > 0 && lastObject.has(MiningConstants.STAKED_TOKEN)) {
+
+ Boolean minedTokenStatus = true;
+
+ String mineID = lastObject.getString(MiningConstants.MINE_ID);
+
+ String mineIDContent = get(mineID, ipfs);
+ JSONObject mineIDContentJSON = new JSONObject(mineIDContent);
+
+ JSONObject stakeData = mineIDContentJSON.getJSONObject(MiningConstants.STAKE_DATA);
+
+ ArrayList ownersArray = IPFSNetwork
+ .dhtOwnerCheck(stakeData.getString(MiningConstants.STAKED_TOKEN));
+ for (int i = 0; i < ownersArray.size(); i++) {
+ if (!VerifyStakedToken.Contact(ownersArray.get(i), SEND_PORT + 16,
+ stakeData.getString(MiningConstants.STAKED_TOKEN),
+ mineIDContentJSON.getString("tokenContent"))) {
+ minedTokenStatus = false;
+ }
+ }
+ if (!minedTokenStatus) {
+ TokenReceiverLogger.debug("Staking check failed: Found staked token but token height < 46");
+ ownerCheck = false;
+ invalidTokens.put(tokens);
+ }
+
+ TokenReceiverLogger.debug(
+ "Staking check failed: Found staked token but unable to transfer while mined token height is not satisfied for the network");
+ ownerCheck = false;
+ invalidTokens.put(tokens);
+
+ // JSONObject tokenToVerify = new JSONObject();
+ // if (mineIDContentJSON.has(MiningConstants.STAKE_DATA)) {
+
+ // JSONObject stakeData =
+ // mineIDContentJSON.getJSONObject(MiningConstants.STAKE_DATA);
+ // String stakerDID = stakeData.getString(STAKED_QUORUM_DID);
+ // String stakedToken = stakeData.getString(STAKED_TOKEN);
+ // String stakedTokenSign = stakeData.getString(STAKED_TOKEN_SIGN);
+
+ // tokenToVerify.put("did", senderDidIpfsHash);
+ // tokenToVerify.put("hash", stakedToken);
+ // tokenToVerify.put("signature", stakedTokenSign);
+
+ // if (Authenticate.verifySignature(tokenToVerify.toString())) {
+
+ // ArrayList ownersArray = IPFSNetwork.dhtOwnerCheck(stakedToken);
+ // for (int i = 0; i < ownersArray.size(); i++) {
+ // if (!VerifyStakedToken.Contact(ownersArray.get(i), SEND_PORT + 16,
+ // mineIDContentJSON.getString("tokenContent"))) {
+ // minedTokenStatus = false;
+ // }
+ // }
+ // if (!minedTokenStatus) {
+ // TokenReceiverLogger
+ // .debug("Staking check failed: Found staked token but token height < 46");
+ // ownerCheck = false;
+ // invalidTokens.put(tokens);
+ // }
+
+ // TokenReceiverLogger.debug(
+ // "Staking check failed: Found staked token but unable to transfer while mined
+ // token height is not satisfied for the network");
+ // ownerCheck = false;
+ // invalidTokens.put(tokens);
+
+ // } else {
+ // TokenReceiverLogger.debug(
+ // "Staking check failed: Found staked token but unable to verify staked token
+ // height");
+ // ownerCheck = false;
+ // invalidTokens.put(tokens);
+ // }
+ // }
+
+ }
+
+ }
+
+ if (!ownerCheck) {
+ TokenReceiverLogger.debug("Ownership Check Failed");
+ String errorMessage = "Ownership Check Failed";
+ output.println("424");
+ output.println(invalidTokens.toString());
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", errorMessage);
+ TokenReceiverLogger.debug(errorMessage);
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ } else
+ TokenReceiverLogger.debug("Ownership Check Passed");
+
+ // -------------------------------------------------------
+
+ boolean partsAvailable = true;
+ for (int i = 0; i < partTokenChainContent.length(); i++) {
+ Double senderCount = 0.000D, receiverCount = 0.000D;
+ JSONArray tokenChainContent = partTokenChainContent.getJSONArray(i);
+ for (int k = 0; k < tokenChainContent.length(); k++) {
+ if (tokenChainContent.getJSONObject(k).has("role")) {
+ if (tokenChainContent.getJSONObject(k).getString("role").equals("Sender")
+ && tokenChainContent.getJSONObject(k).getString("sender").equals(senderDidIpfsHash)) {
+ senderCount += tokenChainContent.getJSONObject(k).getDouble("amount");
+ } else if (tokenChainContent.getJSONObject(k).getString("role").equals("Receiver")
+ && tokenChainContent.getJSONObject(k).getString("receiver").equals(senderDidIpfsHash)) {
+ receiverCount += tokenChainContent.getJSONObject(k).getDouble("amount");
+ }
+ }
+ }
+ FunctionsLogger.debug("Sender Parts: " + formatAmount(senderCount));
+ FunctionsLogger.debug("Receiver Parts: " + formatAmount(receiverCount));
+ Double availableParts = receiverCount - senderCount;
+
+ availableParts = formatAmount(availableParts);
+ availableParts += amountLedger.getDouble(partTokens.getString(i));
+ availableParts = formatAmount(availableParts);
+
+ if (availableParts > 1.000D) {
+ TokenReceiverLogger.debug("Token wholly spent: " + partTokens.getString(i));
+ TokenReceiverLogger.debug("Parts: " + availableParts);
+ partsAvailable = false;
+ }
+ }
+ if (!partsAvailable) {
+ String errorMessage = "Token wholly spent already";
+ output.println("425");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", errorMessage);
+ TokenReceiverLogger.debug(errorMessage);
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+ output.println("200");
+
+ String senderDetails;
+ try {
+ senderDetails = input.readLine();
+ } catch (SocketException e) {
+ TokenReceiverLogger.warn("Sender Stream Null - Sender Details");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - Sender Details");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ JSONObject SenderDetails = new JSONObject(senderDetails);
+ String senderSignature = SenderDetails.getString("sign");
+ String tid = SenderDetails.getString("tid");
+ String comment = SenderDetails.getString("comment");
+ String Status = SenderDetails.getString("status");
+ String QuorumDetails = SenderDetails.getString("quorumsign");
+ String BlockHash = new String();
+ if(SenderDetails.toString().contains("blockHash")) {
+ BlockHash = SenderDetails.getString("blockHash");
+ }
+
+ BufferedImage senderWidImage = ImageIO.read(new File(DATA_PATH + senderDidIpfsHash + "/PublicShare.png"));
+ SenWalletBin = PropImage.img2bin(senderWidImage);
+
+ TokenReceiverLogger.debug("Verifying Quorum ... ");
+ TokenReceiverLogger.debug("Please wait, this might take a few seconds");
+
+ if (!Status.equals("Consensus Failed")) {
+ boolean yesQuorum = false;
+ if (Status.equals("Consensus Reached")) {
+ quorumSignatures = new JSONObject(QuorumDetails);
+ String selectQuorumHash = calculateHash(senderToken, "SHA3-256");
+ String verifyQuorumHash = calculateHash(selectQuorumHash.concat(receiverDidIpfsHash), "SHA3-256");
+
+ Iterator keys = quorumSignatures.keys();
+ while (keys.hasNext()) {
+ String key = keys.next();
+ quorumDID.add(key);
+ }
+
+ for (String quorumDidIpfsHash : quorumDID) {
+ syncDataTable(quorumDidIpfsHash, null);
+ String quorumWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "didHash",
+ quorumDidIpfsHash);
+
+ nodeData(quorumDidIpfsHash, quorumWidIpfsHash, ipfs);
+ }
+
+ for (int i = 0; i < quorumSignatures.length(); i++) {
+
+ JSONObject detailsForVerify = new JSONObject();
+ detailsForVerify.put("did", quorumDID.get(i));
+ detailsForVerify.put("hash", verifyQuorumHash);
+ detailsForVerify.put("signature", quorumSignatures.getString(quorumDID.get(i)));
+ boolean val = Authenticate.verifySignature(detailsForVerify.toString());
+ if (val)
+ quorumSignVerifyCount++;
+ }
+ TokenReceiverLogger.debug("Verified Quorum Count " + quorumSignVerifyCount);
+ yesQuorum = quorumSignVerifyCount >= quorumSignatures.length();
+ }
+ JSONArray wholeTokenChainHash = new JSONArray();
+ for (int i = 0; i < intPart; i++)
+ wholeTokenChainHash.put(wholeTokenChains.getString(i));
+
+ String hash = calculateHash(
+ wholeTokens.toString() + wholeTokenChainHash.toString() + partTokens.toString()
+ + partTokenChainsHash.toString() + receiverDidIpfsHash + senderDidIpfsHash + comment,
+ "SHA3-256");
+ TokenReceiverLogger.debug("Hash to verify Sender: " + hash);
+ JSONObject detailsForVerify = new JSONObject();
+ detailsForVerify.put("did", senderDidIpfsHash);
+ detailsForVerify.put("hash", hash);
+ detailsForVerify.put("signature", senderSignature);
+
+ boolean yesSender = Authenticate.verifySignature(detailsForVerify.toString());
+ TokenReceiverLogger.debug("Quorum Auth : " + yesQuorum + " Sender Auth : " + yesSender);
+ if (!(yesSender && yesQuorum)) {
+ output.println("420");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender / Quorum not verified");
+ TokenReceiverLogger.info("Sender / Quorum not verified");
+ IPFSNetwork.executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+ }
+
+ repo(ipfs);
+ TokenReceiverLogger.debug("Sender and Quorum Verified");
+ output.println("200");
+
+ String pinDetails;
+ try {
+ pinDetails = input.readLine();
+ } catch (SocketException e) {
+ TokenReceiverLogger.warn("Sender Stream Null - Pinning Status");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - Pinning Status");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ if (pinDetails.equals("Unpinned")) {
+ int pinCount = 0;
+ for (int i = 0; i < intPart; i++) {
+
+ Path tokensPath = Paths.get(TOKENS_PATH + wholeTokens.get(i));
+ Files.write(tokensPath, wholeTokenContent.get(i).getBytes());
+ add(TOKENS_PATH + wholeTokens.get(i), ipfs);
+ pin(wholeTokens.get(i).toString(), ipfs);
+ pinCount++;
+
+ }
+
+ for (int i = 0; i < partTokens.length(); i++) {
+ File tokenFile = new File(PART_TOKEN_PATH + partTokens.getString(i));
+ if (!tokenFile.exists())
+ tokenFile.createNewFile();
+
+ writeToFile(PART_TOKEN_PATH + partTokens.getString(i), partTokenContent.getString(i), false);
+ String tokenHash = add(PART_TOKEN_PATH + partTokens.getString(i), ipfs);
+ pin(tokenHash, ipfs);
+
+ }
+
+ if (pinCount == intPart) {
+ TokenReceiverLogger.debug("Pinned All Tokens");
+ output.println("Successfully Pinned");
+
+ String essentialShare;
+ try {
+ essentialShare = input.readLine();
+ } catch (SocketException e) {
+ TokenReceiverLogger.warn("Sender Stream Null - EShare Details");
+ APIResponse.put("did", "");
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender Stream Null - EShare Details");
+
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ long endTime = System.currentTimeMillis();
+ for (int i = 0; i < intPart; i++) {
+ String wholeToken = wholeTokens.getString(i);
+ String hashString = wholeToken.concat(receiverDidIpfsHash);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+
+ BufferedImage pvt = ImageIO
+ .read(new File(DATA_PATH.concat(receiverDidIpfsHash).concat("/PrivateShare.png")));
+ String firstPrivate = PropImage.img2bin(pvt);
+ int[] privateIntegerArray1 = strToIntArray(firstPrivate);
+ String privateBinary = Functions.intArrayToStr(privateIntegerArray1);
+ String positions = "";
+ for (int j = 0; j < privateIntegerArray1.length; j += 49152) {
+ positions += privateBinary.charAt(j);
+ }
+ String ownerIdentity = hashForPositions.concat(positions);
+ String ownerIdentityHash = calculateHash(ownerIdentity, "SHA3-256");
+
+ TokenReceiverLogger.debug("Ownership Here");
+ TokenReceiverLogger.debug("tokens: " + wholeTokens.getString(i));
+ TokenReceiverLogger.debug("hashString: " + hashString);
+ TokenReceiverLogger.debug("hashForPositions: " + hashForPositions);
+ TokenReceiverLogger.debug("p1: " + positions);
+ TokenReceiverLogger.debug("ownerIdentity: " + ownerIdentity);
+ TokenReceiverLogger.debug("ownerIdentityHash: " + ownerIdentityHash);
+
+ ArrayList groupTokens = new ArrayList<>();
+ for (int k = 0; k < intPart; k++) {
+ if (!wholeTokens.getString(i).equals(wholeTokens.getString(k)))
+ groupTokens.add(wholeTokens.getString(k));
+ }
+
+ JSONArray arrToken = new JSONArray();
+ JSONObject objectToken = new JSONObject();
+ objectToken.put("tokenHash", wholeTokens.getString(i));
+ arrToken.put(objectToken);
+ JSONArray arr1 = new JSONArray(wholeTokenChainContent.get(i));
+ JSONObject obj2 = new JSONObject();
+ obj2.put("senderSign", senderSignature);
+ obj2.put("sender", senderDidIpfsHash);
+ obj2.put("group", groupTokens);
+ obj2.put("comment", comment);
+ obj2.put("tid", tid);
+ obj2.put("owner", ownerIdentityHash);
+ if(!BlockHash.isEmpty()){
+ obj2.put("blockHash",BlockHash);
+ }
+ arr1.put(obj2);
+
+ writeToFile(TOKENCHAIN_PATH + wholeTokens.getString(i) + ".json", arr1.toString(), false);
+
+ }
+
+ for (int i = 0; i < partTokens.length(); i++) {
+ JSONObject chequeObject = new JSONObject();
+ chequeObject.put("sender", senderDidIpfsHash);
+ chequeObject.put("receiver", receiverDidIpfsHash);
+ chequeObject.put("parent-token", partTokens.getString(i));
+ chequeObject.put("parent-chain", partTokenChains.getJSONArray(partTokens.getString(i)));
+ Double partAmount = formatAmount(amountLedger.getDouble(partTokens.getString(i)));
+ chequeObject.put("amount", partAmount);
+ chequeObject.put("tid", tid);
+
+ writeToFile(LOGGER_PATH.concat(partTokens.getString(i)), chequeObject.toString(), false);
+ String chequeHash = IPFSNetwork.add(LOGGER_PATH.concat(partTokens.getString(i)), ipfs);
+ deleteFile(LOGGER_PATH.concat(partTokens.getString(i)));
+
+ String partsTokens = partTokens.getString(i);
+ String hashString = partsTokens.concat(receiverDidIpfsHash);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+ BufferedImage pvt = ImageIO
+ .read(new File(DATA_PATH.concat(receiverDidIpfsHash).concat("/PrivateShare.png")));
+ String firstPrivate = PropImage.img2bin(pvt);
+ int[] privateIntegerArray1 = strToIntArray(firstPrivate);
+ String privateBinary = Functions.intArrayToStr(privateIntegerArray1);
+ String positions = "";
+ for (int j = 0; j < privateIntegerArray1.length; j += 49152) {
+ positions += privateBinary.charAt(j);
+ }
+
+ String ownerIdentity = hashForPositions.concat(positions);
+ String ownerIdentityHash = calculateHash(ownerIdentity, "SHA3-256");
+
+ TokenReceiverLogger.debug("Ownership Here");
+ TokenReceiverLogger.debug("tokens: " + partTokens.getString(i));
+ TokenReceiverLogger.debug("hashString: " + hashString);
+ TokenReceiverLogger.debug("hashForPositions: " + hashForPositions);
+ TokenReceiverLogger.debug("p1: " + positions);
+ TokenReceiverLogger.debug("ownerIdentity: " + ownerIdentity);
+ TokenReceiverLogger.debug("ownerIdentityHash: " + ownerIdentityHash);
+
+ JSONObject newPartObject = new JSONObject();
+ newPartObject.put("senderSign", senderSignature);
+ newPartObject.put("sender", senderDidIpfsHash);
+ newPartObject.put("receiver", receiverDidIpfsHash);
+ newPartObject.put("comment", comment);
+ newPartObject.put("tid", tid);
+ newPartObject.put("nextHash", "");
+ newPartObject.put("owner", ownerIdentityHash);
+ if (partTokenChainContent.getJSONArray(i).length() == 0)
+ newPartObject.put("previousHash", "");
+ else
+ newPartObject.put("previousHash",
+ calculateHash(partTokenChainContent.getJSONArray(i)
+ .getJSONObject(partTokenChainContent.getJSONArray(i).length() - 1)
+ .getString("tid"), "SHA3-256"));
+
+ newPartObject.put("amount", partAmount);
+ newPartObject.put("cheque", chequeHash);
+ newPartObject.put("role", "Receiver");
+
+ File chainFile = new File(
+ PART_TOKEN_CHAIN_PATH.concat(partTokens.getString(i)).concat(".json"));
+ if (chainFile.exists()) {
+
+ String readChain = readFile(PART_TOKEN_CHAIN_PATH + partTokens.getString(i) + ".json");
+ JSONArray readChainArray = new JSONArray(readChain);
+ readChainArray.put(partTokenChainContent.getJSONArray(i)
+ .getJSONObject(partTokenChainContent.getJSONArray(i).length() - 1));
+ readChainArray.put(newPartObject);
+
+ writeToFile(PART_TOKEN_CHAIN_PATH + partTokens.getString(i) + ".json",
+ readChainArray.toString(), false);
+
+ } else {
+ partTokenChainContent.getJSONArray(i).put(newPartObject);
+ writeToFile(PART_TOKEN_CHAIN_PATH + partTokens.getString(i) + ".json",
+ partTokenChainContent.getJSONArray(i).toString(), false);
+ }
+ }
+
+ JSONObject transactionRecord = new JSONObject();
+ transactionRecord.put("role", "Receiver");
+ transactionRecord.put("tokens", allTokens);
+ transactionRecord.put("txn", tid);
+ transactionRecord.put("quorumList", quorumSignatures.keys());
+ transactionRecord.put("senderDID", senderDidIpfsHash);
+ transactionRecord.put("receiverDID", receiverDidIpfsHash);
+ transactionRecord.put("Date", getCurrentUtcTime());
+ transactionRecord.put("totalTime", (endTime - startTime));
+ transactionRecord.put("comment", comment);
+ transactionRecord.put("essentialShare", essentialShare);
+ amount = formatAmount(amount);
+ transactionRecord.put("amount-received", amount);
+
+ JSONArray transactionHistoryEntry = new JSONArray();
+ transactionHistoryEntry.put(transactionRecord);
+ updateJSON("add", WALLET_DATA_PATH + "TransactionHistory.json",
+ transactionHistoryEntry.toString());
+
+ for (int i = 0; i < wholeTokens.length(); i++) {
+ String bankFile = readFile(PAYMENTS_PATH.concat("BNK00.json"));
+ JSONArray bankArray = new JSONArray(bankFile);
+ JSONObject tokenObject1 = new JSONObject();
+ tokenObject1.put("tokenHash", wholeTokens.getString(i));
+ bankArray.put(tokenObject1);
+ Functions.writeToFile(PAYMENTS_PATH.concat("BNK00.json"), bankArray.toString(), false);
+ Path tokensPath = Paths.get(TOKENS_PATH + wholeTokens.get(i));
+ Files.write(tokensPath, wholeTokenContent.get(i).getBytes());
+ }
+
+ String partsFile = readFile(PAYMENTS_PATH.concat("PartsToken.json"));
+ JSONArray partsReadArray = new JSONArray(partsFile);
+
+ for (int i = 0; i < partTokens.length(); i++) {
+ boolean writeParts = true;
+ for (int j = 0; j < partsReadArray.length(); j++) {
+ if (partsReadArray.getJSONObject(j).getString("tokenHash")
+ .equals(partTokens.getString(i)))
+ writeParts = false;
+ }
+ if (writeParts) {
+ JSONObject partObject = new JSONObject();
+ partObject.put("tokenHash", partTokens.getString(i));
+ partsReadArray.put(partObject);
+ }
+ }
+ writeToFile(PAYMENTS_PATH.concat("PartsToken.json"), partsReadArray.toString(), false);
+
+ TokenReceiverLogger.info("Transaction ID: " + tid + "Transaction Successful");
+ output.println("Send Response");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Success");
+ APIResponse.put("tokens", wholeTokens);
+ APIResponse.put("comment", comment);
+ APIResponse.put("message", "Transaction Successful");
+ TokenReceiverLogger.info(" Transaction Successful");
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ output.println("count mistmatch");
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "count mismacth");
+ TokenReceiverLogger.info(" Transaction failed");
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Failed to unpin");
+ TokenReceiverLogger.info(" Transaction failed");
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ }
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Consensus failed at Sender side");
+ TokenReceiverLogger.info(" Transaction failed");
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ output.close();
+ input.close();
+ sk.close();
+ ss.close();
+ return APIResponse.toString();
+
+ } catch (Exception e) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ TokenReceiverLogger.error("Exception Occurred", e);
+ return APIResponse.toString();
+ } finally {
+ try {
+ ss.close();
+ sk.close();
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ } catch (Exception e) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + senderPeerID);
+ TokenReceiverLogger.error("Exception Occurred", e);
+ }
+
+ }
+ }
}
diff --git a/src/com/rubix/TokenTransfer/TokenSender.java b/src/com/rubix/TokenTransfer/TokenSender.java
index efc01e02..febf7e07 100644
--- a/src/com/rubix/TokenTransfer/TokenSender.java
+++ b/src/com/rubix/TokenTransfer/TokenSender.java
@@ -1,18 +1,50 @@
package com.rubix.TokenTransfer;
-import com.rubix.Consensus.InitiatorConsensus;
-import com.rubix.Consensus.InitiatorProcedure;
-import com.rubix.Resources.Functions;
-import com.rubix.Resources.IPFSNetwork;
-import io.ipfs.api.IPFS;
-import org.apache.log4j.Logger;
-import org.apache.log4j.PropertyConfigurator;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
-
-import javax.net.ssl.HttpsURLConnection;
-import java.io.*;
+import static com.rubix.Resources.Functions.DATA_PATH;
+import static com.rubix.Resources.Functions.EXPLORER_IP;
+import static com.rubix.Resources.Functions.LOGGER_PATH;
+import static com.rubix.Resources.Functions.PAYMENTS_PATH;
+import static com.rubix.Resources.Functions.QuorumCheck;
+import static com.rubix.Resources.Functions.QuorumSwarmConnect;
+import static com.rubix.Resources.Functions.SEND_PORT;
+import static com.rubix.Resources.Functions.TOKENCHAIN_PATH;
+import static com.rubix.Resources.Functions.TOKENS_PATH;
+import static com.rubix.Resources.Functions.WALLET_DATA_PATH;
+import static com.rubix.Resources.Functions.calculateHash;
+import static com.rubix.Resources.Functions.deleteFile;
+import static com.rubix.Resources.Functions.formatAmount;
+import static com.rubix.Resources.Functions.getCurrentUtcTime;
+import static com.rubix.Resources.Functions.getPeerID;
+import static com.rubix.Resources.Functions.getQuorum;
+import static com.rubix.Resources.Functions.getSignFromShares;
+import static com.rubix.Resources.Functions.getValues;
+import static com.rubix.Resources.Functions.minQuorum;
+import static com.rubix.Resources.Functions.nodeData;
+import static com.rubix.Resources.Functions.partTokenBalance;
+import static com.rubix.Resources.Functions.readFile;
+import static com.rubix.Resources.Functions.sanityCheck;
+import static com.rubix.Resources.Functions.sanityMessage;
+import static com.rubix.Resources.Functions.strToIntArray;
+import static com.rubix.Resources.Functions.syncDataTable;
+import static com.rubix.Resources.Functions.syncDataTableByDID;
+import static com.rubix.Resources.Functions.updateJSON;
+import static com.rubix.Resources.Functions.updateQuorum;
+import static com.rubix.Resources.Functions.writeToFile;
+import static com.rubix.Resources.IPFSNetwork.add;
+import static com.rubix.Resources.IPFSNetwork.executeIPFSCommands;
+import static com.rubix.Resources.IPFSNetwork.forward;
+import static com.rubix.Resources.IPFSNetwork.pin;
+import static com.rubix.Resources.IPFSNetwork.repo;
+import static com.rubix.Resources.IPFSNetwork.swarmConnectP2P;
+import static com.rubix.Resources.IPFSNetwork.unpin;
+
+import java.awt.image.BufferedImage;
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
@@ -20,12 +52,27 @@
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
-import static com.rubix.Resources.Functions.*;
-import static com.rubix.Resources.IPFSNetwork.*;
+import javax.imageio.ImageIO;
+import javax.net.ssl.HttpsURLConnection;
+import com.rubix.AuthenticateNode.PropImage;
+import com.rubix.Consensus.InitiatorConsensus;
+import com.rubix.Consensus.InitiatorProcedure;
+import com.rubix.Constants.*;
+import com.rubix.Resources.Functions;
+import com.rubix.Resources.IPFSNetwork;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import io.ipfs.api.IPFS;
public class TokenSender {
private static final Logger TokenSenderLogger = Logger.getLogger(TokenSender.class);
@@ -53,20 +100,32 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
JSONObject APIResponse = new JSONObject();
PropertyConfigurator.configure(LOGGER_PATH + "log4jWallet.properties");
- String receiverPeerId;
JSONObject detailsObject = new JSONObject(data);
String receiverDidIpfsHash = detailsObject.getString("receiverDidIpfsHash");
+ syncDataTableByDID(receiverDidIpfsHash);
+ String receiverPeerId = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", receiverDidIpfsHash);
String pvt = detailsObject.getString("pvt");
double requestedAmount = detailsObject.getDouble("amount");
int type = detailsObject.getInt("type");
String comment = detailsObject.getString("comment");
APIResponse = new JSONObject();
+ int intPart = (int) requestedAmount, wholeAmount;
String senderPeerID = getPeerID(DATA_PATH + "DID.json");
TokenSenderLogger.debug("sender peer id" + senderPeerID);
String senderDidIpfsHash = getValues(DATA_PATH + "DataTable.json", "didHash", "peerid", senderPeerID);
TokenSenderLogger.debug("sender did ipfs hash" + senderDidIpfsHash);
+ boolean sanityCheck = sanityCheck("Receiver", receiverPeerId, ipfs, port + 10);
+ if (!sanityCheck) {
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", sanityMessage);
+ TokenSenderLogger.warn(sanityMessage);
+ return APIResponse;
+ }
+
if (senderMutex) {
APIResponse.put("did", senderDidIpfsHash);
APIResponse.put("tid", "null");
@@ -92,9 +151,8 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
writeToFile(partTokensFile.toString(), "[]", false);
}
- int intPart = (int) requestedAmount, wholeAmount;
- TokenSenderLogger.debug("Requested Part: " +requestedAmount);
- TokenSenderLogger.debug("Int Part: " +intPart);
+ TokenSenderLogger.debug("Requested Part: " + requestedAmount);
+ TokenSenderLogger.debug("Int Part: " + intPart);
String bankFile = readFile(PAYMENTS_PATH.concat("BNK00.json"));
JSONArray bankArray = new JSONArray(bankFile);
JSONArray wholeTokens = new JSONArray();
@@ -107,15 +165,16 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
wholeTokens.put(bankArray.getJSONObject(i).getString("tokenHash"));
}
- for(int i = 0; i < wholeTokens.length(); i++){
+ for (int i = 0; i < wholeTokens.length(); i++) {
String tokenRemove = wholeTokens.getString(i);
- for(int j = 0; j < bankArray.length(); j++){
- if(bankArray.getJSONObject(j).getString("tokenHash").equals(tokenRemove))
+ for (int j = 0; j < bankArray.length(); j++) {
+ if (bankArray.getJSONObject(j).getString("tokenHash").equals(tokenRemove))
bankArray.remove(j);
}
}
JSONArray wholeTokenChainHash = new JSONArray();
JSONArray tokenPreviousSender = new JSONArray();
+
for (int i = 0; i < wholeTokens.length(); i++) {
File token = new File(TOKENS_PATH + wholeTokens.get(i));
File tokenchain = new File(TOKENCHAIN_PATH + wholeTokens.get(i) + ".json");
@@ -134,25 +193,42 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
String tokenChainHash = add(TOKENCHAIN_PATH + wholeTokens.get(i) + ".json", ipfs);
wholeTokenChainHash.put(tokenChainHash);
-
String tokenChainFileContent = readFile(TOKENCHAIN_PATH + wholeTokens.get(i) + ".json");
JSONArray tokenChainFileArray = new JSONArray(tokenChainFileContent);
JSONArray previousSenderArray = new JSONArray();
- for (int j = 0; j < tokenChainFileArray.length(); j++) {
- String peerID = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", tokenChainFileArray.getJSONObject(j).getString("sender"));
- previousSenderArray.put(peerID);
+
+ TokenSenderLogger.debug("tokenChainFileArray is " + tokenChainFileArray);
+ TokenSenderLogger.debug("tokenChainFileArray length is " + tokenChainFileArray.length());
+
+
+ if (tokenChainFileArray.length() > 0) {
+ // JSONObject lastObject =
+ // tokenChainFileArray.getJSONObject(tokenChainFileArray.length() - 1);
+
+ //for(int k=0;k 0.000D) {
counter = formatAmount(counter);
- TokenSenderLogger.debug("Counter: " + formatAmount(counter) );
- if(!(selectParts.length() == 0)) {
+ TokenSenderLogger.debug("Counter: " + formatAmount(counter));
+ if (!(selectParts.length() == 0)) {
TokenSenderLogger.debug("Old Parts");
String currentPartToken = selectParts.getJSONObject(0).getString("tokenHash");
Double currentPartBalance = partTokenBalance(currentPartToken);
@@ -189,7 +265,7 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
partTokens.put(currentPartToken);
counter -= currentPartBalance;
selectParts.remove(0);
- }else{
+ } else {
oldNew = true;
TokenSenderLogger.debug("Old Parts then new parts");
String chosenToken = bankArray.getJSONObject(0).getString("tokenHash");
@@ -200,13 +276,13 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
File chainFile = new File(TOKENCHAIN_PATH.concat(chosenToken).concat(".json"));
chainFile.renameTo(new File(PART_TOKEN_CHAIN_PATH.concat(chosenToken).concat(".json")));
-
File shiftedFile = new File(PAYMENTS_PATH.concat("ShiftedTokens.json"));
if (!shiftedFile.exists()) {
shiftedFile.createNewFile();
JSONArray shiftedTokensArray = new JSONArray();
shiftedTokensArray.put(chosenToken);
- writeToFile(PAYMENTS_PATH.concat("ShiftedTokens.json"), shiftedTokensArray.toString(), false);
+ writeToFile(PAYMENTS_PATH.concat("ShiftedTokens.json"), shiftedTokensArray.toString(),
+ false);
} else {
String shiftedContent = readFile(PAYMENTS_PATH.concat("ShiftedTokens.json"));
JSONArray shiftedArray = new JSONArray(shiftedContent);
@@ -219,11 +295,10 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
}
}
String tokenChainPath = "", tokenPath = "";
- if(newPart) {
+ if (newPart) {
tokenChainPath = TOKENCHAIN_PATH;
tokenPath = TOKENS_PATH;
- }
- else{
+ } else {
tokenChainPath = TOKENCHAIN_PATH.concat("PARTS/");
tokenPath = TOKENS_PATH.concat("PARTS/");
}
@@ -251,7 +326,6 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
String hash = add(tokenPath + partTokens.getString(i), ipfs);
pin(hash, ipfs);
-
String chainContent = readFile(tokenChainPath.concat(partTokens.getString(i)).concat(".json"));
JSONArray chainArray = new JSONArray();
JSONArray finalChainArray = new JSONArray(chainContent);
@@ -263,13 +337,17 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
} else if (finalChainArray.length() > 1) {
if (j == 0) {
object.put("previousHash", "");
- object.put("nextHash", calculateHash(finalChainArray.getJSONObject(j + 1).getString("tid"), "SHA3-256"));
+ object.put("nextHash",
+ calculateHash(finalChainArray.getJSONObject(j + 1).getString("tid"), "SHA3-256"));
} else if (j == finalChainArray.length() - 1) {
- object.put("previousHash", calculateHash(finalChainArray.getJSONObject(j - 1).getString("tid"), "SHA3-256"));
+ object.put("previousHash",
+ calculateHash(finalChainArray.getJSONObject(j - 1).getString("tid"), "SHA3-256"));
object.put("nextHash", "");
} else {
- object.put("previousHash", calculateHash(finalChainArray.getJSONObject(j - 1).getString("tid"), "SHA3-256"));
- object.put("nextHash", calculateHash(finalChainArray.getJSONObject(j + 1).getString("tid"), "SHA3-256"));
+ object.put("previousHash",
+ calculateHash(finalChainArray.getJSONObject(j - 1).getString("tid"), "SHA3-256"));
+ object.put("nextHash",
+ calculateHash(finalChainArray.getJSONObject(j + 1).getString("tid"), "SHA3-256"));
}
}
chainArray.put(object);
@@ -280,24 +358,48 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
partTokenChainHash.put(add(tokenChainPath.concat(partTokens.getString(i)).concat(".json"), ipfs));
}
- String authSenderByRecHash = calculateHash(wholeTokens.toString() + wholeTokenChainHash.toString() + partTokens.toString() + partTokenChainHash.toString() + receiverDidIpfsHash + senderDidIpfsHash + comment, "SHA3-256");
+ String authSenderByRecHash = calculateHash(
+ wholeTokens.toString() + wholeTokenChainHash.toString() + partTokens.toString()
+ + partTokenChainHash.toString() + receiverDidIpfsHash + senderDidIpfsHash + comment,
+ "SHA3-256");
TokenSenderLogger.debug("Hash to verify Sender: " + authSenderByRecHash);
String tid = calculateHash(authSenderByRecHash, "SHA3-256");
TokenSenderLogger.debug("Sender by Receiver Hash " + authSenderByRecHash);
TokenSenderLogger.debug("TID on sender " + tid);
+ JSONArray allTokens = new JSONArray();
+ for (int i = 0; i < wholeTokens.length(); i++)
+ allTokens.put(wholeTokens.getString(i));
+ for (int i = 0; i < partTokens.length(); i++)
+ allTokens.put(partTokens.getString(i));
- JSONArray quorumArray;
- JSONArray alphaQuorum = new JSONArray();
- JSONArray betaQuorum = new JSONArray();
- JSONArray gammaQuorum = new JSONArray();
- int alphaSize;
+ JSONArray positionsArray = new JSONArray();
+ for (int i = 0; i < allTokens.length(); i++) {
+ String tokens = allTokens.getString(i);
+ String hashString = tokens.concat(senderDidIpfsHash);
+ String hashForPositions = calculateHash(hashString, "SHA3-256");
+ BufferedImage privateShare = ImageIO
+ .read(new File(DATA_PATH.concat(senderDidIpfsHash).concat("/PrivateShare.png")));
+ String firstPrivate = PropImage.img2bin(privateShare);
+ int[] privateIntegerArray1 = strToIntArray(firstPrivate);
+ String privateBinary = Functions.intArrayToStr(privateIntegerArray1);
+ String positions = "";
+ for (int j = 0; j < privateIntegerArray1.length; j += 49152) {
+ positions += privateBinary.charAt(j);
+ }
+ positionsArray.put(positions);
- ArrayList alphaPeersList;
- ArrayList betaPeersList;
- ArrayList gammaPeersList;
+ TokenSenderLogger.debug("Ownership Here Sender Calculation");
+ TokenSenderLogger.debug("tokens: " + tokens);
+ TokenSenderLogger.debug("hashString: " + hashString);
+ TokenSenderLogger.debug("hashForPositions: " + hashForPositions);
+ TokenSenderLogger.debug("p1: " + positions);
+ }
- long startTime = System.currentTimeMillis();
+ JSONArray alphaQuorum = new JSONArray();
+ JSONArray betaQuorum = new JSONArray();
+ JSONArray gammaQuorum = new JSONArray();
+ JSONArray quorumArray = new JSONArray();
switch (type) {
case 1: {
writeToFile(LOGGER_PATH + "tempbeta", tid.concat(senderDidIpfsHash), false);
@@ -308,7 +410,8 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
String gammaHash = IPFSNetwork.add(LOGGER_PATH + "tempgamma", ipfs);
deleteFile(LOGGER_PATH + "tempgamma");
- quorumArray = getQuorum(betaHash, gammaHash, senderDidIpfsHash, receiverDidIpfsHash, wholeTokens.length());
+ quorumArray = getQuorum(senderDidIpfsHash, receiverDidIpfsHash,
+ allTokens.length());
break;
}
@@ -328,19 +431,69 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
}
}
+ int alphaSize;
- TokenSenderLogger.debug("1");
- TokenSenderLogger.debug("Whole tokens: " + wholeTokens);
- TokenSenderLogger.debug("Part tokens: " + partTokens);
- long endTime = System.currentTimeMillis();
- long totalTime = endTime - startTime;
- eventLogger.debug("Get Quorum List " + totalTime);
+ ArrayList alphaPeersList;
+ ArrayList betaPeersList;
+ ArrayList gammaPeersList;
+
+ List quorumList = new ArrayList<>();
+ String errMessage = null;
+ for (int i = 0; i < quorumArray.length(); i++) {
+
+ if(quorumArray.get(i).equals(senderDidIpfsHash)) {
+ TokenSenderLogger.error("SenderDID "+senderDidIpfsHash+" cannot be a Quorum");
+ errMessage = "SenderDID "+senderDidIpfsHash;
+ }
+ if(quorumArray.get(i).equals(receiverDidIpfsHash)) {
+ TokenSenderLogger.error("ReceiverDID "+receiverDidIpfsHash+" cannot be a Quorum");
+ if(errMessage != null) {
+ errMessage = errMessage+" and ";
+ }
+ errMessage = "ReceiverDID "+receiverDidIpfsHash;
+ }
+ if(errMessage != null) {
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", errMessage+" cannot be a Quorum ");
+ return APIResponse;
+ }
+
+ }
+
+ TokenSenderLogger.debug("Updated quorumlist is "+quorumArray.toString());
+
+ //sanity check for Quorum - starts
+ int alphaCheck = 0, betaCheck = 0, gammaCheck = 0;
+ JSONArray sanityFailedQuorum = new JSONArray();
+ for (int i = 0; i < quorumArray.length(); i++) {
+ String quorumPeerID = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
+ quorumArray.getString(i));
+ boolean quorumSanityCheck = sanityCheck("Quorum",quorumPeerID, ipfs, port + 11);
+
+ if (!quorumSanityCheck) {
+ sanityFailedQuorum.put(quorumPeerID);
+ if (i <= 6)
+ alphaCheck++;
+ if (i >= 7 && i <= 13)
+ betaCheck++;
+ if (i >= 14 && i <= 20)
+ gammaCheck++;
+ }
+ }
+
+ if (alphaCheck > 2 || betaCheck > 2 || gammaCheck > 2) {
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", "null");
+ APIResponse.put("status", "Failed");
+ String message = "Quorum: ".concat(sanityFailedQuorum.toString()).concat(" ");
+ APIResponse.put("message", message.concat(sanityMessage));
+ TokenSenderLogger.warn("Quorum: ".concat(message.concat(sanityMessage)));
+ return APIResponse;
+ }
+ //sanity check for Quorum - Ends
+ long startTime, endTime, totalTime;
- startTime = System.currentTimeMillis();
QuorumSwarmConnect(quorumArray, ipfs);
- endTime = System.currentTimeMillis();
- totalTime = endTime - startTime;
- eventLogger.debug("Swarm Connect " + totalTime);
alphaSize = quorumArray.length() - 14;
@@ -372,9 +525,9 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}
-
syncDataTable(receiverDidIpfsHash, null);
- receiverPeerId = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash", receiverDidIpfsHash);
+ // receiverPeerId = getValues(DATA_PATH + "DataTable.json", "peerid", "didHash",
+ // receiverDidIpfsHash);
if (!receiverPeerId.equals("")) {
TokenSenderLogger.debug("Swarm connecting to " + receiverPeerId);
@@ -390,7 +543,8 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}
- String receiverWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "didHash", receiverDidIpfsHash);
+ String receiverWidIpfsHash = getValues(DATA_PATH + "DataTable.json", "walletHash", "didHash",
+ receiverDidIpfsHash);
if (!receiverWidIpfsHash.equals("")) {
nodeData(receiverDidIpfsHash, receiverWidIpfsHash, ipfs);
} else {
@@ -438,9 +592,22 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}
+
+ if (peerAuth == null) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ TokenSenderLogger.info("Receiver is unable to authenticate the sender!");
+ output.close();
+ input.close();
+ senderSocket.close();
+ senderMutex = false;
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Receiver is unable to authenticate the sender!");
+ return APIResponse;
-
- if (peerAuth != null && (!peerAuth.equals("200"))) {
+ }else if (peerAuth != null && (!peerAuth.equals("200"))) {
executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
TokenSenderLogger.info("Sender Data Not Available");
output.close();
@@ -455,12 +622,13 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}
-
+
String senderSign = getSignFromShares(pvt, authSenderByRecHash);
JSONObject senderDetails2Receiver = new JSONObject();
senderDetails2Receiver.put("sign", senderSign);
senderDetails2Receiver.put("tid", tid);
senderDetails2Receiver.put("comment", comment);
+
JSONObject partTokenChainArrays = new JSONObject();
for (int i = 0; i < partTokens.length(); i++) {
String chainContent = readFile(tokenChainPath.concat(partTokens.getString(i)).concat(".json"));
@@ -472,7 +640,8 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
} else {
JSONObject secondLastObject = chainArray.getJSONObject(chainArray.length() - 1);
secondLastObject.put("nextHash", calculateHash(tid, "SHA3-256"));
- newLastObject.put("previousHash", calculateHash(chainArray.getJSONObject(chainArray.length() - 1).getString("tid"), "SHA3-256"));
+ newLastObject.put("previousHash",
+ calculateHash(chainArray.getJSONObject(chainArray.length() - 1).getString("tid"), "SHA3-256"));
}
Double amount = formatAmount(amountLedger.getDouble(partTokens.getString(i)));
@@ -489,7 +658,7 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
partTokenChainArrays.put(partTokens.getString(i), chainArray);
}
-
+
JSONObject tokenDetails = new JSONObject();
tokenDetails.put("whole-tokens", wholeTokens);
tokenDetails.put("whole-tokenChains", wholeTokenChainHash);
@@ -498,31 +667,40 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
tokenDetails.put("part-tokenChains", partTokenChainArrays);
tokenDetails.put("sender", senderDidIpfsHash);
String doubleSpendString = tokenDetails.toString();
-
+
String doubleSpend = calculateHash(doubleSpendString, "SHA3-256");
writeToFile(LOGGER_PATH + "doubleSpend", doubleSpend, false);
TokenSenderLogger.debug("********Double Spend Hash*********: " + doubleSpend);
IPFSNetwork.addHashOnly(LOGGER_PATH + "doubleSpend", ipfs);
deleteFile(LOGGER_PATH + "doubleSpend");
-
-
+
JSONObject tokenObject = new JSONObject();
tokenObject.put("tokenDetails", tokenDetails);
tokenObject.put("previousSender", tokenPreviousSender);
+ tokenObject.put("positions", positionsArray);
tokenObject.put("amount", requestedAmount);
tokenObject.put("amountLedger", amountLedger);
-
-
-
- /**
- * Sending Token Details to Receiver
- * Receiver to authenticate Tokens (Double Spending, IPFS availability)
- */
- output.println(tokenObject);
+
+ if(Functions.multiplePinCheck(senderDidIpfsHash, tokenObject, ipfs) == 420) {
+ APIResponse.put("message", "Multiple Owners Found. Kindly re-initiate transaction");
+ return APIResponse;
+ }else {
+ TokenSenderLogger.debug("No Multiple Pins found, initating transcation");
+ }
+
+
+ /**
+ * Sending Token Details to Receiver
+ * Receiver to authenticate Tokens (Double Spending, IPFS availability)
+ */
+ TokenSenderLogger.debug("Token info sending is "+tokenObject.toString());
+ output.println(tokenObject);
+
String tokenAuth;
try {
tokenAuth = input.readLine();
+ TokenSenderLogger.debug("Token Auth Code: " + tokenAuth);
} catch (SocketException e) {
TokenSenderLogger.warn("Receiver " + receiverDidIpfsHash + " is unable to Respond! - Token Auth");
executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
@@ -538,70 +716,63 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}
- if (tokenAuth != null && (!tokenAuth.equals("200"))) {
+ if(tokenAuth == null) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ TokenSenderLogger.info("Receiver is unable to verify the tokens!");
+ output.close();
+ input.close();
+ senderSocket.close();
+ senderMutex = false;
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Receiver is unable to verify the tokens!");
+ return APIResponse;
+ }else if (tokenAuth != null && (tokenAuth.startsWith("4"))) {
switch (tokenAuth) {
case "420":
String doubleSpent = input.readLine();
String owners = input.readLine();
JSONArray ownersArray = new JSONArray(owners);
TokenSenderLogger.info("Multiple Owners for " + doubleSpent);
- APIResponse.put("message", "Multiple Owners for " + doubleSpent);
- APIResponse.put("Owners", ownersArray);
- removeToken();
+ TokenSenderLogger.info("Owners " + ownersArray);
+ TokenSenderLogger.info("Kindly re-initiate transaction");
+ APIResponse.put("message", "Multiple Owners for " + doubleSpent + " Owners: " + ownersArray
+ + ". Kindly re-initiate transaction");
break;
case "421":
- TokenSenderLogger.info("Consensus ID not unique");
- APIResponse.put("message", "Consensus ID not unique");
- removeToken();
+ TokenSenderLogger.info("Consensus ID not unique. Kindly re-initiate transaction");
+ APIResponse.put("message", "Consensus ID not unique. Kindly re-initiate transaction");
break;
case "422":
- TokenSenderLogger.info("Tokens Not Verified");
- APIResponse.put("message", "Tokens Not Verified");
- removeToken();
+ TokenSenderLogger.info("Tokens Not Verified. Kindly re-initiate transaction");
+ APIResponse.put("message", "Tokens Not Verified. Kindly re-initiate transaction");
break;
case "423":
- TokenSenderLogger.info("Broken Cheque Chain");
- APIResponse.put("message", "Broken Cheque Chain");
+ TokenSenderLogger.info("Broken Cheque Chain. Kindly re-initiate transaction");
+ APIResponse.put("message", "Broken Cheque Chain. Kindly re-initiate transaction");
break;
case "424":
- TokenSenderLogger.info("Token wholly spent already");
- APIResponse.put("message", "Token wholly spent already");
+ String invalidTokens = input.readLine();
+ JSONArray tokensArray = new JSONArray(invalidTokens);
+ TokenSenderLogger.info("Ownership Check Failed for " + tokensArray);
+ APIResponse.put("message", "Ownership Check Failed");
break;
- }
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
-
- output.close();
- input.close();
- senderSocket.close();
- senderMutex = false;
+ case "425":
+ TokenSenderLogger.info("Token wholly spent already. Kindly re-initiate transaction");
+ APIResponse.put("message", "Token wholly spent already. Kindly re-initiate transaction");
+ break;
- updateQuorum(quorumArray, null, false, type);
- APIResponse.put("did", senderDidIpfsHash);
- APIResponse.put("tid", tid);
- APIResponse.put("status", "Failed");
- return APIResponse;
- }
+ case "426":
+ TokenSenderLogger.info("Contains Invalid Tokens. Kindly check tokens in your wallet");
+ APIResponse.put("message",
+ "Contains Invalid Tokens. Kindly check tokens in your wallet");
+ break;
- JSONObject dataObject = new JSONObject();
- dataObject.put("tid", tid);
- dataObject.put("message", doubleSpendString);
- dataObject.put("receiverDidIpfs", receiverDidIpfsHash);
- dataObject.put("pvt", pvt);
- dataObject.put("senderDidIpfs", senderDidIpfsHash);
- dataObject.put("token", wholeTokens.toString());
- dataObject.put("alphaList", alphaPeersList);
- dataObject.put("betaList", betaPeersList);
- dataObject.put("gammaList", gammaPeersList);
-
-
- InitiatorProcedure.consensusSetUp(dataObject.toString(), ipfs, SEND_PORT + 100, alphaSize, "");
- TokenSenderLogger.debug("length on sender " + InitiatorConsensus.quorumSignature.length() + "response count " + InitiatorConsensus.quorumResponse);
- if (InitiatorConsensus.quorumSignature.length() < (minQuorum(alphaSize) + 2 * minQuorum(7))) {
- TokenSenderLogger.debug("Consensus Failed");
- senderDetails2Receiver.put("status", "Consensus Failed");
- output.println(senderDetails2Receiver);
+ }
executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
output.close();
input.close();
@@ -611,17 +782,48 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
APIResponse.put("did", senderDidIpfsHash);
APIResponse.put("tid", tid);
APIResponse.put("status", "Failed");
- APIResponse.put("message", "Transaction declined by Quorum");
return APIResponse;
-
}
-
- TokenSenderLogger.debug("Consensus Reached");
- senderDetails2Receiver.put("status", "Consensus Reached");
- senderDetails2Receiver.put("quorumsign", InitiatorConsensus.quorumSignature.toString());
-
- output.println(senderDetails2Receiver);
- TokenSenderLogger.debug("Quorum Signatures length " + InitiatorConsensus.quorumSignature.length());
+
+ TokenSenderLogger.debug("Token Auth Code: " + tokenAuth);
+
+ JSONObject dataObject = new JSONObject();
+ dataObject.put("tid", tid);
+ dataObject.put("message", doubleSpendString);
+ dataObject.put("receiverDidIpfs", receiverDidIpfsHash);
+ dataObject.put("pvt", pvt);
+ dataObject.put("senderDidIpfs", senderDidIpfsHash);
+ dataObject.put("token", wholeTokens.toString());
+ dataObject.put("alphaList", alphaPeersList);
+ dataObject.put("betaList", betaPeersList);
+ dataObject.put("gammaList", gammaPeersList);
+
+ InitiatorProcedure.consensusSetUp(dataObject.toString(), ipfs, SEND_PORT + 100, alphaSize, "");
+
+ if (InitiatorConsensus.quorumSignature.length() < (minQuorum(alphaSize) + 2 * minQuorum(7))) {
+ TokenSenderLogger.debug("Consensus Failed");
+ senderDetails2Receiver.put("status", "Consensus Failed");
+ output.println(senderDetails2Receiver);
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ output.close();
+ input.close();
+ senderSocket.close();
+ senderMutex = false;
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Transaction declined by Quorum");
+ return APIResponse;
+
+ }
+
+ TokenSenderLogger.debug("Consensus Reached");
+ senderDetails2Receiver.put("status", "Consensus Reached");
+ senderDetails2Receiver.put("quorumsign", InitiatorConsensus.quorumSignature.toString());
+
+ output.println(senderDetails2Receiver);
+ TokenSenderLogger.debug("Quorum Signatures length " + InitiatorConsensus.quorumSignature.length());
String signatureAuth;
try {
@@ -644,9 +846,9 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
TokenSenderLogger.info("signatureAuth : " + signatureAuth);
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
- if (signatureAuth != null && (!signatureAuth.equals("200"))) {
+ if (signatureAuth == null) {
executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
- TokenSenderLogger.info("Authentication Failed");
+ TokenSenderLogger.info("Receiver is unable to authenticate Sender!");
output.close();
input.close();
senderSocket.close();
@@ -655,18 +857,29 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
APIResponse.put("did", senderDidIpfsHash);
APIResponse.put("tid", tid);
APIResponse.put("status", "Failed");
- APIResponse.put("message", "Sender not authenticated");
+ APIResponse.put("message", "Receiver is unable to authenticate Sender!");
return APIResponse;
}
+ else if (signatureAuth != null && (!signatureAuth.equals("200"))) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ TokenSenderLogger.info("Authentication Failed!");
+ output.close();
+ input.close();
+ senderSocket.close();
+ senderMutex = false;
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Sender not authenticated");
+ return APIResponse;
- for (int i = 0; i < wholeTokens.length(); i++)
- unpin(String.valueOf(wholeTokens.get(i)), ipfs);
- repo(ipfs);
-
-
+ }
TokenSenderLogger.debug("Unpinned Tokens");
output.println("Unpinned");
+
+
String confirmation;
try {
confirmation = input.readLine();
@@ -685,7 +898,21 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}
- if (confirmation != null && (!confirmation.equals("Successfully Pinned"))) {
+ if (confirmation == null) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ TokenSenderLogger.info("Receiver is unable to Pin the tokens!");
+ output.close();
+ input.close();
+ senderSocket.close();
+ senderMutex = false;
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Receiver is unable to Pin the tokens!");
+ return APIResponse;
+
+ }else if (confirmation != null && (!confirmation.equals("Successfully Pinned"))) {
TokenSenderLogger.warn("Multiple Owners for the token");
executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
TokenSenderLogger.info("Tokens with multiple pins");
@@ -701,10 +928,13 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
return APIResponse;
}
- TokenSenderLogger.debug("3");
+
+ TokenSenderLogger.debug("3");
TokenSenderLogger.debug("Whole tokens: " + wholeTokens);
TokenSenderLogger.debug("Part tokens: " + partTokens);
output.println(InitiatorProcedure.essential);
+
+
String respAuth;
try {
respAuth = input.readLine();
@@ -719,13 +949,27 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
APIResponse.put("did", senderDidIpfsHash);
APIResponse.put("tid", "null");
APIResponse.put("status", "Failed");
- APIResponse.put("message", "Receiver " + receiverDidIpfsHash + "is unable to respond! - Share Confirmation");
+ APIResponse.put("message",
+ "Receiver " + receiverDidIpfsHash + "is unable to respond! - Share Confirmation");
return APIResponse;
}
-
- if (respAuth != null && (!respAuth.equals("Send Response"))) {
-
+ if(respAuth == null) {
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ TokenSenderLogger.info("Receiver is unable to complete the transaction!");
+ output.close();
+ input.close();
+ senderSocket.close();
+ senderMutex = false;
+ updateQuorum(quorumArray, null, false, type);
+ APIResponse.put("did", senderDidIpfsHash);
+ APIResponse.put("tid", tid);
+ APIResponse.put("status", "Failed");
+ APIResponse.put("message", "Receiver is unable to complete the transaction!");
+ return APIResponse;
+
+
+ }else if (respAuth != null && (!respAuth.equals("Send Response"))) {
executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
output.close();
input.close();
@@ -738,10 +982,14 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
APIResponse.put("message", "Receiver process not over");
TokenSenderLogger.info("Incomplete Transaction");
return APIResponse;
-
}
-
- TokenSenderLogger.debug("Operation over");
+
+ TokenSenderLogger.debug("Operation over");
+
+ for (int i = 0; i < wholeTokens.length(); i++)
+ unpin(String.valueOf(wholeTokens.get(i)), ipfs);
+ repo(ipfs);
+
Iterator keys = InitiatorConsensus.quorumSignature.keys();
JSONArray signedQuorumList = new JSONArray();
while (keys.hasNext())
@@ -753,21 +1001,8 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
APIResponse.put("quorumlist", signedQuorumList);
APIResponse.put("receiver", receiverDidIpfsHash);
APIResponse.put("totaltime", totalTime);
-
- updateQuorum(quorumArray, signedQuorumList, true, type);
-
-
- JSONArray allTokens = new JSONArray();
- for(int i = 0; i < wholeTokens.length(); i++)
- allTokens.put(wholeTokens.getString(i));
- for(int i = 0; i < partTokens.length(); i++)
- allTokens.put(partTokens.getString(i));
-
- TokenSenderLogger.debug("4");
- TokenSenderLogger.debug("All tokens: " + allTokens);
- TokenSenderLogger.debug("Whole tokens: " + wholeTokens);
- TokenSenderLogger.debug("Part tokens: " + partTokens);
-
+
+
JSONObject transactionRecord = new JSONObject();
transactionRecord.put("role", "Sender");
transactionRecord.put("tokens", allTokens);
@@ -782,16 +1017,13 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
requestedAmount = formatAmount(requestedAmount);
transactionRecord.put("amount-spent", requestedAmount);
-
JSONArray transactionHistoryEntry = new JSONArray();
transactionHistoryEntry.put(transactionRecord);
updateJSON("add", WALLET_DATA_PATH + "TransactionHistory.json", transactionHistoryEntry.toString());
- for (int i = 0; i < wholeTokens.length(); i++)
- Files.deleteIfExists(Paths.get(tokenPath + wholeTokens.get(i)));
-
for (int i = 0; i < wholeTokens.length(); i++) {
+ deleteFile(TOKENS_PATH.concat(wholeTokens.getString(i)));
Functions.updateJSON("remove", PAYMENTS_PATH.concat("BNK00.json"), wholeTokens.getString(i));
}
@@ -818,7 +1050,8 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
} else {
JSONObject secondLastObject = chainArray.getJSONObject(chainArray.length() - 1);
secondLastObject.put("nextHash", calculateHash(tid, "SHA3-256"));
- newLastObject.put("previousHash", calculateHash(chainArray.getJSONObject(chainArray.length() - 1).getString("tid"), "SHA3-256"));
+ newLastObject.put("previousHash",
+ calculateHash(chainArray.getJSONObject(chainArray.length() - 1).getString("tid"), "SHA3-256"));
}
Double amount = formatAmount(decimalAmount);
@@ -839,7 +1072,6 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
File chainFile = new File(TOKENCHAIN_PATH.concat(partTokens.getString(0)).concat(".json"));
chainFile.renameTo(new File(PART_TOKEN_CHAIN_PATH.concat(partTokens.getString(0)).concat(".json")));
-
File shiftedFile = new File(PAYMENTS_PATH.concat("ShiftedTokens.json"));
if (!shiftedFile.exists()) {
shiftedFile.createNewFile();
@@ -865,10 +1097,12 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
} else {
JSONObject secondLastObject = chainArray.getJSONObject(chainArray.length() - 1);
secondLastObject.put("nextHash", calculateHash(tid, "SHA3-256"));
- newLastObject.put("previousHash", calculateHash(chainArray.getJSONObject(chainArray.length() - 1).getString("tid"), "SHA3-256"));
+ newLastObject.put("previousHash", calculateHash(
+ chainArray.getJSONObject(chainArray.length() - 1).getString("tid"), "SHA3-256"));
}
- TokenSenderLogger.debug("Amount from ledger: " + formatAmount(amountLedger.getDouble(partTokens.getString(i))));
+ TokenSenderLogger
+ .debug("Amount from ledger: " + formatAmount(amountLedger.getDouble(partTokens.getString(i))));
Double amount = formatAmount(amountLedger.getDouble(partTokens.getString(i)));
newLastObject.put("senderSign", senderSign);
@@ -880,7 +1114,8 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
newLastObject.put("role", "Sender");
newLastObject.put("amount", amount);
chainArray.put(newLastObject);
- writeToFile(TOKENCHAIN_PATH.concat("PARTS/").concat(partTokens.getString(i)).concat(".json"), chainArray.toString(), false);
+ writeToFile(TOKENCHAIN_PATH.concat("PARTS/").concat(partTokens.getString(i)).concat(".json"),
+ chainArray.toString(), false);
TokenSenderLogger.debug("Checking Parts Token Balance ...");
Double availableParts = partTokenBalance(partTokens.getString(i));
@@ -897,12 +1132,12 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
deleteFile(PART_TOKEN_PATH.concat(partTokens.getString(i)));
}
}
- if(oldNew){
- String token = partTokens.getString(partTokens.length()-1);
+ if (oldNew) {
+ String token = partTokens.getString(partTokens.length() - 1);
String bnk = readFile(PAYMENTS_PATH.concat("BNK00.json"));
JSONArray bnkArray = new JSONArray(bnk);
- for(int i = 0; i < bnkArray.length(); i++){
- if(bnkArray.getJSONObject(i).getString("tokenHash").equals(token))
+ for (int i = 0; i < bnkArray.length(); i++) {
+ if (bnkArray.getJSONObject(i).getString("tokenHash").equals(token))
bnkArray.remove(i);
}
writeToFile(PAYMENTS_PATH.concat("BNK00.json"), bnkArray.toString(), false);
@@ -915,7 +1150,16 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
}
}
- //Populating data to explorer
+ TokenSenderLogger.info("Transaction Successful");
+ executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
+ updateQuorum(quorumArray, signedQuorumList, true, type);
+ output.close();
+ input.close();
+ senderSocket.close();
+ senderMutex = false;
+
+
+ // Populating data to explorer
if (!EXPLORER_IP.contains("127.0.0.1")) {
List tokenList = new ArrayList<>();
@@ -970,14 +1214,6 @@ public static JSONObject Send(String data, IPFS ipfs, int port) throws Exception
TokenSenderLogger.debug(response.toString());
}
-
-
- TokenSenderLogger.info("Transaction Successful");
- executeIPFSCommands(" ipfs p2p close -t /p2p/" + receiverPeerId);
- output.close();
- input.close();
- senderSocket.close();
- senderMutex = false;
return APIResponse;
}