Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
donlaiq authored Nov 29, 2018
1 parent 746e71a commit 8f3c050
Show file tree
Hide file tree
Showing 37 changed files with 3,178 additions and 0 deletions.
143 changes: 143 additions & 0 deletions src/main/java/com/donlaiq/command/NodeStarter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* It starts the node and verify its availability before to operate on it.
*
* @author donlaiq
*/

package com.donlaiq.command;
import java.io.File;
import java.util.Properties;
import java.util.Scanner;

public class NodeStarter {

// System command to start a Command Line Interface (sh for Linux, cmd.exe for Windows)
private String systemCommand;

// Parameter to run the system command (-c for Linux, /k for Windows)
private String systemCommandParameter;

// Absolute path to the directory holding the commands to interact with the node
private String nodeAbsolutePath;

// Command to start the node
private String startCommand;

// Command to make some request to the running node
private String cliCommand;

// Process running the node
private Process cryptoNodeProcess;

/*
* The commands are readed from an external file trying to make the application as extensible as possible.
*/
public NodeStarter()
{
try
{
Properties walletProperties = new Properties();
walletProperties.load(NodeStarter.class.getClassLoader().getResourceAsStream("resources/wallet.properties"));
nodeAbsolutePath = walletProperties.getProperty("node.path");
startCommand = walletProperties.getProperty("start.command");
cliCommand = walletProperties.getProperty("cli.command");
systemCommand = walletProperties.getProperty("system.command");
systemCommandParameter = walletProperties.getProperty("system.command.parameter");
}
catch(Exception e)
{
e.printStackTrace();
}

}


/*
* It tries to start the node.
*/
public boolean startNode()
{
try
{
File file = new File(nodeAbsolutePath + startCommand);
if(file.exists())
{
ProcessBuilder bitcoinzCli = new ProcessBuilder();
bitcoinzCli.command(systemCommand, systemCommandParameter, nodeAbsolutePath + startCommand);
cryptoNodeProcess = bitcoinzCli.start();
}
else
throw new Exception();
}
catch(Exception e)
{
if(cryptoNodeProcess != null)
{
cryptoNodeProcess.descendants().forEach(sub->sub.destroy());
cryptoNodeProcess.destroy();
}
System.out.println("The path to the command doesn't exists.");
return false;
}
return true;
}

/*
* There's a delay between the node is started and its availability to further use.
* It keeps trying till the node is usable.
*/
public void waitUntilNodeIsAvailable()
{
Process commandProcess = null;
Scanner in = null;
boolean isStarted = false;
try
{
File file = new File(nodeAbsolutePath + cliCommand);
if(file.exists())
{
ProcessBuilder pb = new ProcessBuilder(systemCommand, systemCommandParameter, nodeAbsolutePath + cliCommand + " getwalletinfo");

while(!isStarted)
{
commandProcess = pb.start();

in = new Scanner(commandProcess.getInputStream());
if(in.hasNextLine())
isStarted = true;

Thread.sleep(1000);
}
}
else
throw new Exception();
}
catch(Exception e)
{
System.out.println("The path to the command doesn't exists.");
}
finally
{
if(in != null)
in.close();
if(commandProcess != null)
{
commandProcess.descendants().forEach(sub->sub.destroy());
commandProcess.destroy();
}
}
}


/*
* When the application finishes, it tries to stop the node and release its resources.
*/
public void releaseResources()
{
if(cryptoNodeProcess != null)
{
cryptoNodeProcess.descendants().forEach(sub->sub.destroy());
cryptoNodeProcess.destroy();
}
}
}
198 changes: 198 additions & 0 deletions src/main/java/com/donlaiq/command/ProcessHandlerWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/**
* Wrapper to put together all the cli commands used by the application.
*
* @author donlaiq
*/

package com.donlaiq.command;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

import com.donlaiq.command.factory.ReturnSingleOutputProcessHandler;
import com.donlaiq.command.factory.ProcessHandler;
import com.donlaiq.command.factory.ReturnNothingProcessHandler;
import com.donlaiq.command.factory.TAddressBalanceProcessHandler;
import com.donlaiq.command.factory.TAddressFinderProcessHandler;
import com.donlaiq.command.factory.TotalBalanceProcessHandler;
import com.donlaiq.command.factory.TransactionListProcessHandler;
import com.donlaiq.command.factory.ZAddressFinderProcessHandler;
import com.donlaiq.controller.model.Address;
import com.donlaiq.controller.model.Transaction;

public class ProcessHandlerWrapper {

private Properties properties, commandProperties;

public ProcessHandlerWrapper()
{
try
{
properties = new Properties();
properties.load(TransactionListProcessHandler.class.getClassLoader().getResourceAsStream("resources/wallet.properties"));

commandProperties = new Properties();
commandProperties.load(TransactionListProcessHandler.class.getClassLoader().getResourceAsStream("resources/command.properties"));
}
catch(Exception e) {}
}

/*
* Get a list with the last 100 transactions, where some address of the wallet is associated with them.
*/
public List<Transaction> getTransactionList()
{
ProcessHandler processHandler = new TransactionListProcessHandler(" " + commandProperties.getProperty("list.transactions"));
return (List<Transaction>)processHandler.executeProcess();
}

/*
* Get the balances of the wallet. Two balances and the final sum of them, if there are more than one kind of address.
*/
public String[] getBalances()
{
ProcessHandler processHandler = new TotalBalanceProcessHandler(" " + commandProperties.getProperty("get.total.balance"));
return (String[])processHandler.executeProcess();
}

/*
* Get the list of transparent addresses.
*/
public List<Address> getTAddressesList()
{
ProcessHandler processHandler = new TAddressFinderProcessHandler(" " + commandProperties.getProperty("get.addresses.by.account"));
TAddressBalanceProcessHandler tAddressBalanceProcessHandler = new TAddressBalanceProcessHandler(" " + commandProperties.getProperty("t.address.balance"));
tAddressBalanceProcessHandler.setAllAddresses((List<String>)processHandler.executeProcess());
return (List<Address>)tAddressBalanceProcessHandler.executeProcess();
}

/*
* Get the list of private addresses.
*/
public List<Address> getZAddressesList()
{
List<Address> zAddressList = new ArrayList<Address>();
ProcessHandler processHandler = new ZAddressFinderProcessHandler(" " + commandProperties.getProperty("z.list.addresses"));
List<String> zAddresses = (List<String>)processHandler.executeProcess();
for(String address : zAddresses)
{
ProcessHandler zAddressBalanceProcessHandler = new ReturnSingleOutputProcessHandler(" " + commandProperties.getProperty("z.get.balance") + " " + address);
zAddressList.add(new Address(address, (String)zAddressBalanceProcessHandler.executeProcess()));
}
return zAddressList;
}

/*
* Get the private key of a transparent address.
*/
public String getTPrivateKey(String publicKey)
{
ProcessHandler processHandler = new ReturnSingleOutputProcessHandler(" " + commandProperties.getProperty("dump.priv.key") + " \"" + publicKey + "\"");
return (String) processHandler.executeProcess();
}

/*
* Get the private key of a private address.
*/
public String getZPrivateKey(String publicKey)
{
ProcessHandler processHandler = new ReturnSingleOutputProcessHandler(" " + commandProperties.getProperty("z.export.key") + " \"" + publicKey + "\"");
return (String) processHandler.executeProcess();
}

/*
* Import a transparent address to the wallet given its private key.
*/
public void importTPrivateKey(String privateKey, String rescan)
{
ProcessHandler processHandler = new ReturnNothingProcessHandler(" " + commandProperties.getProperty("import.priv.key") + " \"" + privateKey + "\" \"\" " + rescan);
processHandler.executeProcess();
}

/*
* Import a private address to the wallet given its private key.
*/
public void importZPrivateKey(String privateKey, String rescan)
{
ProcessHandler processHandler = new ReturnNothingProcessHandler(" " + commandProperties.getProperty("z.import.key") + " \"" + privateKey + "\" " + rescan);
processHandler.executeProcess();
}

/*
* Generate a new transparent address.
*/
public String newTAddress()
{
ProcessHandler processHandler = new ReturnSingleOutputProcessHandler(" " + commandProperties.getProperty("get.new.address"));
return (String)processHandler.executeProcess();
}

/*
* Generate a new private address.
*/
public String newZAddress()
{
ProcessHandler processHandler = new ReturnSingleOutputProcessHandler(" " + commandProperties.getProperty("z.get.new.address"));
return (String)processHandler.executeProcess();
}

/*
* It calls a RESTful API to get the amount of blocks of a full synchronized node.
*/
private String getTotalBlockCount()
{
String totalBlocks = null;
try
{
String uri = properties.getProperty("blockchain.explorer.blockcount.api");
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
Scanner scanner = new Scanner(connection.getInputStream());
if(scanner.hasNext())
totalBlocks = scanner.nextLine();
scanner.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return totalBlocks;
}

/*
* Returns the percentage of synchronization of the local node.
*/
public String getBlockchainPercentage()
{
String totalBlocks = getTotalBlockCount();

ProcessHandler processHandler = new ReturnSingleOutputProcessHandler(" " + commandProperties.getProperty("get.block.count"));
String totalDownloadedBlocks = (String)processHandler.executeProcess();

if(totalBlocks != null && totalDownloadedBlocks != null && !totalBlocks.equals("") && !totalDownloadedBlocks.equals(""))
{
Double percentage = (Double.valueOf(totalDownloadedBlocks) / Double.valueOf(totalBlocks)) * 100.0;
//System.out.println(percentage);
String stringPercentage = String.valueOf(percentage);
if(stringPercentage.length() >= 5)
return stringPercentage.substring(0, 5);
return stringPercentage;
}
return "";
}


/*
* It allows to send coins to another address.
*/
public void sendMoney(String addressFrom, String addressTo, String amount)
{
ProcessHandler processHandler = new ReturnNothingProcessHandler(" " + commandProperties.getProperty("send.many") + " \"" + addressFrom + "\" \"[{\\\"address\\\":\\\"" + addressTo + "\\\", \\\"amount\\\":" + amount + "}]\"");
processHandler.executeProcess();
}
}
Loading

0 comments on commit 8f3c050

Please sign in to comment.